tcmalloc: use relative addresses with the windows addr2line wrapper

Modifies the Windows addr2line wrapper to expect addresses relative to
DllBase to better simulate how addr2line works with modules in Linux.

Windows DLLs have a concept of "default load address" which hints to the OS
where to load the binary image after relocation. The dbghelp.dll
symbolization library will load the module at this location in the virtual
address space meaning the caller of these functions would need to be aware
of the base address. This makes things unnecessarily complex in the face of
ASLR and also diverges from the behavior of addr2line when used with linux-
style DSOs. This CL simply adds the module base address to the incoming
addresses, thereby making the input relative addresses for the module which
both is easier to use and lines up better with linux's addr2line behavior.

These changes were made originally as part of CL
https://codereview.chromium.org/2730473002.

BUG=724399,b:70905156

Change-Id: I0abe9e0c380e7e60ae29a11021bb805b31718d08
This commit is contained in:
Gabriel Marin 2018-07-19 11:10:31 -07:00 committed by Aliaksey Kandratsenka
parent d8f8d1cced
commit e42bfc8c06

View File

@ -142,7 +142,7 @@ int main(int argc, char *argv[]) {
/* GNU addr2line seems to just do a strtol and ignore any
* weird characters it gets, so we will too.
*/
unsigned __int64 addr = _strtoui64(buf, NULL, 16);
unsigned __int64 reladdr = _strtoui64(buf, NULL, 16);
ULONG64 buffer[(sizeof(SYMBOL_INFO) +
MAX_SYM_NAME*sizeof(TCHAR) +
sizeof(ULONG64) - 1)
@ -150,17 +150,25 @@ int main(int argc, char *argv[]) {
PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;
IMAGEHLP_LINE64 line;
DWORD dummy;
// Just ignore overflow. In an overflow scenario, the resulting address
// will be lower than module_base which hasn't been mapped by any prior
// SymLoadModuleEx() command. This will cause SymFromAddr() and
// SymGetLineFromAddr64() both to return failures and print the correct
// ?? and ??:0 message variant.
ULONG64 absaddr = reladdr + module_base;
pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
pSymbol->MaxNameLen = MAX_SYM_NAME;
if (print_function_name) {
if (SymFromAddr(process, (DWORD64)addr, NULL, pSymbol)) {
if (SymFromAddr(process, (DWORD64)absaddr, NULL, pSymbol)) {
printf("%s\n", pSymbol->Name);
} else {
printf("??\n");
}
}
line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
if (SymGetLineFromAddr64(process, (DWORD64)addr, &dummy, &line)) {
if (SymGetLineFromAddr64(process, (DWORD64)absaddr, &dummy, &line)) {
printf("%s:%d\n", line.FileName, (int)line.LineNumber);
} else {
printf("??:0\n");