1 // Copyright 2015 The Chromium Authors. All rights reserved.
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 
11 #include <map>
12 
13 #include "base/base_export.h"
14 #include "base/macros.h"
15 #include "base/strings/string_piece.h"
16 #include "base/trace_event/trace_event_impl.h"
17 
18 namespace base {
19 
20 template <typename Type>
21 struct StaticMemorySingletonTraits;
22 
23 namespace trace_event {
24 
25 class BASE_EXPORT TraceEventETWExport {
26  public:
27   ~TraceEventETWExport();
28 
29   // Retrieves the singleton.
30   // Note that this may return NULL post-AtExit processing.
31   static TraceEventETWExport* GetInstance();
32 
33   // Retrieves the singleton iff it was previously instantiated by a
34   // GetInstance() call. Avoids creating the instance only to check that it
35   // wasn't disabled. Note that, like GetInstance(), this may also return NULL
36   // post-AtExit processing.
37   static TraceEventETWExport* GetInstanceIfExists();
38 
39   // Enables exporting of events to ETW. If tracing is disabled for the Chrome
40   // provider, AddEvent and AddCustomEvent will simply return when called.
41   static void EnableETWExport();
42 
43   // Exports an event to ETW. This is mainly used in
44   // TraceLog::AddTraceEventWithThreadIdAndTimestamp to export internal events.
45   static void AddEvent(char phase,
46                        const unsigned char* category_group_enabled,
47                        const char* name,
48                        unsigned long long id,
49                        const TraceArguments* args);
50 
51   // Exports an ETW event that marks the end of a complete event.
52   static void AddCompleteEndEvent(const char* name);
53 
54   // Returns true if any category in the group is enabled.
55   static bool IsCategoryGroupEnabled(StringPiece category_group_name);
56 
57   // Called from the ETW EnableCallback when the state of the provider or
58   // keywords has changed.
59   static void OnETWEnableUpdate();
60 
61  private:
62   // Ensure only the provider can construct us.
63   friend struct StaticMemorySingletonTraits<TraceEventETWExport>;
64 
65   TraceEventETWExport();
66 
67   // Updates the list of enabled categories by consulting the ETW keyword.
68   // Returns true if there was a change, false otherwise.
69   bool UpdateEnabledCategories();
70 
71   // Returns true if the category is enabled.
72   bool IsCategoryEnabled(StringPiece category_name) const;
73 
74   static bool is_registration_complete_;
75 
76   // Maps category names to their status (enabled/disabled).
77   std::map<StringPiece, bool> categories_status_;
78 
79   // Local copy of the ETW keyword.
80   uint64_t etw_match_any_keyword_;
81 
82   DISALLOW_COPY_AND_ASSIGN(TraceEventETWExport);
83 };
84 
85 }  // namespace trace_event
86 }  // namespace base
87 
88 #endif  // BASE_TRACE_EVENT_TRACE_EVENT_ETW_EXPORT_WIN_H_
89