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