Adds support for the embedded_test_server in webview's app_shell_browsertest.
[chromium-blink-merge.git] / base / debug / stack_trace_android.cc
blobc07f34a9957ac536eab7f4f50a4b9ad88898bfdd
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>
8 #include <unwind.h>
10 #include "base/debug/proc_maps_linux.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/threading/thread_restrictions.h"
14 #ifdef __LP64__
15 #define FMT_ADDR "0x%016lx"
16 #else
17 #define FMT_ADDR "0x%08x"
18 #endif
20 namespace {
22 struct StackCrawlState {
23 StackCrawlState(uintptr_t* frames, size_t max_depth)
24 : frames(frames),
25 frame_count(0),
26 max_depth(max_depth),
27 have_skipped_self(false) {}
29 uintptr_t* frames;
30 size_t frame_count;
31 size_t max_depth;
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;
51 } // namespace
53 namespace base {
54 namespace debug {
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
89 // UI thread.
90 base::ThreadRestrictions::ScopedAllowIO allow_io;
91 if (!ReadProcMaps(&proc_maps)) {
92 __android_log_write(
93 ANDROID_LOG_ERROR, "chromium", "Failed to read /proc/self/maps");
94 } else if (!ParseProcMaps(proc_maps, &regions)) {
95 __android_log_write(
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()) {
108 break;
110 ++iter;
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);
119 } else {
120 *os << "<unknown>";
123 *os << "\n";
127 } // namespace debug
128 } // namespace base