task_trace.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2019 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. #ifndef BASE_DEBUG_TASK_TRACE_H_
  5. #define BASE_DEBUG_TASK_TRACE_H_
  6. #include <iosfwd>
  7. #include <string>
  8. #include "base/base_export.h"
  9. #include "base/containers/span.h"
  10. #include "base/debug/stack_trace.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. namespace base {
  13. namespace debug {
  14. // Provides a snapshot of which places in the code called
  15. // base::TaskRunner::PostTask() that led to the TaskTrace() constructor call.
  16. // Analogous to base::StackTrace, but for posted tasks rather than function
  17. // calls.
  18. //
  19. // Example usage:
  20. // TaskTrace().Print();
  21. //
  22. // Example output:
  23. // Task trace:
  24. // #0 content::ServiceWorkerContextWrapper::DidCheckHasServiceWorker()
  25. // #1 content::ServiceWorkerStorage::FindForDocumentInDB()
  26. // #2 content::ServiceWorkerStorage::FindRegistrationForDocument()
  27. // #3 content::ServiceWorkerContextWrapper::CheckHasServiceWorker()
  28. // #4 content::ManifestIconDownloader::ScaleIcon()
  29. // Task trace buffer limit hit, update PendingTask::kTaskBacktraceLength to
  30. // increase.
  31. class BASE_EXPORT TaskTrace {
  32. public:
  33. TaskTrace();
  34. // Whether there is any trace data.
  35. bool empty() const;
  36. // Outputs to stderr via OutputToStream.
  37. void Print() const;
  38. // Outputs trace to |os|, may be called when empty() is true.
  39. void OutputToStream(std::ostream* os) const;
  40. // Resolves trace to symbols and returns as string.
  41. std::string ToString() const;
  42. // Returns the list of addresses in the task trace for testing.
  43. base::span<const void* const> AddressesForTesting() const;
  44. private:
  45. absl::optional<StackTrace> stack_trace_;
  46. bool trace_overflow_ = false;
  47. };
  48. // Forwards to TaskTrace::OutputToStream.
  49. BASE_EXPORT std::ostream& operator<<(std::ostream& os,
  50. const TaskTrace& task_trace);
  51. } // namespace debug
  52. } // namespace base
  53. #endif // BASE_DEBUG_TASK_TRACE_H_