Automatic acquisition source and line number from the symbol name and offset

In the past, unmanaged code developers have to code very hard to offset associate it with the source file name and line number. One method involves generating .cod files (programs list) for each module, and then put the contents of the instruction shift amount compared .cod files.
For example, if having received from a client to the error BatteryMeter! TemperatureAndBatteryUpdaterThread + 0xd0, a .cod file may be returned BatteryMeter.exe find TemperatureAndBatteryUpdaterThread code list, and then find the source line at (or near) the offset of 0xd0.
This process can be automated. A few days ago someone asked me if I still need to use the .cod file, the answer is no. If you start WinDbg, you only need to "File> Open dump file", enter your .exe or .dll file name as a dump, and issue the ln command as follows:

0:000> ln BatteryMeter!TemperatureAndBatteryUpdaterThread+0xd0
d:\dev\batterymeter\batterymeterdlg.cpp(58)

If you do not have WinDbg and want to do this automatically (possibly through a script), you can use DbgHelp API load the appropriate module symbols, then look for the symbol name and source information. Functions involved are SymLoadModule64, SymFromName and SymGetLineFromAddr64, the program generated is not more than 100 lines of code:

 

DWORD displacement;
IMAGEHLP_LINE64 line;
RtlZeroMemory(&line, sizeof(line));
line.SizeOfStruct = sizeof(line);
if (!SymGetLineFromAddr64(hProcess, symbolAddress, &displacement, &line))
{
    printf("*** Error retrieving source line for %s: 0x%x\n",
        argv[1], GetLastError());
    return 1;
}
printf("%s [0x%I64x] = %s line %d (+0x%x)\n", argv[1], symbolAddress,
    line.FileName, line.LineNumber, displacement);

 

Guess you like

Origin www.cnblogs.com/yilang/p/12012507.html