Skip to content

Commit e60be82

Browse files
l3utterflyslaren
authored andcommitted
added android implementation of ggml_print_backtrace_symbols (llama/8751)
* added android implementation of ggml_print_backtrace_symbols * Update ggml/src/ggml.c Co-authored-by: slaren <[email protected]> * Update ggml/src/ggml.c Co-authored-by: slaren <[email protected]> * Update ggml/src/ggml.c Co-authored-by: slaren <[email protected]> * Update ggml/src/ggml.c Co-authored-by: slaren <[email protected]> * Update ggml/src/ggml.c Co-authored-by: slaren <[email protected]> --------- Co-authored-by: slaren <[email protected]>
1 parent 19708df commit e60be82

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

ggml/src/ggml.c

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,51 @@ typedef pthread_t ggml_thread_t;
144144

145145
#include <sys/wait.h>
146146

147-
#if defined(__linux__)
147+
#if defined(__ANDROID__)
148+
#include <unwind.h>
149+
#include <dlfcn.h>
150+
#include <stdio.h>
151+
152+
struct backtrace_state {
153+
void ** current;
154+
void ** end;
155+
};
156+
157+
static _Unwind_Reason_Code unwind_callback(struct _Unwind_Context* context, void* arg) {
158+
struct backtrace_state * state = (struct backtrace_state *)arg;
159+
uintptr_t pc = _Unwind_GetIP(context);
160+
if (pc) {
161+
if (state->current == state->end) {
162+
return _URC_END_OF_STACK;
163+
} else {
164+
*state->current++ = (void*)pc;
165+
}
166+
}
167+
return _URC_NO_REASON;
168+
}
169+
170+
static void ggml_print_backtrace_symbols(void) {
171+
const int max = 100;
172+
void* buffer[max];
173+
174+
struct backtrace_state state = {buffer, buffer + max};
175+
_Unwind_Backtrace(unwind_callback, &state);
176+
177+
int count = state.current - buffer;
178+
179+
for (int idx = 0; idx < count; ++idx) {
180+
const void * addr = buffer[idx];
181+
const char * symbol = "";
182+
183+
Dl_info info;
184+
if (dladdr(addr, &info) && info.dli_sname) {
185+
symbol = info.dli_sname;
186+
}
187+
188+
fprintf(stderr, "%d: %p %s\n", idx, addr, symbol);
189+
}
190+
}
191+
#elif defined(__linux__)
148192
#include <execinfo.h>
149193
static void ggml_print_backtrace_symbols(void) {
150194
void * trace[100];

0 commit comments

Comments
 (0)