blob: df94280f6048fbe0c83e3ce8278a959ac92d79f0 [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
Dan Albert23f750b2015-04-30 12:52:21 -070030#include <cutils/threads.h>
31
Christopher Ferris2c43cff2015-03-26 19:18:36 -070032#include "BacktraceLog.h"
Yabin Cui9e402bb2015-09-22 04:46:57 +000033#include "BacktraceOffline.h"
Christopher Ferris2c43cff2015-03-26 19:18:36 -070034#include "thread_utils.h"
35#include "UnwindCurrent.h"
36#include "UnwindPtrace.h"
37
38using android::base::StringPrintf;
39
40//-------------------------------------------------------------------------
41// Backtrace functions.
42//-------------------------------------------------------------------------
43Backtrace::Backtrace(pid_t pid, pid_t tid, BacktraceMap* map)
44 : pid_(pid), tid_(tid), map_(map), map_shared_(true) {
45 if (map_ == nullptr) {
46 map_ = BacktraceMap::Create(pid);
47 map_shared_ = false;
48 }
49}
50
51Backtrace::~Backtrace() {
52 if (map_ && !map_shared_) {
53 delete map_;
54 map_ = nullptr;
55 }
56}
57
58extern "C" char* __cxa_demangle(const char* mangled, char* buf, size_t* len,
59 int* status);
60
61std::string Backtrace::GetFunctionName(uintptr_t pc, uintptr_t* offset) {
62 std::string func_name = GetFunctionNameRaw(pc, offset);
63 if (!func_name.empty()) {
64#if defined(__APPLE__)
65 // Mac OS' __cxa_demangle demangles "f" as "float"; last tested on 10.7.
66 if (func_name[0] != '_') {
67 return func_name;
68 }
69#endif
70 char* name = __cxa_demangle(func_name.c_str(), 0, 0, 0);
71 if (name) {
72 func_name = name;
73 free(name);
74 }
75 }
76 return func_name;
77}
78
79bool Backtrace::VerifyReadWordArgs(uintptr_t ptr, word_t* out_value) {
80 if (ptr & (sizeof(word_t)-1)) {
81 BACK_LOGW("invalid pointer %p", reinterpret_cast<void*>(ptr));
82 *out_value = static_cast<word_t>(-1);
83 return false;
84 }
85 return true;
86}
87
88std::string Backtrace::FormatFrameData(size_t frame_num) {
89 if (frame_num >= frames_.size()) {
90 return "";
91 }
92 return FormatFrameData(&frames_[frame_num]);
93}
94
95std::string Backtrace::FormatFrameData(const backtrace_frame_data_t* frame) {
Christopher Ferrisda750a72015-11-30 13:36:08 -080096 uintptr_t relative_pc;
97 std::string map_name;
98 if (BacktraceMap::IsValid(frame->map)) {
99 relative_pc = BacktraceMap::GetRelativePc(frame->map, frame->pc);
100 if (!frame->map.name.empty()) {
101 map_name = frame->map.name.c_str();
102 if (map_name[0] == '[' && map_name[map_name.size() - 1] == ']') {
103 map_name.resize(map_name.size() - 1);
104 map_name += StringPrintf(":%" PRIPTR "]", frame->map.start);
105 }
106 } else {
107 map_name = StringPrintf("<anonymous:%" PRIPTR ">", frame->map.start);
108 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700109 } else {
110 map_name = "<unknown>";
Christopher Ferrisda750a72015-11-30 13:36:08 -0800111 relative_pc = frame->pc;
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700112 }
113
Christopher Ferrisda750a72015-11-30 13:36:08 -0800114 std::string line(StringPrintf("#%02zu pc %" PRIPTR " ", frame->num, relative_pc));
115 line += map_name;
Christopher Ferris60001732015-08-20 11:16:54 -0700116 // Special handling for non-zero offset maps, we need to print that
117 // information.
118 if (frame->map.offset != 0) {
119 line += " (offset " + StringPrintf("0x%" PRIxPTR, frame->map.offset) + ")";
120 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700121 if (!frame->func_name.empty()) {
122 line += " (" + frame->func_name;
123 if (frame->func_offset) {
124 line += StringPrintf("+%" PRIuPTR, frame->func_offset);
125 }
126 line += ')';
127 }
128
129 return line;
130}
131
132void Backtrace::FillInMap(uintptr_t pc, backtrace_map_t* map) {
Christopher Ferris30c942c2015-05-14 15:39:52 -0700133 if (map_ != nullptr) {
134 map_->FillIn(pc, map);
135 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700136}
137
138Backtrace* Backtrace::Create(pid_t pid, pid_t tid, BacktraceMap* map) {
139 if (pid == BACKTRACE_CURRENT_PROCESS) {
140 pid = getpid();
141 if (tid == BACKTRACE_CURRENT_THREAD) {
142 tid = gettid();
143 }
144 } else if (tid == BACKTRACE_CURRENT_THREAD) {
145 tid = pid;
146 }
147
148 if (pid == getpid()) {
149 return new UnwindCurrent(pid, tid, map);
150 } else {
151 return new UnwindPtrace(pid, tid, map);
152 }
153}
Yabin Cui9e402bb2015-09-22 04:46:57 +0000154
155Backtrace* Backtrace::CreateOffline(pid_t pid, pid_t tid, BacktraceMap* map,
156 const backtrace_stackinfo_t& stack, bool cache_file) {
157 return new BacktraceOffline(pid, tid, map, stack, cache_file);
158}