• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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/task_annotator.h"
6 
7 #include <array>
8 
9 #include "base/debug/activity_tracker.h"
10 #include "base/debug/alias.h"
11 #include "base/no_destructor.h"
12 #include "base/pending_task.h"
13 #include "base/threading/thread_local.h"
14 #include "base/trace_event/trace_event.h"
15 
16 namespace base {
17 namespace debug {
18 
19 namespace {
20 
21 TaskAnnotator::ObserverForTesting* g_task_annotator_observer = nullptr;
22 
23 // Returns the TLS slot that stores the PendingTask currently in progress on
24 // each thread. Used to allow creating a breadcrumb of program counters on the
25 // stack to help identify a task's origin in crashes.
GetTLSForCurrentPendingTask()26 ThreadLocalPointer<const PendingTask>* GetTLSForCurrentPendingTask() {
27   static NoDestructor<ThreadLocalPointer<const PendingTask>>
28       tls_for_current_pending_task;
29   return tls_for_current_pending_task.get();
30 }
31 
32 }  // namespace
33 
34 TaskAnnotator::TaskAnnotator() = default;
35 
36 TaskAnnotator::~TaskAnnotator() = default;
37 
WillQueueTask(const char * queue_function,PendingTask * pending_task)38 void TaskAnnotator::WillQueueTask(const char* queue_function,
39                                   PendingTask* pending_task) {
40   if (queue_function) {
41     TRACE_EVENT_WITH_FLOW0(TRACE_DISABLED_BY_DEFAULT("toplevel.flow"),
42                            queue_function,
43                            TRACE_ID_MANGLE(GetTaskTraceID(*pending_task)),
44                            TRACE_EVENT_FLAG_FLOW_OUT);
45   }
46 
47   // TODO(https://crbug.com/826902): Fix callers that invoke WillQueueTask()
48   // twice for the same PendingTask.
49   // DCHECK(!pending_task.task_backtrace[0])
50   //     << "Task backtrace was already set, task posted twice??";
51   if (!pending_task->task_backtrace[0]) {
52     const PendingTask* parent_task = GetTLSForCurrentPendingTask()->Get();
53     if (parent_task) {
54       pending_task->task_backtrace[0] =
55           parent_task->posted_from.program_counter();
56       std::copy(parent_task->task_backtrace.begin(),
57                 parent_task->task_backtrace.end() - 1,
58                 pending_task->task_backtrace.begin() + 1);
59     }
60   }
61 }
62 
RunTask(const char * queue_function,PendingTask * pending_task)63 void TaskAnnotator::RunTask(const char* queue_function,
64                             PendingTask* pending_task) {
65   ScopedTaskRunActivity task_activity(*pending_task);
66 
67   if (queue_function) {
68     TRACE_EVENT_WITH_FLOW0(TRACE_DISABLED_BY_DEFAULT("toplevel.flow"),
69                            queue_function,
70                            TRACE_ID_MANGLE(GetTaskTraceID(*pending_task)),
71                            TRACE_EVENT_FLAG_FLOW_IN);
72   }
73 
74   // Before running the task, store the task backtrace with the chain of
75   // PostTasks that resulted in this call and deliberately alias it to ensure
76   // it is on the stack if the task crashes. Be careful not to assume that the
77   // variable itself will have the expected value when displayed by the
78   // optimizer in an optimized build. Look at a memory dump of the stack.
79   static constexpr int kStackTaskTraceSnapshotSize =
80       std::tuple_size<decltype(pending_task->task_backtrace)>::value + 3;
81   std::array<const void*, kStackTaskTraceSnapshotSize> task_backtrace;
82 
83   // Store a marker to locate |task_backtrace| content easily on a memory
84   // dump.
85   task_backtrace.front() = reinterpret_cast<void*>(0xefefefefefefefef);
86   task_backtrace.back() = reinterpret_cast<void*>(0xfefefefefefefefe);
87 
88   task_backtrace[1] = pending_task->posted_from.program_counter();
89   std::copy(pending_task->task_backtrace.begin(),
90             pending_task->task_backtrace.end(), task_backtrace.begin() + 2);
91   debug::Alias(&task_backtrace);
92 
93   ThreadLocalPointer<const PendingTask>* tls_for_current_pending_task =
94       GetTLSForCurrentPendingTask();
95   const PendingTask* previous_pending_task =
96       tls_for_current_pending_task->Get();
97   tls_for_current_pending_task->Set(pending_task);
98 
99   if (g_task_annotator_observer)
100     g_task_annotator_observer->BeforeRunTask(pending_task);
101   std::move(pending_task->task).Run();
102 
103   tls_for_current_pending_task->Set(previous_pending_task);
104 }
105 
GetTaskTraceID(const PendingTask & task) const106 uint64_t TaskAnnotator::GetTaskTraceID(const PendingTask& task) const {
107   return (static_cast<uint64_t>(task.sequence_num) << 32) |
108          ((static_cast<uint64_t>(reinterpret_cast<intptr_t>(this)) << 32) >>
109           32);
110 }
111 
112 // static
RegisterObserverForTesting(ObserverForTesting * observer)113 void TaskAnnotator::RegisterObserverForTesting(ObserverForTesting* observer) {
114   DCHECK(!g_task_annotator_observer);
115   g_task_annotator_observer = observer;
116 }
117 
118 // static
ClearObserverForTesting()119 void TaskAnnotator::ClearObserverForTesting() {
120   g_task_annotator_observer = nullptr;
121 }
122 
123 }  // namespace debug
124 }  // namespace base
125