SDL_test: do late addres to name conversion, if libunwind supports it
This commit is contained in:
1 parent
622b511613
commit
877c19da41
1 file changed
+44
-4
@@ -29,6 +29,10 @@
|
|||||||
#ifdef HAVE_LIBUNWIND_H
|
#ifdef HAVE_LIBUNWIND_H
|
||||||
#define UNW_LOCAL_ONLY
|
#define UNW_LOCAL_ONLY
|
||||||
#include <libunwind.h>
|
#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
|
#endif
|
||||||
|
|
||||||
/* This is a simple tracking allocator to demonstrate the use of SDL's
|
/* This is a simple tracking allocator to demonstrate the use of SDL's
|
||||||
@@ -43,8 +47,10 @@ typedef struct SDL_tracked_allocation
|
|||||||
void *mem;
|
void *mem;
|
||||||
size_t size;
|
size_t size;
|
||||||
Uint64 stack[10];
|
Uint64 stack[10];
|
||||||
char stack_names[10][256];
|
|
||||||
struct SDL_tracked_allocation *next;
|
struct SDL_tracked_allocation *next;
|
||||||
|
#ifdef SDLTEST_UNWIND_NO_PROC_NAME_BY_IP
|
||||||
|
char stack_names[10][256];
|
||||||
|
#endif
|
||||||
} SDL_tracked_allocation;
|
} SDL_tracked_allocation;
|
||||||
|
|
||||||
static SDLTest_Crc32Context s_crc32_context;
|
static SDLTest_Crc32Context s_crc32_context;
|
||||||
@@ -121,15 +127,20 @@ static void SDL_TrackAllocation(void *mem, size_t size)
|
|||||||
|
|
||||||
stack_index = 0;
|
stack_index = 0;
|
||||||
while (unw_step(&cursor) > 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];
|
char sym[236];
|
||||||
|
#endif
|
||||||
|
|
||||||
unw_get_reg(&cursor, UNW_REG_IP, &pc);
|
unw_get_reg(&cursor, UNW_REG_IP, &pc);
|
||||||
entry->stack[stack_index] = 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);
|
SDL_snprintf(entry->stack_names[stack_index], sizeof(entry->stack_names[stack_index]), "%s+0x%llx", sym, (unsigned long long)offset);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
++stack_index;
|
++stack_index;
|
||||||
|
|
||||||
if (stack_index == SDL_arraysize(entry->stack)) {
|
if (stack_index == SDL_arraysize(entry->stack)) {
|
||||||
@@ -229,6 +240,19 @@ int SDLTest_TrackAllocations(void)
|
|||||||
if (s_previous_allocations != 0) {
|
if (s_previous_allocations != 0) {
|
||||||
SDL_Log("SDLTest_TrackAllocations(): There are %d previous allocations, disabling free() validation", s_previous_allocations);
|
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_GetMemoryFunctions(&SDL_malloc_orig,
|
||||||
&SDL_calloc_orig,
|
&SDL_calloc_orig,
|
||||||
@@ -283,10 +307,26 @@ void SDLTest_LogAllocations(void)
|
|||||||
ADD_LINE();
|
ADD_LINE();
|
||||||
/* Start at stack index 1 to skip our tracking functions */
|
/* Start at stack index 1 to skip our tracking functions */
|
||||||
for (stack_index = 1; stack_index < SDL_arraysize(entry->stack); ++stack_index) {
|
for (stack_index = 1; stack_index < SDL_arraysize(entry->stack); ++stack_index) {
|
||||||
|
char stack_entry_description[256] = "???";
|
||||||
if (!entry->stack[stack_index]) {
|
if (!entry->stack[stack_index]) {
|
||||||
break;
|
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();
|
ADD_LINE();
|
||||||
}
|
}
|
||||||
total_allocated += entry->size;
|
total_allocated += entry->size;
|
||||||
|
|||||||
Reference in new issue
Block a user