blob: 128bb047b390562c9300edb6e3849d5a6297b4c6 [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
25#include <base/stringprintf.h>
26
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"
33#include "thread_utils.h"
34#include "UnwindCurrent.h"
35#include "UnwindPtrace.h"
36
37using android::base::StringPrintf;
38
39//-------------------------------------------------------------------------
40// Backtrace functions.
41//-------------------------------------------------------------------------
42Backtrace::Backtrace(pid_t pid, pid_t tid, BacktraceMap* map)
43 : pid_(pid), tid_(tid), map_(map), map_shared_(true) {
44 if (map_ == nullptr) {
45 map_ = BacktraceMap::Create(pid);
46 map_shared_ = false;
47 }
48}
49
50Backtrace::~Backtrace() {
51 if (map_ && !map_shared_) {
52 delete map_;
53 map_ = nullptr;
54 }
55}
56
57extern "C" char* __cxa_demangle(const char* mangled, char* buf, size_t* len,
58 int* status);
59
60std::string Backtrace::GetFunctionName(uintptr_t pc, uintptr_t* offset) {
61 std::string func_name = GetFunctionNameRaw(pc, offset);
62 if (!func_name.empty()) {
63#if defined(__APPLE__)
64 // Mac OS' __cxa_demangle demangles "f" as "float"; last tested on 10.7.
65 if (func_name[0] != '_') {
66 return func_name;
67 }
68#endif
69 char* name = __cxa_demangle(func_name.c_str(), 0, 0, 0);
70 if (name) {
71 func_name = name;
72 free(name);
73 }
74 }
75 return func_name;
76}
77
78bool Backtrace::VerifyReadWordArgs(uintptr_t ptr, word_t* out_value) {
79 if (ptr & (sizeof(word_t)-1)) {
80 BACK_LOGW("invalid pointer %p", reinterpret_cast<void*>(ptr));
81 *out_value = static_cast<word_t>(-1);
82 return false;
83 }
84 return true;
85}
86
87std::string Backtrace::FormatFrameData(size_t frame_num) {
88 if (frame_num >= frames_.size()) {
89 return "";
90 }
91 return FormatFrameData(&frames_[frame_num]);
92}
93
94std::string Backtrace::FormatFrameData(const backtrace_frame_data_t* frame) {
95 const char* map_name;
96 if (BacktraceMap::IsValid(frame->map) && !frame->map.name.empty()) {
97 map_name = frame->map.name.c_str();
98 } else {
99 map_name = "<unknown>";
100 }
101
Christopher Ferris2106f4b2015-05-01 15:02:03 -0700102 uintptr_t relative_pc = BacktraceMap::GetRelativePc(frame->map, frame->pc);
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700103
104 std::string line(StringPrintf("#%02zu pc %" PRIPTR " %s", frame->num, relative_pc, map_name));
105 if (!frame->func_name.empty()) {
106 line += " (" + frame->func_name;
107 if (frame->func_offset) {
108 line += StringPrintf("+%" PRIuPTR, frame->func_offset);
109 }
110 line += ')';
111 }
112
113 return line;
114}
115
116void Backtrace::FillInMap(uintptr_t pc, backtrace_map_t* map) {
Christopher Ferris30c942c2015-05-14 15:39:52 -0700117 if (map_ != nullptr) {
118 map_->FillIn(pc, map);
119 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700120}
121
122Backtrace* Backtrace::Create(pid_t pid, pid_t tid, BacktraceMap* map) {
123 if (pid == BACKTRACE_CURRENT_PROCESS) {
124 pid = getpid();
125 if (tid == BACKTRACE_CURRENT_THREAD) {
126 tid = gettid();
127 }
128 } else if (tid == BACKTRACE_CURRENT_THREAD) {
129 tid = pid;
130 }
131
132 if (pid == getpid()) {
133 return new UnwindCurrent(pid, tid, map);
134 } else {
135 return new UnwindPtrace(pid, tid, map);
136 }
137}