OLD | NEW |
---|---|
(Empty) | |
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 | |
5 #ifndef CHROME_TEST_BASE_MOF_DATA_PARSER_WIN_H_ | |
6 #define CHROME_TEST_BASE_MOF_DATA_PARSER_WIN_H_ | |
7 #pragma once | |
8 | |
9 #include <stddef.h> | |
10 #include <windows.h> | |
11 #include <wmistr.h> | |
12 #include <evntrace.h> | |
13 | |
14 #include "base/basictypes.h" | |
15 #include "base/string_piece.h" | |
16 | |
17 namespace logging_win { | |
18 | |
19 // A parser for Mof data found in an EVENT_TRACE object as formatted by | |
20 // Chromium-related classes. Instances have an implicit cursor that scans the | |
21 // data. Callers invoke Read* methods to extract primitive data types values or | |
22 // pointers to complex data types (arrays and strings). In the latter case, the | |
23 // pointers are only valid for the lifetime of the underlying event. | |
24 class MofDataParser { | |
25 public: | |
26 explicit MofDataParser(const EVENT_TRACE* event); | |
27 | |
28 bool ReadDWORD(DWORD* value) { | |
29 return ReadPrimitive(value); | |
30 } | |
31 | |
32 bool ReadInt(int* value) { | |
33 return ReadPrimitive(value); | |
34 } | |
35 | |
36 bool ReadPointer(intptr_t* value) { | |
37 return ReadPrimitive(value); | |
38 } | |
39 | |
40 // Populates |values| with a pointer to an array of |size| pointer-sized | |
41 // values in the data. | |
42 bool ReadPointerArray(DWORD size, const intptr_t** values) { | |
43 return ReadPrimitiveArray(size, values); | |
44 } | |
45 | |
46 // Populates |value| with a pointer to an arbitrary data structure at the | |
47 // current position. | |
48 template<typename T> bool ReadStructure(const T** value) { | |
49 if (length_ < sizeof(**value)) | |
50 return false; | |
51 *value = reinterpret_cast<const T*>(scan_); | |
52 Advance(sizeof(**value)); | |
53 return true; | |
54 } | |
55 | |
56 // Sets |value| such that it points to a string in the data. Although the | |
57 // string in the data must be null-terminated, the returned piece may not, | |
58 // as trailing newlines are removed. | |
59 bool ReadString(base::StringPiece* value); | |
erikwright (departed)
2012/03/09 18:50:33
I don't understand the logic of stripping \n\0 if
grt (UTC plus 2)
2012/03/09 20:38:57
Sorry, the comment is confusing. The encoded stri
| |
60 | |
61 bool empty() { return length_ == 0; } | |
62 | |
63 private: | |
64 void Advance(size_t num_bytes) { | |
65 scan_ += num_bytes; | |
66 length_ -= num_bytes; | |
67 } | |
68 | |
69 template<typename T> bool ReadPrimitive(T* value) { | |
70 if (length_ < sizeof(*value)) | |
71 return false; | |
72 *value = *reinterpret_cast<const T*>(scan_); | |
73 Advance(sizeof(*value)); | |
74 return true; | |
75 } | |
76 | |
77 template<typename T> bool ReadPrimitiveArray(DWORD size, const T** values) { | |
78 if (length_ < sizeof(**values) * size) | |
79 return false; | |
80 *values = reinterpret_cast<const T*>(scan_); | |
81 Advance(sizeof(**values) * size); | |
82 return true; | |
83 } | |
84 | |
85 const uint8* scan_; | |
86 uint32 length_; | |
87 }; | |
88 | |
89 } // namespace logging_win | |
90 | |
91 #endif // CHROME_TEST_BASE_MOF_DATA_PARSER_WIN_H_ | |
OLD | NEW |