xref: /aosp_15_r20/external/libchrome/base/trace_event/trace_log.h (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright 2015 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_TRACE_EVENT_TRACE_LOG_H_
6*635a8641SAndroid Build Coastguard Worker #define BASE_TRACE_EVENT_TRACE_LOG_H_
7*635a8641SAndroid Build Coastguard Worker 
8*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
9*635a8641SAndroid Build Coastguard Worker #include <stdint.h>
10*635a8641SAndroid Build Coastguard Worker 
11*635a8641SAndroid Build Coastguard Worker #include <memory>
12*635a8641SAndroid Build Coastguard Worker #include <string>
13*635a8641SAndroid Build Coastguard Worker #include <unordered_map>
14*635a8641SAndroid Build Coastguard Worker #include <vector>
15*635a8641SAndroid Build Coastguard Worker 
16*635a8641SAndroid Build Coastguard Worker #include "base/atomicops.h"
17*635a8641SAndroid Build Coastguard Worker #include "base/containers/stack.h"
18*635a8641SAndroid Build Coastguard Worker #include "base/gtest_prod_util.h"
19*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
20*635a8641SAndroid Build Coastguard Worker #include "base/time/time_override.h"
21*635a8641SAndroid Build Coastguard Worker #include "base/trace_event/memory_dump_provider.h"
22*635a8641SAndroid Build Coastguard Worker #include "base/trace_event/trace_config.h"
23*635a8641SAndroid Build Coastguard Worker #include "base/trace_event/trace_event_impl.h"
24*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h"
25*635a8641SAndroid Build Coastguard Worker 
26*635a8641SAndroid Build Coastguard Worker namespace base {
27*635a8641SAndroid Build Coastguard Worker 
28*635a8641SAndroid Build Coastguard Worker class MessageLoop;
29*635a8641SAndroid Build Coastguard Worker class RefCountedString;
30*635a8641SAndroid Build Coastguard Worker 
31*635a8641SAndroid Build Coastguard Worker template <typename T>
32*635a8641SAndroid Build Coastguard Worker class NoDestructor;
33*635a8641SAndroid Build Coastguard Worker 
34*635a8641SAndroid Build Coastguard Worker namespace trace_event {
35*635a8641SAndroid Build Coastguard Worker 
36*635a8641SAndroid Build Coastguard Worker struct TraceCategory;
37*635a8641SAndroid Build Coastguard Worker class TraceBuffer;
38*635a8641SAndroid Build Coastguard Worker class TraceBufferChunk;
39*635a8641SAndroid Build Coastguard Worker class TraceEvent;
40*635a8641SAndroid Build Coastguard Worker class TraceEventFilter;
41*635a8641SAndroid Build Coastguard Worker class TraceEventMemoryOverhead;
42*635a8641SAndroid Build Coastguard Worker 
43*635a8641SAndroid Build Coastguard Worker struct BASE_EXPORT TraceLogStatus {
44*635a8641SAndroid Build Coastguard Worker   TraceLogStatus();
45*635a8641SAndroid Build Coastguard Worker   ~TraceLogStatus();
46*635a8641SAndroid Build Coastguard Worker   uint32_t event_capacity;
47*635a8641SAndroid Build Coastguard Worker   uint32_t event_count;
48*635a8641SAndroid Build Coastguard Worker };
49*635a8641SAndroid Build Coastguard Worker 
50*635a8641SAndroid Build Coastguard Worker class BASE_EXPORT TraceLog : public MemoryDumpProvider {
51*635a8641SAndroid Build Coastguard Worker  public:
52*635a8641SAndroid Build Coastguard Worker   // Argument passed to TraceLog::SetEnabled.
53*635a8641SAndroid Build Coastguard Worker   enum Mode : uint8_t {
54*635a8641SAndroid Build Coastguard Worker     // Enables normal tracing (recording trace events in the trace buffer).
55*635a8641SAndroid Build Coastguard Worker     RECORDING_MODE = 1 << 0,
56*635a8641SAndroid Build Coastguard Worker 
57*635a8641SAndroid Build Coastguard Worker     // Trace events are enabled just for filtering but not for recording. Only
58*635a8641SAndroid Build Coastguard Worker     // event filters config of |trace_config| argument is used.
59*635a8641SAndroid Build Coastguard Worker     FILTERING_MODE = 1 << 1
60*635a8641SAndroid Build Coastguard Worker   };
61*635a8641SAndroid Build Coastguard Worker 
62*635a8641SAndroid Build Coastguard Worker   static TraceLog* GetInstance();
63*635a8641SAndroid Build Coastguard Worker 
64*635a8641SAndroid Build Coastguard Worker   // Get set of known category groups. This can change as new code paths are
65*635a8641SAndroid Build Coastguard Worker   // reached. The known category groups are inserted into |category_groups|.
66*635a8641SAndroid Build Coastguard Worker   void GetKnownCategoryGroups(std::vector<std::string>* category_groups);
67*635a8641SAndroid Build Coastguard Worker 
68*635a8641SAndroid Build Coastguard Worker   // Retrieves a copy (for thread-safety) of the current TraceConfig.
69*635a8641SAndroid Build Coastguard Worker   TraceConfig GetCurrentTraceConfig() const;
70*635a8641SAndroid Build Coastguard Worker 
71*635a8641SAndroid Build Coastguard Worker   // Initializes the thread-local event buffer, if not already initialized and
72*635a8641SAndroid Build Coastguard Worker   // if the current thread supports that (has a message loop).
73*635a8641SAndroid Build Coastguard Worker   void InitializeThreadLocalEventBufferIfSupported();
74*635a8641SAndroid Build Coastguard Worker 
75*635a8641SAndroid Build Coastguard Worker   // See TraceConfig comments for details on how to control which categories
76*635a8641SAndroid Build Coastguard Worker   // will be traced. SetDisabled must be called distinctly for each mode that is
77*635a8641SAndroid Build Coastguard Worker   // enabled. If tracing has already been enabled for recording, category filter
78*635a8641SAndroid Build Coastguard Worker   // (enabled and disabled categories) will be merged into the current category
79*635a8641SAndroid Build Coastguard Worker   // filter. Enabling RECORDING_MODE does not enable filters. Trace event
80*635a8641SAndroid Build Coastguard Worker   // filters will be used only if FILTERING_MODE is set on |modes_to_enable|.
81*635a8641SAndroid Build Coastguard Worker   // Conversely to RECORDING_MODE, FILTERING_MODE doesn't support upgrading,
82*635a8641SAndroid Build Coastguard Worker   // i.e. filters can only be enabled if not previously enabled.
83*635a8641SAndroid Build Coastguard Worker   void SetEnabled(const TraceConfig& trace_config, uint8_t modes_to_enable);
84*635a8641SAndroid Build Coastguard Worker 
85*635a8641SAndroid Build Coastguard Worker   // TODO(ssid): Remove the default SetEnabled and IsEnabled. They should take
86*635a8641SAndroid Build Coastguard Worker   // Mode as argument.
87*635a8641SAndroid Build Coastguard Worker 
88*635a8641SAndroid Build Coastguard Worker   // Disables tracing for all categories for the specified |modes_to_disable|
89*635a8641SAndroid Build Coastguard Worker   // only. Only RECORDING_MODE is taken as default |modes_to_disable|.
90*635a8641SAndroid Build Coastguard Worker   void SetDisabled();
91*635a8641SAndroid Build Coastguard Worker   void SetDisabled(uint8_t modes_to_disable);
92*635a8641SAndroid Build Coastguard Worker 
93*635a8641SAndroid Build Coastguard Worker   // Returns true if TraceLog is enabled on recording mode.
94*635a8641SAndroid Build Coastguard Worker   // Note: Returns false even if FILTERING_MODE is enabled.
IsEnabled()95*635a8641SAndroid Build Coastguard Worker   bool IsEnabled() { return enabled_modes_ & RECORDING_MODE; }
96*635a8641SAndroid Build Coastguard Worker 
97*635a8641SAndroid Build Coastguard Worker   // Returns a bitmap of enabled modes from TraceLog::Mode.
enabled_modes()98*635a8641SAndroid Build Coastguard Worker   uint8_t enabled_modes() { return enabled_modes_; }
99*635a8641SAndroid Build Coastguard Worker 
100*635a8641SAndroid Build Coastguard Worker   // The number of times we have begun recording traces. If tracing is off,
101*635a8641SAndroid Build Coastguard Worker   // returns -1. If tracing is on, then it returns the number of times we have
102*635a8641SAndroid Build Coastguard Worker   // recorded a trace. By watching for this number to increment, you can
103*635a8641SAndroid Build Coastguard Worker   // passively discover when a new trace has begun. This is then used to
104*635a8641SAndroid Build Coastguard Worker   // implement the TRACE_EVENT_IS_NEW_TRACE() primitive.
105*635a8641SAndroid Build Coastguard Worker   int GetNumTracesRecorded();
106*635a8641SAndroid Build Coastguard Worker 
107*635a8641SAndroid Build Coastguard Worker #if defined(OS_ANDROID)
108*635a8641SAndroid Build Coastguard Worker   void StartATrace();
109*635a8641SAndroid Build Coastguard Worker   void StopATrace();
110*635a8641SAndroid Build Coastguard Worker   void AddClockSyncMetadataEvent();
111*635a8641SAndroid Build Coastguard Worker #endif
112*635a8641SAndroid Build Coastguard Worker 
113*635a8641SAndroid Build Coastguard Worker   // Enabled state listeners give a callback when tracing is enabled or
114*635a8641SAndroid Build Coastguard Worker   // disabled. This can be used to tie into other library's tracing systems
115*635a8641SAndroid Build Coastguard Worker   // on-demand.
116*635a8641SAndroid Build Coastguard Worker   class BASE_EXPORT EnabledStateObserver {
117*635a8641SAndroid Build Coastguard Worker    public:
118*635a8641SAndroid Build Coastguard Worker     virtual ~EnabledStateObserver() = default;
119*635a8641SAndroid Build Coastguard Worker 
120*635a8641SAndroid Build Coastguard Worker     // Called just after the tracing system becomes enabled, outside of the
121*635a8641SAndroid Build Coastguard Worker     // |lock_|. TraceLog::IsEnabled() is true at this point.
122*635a8641SAndroid Build Coastguard Worker     virtual void OnTraceLogEnabled() = 0;
123*635a8641SAndroid Build Coastguard Worker 
124*635a8641SAndroid Build Coastguard Worker     // Called just after the tracing system disables, outside of the |lock_|.
125*635a8641SAndroid Build Coastguard Worker     // TraceLog::IsEnabled() is false at this point.
126*635a8641SAndroid Build Coastguard Worker     virtual void OnTraceLogDisabled() = 0;
127*635a8641SAndroid Build Coastguard Worker   };
128*635a8641SAndroid Build Coastguard Worker   void AddEnabledStateObserver(EnabledStateObserver* listener);
129*635a8641SAndroid Build Coastguard Worker   void RemoveEnabledStateObserver(EnabledStateObserver* listener);
130*635a8641SAndroid Build Coastguard Worker   bool HasEnabledStateObserver(EnabledStateObserver* listener) const;
131*635a8641SAndroid Build Coastguard Worker 
132*635a8641SAndroid Build Coastguard Worker   // Asynchronous enabled state listeners. When tracing is enabled or disabled,
133*635a8641SAndroid Build Coastguard Worker   // for each observer, a task for invoking its appropriate callback is posted
134*635a8641SAndroid Build Coastguard Worker   // to the thread from which AddAsyncEnabledStateObserver() was called. This
135*635a8641SAndroid Build Coastguard Worker   // allows the observer to be safely destroyed, provided that it happens on the
136*635a8641SAndroid Build Coastguard Worker   // same thread that invoked AddAsyncEnabledStateObserver().
137*635a8641SAndroid Build Coastguard Worker   class BASE_EXPORT AsyncEnabledStateObserver {
138*635a8641SAndroid Build Coastguard Worker    public:
139*635a8641SAndroid Build Coastguard Worker     virtual ~AsyncEnabledStateObserver() = default;
140*635a8641SAndroid Build Coastguard Worker 
141*635a8641SAndroid Build Coastguard Worker     // Posted just after the tracing system becomes enabled, outside |lock_|.
142*635a8641SAndroid Build Coastguard Worker     // TraceLog::IsEnabled() is true at this point.
143*635a8641SAndroid Build Coastguard Worker     virtual void OnTraceLogEnabled() = 0;
144*635a8641SAndroid Build Coastguard Worker 
145*635a8641SAndroid Build Coastguard Worker     // Posted just after the tracing system becomes disabled, outside |lock_|.
146*635a8641SAndroid Build Coastguard Worker     // TraceLog::IsEnabled() is false at this point.
147*635a8641SAndroid Build Coastguard Worker     virtual void OnTraceLogDisabled() = 0;
148*635a8641SAndroid Build Coastguard Worker   };
149*635a8641SAndroid Build Coastguard Worker   void AddAsyncEnabledStateObserver(
150*635a8641SAndroid Build Coastguard Worker       WeakPtr<AsyncEnabledStateObserver> listener);
151*635a8641SAndroid Build Coastguard Worker   void RemoveAsyncEnabledStateObserver(AsyncEnabledStateObserver* listener);
152*635a8641SAndroid Build Coastguard Worker   bool HasAsyncEnabledStateObserver(AsyncEnabledStateObserver* listener) const;
153*635a8641SAndroid Build Coastguard Worker 
154*635a8641SAndroid Build Coastguard Worker   TraceLogStatus GetStatus() const;
155*635a8641SAndroid Build Coastguard Worker   bool BufferIsFull() const;
156*635a8641SAndroid Build Coastguard Worker 
157*635a8641SAndroid Build Coastguard Worker   // Computes an estimate of the size of the TraceLog including all the retained
158*635a8641SAndroid Build Coastguard Worker   // objects.
159*635a8641SAndroid Build Coastguard Worker   void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead);
160*635a8641SAndroid Build Coastguard Worker 
161*635a8641SAndroid Build Coastguard Worker   void SetArgumentFilterPredicate(
162*635a8641SAndroid Build Coastguard Worker       const ArgumentFilterPredicate& argument_filter_predicate);
163*635a8641SAndroid Build Coastguard Worker 
164*635a8641SAndroid Build Coastguard Worker   // Flush all collected events to the given output callback. The callback will
165*635a8641SAndroid Build Coastguard Worker   // be called one or more times either synchronously or asynchronously from
166*635a8641SAndroid Build Coastguard Worker   // the current thread with IPC-bite-size chunks. The string format is
167*635a8641SAndroid Build Coastguard Worker   // undefined. Use TraceResultBuffer to convert one or more trace strings to
168*635a8641SAndroid Build Coastguard Worker   // JSON. The callback can be null if the caller doesn't want any data.
169*635a8641SAndroid Build Coastguard Worker   // Due to the implementation of thread-local buffers, flush can't be
170*635a8641SAndroid Build Coastguard Worker   // done when tracing is enabled. If called when tracing is enabled, the
171*635a8641SAndroid Build Coastguard Worker   // callback will be called directly with (empty_string, false) to indicate
172*635a8641SAndroid Build Coastguard Worker   // the end of this unsuccessful flush. Flush does the serialization
173*635a8641SAndroid Build Coastguard Worker   // on the same thread if the caller doesn't set use_worker_thread explicitly.
174*635a8641SAndroid Build Coastguard Worker   typedef base::Callback<void(const scoped_refptr<base::RefCountedString>&,
175*635a8641SAndroid Build Coastguard Worker                               bool has_more_events)> OutputCallback;
176*635a8641SAndroid Build Coastguard Worker   void Flush(const OutputCallback& cb, bool use_worker_thread = false);
177*635a8641SAndroid Build Coastguard Worker 
178*635a8641SAndroid Build Coastguard Worker   // Cancels tracing and discards collected data.
179*635a8641SAndroid Build Coastguard Worker   void CancelTracing(const OutputCallback& cb);
180*635a8641SAndroid Build Coastguard Worker 
181*635a8641SAndroid Build Coastguard Worker   typedef void (*AddTraceEventOverrideCallback)(const TraceEvent&);
182*635a8641SAndroid Build Coastguard Worker   // The callback will be called up until the point where the flush is
183*635a8641SAndroid Build Coastguard Worker   // finished, i.e. must be callable until OutputCallback is called with
184*635a8641SAndroid Build Coastguard Worker   // has_more_events==false.
185*635a8641SAndroid Build Coastguard Worker   void SetAddTraceEventOverride(const AddTraceEventOverrideCallback& override);
186*635a8641SAndroid Build Coastguard Worker 
187*635a8641SAndroid Build Coastguard Worker   // Called by TRACE_EVENT* macros, don't call this directly.
188*635a8641SAndroid Build Coastguard Worker   // The name parameter is a category group for example:
189*635a8641SAndroid Build Coastguard Worker   // TRACE_EVENT0("renderer,webkit", "WebViewImpl::HandleInputEvent")
190*635a8641SAndroid Build Coastguard Worker   static const unsigned char* GetCategoryGroupEnabled(const char* name);
191*635a8641SAndroid Build Coastguard Worker   static const char* GetCategoryGroupName(
192*635a8641SAndroid Build Coastguard Worker       const unsigned char* category_group_enabled);
193*635a8641SAndroid Build Coastguard Worker 
194*635a8641SAndroid Build Coastguard Worker   // Called by TRACE_EVENT* macros, don't call this directly.
195*635a8641SAndroid Build Coastguard Worker   // If |copy| is set, |name|, |arg_name1| and |arg_name2| will be deep copied
196*635a8641SAndroid Build Coastguard Worker   // into the event; see "Memory scoping note" and TRACE_EVENT_COPY_XXX above.
197*635a8641SAndroid Build Coastguard Worker   TraceEventHandle AddTraceEvent(
198*635a8641SAndroid Build Coastguard Worker       char phase,
199*635a8641SAndroid Build Coastguard Worker       const unsigned char* category_group_enabled,
200*635a8641SAndroid Build Coastguard Worker       const char* name,
201*635a8641SAndroid Build Coastguard Worker       const char* scope,
202*635a8641SAndroid Build Coastguard Worker       unsigned long long id,
203*635a8641SAndroid Build Coastguard Worker       int num_args,
204*635a8641SAndroid Build Coastguard Worker       const char* const* arg_names,
205*635a8641SAndroid Build Coastguard Worker       const unsigned char* arg_types,
206*635a8641SAndroid Build Coastguard Worker       const unsigned long long* arg_values,
207*635a8641SAndroid Build Coastguard Worker       std::unique_ptr<ConvertableToTraceFormat>* convertable_values,
208*635a8641SAndroid Build Coastguard Worker       unsigned int flags);
209*635a8641SAndroid Build Coastguard Worker   TraceEventHandle AddTraceEventWithBindId(
210*635a8641SAndroid Build Coastguard Worker       char phase,
211*635a8641SAndroid Build Coastguard Worker       const unsigned char* category_group_enabled,
212*635a8641SAndroid Build Coastguard Worker       const char* name,
213*635a8641SAndroid Build Coastguard Worker       const char* scope,
214*635a8641SAndroid Build Coastguard Worker       unsigned long long id,
215*635a8641SAndroid Build Coastguard Worker       unsigned long long bind_id,
216*635a8641SAndroid Build Coastguard Worker       int num_args,
217*635a8641SAndroid Build Coastguard Worker       const char* const* arg_names,
218*635a8641SAndroid Build Coastguard Worker       const unsigned char* arg_types,
219*635a8641SAndroid Build Coastguard Worker       const unsigned long long* arg_values,
220*635a8641SAndroid Build Coastguard Worker       std::unique_ptr<ConvertableToTraceFormat>* convertable_values,
221*635a8641SAndroid Build Coastguard Worker       unsigned int flags);
222*635a8641SAndroid Build Coastguard Worker   TraceEventHandle AddTraceEventWithProcessId(
223*635a8641SAndroid Build Coastguard Worker       char phase,
224*635a8641SAndroid Build Coastguard Worker       const unsigned char* category_group_enabled,
225*635a8641SAndroid Build Coastguard Worker       const char* name,
226*635a8641SAndroid Build Coastguard Worker       const char* scope,
227*635a8641SAndroid Build Coastguard Worker       unsigned long long id,
228*635a8641SAndroid Build Coastguard Worker       int process_id,
229*635a8641SAndroid Build Coastguard Worker       int num_args,
230*635a8641SAndroid Build Coastguard Worker       const char* const* arg_names,
231*635a8641SAndroid Build Coastguard Worker       const unsigned char* arg_types,
232*635a8641SAndroid Build Coastguard Worker       const unsigned long long* arg_values,
233*635a8641SAndroid Build Coastguard Worker       std::unique_ptr<ConvertableToTraceFormat>* convertable_values,
234*635a8641SAndroid Build Coastguard Worker       unsigned int flags);
235*635a8641SAndroid Build Coastguard Worker   TraceEventHandle AddTraceEventWithThreadIdAndTimestamp(
236*635a8641SAndroid Build Coastguard Worker       char phase,
237*635a8641SAndroid Build Coastguard Worker       const unsigned char* category_group_enabled,
238*635a8641SAndroid Build Coastguard Worker       const char* name,
239*635a8641SAndroid Build Coastguard Worker       const char* scope,
240*635a8641SAndroid Build Coastguard Worker       unsigned long long id,
241*635a8641SAndroid Build Coastguard Worker       int thread_id,
242*635a8641SAndroid Build Coastguard Worker       const TimeTicks& timestamp,
243*635a8641SAndroid Build Coastguard Worker       int num_args,
244*635a8641SAndroid Build Coastguard Worker       const char* const* arg_names,
245*635a8641SAndroid Build Coastguard Worker       const unsigned char* arg_types,
246*635a8641SAndroid Build Coastguard Worker       const unsigned long long* arg_values,
247*635a8641SAndroid Build Coastguard Worker       std::unique_ptr<ConvertableToTraceFormat>* convertable_values,
248*635a8641SAndroid Build Coastguard Worker       unsigned int flags);
249*635a8641SAndroid Build Coastguard Worker   TraceEventHandle AddTraceEventWithThreadIdAndTimestamp(
250*635a8641SAndroid Build Coastguard Worker       char phase,
251*635a8641SAndroid Build Coastguard Worker       const unsigned char* category_group_enabled,
252*635a8641SAndroid Build Coastguard Worker       const char* name,
253*635a8641SAndroid Build Coastguard Worker       const char* scope,
254*635a8641SAndroid Build Coastguard Worker       unsigned long long id,
255*635a8641SAndroid Build Coastguard Worker       unsigned long long bind_id,
256*635a8641SAndroid Build Coastguard Worker       int thread_id,
257*635a8641SAndroid Build Coastguard Worker       const TimeTicks& timestamp,
258*635a8641SAndroid Build Coastguard Worker       int num_args,
259*635a8641SAndroid Build Coastguard Worker       const char* const* arg_names,
260*635a8641SAndroid Build Coastguard Worker       const unsigned char* arg_types,
261*635a8641SAndroid Build Coastguard Worker       const unsigned long long* arg_values,
262*635a8641SAndroid Build Coastguard Worker       std::unique_ptr<ConvertableToTraceFormat>* convertable_values,
263*635a8641SAndroid Build Coastguard Worker       unsigned int flags);
264*635a8641SAndroid Build Coastguard Worker 
265*635a8641SAndroid Build Coastguard Worker   // Adds a metadata event that will be written when the trace log is flushed.
266*635a8641SAndroid Build Coastguard Worker   void AddMetadataEvent(
267*635a8641SAndroid Build Coastguard Worker       const unsigned char* category_group_enabled,
268*635a8641SAndroid Build Coastguard Worker       const char* name,
269*635a8641SAndroid Build Coastguard Worker       int num_args,
270*635a8641SAndroid Build Coastguard Worker       const char* const* arg_names,
271*635a8641SAndroid Build Coastguard Worker       const unsigned char* arg_types,
272*635a8641SAndroid Build Coastguard Worker       const unsigned long long* arg_values,
273*635a8641SAndroid Build Coastguard Worker       std::unique_ptr<ConvertableToTraceFormat>* convertable_values,
274*635a8641SAndroid Build Coastguard Worker       unsigned int flags);
275*635a8641SAndroid Build Coastguard Worker 
276*635a8641SAndroid Build Coastguard Worker   void UpdateTraceEventDuration(const unsigned char* category_group_enabled,
277*635a8641SAndroid Build Coastguard Worker                                 const char* name,
278*635a8641SAndroid Build Coastguard Worker                                 TraceEventHandle handle);
279*635a8641SAndroid Build Coastguard Worker 
280*635a8641SAndroid Build Coastguard Worker   void UpdateTraceEventDurationExplicit(
281*635a8641SAndroid Build Coastguard Worker       const unsigned char* category_group_enabled,
282*635a8641SAndroid Build Coastguard Worker       const char* name,
283*635a8641SAndroid Build Coastguard Worker       TraceEventHandle handle,
284*635a8641SAndroid Build Coastguard Worker       const TimeTicks& now,
285*635a8641SAndroid Build Coastguard Worker       const ThreadTicks& thread_now);
286*635a8641SAndroid Build Coastguard Worker 
287*635a8641SAndroid Build Coastguard Worker   void EndFilteredEvent(const unsigned char* category_group_enabled,
288*635a8641SAndroid Build Coastguard Worker                         const char* name,
289*635a8641SAndroid Build Coastguard Worker                         TraceEventHandle handle);
290*635a8641SAndroid Build Coastguard Worker 
process_id()291*635a8641SAndroid Build Coastguard Worker   int process_id() const { return process_id_; }
292*635a8641SAndroid Build Coastguard Worker 
293*635a8641SAndroid Build Coastguard Worker   uint64_t MangleEventId(uint64_t id);
294*635a8641SAndroid Build Coastguard Worker 
295*635a8641SAndroid Build Coastguard Worker   // Exposed for unittesting:
296*635a8641SAndroid Build Coastguard Worker 
297*635a8641SAndroid Build Coastguard Worker   // Testing factory for TraceEventFilter.
298*635a8641SAndroid Build Coastguard Worker   typedef std::unique_ptr<TraceEventFilter> (*FilterFactoryForTesting)(
299*635a8641SAndroid Build Coastguard Worker       const std::string& /* predicate_name */);
SetFilterFactoryForTesting(FilterFactoryForTesting factory)300*635a8641SAndroid Build Coastguard Worker   void SetFilterFactoryForTesting(FilterFactoryForTesting factory) {
301*635a8641SAndroid Build Coastguard Worker     filter_factory_for_testing_ = factory;
302*635a8641SAndroid Build Coastguard Worker   }
303*635a8641SAndroid Build Coastguard Worker 
304*635a8641SAndroid Build Coastguard Worker   // Allows clearing up our singleton instance.
305*635a8641SAndroid Build Coastguard Worker   static void ResetForTesting();
306*635a8641SAndroid Build Coastguard Worker 
307*635a8641SAndroid Build Coastguard Worker   // Allow tests to inspect TraceEvents.
308*635a8641SAndroid Build Coastguard Worker   TraceEvent* GetEventByHandle(TraceEventHandle handle);
309*635a8641SAndroid Build Coastguard Worker 
310*635a8641SAndroid Build Coastguard Worker   void SetProcessID(int process_id);
311*635a8641SAndroid Build Coastguard Worker 
312*635a8641SAndroid Build Coastguard Worker   // Process sort indices, if set, override the order of a process will appear
313*635a8641SAndroid Build Coastguard Worker   // relative to other processes in the trace viewer. Processes are sorted first
314*635a8641SAndroid Build Coastguard Worker   // on their sort index, ascending, then by their name, and then tid.
315*635a8641SAndroid Build Coastguard Worker   void SetProcessSortIndex(int sort_index);
316*635a8641SAndroid Build Coastguard Worker 
317*635a8641SAndroid Build Coastguard Worker   // Sets the name of the process.
set_process_name(const std::string & process_name)318*635a8641SAndroid Build Coastguard Worker   void set_process_name(const std::string& process_name) {
319*635a8641SAndroid Build Coastguard Worker     AutoLock lock(lock_);
320*635a8641SAndroid Build Coastguard Worker     process_name_ = process_name;
321*635a8641SAndroid Build Coastguard Worker   }
322*635a8641SAndroid Build Coastguard Worker 
IsProcessNameEmpty()323*635a8641SAndroid Build Coastguard Worker   bool IsProcessNameEmpty() const { return process_name_.empty(); }
324*635a8641SAndroid Build Coastguard Worker 
325*635a8641SAndroid Build Coastguard Worker   // Processes can have labels in addition to their names. Use labels, for
326*635a8641SAndroid Build Coastguard Worker   // instance, to list out the web page titles that a process is handling.
327*635a8641SAndroid Build Coastguard Worker   void UpdateProcessLabel(int label_id, const std::string& current_label);
328*635a8641SAndroid Build Coastguard Worker   void RemoveProcessLabel(int label_id);
329*635a8641SAndroid Build Coastguard Worker 
330*635a8641SAndroid Build Coastguard Worker   // Thread sort indices, if set, override the order of a thread will appear
331*635a8641SAndroid Build Coastguard Worker   // within its process in the trace viewer. Threads are sorted first on their
332*635a8641SAndroid Build Coastguard Worker   // sort index, ascending, then by their name, and then tid.
333*635a8641SAndroid Build Coastguard Worker   void SetThreadSortIndex(PlatformThreadId thread_id, int sort_index);
334*635a8641SAndroid Build Coastguard Worker 
335*635a8641SAndroid Build Coastguard Worker   // Allow setting an offset between the current TimeTicks time and the time
336*635a8641SAndroid Build Coastguard Worker   // that should be reported.
337*635a8641SAndroid Build Coastguard Worker   void SetTimeOffset(TimeDelta offset);
338*635a8641SAndroid Build Coastguard Worker 
339*635a8641SAndroid Build Coastguard Worker   size_t GetObserverCountForTest() const;
340*635a8641SAndroid Build Coastguard Worker 
341*635a8641SAndroid Build Coastguard Worker   // Call this method if the current thread may block the message loop to
342*635a8641SAndroid Build Coastguard Worker   // prevent the thread from using the thread-local buffer because the thread
343*635a8641SAndroid Build Coastguard Worker   // may not handle the flush request in time causing lost of unflushed events.
344*635a8641SAndroid Build Coastguard Worker   void SetCurrentThreadBlocksMessageLoop();
345*635a8641SAndroid Build Coastguard Worker 
346*635a8641SAndroid Build Coastguard Worker #if defined(OS_WIN)
347*635a8641SAndroid Build Coastguard Worker   // This function is called by the ETW exporting module whenever the ETW
348*635a8641SAndroid Build Coastguard Worker   // keyword (flags) changes. This keyword indicates which categories should be
349*635a8641SAndroid Build Coastguard Worker   // exported, so whenever it changes, we adjust accordingly.
350*635a8641SAndroid Build Coastguard Worker   void UpdateETWCategoryGroupEnabledFlags();
351*635a8641SAndroid Build Coastguard Worker #endif
352*635a8641SAndroid Build Coastguard Worker 
353*635a8641SAndroid Build Coastguard Worker   // Replaces |logged_events_| with a new TraceBuffer for testing.
354*635a8641SAndroid Build Coastguard Worker   void SetTraceBufferForTesting(std::unique_ptr<TraceBuffer> trace_buffer);
355*635a8641SAndroid Build Coastguard Worker 
356*635a8641SAndroid Build Coastguard Worker  private:
357*635a8641SAndroid Build Coastguard Worker   typedef unsigned int InternalTraceOptions;
358*635a8641SAndroid Build Coastguard Worker 
359*635a8641SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture,
360*635a8641SAndroid Build Coastguard Worker                            TraceBufferRingBufferGetReturnChunk);
361*635a8641SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture,
362*635a8641SAndroid Build Coastguard Worker                            TraceBufferRingBufferHalfIteration);
363*635a8641SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture,
364*635a8641SAndroid Build Coastguard Worker                            TraceBufferRingBufferFullIteration);
365*635a8641SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture, TraceBufferVectorReportFull);
366*635a8641SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture,
367*635a8641SAndroid Build Coastguard Worker                            ConvertTraceConfigToInternalOptions);
368*635a8641SAndroid Build Coastguard Worker   FRIEND_TEST_ALL_PREFIXES(TraceEventTestFixture,
369*635a8641SAndroid Build Coastguard Worker                            TraceRecordAsMuchAsPossibleMode);
370*635a8641SAndroid Build Coastguard Worker 
371*635a8641SAndroid Build Coastguard Worker   friend class base::NoDestructor<TraceLog>;
372*635a8641SAndroid Build Coastguard Worker 
373*635a8641SAndroid Build Coastguard Worker   // MemoryDumpProvider implementation.
374*635a8641SAndroid Build Coastguard Worker   bool OnMemoryDump(const MemoryDumpArgs& args,
375*635a8641SAndroid Build Coastguard Worker                     ProcessMemoryDump* pmd) override;
376*635a8641SAndroid Build Coastguard Worker 
377*635a8641SAndroid Build Coastguard Worker   // Enable/disable each category group based on the current mode_,
378*635a8641SAndroid Build Coastguard Worker   // category_filter_ and event_filters_enabled_.
379*635a8641SAndroid Build Coastguard Worker   // Enable the category group in the recording mode if category_filter_ matches
380*635a8641SAndroid Build Coastguard Worker   // the category group, is not null. Enable category for filtering if any
381*635a8641SAndroid Build Coastguard Worker   // filter in event_filters_enabled_ enables it.
382*635a8641SAndroid Build Coastguard Worker   void UpdateCategoryRegistry();
383*635a8641SAndroid Build Coastguard Worker   void UpdateCategoryState(TraceCategory* category);
384*635a8641SAndroid Build Coastguard Worker 
385*635a8641SAndroid Build Coastguard Worker   void CreateFiltersForTraceConfig();
386*635a8641SAndroid Build Coastguard Worker 
387*635a8641SAndroid Build Coastguard Worker   InternalTraceOptions GetInternalOptionsFromTraceConfig(
388*635a8641SAndroid Build Coastguard Worker       const TraceConfig& config);
389*635a8641SAndroid Build Coastguard Worker 
390*635a8641SAndroid Build Coastguard Worker   class ThreadLocalEventBuffer;
391*635a8641SAndroid Build Coastguard Worker   class OptionalAutoLock;
392*635a8641SAndroid Build Coastguard Worker   struct RegisteredAsyncObserver;
393*635a8641SAndroid Build Coastguard Worker 
394*635a8641SAndroid Build Coastguard Worker   TraceLog();
395*635a8641SAndroid Build Coastguard Worker   ~TraceLog() override;
396*635a8641SAndroid Build Coastguard Worker   void AddMetadataEventsWhileLocked();
397*635a8641SAndroid Build Coastguard Worker 
trace_options()398*635a8641SAndroid Build Coastguard Worker   InternalTraceOptions trace_options() const {
399*635a8641SAndroid Build Coastguard Worker     return static_cast<InternalTraceOptions>(
400*635a8641SAndroid Build Coastguard Worker         subtle::NoBarrier_Load(&trace_options_));
401*635a8641SAndroid Build Coastguard Worker   }
402*635a8641SAndroid Build Coastguard Worker 
trace_buffer()403*635a8641SAndroid Build Coastguard Worker   TraceBuffer* trace_buffer() const { return logged_events_.get(); }
404*635a8641SAndroid Build Coastguard Worker   TraceBuffer* CreateTraceBuffer();
405*635a8641SAndroid Build Coastguard Worker 
406*635a8641SAndroid Build Coastguard Worker   std::string EventToConsoleMessage(unsigned char phase,
407*635a8641SAndroid Build Coastguard Worker                                     const TimeTicks& timestamp,
408*635a8641SAndroid Build Coastguard Worker                                     TraceEvent* trace_event);
409*635a8641SAndroid Build Coastguard Worker 
410*635a8641SAndroid Build Coastguard Worker   TraceEvent* AddEventToThreadSharedChunkWhileLocked(TraceEventHandle* handle,
411*635a8641SAndroid Build Coastguard Worker                                                      bool check_buffer_is_full);
412*635a8641SAndroid Build Coastguard Worker   void CheckIfBufferIsFullWhileLocked();
413*635a8641SAndroid Build Coastguard Worker   void SetDisabledWhileLocked(uint8_t modes);
414*635a8641SAndroid Build Coastguard Worker 
415*635a8641SAndroid Build Coastguard Worker   TraceEvent* GetEventByHandleInternal(TraceEventHandle handle,
416*635a8641SAndroid Build Coastguard Worker                                        OptionalAutoLock* lock);
417*635a8641SAndroid Build Coastguard Worker 
418*635a8641SAndroid Build Coastguard Worker   void FlushInternal(const OutputCallback& cb,
419*635a8641SAndroid Build Coastguard Worker                      bool use_worker_thread,
420*635a8641SAndroid Build Coastguard Worker                      bool discard_events);
421*635a8641SAndroid Build Coastguard Worker 
422*635a8641SAndroid Build Coastguard Worker   // |generation| is used in the following callbacks to check if the callback
423*635a8641SAndroid Build Coastguard Worker   // is called for the flush of the current |logged_events_|.
424*635a8641SAndroid Build Coastguard Worker   void FlushCurrentThread(int generation, bool discard_events);
425*635a8641SAndroid Build Coastguard Worker   // Usually it runs on a different thread.
426*635a8641SAndroid Build Coastguard Worker   static void ConvertTraceEventsToTraceFormat(
427*635a8641SAndroid Build Coastguard Worker       std::unique_ptr<TraceBuffer> logged_events,
428*635a8641SAndroid Build Coastguard Worker       const TraceLog::OutputCallback& flush_output_callback,
429*635a8641SAndroid Build Coastguard Worker       const ArgumentFilterPredicate& argument_filter_predicate);
430*635a8641SAndroid Build Coastguard Worker   void FinishFlush(int generation, bool discard_events);
431*635a8641SAndroid Build Coastguard Worker   void OnFlushTimeout(int generation, bool discard_events);
432*635a8641SAndroid Build Coastguard Worker 
generation()433*635a8641SAndroid Build Coastguard Worker   int generation() const {
434*635a8641SAndroid Build Coastguard Worker     return static_cast<int>(subtle::NoBarrier_Load(&generation_));
435*635a8641SAndroid Build Coastguard Worker   }
CheckGeneration(int generation)436*635a8641SAndroid Build Coastguard Worker   bool CheckGeneration(int generation) const {
437*635a8641SAndroid Build Coastguard Worker     return generation == this->generation();
438*635a8641SAndroid Build Coastguard Worker   }
439*635a8641SAndroid Build Coastguard Worker   void UseNextTraceBuffer();
440*635a8641SAndroid Build Coastguard Worker 
OffsetNow()441*635a8641SAndroid Build Coastguard Worker   TimeTicks OffsetNow() const {
442*635a8641SAndroid Build Coastguard Worker     // This should be TRACE_TIME_TICKS_NOW but include order makes that hard.
443*635a8641SAndroid Build Coastguard Worker     return OffsetTimestamp(base::subtle::TimeTicksNowIgnoringOverride());
444*635a8641SAndroid Build Coastguard Worker   }
OffsetTimestamp(const TimeTicks & timestamp)445*635a8641SAndroid Build Coastguard Worker   TimeTicks OffsetTimestamp(const TimeTicks& timestamp) const {
446*635a8641SAndroid Build Coastguard Worker     return timestamp - time_offset_;
447*635a8641SAndroid Build Coastguard Worker   }
448*635a8641SAndroid Build Coastguard Worker 
449*635a8641SAndroid Build Coastguard Worker   // Internal representation of trace options since we store the currently used
450*635a8641SAndroid Build Coastguard Worker   // trace option as an AtomicWord.
451*635a8641SAndroid Build Coastguard Worker   static const InternalTraceOptions kInternalNone;
452*635a8641SAndroid Build Coastguard Worker   static const InternalTraceOptions kInternalRecordUntilFull;
453*635a8641SAndroid Build Coastguard Worker   static const InternalTraceOptions kInternalRecordContinuously;
454*635a8641SAndroid Build Coastguard Worker   static const InternalTraceOptions kInternalEchoToConsole;
455*635a8641SAndroid Build Coastguard Worker   static const InternalTraceOptions kInternalRecordAsMuchAsPossible;
456*635a8641SAndroid Build Coastguard Worker   static const InternalTraceOptions kInternalEnableArgumentFilter;
457*635a8641SAndroid Build Coastguard Worker 
458*635a8641SAndroid Build Coastguard Worker   // This lock protects TraceLog member accesses (except for members protected
459*635a8641SAndroid Build Coastguard Worker   // by thread_info_lock_) from arbitrary threads.
460*635a8641SAndroid Build Coastguard Worker   mutable Lock lock_;
461*635a8641SAndroid Build Coastguard Worker   // This lock protects accesses to thread_names_, thread_event_start_times_
462*635a8641SAndroid Build Coastguard Worker   // and thread_colors_.
463*635a8641SAndroid Build Coastguard Worker   Lock thread_info_lock_;
464*635a8641SAndroid Build Coastguard Worker   uint8_t enabled_modes_;  // See TraceLog::Mode.
465*635a8641SAndroid Build Coastguard Worker   int num_traces_recorded_;
466*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<TraceBuffer> logged_events_;
467*635a8641SAndroid Build Coastguard Worker   std::vector<std::unique_ptr<TraceEvent>> metadata_events_;
468*635a8641SAndroid Build Coastguard Worker   bool dispatching_to_observer_list_;
469*635a8641SAndroid Build Coastguard Worker   std::vector<EnabledStateObserver*> enabled_state_observer_list_;
470*635a8641SAndroid Build Coastguard Worker   std::map<AsyncEnabledStateObserver*, RegisteredAsyncObserver>
471*635a8641SAndroid Build Coastguard Worker       async_observers_;
472*635a8641SAndroid Build Coastguard Worker 
473*635a8641SAndroid Build Coastguard Worker   std::string process_name_;
474*635a8641SAndroid Build Coastguard Worker   std::unordered_map<int, std::string> process_labels_;
475*635a8641SAndroid Build Coastguard Worker   int process_sort_index_;
476*635a8641SAndroid Build Coastguard Worker   std::unordered_map<int, int> thread_sort_indices_;
477*635a8641SAndroid Build Coastguard Worker   std::unordered_map<int, std::string> thread_names_;
478*635a8641SAndroid Build Coastguard Worker   base::Time process_creation_time_;
479*635a8641SAndroid Build Coastguard Worker 
480*635a8641SAndroid Build Coastguard Worker   // The following two maps are used only when ECHO_TO_CONSOLE.
481*635a8641SAndroid Build Coastguard Worker   std::unordered_map<int, base::stack<TimeTicks>> thread_event_start_times_;
482*635a8641SAndroid Build Coastguard Worker   std::unordered_map<std::string, int> thread_colors_;
483*635a8641SAndroid Build Coastguard Worker 
484*635a8641SAndroid Build Coastguard Worker   TimeTicks buffer_limit_reached_timestamp_;
485*635a8641SAndroid Build Coastguard Worker 
486*635a8641SAndroid Build Coastguard Worker   // XORed with TraceID to make it unlikely to collide with other processes.
487*635a8641SAndroid Build Coastguard Worker   unsigned long long process_id_hash_;
488*635a8641SAndroid Build Coastguard Worker 
489*635a8641SAndroid Build Coastguard Worker   int process_id_;
490*635a8641SAndroid Build Coastguard Worker 
491*635a8641SAndroid Build Coastguard Worker   TimeDelta time_offset_;
492*635a8641SAndroid Build Coastguard Worker 
493*635a8641SAndroid Build Coastguard Worker   subtle::AtomicWord /* Options */ trace_options_;
494*635a8641SAndroid Build Coastguard Worker 
495*635a8641SAndroid Build Coastguard Worker   TraceConfig trace_config_;
496*635a8641SAndroid Build Coastguard Worker   TraceConfig::EventFilters enabled_event_filters_;
497*635a8641SAndroid Build Coastguard Worker 
498*635a8641SAndroid Build Coastguard Worker   ThreadLocalPointer<ThreadLocalEventBuffer> thread_local_event_buffer_;
499*635a8641SAndroid Build Coastguard Worker   ThreadLocalBoolean thread_blocks_message_loop_;
500*635a8641SAndroid Build Coastguard Worker   ThreadLocalBoolean thread_is_in_trace_event_;
501*635a8641SAndroid Build Coastguard Worker 
502*635a8641SAndroid Build Coastguard Worker   // Contains the message loops of threads that have had at least one event
503*635a8641SAndroid Build Coastguard Worker   // added into the local event buffer. Not using SingleThreadTaskRunner
504*635a8641SAndroid Build Coastguard Worker   // because we need to know the life time of the message loops.
505*635a8641SAndroid Build Coastguard Worker   hash_set<MessageLoop*> thread_message_loops_;
506*635a8641SAndroid Build Coastguard Worker 
507*635a8641SAndroid Build Coastguard Worker   // For events which can't be added into the thread local buffer, e.g. events
508*635a8641SAndroid Build Coastguard Worker   // from threads without a message loop.
509*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<TraceBufferChunk> thread_shared_chunk_;
510*635a8641SAndroid Build Coastguard Worker   size_t thread_shared_chunk_index_;
511*635a8641SAndroid Build Coastguard Worker 
512*635a8641SAndroid Build Coastguard Worker   // Set when asynchronous Flush is in progress.
513*635a8641SAndroid Build Coastguard Worker   OutputCallback flush_output_callback_;
514*635a8641SAndroid Build Coastguard Worker   scoped_refptr<SequencedTaskRunner> flush_task_runner_;
515*635a8641SAndroid Build Coastguard Worker   ArgumentFilterPredicate argument_filter_predicate_;
516*635a8641SAndroid Build Coastguard Worker   subtle::AtomicWord generation_;
517*635a8641SAndroid Build Coastguard Worker   bool use_worker_thread_;
518*635a8641SAndroid Build Coastguard Worker   subtle::AtomicWord trace_event_override_;
519*635a8641SAndroid Build Coastguard Worker 
520*635a8641SAndroid Build Coastguard Worker   FilterFactoryForTesting filter_factory_for_testing_;
521*635a8641SAndroid Build Coastguard Worker 
522*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(TraceLog);
523*635a8641SAndroid Build Coastguard Worker };
524*635a8641SAndroid Build Coastguard Worker 
525*635a8641SAndroid Build Coastguard Worker }  // namespace trace_event
526*635a8641SAndroid Build Coastguard Worker }  // namespace base
527*635a8641SAndroid Build Coastguard Worker 
528*635a8641SAndroid Build Coastguard Worker #endif  // BASE_TRACE_EVENT_TRACE_LOG_H_
529