xref: /aosp_15_r20/external/cronet/base/trace_event/trace_event_etw_export_win.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // This file contains the Windows-specific exporting to ETW.
6 #ifndef BASE_TRACE_EVENT_TRACE_EVENT_ETW_EXPORT_WIN_H_
7 #define BASE_TRACE_EVENT_TRACE_EVENT_ETW_EXPORT_WIN_H_
8 
9 #include <windows.h>
10 
11 #include <stdint.h>
12 
13 #include <map>
14 #include <memory>
15 #include <string_view>
16 
17 #include "base/base_export.h"
18 #include "base/trace_event/trace_event_impl.h"
19 #include "base/trace_event/trace_logging_minimal_win.h"
20 
21 namespace base {
22 
23 template <typename Type>
24 struct StaticMemorySingletonTraits;
25 
26 namespace trace_event {
27 
28 // This GUID is the used to identify the Chrome provider and is used whenever
29 // ETW is enabled via tracing tools and cannot change without updating tools
30 // that collect Chrome ETW data.
31 inline constexpr GUID Chrome_GUID = {
32     0xD2D578D9,
33     0x2936,
34     0x45B6,
35     {0xA0, 0x9F, 0x30, 0xE3, 0x27, 0x15, 0xF4, 0x2D}};
36 
37 class BASE_EXPORT TraceEventETWExport {
38  public:
39   TraceEventETWExport(const TraceEventETWExport&) = delete;
40   TraceEventETWExport& operator=(const TraceEventETWExport&) = delete;
41   ~TraceEventETWExport();
42 
43   // Retrieves the singleton.
44   // Note that this may return NULL post-AtExit processing.
45   static TraceEventETWExport* GetInstance();
46 
47   // Retrieves the singleton iff it was previously instantiated by a
48   // GetInstance() call. Avoids creating the instance only to check that it
49   // wasn't disabled. Note that, like GetInstance(), this may also return NULL
50   // post-AtExit processing.
51   static TraceEventETWExport* GetInstanceIfExists();
52 
53   // Enables exporting of events to ETW. If tracing is disabled for the Chrome
54   // provider, AddEvent and AddCustomEvent will simply return when called.
55   static void EnableETWExport();
56 
57   // Exports an event to ETW. This is mainly used in
58   // TraceLog::AddTraceEventWithThreadIdAndTimestamp to export internal events.
59   static void AddEvent(char phase,
60                        const unsigned char* category_group_enabled,
61                        const char* name,
62                        unsigned long long id,
63                        TimeTicks timestamp,
64                        const TraceArguments* args);
65 
66   // Exports an ETW event that marks the end of a complete event.
67   static void AddCompleteEndEvent(const unsigned char* category_group_enabled,
68                                   const char* name);
69 
70   // Returns true if any category in the group is enabled.
71   static bool IsCategoryGroupEnabled(std::string_view category_group_name);
72 
73  private:
74   // Ensure only the provider can construct us.
75   friend struct StaticMemorySingletonTraits<TraceEventETWExport>;
76   TraceEventETWExport();
77 
78   // Called from the ETW EnableCallback when the state of the provider or
79   // keywords has changed.
80   void OnETWEnableUpdate(TlmProvider::EventControlCode enabled);
81 
82   // Updates the list of enabled categories by consulting the ETW keyword.
83   // Returns true if there was a change, false otherwise.
84   bool UpdateEnabledCategories();
85 
86   // Returns true if the category is enabled.
87   bool IsCategoryEnabled(std::string_view category_name) const;
88 
89   uint64_t CategoryStateToETWKeyword(const uint8_t* category_state);
90 
91   bool is_registration_complete_ = false;
92 
93   // The keywords that were enabled last time the callback was made.
94   uint64_t etw_match_any_keyword_ = 0;
95 
96   // The provider is set based on channel for MSEdge, in other Chromium
97   // based browsers all channels use the same GUID/provider.
98   std::unique_ptr<TlmProvider> etw_provider_;
99 
100   // Maps category names to their status (enabled/disabled).
101   std::map<std::string_view, bool> categories_status_;
102 };
103 
104 BASE_EXPORT uint64_t
105 CategoryGroupToETWKeyword(std::string_view category_group_name);
106 
107 #if BUILDFLAG(USE_PERFETTO_CLIENT_LIBRARY)
108 
109 BASE_EXPORT perfetto::protos::gen::TrackEventConfig
110 ETWKeywordToTrackEventConfig(uint64_t keyword);
111 
112 #endif  // BUILDFLAG(USE_PERFETTO_CLIENT_LIBRARY)
113 
114 }  // namespace trace_event
115 }  // namespace base
116 
117 #endif  // BASE_TRACE_EVENT_TRACE_EVENT_ETW_EXPORT_WIN_H_
118