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