Skip to content

Commit

Permalink
SDL_test: do late addres to name conversion, if libunwind supports it
Browse files Browse the repository at this point in the history
  • Loading branch information
madebr committed Aug 31, 2024
1 parent dae4ce9 commit 38d2a8a
Showing 1 changed file with 44 additions and 4 deletions.
48 changes: 44 additions & 4 deletions src/test/SDL_test_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
#ifdef HAVE_LIBUNWIND_H
#define UNW_LOCAL_ONLY
#include <libunwind.h>
#ifndef unw_get_proc_name_by_ip
#define SDLTEST_UNWIND_NO_PROC_NAME_BY_IP
static SDL_bool s_unwind_symbol_names = SDL_TRUE;
#endif
#endif

/* This is a simple tracking allocator to demonstrate the use of SDL's
Expand All @@ -43,8 +47,10 @@ typedef struct SDL_tracked_allocation
void *mem;
size_t size;
Uint64 stack[10];
char stack_names[10][256];
struct SDL_tracked_allocation *next;
#ifdef SDLTEST_UNWIND_NO_PROC_NAME_BY_IP
char stack_names[10][256];
#endif
} SDL_tracked_allocation;

static SDLTest_Crc32Context s_crc32_context;
Expand Down Expand Up @@ -121,15 +127,20 @@ static void SDL_TrackAllocation(void *mem, size_t size)

stack_index = 0;
while (unw_step(&cursor) > 0) {
unw_word_t offset, pc;
unw_word_t pc;
#ifdef SDLTEST_UNWIND_NO_PROC_NAME_BY_IP
unw_word_t offset;
char sym[236];
#endif

unw_get_reg(&cursor, UNW_REG_IP, &pc);
entry->stack[stack_index] = pc;

if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) == 0) {
#ifdef SDLTEST_UNWIND_NO_PROC_NAME_BY_IP
if (s_unwind_symbol_names && 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);
}
#endif
++stack_index;

if (stack_index == SDL_arraysize(entry->stack)) {
Expand Down Expand Up @@ -229,6 +240,19 @@ int SDLTest_TrackAllocations(void)
if (s_previous_allocations != 0) {
SDL_Log("SDLTest_TrackAllocations(): There are %d previous allocations, disabling free() validation", s_previous_allocations);
}
#ifdef SDLTEST_UNWIND_NO_PROC_NAME_BY_IP
do {
/* Don't use SDL_GetHint: SDL_malloc is off limits. */
const char *env_trackmem = SDL_getenv("SDL_TRACKMEM_SYMBOL_NAMES");
if (env_trackmem) {
if (SDL_strcasecmp(env_trackmem, "1") == 0 || SDL_strcasecmp(env_trackmem, "yes") == 0 || SDL_strcasecmp(env_trackmem, "true") == 0) {
s_unwind_symbol_names = SDL_TRUE;
} else if (SDL_strcasecmp(env_trackmem, "0") == 0 || SDL_strcasecmp(env_trackmem, "no") == 0 || SDL_strcasecmp(env_trackmem, "false") == 0) {
s_unwind_symbol_names = SDL_FALSE;
}
}
} while (0);
#endif

SDL_GetMemoryFunctions(&SDL_malloc_orig,
&SDL_calloc_orig,
Expand Down Expand Up @@ -283,10 +307,26 @@ 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 SDLTEST_UNWIND_NO_PROC_NAME_BY_IP
if (s_unwind_symbol_names) {
(void)SDL_snprintf(stack_entry_description, sizeof(stack_entry_description), "%s", entry->stack_names[stack_index]);
}
#else
char name[256] = "???";
unw_word_t offset = 0;
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
}
#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;
Expand Down

0 comments on commit 38d2a8a

Please sign in to comment.