about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorYang Liu <liuyang22@iscas.ac.cn>2025-04-09 17:16:32 +0800
committerGitHub <noreply@github.com>2025-04-09 11:16:32 +0200
commitc9b6b5fbe0888f443a080c43fc38a215fddb42b3 (patch)
tree4cc4b7dfa769110faeb7a778dcaa3b4a8d3928d9 /src
parent62765f151a2fcd5c1bd4fe5d46fc6277d3a1bee3 (diff)
downloadbox64-c9b6b5fbe0888f443a080c43fc38a215fddb42b3.tar.gz
box64-c9b6b5fbe0888f443a080c43fc38a215fddb42b3.zip
Added backtrace.h for holding backtrace-related functions (#2515)
Diffstat (limited to 'src')
-rw-r--r--src/custommem.c6
-rw-r--r--src/include/backtrace.h6
-rw-r--r--src/include/signals.h2
-rw-r--r--src/libtools/signals.c34
-rw-r--r--src/os/backtrace.c46
5 files changed, 58 insertions, 36 deletions
diff --git a/src/custommem.c b/src/custommem.c
index f74666e7..90b14c7a 100644
--- a/src/custommem.c
+++ b/src/custommem.c
@@ -2,10 +2,10 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <pthread.h>
 #include <errno.h>
 
 #include "os.h"
+#include "backtrace.h"
 #include "box64context.h"
 #include "debug.h"
 #include "x64trace.h"
@@ -557,7 +557,7 @@ void* map128_customMalloc(size_t size, int is32bits)
             // mask size from this block
             p_blocks[i].size = 0;
             mutex_unlock(&mutex_blocks);
-            showNativeBT(LOG_NONE);
+            ShowNativeBT(LOG_NONE);
             testAllBlocks();
             if(BOX64ENV(log)>=LOG_DEBUG) {
                 printf_log(LOG_NONE, "Used 32bits address space map:\n");
@@ -663,7 +663,7 @@ void* internal_customMalloc(size_t size, int is32bits)
             // mask size from this block
             p_blocks[i].size = 0;
             mutex_unlock(&mutex_blocks);
-            showNativeBT(LOG_NONE);
+            ShowNativeBT(LOG_NONE);
             testAllBlocks();
             if(BOX64ENV(log)>=LOG_DEBUG) {
                 printf_log(LOG_NONE, "Used 32bits address space map:\n");
diff --git a/src/include/backtrace.h b/src/include/backtrace.h
new file mode 100644
index 00000000..b098d775
--- /dev/null
+++ b/src/include/backtrace.h
@@ -0,0 +1,6 @@
+#ifndef __BACKTRACE_H_
+#define __BACKTRACE_H_
+
+void ShowNativeBT(int log_minimum);
+
+#endif // __BACKTRACE_H_
\ No newline at end of file
diff --git a/src/include/signals.h b/src/include/signals.h
index cb627250..ffe2df04 100644
--- a/src/include/signals.h
+++ b/src/include/signals.h
@@ -79,6 +79,4 @@ void emit_interruption(x64emu_t* emu, int num, void* addr);
 void emit_div0(x64emu_t* emu, void* addr, int code);
 void check_exec(x64emu_t* emu, uintptr_t addr);
 
-void showNativeBT(int log_minimum);
-
 #endif //__SIGNALS_H__
diff --git a/src/libtools/signals.c b/src/libtools/signals.c
index 5190515e..62081432 100644
--- a/src/libtools/signals.c
+++ b/src/libtools/signals.c
@@ -18,6 +18,7 @@
 #endif
 
 #include "os.h"
+#include "backtrace.h"
 #include "box64context.h"
 #include "debug.h"
 #include "x64emu.h"
@@ -1893,9 +1894,9 @@ dynarec_log(/*LOG_DEBUG*/LOG_INFO, "%04d|Repeated SIGSEGV with Access error on %
 
         if((BOX64ENV(showbt) || sig==SIGABRT) && log_minimum<=BOX64ENV(log)) {
             // show native bt
-            showNativeBT(log_minimum);
+            ShowNativeBT(log_minimum);
 
-            #define BT_BUF_SIZE 100
+#define BT_BUF_SIZE 100
             int nptrs;
             void *buffer[BT_BUF_SIZE];
             char **strings;
@@ -2587,35 +2588,6 @@ EXPORT int my_swapcontext(x64emu_t* emu, void* ucp1, void* ucp2)
     return 0;
 }
 
-void showNativeBT(int log_minimum)
-{
-#ifndef ANDROID
-    // grab current name
-    // and temporarily rename binary to original box64
-    // to get exact backtrace
-    size_t boxpath_lenth = strlen(my_context->box64path)+1;
-    char current_name[boxpath_lenth];
-    memcpy(current_name, my_context->orig_argv[0], boxpath_lenth);
-    memcpy(my_context->orig_argv[0], my_context->box64path, boxpath_lenth);
-    // show native bt
-    #define BT_BUF_SIZE 100
-    int nptrs;
-    void *buffer[BT_BUF_SIZE];
-    char **strings;
-
-    nptrs = backtrace(buffer, BT_BUF_SIZE);
-    strings = backtrace_symbols(buffer, nptrs);
-    if(strings) {
-        for (int j = 0; j < nptrs; j++)
-            printf_log(log_minimum, "NativeBT: %s\n", strings[j]);
-        free(strings);
-    } else
-        printf_log(log_minimum, "NativeBT: none (%d/%s)\n", errno, strerror(errno));
-    // restore modified name
-    memcpy(my_context->box64path, my_context->orig_argv[0], boxpath_lenth);
-#endif
-}
-
 #ifdef USE_SIGNAL_MUTEX
 static void atfork_child_dynarec_prot(void)
 {
diff --git a/src/os/backtrace.c b/src/os/backtrace.c
new file mode 100644
index 00000000..f0345cd6
--- /dev/null
+++ b/src/os/backtrace.c
@@ -0,0 +1,46 @@
+#include "backtrace.h"
+
+#if !defined(ANDROID) && !defined(_WIN32)
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <signal.h>
+#include <errno.h>
+#include <string.h>
+#include <execinfo.h>
+
+#include "debug.h"
+#include "box64context.h"
+
+#define BT_BUF_SIZE 100
+void ShowNativeBT(int log_minimum)
+{
+    // grab current name
+    // and temporarily rename binary to original box64
+    // to get exact backtrace
+    size_t boxpath_lenth = strlen(my_context->box64path) + 1;
+    char current_name[boxpath_lenth];
+    memcpy(current_name, my_context->orig_argv[0], boxpath_lenth);
+    memcpy(my_context->orig_argv[0], my_context->box64path, boxpath_lenth);
+    // show native bt
+    int nptrs;
+    void* buffer[BT_BUF_SIZE];
+    char** strings;
+
+    nptrs = backtrace(buffer, BT_BUF_SIZE);
+    strings = backtrace_symbols(buffer, nptrs);
+    if (strings) {
+        for (int j = 0; j < nptrs; j++)
+            printf_log(log_minimum, "NativeBT: %s\n", strings[j]);
+        free(strings);
+    } else
+        printf_log(log_minimum, "NativeBT: none (%d/%s)\n", errno, strerror(errno));
+    // restore modified name
+    memcpy(my_context->box64path, my_context->orig_argv[0], boxpath_lenth);
+}
+#undef BT_BUF_SIZE
+
+#else
+void ShowNativeBT(int log_minimum) { }
+#endif
\ No newline at end of file