Skip to content

Commit 7c27a19

Browse files
l3utterflyslaren
andauthored
added android implementation of ggml_print_backtrace_symbols (#8751)
* added android implementation of ggml_print_backtrace_symbols * Update ggml/src/ggml.c Co-authored-by: slaren <slarengh@gmail.com> * Update ggml/src/ggml.c Co-authored-by: slaren <slarengh@gmail.com> * Update ggml/src/ggml.c Co-authored-by: slaren <slarengh@gmail.com> * Update ggml/src/ggml.c Co-authored-by: slaren <slarengh@gmail.com> * Update ggml/src/ggml.c Co-authored-by: slaren <slarengh@gmail.com> --------- Co-authored-by: slaren <slarengh@gmail.com>
1 parent 140074b commit 7c27a19

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
@@ -141,7 +141,51 @@ typedef pthread_t ggml_thread_t;
141141

142142
#include <sys/wait.h>
143143

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

0 commit comments

Comments
 (0)