+"""
+
+ if self.target == Target.ANDROID:
+ self.c_file_pfx = self.c_file_pfx + """
+#include <unwind.h>
+#include <dlfcn.h>
+
+// Implement the execinfo functions using _Unwind_backtrace. This seems to miss many Rust
+// symbols, so we only use it on Android, where execinfo.h is unavailable.
+
+struct BacktraceState {
+ void** current;
+ void** end;
+};
+static _Unwind_Reason_Code unwind_callback(struct _Unwind_Context* context, void* arg) {
+ struct BacktraceState* state = (struct BacktraceState*)arg;
+ uintptr_t pc = _Unwind_GetIP(context);
+ if (pc) {
+ if (state->current == state->end) {
+ return _URC_END_OF_STACK;
+ } else {
+ *state->current++ = (void*)(pc);
+ }
+ }
+ return _URC_NO_REASON;
+}
+
+int backtrace(void** buffer, int max) {
+ struct BacktraceState state = { buffer, buffer + max };
+ _Unwind_Backtrace(unwind_callback, &state);
+ return state.current - buffer;
+}
+
+void backtrace_symbols_fd(void ** buffer, int count, int _fd) {
+ for (int idx = 0; idx < count; ++idx) {
+ Dl_info info;
+ if (dladdr(buffer[idx], &info) && info.dli_sname) {
+ DEBUG_PRINT("%p: %s", buffer[idx], info.dli_sname);
+ } else {
+ DEBUG_PRINT("%p: ???", buffer[idx]);
+ }
+ }
+}
+"""
+ else:
+ self.c_file_pfx = self.c_file_pfx + "#include <execinfo.h>\n"
+ self.c_file_pfx = self.c_file_pfx + """