summaryrefslogtreecommitdiffstats
path: root/libbacktrace/UnwindCurrent.cpp
blob: 67e583f08069f7ac2c73df9150720d100717280d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdint.h>
#include <ucontext.h>

#include <memory>
#include <string>

#define UNW_LOCAL_ONLY
#include <libunwind.h>

#include <backtrace/Backtrace.h>

#include "BacktraceLog.h"
#include "UnwindCurrent.h"

std::string UnwindCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
  *offset = 0;
  char buf[512];
  unw_word_t value;
  if (unw_get_proc_name_by_ip(unw_local_addr_space, pc, buf, sizeof(buf),
                              &value, &context_) >= 0 && buf[0] != '\0') {
    *offset = static_cast<uintptr_t>(value);
    return buf;
  }
  return "";
}

void UnwindCurrent::GetUnwContextFromUcontext(const ucontext_t* ucontext) {
  unw_tdep_context_t* unw_context = reinterpret_cast<unw_tdep_context_t*>(&context_);

#if defined(__arm__)
  unw_context->regs[0] = ucontext->uc_mcontext.arm_r0;
  unw_context->regs[1] = ucontext->uc_mcontext.arm_r1;
  unw_context->regs[2] = ucontext->uc_mcontext.arm_r2;
  unw_context->regs[3] = ucontext->uc_mcontext.arm_r3;
  unw_context->regs[4] = ucontext->uc_mcontext.arm_r4;
  unw_context->regs[5] = ucontext->uc_mcontext.arm_r5;
  unw_context->regs[6] = ucontext->uc_mcontext.arm_r6;
  unw_context->regs[7] = ucontext->uc_mcontext.arm_r7;
  unw_context->regs[8] = ucontext->uc_mcontext.arm_r8;
  unw_context->regs[9] = ucontext->uc_mcontext.arm_r9;
  unw_context->regs[10] = ucontext->uc_mcontext.arm_r10;
  unw_context->regs[11] = ucontext->uc_mcontext.arm_fp;
  unw_context->regs[12] = ucontext->uc_mcontext.arm_ip;
  unw_context->regs[13] = ucontext->uc_mcontext.arm_sp;
  unw_context->regs[14] = ucontext->uc_mcontext.arm_lr;
  unw_context->regs[15] = ucontext->uc_mcontext.arm_pc;
#else
  unw_context->uc_mcontext = ucontext->uc_mcontext;
#endif
}

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;
}