UnwindCurrent.cpp revision c58287d60166ebdb1bd39fa40e5524c868c73a9b
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <sys/types.h>
18#include <ucontext.h>
19
20#include <backtrace/Backtrace.h>
21#include <backtrace/BacktraceMap.h>
22
23#define UNW_LOCAL_ONLY
24#include <libunwind.h>
25
26#include "BacktraceLog.h"
27#include "BacktraceThread.h"
28#include "UnwindCurrent.h"
29#include "UnwindMap.h"
30
31//-------------------------------------------------------------------------
32// UnwindCurrent functions.
33//-------------------------------------------------------------------------
34UnwindCurrent::UnwindCurrent() {
35}
36
37UnwindCurrent::~UnwindCurrent() {
38}
39
40bool UnwindCurrent::Unwind(size_t num_ignore_frames, ucontext_t* ucontext) {
41  if (!ucontext) {
42    int ret = unw_getcontext(&context_);
43    if (ret < 0) {
44      BACK_LOGW("unw_getcontext failed %d", ret);
45      return false;
46    }
47  }
48  else {
49    GetUnwContextFromUcontext(ucontext);
50  }
51  return UnwindFromContext(num_ignore_frames, false);
52}
53
54void UnwindCurrent::GetUnwContextFromUcontext(const ucontext_t* ucontext) {
55  unw_tdep_context_t* unw_context = reinterpret_cast<unw_tdep_context_t*>(&context_);
56
57#if defined(__arm__)
58  unw_context->regs[0] = ucontext->uc_mcontext.arm_r0;
59  unw_context->regs[1] = ucontext->uc_mcontext.arm_r1;
60  unw_context->regs[2] = ucontext->uc_mcontext.arm_r2;
61  unw_context->regs[3] = ucontext->uc_mcontext.arm_r3;
62  unw_context->regs[4] = ucontext->uc_mcontext.arm_r4;
63  unw_context->regs[5] = ucontext->uc_mcontext.arm_r5;
64  unw_context->regs[6] = ucontext->uc_mcontext.arm_r6;
65  unw_context->regs[7] = ucontext->uc_mcontext.arm_r7;
66  unw_context->regs[8] = ucontext->uc_mcontext.arm_r8;
67  unw_context->regs[9] = ucontext->uc_mcontext.arm_r9;
68  unw_context->regs[10] = ucontext->uc_mcontext.arm_r10;
69  unw_context->regs[11] = ucontext->uc_mcontext.arm_fp;
70  unw_context->regs[12] = ucontext->uc_mcontext.arm_ip;
71  unw_context->regs[13] = ucontext->uc_mcontext.arm_sp;
72  unw_context->regs[14] = ucontext->uc_mcontext.arm_lr;
73  unw_context->regs[15] = ucontext->uc_mcontext.arm_pc;
74#else
75  unw_context->uc_mcontext = ucontext->uc_mcontext;
76#endif
77}
78
79std::string UnwindCurrent::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
80  *offset = 0;
81  char buf[512];
82  unw_word_t value;
83  if (unw_get_proc_name_by_ip(unw_local_addr_space, pc, buf, sizeof(buf),
84                              &value, &context_) >= 0 && buf[0] != '\0') {
85    *offset = static_cast<uintptr_t>(value);
86    return buf;
87  }
88  return "";
89}
90
91bool UnwindCurrent::UnwindFromContext(size_t num_ignore_frames, bool within_handler) {
92  // The cursor structure is pretty large, do not put it on the stack.
93  unw_cursor_t* cursor = new unw_cursor_t;
94  int ret = unw_init_local(cursor, &context_);
95  if (ret < 0) {
96    if (!within_handler) {
97      BACK_LOGW("unw_init_local failed %d", ret);
98    }
99    delete cursor;
100    return false;
101  }
102
103  std::vector<backtrace_frame_data_t>* frames = GetFrames();
104  frames->reserve(MAX_BACKTRACE_FRAMES);
105  size_t num_frames = 0;
106  do {
107    unw_word_t pc;
108    ret = unw_get_reg(cursor, UNW_REG_IP, &pc);
109    if (ret < 0) {
110      if (!within_handler) {
111        BACK_LOGW("Failed to read IP %d", ret);
112      }
113      break;
114    }
115    unw_word_t sp;
116    ret = unw_get_reg(cursor, UNW_REG_SP, &sp);
117    if (ret < 0) {
118      if (!within_handler) {
119        BACK_LOGW("Failed to read SP %d", ret);
120      }
121      break;
122    }
123
124    if (num_ignore_frames == 0) {
125      frames->resize(num_frames+1);
126      backtrace_frame_data_t* frame = &frames->at(num_frames);
127      frame->num = num_frames;
128      frame->pc = static_cast<uintptr_t>(pc);
129      frame->sp = static_cast<uintptr_t>(sp);
130      frame->stack_size = 0;
131
132      if (num_frames > 0) {
133        // Set the stack size for the previous frame.
134        backtrace_frame_data_t* prev = &frames->at(num_frames-1);
135        prev->stack_size = frame->sp - prev->sp;
136      }
137
138      if (!within_handler) {
139        frame->func_name = GetFunctionName(frame->pc, &frame->func_offset);
140        frame->map = FindMap(frame->pc);
141      } else {
142        frame->map = NULL;
143        frame->func_offset = 0;
144      }
145      num_frames++;
146    } else {
147      num_ignore_frames--;
148    }
149    ret = unw_step (cursor);
150  } while (ret > 0 && num_frames < MAX_BACKTRACE_FRAMES);
151
152  delete cursor;
153  return true;
154}
155
156//-------------------------------------------------------------------------
157// C++ object creation function.
158//-------------------------------------------------------------------------
159Backtrace* CreateCurrentObj(BacktraceMap* map) {
160  return new BacktraceCurrent(new UnwindCurrent(), map);
161}
162
163Backtrace* CreateThreadObj(pid_t tid, BacktraceMap* map) {
164  return new BacktraceThread(new UnwindCurrent(), tid, map);
165}
166