OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/test/logging/win/log_file_reader.h" | 5 #include "chrome/test/logging/win/log_file_reader.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/logging_win.h" | 9 #include "base/logging_win.h" |
10 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
11 #include "base/trace_event/trace_event_win.h" | |
12 #include "base/win/event_trace_consumer.h" | 11 #include "base/win/event_trace_consumer.h" |
13 #include "chrome/test/logging/win/mof_data_parser.h" | 12 #include "chrome/test/logging/win/mof_data_parser.h" |
14 | 13 |
15 namespace logging_win { | 14 namespace logging_win { |
16 | 15 |
17 namespace { | 16 namespace { |
18 | 17 |
19 // TODO(grt) This reverses a mapping produced by base/logging_win.cc's | 18 // TODO(grt) This reverses a mapping produced by base/logging_win.cc's |
20 // LogEventProvider::LogMessage. LogEventProvider should expose a way to map an | 19 // LogEventProvider::LogMessage. LogEventProvider should expose a way to map an |
21 // event level back to a log severity. | 20 // event level back to a log severity. |
(...skipping 10 matching lines...) Expand all Loading... |
32 return logging::LOG_WARNING; | 31 return logging::LOG_WARNING; |
33 case TRACE_LEVEL_INFORMATION: | 32 case TRACE_LEVEL_INFORMATION: |
34 return logging::LOG_INFO; | 33 return logging::LOG_INFO; |
35 default: | 34 default: |
36 // Trace levels above information correspond to negative severity levels, | 35 // Trace levels above information correspond to negative severity levels, |
37 // which are used for VLOG verbosity levels. | 36 // which are used for VLOG verbosity levels. |
38 return TRACE_LEVEL_INFORMATION - level; | 37 return TRACE_LEVEL_INFORMATION - level; |
39 } | 38 } |
40 } | 39 } |
41 | 40 |
42 // TODO(grt): This reverses a mapping produced by | |
43 // base/trace_event/trace_event_win.cc's TraceEventETWProvider::TraceEvent. | |
44 // TraceEventETWProvider should expose a way to map an event type back to a | |
45 // trace type. | |
46 char EventTypeToTraceType(uint8 event_type) { | |
47 switch (event_type) { | |
48 case base::trace_event::kTraceEventTypeBegin: | |
49 return TRACE_EVENT_PHASE_BEGIN; | |
50 break; | |
51 case base::trace_event::kTraceEventTypeEnd: | |
52 return TRACE_EVENT_PHASE_END; | |
53 break; | |
54 case base::trace_event::kTraceEventTypeInstant: | |
55 return TRACE_EVENT_PHASE_INSTANT; | |
56 break; | |
57 default: | |
58 NOTREACHED(); | |
59 return '\0'; | |
60 break; | |
61 } | |
62 } | |
63 | |
64 class LogFileReader { | 41 class LogFileReader { |
65 public: | 42 public: |
66 explicit LogFileReader(LogFileDelegate* delegate); | 43 explicit LogFileReader(LogFileDelegate* delegate); |
67 ~LogFileReader(); | 44 ~LogFileReader(); |
68 | 45 |
69 static void ReadFile(const base::FilePath& log_file, | 46 static void ReadFile(const base::FilePath& log_file, |
70 LogFileDelegate* delegate); | 47 LogFileDelegate* delegate); |
71 | 48 |
72 private: | 49 private: |
73 // An implementation of a trace consumer that delegates to a given (at | 50 // An implementation of a trace consumer that delegates to a given (at |
74 // compile-time) event processing function. | 51 // compile-time) event processing function. |
75 template<void (*ProcessEventFn)(EVENT_TRACE*)> | 52 template<void (*ProcessEventFn)(EVENT_TRACE*)> |
76 class TraceConsumer | 53 class TraceConsumer |
77 : public base::win::EtwTraceConsumerBase<TraceConsumer<ProcessEventFn> > { | 54 : public base::win::EtwTraceConsumerBase<TraceConsumer<ProcessEventFn> > { |
78 public: | 55 public: |
79 TraceConsumer() { } | 56 TraceConsumer() { } |
80 static void ProcessEvent(EVENT_TRACE* event) { (*ProcessEventFn)(event); } | 57 static void ProcessEvent(EVENT_TRACE* event) { (*ProcessEventFn)(event); } |
81 private: | 58 private: |
82 DISALLOW_COPY_AND_ASSIGN(TraceConsumer); | 59 DISALLOW_COPY_AND_ASSIGN(TraceConsumer); |
83 }; | 60 }; |
84 | 61 |
85 // Delegates to DispatchEvent() of the current LogDumper instance. | 62 // Delegates to DispatchEvent() of the current LogDumper instance. |
86 static void ProcessEvent(EVENT_TRACE* event); | 63 static void ProcessEvent(EVENT_TRACE* event); |
87 | 64 |
88 // Handlers for the supported event types. | 65 // Handlers for the supported event types. |
89 bool OnLogMessageEvent(const EVENT_TRACE* event); | 66 bool OnLogMessageEvent(const EVENT_TRACE* event); |
90 bool OnLogMessageFullEvent(const EVENT_TRACE* event); | 67 bool OnLogMessageFullEvent(const EVENT_TRACE* event); |
91 bool OnTraceEvent(const EVENT_TRACE* event); | |
92 bool OnFileHeader(const EVENT_TRACE* event); | 68 bool OnFileHeader(const EVENT_TRACE* event); |
93 | 69 |
94 // Parses an event and passes it along to the delegate for processing. | 70 // Parses an event and passes it along to the delegate for processing. |
95 void DispatchEvent(const EVENT_TRACE* event); | 71 void DispatchEvent(const EVENT_TRACE* event); |
96 | 72 |
97 // Reads the file using a trace consumer. |ProcessEvent| will be invoked for | 73 // Reads the file using a trace consumer. |ProcessEvent| will be invoked for |
98 // each event in the file. | 74 // each event in the file. |
99 void Read(const base::FilePath& log_file); | 75 void Read(const base::FilePath& log_file); |
100 | 76 |
101 // Protects use of the class; only one instance may be live at a time. | 77 // Protects use of the class; only one instance may be live at a time. |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 parser.ReadString(&message) && | 139 parser.ReadString(&message) && |
164 parser.empty()) { | 140 parser.empty()) { |
165 delegate_->OnLogMessageFull(event, | 141 delegate_->OnLogMessageFull(event, |
166 EventLevelToSeverity(event->Header.Class.Level), stack_depth, backtrace, | 142 EventLevelToSeverity(event->Header.Class.Level), stack_depth, backtrace, |
167 line, file, message); | 143 line, file, message); |
168 return true; | 144 return true; |
169 } | 145 } |
170 return false; | 146 return false; |
171 } | 147 } |
172 | 148 |
173 bool LogFileReader::OnTraceEvent(const EVENT_TRACE* event) { | |
174 MofDataParser parser(event); | |
175 base::StringPiece name; | |
176 intptr_t id = 0; | |
177 base::StringPiece extra; | |
178 DWORD stack_depth = 0; | |
179 const intptr_t* backtrace = NULL; | |
180 | |
181 // See TraceEventETWProvider::TraceEvent. | |
182 if (parser.ReadString(&name) && parser.ReadPointer(&id) && | |
183 parser.ReadString(&extra) && | |
184 (parser.empty() || | |
185 (parser.ReadDWORD(&stack_depth) && | |
186 parser.ReadPointerArray(stack_depth, &backtrace) && parser.empty()))) { | |
187 delegate_->OnTraceEvent(event, name, | |
188 EventTypeToTraceType(event->Header.Class.Type), id, extra, stack_depth, | |
189 backtrace); | |
190 return true; | |
191 } | |
192 return false; | |
193 } | |
194 | |
195 bool LogFileReader::OnFileHeader(const EVENT_TRACE* event) { | 149 bool LogFileReader::OnFileHeader(const EVENT_TRACE* event) { |
196 MofDataParser parser(event); | 150 MofDataParser parser(event); |
197 const TRACE_LOGFILE_HEADER* header = NULL; | 151 const TRACE_LOGFILE_HEADER* header = NULL; |
198 | 152 |
199 if (parser.ReadStructure(&header)) { | 153 if (parser.ReadStructure(&header)) { |
200 delegate_->OnFileHeader(event, header); | 154 delegate_->OnFileHeader(event, header); |
201 return true; | 155 return true; |
202 } | 156 } |
203 return false; | 157 return false; |
204 } | 158 } |
205 | 159 |
206 void LogFileReader::DispatchEvent(const EVENT_TRACE* event) { | 160 void LogFileReader::DispatchEvent(const EVENT_TRACE* event) { |
207 bool parsed = true; | 161 bool parsed = true; |
208 | 162 |
209 if (IsEqualGUID(event->Header.Guid, logging::kLogEventId)) { | 163 if (IsEqualGUID(event->Header.Guid, logging::kLogEventId)) { |
210 if (event->Header.Class.Type == logging::LOG_MESSAGE) | 164 if (event->Header.Class.Type == logging::LOG_MESSAGE) |
211 parsed = OnLogMessageEvent(event); | 165 parsed = OnLogMessageEvent(event); |
212 else if (event->Header.Class.Type == logging::LOG_MESSAGE_FULL) | 166 else if (event->Header.Class.Type == logging::LOG_MESSAGE_FULL) |
213 parsed = OnLogMessageFullEvent(event); | 167 parsed = OnLogMessageFullEvent(event); |
214 } else if (IsEqualGUID(event->Header.Guid, | |
215 base::trace_event::kTraceEventClass32)) { | |
216 parsed = OnTraceEvent(event); | |
217 } else if (IsEqualGUID(event->Header.Guid, EventTraceGuid)) { | 168 } else if (IsEqualGUID(event->Header.Guid, EventTraceGuid)) { |
218 parsed = OnFileHeader(event); | 169 parsed = OnFileHeader(event); |
219 } else { | 170 } else { |
220 DCHECK(parsed); | 171 DCHECK(parsed); |
221 delegate_->OnUnknownEvent(event); | 172 delegate_->OnUnknownEvent(event); |
222 } | 173 } |
223 if (!parsed) | 174 if (!parsed) |
224 delegate_->OnUnparsableEvent(event); | 175 delegate_->OnUnparsableEvent(event); |
225 } | 176 } |
226 | 177 |
(...skipping 26 matching lines...) Expand all Loading... |
253 | 204 |
254 LogFileDelegate::~LogFileDelegate() { | 205 LogFileDelegate::~LogFileDelegate() { |
255 } | 206 } |
256 | 207 |
257 void ReadLogFile(const base::FilePath& log_file, LogFileDelegate* delegate) { | 208 void ReadLogFile(const base::FilePath& log_file, LogFileDelegate* delegate) { |
258 DCHECK(delegate); | 209 DCHECK(delegate); |
259 LogFileReader::ReadFile(log_file, delegate); | 210 LogFileReader::ReadFile(log_file, delegate); |
260 } | 211 } |
261 | 212 |
262 } // namespace logging_win | 213 } // namespace logging_win |
OLD | NEW |