blob: d643e766a3c95a2c2e9447c8ea97cdc020eec372 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2019 The Chromium Authors
Alan Cutter9b0e1ab2019-03-21 04:22:162// 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 Sonzognie5fff99c2024-02-21 15:58:249#include <optional>
Daniel Cheng32ee1b22019-05-09 22:51:3010#include <string>
Alan Cutter9b0e1ab2019-03-21 04:22:1611
12#include "base/base_export.h"
13#include "base/containers/span.h"
14#include "base/debug/stack_trace.h"
Alan Cutter9b0e1ab2019-03-21 04:22:1615
16namespace base {
17namespace debug {
18
Alan Cutterbab00e22019-05-09 08:24:3119// 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 Cutter9b0e1ab2019-03-21 04:22:1623//
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.
36class 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 Cheng32ee1b22019-05-09 22:51:3049 // Resolves trace to symbols and returns as string.
50 std::string ToString() const;
51
Mark Brand82fbec842023-01-18 15:39:4652 // 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 Cutter9b0e1ab2019-03-21 04:22:1656
57 private:
Arthur Sonzognie5fff99c2024-02-21 15:58:2458 std::optional<StackTrace> stack_trace_;
Alan Cutter9b0e1ab2019-03-21 04:22:1659 bool trace_overflow_ = false;
60};
61
62// Forwards to TaskTrace::OutputToStream.
63BASE_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_