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.
5 #include "chrome/test/logging/win/log_file_reader.h"
7 #include "base/debug/trace_event_win.h"
8 #include "base/files/file_path.h"
9 #include "base/lazy_instance.h"
10 #include "base/logging_win.h"
11 #include "base/synchronization/lock.h"
12 #include "base/win/event_trace_consumer.h"
13 #include "chrome/test/logging/win/mof_data_parser.h"
15 namespace logging_win
{
19 // TODO(grt) This reverses a mapping produced by base/logging_win.cc's
20 // LogEventProvider::LogMessage. LogEventProvider should expose a way to map an
21 // event level back to a log severity.
22 logging::LogSeverity
EventLevelToSeverity(uint8 level
) {
24 case TRACE_LEVEL_NONE
:
26 return logging::LOG_ERROR
;
27 case TRACE_LEVEL_FATAL
:
28 return logging::LOG_FATAL
;
29 case TRACE_LEVEL_ERROR
:
30 return logging::LOG_ERROR
;
31 case TRACE_LEVEL_WARNING
:
32 return logging::LOG_WARNING
;
33 case TRACE_LEVEL_INFORMATION
:
34 return logging::LOG_INFO
;
36 // Trace levels above information correspond to negative severity levels,
37 // which are used for VLOG verbosity levels.
38 return TRACE_LEVEL_INFORMATION
- level
;
42 // TODO(grt) This reverses a mapping produced by base/debug/trace_event_win.cc's
43 // TraceEventETWProvider::TraceEvent. TraceEventETWProvider should expose a way
44 // to map an event type back to a trace type.
45 char EventTypeToTraceType(uint8 event_type
) {
47 case base::debug::kTraceEventTypeBegin
:
48 return TRACE_EVENT_PHASE_BEGIN
;
50 case base::debug::kTraceEventTypeEnd
:
51 return TRACE_EVENT_PHASE_END
;
53 case base::debug::kTraceEventTypeInstant
:
54 return TRACE_EVENT_PHASE_INSTANT
;
65 explicit LogFileReader(LogFileDelegate
* delegate
);
68 static void ReadFile(const base::FilePath
& log_file
,
69 LogFileDelegate
* delegate
);
72 // An implementation of a trace consumer that delegates to a given (at
73 // compile-time) event processing function.
74 template<void (*ProcessEventFn
)(EVENT_TRACE
*)>
76 : public base::win::EtwTraceConsumerBase
<TraceConsumer
<ProcessEventFn
> > {
79 static void ProcessEvent(EVENT_TRACE
* event
) { (*ProcessEventFn
)(event
); }
81 DISALLOW_COPY_AND_ASSIGN(TraceConsumer
);
84 // Delegates to DispatchEvent() of the current LogDumper instance.
85 static void ProcessEvent(EVENT_TRACE
* event
);
87 // Handlers for the supported event types.
88 bool OnLogMessageEvent(const EVENT_TRACE
* event
);
89 bool OnLogMessageFullEvent(const EVENT_TRACE
* event
);
90 bool OnTraceEvent(const EVENT_TRACE
* event
);
91 bool OnFileHeader(const EVENT_TRACE
* event
);
93 // Parses an event and passes it along to the delegate for processing.
94 void DispatchEvent(const EVENT_TRACE
* event
);
96 // Reads the file using a trace consumer. |ProcessEvent| will be invoked for
97 // each event in the file.
98 void Read(const base::FilePath
& log_file
);
100 // Protects use of the class; only one instance may be live at a time.
101 static base::LazyInstance
<base::Lock
>::Leaky reader_lock_
;
103 // The currently living instance.
104 static LogFileReader
* instance_
;
106 // The delegate to be notified of events.
107 LogFileDelegate
* delegate_
;
111 base::LazyInstance
<base::Lock
>::Leaky
LogFileReader::reader_lock_
=
112 LAZY_INSTANCE_INITIALIZER
;
115 LogFileReader
* LogFileReader::instance_
= NULL
;
117 LogFileReader::LogFileReader(LogFileDelegate
* delegate
)
118 : delegate_(delegate
) {
119 DCHECK(instance_
== NULL
);
120 DCHECK(delegate
!= NULL
);
124 LogFileReader::~LogFileReader() {
125 DCHECK_EQ(instance_
, this);
130 void LogFileReader::ProcessEvent(EVENT_TRACE
* event
) {
131 if (instance_
!= NULL
)
132 instance_
->DispatchEvent(event
);
135 bool LogFileReader::OnLogMessageEvent(const EVENT_TRACE
* event
) {
136 base::StringPiece message
;
137 MofDataParser
parser(event
);
139 // See LogEventProvider::LogMessage where ENABLE_LOG_MESSAGE_ONLY is set.
140 if (parser
.ReadString(&message
) && parser
.empty()) {
141 delegate_
->OnLogMessage(event
,
142 EventLevelToSeverity(event
->Header
.Class
.Level
),
149 bool LogFileReader::OnLogMessageFullEvent(const EVENT_TRACE
* event
) {
150 DWORD stack_depth
= 0;
151 const intptr_t* backtrace
= NULL
;
153 base::StringPiece file
;
154 base::StringPiece message
;
155 MofDataParser
parser(event
);
157 // See LogEventProvider::LogMessage where ENABLE_LOG_MESSAGE_ONLY is not set.
158 if (parser
.ReadDWORD(&stack_depth
) &&
159 parser
.ReadPointerArray(stack_depth
, &backtrace
) &&
160 parser
.ReadInt(&line
) &&
161 parser
.ReadString(&file
) &&
162 parser
.ReadString(&message
) &&
164 delegate_
->OnLogMessageFull(event
,
165 EventLevelToSeverity(event
->Header
.Class
.Level
), stack_depth
, backtrace
,
166 line
, file
, message
);
172 bool LogFileReader::OnTraceEvent(const EVENT_TRACE
* event
) {
173 MofDataParser
parser(event
);
174 base::StringPiece name
;
176 base::StringPiece extra
;
177 DWORD stack_depth
= 0;
178 const intptr_t* backtrace
= NULL
;
180 // See TraceEventETWProvider::TraceEvent.
181 if (parser
.ReadString(&name
) &&
182 parser
.ReadPointer(&id
) &&
183 parser
.ReadString(&extra
) &&
185 parser
.ReadDWORD(&stack_depth
) &&
186 parser
.ReadPointerArray(stack_depth
, &backtrace
) &&
188 delegate_
->OnTraceEvent(event
, name
,
189 EventTypeToTraceType(event
->Header
.Class
.Type
), id
, extra
, stack_depth
,
196 bool LogFileReader::OnFileHeader(const EVENT_TRACE
* event
) {
197 MofDataParser
parser(event
);
198 const TRACE_LOGFILE_HEADER
* header
= NULL
;
200 if (parser
.ReadStructure(&header
)) {
201 delegate_
->OnFileHeader(event
, header
);
207 void LogFileReader::DispatchEvent(const EVENT_TRACE
* event
) {
210 if (IsEqualGUID(event
->Header
.Guid
, logging::kLogEventId
)) {
211 if (event
->Header
.Class
.Type
== logging::LOG_MESSAGE
)
212 parsed
= OnLogMessageEvent(event
);
213 else if (event
->Header
.Class
.Type
== logging::LOG_MESSAGE_FULL
)
214 parsed
= OnLogMessageFullEvent(event
);
215 } else if (IsEqualGUID(event
->Header
.Guid
, base::debug::kTraceEventClass32
)) {
216 parsed
= OnTraceEvent(event
);
217 } else if (IsEqualGUID(event
->Header
.Guid
, EventTraceGuid
)) {
218 parsed
= OnFileHeader(event
);
221 delegate_
->OnUnknownEvent(event
);
224 delegate_
->OnUnparsableEvent(event
);
227 void LogFileReader::Read(const base::FilePath
& log_file
) {
228 TraceConsumer
<&ProcessEvent
> consumer
;
231 hr
= consumer
.OpenFileSession(log_file
.value().c_str());
233 LOG(ERROR
) << "Failed to open session for log file " << log_file
.value()
234 << "; hr=" << std::hex
<< hr
;
242 void LogFileReader::ReadFile(const base::FilePath
& log_file
,
243 LogFileDelegate
* delegate
) {
244 base::AutoLock
lock(reader_lock_
.Get());
246 LogFileReader(delegate
).Read(log_file
);
251 LogFileDelegate::LogFileDelegate() {
254 LogFileDelegate::~LogFileDelegate() {
257 void ReadLogFile(const base::FilePath
& log_file
, LogFileDelegate
* delegate
) {
259 LogFileReader::ReadFile(log_file
, delegate
);