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 #include "base/debug/trace_event_win.h"
5
6 #include "base/logging.h"
7 #include "base/memory/singleton.h"
8 #include <initguid.h> // NOLINT
9
10 namespace base {
11 namespace debug {
12
13 using base::win::EtwEventType;
14 using base::win::EtwMofEvent;
15
16 // {3DADA31D-19EF-4dc1-B345-037927193422}
17 const GUID kChromeTraceProviderName = {
18 0x3dada31d, 0x19ef, 0x4dc1, 0xb3, 0x45, 0x3, 0x79, 0x27, 0x19, 0x34, 0x22 };
19
20 // {B967AE67-BB22-49d7-9406-55D91EE1D560}
21 const GUID kTraceEventClass32 = {
22 0xb967ae67, 0xbb22, 0x49d7, 0x94, 0x6, 0x55, 0xd9, 0x1e, 0xe1, 0xd5, 0x60 };
23
24 // {97BE602D-2930-4ac3-8046-B6763B631DFE}
25 const GUID kTraceEventClass64 = {
26 0x97be602d, 0x2930, 0x4ac3, 0x80, 0x46, 0xb6, 0x76, 0x3b, 0x63, 0x1d, 0xfe};
27
28
TraceEventETWProvider()29 TraceEventETWProvider::TraceEventETWProvider() :
30 EtwTraceProvider(kChromeTraceProviderName) {
31 Register();
32 }
33
GetInstance()34 TraceEventETWProvider* TraceEventETWProvider::GetInstance() {
35 return Singleton<TraceEventETWProvider,
36 StaticMemorySingletonTraits<TraceEventETWProvider> >::get();
37 }
38
StartTracing()39 bool TraceEventETWProvider::StartTracing() {
40 return true;
41 }
42
TraceEvent(const char * name,size_t name_len,char type,const void * id,const char * extra,size_t extra_len)43 void TraceEventETWProvider::TraceEvent(const char* name,
44 size_t name_len,
45 char type,
46 const void* id,
47 const char* extra,
48 size_t extra_len) {
49 // Make sure we don't touch NULL.
50 if (name == NULL)
51 name = "";
52 if (extra == NULL)
53 extra = "";
54
55 EtwEventType etw_type = 0;
56 switch (type) {
57 case TRACE_EVENT_PHASE_BEGIN:
58 etw_type = kTraceEventTypeBegin;
59 break;
60 case TRACE_EVENT_PHASE_END:
61 etw_type = kTraceEventTypeEnd;
62 break;
63
64 case TRACE_EVENT_PHASE_INSTANT:
65 etw_type = kTraceEventTypeInstant;
66 break;
67
68 default:
69 NOTREACHED() << "Unknown event type";
70 etw_type = kTraceEventTypeInstant;
71 break;
72 }
73
74 EtwMofEvent<5> event(kTraceEventClass32,
75 etw_type,
76 TRACE_LEVEL_INFORMATION);
77 event.SetField(0, name_len + 1, name);
78 event.SetField(1, sizeof(id), &id);
79 event.SetField(2, extra_len + 1, extra);
80
81 // These variables are declared here so that they are not out of scope when
82 // the event is logged.
83 DWORD depth;
84 void* backtrace[32];
85
86 // See whether we're to capture a backtrace.
87 if (enable_flags() & CAPTURE_STACK_TRACE) {
88 depth = CaptureStackBackTrace(0,
89 arraysize(backtrace),
90 backtrace,
91 NULL);
92 event.SetField(3, sizeof(depth), &depth);
93 event.SetField(4, sizeof(backtrace[0]) * depth, backtrace);
94 }
95
96 // Trace the event.
97 Log(event.get());
98 }
99
Trace(const char * name,size_t name_len,char type,const void * id,const char * extra,size_t extra_len)100 void TraceEventETWProvider::Trace(const char* name,
101 size_t name_len,
102 char type,
103 const void* id,
104 const char* extra,
105 size_t extra_len) {
106 TraceEventETWProvider* provider = TraceEventETWProvider::GetInstance();
107 if (provider && provider->IsTracing()) {
108 // Compute the name & extra lengths if not supplied already.
109 if (name_len == -1)
110 name_len = (name == NULL) ? 0 : strlen(name);
111 if (extra_len == -1)
112 extra_len = (extra == NULL) ? 0 : strlen(extra);
113
114 provider->TraceEvent(name, name_len, type, id, extra, extra_len);
115 }
116 }
117
Resurrect()118 void TraceEventETWProvider::Resurrect() {
119 StaticMemorySingletonTraits<TraceEventETWProvider>::Resurrect();
120 }
121
122 } // namespace debug
123 } // namespace base
124