1// Copyright (c) 2012 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 declarations for trace_event.h.
6#ifndef BASE_DEBUG_TRACE_EVENT_WIN_H_
7#define BASE_DEBUG_TRACE_EVENT_WIN_H_
8
9#include <string>
10
11#include "base/base_export.h"
12#include "base/debug/trace_event.h"
13#include "base/win/event_trace_provider.h"
14
15// Fwd.
16template <typename Type>
17struct StaticMemorySingletonTraits;
18
19namespace base {
20namespace debug {
21
22// This EtwTraceProvider subclass implements ETW logging
23// for the macros above on Windows.
24class BASE_EXPORT TraceEventETWProvider : public base::win::EtwTraceProvider {
25 public:
26   static const size_t kUseStrlen = static_cast<size_t>(-1);
27
28  // Start logging trace events.
29  // This is a noop in this implementation.
30  static bool StartTracing();
31
32  // Trace begin/end/instant events, this is the bottleneck implementation
33  // all the others defer to.
34  // Allowing the use of std::string for name or extra is a convenience,
35  // whereas passing name or extra as a const char* avoids the construction
36  // of temporary std::string instances.
37  // If kUseStrlen is passed for name_len or extra_len, the strlen of the string
38  // will be used for length.
39  static void Trace(const char* name,
40                    size_t name_len,
41                    char type,
42                    const void* id,
43                    const char* extra,
44                    size_t extra_len);
45
46  // Allows passing extra as a std::string for convenience.
47  static void Trace(const char* name,
48                    char type,
49                    const void* id,
50                    const std::string& extra) {
51    return Trace(name, kUseStrlen, type, id, extra.c_str(), extra.length());
52  }
53
54  // Allows passing extra as a const char* to avoid constructing temporary
55  // std::string instances where not needed.
56  static void Trace(const char* name,
57                    char type,
58                    const void* id,
59                    const char* extra) {
60    return Trace(name, kUseStrlen, type, id, extra, kUseStrlen);
61  }
62
63  // Retrieves the singleton.
64  // Note that this may return NULL post-AtExit processing.
65  static TraceEventETWProvider* GetInstance();
66
67  // Returns true iff tracing is turned on.
68  bool IsTracing() {
69    return enable_level() >= TRACE_LEVEL_INFORMATION;
70  }
71
72  // Emit a trace of type |type| containing |name|, |id|, and |extra|.
73  // Note: |name| and |extra| must be NULL, or a zero-terminated string of
74  //    length |name_len| or |extra_len| respectively.
75  // Note: if name_len or extra_len are kUseStrlen, the length of the
76  //    corresponding string will be used.
77  void TraceEvent(const char* name,
78                  size_t name_len,
79                  char type,
80                  const void* id,
81                  const char* extra,
82                  size_t extra_len);
83
84  // Exposed for unittesting only, allows resurrecting our
85  // singleton instance post-AtExit processing.
86  static void Resurrect();
87
88 private:
89  // Ensure only the provider can construct us.
90  friend struct StaticMemorySingletonTraits<TraceEventETWProvider>;
91  TraceEventETWProvider();
92
93  DISALLOW_COPY_AND_ASSIGN(TraceEventETWProvider);
94};
95
96// The ETW trace provider GUID.
97BASE_EXPORT extern const GUID kChromeTraceProviderName;
98
99// The ETW event class GUID for 32 bit events.
100BASE_EXPORT extern const GUID kTraceEventClass32;
101
102// The ETW event class GUID for 64 bit events.
103BASE_EXPORT extern const GUID kTraceEventClass64;
104
105// The ETW event types, IDs 0x00-0x09 are reserved, so start at 0x10.
106const base::win::EtwEventType kTraceEventTypeBegin = 0x10;
107const base::win::EtwEventType kTraceEventTypeEnd = 0x11;
108const base::win::EtwEventType kTraceEventTypeInstant = 0x12;
109
110// If this flag is set in enable flags
111enum TraceEventETWFlags {
112  CAPTURE_STACK_TRACE = 0x0001,
113};
114
115// The event format consists of:
116// The "name" string as a zero-terminated ASCII string.
117// The id pointer in the machine bitness.
118// The "extra" string as a zero-terminated ASCII string.
119// Optionally the stack trace, consisting of a DWORD "depth", followed
120//    by an array of void* (machine bitness) of length "depth".
121
122}  // namespace debug
123}  // namespace base
124
125#endif  // BASE_DEBUG_TRACE_EVENT_WIN_H_
126