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