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