1 : // Copyright 2012 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 :
15 : #ifndef SYZYGY_TRACE_PROTOCOL_CALL_TRACE_DEFS_H_
16 : #define SYZYGY_TRACE_PROTOCOL_CALL_TRACE_DEFS_H_
17 :
18 : #include <windows.h>
19 : #include <wmistr.h>
20 : #include <evntrace.h> // NOLINT - wmistr must precede envtrace.h
21 : #include <vector>
22 :
23 : #include "base/basictypes.h"
24 : #include "base/string_piece.h"
25 :
26 : // ID for the call trace provider.
27 m : extern const GUID kCallTraceProvider;
28 :
29 : // Class of trace provider events.
30 m : extern const GUID kCallTraceEventClass;
31 :
32 : // GUID for the kernel trace control interface.
33 m : extern const GUID kSystemTraceControlGuid;
34 :
35 : // This is the absolute minimum number of buffers we will allow, across all
36 : // CPUs.
37 m : extern const size_t kMinEtwBuffers;
38 :
39 : // This is the minimum number of buffers per CPU we'll allow.
40 m : extern const size_t kMinEtwBuffersPerProcessor;
41 :
42 : // Max buffers will be min buffers * kEtwBufferMultiplier.
43 m : extern const size_t kEtwBufferMultiplier;
44 :
45 : // The set of flags to use when logging trace events via ETW.
46 m : extern const int kDefaultEtwTraceFlags;
47 :
48 : // The set of flags to use when logging kernel events via ETW.
49 m : extern const int kDefaultEtwKernelFlags;
50 :
51 : // RPC protocol and endpoint.
52 m : extern const char kSyzygyRpcInstanceIdEnvVar[];
53 m : void GetSyzygyCallTraceRpcProtocol(std::wstring* protocol);
54 m : void GetSyzygyCallTraceRpcEndpoint(const base::StringPiece16& id,
55 m : std::wstring* endpoint);
56 m : void GetSyzygyCallTraceRpcMutexName(const base::StringPiece16& id,
57 m : std::wstring* mutex_name);
58 m : void GetSyzygyCallTraceRpcEventName(const base::StringPiece16& id,
59 m : std::wstring* event_name);
60 :
61 : // Environment variable used to indicate that an RPC session is mandatory.
62 m : extern const char kSyzygyRpcSessionMandatoryEnvVar[];
63 :
64 : // This must be bumped anytime the file format is changed.
65 m : enum {
66 m : TRACE_VERSION_HI = 1,
67 m : TRACE_VERSION_LO = 3,
68 m : };
69 :
70 m : enum TraceEventType {
71 : // Header prefix for a "page" of call trace events.
72 m : TRACE_PAGE_HEADER,
73 : // The actual events are below.
74 m : TRACE_PROCESS_STARTED = 10,
75 m : TRACE_PROCESS_ENDED,
76 m : TRACE_ENTER_EVENT,
77 m : TRACE_EXIT_EVENT,
78 m : TRACE_PROCESS_ATTACH_EVENT,
79 m : TRACE_PROCESS_DETACH_EVENT,
80 m : TRACE_THREAD_ATTACH_EVENT,
81 m : TRACE_THREAD_DETACH_EVENT,
82 m : TRACE_MODULE_EVENT,
83 m : TRACE_BATCH_ENTER,
84 m : TRACE_BATCH_INVOCATION,
85 m : TRACE_THREAD_NAME,
86 m : TRACE_INDEXED_FREQUENCY,
87 m : };
88 :
89 : // All traces are emitted at this trace level.
90 m : const UCHAR CALL_TRACE_LEVEL = TRACE_LEVEL_INFORMATION;
91 :
92 m : enum TraceEventFlags {
93 : // Trace function entry.
94 m : TRACE_FLAG_ENTER = 0x0001,
95 : // Trace function exit.
96 m : TRACE_FLAG_EXIT = 0x0002,
97 : // Capture stack traces on entry and exit.
98 m : TRACE_FLAG_STACK_TRACES = 0x0004,
99 : // Trace DLL load/unload events.
100 m : TRACE_FLAG_LOAD_EVENTS = 0x0008,
101 : // Trace DLL thread events.
102 m : TRACE_FLAG_THREAD_EVENTS = 0x0010,
103 : // Batch entry traces.
104 m : TRACE_FLAG_BATCH_ENTER = 0x0020,
105 m : };
106 :
107 : // Max depth of stack trace captured on entry/exit.
108 m : const size_t kMaxTraceDepth = 32;
109 :
110 m : typedef const void* RetAddr;
111 m : typedef const void* FuncAddr;
112 m : typedef const void* ModuleAddr;
113 m : typedef DWORD ArgumentWord;
114 m : typedef DWORD RetValueWord;
115 m : typedef void* SessionHandle;
116 :
117 : // A prefix for each trace record on disk.
118 m : struct RecordPrefix {
119 : // The timestamp of the trace event.
120 m : uint32 timestamp;
121 :
122 : // The size of the record, in bytes;
123 m : uint32 size;
124 :
125 : // The type of trace record. Will be a value from the TraceEventType
126 : // enumeration.
127 m : uint16 type;
128 :
129 : // If the call trace service aggregates all trace records to a single
130 : // file, instead of a file per process, then it's possible that a
131 : // single file could contain traces produced by multiple versions of
132 : // the client library.
133 m : struct {
134 m : uint8 hi;
135 m : uint8 lo;
136 m : } version;
137 m : };
138 :
139 m : COMPILE_ASSERT(sizeof(RecordPrefix) == 12, record_prefix_size_is_12);
140 :
141 : // This structure is written at the beginning of a call trace file. If the
142 : // format of this trace file changes the server version must be increased.
143 m : struct TraceFileHeader {
144 : // Everything in this header up to and including the header_size field should
145 : // not be changed in order, layout or alignment. This allows the beginning of
146 : // the header to be read across all trace file versions. If adding a new
147 : // fixed length field, do so immediately prior to blob_data. If adding a new
148 : // variable length field, append it to blob data updating the comment below,
149 : // and both the reading and writing of TraceFileHeader.
150 :
151 : // The "magic-number" identifying this as a Syzygy call-trace file.
152 : // In a valid trace file this will be "SZGY".
153 m : typedef char Signature[4];
154 :
155 : // A canonical value for the signature.
156 m : static const Signature kSignatureValue;
157 :
158 : // A signature is at the start of the trace file header.
159 m : Signature signature;
160 :
161 : // The version of the call trace service which recorded this trace file.
162 m : struct {
163 m : uint16 lo;
164 m : uint16 hi;
165 m : } server_version;
166 :
167 : // The number of bytes in the header. This is the size of this structure
168 : // plus the length of the blob.
169 m : uint32 header_size;
170 :
171 : // The block size used when writing the file to disk. The header and
172 : // all segments are padded and byte aligned to this block size.
173 m : uint32 block_size;
174 :
175 : // The id of the process being traced.
176 m : uint32 process_id;
177 :
178 : // The timestamp (in ticks) when this trace file was created.
179 m : uint32 timestamp;
180 :
181 : // The base address at which the executable module was loaded when the
182 : // trace file was created.
183 m : uint32 module_base_address;
184 :
185 : // The size of the executable module.
186 m : uint32 module_size;
187 :
188 : // The checksum of the executable module.
189 m : uint32 module_checksum;
190 :
191 : // The timestamp of the executable module.
192 m : uint32 module_time_date_stamp;
193 :
194 : // System information.
195 m : OSVERSIONINFOEX os_version_info;
196 m : SYSTEM_INFO system_info;
197 m : MEMORYSTATUSEX memory_status;
198 :
199 : // The header is required to store multiple variable length fields. We do
200 : // this via a blob mechanism. The header contains a single binary blob at the
201 : // end, whose length in bytes) is encoded via blob_length.
202 : //
203 : // Currently, the header stores the following variable length fields (in
204 : // the order indicated):
205 : //
206 : // 1. The path to the instrumented module, a NULL terminated wide string.
207 : // 2. The command line for the process, a NULL terminated wide string.
208 : // 3. The environment string for the process, an array of wide chars
209 : // terminated by a double NULL (individual environment variables are
210 : // separated by single NULLs).
211 :
212 : // This stores the variable length data, concatenated. This should be pointer
213 : // aligned so that PODs with alignment constraints embedded in the blob can be
214 : // read directly from a header loaded into memory.
215 m : uint8 blob_data[1];
216 m : };
217 :
218 : // Written at the beginning of a call trace file segment. Each call trace file
219 : // segment has a length, which on-disk is rounded up to the block_size, as
220 : // recorded in the TraceFileHeader. Within a call trace segment, there are one
221 : // or more records, each prefixed with a RecordPrefix, which describes the
222 : // length and type of the data to follow.
223 m : struct TraceFileSegmentHeader {
224 : // Type identifiers used for these headers.
225 m : enum { kTypeId = TRACE_PAGE_HEADER };
226 :
227 : // The identity of the thread that is reporting in this segment
228 : // of the trace file.
229 m : uint32 thread_id;
230 :
231 : // The number of data bytes in this segment of the trace file. This
232 : // value does not include the size of the record prefix nor the size
233 : // of the segment header.
234 m : uint32 segment_length;
235 m : };
236 :
237 : // The structure traced on function entry or exit.
238 m : template<int TypeId>
239 m : struct TraceEnterExitEventDataTempl {
240 m : enum { kTypeId = TypeId };
241 m : RetAddr retaddr;
242 m : FuncAddr function;
243 m : };
244 :
245 m : typedef TraceEnterExitEventDataTempl<TRACE_ENTER_EVENT> TraceEnterEventData;
246 m : typedef TraceEnterExitEventDataTempl<TRACE_EXIT_EVENT> TraceExitEventData;
247 m : typedef TraceEnterEventData TraceEnterExitEventData;
248 :
249 : // The structure written for each loaded module when module event tracing is
250 : // enabled.
251 m : struct TraceModuleData {
252 m : ModuleAddr module_base_addr;
253 m : size_t module_base_size;
254 m : uint32 module_checksum;
255 m : uint32 module_time_date_stamp;
256 m : wchar_t module_name[256];
257 m : wchar_t module_exe[MAX_PATH];
258 m : };
259 :
260 : // This is for storing environment string information. Each environment string
261 : // consists of a pair of strings, the key and the value. Certain special
262 : // strings have empty keys.
263 m : typedef std::vector<std::pair<std::wstring, std::wstring>>
264 m : TraceEnvironmentStrings;
265 :
266 : // Describes the system information and environment in which a process is
267 : // running.
268 m : struct TraceSystemInfo {
269 m : OSVERSIONINFOEX os_version_info;
270 m : SYSTEM_INFO system_info;
271 m : MEMORYSTATUSEX memory_status;
272 m : TraceEnvironmentStrings environment_strings;
273 m : };
274 :
275 : // The structure traced for batch entry traces.
276 m : struct TraceBatchEnterData {
277 m : enum { kTypeId = TRACE_BATCH_ENTER };
278 :
279 : // The thread ID from which these traces originate. This can differ
280 : // from the logging thread ID when a process exits, and the exiting
281 : // thread flushes the trace buffers from its expired brethren.
282 m : DWORD thread_id;
283 :
284 : // Number of function entries.
285 m : size_t num_calls;
286 :
287 : // Back-to-back entry events.
288 m : TraceEnterEventData calls[1];
289 m : };
290 :
291 : // This is the data recorded for each distinct caller/function
292 : // pair by the profiler.
293 m : struct InvocationInfo {
294 m : RetAddr caller;
295 m : FuncAddr function;
296 m : size_t num_calls;
297 m : uint64 cycles_min;
298 m : uint64 cycles_max;
299 m : uint64 cycles_sum;
300 m : };
301 :
302 m : struct TraceBatchInvocationInfo {
303 m : enum { kTypeId = TRACE_BATCH_INVOCATION };
304 :
305 : // TODO(siggi): Perhaps the batch should carry the time resolution for
306 : // the invocation data?
307 :
308 : // Back to back entries, as many as our enclosing record's size allows for.
309 m : InvocationInfo invocations[1];
310 m : };
311 :
312 m : struct TraceThreadNameInfo {
313 m : enum { kTypeId = TRACE_THREAD_NAME };
314 : // In fact as many as our enclosing record's size allows for,
315 : // zero terminated.
316 m : char thread_name[1];
317 m : };
318 :
319 m : struct TraceIndexedFrequencyData {
320 m : enum { kTypeId = TRACE_INDEXED_FREQUENCY };
321 :
322 m : enum DataType {
323 m : BASIC_BLOCK = 0,
324 m : JUMP_TABLE = 1,
325 m : };
326 :
327 : // This is used to tie the data to a particular module, which has already
328 : // been reported via a TraceModuleData struct.
329 m : ModuleAddr module_base_addr;
330 m : size_t module_base_size;
331 m : uint32 module_checksum;
332 m : uint32 module_time_date_stamp;
333 :
334 : // The number of entries being reported. It is up to the instrumentation to
335 : // output any other metadata that is required to map an index to an address.
336 m : uint32 num_entries;
337 :
338 : // The type of data contained in this frequency record.
339 m : uint8 data_type;
340 :
341 : // The size of the frequency reports: 1, 2 or 4 bytes.
342 m : uint8 frequency_size;
343 :
344 : // In fact, there are frequency_size * num_basic_blocks bytes that follow.
345 m : uint8 frequency_data[1];
346 m : };
347 :
348 : #endif // SYZYGY_TRACE_PROTOCOL_CALL_TRACE_DEFS_H_
|