int getCurrentStackTrace(size_t skip, StackTrace* trace) { trace->frameIPs = NULL; trace->frameCount = 0; struct Context ctx; ctx.trace = trace; ctx.skip = skip; ctx.capacity = 0; unw_context_t uctx; int r = unw_getcontext(&uctx); int err = checkError("unw_get_context", r); if (err) return err; unw_cursor_t cursor; r = unw_init_local(&cursor, &uctx); err = checkError("unw_init_local", r); if (err) return err; while ((r = unw_step(&cursor)) > 0) { if ((err = addIP(&ctx, &cursor)) != 0) { destroyStackTrace(trace); return err; } } err = checkError("unw_step", r); if (err) return err; return 0; }
int dwarf_backtrace(CallStack **returnStack) { *returnStack = calloc(1, sizeof(CallStack)); CallStack *callStack = *returnStack; callStack->stack = calloc(INITIAL_LIVE_FUNCTION_SIZE, sizeof(LiveFunction)); callStack->count = 0; callStack->capacity = INITIAL_LIVE_FUNCTION_SIZE; unw_cursor_t cursor; unw_context_t uc; unw_word_t ip, sp; unw_getcontext(&uc); unw_init_local(&cursor, &uc); while (unw_step(&cursor) > 0) { unw_get_reg(&cursor, UNW_REG_IP, &ip); unw_get_reg(&cursor, UNW_REG_SP, &sp); if (callStack->count >= callStack->capacity) { callStack->capacity *= 2; callStack->stack = realloc(callStack->stack, callStack->capacity * sizeof(LiveFunction *)); } callStack->stack[callStack->count].cursor = cursor; callStack->stack[callStack->count].pc = (void *)ip; callStack->stack[callStack->count].sp = (void *)sp; printf("%p => %p\n", (void *)ip, (void *)sp); callStack->count++; } return callStack->count; }
static void findRealErrorOnStack (ShockerScriptableControlObject* obj) { unw_context_t uc; unw_cursor_t cursor, prev; unw_word_t bp; unw_getcontext(&uc); unw_init_local(&cursor, &uc); char framename [1024]; int count = 0; while (unw_step(&cursor) > 0 && count < 18) { count++; unw_get_proc_name (&cursor, framename, sizeof(framename), 0); if (!*framename) continue; if (strstr (framename, "js_ReportErrorAgain")) { #if (__i386__) unw_get_reg(&prev, UNW_X86_EBP, &bp); #elif (__amd64__) unw_get_reg(&prev, UNW_X86_64_RBP, &bp); #endif bp += 12; char ** n = (char**)bp; obj->GetLogProvider ()->LogError (*n); break; } prev = cursor; } }
static void _adjust_stack_pre_safepoint(hlt_execution_context* ctx) { // Climb up the stack and adjust the reference counts for live objects // referenced there so that it reflects reality. unw_cursor_t cursor; unw_context_t uc; unw_word_t ip, sp; unw_getcontext(&uc); unw_init_local(&cursor, &uc); int level = 0; while ( unw_step(&cursor) > 0 ) { unw_get_reg(&cursor, UNW_REG_IP, &ip); unw_get_reg(&cursor, UNW_REG_SP, &sp); printf("level = %d ip = %lx, sp = %lx\n", level, (long)ip, (long)sp); level++; } #ifdef DEBUG // DBG_LOG("hilti-mem", "%10s stack-adjust-ctor %p %d %s::%s = %s", obj, level, func, id, render); #endif }
/* * ut_dump_backtrace -- dump stacktrace to error log using libunwind */ void ut_dump_backtrace(void) { unw_context_t context; unw_proc_info_t pip; pip.unwind_info = NULL; int ret = unw_getcontext(&context); if (ret) { ERR("unw_getcontext: %s [%d]", unw_strerror(ret), ret); return; } unw_cursor_t cursor; ret = unw_init_local(&cursor, &context); if (ret) { ERR("unw_init_local: %s [%d]", unw_strerror(ret), ret); return; } ret = unw_step(&cursor); char procname[PROCNAMELEN]; unsigned i = 0; while (ret > 0) { ret = unw_get_proc_info(&cursor, &pip); if (ret) { ERR("unw_get_proc_info: %s [%d]", unw_strerror(ret), ret); break; } unw_word_t off; ret = unw_get_proc_name(&cursor, procname, PROCNAMELEN, &off); if (ret && ret != -UNW_ENOMEM) { if (ret != -UNW_EUNSPEC) { ERR("unw_get_proc_name: %s [%d]", unw_strerror(ret), ret); } strcpy(procname, "?"); } void *ptr = (void *)(pip.start_ip + off); Dl_info dlinfo; const char *fname = "?"; if (dladdr(ptr, &dlinfo) && dlinfo.dli_fname && *dlinfo.dli_fname) fname = dlinfo.dli_fname; ERR("%u: %s (%s%s+0x%lx) [%p]", i++, fname, procname, ret == -UNW_ENOMEM ? "..." : "", off, ptr); ret = unw_step(&cursor); if (ret < 0) ERR("unw_step: %s [%d]", unw_strerror(ret), ret); } }
ssize_t getStackTraceSafe(uintptr_t* addresses, size_t maxAddresses) { if (maxAddresses == 0) { return 0; } unw_context_t context; if (unw_getcontext(&context) < 0) { return -1; } unw_cursor_t cursor; if (unw_init_local(&cursor, &context) < 0) { return -1; } if (!getFrameInfo(&cursor, *addresses)) { return -1; } ++addresses; size_t count = 1; for (; count != maxAddresses; ++count, ++addresses) { int r = unw_step(&cursor); if (r < 0) { return -1; } if (r == 0) { break; } if (!getFrameInfo(&cursor, *addresses)) { return -1; } } return count; }
int xbt_libunwind_backtrace(void** bt, int size){ int i = 0; for(i=0; i < size; i++) bt[i] = nullptr; i=0; unw_cursor_t c; unw_context_t uc; unw_getcontext (&uc); unw_init_local (&c, &uc); unw_word_t ip; unw_step(&c); while(unw_step(&c) >= 0 && i < size){ unw_get_reg(&c, UNW_REG_IP, &ip); bt[i] = (void*)(long)ip; i++; } return i; }
UINT64 Extrae_get_caller (int offset) { int current_deep = 0; unw_cursor_t cursor; unw_context_t uc; unw_word_t ip; if (unw_getcontext(&uc) < 0) return 0; if (unw_init_local(&cursor, &uc)) return 0; offset --; /* Don't compute call to unw_getcontext */ while (current_deep <= offset) { if (unw_get_reg(&cursor, UNW_REG_IP, &ip) < 0) break; #if defined(DEBUG) fprintf (stderr, "DEBUG: offset %d depth %d address %08llx %c\n", offset, current_deep, ip, (offset == current_deep)?'*':' '); #endif if (unw_step (&cursor) <= 0) return 0; current_deep ++; } return (UINT64) ip; }
void backtrace() { unw_cursor_t cursor; unw_context_t context; // Initialize cursor to current frame for local unwinding. unw_getcontext(&context); unw_init_local(&cursor, &context); // Unwind frames one by one, going up the frame stack. while (unw_step(&cursor) > 0) { unw_word_t offset, pc; unw_get_reg(&cursor, UNW_REG_IP, &pc); if (pc == 0) { break; } printf("0x%lx:", pc); char sym[256]; if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) == 0) { char* nameptr = sym; int status; char* demangled = abi::__cxa_demangle(sym, nullptr, nullptr, &status); if (status == 0) { nameptr = demangled; } char file[256]; int line = 0; getFileAndLine((long)pc, file, 256, &line); printf(" (%s+0x%lx) in %s:%d\n", nameptr, offset, file, line); free(demangled); } else { printf(" -- error: unable to obtain symbol name for this frame\n"); } } }
static void print_trace (void) { #ifdef HAVE_LIBUNWIND unw_context_t uc; unw_cursor_t cursor; guint stack_num = 0; if (!display_filter (DISPLAY_FLAG_BACKTRACE)) return; unw_getcontext (&uc); unw_init_local (&cursor, &uc); while (unw_step (&cursor) > 0) { gchar name[129]; unw_word_t off; int result; result = unw_get_proc_name (&cursor, name, sizeof (name) - 1, &off); if (result < 0 && result != UNW_ENOMEM) { g_print ("Error getting proc name\n"); break; } g_print ("#%d %s + [0x%08x]\n", stack_num++, name, (unsigned int)off); } }
// Initialize unw_cursor_t and unw_context_t from REGDISPLAY bool InitializeUnwindContextAndCursor(REGDISPLAY* regDisplay, unw_cursor_t* cursor, unw_context_t* unwContext) { #ifndef CAN_LINK_SHARED_LIBUNWIND return false; #else // CAN_LINK_SHARED_LIBUNWIND int st; #if !UNWIND_CONTEXT_IS_UCONTEXT_T st = unw_getcontext(unwContext); if (st < 0) { return false; } #endif RegDisplayToUnwindContext(regDisplay, unwContext); st = unw_init_local(cursor, unwContext); if (st < 0) { return false; } #if !UNWIND_CONTEXT_IS_UCONTEXT_T // Set the unwind context to the specified windows context RegDisplayToUnwindCursor(regDisplay, cursor); #endif return true; #endif // CAN_LINK_SHARED_LIBUNWIND }
void VSDebugLib::printStack() { unw_cursor_t cursor; unw_context_t uc; unw_word_t ip, sp, off; unw_proc_info_t pi; char file[256], name[256]; int line; int status; unw_getcontext(&uc); unw_init_local(&cursor, &uc); while (unw_step(&cursor) > 0) { unw_get_reg(&cursor, UNW_REG_IP, &ip); unw_get_reg(&cursor, UNW_REG_SP, &sp); unw_get_proc_name (&cursor, name, sizeof (name), &off); getFileAndLine((long)ip, file, 256, &line); if (line >= 0) { char *realname; realname = abi::__cxa_demangle(name, 0, 0, &status); if (realname) { printf("%s: %s, %d\n", realname, file, line); free(realname); } else { printf("%s: %s, %d\n", name, file, line); } } } }
static void GetContextAndExit(void* data) { GetContextArg* arg = (GetContextArg*)data; unw_getcontext(arg->unw_context); // Don't touch the stack anymore. while (*arg->exit_flag == 0) { } }
vector<long> StackUnwind::fullUnwind() { #ifdef MAKE_DYNA return dynaUnwind(); #endif unw_getcontext(&uc); unw_init_local(&cursor, &uc); vector<long> call_stack; /* Need to step twice to remove mention of WMTrace from the call stack */ if (unw_step(&cursor) <= 0) return call_stack; if (unw_step(&cursor) <= 0) return call_stack; int counter = 0; while (unw_step(&cursor) > 0 && counter < unw_max_depth) { unw_get_reg(&cursor, UNW_REG_IP, &ip); call_stack.push_back((long) ip); counter++; } return call_stack; }
static void handler(int sig) { unw_context_t context; unw_getcontext(&context); unw_cursor_t cursor; unw_init_local(&cursor, &context); SkDebugf("\nSignal %d:\n", sig); while (unw_step(&cursor) > 0) { static const size_t kMax = 256; char mangled[kMax], demangled[kMax]; unw_word_t offset; unw_get_proc_name(&cursor, mangled, kMax, &offset); int ok; size_t len = kMax; abi::__cxa_demangle(mangled, demangled, &len, &ok); SkDebugf("%s (+0x%zx)\n", ok == 0 ? demangled : mangled, (size_t)offset); } SkDebugf("\n"); // Exit NOW. Don't notify other threads, don't call anything registered with atexit(). _Exit(sig); }
void fakegc() { std::cout << "GC\n"; unw_cursor_t cursor; unw_word_t ip, sp; unw_context_t uc; root_info_t root_info_ptr; unw_getcontext(&uc); unw_init_local(&cursor, &uc); do { unw_get_reg(&cursor, UNW_REG_IP, &ip); unw_get_reg(&cursor, UNW_REG_SP, &sp); // printf("ip=%p sp=%p\n", ip, sp); if (stk_map.find_root_info((void*)ip, root_info_ptr)) { for(auto it : root_info_ptr) { int *cptr = *((int**)sp - it); printf("x=%p\n", cptr); printf("i=%d\n", *cptr); *cptr = *cptr + 1; } } } while (unw_step(&cursor) > 0); }
static void throw_exception (MonoObject *exc, guint64 rethrow) { unw_context_t unw_ctx; MonoContext ctx; MonoJitInfo *ji; unw_word_t ip, sp; int res; if (mono_object_isinst (exc, mono_defaults.exception_class)) { MonoException *mono_ex = (MonoException*)exc; if (!rethrow) { mono_ex->stack_trace = NULL; mono_ex->trace_ips = NULL; } } res = unw_getcontext (&unw_ctx); g_assert (res == 0); res = unw_init_local (&ctx.cursor, &unw_ctx); g_assert (res == 0); /* * Unwind until the first managed frame. This is needed since * mono_handle_exception expects the variables in the original context to * correspond to the method returned by mono_find_jit_info. */ while (TRUE) { res = unw_get_reg (&ctx.cursor, UNW_IA64_IP, &ip); g_assert (res == 0); res = unw_get_reg (&ctx.cursor, UNW_IA64_SP, &sp); g_assert (res == 0); ji = mini_jit_info_table_find (mono_domain_get (), (gpointer)ip, NULL); //printf ("UN: %s %lx %lx\n", ji ? jinfo_get_method (ji)->name : "", ip, sp); if (ji) break; res = unw_step (&ctx.cursor); if (res == 0) { /* * This means an unhandled exception during the compilation of a * topmost method like Main */ break; } g_assert (res >= 0); } ctx.precise_ip = FALSE; mono_handle_exception (&ctx, exc); restore_context (&ctx); g_assert_not_reached (); }
void collectStackRoots(TraceStack *stack) { unw_cursor_t cursor; unw_context_t uc; unw_word_t ip, sp, bp; // force callee-save registers onto the stack: // Actually, I feel like this is pretty brittle: // collectStackRoots itself is allowed to save the callee-save registers // on its own stack. jmp_buf registers __attribute__((aligned(sizeof(void*)))); #ifdef VALGRIND memset(®isters, 0, sizeof(registers)); memset(&cursor, 0, sizeof(cursor)); memset(&uc, 0, sizeof(uc)); memset(&ip, 0, sizeof(ip)); memset(&sp, 0, sizeof(sp)); memset(&bp, 0, sizeof(bp)); #endif setjmp(registers); assert(sizeof(registers) % 8 == 0); //void* stack_bottom = __builtin_frame_address(0); collectRoots(®isters, ®isters + 1, stack); unw_getcontext(&uc); unw_init_local(&cursor, &uc); TraceStackGCVisitor visitor(stack); int code; while (true) { int code = unw_step(&cursor); assert(code > 0 && "something broke unwinding!"); unw_get_reg(&cursor, UNW_REG_IP, &ip); unw_get_reg(&cursor, UNW_REG_SP, &sp); unw_get_reg(&cursor, UNW_TDEP_BP, &bp); void* cur_sp = (void*)sp; void* cur_bp = (void*)bp; //std::string name = g.func_addr_registry.getFuncNameAtAddress((void*)ip, true); //if (VERBOSITY()) printf("ip = %lx (%s), stack = [%p, %p)\n", (long) ip, name.c_str(), cur_sp, cur_bp); unw_proc_info_t pip; unw_get_proc_info(&cursor, &pip); if (pip.start_ip == (uintptr_t)&__libc_start_main) { break; } if (pip.start_ip == (intptr_t)interpretFunction) { // TODO Do we still need to crawl the interpreter itself? gatherInterpreterRootsForFrame(&visitor, cur_bp); } collectRoots(cur_sp, (char*)cur_bp, stack); } }
bool UnwindCurrent::Unwind(size_t num_ignore_frames) { int ret = unw_getcontext(&context_); if (ret < 0) { ALOGW("UnwindCurrent::Unwind: unw_getcontext failed %d\n", ret); return false; } return UnwindFromContext(num_ignore_frames, true); }
int backtrace(void **trace, int size) { unw_cursor_t cursor; unw_context_t uc; unw_word_t ip; int n = 0; unw_getcontext(&uc); unw_init_local(&cursor, &uc); while (unw_step(&cursor) > 0) { unw_get_reg(&cursor, UNW_REG_IP, &ip); trace[n++] = (void *)ip; { char buf[256]; unw_get_proc_name(&cursor, buf, 256, &ip); if (strncmp("_sigtramp", buf, sizeof("_sigtramp")) == 0) { goto darwin_sigtramp; } } } return n; darwin_sigtramp: /* darwin's bundled libunwind doesn't support signal trampoline */ { ucontext_t *uctx; /* get _sigtramp's ucontext_t and set values to cursor * http://www.opensource.apple.com/source/Libc/Libc-825.25/i386/sys/_sigtramp.s * http://www.opensource.apple.com/source/libunwind/libunwind-35.1/src/unw_getcontext.s */ unw_get_reg(&cursor, UNW_X86_64_RBX, &ip); uctx = (ucontext_t *)ip; unw_set_reg(&cursor, UNW_X86_64_RAX, uctx->uc_mcontext->__ss.__rax); unw_set_reg(&cursor, UNW_X86_64_RBX, uctx->uc_mcontext->__ss.__rbx); unw_set_reg(&cursor, UNW_X86_64_RCX, uctx->uc_mcontext->__ss.__rcx); unw_set_reg(&cursor, UNW_X86_64_RDX, uctx->uc_mcontext->__ss.__rdx); unw_set_reg(&cursor, UNW_X86_64_RDI, uctx->uc_mcontext->__ss.__rdi); unw_set_reg(&cursor, UNW_X86_64_RSI, uctx->uc_mcontext->__ss.__rsi); unw_set_reg(&cursor, UNW_X86_64_RBP, uctx->uc_mcontext->__ss.__rbp); unw_set_reg(&cursor, UNW_X86_64_RSP, 8+(uctx->uc_mcontext->__ss.__rsp)); unw_set_reg(&cursor, UNW_X86_64_R8, uctx->uc_mcontext->__ss.__r8); unw_set_reg(&cursor, UNW_X86_64_R9, uctx->uc_mcontext->__ss.__r9); unw_set_reg(&cursor, UNW_X86_64_R10, uctx->uc_mcontext->__ss.__r10); unw_set_reg(&cursor, UNW_X86_64_R11, uctx->uc_mcontext->__ss.__r11); unw_set_reg(&cursor, UNW_X86_64_R12, uctx->uc_mcontext->__ss.__r12); unw_set_reg(&cursor, UNW_X86_64_R13, uctx->uc_mcontext->__ss.__r13); unw_set_reg(&cursor, UNW_X86_64_R14, uctx->uc_mcontext->__ss.__r14); unw_set_reg(&cursor, UNW_X86_64_R15, uctx->uc_mcontext->__ss.__r15); ip = *(unw_word_t*)uctx->uc_mcontext->__ss.__rsp; unw_set_reg(&cursor, UNW_REG_IP, ip); trace[n++] = (void *)uctx->uc_mcontext->__ss.__rip; trace[n++] = (void *)ip; } while (unw_step(&cursor) > 0) { unw_get_reg(&cursor, UNW_REG_IP, &ip); trace[n++] = (void *)ip; } return n; }
static void luw_backtrace() { unw_cursor_t cursor; unw_context_t context; unw_getcontext(&context); unw_init_local(&cursor, &context); std::stringstream output; output << "Traceback (most recent call first):" << std::endl; while (unw_step(&cursor) > 0) { unw_word_t pc; unw_get_reg(&cursor, UNW_REG_IP, &pc); if (pc == 0) break; const char *file = NULL; const char *func = NULL; unsigned line = 0; resolve(pc, &file, &line, &func); if (file != NULL) { if (strcmp("__cxa_throw", func) == 0) continue; if (strncmp(func, "_Z", 2) == 0) { // This is a C++ mangled name (as per the IA-64 spec) output << " 0x" << std::hex << pc << std::dec << " " << demangle(func) << " (" << file << ":" << line << ")" << std::endl; } else { output << " 0x" << std::hex << pc << std::dec << " " << func << " (" << file << ":" << line << ")" << std::endl; } // Try to load the file and print the offending line std::ifstream f; f.open(file); if (!f.is_open()) { output << " <unable to find file>" << std::endl; } else { seek_to_line(f, line); std::string line; std::getline(f, line); output << " " << line << std::endl; } } else { output << " <unknown> (<unknown file>:<unknown line>)" << std::endl; } } output << std::endl; __latest_backtrace = output.str(); }
void jffi_longjmp (jmp_buf env, int val) { extern int _jffi_longjmp_cont; unw_context_t uc; unw_cursor_t c; unw_word_t sp, ip, bp = 0; uintptr_t *wp = (uintptr_t *) env; int i, setjmp_frame; if (unw_getcontext (&uc) < 0 || unw_init_local (&c, &uc) < 0) { debug("failed to get context"); abort (); } #ifdef __x86_86__ # define UNW_REG_BP UNW_X86_64_RBP #else # define UNW_REG_BP UNW_X86_EBP #endif setjmp_frame = 0; do { char name[256]; unw_proc_info_t pi; unw_word_t off; if (unw_get_reg (&c, UNW_REG_BP, &bp) < 0) { abort(); } if (unw_get_reg (&c, UNW_REG_SP, &sp) < 0) { abort(); } if (unw_get_reg (&c, UNW_REG_IP, &ip) < 0) { abort(); } unw_get_proc_name(&c, name, sizeof(name), &off); unw_get_proc_info(&c, &pi); // debug("frame %s ip=%llx sp=%llx bp=%llx wp[RP]=%p wp[SP]=%p, pi.start_ip=%llx, pi.end_ip=%llx", // name, (long long) ip, (long long) sp, (long long) bp, (void *) wp[JB_RP], (void *) wp[JB_SP], // pi.start_ip, pi.end_ip); if (wp[JB_SP] > sp || wp[JB_RP] < pi.start_ip || wp[JB_RP] > pi.end_ip) continue; /* found the right frame: */ // debug("found frame to jump back to"); assert (UNW_NUM_EH_REGS >= 2); if (unw_set_reg (&c, UNW_REG_EH + 0, wp[JB_RP]) < 0 || unw_set_reg (&c, UNW_REG_EH + 1, val) < 0 || unw_set_reg (&c, UNW_REG_IP, (unw_word_t) (uintptr_t) &_jffi_longjmp_cont)) abort (); unw_resume (&c); // should not reach here abort (); } while (unw_step (&c) > 0); // debug("failed to find correct frame to jmp to"); }
int main() { // verify unw_getcontext() returns no UNW_ESUCCESS unw_context_t uc; if ( unw_getcontext(&uc) == UNW_ESUCCESS ) return 0; else return 1; }
void log_stack_trace(const char *msg) { #ifdef USE_UNWIND unw_context_t ctx; unw_cursor_t cur; unw_word_t ip, off; unsigned level; char sym[512], *dsym; int status; const char *log = stack_trace_log.empty() ? NULL : stack_trace_log.c_str(); #endif if (msg) ST_LOG(msg); ST_LOG("Unwound call stack:"); #ifdef USE_UNWIND if (unw_getcontext(&ctx) < 0) { ST_LOG("Failed to create unwind context"); return; } if (unw_init_local(&cur, &ctx) < 0) { ST_LOG("Failed to find the first unwind frame"); return; } for (level = 1; level < 999; ++level) { // 999 for safety int ret = unw_step(&cur); if (ret < 0) { ST_LOG("Failed to find the next frame"); return; } if (ret == 0) break; if (unw_get_reg(&cur, UNW_REG_IP, &ip) < 0) { ST_LOG(" " << std::setw(4) << level); continue; } if (unw_get_proc_name(&cur, sym, sizeof(sym), &off) < 0) { ST_LOG(" " << std::setw(4) << level << std::setbase(16) << std::setw(20) << "0x" << ip); continue; } dsym = abi::__cxa_demangle(sym, NULL, NULL, &status); ST_LOG(" " << std::setw(4) << level << std::setbase(16) << std::setw(20) << "0x" << ip << " " << (!status && dsym ? dsym : sym) << " + " << "0x" << off); free(dsym); } #else std::stringstream ss; ss << el::base::debug::StackTrace(); std::vector<std::string> lines; std::string s = ss.str(); boost::split(lines, s, boost::is_any_of("\n")); for (const auto &line: lines) ST_LOG(line); #endif }
void AS_UTL_catchCrash(int sig_num, siginfo_t *info, void *ctx) { WRITE_STRING("\nFailed with '"); WRITE_STRING(strsignal(sig_num)); WRITE_STRING("'\n"); WRITE_STRING("\nBacktrace (mangled):\n\n"); unw_cursor_t cursor; unw_context_t uc; unw_word_t ip, sp; int depth = 0; unw_getcontext(&uc); // Get state unw_init_local(&cursor, &uc); // Initialize state cursor while (unw_step(&cursor) > 0) { unw_get_reg(&cursor, UNW_REG_IP, &ip); unw_get_reg(&cursor, UNW_REG_SP, &sp); unw_word_t off; char name[256]; if (unw_get_proc_name (&cursor, name, sizeof (name), &off) == 0) { if (off) fprintf(stderr, "%02d <%s + 0x%lx> ip=%lx sp=%lx\n", depth, name, (long)off, ip, sp); else fprintf(stderr, "%02d <%s> ip=%lx sp=%lx\n", depth, name, ip, sp); } else { fprintf(stderr, "%02d <?> ip=%lx sp=%lx\n", depth, ip, sp); } depth++; } //WRITE_STRING("\nBacktrace (demangled):\n\n"); //WRITE_STRING("\nGDB:\n\n"); //AS_UTL_envokeGDB(); // Pass the signal through, only so a core file can get generated. struct sigaction sa; sa.sa_handler = SIG_DFL; sigemptyset (&sa.sa_mask); sa.sa_flags = 0; sigaction(sig_num, &sa, NULL); raise(sig_num); }
// // Not used by C++. // Unwinds stack, calling "stop" function at each frame // Could be used to implement longjmp(). // EXPORT _Unwind_Reason_Code _Unwind_ForcedUnwind(struct _Unwind_Exception* exception_object, _Unwind_Stop_Fn stop, void* stop_parameter) { DEBUG_PRINT_API("_Unwind_ForcedUnwind(ex_obj=%p, stop=%p)\n", exception_object, stop); unw_context_t uc; unw_getcontext(&uc); // mark that this is a forced unwind, so _Unwind_Resume() can do the right thing exception_object->private_1 = (uintptr_t)stop; exception_object->private_2 = (uintptr_t)stop_parameter; // doit return unwind_phase2_forced(&uc, exception_object, stop, stop_parameter); }
int main(int argc, char** argv) { SIMIX_global_init(&argc, argv); simgrid::mc::Variable* var; simgrid::mc::Type* type; simgrid::mc::Process process(getpid(), -1); process.init(); test_global_variable(process, process.binary_info.get(), "some_local_variable", &some_local_variable, sizeof(int)); var = test_global_variable(process, process.binary_info.get(), "test_some_array", &test_some_array, sizeof(test_some_array)); auto i = process.binary_info->types.find(var->type_id); xbt_assert(i != process.binary_info->types.end(), "Missing type"); type = &i->second; xbt_assert(type->element_count == 6*5*4, "element_count mismatch in test_some_array : %i / %i", type->element_count, 6*5*4); var = test_global_variable(process, process.binary_info.get(), "test_some_struct", &test_some_struct, sizeof(test_some_struct)); i = process.binary_info->types.find(var->type_id); xbt_assert(i != process.binary_info->types.end(), "Missing type"); type = &i->second; assert(type); assert(find_member(*type, "first")->offset() == 0); assert(find_member(*type, "second")->offset() == ((const char*)&test_some_struct.second) - (const char*)&test_some_struct); unw_context_t context; unw_cursor_t cursor; unw_getcontext(&context); unw_init_local(&cursor, &context); test_local_variable(process.binary_info.get(), "main", "argc", &argc, &cursor); { int lexical_block_variable = 50; test_local_variable(process.binary_info.get(), "main", "lexical_block_variable", &lexical_block_variable, &cursor); } s_foo my_foo; test_type_by_name(process, my_foo); _exit(0); }
void Extrae_trace_callers (iotimer_t time, int offset, int type) { int current_deep = 1; unw_cursor_t cursor; unw_context_t uc; unw_word_t ip; /* Leave if they aren't initialized (asked by user!) */ if (Trace_Caller[type] == NULL) return; if (unw_getcontext(&uc) < 0) return; if (unw_init_local(&cursor, &uc) < 0) return; offset --; /* Don't compute call to unw_getcontext */ while ((unw_step(&cursor) > 0) && (current_deep < Caller_Deepness[type]+offset)) { if (unw_get_reg(&cursor, UNW_REG_IP, &ip) < 0) break; #if defined(MPICALLER_DEBUG) if (current_deep >= offset) fprintf (stderr, "emitted (deep = %d, offset = %d) ip = %lx\n", current_deep, offset, (long) ip); else fprintf (stderr, "ignored (deep = %d, offset = %d) ip = %lx\n", current_deep, offset, (long) ip); #endif if (current_deep >= offset) { if (type == CALLER_MPI || type == CALLER_DYNAMIC_MEMORY) { if (Trace_Caller[type][current_deep-offset]) { TRACE_EVENT(time, CALLER_EVENT_TYPE(type, current_deep-offset+1), (UINT64)ip); } } #if defined(SAMPLING_SUPPORT) else if (type == CALLER_SAMPLING) { if (Trace_Caller[type][current_deep-offset]) SAMPLE_EVENT_NOHWC(time, SAMPLING_EV+current_deep-offset+1, (UINT64) ip); } #endif } current_deep ++; } }
// // When _Unwind_RaiseException() is in phase2, it hands control // to the personality function at each frame. The personality // may force a jump to a landing pad in that function, the landing // pad code may then call _Unwind_Resume() to continue with the // unwinding. Note: the call to _Unwind_Resume() is from compiler // geneated user code. All other _Unwind_* routines are called // by the C++ runtime __cxa_* routines. // // Re-throwing an exception is implemented by having the code call // __cxa_rethrow() which in turn calls _Unwind_Resume_or_Rethrow() // EXPORT void _Unwind_Resume(struct _Unwind_Exception* exception_object) { DEBUG_PRINT_API("_Unwind_Resume(ex_obj=%p)\n", exception_object); unw_context_t uc; unw_getcontext(&uc); if ( exception_object->private_1 != 0 ) unwind_phase2_forced(&uc, exception_object, (_Unwind_Stop_Fn)exception_object->private_1, (void*)exception_object->private_2); else unwind_phase2(&uc, exception_object); // clients assume _Unwind_Resume() does not return, so all we can do is abort. ABORT("_Unwind_Resume() can't return"); }
/// Scans unwind information to find the function that contains the /// specified code address "pc". _LIBUNWIND_EXPORT void *_Unwind_FindEnclosingFunction(void *pc) { _LIBUNWIND_TRACE_API("_Unwind_FindEnclosingFunction(pc=%p)\n", pc); // This is slow, but works. // We create an unwind cursor then alter the IP to be pc unw_cursor_t cursor; unw_context_t uc; unw_proc_info_t info; unw_getcontext(&uc); unw_init_local(&cursor, &uc); unw_set_reg(&cursor, UNW_REG_IP, (unw_word_t)(long) pc); if (unw_get_proc_info(&cursor, &info) == UNW_ESUCCESS) return (void *)(long) info.start_ip; else return NULL; }