blob: 0d2e11bdf6f37d9949bc4c85ca227525fe90c166 [file] [log] [blame]
Christopher Ferris2c43cff2015-03-26 19:18:36 -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#include <inttypes.h>
18#include <stdint.h>
19#include <stdlib.h>
20#include <sys/types.h>
21#include <ucontext.h>
22
23#include <string>
24
Elliott Hughes4f713192015-12-04 22:00:26 -080025#include <android-base/stringprintf.h>
Christopher Ferris2c43cff2015-03-26 19:18:36 -070026
27#include <backtrace/Backtrace.h>
28#include <backtrace/BacktraceMap.h>
29
Christopher Ferris2c43cff2015-03-26 19:18:36 -070030#include "BacktraceLog.h"
31#include "thread_utils.h"
32#include "UnwindCurrent.h"
33#include "UnwindPtrace.h"
34
35using android::base::StringPrintf;
36
37//-------------------------------------------------------------------------
38// Backtrace functions.
39//-------------------------------------------------------------------------
40Backtrace::Backtrace(pid_t pid, pid_t tid, BacktraceMap* map)
41 : pid_(pid), tid_(tid), map_(map), map_shared_(true) {
42 if (map_ == nullptr) {
43 map_ = BacktraceMap::Create(pid);
44 map_shared_ = false;
45 }
46}
47
48Backtrace::~Backtrace() {
49 if (map_ && !map_shared_) {
50 delete map_;
51 map_ = nullptr;
52 }
53}
54
Christopher Ferris2c43cff2015-03-26 19:18:36 -070055std::string Backtrace::GetFunctionName(uintptr_t pc, uintptr_t* offset) {
56 std::string func_name = GetFunctionNameRaw(pc, offset);
Christopher Ferris2c43cff2015-03-26 19:18:36 -070057 return func_name;
58}
59
60bool Backtrace::VerifyReadWordArgs(uintptr_t ptr, word_t* out_value) {
61 if (ptr & (sizeof(word_t)-1)) {
62 BACK_LOGW("invalid pointer %p", reinterpret_cast<void*>(ptr));
63 *out_value = static_cast<word_t>(-1);
64 return false;
65 }
66 return true;
67}
68
69std::string Backtrace::FormatFrameData(size_t frame_num) {
70 if (frame_num >= frames_.size()) {
71 return "";
72 }
73 return FormatFrameData(&frames_[frame_num]);
74}
75
76std::string Backtrace::FormatFrameData(const backtrace_frame_data_t* frame) {
Christopher Ferrisda750a72015-11-30 13:36:08 -080077 uintptr_t relative_pc;
78 std::string map_name;
79 if (BacktraceMap::IsValid(frame->map)) {
80 relative_pc = BacktraceMap::GetRelativePc(frame->map, frame->pc);
81 if (!frame->map.name.empty()) {
82 map_name = frame->map.name.c_str();
83 if (map_name[0] == '[' && map_name[map_name.size() - 1] == ']') {
84 map_name.resize(map_name.size() - 1);
85 map_name += StringPrintf(":%" PRIPTR "]", frame->map.start);
86 }
87 } else {
88 map_name = StringPrintf("<anonymous:%" PRIPTR ">", frame->map.start);
89 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -070090 } else {
91 map_name = "<unknown>";
Christopher Ferrisda750a72015-11-30 13:36:08 -080092 relative_pc = frame->pc;
Christopher Ferris2c43cff2015-03-26 19:18:36 -070093 }
94
Christopher Ferrisda750a72015-11-30 13:36:08 -080095 std::string line(StringPrintf("#%02zu pc %" PRIPTR " ", frame->num, relative_pc));
96 line += map_name;
Christopher Ferris60001732015-08-20 11:16:54 -070097 // Special handling for non-zero offset maps, we need to print that
98 // information.
99 if (frame->map.offset != 0) {
100 line += " (offset " + StringPrintf("0x%" PRIxPTR, frame->map.offset) + ")";
101 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700102 if (!frame->func_name.empty()) {
103 line += " (" + frame->func_name;
104 if (frame->func_offset) {
105 line += StringPrintf("+%" PRIuPTR, frame->func_offset);
106 }
107 line += ')';
108 }
109
110 return line;
111}
112
113void Backtrace::FillInMap(uintptr_t pc, backtrace_map_t* map) {
Christopher Ferris30c942c2015-05-14 15:39:52 -0700114 if (map_ != nullptr) {
115 map_->FillIn(pc, map);
116 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700117}
118
119Backtrace* Backtrace::Create(pid_t pid, pid_t tid, BacktraceMap* map) {
120 if (pid == BACKTRACE_CURRENT_PROCESS) {
121 pid = getpid();
122 if (tid == BACKTRACE_CURRENT_THREAD) {
123 tid = gettid();
124 }
125 } else if (tid == BACKTRACE_CURRENT_THREAD) {
126 tid = pid;
127 }
128
129 if (pid == getpid()) {
130 return new UnwindCurrent(pid, tid, map);
131 } else {
132 return new UnwindPtrace(pid, tid, map);
133 }
134}
Christopher Ferrisc463ba42016-03-09 14:35:54 -0800135
136std::string Backtrace::GetErrorString(BacktraceUnwindError error) {
137 switch (error) {
138 case BACKTRACE_UNWIND_NO_ERROR:
139 return "No error";
140 case BACKTRACE_UNWIND_ERROR_SETUP_FAILED:
141 return "Setup failed";
142 case BACKTRACE_UNWIND_ERROR_MAP_MISSING:
143 return "No map found";
144 case BACKTRACE_UNWIND_ERROR_INTERNAL:
145 return "Internal libbacktrace error, please submit a bugreport";
146 case BACKTRACE_UNWIND_ERROR_THREAD_DOESNT_EXIST:
147 return "Thread doesn't exist";
148 case BACKTRACE_UNWIND_ERROR_THREAD_TIMEOUT:
149 return "Thread has not repsonded to signal in time";
150 case BACKTRACE_UNWIND_ERROR_UNSUPPORTED_OPERATION:
151 return "Attempt to use an unsupported feature";
152 case BACKTRACE_UNWIND_ERROR_NO_CONTEXT:
153 return "Attempt to do an offline unwind without a context";
154 }
155}