BacktraceImpl.cpp revision a2efd3ac7abe223aa7a8ba8b5ba448216c4953b4
1/*
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 <errno.h>
18#include <stdlib.h>
19#include <string.h>
20#include <sys/ptrace.h>
21#include <sys/types.h>
22#include <unistd.h>
23
24#include <string>
25
26#include <backtrace/Backtrace.h>
27#include <backtrace/BacktraceMap.h>
28
29#include "BacktraceImpl.h"
30#include "BacktraceLog.h"
31#include "thread_utils.h"
32
33//-------------------------------------------------------------------------
34// Backtrace functions.
35//-------------------------------------------------------------------------
36Backtrace::Backtrace(BacktraceImpl* impl, pid_t pid, BacktraceMap* map)
37    : pid_(pid), tid_(-1), map_(map), map_shared_(true), impl_(impl) {
38  impl_->SetParent(this);
39
40  if (map_ == NULL) {
41    map_ = BacktraceMap::Create(pid);
42    map_shared_ = false;
43  }
44}
45
46Backtrace::~Backtrace() {
47  if (impl_) {
48    delete impl_;
49    impl_ = NULL;
50  }
51
52  if (map_ && !map_shared_) {
53    delete map_;
54    map_ = NULL;
55  }
56}
57
58bool Backtrace::Unwind(size_t num_ignore_frames, ucontext_t* ucontext) {
59  return impl_->Unwind(num_ignore_frames, ucontext);
60}
61
62extern "C" char* __cxa_demangle(const char* mangled, char* buf, size_t* len,
63                                int* status);
64
65std::string Backtrace::GetFunctionName(uintptr_t pc, uintptr_t* offset) {
66  std::string func_name = impl_->GetFunctionNameRaw(pc, offset);
67  if (!func_name.empty()) {
68#if defined(__APPLE__)
69    // Mac OS' __cxa_demangle demangles "f" as "float"; last tested on 10.7.
70    if (func_name[0] != '_') {
71      return func_name;
72    }
73#endif
74    char* name = __cxa_demangle(func_name.c_str(), 0, 0, 0);
75    if (name) {
76      func_name = name;
77      free(name);
78    }
79  }
80  return func_name;
81}
82
83bool Backtrace::VerifyReadWordArgs(uintptr_t ptr, word_t* out_value) {
84  if (ptr & (sizeof(word_t)-1)) {
85    BACK_LOGW("invalid pointer %p", (void*)ptr);
86    *out_value = (word_t)-1;
87    return false;
88  }
89  return true;
90}
91
92std::string Backtrace::FormatFrameData(size_t frame_num) {
93  if (frame_num >= frames_.size()) {
94    return "";
95  }
96  return FormatFrameData(&frames_[frame_num]);
97}
98
99std::string Backtrace::FormatFrameData(const backtrace_frame_data_t* frame) {
100  const char* map_name;
101  if (frame->map && !frame->map->name.empty()) {
102    map_name = frame->map->name.c_str();
103  } else {
104    map_name = "<unknown>";
105  }
106
107  uintptr_t relative_pc;
108  if (frame->map) {
109    relative_pc = frame->pc - frame->map->start;
110  } else {
111    relative_pc = frame->pc;
112  }
113
114  char buf[512];
115  if (!frame->func_name.empty() && frame->func_offset) {
116    snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR "  %s (%s+%" PRIuPTR ")",
117             frame->num, (int)sizeof(uintptr_t)*2, relative_pc, map_name,
118             frame->func_name.c_str(), frame->func_offset);
119  } else if (!frame->func_name.empty()) {
120    snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR "  %s (%s)", frame->num,
121             (int)sizeof(uintptr_t)*2, relative_pc, map_name, frame->func_name.c_str());
122  } else {
123    snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR "  %s", frame->num,
124             (int)sizeof(uintptr_t)*2, relative_pc, map_name);
125  }
126
127  return buf;
128}
129
130const backtrace_map_t* Backtrace::FindMap(uintptr_t pc) {
131  return map_->Find(pc);
132}
133
134//-------------------------------------------------------------------------
135// BacktraceCurrent functions.
136//-------------------------------------------------------------------------
137BacktraceCurrent::BacktraceCurrent(
138    BacktraceImpl* impl, BacktraceMap* map) : Backtrace(impl, getpid(), map) {
139}
140
141BacktraceCurrent::~BacktraceCurrent() {
142}
143
144bool BacktraceCurrent::ReadWord(uintptr_t ptr, word_t* out_value) {
145  if (!VerifyReadWordArgs(ptr, out_value)) {
146    return false;
147  }
148
149  const backtrace_map_t* map = FindMap(ptr);
150  if (map && map->flags & PROT_READ) {
151    *out_value = *reinterpret_cast<word_t*>(ptr);
152    return true;
153  } else {
154    BACK_LOGW("pointer %p not in a readable map", reinterpret_cast<void*>(ptr));
155    *out_value = static_cast<word_t>(-1);
156    return false;
157  }
158}
159
160//-------------------------------------------------------------------------
161// BacktracePtrace functions.
162//-------------------------------------------------------------------------
163BacktracePtrace::BacktracePtrace(
164    BacktraceImpl* impl, pid_t pid, pid_t tid, BacktraceMap* map)
165    : Backtrace(impl, pid, map) {
166  tid_ = tid;
167}
168
169BacktracePtrace::~BacktracePtrace() {
170}
171
172bool BacktracePtrace::ReadWord(uintptr_t ptr, word_t* out_value) {
173  if (!VerifyReadWordArgs(ptr, out_value)) {
174    return false;
175  }
176
177#if defined(__APPLE__)
178  BACK_LOGW("MacOS does not support reading from another pid.");
179  return false;
180#else
181  // ptrace() returns -1 and sets errno when the operation fails.
182  // To disambiguate -1 from a valid result, we clear errno beforehand.
183  errno = 0;
184  *out_value = ptrace(PTRACE_PEEKTEXT, Tid(), reinterpret_cast<void*>(ptr), NULL);
185  if (*out_value == static_cast<word_t>(-1) && errno) {
186    BACK_LOGW("invalid pointer %p reading from tid %d, ptrace() strerror(errno)=%s",
187              reinterpret_cast<void*>(ptr), Tid(), strerror(errno));
188    return false;
189  }
190  return true;
191#endif
192}
193
194Backtrace* Backtrace::Create(pid_t pid, pid_t tid, BacktraceMap* map) {
195  if (pid == BACKTRACE_CURRENT_PROCESS || pid == getpid()) {
196    if (tid == BACKTRACE_CURRENT_THREAD || tid == gettid()) {
197      return CreateCurrentObj(map);
198    } else {
199      return CreateThreadObj(tid, map);
200    }
201  } else if (tid == BACKTRACE_CURRENT_THREAD) {
202    return CreatePtraceObj(pid, pid, map);
203  } else {
204    return CreatePtraceObj(pid, tid, map);
205  }
206}
207