summaryrefslogtreecommitdiffstats
path: root/base/debug/trace_event_win.cc
blob: 4929944db8e465d7bd1d9ea49d5f786b9d82184b (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
// 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.

#include "base/debug/trace_event_win.h"

#include "base/logging.h"
#include "base/memory/singleton.h"
#include <initguid.h>  // NOLINT

namespace base {
namespace debug {

using base::win::EtwEventType;
using base::win::EtwMofEvent;

// {3DADA31D-19EF-4dc1-B345-037927193422}
const GUID kChromeTraceProviderName = {
    0x3dada31d, 0x19ef, 0x4dc1, 0xb3, 0x45, 0x3, 0x79, 0x27, 0x19, 0x34, 0x22 };

// {B967AE67-BB22-49d7-9406-55D91EE1D560}
const GUID kTraceEventClass32 = {
    0xb967ae67, 0xbb22, 0x49d7, 0x94, 0x6, 0x55, 0xd9, 0x1e, 0xe1, 0xd5, 0x60 };

// {97BE602D-2930-4ac3-8046-B6763B631DFE}
const GUID kTraceEventClass64 = {
    0x97be602d, 0x2930, 0x4ac3, 0x80, 0x46, 0xb6, 0x76, 0x3b, 0x63, 0x1d, 0xfe};


TraceLog::TraceLog() : EtwTraceProvider(kChromeTraceProviderName) {
  Register();
}

TraceLog* TraceLog::GetInstance() {
  return Singleton<TraceLog, StaticMemorySingletonTraits<TraceLog> >::get();
}

bool TraceLog::StartTracing() {
  return true;
}

void TraceLog::TraceEvent(const char* name,
                          size_t name_len,
                          EventType type,
                          const void* id,
                          const char* extra,
                          size_t extra_len) {
  // Make sure we don't touch NULL.
  if (name == NULL)
    name = "";
  if (extra == NULL)
    extra = "";

  EtwEventType etw_type = 0;
  switch (type) {
    case TraceLog::EVENT_BEGIN:
      etw_type = kTraceEventTypeBegin;
      break;
    case TraceLog::EVENT_END:
      etw_type = kTraceEventTypeEnd;
      break;

    case TraceLog::EVENT_INSTANT:
      etw_type = kTraceEventTypeInstant;
      break;

    default:
      NOTREACHED() << "Unknown event type";
      etw_type = kTraceEventTypeInstant;
      break;
  }

  EtwMofEvent<5> event(kTraceEventClass32,
                       etw_type,
                       TRACE_LEVEL_INFORMATION);
  event.SetField(0, name_len + 1, name);
  event.SetField(1, sizeof(id), &id);
  event.SetField(2, extra_len + 1, extra);

  // See whether we're to capture a backtrace.
  void* backtrace[32];
  if (enable_flags() & CAPTURE_STACK_TRACE) {
    DWORD hash = 0;
    DWORD depth = CaptureStackBackTrace(0,
                                        arraysize(backtrace),
                                        backtrace,
                                        &hash);
    event.SetField(3, sizeof(depth), &depth);
    event.SetField(4, sizeof(backtrace[0]) * depth, backtrace);
  }

  // Trace the event.
  Log(event.get());
}

void TraceLog::Trace(const char* name,
                     size_t name_len,
                     EventType type,
                     const void* id,
                     const char* extra,
                     size_t extra_len) {
  TraceLog* provider = TraceLog::GetInstance();
  if (provider && provider->IsTracing()) {
    // Compute the name & extra lengths if not supplied already.
    if (name_len == -1)
      name_len = (name == NULL) ? 0 : strlen(name);
    if (extra_len == -1)
      extra_len = (extra == NULL) ? 0 : strlen(extra);

    provider->TraceEvent(name, name_len, type, id, extra, extra_len);
  }
}

void TraceLog::Resurrect() {
  StaticMemorySingletonTraits<TraceLog>::Resurrect();
}

}  // namespace debug
}  // namespace base