bool UnwindCurrent::UnwindFromContext(size_t num_ignore_frames, ucontext_t* ucontext) { if (ucontext == nullptr) { int ret = unw_getcontext(&context_); if (ret < 0) { BACK_LOGW("unw_getcontext failed %d", ret); return false; } } else { GetUnwContextFromUcontext(ucontext); } // The cursor structure is pretty large, do not put it on the stack. std::unique_ptr<unw_cursor_t> cursor(new unw_cursor_t); int ret = unw_init_local(cursor.get(), &context_); if (ret < 0) { BACK_LOGW("unw_init_local failed %d", ret); return false; } size_t num_frames = 0; do { unw_word_t pc; ret = unw_get_reg(cursor.get(), UNW_REG_IP, &pc); if (ret < 0) { BACK_LOGW("Failed to read IP %d", ret); break; } unw_word_t sp; ret = unw_get_reg(cursor.get(), UNW_REG_SP, &sp); if (ret < 0) { BACK_LOGW("Failed to read SP %d", ret); break; } frames_.resize(num_frames+1); backtrace_frame_data_t* frame = &frames_.at(num_frames); frame->num = num_frames; frame->pc = static_cast<uintptr_t>(pc); frame->sp = static_cast<uintptr_t>(sp); frame->stack_size = 0; FillInMap(frame->pc, &frame->map); // Check to see if we should skip this frame because it's coming // from within the library, and we are doing a local unwind. if (ucontext != nullptr || num_frames != 0 || !DiscardFrame(*frame)) { if (num_ignore_frames == 0) { // GetFunctionName is an expensive call, only do it if we are // keeping the frame. frame->func_name = GetFunctionName(frame->pc, &frame->func_offset); if (num_frames > 0) { // Set the stack size for the previous frame. backtrace_frame_data_t* prev = &frames_.at(num_frames-1); prev->stack_size = frame->sp - prev->sp; } num_frames++; } else { num_ignore_frames--; } } ret = unw_step (cursor.get()); } while (ret > 0 && num_frames < MAX_BACKTRACE_FRAMES); return true; }
bool UnwindCurrent::UnwindFromContext(size_t num_ignore_frames, ucontext_t* ucontext) { if (ucontext == nullptr) { int ret = unw_getcontext(&context_); if (ret < 0) { BACK_LOGW("unw_getcontext failed %d", ret); error_ = BACKTRACE_UNWIND_ERROR_SETUP_FAILED; return false; } } else { GetUnwContextFromUcontext(ucontext); } // The cursor structure is pretty large, do not put it on the stack. std::unique_ptr<unw_cursor_t> cursor(new unw_cursor_t); int ret = unw_init_local(cursor.get(), &context_); if (ret < 0) { BACK_LOGW("unw_init_local failed %d", ret); error_ = BACKTRACE_UNWIND_ERROR_SETUP_FAILED; return false; } initialized_ = true; size_t num_frames = 0; do { unw_word_t pc; ret = unw_get_reg(cursor.get(), UNW_REG_IP, &pc); if (ret < 0) { BACK_LOGW("Failed to read IP %d", ret); break; } unw_word_t sp; ret = unw_get_reg(cursor.get(), UNW_REG_SP, &sp); if (ret < 0) { BACK_LOGW("Failed to read SP %d", ret); break; } frames_.resize(num_frames+1); backtrace_frame_data_t* frame = &frames_.at(num_frames); frame->num = num_frames; frame->pc = static_cast<uintptr_t>(pc); frame->sp = static_cast<uintptr_t>(sp); frame->stack_size = 0; FillInMap(frame->pc, &frame->map); // Check to see if we should skip this frame because it's coming // from within the library, and we are doing a local unwind. if (ucontext != nullptr || num_frames != 0 || !DiscardFrame(*frame)) { if (num_ignore_frames == 0) { // GetFunctionName is an expensive call, only do it if we are // keeping the frame. frame->func_name = GetFunctionName(frame->pc, &frame->func_offset, &frame->map); if (num_frames > 0) { // Set the stack size for the previous frame. backtrace_frame_data_t* prev = &frames_.at(num_frames-1); prev->stack_size = frame->sp - prev->sp; } if (BacktraceMap::IsValid(frame->map)) { frame->rel_pc = frame->pc - frame->map.start + frame->map.load_bias; } else { frame->rel_pc = frame->pc; } num_frames++; } else { num_ignore_frames--; // Set the number of frames to zero to remove the frame added // above. By definition, if we still have frames to ignore // there should only be one frame in the vector. CHECK(num_frames == 0); frames_.resize(0); } } // If the pc is in a device map, then don't try to step. if (frame->map.flags & PROT_DEVICE_MAP) { break; } // Verify the sp is not in a device map too. backtrace_map_t map; FillInMap(frame->sp, &map); if (map.flags & PROT_DEVICE_MAP) { break; } ret = unw_step (cursor.get()); } while (ret > 0 && num_frames < MAX_BACKTRACE_FRAMES); return true; }