blob: d75c2106bd7bdfb4cc2cf1c4400827fd5a0814ae [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
30#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
55extern "C" char* __cxa_demangle(const char* mangled, char* buf, size_t* len,
56 int* status);
57
58std::string Backtrace::GetFunctionName(uintptr_t pc, uintptr_t* offset) {
59 std::string func_name = GetFunctionNameRaw(pc, offset);
60 if (!func_name.empty()) {
61#if defined(__APPLE__)
62 // Mac OS' __cxa_demangle demangles "f" as "float"; last tested on 10.7.
63 if (func_name[0] != '_') {
64 return func_name;
65 }
66#endif
67 char* name = __cxa_demangle(func_name.c_str(), 0, 0, 0);
68 if (name) {
69 func_name = name;
70 free(name);
71 }
72 }
73 return func_name;
74}
75
76bool Backtrace::VerifyReadWordArgs(uintptr_t ptr, word_t* out_value) {
77 if (ptr & (sizeof(word_t)-1)) {
78 BACK_LOGW("invalid pointer %p", reinterpret_cast<void*>(ptr));
79 *out_value = static_cast<word_t>(-1);
80 return false;
81 }
82 return true;
83}
84
85std::string Backtrace::FormatFrameData(size_t frame_num) {
86 if (frame_num >= frames_.size()) {
87 return "";
88 }
89 return FormatFrameData(&frames_[frame_num]);
90}
91
92std::string Backtrace::FormatFrameData(const backtrace_frame_data_t* frame) {
93 const char* map_name;
94 if (BacktraceMap::IsValid(frame->map) && !frame->map.name.empty()) {
95 map_name = frame->map.name.c_str();
96 } else {
97 map_name = "<unknown>";
98 }
99
Christopher Ferris329ed7d2015-05-01 15:02:03 -0700100 uintptr_t relative_pc = BacktraceMap::GetRelativePc(frame->map, frame->pc);
Christopher Ferris2c43cff2015-03-26 19:18:36 -0700101
102 std::string line(StringPrintf("#%02zu pc %" PRIPTR " %s", frame->num, relative_pc, map_name));
103 if (!frame->func_name.empty()) {
104 line += " (" + frame->func_name;
105 if (frame->func_offset) {
106 line += StringPrintf("+%" PRIuPTR, frame->func_offset);
107 }
108 line += ')';
109 }
110
111 return line;
112}
113
114void Backtrace::FillInMap(uintptr_t pc, backtrace_map_t* map) {
115 map_->FillIn(pc, map);
116}
117
118Backtrace* Backtrace::Create(pid_t pid, pid_t tid, BacktraceMap* map) {
119 if (pid == BACKTRACE_CURRENT_PROCESS) {
120 pid = getpid();
121 if (tid == BACKTRACE_CURRENT_THREAD) {
122 tid = gettid();
123 }
124 } else if (tid == BACKTRACE_CURRENT_THREAD) {
125 tid = pid;
126 }
127
128 if (pid == getpid()) {
129 return new UnwindCurrent(pid, tid, map);
130 } else {
131 return new UnwindPtrace(pid, tid, map);
132 }
133}