Added backtrace.h for holding backtrace-related functions (#2515)

This commit is contained in:
Yang Liu 2025-04-09 17:16:32 +08:00 committed by GitHub
parent 62765f151a
commit c9b6b5fbe0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 59 additions and 36 deletions

View File

@ -347,6 +347,7 @@ set_source_files_properties(
HEADER_FILE_ONLY TRUE)
set(OS_LINUX_SRC
"${BOX64_ROOT}/src/os/backtrace.c"
"${BOX64_ROOT}/src/os/os_linux.c"
)

View File

@ -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");

6
src/include/backtrace.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef __BACKTRACE_H_
#define __BACKTRACE_H_
void ShowNativeBT(int log_minimum);
#endif // __BACKTRACE_H_

View File

@ -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__

View File

@ -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)
{

46
src/os/backtrace.c Normal file
View File

@ -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