1 // Copyright 2019 The Chromium Authors
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/task_trace.h"
6
7 #include <algorithm>
8 #include <iostream>
9 #include <sstream>
10
11 #include "base/pending_task.h"
12 #include "base/ranges/algorithm.h"
13 #include "base/task/common/task_annotator.h"
14 #include "build/build_config.h"
15
16 #if BUILDFLAG(IS_ANDROID)
17 #include <android/log.h>
18
19 #include "base/no_destructor.h"
20 #endif // BUILDFLAG(IS_ANDROID)
21
22 namespace base {
23 namespace debug {
24 namespace {
25 #if BUILDFLAG(IS_ANDROID)
26 // Android sends stdout and stderr to /dev/null; logging should be done through
27 // the __android_log_write() function. Here we create an override of
28 // std::stringbuf that writes to the Android log.
29 class AndroidErrBuffer : public std::stringbuf {
30 protected:
sync()31 int sync() override {
32 __android_log_write(ANDROID_LOG_ERROR, "chromium", str().c_str());
33 return 0;
34 }
35 };
36
DefaultOutputStream()37 std::ostream& DefaultOutputStream() {
38 static NoDestructor<AndroidErrBuffer> buf;
39 static NoDestructor<std::ostream> out(buf.get());
40 return *out;
41 }
42 #else
43 // Use stderr by default.
44 std::ostream& DefaultOutputStream() {
45 return std::cerr;
46 }
47 #endif // BUILDFLAG(IS_ANDROID)
48 } // namespace
49
TaskTrace()50 TaskTrace::TaskTrace() {
51 const PendingTask* current_task = TaskAnnotator::CurrentTaskForThread();
52 if (!current_task)
53 return;
54 std::array<const void*, PendingTask::kTaskBacktraceLength + 1> task_trace;
55 task_trace[0] = current_task->posted_from.program_counter();
56 ranges::copy(current_task->task_backtrace, task_trace.begin() + 1);
57 size_t length = 0;
58 while (length < task_trace.size() && task_trace[length])
59 ++length;
60 if (length == 0)
61 return;
62 stack_trace_.emplace(task_trace.data(), length);
63 trace_overflow_ = current_task->task_backtrace_overflow;
64 }
65
empty() const66 bool TaskTrace::empty() const {
67 return !stack_trace_.has_value();
68 }
69
Print() const70 void TaskTrace::Print() const {
71 OutputToStream(&DefaultOutputStream());
72 }
73
OutputToStream(std::ostream * os) const74 void TaskTrace::OutputToStream(std::ostream* os) const {
75 *os << "Task trace:" << std::endl;
76 if (!stack_trace_) {
77 *os << "No active task.";
78 return;
79 }
80 *os << *stack_trace_;
81 if (trace_overflow_) {
82 *os << "Task trace buffer limit hit, update "
83 "PendingTask::kTaskBacktraceLength to increase."
84 << std::endl;
85 }
86 }
87
ToString() const88 std::string TaskTrace::ToString() const {
89 std::stringstream stream;
90 OutputToStream(&stream);
91 return stream.str();
92 }
93
GetAddresses(span<const void * > addresses) const94 size_t TaskTrace::GetAddresses(span<const void*> addresses) const {
95 size_t count = 0;
96 if (empty()) {
97 return count;
98 }
99 span<const void* const> current_addresses = stack_trace_->addresses();
100 ranges::copy_n(current_addresses.begin(),
101 std::min(current_addresses.size(), addresses.size()),
102 addresses.begin());
103 return current_addresses.size();
104 }
105
operator <<(std::ostream & os,const TaskTrace & task_trace)106 std::ostream& operator<<(std::ostream& os, const TaskTrace& task_trace) {
107 task_trace.OutputToStream(&os);
108 return os;
109 }
110
111 } // namespace debug
112 } // namespace base
113