Skip to content

Commit

Permalink
feat: capture LR in ndk unwind
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Oct 27, 2023
1 parent 6f4a6a2 commit f47a077
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ uint64_t RegsArm::sp() {
return regs_[ARM_REG_SP];
}

uint64_t RegsArm::lr() {
return regs_[ARM_REG_LR];
}

void RegsArm::set_pc(uint64_t pc) {
regs_[ARM_REG_PC] = pc;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ uint64_t RegsArm64::sp() {
return regs_[ARM64_REG_SP];
}

uint64_t RegsArm64::lr() {
return regs_[ARM64_REG_LR];
}

static uint64_t strip_pac(uint64_t pc, uint64_t mask) {
// If the target is aarch64 then the return address may have been
// signed using the Armv8.3-A Pointer Authentication extension. The
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ uint64_t RegsX86::sp() {
return regs_[X86_REG_SP];
}

uint64_t RegsX86::lr() {
return 0;
}

void RegsX86::set_pc(uint64_t pc) {
regs_[X86_REG_PC] = static_cast<uint32_t>(pc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ uint64_t RegsX86_64::sp() {
return regs_[X86_64_REG_SP];
}

uint64_t RegsX86_64::lr() {
return 0;
}

void RegsX86_64::set_pc(uint64_t pc) {
regs_[X86_64_REG_PC] = pc;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class Regs {
virtual void* RawData() = 0;
virtual uint64_t pc() = 0;
virtual uint64_t sp() = 0;
virtual uint64_t lr() = 0; // embrace added

virtual void set_pc(uint64_t pc) = 0;
virtual void set_sp(uint64_t sp) = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class RegsArm : public RegsImpl<uint32_t> {

uint64_t pc() override;
uint64_t sp() override;
uint64_t lr() override;

void set_pc(uint64_t pc) override;
void set_sp(uint64_t sp) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class RegsArm64 : public RegsImpl<uint64_t> {

uint64_t pc() override;
uint64_t sp() override;
uint64_t lr() override;

void set_pc(uint64_t pc) override;
void set_sp(uint64_t sp) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class RegsX86 : public RegsImpl<uint32_t> {

uint64_t pc() override;
uint64_t sp() override;
uint64_t lr() override;

void set_pc(uint64_t pc) override;
void set_sp(uint64_t sp) override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class RegsX86_64 : public RegsImpl<uint64_t> {

uint64_t pc() override;
uint64_t sp() override;
uint64_t lr() override;

void set_pc(uint64_t pc) override;
void set_sp(uint64_t sp) override;
Expand Down
3 changes: 3 additions & 0 deletions embrace-android-sdk/src/main/cpp/file_writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ static const char *kFunctionNameKey = "function_name";
static const char *kRelPcKey = "rel_pc";
static const char *kPcKey = "pc";
static const char *kSpKey = "sp";
static const char *kLrKey = "lr";
static const char *kStartKey = "start";
static const char *kEndKey = "end";
static const char *kOffsetKey = "offset";
Expand Down Expand Up @@ -148,6 +149,7 @@ void emb_log_frame_dbg_info(int i, emb_sframe *frame) {
EMB_LOGDEV("rel_pc: 0x%lx", (unsigned long) frame->rel_pc);
EMB_LOGDEV("pc: 0x%lx", (unsigned long) frame->pc);
EMB_LOGDEV("sp: 0x%lx", (unsigned long) frame->sp);
EMB_LOGDEV("lr: 0x%lx", (unsigned long) frame->lr);
EMB_LOGDEV("start: 0x%lx", (unsigned long) frame->start);
EMB_LOGDEV("end: 0x%lx", (unsigned long) frame->end);
EMB_LOGDEV("offset: 0x%lx", (unsigned long) frame->offset);
Expand Down Expand Up @@ -245,6 +247,7 @@ char *emb_crash_to_json(emb_crash *crash) {
json_object_set_number(frame_object, kRelPcKey, (unsigned long) frame.rel_pc);
json_object_set_number(frame_object, kPcKey, (unsigned long) frame.pc);
json_object_set_number(frame_object, kSpKey, (unsigned long) frame.sp);
json_object_set_number(frame_object, kLrKey, (unsigned long) frame.lr);
json_object_set_number(frame_object, kStartKey, (unsigned long) frame.start);
json_object_set_number(frame_object, kEndKey, (unsigned long) frame.end);
json_object_set_number(frame_object, kOffsetKey, (unsigned long) frame.offset);
Expand Down
1 change: 1 addition & 0 deletions embrace-android-sdk/src/main/cpp/stack_frames.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ typedef struct {
uint64_t rel_pc;
uint64_t pc;
uint64_t sp;
uint64_t lr; // only populated for the first frame.
uint64_t function_offset;
char function_name[EMB_FRAME_STR_SIZE];

Expand Down
8 changes: 8 additions & 0 deletions embrace-android-sdk/src/main/cpp/unwinders/unwinder_stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,16 @@ emb_process_stack(emb_env *env, siginfo_t *info, void *user_context) {

if (unwindSuccessful) {
int i = 0;


for (const auto &frame: android_unwinder_data.frames) {
emb_sframe *data = &stacktrace[i++];

// populate the link register for the first value only
if (i == 0 && android_unwinder_data.saved_initial_regs.has_value()) {
data->lr = android_unwinder_data.saved_initial_regs->get()->lr();
}

data->frame_addr = frame.pc;
const auto map_info = frame.map_info;
emb_strncpy(data->build_id, map_info->GetPrintableBuildID().c_str(), EMB_FRAME_STR_SIZE);
Expand Down

0 comments on commit f47a077

Please sign in to comment.