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