trace_event_etw_export_win.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // This file contains the Windows-specific exporting to ETW.
  5. #ifndef BASE_TRACE_EVENT_TRACE_EVENT_ETW_EXPORT_WIN_H_
  6. #define BASE_TRACE_EVENT_TRACE_EVENT_ETW_EXPORT_WIN_H_
  7. #include <stdint.h>
  8. #include <windows.h>
  9. #include <map>
  10. #include <memory>
  11. #include "base/base_export.h"
  12. #include "base/strings/string_piece.h"
  13. #include "base/trace_event/trace_event_impl.h"
  14. #include "base/trace_event/trace_logging_minimal_win.h"
  15. namespace base {
  16. template <typename Type>
  17. struct StaticMemorySingletonTraits;
  18. namespace trace_event {
  19. class BASE_EXPORT TraceEventETWExport {
  20. public:
  21. TraceEventETWExport(const TraceEventETWExport&) = delete;
  22. TraceEventETWExport& operator=(const TraceEventETWExport&) = delete;
  23. ~TraceEventETWExport();
  24. // Retrieves the singleton.
  25. // Note that this may return NULL post-AtExit processing.
  26. static TraceEventETWExport* GetInstance();
  27. // Retrieves the singleton iff it was previously instantiated by a
  28. // GetInstance() call. Avoids creating the instance only to check that it
  29. // wasn't disabled. Note that, like GetInstance(), this may also return NULL
  30. // post-AtExit processing.
  31. static TraceEventETWExport* GetInstanceIfExists();
  32. // Enables exporting of events to ETW. If tracing is disabled for the Chrome
  33. // provider, AddEvent and AddCustomEvent will simply return when called.
  34. static void EnableETWExport();
  35. // Exports an event to ETW. This is mainly used in
  36. // TraceLog::AddTraceEventWithThreadIdAndTimestamp to export internal events.
  37. static void AddEvent(char phase,
  38. const unsigned char* category_group_enabled,
  39. const char* name,
  40. unsigned long long id,
  41. const TraceArguments* args);
  42. // Exports an ETW event that marks the end of a complete event.
  43. static void AddCompleteEndEvent(const unsigned char* category_group_enabled,
  44. const char* name);
  45. // Returns true if any category in the group is enabled.
  46. static bool IsCategoryGroupEnabled(StringPiece category_group_name);
  47. // Called from the ETW EnableCallback when the state of the provider or
  48. // keywords has changed.
  49. static void OnETWEnableUpdate();
  50. private:
  51. // Ensure only the provider can construct us.
  52. friend struct StaticMemorySingletonTraits<TraceEventETWExport>;
  53. TraceEventETWExport();
  54. // Updates the list of enabled categories by consulting the ETW keyword.
  55. // Returns true if there was a change, false otherwise.
  56. bool UpdateEnabledCategories();
  57. static uint64_t CategoryGroupToKeyword(const uint8_t* category_state);
  58. // Returns true if the category is enabled.
  59. bool IsCategoryEnabled(StringPiece category_name) const;
  60. static bool is_registration_complete_;
  61. // The keywords that were enabled last time the callback was made.
  62. uint64_t etw_match_any_keyword_ = 0;
  63. // The provider is set based on channel for MSEdge, in other Chromium
  64. // based browsers all channels use the same GUID/provider.
  65. std::unique_ptr<TlmProvider> etw_provider_;
  66. // Maps category names to their status (enabled/disabled).
  67. std::map<StringPiece, bool> categories_status_;
  68. // Maps category names to their keyword.
  69. std::map<StringPiece, uint64_t> categories_keyword_;
  70. };
  71. } // namespace trace_event
  72. } // namespace base
  73. #endif // BASE_TRACE_EVENT_TRACE_EVENT_ETW_EXPORT_WIN_H_