Record async "task backtraces"

Places a small array in PendingTask that records the past 4 PostTasks
that result in this task being run. In essence, it creates a backtrace
of the asynchronous call stack resulting in this task being run.

This array is copied onto the stack during execution so that it is
available in crash dumps.

BUG=690197

Review-Url: https://codereview.chromium.org/1044413002
Cr-Commit-Position: refs/heads/master@{#449471}
diff --git a/base/BUILD.gn b/base/BUILD.gn
index 99017c74..805b7a1d3 100644
--- a/base/BUILD.gn
+++ b/base/BUILD.gn
@@ -2019,6 +2019,7 @@
     "optional_unittest.cc",
     "os_compat_android_unittest.cc",
     "path_service_unittest.cc",
+    "pending_task_unittest.cc",
     "pickle_unittest.cc",
     "posix/file_descriptor_shuffle_unittest.cc",
     "posix/unix_domain_socket_linux_unittest.cc",
diff --git a/base/debug/task_annotator.cc b/base/debug/task_annotator.cc
index 437d69a7..46969f2 100644
--- a/base/debug/task_annotator.cc
+++ b/base/debug/task_annotator.cc
@@ -4,6 +4,8 @@
 
 #include "base/debug/task_annotator.h"
 
+#include <array>
+
 #include "base/debug/activity_tracker.h"
 #include "base/debug/alias.h"
 #include "base/pending_task.h"
@@ -41,13 +43,18 @@
       TRACE_ID_MANGLE(GetTaskTraceID(*pending_task)), TRACE_EVENT_FLAG_FLOW_IN,
       "queue_duration", queue_duration.InMilliseconds());
 
-  // Before running the task, store the program counter where it was posted
-  // and deliberately alias it to ensure it is on the stack if the task
-  // crashes. Be careful not to assume that the variable itself will have the
-  // expected value when displayed by the optimizer in an optimized build.
-  // Look at a memory dump of the stack.
-  const void* program_counter = pending_task->posted_from.program_counter();
-  debug::Alias(&program_counter);
+  // Before running the task, store the task backtrace with the chain of
+  // PostTasks that resulted in this call and deliberately alias it to ensure
+  // it is on the stack if the task crashes. Be careful not to assume that the
+  // variable itself will have the expected value when displayed by the
+  // optimizer in an optimized build. Look at a memory dump of the stack.
+  static constexpr int kStackTaskTraceSnapshotSize =
+      std::tuple_size<decltype(pending_task->task_backtrace)>::value + 1;
+  std::array<const void*, kStackTaskTraceSnapshotSize> task_backtrace;
+  task_backtrace[0] = pending_task->posted_from.program_counter();
+  std::copy(pending_task->task_backtrace.begin(),
+            pending_task->task_backtrace.end(), task_backtrace.begin() + 1);
+  debug::Alias(&task_backtrace);
 
   std::move(pending_task->task).Run();
 
diff --git a/base/message_loop/incoming_task_queue.cc b/base/message_loop/incoming_task_queue.cc
index c32afda1..c7faa9a 100644
--- a/base/message_loop/incoming_task_queue.cc
+++ b/base/message_loop/incoming_task_queue.cc
@@ -67,8 +67,8 @@
       << "Requesting super-long task delay period of " << delay.InSeconds()
       << " seconds from here: " << from_here.ToString();
 
-  PendingTask pending_task(
-      from_here, task, CalculateDelayedRuntime(delay), nestable);
+  PendingTask pending_task(from_here, task, CalculateDelayedRuntime(delay),
+                           nestable);
 #if defined(OS_WIN)
   // We consider the task needs a high resolution timer if the delay is
   // more than 0 and less than 32ms. This caps the relative error to
diff --git a/base/message_loop/message_loop.cc b/base/message_loop/message_loop.cc
index 72185a8..60df713 100644
--- a/base/message_loop/message_loop.cc
+++ b/base/message_loop/message_loop.cc
@@ -320,7 +320,8 @@
 #endif
       nestable_tasks_allowed_(true),
       pump_factory_(pump_factory),
-      run_loop_(NULL),
+      run_loop_(nullptr),
+      current_pending_task_(nullptr),
       incoming_task_queue_(new internal::IncomingTaskQueue(this)),
       unbound_task_runner_(
           new internal::MessageLoopTaskRunner(incoming_task_queue_)),
@@ -403,6 +404,7 @@
 
 void MessageLoop::RunTask(PendingTask* pending_task) {
   DCHECK(nestable_tasks_allowed_);
+  current_pending_task_ = pending_task;
 
 #if defined(OS_WIN)
   if (pending_task->is_high_res) {
@@ -423,6 +425,8 @@
     observer.DidProcessTask(*pending_task);
 
   nestable_tasks_allowed_ = true;
+
+  current_pending_task_ = nullptr;
 }
 
 bool MessageLoop::DeferOrRunPendingTask(PendingTask pending_task) {
diff --git a/base/message_loop/message_loop.h b/base/message_loop/message_loop.h
index 91a7b1d3..8417ce49c 100644
--- a/base/message_loop/message_loop.h
+++ b/base/message_loop/message_loop.h
@@ -346,11 +346,13 @@
   void BindToCurrentThread();
 
  private:
-  friend class RunLoop;
   friend class internal::IncomingTaskQueue;
+  friend class RunLoop;
   friend class ScheduleWorkTest;
   friend class Thread;
+  friend struct PendingTask;
   FRIEND_TEST_ALL_PREFIXES(MessageLoopTest, DeleteUnboundLoop);
+  friend class PendingTaskTest;
 
   // Creates a MessageLoop without binding to a thread.
   // If |type| is TYPE_CUSTOM non-null |pump_factory| must be also given
@@ -450,6 +452,13 @@
 
   debug::TaskAnnotator task_annotator_;
 
+  // Used to allow creating a breadcrumb of program counters in PostTask.
+  // This variable is only initialized while a task is being executed and is
+  // meant only to store context for creating a backtrace breadcrumb. Do not
+  // attach other semantics to it without thinking through the use caes
+  // thoroughly.
+  const PendingTask* current_pending_task_;
+
   scoped_refptr<internal::IncomingTaskQueue> incoming_task_queue_;
 
   // A task runner which we haven't bound to a thread yet.
diff --git a/base/pending_task.cc b/base/pending_task.cc
index cca9ebf..b2f95b4 100644
--- a/base/pending_task.cc
+++ b/base/pending_task.cc
@@ -4,18 +4,14 @@
 
 #include "base/pending_task.h"
 
+#include "base/message_loop/message_loop.h"
 #include "base/tracked_objects.h"
 
 namespace base {
 
 PendingTask::PendingTask(const tracked_objects::Location& posted_from,
                          OnceClosure task)
-    : base::TrackingInfo(posted_from, TimeTicks()),
-      task(std::move(task)),
-      posted_from(posted_from),
-      sequence_num(0),
-      nestable(true),
-      is_high_res(false) {}
+    : PendingTask(posted_from, std::move(task), TimeTicks(), true) {}
 
 PendingTask::PendingTask(const tracked_objects::Location& posted_from,
                          OnceClosure task,
@@ -26,7 +22,19 @@
       posted_from(posted_from),
       sequence_num(0),
       nestable(nestable),
-      is_high_res(false) {}
+      is_high_res(false) {
+  const PendingTask* parent_task =
+      MessageLoop::current() ? MessageLoop::current()->current_pending_task_
+                             : nullptr;
+  if (parent_task) {
+    task_backtrace[0] = parent_task->posted_from.program_counter();
+    std::copy(parent_task->task_backtrace.begin(),
+              parent_task->task_backtrace.end() - 1,
+              task_backtrace.begin() + 1);
+  } else {
+    task_backtrace.fill(nullptr);
+  }
+}
 
 PendingTask::PendingTask(PendingTask&& other) = default;
 
diff --git a/base/pending_task.h b/base/pending_task.h
index a55fa518..7f3fccd8 100644
--- a/base/pending_task.h
+++ b/base/pending_task.h
@@ -5,6 +5,7 @@
 #ifndef BASE_PENDING_TASK_H_
 #define BASE_PENDING_TASK_H_
 
+#include <array>
 #include <queue>
 
 #include "base/base_export.h"
@@ -37,6 +38,9 @@
   // The site this PendingTask was posted from.
   tracked_objects::Location posted_from;
 
+  // Task backtrace.
+  std::array<const void*, 4> task_backtrace;
+
   // Secondary sort key for run time.
   int sequence_num;
 
diff --git a/base/pending_task_unittest.cc b/base/pending_task_unittest.cc
new file mode 100644
index 0000000..8d52daa9
--- /dev/null
+++ b/base/pending_task_unittest.cc
@@ -0,0 +1,169 @@
+// Copyright (c) 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/pending_task.h"
+
+#include <vector>
+
+#include "base/bind.h"
+#include "base/message_loop/message_loop.h"
+#include "base/run_loop.h"
+#include "base/strings/stringprintf.h"
+#include "base/threading/thread.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace base {
+
+class PendingTaskTest : public ::testing::Test {
+ public:
+  PendingTaskTest() = default;
+
+  ~PendingTaskTest() override = default;
+
+ protected:
+  using ExpectedTrace = std::vector<const void*>;
+
+  static void VerifyTraceAndPost(
+      const scoped_refptr<TaskRunner>& task_runner,
+      const tracked_objects::Location& posted_from,
+      const tracked_objects::Location& next_from_here,
+      const std::vector<const void*>& expected_trace,
+      Closure task) {
+    SCOPED_TRACE(StringPrintf("Callback Depth: %zu", expected_trace.size()));
+
+    // Beyond depth + 1, the trace is nonsensical because there haven't been
+    // enough nested tasks called.
+    const PendingTask* current_pending_task =
+        MessageLoop::current()->current_pending_task_;
+    size_t window = std::min(current_pending_task->task_backtrace.size(),
+                             expected_trace.size());
+
+    EXPECT_EQ(posted_from,
+              MessageLoop::current()->current_pending_task_->posted_from);
+    for (size_t i = 0; i < window; i++) {
+      SCOPED_TRACE(StringPrintf("Trace frame: %zu", i));
+      EXPECT_EQ(expected_trace[i], current_pending_task->task_backtrace[i]);
+    }
+    task_runner->PostTask(next_from_here, std::move(task));
+  }
+
+  static void RunTwo(Closure c1, Closure c2) {
+    c1.Run();
+    c2.Run();
+  }
+};
+
+// Ensure the task backtrace populates correctly.
+TEST_F(PendingTaskTest, SingleThreadedSimple) {
+  MessageLoop loop;
+  const tracked_objects::Location& location0 = FROM_HERE;
+  const tracked_objects::Location& location1 = FROM_HERE;
+  const tracked_objects::Location& location2 = FROM_HERE;
+  const tracked_objects::Location& location3 = FROM_HERE;
+  const tracked_objects::Location& location4 = FROM_HERE;
+  const tracked_objects::Location& location5 = FROM_HERE;
+
+  Closure task5 = Bind(
+      &PendingTaskTest::VerifyTraceAndPost, loop.task_runner(), location4,
+      location5,
+      ExpectedTrace({location3.program_counter(), location2.program_counter(),
+                     location1.program_counter(), location0.program_counter()}),
+      Bind(&DoNothing));
+  Closure task4 = Bind(
+      &PendingTaskTest::VerifyTraceAndPost, loop.task_runner(), location3,
+      location4,
+      ExpectedTrace({location2.program_counter(), location1.program_counter(),
+                     location0.program_counter(), nullptr}),
+      task5);
+  Closure task3 = Bind(
+      &PendingTaskTest::VerifyTraceAndPost, loop.task_runner(), location2,
+      location3, ExpectedTrace({location1.program_counter(),
+                                location0.program_counter(), nullptr, nullptr}),
+      task4);
+  Closure task2 =
+      Bind(&PendingTaskTest::VerifyTraceAndPost, loop.task_runner(), location1,
+           location2, ExpectedTrace({location0.program_counter()}), task3);
+  Closure task1 = Bind(&PendingTaskTest::VerifyTraceAndPost, loop.task_runner(),
+                       location0, location1, ExpectedTrace({}), task2);
+
+  loop.task_runner()->PostTask(location0, task1);
+
+  RunLoop().RunUntilIdle();
+}
+
+// Post a task onto another thread. Ensure on the other thread, it has the
+// right stack trace.
+TEST_F(PendingTaskTest, MultipleThreads) {
+  MessageLoop loop;  // Implicitly "thread a."
+  Thread thread_b("pt_test_b");
+  Thread thread_c("pt_test_c");
+  thread_b.StartAndWaitForTesting();
+  thread_c.StartAndWaitForTesting();
+
+  const tracked_objects::Location& location_a0 = FROM_HERE;
+  const tracked_objects::Location& location_a1 = FROM_HERE;
+  const tracked_objects::Location& location_a2 = FROM_HERE;
+  const tracked_objects::Location& location_a3 = FROM_HERE;
+
+  const tracked_objects::Location& location_b0 = FROM_HERE;
+  const tracked_objects::Location& location_b1 = FROM_HERE;
+
+  const tracked_objects::Location& location_c0 = FROM_HERE;
+
+  // On thread c, post a task back to thread a that verifies its trace
+  // and terminates after one more self-post.
+  Closure task_a2 =
+      Bind(&PendingTaskTest::VerifyTraceAndPost, loop.task_runner(),
+           location_a2, location_a3,
+           ExpectedTrace(
+               {location_c0.program_counter(), location_b0.program_counter(),
+                location_a1.program_counter(), location_a0.program_counter()}),
+           Bind(&DoNothing));
+  Closure task_c0 = Bind(&PendingTaskTest::VerifyTraceAndPost,
+                         loop.task_runner(), location_c0, location_a2,
+                         ExpectedTrace({location_b0.program_counter(),
+                                        location_a1.program_counter(),
+                                        location_a0.program_counter()}),
+                         task_a2);
+
+  // On thread b run two tasks that conceptually come from the same location
+  // (managed via RunTwo().) One will post back to thread b and another will
+  // post to thread c to test spawning multiple tasks on different message
+  // loops. The task posted to thread c will not get location b1 whereas the
+  // one posted back to thread b will.
+  Closure task_b0_fork =
+      Bind(&PendingTaskTest::VerifyTraceAndPost,
+           thread_c.message_loop()->task_runner(), location_b0, location_c0,
+           ExpectedTrace({location_a1.program_counter(),
+                          location_a0.program_counter(), nullptr}),
+           task_c0);
+  Closure task_b0_local =
+      Bind(&PendingTaskTest::VerifyTraceAndPost,
+           thread_b.message_loop()->task_runner(), location_b0, location_b1,
+           ExpectedTrace({location_a1.program_counter(),
+                          location_a0.program_counter(), nullptr}),
+           Bind(&DoNothing));
+
+  // Push one frame onto the stack in thread a then pass to thread b.
+  Closure task_a1 =
+      Bind(&PendingTaskTest::VerifyTraceAndPost,
+           thread_b.message_loop()->task_runner(), location_a1, location_b0,
+           ExpectedTrace({location_a0.program_counter(), nullptr}),
+           Bind(&PendingTaskTest::RunTwo, task_b0_local, task_b0_fork));
+  Closure task_a0 =
+      Bind(&PendingTaskTest::VerifyTraceAndPost, loop.task_runner(),
+           location_a0, location_a1, ExpectedTrace({nullptr}), task_a1);
+
+  loop.task_runner()->PostTask(location_a0, task_a0);
+
+  RunLoop().RunUntilIdle();
+
+  thread_b.FlushForTesting();
+  thread_b.Stop();
+
+  thread_c.FlushForTesting();
+  thread_c.Stop();
+}
+
+}  // namespace base