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