summaryrefslogtreecommitdiffstats
path: root/base/debug/trace_event_win.h
blob: 6e6bcb46e04a471e1106bc5e12ef51559cba5fcb (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
// Copyright (c) 2011 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_DEBUG_TRACE_EVENT_WIN_H_
#define BASE_DEBUG_TRACE_EVENT_WIN_H_
#pragma once

#include <string>

#include "base/base_api.h"
#include "base/debug/trace_event.h"
#include "base/win/event_trace_provider.h"

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

namespace base {
namespace debug {

// This EtwTraceProvider subclass implements ETW logging
// for the macros above on Windows.
class BASE_API TraceEventETWProvider : public base::win::EtwTraceProvider {
 public:
  // 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,
                    TraceEventPhase 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,
                    TraceEventPhase 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,
                    TraceEventPhase 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 TraceEventETWProvider* GetInstance();

  // 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,
                  TraceEventPhase 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<TraceEventETWProvider>;
  TraceEventETWProvider();

  DISALLOW_COPY_AND_ASSIGN(TraceEventETWProvider);
};

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

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

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

// The ETW event types, IDs 0x00-0x09 are reserved, so start at 0x10.
const base::win::EtwEventType kTraceEventTypeBegin = 0x10;
const base::win::EtwEventType kTraceEventTypeEnd = 0x11;
const base::win::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;

}  // nemspace debug
}  // namespace base

#endif  // BASE_DEBUG_TRACE_EVENT_WIN_H_