Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 60dcd3b

Browse files
lucenticusadityamandaleeka
authored andcommitted
[SOS] [Linux] Initial support of reading source and line number by native offset (#6010)
* Initial support of reading source and line number by native offset using portable PDB reader
1 parent 84aadf1 commit 60dcd3b

File tree

2 files changed

+80
-7
lines changed

2 files changed

+80
-7
lines changed

src/ToolBox/SOS/Strike/util.cpp

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ void *SymbolReader::coreclrLib;
5353
ResolveSequencePointDelegate SymbolReader::resolveSequencePointDelegate;
5454
LoadSymbolsForModuleDelegate SymbolReader::loadSymbolsForModuleDelegate;
5555
GetLocalVariableName SymbolReader::getLocalVariableNameDelegate;
56+
GetLineByILOffsetDelegate SymbolReader::getLineByILOffsetDelegate;
5657
#endif // !FEATURE_PAL
5758

5859
const char * const CorElementTypeName[ELEMENT_TYPE_MAX]=
@@ -5579,8 +5580,6 @@ NoOutputHolder::~NoOutputHolder()
55795580
// Code to support mapping RVAs to managed code line numbers.
55805581
//
55815582

5582-
#ifndef FEATURE_PAL
5583-
55845583
//
55855584
// This function retrieves ImageInfo related to the module
55865585
// containing the addressed passed in "Base".
@@ -5715,8 +5714,10 @@ GetMethodInstanceTokenAndScope(
57155714
{
57165715
return Status;
57175716
}
5717+
#ifndef FEATURE_PAL
57185718

57195719
Status = FindClrModuleImage(Module, Image);
5720+
#endif //FEATURE_PAL
57205721

57215722
Module->Release();
57225723
return Status;
@@ -5852,8 +5853,6 @@ ConvertNativeToIlOffset(
58525853
return Status;
58535854
}
58545855

5855-
#endif // FEATURE_PAL
5856-
58575856
// Based on a native offset, passed in the first argument this function
58585857
// identifies the corresponding source file name and line number.
58595858
HRESULT
@@ -5866,7 +5865,6 @@ GetLineByOffset(
58665865
HRESULT hr = S_OK;
58675866
ULONG64 displacement = 0;
58685867

5869-
#ifndef FEATURE_PAL
58705868
// first let's try it the hard way, this will work with the public debuggers
58715869
{
58725870
ImageInfo Image = {0};
@@ -5875,12 +5873,14 @@ GetLineByOffset(
58755873
ULONG32 MethodOffs;
58765874
ULONG64 IlOffset;
58775875

5876+
#ifndef FEATURE_PAL
58785877
ToRelease<IDebugSymbols3> spSym3(NULL);
58795878
if (FAILED(g_ExtSymbols->QueryInterface(__uuidof(IDebugSymbols3), (void**)&spSym3)))
58805879
{
58815880
hr = E_FAIL;
58825881
goto fallback;
58835882
}
5883+
#endif //!FEATURE_PAL
58845884

58855885
// find the image, method token and IL offset that correspond
58865886
// to "Offset"
@@ -5889,6 +5889,8 @@ GetLineByOffset(
58895889
{
58905890
goto fallback;
58915891
}
5892+
5893+
#ifndef FEATURE_PAL
58925894
modBase = Image.modBase;
58935895
DEBUG_MODULE_AND_ID id;
58945896
DEBUG_SYMBOL_ENTRY symInfo;
@@ -5900,7 +5902,11 @@ GetLineByOffset(
59005902
}
59015903

59025904
IlOffset = symInfo.Offset + MethodOffs;
5903-
5905+
#else
5906+
// Source lines with 0xFEEFEE markers are filtered out on the managed side.
5907+
const String &moduleName = ModuleNameFromIP(TO_CDADDR(Offset));
5908+
return SymbolReader::GetLineByILOffset(moduleName.c_str(), MethodToken, MethodOffs, pLinenum, lpszFileName, cbFileName);
5909+
#endif //!FEATURE_PAL
59045910
//
59055911
// Source maps for managed code can end
59065912
// up with special 0xFEEFEE markers that
@@ -5936,7 +5942,6 @@ GetLineByOffset(
59365942
}
59375943

59385944
fallback:
5939-
#endif // FEATURE_PAL
59405945
return g_ExtSymbols->GetLineByOffset(
59415946
Offset,
59425947
pLinenum,
@@ -6291,6 +6296,44 @@ HRESULT SymbolReader::LoadCoreCLR()
62916296
IfFailRet(CreateDelegate(hostHandle, domainId, SymbolReaderDllName,
62926297
SymbolReaderClassName, "GetLocalVariableName",
62936298
(void **)&getLocalVariableNameDelegate));
6299+
IfFailRet(CreateDelegate(hostHandle, domainId, SymbolReaderDllName,
6300+
SymbolReaderClassName, "GetLineByILOffset",
6301+
(void **)&getLineByILOffsetDelegate));
6302+
return Status;
6303+
}
6304+
HRESULT SymbolReader::GetLineByILOffset(__in_z const char* szModuleName, mdMethodDef MethodToken,
6305+
ULONG64 IlOffset, ___out ULONG *pLinenum,
6306+
__out_ecount(cbFileName) LPSTR lpszFileName,
6307+
___in ULONG cbFileName)
6308+
{
6309+
HRESULT Status = S_OK;
6310+
6311+
if (getLineByILOffsetDelegate == nullptr)
6312+
{
6313+
Status = SymbolReader::LoadCoreCLR();
6314+
}
6315+
if (Status != S_OK)
6316+
{
6317+
return Status;
6318+
}
6319+
BSTR wszFileName = SysAllocStringLen(0, cbFileName);
6320+
if (wszFileName == nullptr)
6321+
{
6322+
return E_OUTOFMEMORY;
6323+
}
6324+
if (SUCCEEDED(getLineByILOffsetDelegate(szModuleName, MethodToken, IlOffset, pLinenum, &wszFileName)))
6325+
{
6326+
WideCharToMultiByte(CP_ACP, 0, wszFileName, (int) (_wcslen(wszFileName) + 1),
6327+
lpszFileName, cbFileName, NULL, NULL);
6328+
if (*pLinenum == 0)
6329+
{
6330+
*pLinenum = -1;
6331+
Status = E_FAIL;
6332+
}
6333+
6334+
}
6335+
SysFreeString(wszFileName);
6336+
62946337
return Status;
62956338
}
62966339
#endif //FEATURE_PAL
@@ -6674,6 +6717,30 @@ WString GetFrameFromAddress(TADDR frameAddr, IXCLRDataStackWalk *pStackWalk, BOO
66746717
return frameOutput;
66756718
}
66766719

6720+
String ModuleNameFromIP(CLRDATA_ADDRESS ip)
6721+
{
6722+
CLRDATA_ADDRESS mdesc = 0;
6723+
if (SUCCEEDED(g_sos->GetMethodDescPtrFromIP(ip, &mdesc)))
6724+
{
6725+
DacpMethodDescData mdescData;
6726+
DacpModuleData moduleData;
6727+
if (SUCCEEDED(mdescData.Request(g_sos, mdesc)))
6728+
{
6729+
if (SUCCEEDED(moduleData.Request(g_sos, mdescData.ModulePtr)))
6730+
{
6731+
ArrayHolder<WCHAR> wszModuleName = new WCHAR[MAX_LONGPATH+1];
6732+
if (SUCCEEDED(g_sos->GetPEFileName(moduleData.File, MAX_LONGPATH, wszModuleName, NULL)))
6733+
{
6734+
ArrayHolder<char> szModuleName = new char[MAX_LONGPATH+1];
6735+
WideCharToMultiByte(CP_ACP, 0, wszModuleName, (int) (_wcslen(wszModuleName) + 1), szModuleName, mdNameLen, NULL, NULL);
6736+
return szModuleName.GetPtr();
6737+
}
6738+
}
6739+
}
6740+
}
6741+
return "";
6742+
}
6743+
66776744
WString MethodNameFromIP(CLRDATA_ADDRESS ip, BOOL bSuppressLines, BOOL bAssemblyName, BOOL bDisplacement)
66786745
{
66796746
ULONG linenum;

src/ToolBox/SOS/Strike/util.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2359,6 +2359,7 @@ class PERvaMemoryReader : IDiaReadExeAtRVACallback
23592359
typedef int (*ResolveSequencePointDelegate)(const char*, const char*, unsigned int, unsigned int*, unsigned int*);
23602360
typedef int (*LoadSymbolsForModuleDelegate)(const char*);
23612361
typedef int (*GetLocalVariableName)(const char*, int, int, BSTR*);
2362+
typedef int (*GetLineByILOffsetDelegate)(const char*, mdMethodDef, ULONG64, ULONG *, BSTR*);
23622363
static const char *SymbolReaderDllName = "System.Diagnostics.Debug.SymbolReader";
23632364
static const char *SymbolReaderClassName = "System.Diagnostics.Debug.SymbolReader.SymbolReader";
23642365
#endif //FEATURE_PAL
@@ -2373,6 +2374,8 @@ class SymbolReader
23732374
static ResolveSequencePointDelegate resolveSequencePointDelegate;
23742375
static LoadSymbolsForModuleDelegate loadSymbolsForModuleDelegate;
23752376
static GetLocalVariableName getLocalVariableNameDelegate;
2377+
static GetLineByILOffsetDelegate getLineByILOffsetDelegate;
2378+
23762379
#endif
23772380

23782381
private:
@@ -2393,6 +2396,8 @@ class SymbolReader
23932396
#ifdef FEATURE_PAL
23942397
static HRESULT LoadCoreCLR();
23952398
static bool SymbolReaderDllExists();
2399+
static HRESULT GetLineByILOffset(__in_z const char* szModuleName, mdMethodDef MethodToken, ULONG64 IlOffset, ___out ULONG *pLinenum,
2400+
__out_ecount(cbFileName) LPSTR lpszFileName, ___in ULONG cbFileName);
23962401
#endif //FEATURE_PAL
23972402
HRESULT LoadSymbols(IMetaDataImport * pMD, ICorDebugModule * pModule);
23982403
HRESULT LoadSymbols(IMetaDataImport * pMD, ULONG64 baseAddress, __in_z WCHAR* pModuleName, BOOL isInMemory);
@@ -2704,6 +2709,7 @@ typedef struct _CROSS_PLATFORM_CONTEXT {
27042709

27052710
WString BuildRegisterOutput(const SOSStackRefData &ref, bool printObj = true);
27062711
WString MethodNameFromIP(CLRDATA_ADDRESS methodDesc, BOOL bSuppressLines = FALSE, BOOL bAssemblyName = FALSE, BOOL bDisplacement = FALSE);
2712+
String ModuleNameFromIP(CLRDATA_ADDRESS ip);
27072713
HRESULT GetGCRefs(ULONG osID, SOSStackRefData **ppRefs, unsigned int *pRefCnt, SOSStackRefError **ppErrors, unsigned int *pErrCount);
27082714
WString GetFrameFromAddress(TADDR frameAddr, IXCLRDataStackWalk *pStackwalk = NULL, BOOL bAssemblyName = FALSE);
27092715

0 commit comments

Comments
 (0)