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