Help with dbghelp/imagehlp

Started by
5 comments, last by the_edd 11 years, 8 months ago
I have a case where SymGetSymFromAddr() says it's succeeding, but in fact it is returning an empty string for the symbol.

Here is my test case:
#include <windows.h>
#include <imagehlp.h>

#include <cassert>
#include <cstddef>
#include <cstring>
#include <iostream>

struct symbol_buffer
{
static const std::size_t max_sym_length = 4096;

symbol_buffer()
{
std::memset(buffer, 0x00, sizeof buffer);

IMAGEHLP_SYMBOL *sym = get();

sym->SizeOfStruct = sizeof(IMAGEHLP_SYMBOL);
sym->MaxNameLength = max_sym_length;
}

IMAGEHLP_SYMBOL *get() { return reinterpret_cast<IMAGEHLP_SYMBOL *>(buffer); }

union
{
DWORD for_alignment;
unsigned char buffer[sizeof(IMAGEHLP_SYMBOL) + max_sym_length];
};
};

void print_function_name(UINT_PTR program_counter, HANDLE process)
{
const char *symbol_ptr = "???";

symbol_buffer sym_buff;

IMAGEHLP_SYMBOL *symbol = sym_buff.get();
//symbol->Address = program_counter; // don't think this is needed, but doesn't make any difference

if (SymGetSymFromAddr(process, program_counter, 0, symbol))
symbol_ptr = symbol->Name;

if (symbol_ptr)
std::cout << "function: '" << symbol_ptr << "'\n";
}

const char *splendid() { return "splendid"; }

int main()
{
const HANDLE process = GetCurrentProcess();

//SymSetOptions(
// SYMOPT_ALLOW_ABSOLUTE_SYMBOLS |
// SYMOPT_ALLOW_ZERO_ADDRESS |
// SymGetOptions()
//);

if (SymInitialize(process, 0, TRUE) == 0)
return 1;

const char *(*fp)() = &splendid;
UINT_PTR fp_val = 0;

assert(sizeof fp_val == sizeof fp);
std::memcpy(&fp_val, &fp, sizeof fp);

print_function_name(fp_val, process);

SymCleanup(process);

return 0;
}


I am building with the Visual C++ 2010 toolchain as follows:


P:\guff>cl /nologo /EHsc /W3 /WX /GR /Zi /GS /arch:SSE2 /MTd /Oy- /c symbols.cpp /Fdsymbols.obj.pdb /Fosymbols.obj
symbols.cpp

P:\guff>link /nologo /incremental /WX symbols.obj imagehlp.lib /debug /pdb:symbols.exe.pdb /out:symbols.exe


When running the code, the output tells me that the function name is the empty string, whereas I would have expected something like "splendid", or "splendid(void)", etc:


P:\guff>symbols
function: ''


When stepping though the code in the Visual C++ debugger, the watch window quite happily tells me that the 'fp' variable in main() refers to "splendid(void)" as shown in the attached screenshot, so it seems the necessary symbolic information is present.

Even more curiously, if I enable global optimization by adding /GL compiler switch and replacing the /incremental linker switch with /LTCG, I get the kind of output I'd expect:


P:\guff>symbols
function: 'splendid'


I have also tried SymFromAddr() instead of SymGetSymFromAddr(), and changing the options in the SymInitialize() (see commented-out code in main()), but the same behaviour persists.

Can anybody see what I'm doing wrong?
Advertisement
I can't reproduce your problem. With MSVC 2010 and the exact same compiler and linker arguments I get function: 'ILT+230(?splendidYAPBDXZ)' as output. Do you have the service pack and all the hotfixes installed?

Probably not related to your problem: is there any reason that you aren't using IMAGEHLP_SYMBOL_PACKAGE instead of your symbol_buffer struct?
Make sure you actually have a recent version of dbghelp.dll present. Some older versions can be flaky, especially on 64-bit Windows.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Thanks chaps. Good to know I haven't done anything obviously wrong. I'm struggling to find an updated dbghelp.dll, though. I've installed VS2010 SP1 and the latest debugging tools, but I still have a dll dated from 2008. I'm on an old 32bit Windows XP, so perhaps that's all I should hope for.


Do you have the service pack and all the hotfixes installed?

Is there a particularly relevant hotfix that you have in mind?


Probably not related to your problem: is there any reason that you aren't using IMAGEHLP_SYMBOL_PACKAGE instead of your symbol_buffer struct?
[/quote]
Just ignorance. OTOH, it doesn't seem to be defined in the MinGW headers, so I'll have to stick the hand-rolled structure for the time being.

Is there a particularly relevant hotfix that you have in mind?

Not really, just trying to think of something that might help.

Thanks chaps. Good to know I haven't done anything obviously wrong. I'm struggling to find an updated dbghelp.dll, though. I've installed VS2010 SP1 and the latest debugging tools, but I still have a dll dated from 2008. I'm on an old 32bit Windows XP, so perhaps that's all I should hope for.

If you've installed the Debugging Tools, the updated dbghelp is in the same directory as WinDbg and them. You have to put it next to your app, it doesn't update the system32 one. You also need to link to dbghelp instead of imagehlp. Imagehlp is a known dll so it and its dependencies are loaded from system32 regardless.

The problem with the 'no-name incremental link' is that old versions of the DIA code (used in 2000/XP versions of dbghelp.dll) treated the incremental link table (the ILT in SiCrane's result) stubs as unnamed Thunk symbols. The non incremental version just has function symbols, which obviously do have names. The newer versions interpret the ILT as Public Symbols, which do have a name.

It's anybody's guess as to whether the old versions had a pdb parsing bug in regards to the ILT symbols, or whether modern PDB's have an incompatible format with that version of the parsing code.

If you've installed the Debugging Tools, the updated dbghelp is in the same directory as WinDbg and them. You have to put it next to your app, it doesn't update the system32 one. You also need to link to dbghelp instead of imagehlp. Imagehlp is a known dll so it and its dependencies are loaded from system32 regardless.

Splendid! :) Everything's working now, thanks.

This topic is closed to new replies.

Advertisement