Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Alan Cutter | 9b0e1ab | 2019-03-21 04:22:16 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_DEBUG_TASK_TRACE_H_ |
| 6 | #define BASE_DEBUG_TASK_TRACE_H_ |
| 7 | |
| 8 | #include <iosfwd> |
Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 9 | #include <optional> |
Daniel Cheng | 32ee1b2 | 2019-05-09 22:51:30 | [diff] [blame] | 10 | #include <string> |
Alan Cutter | 9b0e1ab | 2019-03-21 04:22:16 | [diff] [blame] | 11 | |
| 12 | #include "base/base_export.h" |
| 13 | #include "base/containers/span.h" |
| 14 | #include "base/debug/stack_trace.h" |
Alan Cutter | 9b0e1ab | 2019-03-21 04:22:16 | [diff] [blame] | 15 | |
| 16 | namespace base { |
| 17 | namespace debug { |
| 18 | |
Alan Cutter | bab00e2 | 2019-05-09 08:24:31 | [diff] [blame] | 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. |
Alan Cutter | 9b0e1ab | 2019-03-21 04:22:16 | [diff] [blame] | 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 | |
Daniel Cheng | 32ee1b2 | 2019-05-09 22:51:30 | [diff] [blame] | 49 | // Resolves trace to symbols and returns as string. |
| 50 | std::string ToString() const; |
| 51 | |
Mark Brand | 82fbec84 | 2023-01-18 15:39:46 | [diff] [blame] | 52 | // Reads the list of addresses currently in the task trace into `addresses`, |
| 53 | // and returns the maximum length of addresses that could have been read, |
| 54 | // which may differ from `addresses.size()`. |
| 55 | size_t GetAddresses(span<const void*> addresses) const; |
Alan Cutter | 9b0e1ab | 2019-03-21 04:22:16 | [diff] [blame] | 56 | |
| 57 | private: |
Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 58 | std::optional<StackTrace> stack_trace_; |
Alan Cutter | 9b0e1ab | 2019-03-21 04:22:16 | [diff] [blame] | 59 | bool trace_overflow_ = false; |
| 60 | }; |
| 61 | |
| 62 | // Forwards to TaskTrace::OutputToStream. |
| 63 | BASE_EXPORT std::ostream& operator<<(std::ostream& os, |
| 64 | const TaskTrace& task_trace); |
| 65 | |
| 66 | } // namespace debug |
| 67 | } // namespace base |
| 68 | |
| 69 | #endif // BASE_DEBUG_TASK_TRACE_H_ |