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