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