blob: e45c5f814b13fcb55ed21b1c6a581f4635af3536 [file] [log] [blame]
Christopher Ferris17e91d42013-10-21 13:30:52 -07001/*
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#define LOG_TAG "libbacktrace"
18
Christopher Ferris46756822014-01-14 20:16:30 -080019#include <backtrace/Backtrace.h>
20#include <backtrace/BacktraceMap.h>
Christopher Ferris17e91d42013-10-21 13:30:52 -070021
22#include <sys/types.h>
23#include <string.h>
24
Christopher Ferris17e91d42013-10-21 13:30:52 -070025#include <libunwind.h>
26#include <libunwind-ptrace.h>
27
28#include "UnwindPtrace.h"
29
30UnwindPtrace::UnwindPtrace() : addr_space_(NULL), upt_info_(NULL) {
31}
32
33UnwindPtrace::~UnwindPtrace() {
34 if (upt_info_) {
35 _UPT_destroy(upt_info_);
36 upt_info_ = NULL;
37 }
38 if (addr_space_) {
39 unw_destroy_addr_space(addr_space_);
40 addr_space_ = NULL;
41 }
42}
43
44bool UnwindPtrace::Unwind(size_t num_ignore_frames) {
45 addr_space_ = unw_create_addr_space(&_UPT_accessors, 0);
46 if (!addr_space_) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070047 BACK_LOGW("unw_create_addr_space failed.");
Christopher Ferris17e91d42013-10-21 13:30:52 -070048 return false;
49 }
50
51 upt_info_ = reinterpret_cast<struct UPT_info*>(_UPT_create(backtrace_obj_->Tid()));
52 if (!upt_info_) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070053 BACK_LOGW("Failed to create upt info.");
Christopher Ferris17e91d42013-10-21 13:30:52 -070054 return false;
55 }
56
Christopher Ferris17e91d42013-10-21 13:30:52 -070057 unw_cursor_t cursor;
58 int ret = unw_init_remote(&cursor, addr_space_, upt_info_);
59 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070060 BACK_LOGW("unw_init_remote failed %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -070061 return false;
62 }
63
Christopher Ferris46756822014-01-14 20:16:30 -080064 std::vector<backtrace_frame_data_t>* frames = GetFrames();
65 frames->reserve(MAX_BACKTRACE_FRAMES);
66 size_t num_frames = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -070067 do {
68 unw_word_t pc;
69 ret = unw_get_reg(&cursor, UNW_REG_IP, &pc);
70 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070071 BACK_LOGW("Failed to read IP %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -070072 break;
73 }
74 unw_word_t sp;
75 ret = unw_get_reg(&cursor, UNW_REG_SP, &sp);
76 if (ret < 0) {
Christopher Ferris8ed46272013-10-29 15:44:25 -070077 BACK_LOGW("Failed to read SP %d", ret);
Christopher Ferris17e91d42013-10-21 13:30:52 -070078 break;
79 }
80
81 if (num_ignore_frames == 0) {
Christopher Ferris46756822014-01-14 20:16:30 -080082 frames->resize(num_frames+1);
83 backtrace_frame_data_t* frame = &frames->at(num_frames);
Christopher Ferris20303f82014-01-10 16:33:16 -080084 frame->num = num_frames;
Christopher Ferris17e91d42013-10-21 13:30:52 -070085 frame->pc = static_cast<uintptr_t>(pc);
86 frame->sp = static_cast<uintptr_t>(sp);
87 frame->stack_size = 0;
Christopher Ferris17e91d42013-10-21 13:30:52 -070088
89 if (num_frames > 0) {
Christopher Ferris46756822014-01-14 20:16:30 -080090 backtrace_frame_data_t* prev = &frames->at(num_frames-1);
Christopher Ferris17e91d42013-10-21 13:30:52 -070091 prev->stack_size = frame->sp - prev->sp;
92 }
93
Christopher Ferris46756822014-01-14 20:16:30 -080094 frame->func_name = backtrace_obj_->GetFunctionName(frame->pc, &frame->func_offset);
Christopher Ferris17e91d42013-10-21 13:30:52 -070095
Christopher Ferris46756822014-01-14 20:16:30 -080096 frame->map = backtrace_obj_->FindMap(frame->pc);
Christopher Ferris17e91d42013-10-21 13:30:52 -070097
Christopher Ferris46756822014-01-14 20:16:30 -080098 num_frames++;
Christopher Ferris17e91d42013-10-21 13:30:52 -070099 } else {
100 num_ignore_frames--;
101 }
102 ret = unw_step (&cursor);
Christopher Ferris46756822014-01-14 20:16:30 -0800103 } while (ret > 0 && num_frames < MAX_BACKTRACE_FRAMES);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700104
105 return true;
106}
107
108std::string UnwindPtrace::GetFunctionNameRaw(uintptr_t pc, uintptr_t* offset) {
109 *offset = 0;
110 char buf[512];
111 unw_word_t value;
112 if (unw_get_proc_name_by_ip(addr_space_, pc, buf, sizeof(buf), &value,
113 upt_info_) >= 0 && buf[0] != '\0') {
114 *offset = static_cast<uintptr_t>(value);
115 return buf;
116 }
117 return "";
118}
119
120//-------------------------------------------------------------------------
121// C++ object creation function.
122//-------------------------------------------------------------------------
Christopher Ferris46756822014-01-14 20:16:30 -0800123Backtrace* CreatePtraceObj(pid_t pid, pid_t tid, BacktraceMap* map) {
124 return new BacktracePtrace(new UnwindPtrace(), pid, tid, map);
Christopher Ferris17e91d42013-10-21 13:30:52 -0700125}