1// Copyright (c) 2012 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include "base/debug/stack_trace.h" 6 7#include <android/log.h> 8#include <unwind.h> 9 10#include "base/debug/proc_maps_linux.h" 11#include "base/strings/stringprintf.h" 12#include "base/threading/thread_restrictions.h" 13 14namespace { 15 16struct StackCrawlState { 17 StackCrawlState(uintptr_t* frames, size_t max_depth) 18 : frames(frames), 19 frame_count(0), 20 max_depth(max_depth), 21 have_skipped_self(false) {} 22 23 uintptr_t* frames; 24 size_t frame_count; 25 size_t max_depth; 26 bool have_skipped_self; 27}; 28 29// Clang's unwind.h doesn't provide _Unwind_GetIP on ARM, refer to 30// http://llvm.org/bugs/show_bug.cgi?id=16564 for details. 31#if defined(__clang__) 32uintptr_t _Unwind_GetIP(_Unwind_Context* context) { 33 uintptr_t ip = 0; 34 _Unwind_VRS_Get(context, _UVRSC_CORE, 15, _UVRSD_UINT32, &ip); 35 return ip & ~static_cast<uintptr_t>(0x1); // Remove thumb mode bit. 36} 37#endif 38 39_Unwind_Reason_Code TraceStackFrame(_Unwind_Context* context, void* arg) { 40 StackCrawlState* state = static_cast<StackCrawlState*>(arg); 41 uintptr_t ip = _Unwind_GetIP(context); 42 43 // The first stack frame is this function itself. Skip it. 44 if (ip != 0 && !state->have_skipped_self) { 45 state->have_skipped_self = true; 46 return _URC_NO_REASON; 47 } 48 49 state->frames[state->frame_count++] = ip; 50 if (state->frame_count >= state->max_depth) 51 return _URC_END_OF_STACK; 52 return _URC_NO_REASON; 53} 54 55} // namespace 56 57namespace base { 58namespace debug { 59 60bool EnableInProcessStackDumping() { 61 // When running in an application, our code typically expects SIGPIPE 62 // to be ignored. Therefore, when testing that same code, it should run 63 // with SIGPIPE ignored as well. 64 // TODO(phajdan.jr): De-duplicate this SIGPIPE code. 65 struct sigaction action; 66 memset(&action, 0, sizeof(action)); 67 action.sa_handler = SIG_IGN; 68 sigemptyset(&action.sa_mask); 69 return (sigaction(SIGPIPE, &action, NULL) == 0); 70} 71 72StackTrace::StackTrace() { 73 StackCrawlState state(reinterpret_cast<uintptr_t*>(trace_), kMaxTraces); 74 _Unwind_Backtrace(&TraceStackFrame, &state); 75 count_ = state.frame_count; 76} 77 78void StackTrace::PrintBacktrace() const { 79 std::string backtrace = ToString(); 80 __android_log_write(ANDROID_LOG_ERROR, "chromium", backtrace.c_str()); 81} 82 83// NOTE: Native libraries in APKs are stripped before installing. Print out the 84// relocatable address and library names so host computers can use tools to 85// symbolize and demangle (e.g., addr2line, c++filt). 86void StackTrace::OutputToStream(std::ostream* os) const { 87 std::string proc_maps; 88 std::vector<MappedMemoryRegion> regions; 89 // Allow IO to read /proc/self/maps. Reading this file doesn't hit the disk 90 // since it lives in procfs, and this is currently used to print a stack trace 91 // on fatal log messages in debug builds only. If the restriction is enabled 92 // then it will recursively trigger fatal failures when this enters on the 93 // UI thread. 94 base::ThreadRestrictions::ScopedAllowIO allow_io; 95 if (!ReadProcMaps(&proc_maps)) { 96 __android_log_write( 97 ANDROID_LOG_ERROR, "chromium", "Failed to read /proc/self/maps"); 98 } else if (!ParseProcMaps(proc_maps, ®ions)) { 99 __android_log_write( 100 ANDROID_LOG_ERROR, "chromium", "Failed to parse /proc/self/maps"); 101 } 102 103 for (size_t i = 0; i < count_; ++i) { 104 // Subtract one as return address of function may be in the next 105 // function when a function is annotated as noreturn. 106 uintptr_t address = reinterpret_cast<uintptr_t>(trace_[i]) - 1; 107 108 std::vector<MappedMemoryRegion>::iterator iter = regions.begin(); 109 while (iter != regions.end()) { 110 if (address >= iter->start && address < iter->end && 111 !iter->path.empty()) { 112 break; 113 } 114 ++iter; 115 } 116 117 *os << base::StringPrintf("#%02d 0x%08x ", i, address); 118 119 if (iter != regions.end()) { 120 uintptr_t rel_pc = address - iter->start + iter->offset; 121 const char* path = iter->path.c_str(); 122 *os << base::StringPrintf("%s+0x%08x", path, rel_pc); 123 } else { 124 *os << "<unknown>"; 125 } 126 127 *os << "\n"; 128 } 129} 130 131} // namespace debug 132} // namespace base 133