SDL_test: format tracked allocation names during report generation

This commit is contained in:
Anonymous Maarten 2024-07-27 20:02:43 +02:00 committed by Anonymous Maarten
parent bc8f4a1d0b
commit 4c00433b69

View File

@ -56,7 +56,6 @@ typedef struct SDL_tracked_allocation
void *mem;
size_t size;
Uint64 stack[MAXIMUM_TRACKED_STACK_DEPTH];
char stack_names[MAXIMUM_TRACKED_STACK_DEPTH][256];
struct SDL_tracked_allocation *next;
} SDL_tracked_allocation;
@ -130,15 +129,11 @@ static void SDL_TrackAllocation(void *mem, size_t size)
stack_index = 0;
while (unw_step(&cursor) > 0) {
unw_word_t offset, pc;
char sym[236];
unw_word_t pc;
unw_get_reg(&cursor, UNW_REG_IP, &pc);
entry->stack[stack_index] = pc;
if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) == 0) {
SDL_snprintf(entry->stack_names[stack_index], sizeof(entry->stack_names[stack_index]), "%s+0x%llx", sym, (unsigned long long)offset);
}
++stack_index;
if (stack_index == SDL_arraysize(entry->stack)) {
@ -156,28 +151,7 @@ static void SDL_TrackAllocation(void *mem, size_t size)
count = SDL_min(count, MAXIMUM_TRACKED_STACK_DEPTH);
for (i = 0; i < count; i++) {
char symbol_buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)];
PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)symbol_buffer;
DWORD64 dwDisplacement = 0;
DWORD lineColumn = 0;
pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
pSymbol->MaxNameLen = MAX_SYM_NAME;
IMAGEHLP_LINE64 line;
line.SizeOfStruct = sizeof(line);
entry->stack[i] = (Uint64)(uintptr_t)frames[i];
if (dyn_dbghelp.module) {
if (!dyn_dbghelp.pSymFromAddr(GetCurrentProcess(), (DWORD64)(uintptr_t)frames[i], &dwDisplacement, pSymbol)) {
SDL_strlcpy(pSymbol->Name, "???", MAX_SYM_NAME);
dwDisplacement = 0;
}
if (!dyn_dbghelp.pSymGetLineFromAddr64(GetCurrentProcess(), (DWORD64)(uintptr_t)frames[i], &lineColumn, &line)) {
line.FileName = "";
line.LineNumber = 0;
}
SDL_snprintf(entry->stack_names[i], sizeof(entry->stack_names[i]), "%s+0x%I64x %s:%u", pSymbol->Name, dwDisplacement, line.FileName, (Uint32)line.LineNumber);
}
}
}
#endif /* HAVE_LIBUNWIND_H */
@ -369,10 +343,46 @@ void SDLTest_LogAllocations(void)
ADD_LINE();
/* Start at stack index 1 to skip our tracking functions */
for (stack_index = 1; stack_index < SDL_arraysize(entry->stack); ++stack_index) {
char stack_entry_description[256] = "???";
if (!entry->stack[stack_index]) {
break;
}
(void)SDL_snprintf(line, sizeof(line), "\t0x%" SDL_PRIx64 ": %s\n", entry->stack[stack_index], entry->stack_names[stack_index]);
#ifdef HAVE_LIBUNWIND_H
{
#ifdef unw_get_proc_name_by_ip
unw_word_t offset = 0;
char name[256] = "???";
unw_get_proc_name_by_ip(unw_local_addr_space, entry->stack[stack_index], name, sizeof(name), &offset, NULL);
(void)SDL_snprintf(stack_entry_description, sizeof(stack_entry_description), "%s+0x%llx", name, (long long unsigned int)offset);
#endif
}
#elif defined(SDL_PLATFORM_WIN32)
{
DWORD64 dwDisplacement = 0;
char symbol_buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)];
PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)symbol_buffer;
DWORD lineColumn = 0;
pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
pSymbol->MaxNameLen = MAX_SYM_NAME;
IMAGEHLP_LINE64 dbg_line;
dbg_line.SizeOfStruct = sizeof(dbg_line);
if (dyn_dbghelp.module) {
if (!dyn_dbghelp.pSymFromAddr(GetCurrentProcess(), entry->stack[stack_index], &dwDisplacement, pSymbol)) {
SDL_strlcpy(pSymbol->Name, "???", MAX_SYM_NAME);
dwDisplacement = 0;
}
if (!dyn_dbghelp.pSymGetLineFromAddr64(GetCurrentProcess(), (DWORD64)entry->stack[stack_index], &lineColumn, &dbg_line)) {
dbg_line.FileName = "";
dbg_line.LineNumber = 0;
}
}
SDL_snprintf(stack_entry_description, sizeof(stack_entry_description), "%s+0x%I64x %s:%u", pSymbol->Name, dwDisplacement, dbg_line.FileName, (Uint32)dbg_line.LineNumber);
}
#endif
(void)SDL_snprintf(line, sizeof(line), "\t0x%" SDL_PRIx64 ": %s\n", entry->stack[stack_index], stack_entry_description);
ADD_LINE();
}
total_allocated += entry->size;