Skip to content

Commit 02e409a

Browse files
authored
ggml : Provide macos-specific backtrace printing to avoid terminal death (#17869)
* fix: Provide macos-specific backtrace printing to avoid terminal death Branch: MacOSSafeBacktrace Signed-off-by: Gabe Goodhart <ghart@us.ibm.com> * fix: Add GGML_BACKTRACE_LLDB env var to enable using lldb for backtrace Branch: MacOSSafeBacktrace Signed-off-by: Gabe Goodhart <ghart@us.ibm.com> --------- Signed-off-by: Gabe Goodhart <ghart@us.ibm.com>
1 parent 6b82eb7 commit 02e409a

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

ggml/src/ggml.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ static void ggml_print_backtrace_symbols(void) {
124124
int nptrs = backtrace(trace, sizeof(trace)/sizeof(trace[0]));
125125
backtrace_symbols_fd(trace, nptrs, STDERR_FILENO);
126126
}
127+
#elif defined(__APPLE__)
128+
#include <execinfo.h>
129+
static void ggml_print_backtrace_symbols(void) {
130+
void * trace[100];
131+
int nptrs = backtrace(trace, sizeof(trace)/sizeof(trace[0]));
132+
backtrace_symbols_fd(trace, nptrs, STDERR_FILENO);
133+
}
127134
#else
128135
static void ggml_print_backtrace_symbols(void) {
129136
// platform not supported
@@ -135,6 +142,20 @@ void ggml_print_backtrace(void) {
135142
if (GGML_NO_BACKTRACE) {
136143
return;
137144
}
145+
#if defined(__APPLE__)
146+
// On macOS, fork+debugger attachment is problematic due to:
147+
// 1. libdispatch "poisons" forked child processes
148+
// 2. lldb has issues attaching to parent from forked child
149+
// Use simple backtrace() instead to avoid Terminal.app crashes
150+
const char * GGML_BACKTRACE_LLDB = getenv("GGML_BACKTRACE_LLDB");
151+
if (!GGML_BACKTRACE_LLDB) {
152+
fprintf(stderr, "WARNING: Using native backtrace. Set GGML_BACKTRACE_LLDB for more info.\n");
153+
fprintf(stderr, "WARNING: GGML_BACKTRACE_LLDB may cause native MacOS Terminal.app to crash.\n");
154+
fprintf(stderr, "See: https://github.com/ggml-org/llama.cpp/pull/17869\n");
155+
ggml_print_backtrace_symbols();
156+
return;
157+
}
158+
#endif
138159
#if defined(__linux__)
139160
FILE * f = fopen("/proc/self/status", "r");
140161
size_t size = 0;

0 commit comments

Comments
 (0)