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 <stddef.h>
9 #include <unwind.h>
10
11 #include <algorithm>
12 #include <ostream>
13
14 #include "base/debug/proc_maps_linux.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/threading/thread_restrictions.h"
17
18 #ifdef __LP64__
19 #define FMT_ADDR "0x%016lx"
20 #else
21 #define FMT_ADDR "0x%08x"
22 #endif
23
24 namespace {
25
26 struct StackCrawlState {
StackCrawlState__anonef4b73ed0111::StackCrawlState27 StackCrawlState(uintptr_t* frames, size_t max_depth)
28 : frames(frames),
29 frame_count(0),
30 max_depth(max_depth),
31 have_skipped_self(false) {}
32
33 uintptr_t* frames;
34 size_t frame_count;
35 size_t max_depth;
36 bool have_skipped_self;
37 };
38
TraceStackFrame(_Unwind_Context * context,void * arg)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
57 namespace base {
58 namespace debug {
59
EnableInProcessStackDumping()60 bool 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
StackTrace(size_t count)72 StackTrace::StackTrace(size_t count) {
73 count = std::min(arraysize(trace_), count);
74
75 StackCrawlState state(reinterpret_cast<uintptr_t*>(trace_), count);
76 _Unwind_Backtrace(&TraceStackFrame, &state);
77 count_ = state.frame_count;
78 }
79
Print() const80 void StackTrace::Print() const {
81 std::string backtrace = ToString();
82 __android_log_write(ANDROID_LOG_ERROR, "chromium", backtrace.c_str());
83 }
84
85 // NOTE: Native libraries in APKs are stripped before installing. Print out the
86 // relocatable address and library names so host computers can use tools to
87 // symbolize and demangle (e.g., addr2line, c++filt).
OutputToStream(std::ostream * os) const88 void StackTrace::OutputToStream(std::ostream* os) const {
89 std::string proc_maps;
90 std::vector<MappedMemoryRegion> regions;
91 // Allow IO to read /proc/self/maps. Reading this file doesn't hit the disk
92 // since it lives in procfs, and this is currently used to print a stack trace
93 // on fatal log messages in debug builds only. If the restriction is enabled
94 // then it will recursively trigger fatal failures when this enters on the
95 // UI thread.
96 base::ThreadRestrictions::ScopedAllowIO allow_io;
97 if (!ReadProcMaps(&proc_maps)) {
98 __android_log_write(
99 ANDROID_LOG_ERROR, "chromium", "Failed to read /proc/self/maps");
100 } else if (!ParseProcMaps(proc_maps, ®ions)) {
101 __android_log_write(
102 ANDROID_LOG_ERROR, "chromium", "Failed to parse /proc/self/maps");
103 }
104
105 for (size_t i = 0; i < count_; ++i) {
106 // Subtract one as return address of function may be in the next
107 // function when a function is annotated as noreturn.
108 uintptr_t address = reinterpret_cast<uintptr_t>(trace_[i]) - 1;
109
110 std::vector<MappedMemoryRegion>::iterator iter = regions.begin();
111 while (iter != regions.end()) {
112 if (address >= iter->start && address < iter->end &&
113 !iter->path.empty()) {
114 break;
115 }
116 ++iter;
117 }
118
119 *os << base::StringPrintf("#%02zd " FMT_ADDR " ", i, address);
120
121 if (iter != regions.end()) {
122 uintptr_t rel_pc = address - iter->start + iter->offset;
123 const char* path = iter->path.c_str();
124 *os << base::StringPrintf("%s+" FMT_ADDR, path, rel_pc);
125 } else {
126 *os << "<unknown>";
127 }
128
129 *os << "\n";
130 }
131 }
132
133 } // namespace debug
134 } // namespace base
135