void printStack(void) {
HANDLE process = GetCurrentProcess();
SymInitialize( process, NULL, TRUE );
SymSetOptions(SYMOPT_LOAD_LINES);
void *stack[64];
unsigned short frames = CaptureStackBackTrace( 0, 64, stack, NULL );
SYMBOL_INFO *symbol = (SYMBOL_INFO*) calloc(sizeof(SYMBOL_INFO) + 256 * sizeof(char), 1);
symbol->MaxNameLen = 255;
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
IMAGEHLP_LINE64 *line = NULL;
DWORD lastError = 0;
DWORD displacement;
printf("Backtrace\n");
for(int i = 0; i < frames; i++) {
SymFromAddr(process, (DWORD64)(stack[i]), 0, symbol);
printf("\t %i: %s\n", frames - i - 1, symbol->Name);
line = (IMAGEHLP_LINE64 *)malloc(sizeof(IMAGEHLP_LINE64));
line->SizeOfStruct = sizeof(IMAGEHLP_LINE64);
if (SymGetLineFromAddr64(process, (DWORD64)(stack[i]), &displacement, line)) {
printf("%s %i\n", line->FileName, line->LineNumber);
} else {
printf("failed\n");
}
lastError = GetLastError();
printf("errcode: %i\n", lastError);
free(line);
line = NULL;
}
free(symbol);
}
Backtrace
5: _printStack
failed
errcode: 487
4: _main
failed
errcode: 487
3: __scrt_common_main_seh
failed
errcode: -1073741819
2: BaseThreadInitThunk
failed
errcode: 487
1: RtlGetAppContainerNamedObjectPath
failed
errcode: 487
0: RtlGetAppContainerNamedObjectPath
failed
errcode: 487
I am trying to extract line numbers and file names from SymGetLineFromAddr64 but the address I am inputting is invalid. I am using (DWORD64)(stack[i]) for the address. What address do I use to be able to extract line information?