summaryrefslogtreecommitdiffstats
path: root/libbacktrace/Backtrace.cpp
diff options
context:
space:
mode:
authorChristopher Ferris <cferris@google.com>2014-01-22 19:21:07 -0800
committerChristopher Ferris <cferris@google.com>2014-01-28 12:03:36 -0800
commitdf2906186b6952c57b1f662bfef0b65c9f8c2e0d (patch)
treee5ed8339b7fd42ff20047244f87106c8f1fa0d45 /libbacktrace/Backtrace.cpp
parent6ddabb7a1cc3080ae773acb045f69b5e6afee87a (diff)
downloadsystem_core-df2906186b6952c57b1f662bfef0b65c9f8c2e0d.zip
system_core-df2906186b6952c57b1f662bfef0b65c9f8c2e0d.tar.gz
system_core-df2906186b6952c57b1f662bfef0b65c9f8c2e0d.tar.bz2
Re-enable libunwind for arm.
Update to handle the new optimized way that libunwind works. In addition, a small refactor of the BacktraceMap code. A few new tests of for good measure. Change-Id: I2f9b4f5ad5a0dfe907b31febee76e4b9b94fb76f
Diffstat (limited to 'libbacktrace/Backtrace.cpp')
-rw-r--r--libbacktrace/Backtrace.cpp228
1 files changed, 0 insertions, 228 deletions
diff --git a/libbacktrace/Backtrace.cpp b/libbacktrace/Backtrace.cpp
deleted file mode 100644
index d7b40ab..0000000
--- a/libbacktrace/Backtrace.cpp
+++ /dev/null
@@ -1,228 +0,0 @@
-/*
- * Copyright (C) 2013 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#include <errno.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/ptrace.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-#define __STDC_FORMAT_MACROS
-#include <inttypes.h>
-
-#include <string>
-
-#include <backtrace/Backtrace.h>
-#include <backtrace/BacktraceMap.h>
-
-#include "Backtrace.h"
-#include "thread_utils.h"
-
-//-------------------------------------------------------------------------
-// Backtrace functions.
-//-------------------------------------------------------------------------
-Backtrace::Backtrace(BacktraceImpl* impl, pid_t pid, BacktraceMap* map)
- : pid_(pid), tid_(-1), map_(map), map_shared_(true), impl_(impl) {
- impl_->SetParent(this);
-
- if (map_ == NULL) {
- // The map will be created when needed.
- map_shared_ = false;
- }
-}
-
-Backtrace::~Backtrace() {
- if (map_ && !map_shared_) {
- delete map_;
- map_ = NULL;
- }
-
- if (impl_) {
- delete impl_;
- impl_ = NULL;
- }
-}
-
-bool Backtrace::Unwind(size_t num_ignore_frames) {
- return impl_->Unwind(num_ignore_frames);
-}
-
-extern "C" char* __cxa_demangle(const char* mangled, char* buf, size_t* len,
- int* status);
-
-std::string Backtrace::GetFunctionName(uintptr_t pc, uintptr_t* offset) {
- std::string func_name = impl_->GetFunctionNameRaw(pc, offset);
- if (!func_name.empty()) {
-#if defined(__APPLE__)
- // Mac OS' __cxa_demangle demangles "f" as "float"; last tested on 10.7.
- if (func_name[0] != '_') {
- return func_name;
- }
-#endif
- char* name = __cxa_demangle(func_name.c_str(), 0, 0, 0);
- if (name) {
- func_name = name;
- free(name);
- }
- }
- return func_name;
-}
-
-bool Backtrace::VerifyReadWordArgs(uintptr_t ptr, uint32_t* out_value) {
- if (ptr & 3) {
- BACK_LOGW("invalid pointer %p", (void*)ptr);
- *out_value = (uint32_t)-1;
- return false;
- }
- return true;
-}
-
-std::string Backtrace::FormatFrameData(size_t frame_num) {
- if (frame_num >= frames_.size()) {
- return "";
- }
- return FormatFrameData(&frames_[frame_num]);
-}
-
-std::string Backtrace::FormatFrameData(const backtrace_frame_data_t* frame) {
- const char* map_name;
- if (frame->map && !frame->map->name.empty()) {
- map_name = frame->map->name.c_str();
- } else {
- map_name = "<unknown>";
- }
-
- uintptr_t relative_pc;
- if (frame->map) {
- relative_pc = frame->pc - frame->map->start;
- } else {
- relative_pc = frame->pc;
- }
-
- char buf[512];
- if (!frame->func_name.empty() && frame->func_offset) {
- snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR " %s (%s+%" PRIuPTR ")",
- frame->num, (int)sizeof(uintptr_t)*2, relative_pc, map_name,
- frame->func_name.c_str(), frame->func_offset);
- } else if (!frame->func_name.empty()) {
- snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR " %s (%s)", frame->num,
- (int)sizeof(uintptr_t)*2, relative_pc, map_name, frame->func_name.c_str());
- } else {
- snprintf(buf, sizeof(buf), "#%02zu pc %0*" PRIxPTR " %s", frame->num,
- (int)sizeof(uintptr_t)*2, relative_pc, map_name);
- }
-
- return buf;
-}
-
-bool Backtrace::BuildMap() {
- map_ = impl_->CreateBacktraceMap(pid_);
- if (!map_->Build()) {
- BACK_LOGW("Failed to build map for process %d", pid_);
- return false;
- }
- return true;
-}
-
-const backtrace_map_t* Backtrace::FindMap(uintptr_t pc) {
- if (map_ == NULL) {
- // Lazy eval, time to build the map.
- if (!BuildMap()) {
- return NULL;
- }
- }
- return map_->Find(pc);
-}
-
-BacktraceMap* Backtrace::TakeMapOwnership() {
- map_shared_ = true;
- return map_;
-}
-
-//-------------------------------------------------------------------------
-// BacktraceCurrent functions.
-//-------------------------------------------------------------------------
-BacktraceCurrent::BacktraceCurrent(
- BacktraceImpl* impl, BacktraceMap* map) : Backtrace(impl, getpid(), map) {
-}
-
-BacktraceCurrent::~BacktraceCurrent() {
-}
-
-bool BacktraceCurrent::ReadWord(uintptr_t ptr, uint32_t* out_value) {
- if (!VerifyReadWordArgs(ptr, out_value)) {
- return false;
- }
-
- const backtrace_map_t* map = FindMap(ptr);
- if (map && map->flags & PROT_READ) {
- *out_value = *reinterpret_cast<uint32_t*>(ptr);
- return true;
- } else {
- BACK_LOGW("pointer %p not in a readable map", reinterpret_cast<void*>(ptr));
- *out_value = static_cast<uint32_t>(-1);
- return false;
- }
-}
-
-//-------------------------------------------------------------------------
-// BacktracePtrace functions.
-//-------------------------------------------------------------------------
-BacktracePtrace::BacktracePtrace(
- BacktraceImpl* impl, pid_t pid, pid_t tid, BacktraceMap* map)
- : Backtrace(impl, pid, map) {
- tid_ = tid;
-}
-
-BacktracePtrace::~BacktracePtrace() {
-}
-
-bool BacktracePtrace::ReadWord(uintptr_t ptr, uint32_t* out_value) {
- if (!VerifyReadWordArgs(ptr, out_value)) {
- return false;
- }
-
-#if defined(__APPLE__)
- BACK_LOGW("MacOS does not support reading from another pid.");
- return false;
-#else
- // ptrace() returns -1 and sets errno when the operation fails.
- // To disambiguate -1 from a valid result, we clear errno beforehand.
- errno = 0;
- *out_value = ptrace(PTRACE_PEEKTEXT, Tid(), reinterpret_cast<void*>(ptr), NULL);
- if (*out_value == static_cast<uint32_t>(-1) && errno) {
- BACK_LOGW("invalid pointer %p reading from tid %d, ptrace() strerror(errno)=%s",
- reinterpret_cast<void*>(ptr), Tid(), strerror(errno));
- return false;
- }
- return true;
-#endif
-}
-
-Backtrace* Backtrace::Create(pid_t pid, pid_t tid, BacktraceMap* map) {
- if (pid == BACKTRACE_CURRENT_PROCESS || pid == getpid()) {
- if (tid == BACKTRACE_CURRENT_THREAD || tid == gettid()) {
- return CreateCurrentObj(map);
- } else {
- return CreateThreadObj(tid, map);
- }
- } else if (tid == BACKTRACE_CURRENT_THREAD) {
- return CreatePtraceObj(pid, pid, map);
- } else {
- return CreatePtraceObj(pid, tid, map);
- }
-}