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