Retrieving Symbol Information by Address

Debug Help Library

Retrieving Symbol Information by Address

The following code demonstrates how to call the SymFromAddr function. This function fills in a SYMBOL_INFO structure. Because the name is variable in length, you must supply a buffer that is large enough to hold the name stored at the end of the SYMBOL_INFO structure. Also, the MaxNameLen member must be set to the number of bytes reserved for the name. In this example, dwAddress is the address to be mapped to a symbol. The SymFromAddr function will store an offset to the beginning of the symbol to the address in dwDisplacement. The example assumes you have initialized the symbol handler using the code in Initializing the Symbol Handler.

DWORD  dwAddress;
DWORD  dwDisplacement;

ULONG64 buffer[(sizeof(SYMBOL_INFO) +
    MAX_SYM_NAME*sizeof(TCHAR) +
    sizeof(ULONG64) - 1) /
    sizeof(ULONG64)];
PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;

pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
pSymbol->MaxNameLen = MAX_SYM_NAME;

if (SymFromAddr(hProcess, dwAddress, &dwDisplacement, pSymbol))
{
// SymFromAddr returned success
}
else
{
    // SymFromAddr failed
    error = GetLastError();
    printf("SymFromAddr returned error : %d\n", error);
}

To retrieve the source code line number for a specified address, an application can use SymGetLineFromAddr64. This function requires a pointer to an IMAGEHLP_LINE64 structure to receive the source file name and line number corresponding to a specified code address. Note that the symbol handler can retrieve line number information only when SYMOPT_LOAD_LINES is set using the SymSetOptions function. This option must be set before loading the module. The dwAddress parameter contains the code address for which the source file name and line number will be located.

DWORD64  dwAddress;
DWORD  dwDisplacement;
IMAGEHLP_LINE64 line;

SymSetOptions(SYMOPT_LOAD_LINES);

line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);

if (SymGetLineFromAddr64(hProcess, dwAddress, &dwDisplacement, &line))
{
    // SymGetLineFromAddr64 returned success
}
else
{
    // SymGetLineFromAddr64 failed
    error = GetLastError();
    printf("SymGetLineFromAddr64 returned error : %d\n", error);
}

Send comments about this topic to Microsoft

Build date: 9/25/2007

© 2007 Microsoft Corporation. All rights reserved.