summaryrefslogtreecommitdiffstats
path: root/base/trace_event_win.h
blob: 14f768ffa46e31035f5e731343cbc01ef7407adc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This file contains the Windows-specific declarations for trace_event.h.
#ifndef BASE_TRACE_EVENT_WIN_H_
#define BASE_TRACE_EVENT_WIN_H_

#include <string>
#include "base/event_trace_provider_win.h"

#define TRACE_EVENT_BEGIN(name, id, extra) \
  base::TraceLog::Trace(name, \
                        base::TraceLog::EVENT_BEGIN, \
                        reinterpret_cast<const void*>(id), \
                        extra);

#define TRACE_EVENT_END(name, id, extra) \
  base::TraceLog::Trace(name, \
                        base::TraceLog::EVENT_END, \
                        reinterpret_cast<const void*>(id), \
                        extra);

#define TRACE_EVENT_INSTANT(name, id, extra) \
  base::TraceLog::Trace(name, \
                        base::TraceLog::EVENT_INSTANT, \
                        reinterpret_cast<const void*>(id), \
                        extra);

// Fwd.
template <typename Type>
struct StaticMemorySingletonTraits;

namespace base {

// This EtwTraceProvider subclass implements ETW logging
// for the macros above on Windows.
class TraceLog : public EtwTraceProvider {
 public:
  enum EventType {
    EVENT_BEGIN,
    EVENT_END,
    EVENT_INSTANT
  };

  // Start logging trace events.
  // This is a noop in this implementation.
  static bool StartTracing();

  // Trace begin/end/instant events, this is the bottleneck implementation
  // all the others defer to.
  // Allowing the use of std::string for name or extra is a convenience,
  // whereas passing name or extra as a const char* avoids the construction
  // of temporary std::string instances.
  // If -1 is passed for name_len or extra_len, the strlen of the string will
  // be used for length.
  static void Trace(const char* name,
                    size_t name_len,
                    EventType type,
                    const void* id,
                    const char* extra,
                    size_t extra_len);

  // Allows passing extra as a std::string for convenience.
  static void Trace(const char* name,
                    EventType type,
                    const void* id,
                    const std::string& extra) {
    return Trace(name, -1, type, id, extra.c_str(), extra.length());
  }

  // Allows passing extra as a const char* to avoid constructing temporary
  // std::string instances where not needed.
  static void Trace(const char* name,
                    EventType type,
                    const void* id,
                    const char* extra) {
    return Trace(name, -1, type, id, extra, -1);
  }

  // Retrieves the singleton.
  // Note that this may return NULL post-AtExit processing.
  static TraceLog* Get();

  // Returns true iff tracing is turned on.
  bool IsTracing() {
    return enable_level() >= TRACE_LEVEL_INFORMATION;
  }

  // Emit a trace of type |type| containing |name|, |id|, and |extra|.
  // Note: |name| and |extra| must be NULL, or a zero-terminated string of
  //    length |name_len| or |extra_len| respectively.
  // Note: if name_len or extra_len are -1, the length of the corresponding
  //    string will be used.
  void TraceEvent(const char* name,
                  size_t name_len,
                  base::TraceLog::EventType type,
                  const void* id,
                  const char* extra,
                  size_t extra_len);

  // Exposed for unittesting only, allows resurrecting our
  // singleton instance post-AtExit processing.
  static void Resurrect();

 private:
  // Ensure only the provider can construct us.
  friend struct StaticMemorySingletonTraits<TraceLog>;
  TraceLog();

  DISALLOW_COPY_AND_ASSIGN(TraceLog);
};

// The ETW trace provider GUID.
extern const GUID kChromeTraceProviderName;

// The ETW event class GUID for 32 bit events.
extern const GUID kTraceEventClass32;

// The ETW event class GUID for 64 bit events.
extern const GUID kTraceEventClass64;

// The ETW event types, IDs 0x00-0x09 are reserved, so start at 0x10.
const EtwEventType kTraceEventTypeBegin = 0x10;
const EtwEventType kTraceEventTypeEnd = 0x11;
const EtwEventType kTraceEventTypeInstant = 0x12;

// If this flag is set in enable flags
enum TraceEventFlags {
  CAPTURE_STACK_TRACE = 0x0001,
};

// The event format consists of:
// The "name" string as a zero-terminated ASCII string.
// The id pointer in the machine bitness.
// The "extra" string as a zero-terminated ASCII string.
// Optionally the stack trace, consisting of a DWORD "depth", followed
//    by an array of void* (machine bitness) of length "depth".

// Forward decl.
struct TraceLogSingletonTraits;

}  // namespace base

#endif  // BASE_TRACE_EVENT_WIN_H_