SDL: SDL_test: do late addres to name conversion, if libunwind supports it

From 877c19da419df29ccbfab06314cec9ed8804c27e Mon Sep 17 00:00:00 2001
From: Anonymous Maarten <[EMAIL REDACTED]>
Date: Sat, 31 Aug 2024 13:41:36 +0200
Subject: [PATCH] SDL_test: do late addres to name conversion, if libunwind
 supports it

---
 src/test/SDL_test_memory.c | 48 ++++++++++++++++++++++++++++++++++----
 1 file changed, 44 insertions(+), 4 deletions(-)

diff --git a/src/test/SDL_test_memory.c b/src/test/SDL_test_memory.c
index b82636bf65022..785e140b026e4 100644
--- a/src/test/SDL_test_memory.c
+++ b/src/test/SDL_test_memory.c
@@ -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
@@ -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;
@@ -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)) {
@@ -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,
@@ -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;