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 : #include "syzygy/common/assertions.h"
26 : #include "syzygy/trace/common/clock.h"
27 :
28 : // ID for the call trace provider.
29 m : extern const GUID kCallTraceProvider;
30 :
31 : // Class of trace provider events.
32 m : extern const GUID kCallTraceEventClass;
33 :
34 : // GUID for the kernel trace control interface.
35 m : extern const GUID kSystemTraceControlGuid;
36 :
37 : // This is the absolute minimum number of buffers we will allow, across all
38 : // CPUs.
39 m : extern const size_t kMinEtwBuffers;
40 :
41 : // This is the minimum number of buffers per CPU we'll allow.
42 m : extern const size_t kMinEtwBuffersPerProcessor;
43 :
44 : // Max buffers will be min buffers * kEtwBufferMultiplier.
45 m : extern const size_t kEtwBufferMultiplier;
46 :
47 : // The set of flags to use when logging trace events via ETW.
48 m : extern const int kDefaultEtwTraceFlags;
49 :
50 : // The set of flags to use when logging kernel events via ETW.
51 m : extern const int kDefaultEtwKernelFlags;
52 :
53 : // RPC protocol and endpoint.
54 m : extern const char kSyzygyRpcInstanceIdEnvVar[];
55 m : void GetSyzygyCallTraceRpcProtocol(std::wstring* protocol);
56 m : void GetSyzygyCallTraceRpcEndpoint(const base::StringPiece16& id,
57 m : std::wstring* endpoint);
58 m : void GetSyzygyCallTraceRpcMutexName(const base::StringPiece16& id,
59 m : std::wstring* mutex_name);
60 m : void GetSyzygyCallTraceRpcEventName(const base::StringPiece16& id,
61 m : std::wstring* event_name);
62 :
63 : // Environment variable used to indicate that an RPC session is mandatory.
64 m : extern const char kSyzygyRpcSessionMandatoryEnvVar[];
65 :
66 : // This must be bumped anytime the file format is changed.
67 m : enum {
68 m : TRACE_VERSION_HI = 1,
69 m : TRACE_VERSION_LO = 4,
70 m : };
71 :
72 m : enum TraceEventType {
73 : // Header prefix for a "page" of call trace events.
74 m : TRACE_PAGE_HEADER,
75 : // The actual events are below.
76 m : TRACE_PROCESS_STARTED = 10,
77 m : TRACE_PROCESS_ENDED,
78 m : TRACE_ENTER_EVENT,
79 m : TRACE_EXIT_EVENT,
80 m : TRACE_PROCESS_ATTACH_EVENT,
81 m : TRACE_PROCESS_DETACH_EVENT,
82 m : TRACE_THREAD_ATTACH_EVENT,
83 m : TRACE_THREAD_DETACH_EVENT,
84 m : TRACE_MODULE_EVENT,
85 m : TRACE_BATCH_ENTER,
86 m : TRACE_BATCH_INVOCATION,
87 m : TRACE_THREAD_NAME,
88 m : TRACE_INDEXED_FREQUENCY,
89 m : TRACE_DYNAMIC_SYMBOL,
90 m : TRACE_SAMPLE_DATA,
91 m : };
92 :
93 : // All traces are emitted at this trace level.
94 m : const UCHAR CALL_TRACE_LEVEL = TRACE_LEVEL_INFORMATION;
95 :
96 m : enum TraceEventFlags {
97 : // Trace function entry.
98 m : TRACE_FLAG_ENTER = 0x0001,
99 : // Trace function exit.
100 m : TRACE_FLAG_EXIT = 0x0002,
101 : // Capture stack traces on entry and exit.
102 m : TRACE_FLAG_STACK_TRACES = 0x0004,
103 : // Trace DLL load/unload events.
104 m : TRACE_FLAG_LOAD_EVENTS = 0x0008,
105 : // Trace DLL thread events.
106 m : TRACE_FLAG_THREAD_EVENTS = 0x0010,
107 : // Batch entry traces.
108 m : TRACE_FLAG_BATCH_ENTER = 0x0020,
109 m : };
110 :
111 : // Max depth of stack trace captured on entry/exit.
112 m : const size_t kMaxTraceDepth = 32;
113 :
114 m : typedef const void* RetAddr;
115 m : typedef const void* FuncAddr;
116 m : typedef const void* ModuleAddr;
117 m : typedef DWORD ArgumentWord;
118 m : typedef DWORD RetValueWord;
119 m : typedef void* SessionHandle;
120 :
121 : // A prefix for each trace record on disk.
122 m : struct RecordPrefix {
123 : // The timestamp of the trace event.
124 m : uint64 timestamp;
125 :
126 : // The size of the record, in bytes;
127 m : uint32 size;
128 :
129 : // The type of trace record. Will be a value from the TraceEventType
130 : // enumeration.
131 m : uint16 type;
132 :
133 : // If the call trace service aggregates all trace records to a single
134 : // file, instead of a file per process, then it's possible that a
135 : // single file could contain traces produced by multiple versions of
136 : // the client library.
137 m : struct {
138 m : uint8 hi;
139 m : uint8 lo;
140 m : } version;
141 m : };
142 m : COMPILE_ASSERT_IS_POD_OF_SIZE(RecordPrefix, 16);
143 :
144 : // This structure is written at the beginning of a call trace file. If the
145 : // format of this trace file changes the server version must be increased.
146 m : struct TraceFileHeader {
147 : // Everything in this header up to and including the header_size field should
148 : // not be changed in order, layout or alignment. This allows the beginning of
149 : // the header to be read across all trace file versions. If adding a new
150 : // fixed length field, do so immediately prior to blob_data. If adding a new
151 : // variable length field, append it to blob data updating the comment below,
152 : // and both the reading and writing of TraceFileHeader.
153 :
154 : // The "magic-number" identifying this as a Syzygy call-trace file.
155 : // In a valid trace file this will be "SZGY".
156 m : typedef char Signature[4];
157 :
158 : // A canonical value for the signature.
159 m : static const Signature kSignatureValue;
160 :
161 : // A signature is at the start of the trace file header.
162 m : Signature signature;
163 :
164 : // The version of the call trace service which recorded this trace file.
165 m : struct {
166 m : uint16 lo;
167 m : uint16 hi;
168 m : } server_version;
169 :
170 : // The number of bytes in the header. This is the size of this structure
171 : // plus the length of the blob.
172 m : uint32 header_size;
173 :
174 : // Nothing above this point in the header can change in order to maintain
175 : // the ability to parse the basic header with the version number. This by
176 : // itself doesn't guarantee backwards compatibility, but it does ensure that
177 : // we can detect trace files generated by older versions of the toolchain.
178 :
179 : // The block size used when writing the file to disk. The header and
180 : // all segments are padded and byte aligned to this block size.
181 m : uint32 block_size;
182 :
183 : // The id of the process being traced.
184 m : uint32 process_id;
185 :
186 : // The base address at which the executable module was loaded when the
187 : // trace file was created.
188 m : uint32 module_base_address;
189 :
190 : // The size of the executable module.
191 m : uint32 module_size;
192 :
193 : // The checksum of the executable module.
194 m : uint32 module_checksum;
195 :
196 : // The timestamp of the executable module.
197 m : uint32 module_time_date_stamp;
198 :
199 : // System information.
200 m : OSVERSIONINFOEX os_version_info;
201 m : SYSTEM_INFO system_info;
202 m : MEMORYSTATUSEX memory_status;
203 :
204 : // Clock information. This lets us convert from timestamps (both TSC and
205 : // ticks) to absolute system times. It also contains a timestamp for the
206 : // header itself.
207 m : trace::common::ClockInfo clock_info;
208 :
209 : // The header is required to store multiple variable length fields. We do
210 : // this via a blob mechanism. The header contains a single binary blob at the
211 : // end, whose length in bytes) is encoded via blob_length.
212 : //
213 : // Currently, the header stores the following variable length fields (in
214 : // the order indicated):
215 : //
216 : // 1. The path to the instrumented module, a NULL terminated wide string.
217 : // 2. The command line for the process, a NULL terminated wide string.
218 : // 3. The environment string for the process, an array of wide chars
219 : // terminated by a double NULL (individual environment variables are
220 : // separated by single NULLs).
221 :
222 : // This stores the variable length data, concatenated. This should be pointer
223 : // aligned so that PODs with alignment constraints embedded in the blob can be
224 : // read directly from a header loaded into memory.
225 m : uint8 blob_data[1];
226 m : };
227 m : COMPILE_ASSERT_IS_POD(TraceFileHeader);
228 :
229 : // Written at the beginning of a call trace file segment. Each call trace file
230 : // segment has a length, which on-disk is rounded up to the block_size, as
231 : // recorded in the TraceFileHeader. Within a call trace segment, there are one
232 : // or more records, each prefixed with a RecordPrefix, which describes the
233 : // length and type of the data to follow.
234 m : struct TraceFileSegmentHeader {
235 : // Type identifiers used for these headers.
236 m : enum { kTypeId = TRACE_PAGE_HEADER };
237 :
238 : // The identity of the thread that is reporting in this segment
239 : // of the trace file.
240 m : uint32 thread_id;
241 :
242 : // The number of data bytes in this segment of the trace file. This
243 : // value does not include the size of the record prefix nor the size
244 : // of the segment header.
245 m : uint32 segment_length;
246 m : };
247 m : COMPILE_ASSERT_IS_POD(TraceFileSegmentHeader);
248 :
249 : // The structure traced on function entry or exit.
250 m : template<int TypeId>
251 m : struct TraceEnterExitEventDataTempl {
252 m : enum { kTypeId = TypeId };
253 m : RetAddr retaddr;
254 m : FuncAddr function;
255 m : };
256 :
257 m : typedef TraceEnterExitEventDataTempl<TRACE_ENTER_EVENT> TraceEnterEventData;
258 m : typedef TraceEnterExitEventDataTempl<TRACE_EXIT_EVENT> TraceExitEventData;
259 m : typedef TraceEnterEventData TraceEnterExitEventData;
260 m : COMPILE_ASSERT_IS_POD(TraceEnterEventData);
261 m : COMPILE_ASSERT_IS_POD(TraceExitEventData);
262 m : COMPILE_ASSERT_IS_POD(TraceEnterExitEventData);
263 :
264 : // The structure written for each loaded module when module event tracing is
265 : // enabled.
266 m : struct TraceModuleData {
267 m : ModuleAddr module_base_addr;
268 m : size_t module_base_size;
269 m : uint32 module_checksum;
270 m : uint32 module_time_date_stamp;
271 m : wchar_t module_name[256];
272 m : wchar_t module_exe[MAX_PATH];
273 m : };
274 m : COMPILE_ASSERT_IS_POD(TraceModuleData);
275 :
276 : // This is for storing environment string information. Each environment string
277 : // consists of a pair of strings, the key and the value. Certain special
278 : // strings have empty keys.
279 m : typedef std::vector<std::pair<std::wstring, std::wstring>>
280 m : TraceEnvironmentStrings;
281 :
282 : // Describes the system information and environment in which a process is
283 : // running.
284 m : struct TraceSystemInfo {
285 m : OSVERSIONINFOEX os_version_info;
286 m : SYSTEM_INFO system_info;
287 m : MEMORYSTATUSEX memory_status;
288 m : trace::common::ClockInfo clock_info;
289 m : TraceEnvironmentStrings environment_strings;
290 m : };
291 :
292 : // The structure traced for batch entry traces.
293 m : struct TraceBatchEnterData {
294 m : enum { kTypeId = TRACE_BATCH_ENTER };
295 :
296 : // The thread ID from which these traces originate. This can differ
297 : // from the logging thread ID when a process exits, and the exiting
298 : // thread flushes the trace buffers from its expired brethren.
299 m : DWORD thread_id;
300 :
301 : // Number of function entries.
302 m : size_t num_calls;
303 :
304 : // Back-to-back entry events.
305 m : TraceEnterEventData calls[1];
306 m : };
307 m : COMPILE_ASSERT_IS_POD(TraceBatchEnterData);
308 :
309 m : enum InvocationInfoFlags {
310 : // If this bit is set in InvocationInfo flags, the caller is a dynamic
311 : // symbol id, and caller_offset is the offset of the return site, relative to
312 : // the start of the caller's symbol.
313 m : kCallerIsSymbol = 0x01,
314 : // If this bit is set in InvocationInfo flags, the function is a dynamic
315 : // symbol id, instead of an address.
316 m : kFunctionIsSymbol = 0x02,
317 m : };
318 :
319 : // This is the data recorded for each distinct caller/function
320 : // pair by the profiler.
321 m : struct InvocationInfo {
322 m : union {
323 m : RetAddr caller;
324 m : uint32 caller_symbol_id;
325 m : };
326 m : union {
327 m : FuncAddr function;
328 m : uint32 function_symbol_id;
329 m : };
330 m : size_t num_calls;
331 m : uint32 flags:8;
332 m : uint32 caller_offset:24;
333 m : uint64 cycles_min;
334 m : uint64 cycles_max;
335 m : uint64 cycles_sum;
336 m : };
337 m : COMPILE_ASSERT_IS_POD(InvocationInfo);
338 :
339 m : struct TraceBatchInvocationInfo {
340 m : enum { kTypeId = TRACE_BATCH_INVOCATION };
341 :
342 : // TODO(siggi): Perhaps the batch should carry the time resolution for
343 : // the invocation data?
344 :
345 : // Back to back entries, as many as our enclosing record's size allows for.
346 m : InvocationInfo invocations[1];
347 m : };
348 m : COMPILE_ASSERT_IS_POD(TraceBatchInvocationInfo);
349 :
350 m : struct TraceThreadNameInfo {
351 m : enum { kTypeId = TRACE_THREAD_NAME };
352 : // In fact as many as our enclosing record's size allows for,
353 : // zero terminated.
354 m : char thread_name[1];
355 m : };
356 m : COMPILE_ASSERT_IS_POD(TraceThreadNameInfo);
357 :
358 m : struct TraceIndexedFrequencyData {
359 m : enum { kTypeId = TRACE_INDEXED_FREQUENCY };
360 :
361 : // This is used to tie the data to a particular module, which has already
362 : // been reported via a TraceModuleData struct.
363 m : ModuleAddr module_base_addr;
364 m : size_t module_base_size;
365 m : uint32 module_checksum;
366 m : uint32 module_time_date_stamp;
367 :
368 : // The number of entries being reported. It is up to the instrumentation to
369 : // output any other metadata that is required to map an index to an address.
370 m : uint32 num_entries;
371 :
372 : // The number of columns for each record. Each column entry has the data sized
373 : // specified by |frequency_size|.
374 m : uint32 num_columns;
375 :
376 : // The type of data contained in this frequency record. This should be one of
377 : // the data-types defined in IndexedFrequencyData::DataType.
378 m : uint8 data_type;
379 :
380 : // The size of the frequency reports: 1, 2 or 4 bytes.
381 m : uint8 frequency_size;
382 :
383 : // In fact, there are frequency_size * num_basic_blocks bytes that follow.
384 m : uint8 frequency_data[1];
385 m : };
386 m : COMPILE_ASSERT_IS_POD(TraceIndexedFrequencyData);
387 :
388 m : struct TraceDynamicSymbol {
389 m : enum { kTypeId = TRACE_DYNAMIC_SYMBOL };
390 :
391 : // The symbol's ID, unique per process.
392 m : uint32 symbol_id;
393 : // In fact as many as our enclosing record's size allows for,
394 : // zero terminated.
395 m : char symbol_name[1];
396 m : };
397 m : COMPILE_ASSERT_IS_POD(TraceDynamicSymbol);
398 :
399 m : struct TraceSampleData {
400 m : enum { kTypeId = TRACE_SAMPLE_DATA };
401 :
402 : // This is used to tie the data to a particular module, which has already
403 : // been reported via a TraceModuleData struct.
404 m : ModuleAddr module_base_addr;
405 m : size_t module_size;
406 m : uint32 module_checksum;
407 m : uint32 module_time_date_stamp;
408 :
409 : // The size of each bucket in the sample data. This will be a power of 2 in
410 : // size.
411 m : uint32 bucket_size;
412 :
413 : // The beginning of the sampling buckets as an address in the image.
414 : // This will be aligned with the bucket size.
415 m : ModuleAddr bucket_start;
416 :
417 : // The number of buckets in the sample data.
418 m : uint32 bucket_count;
419 :
420 : // The time when the trace started and ended.
421 m : uint64 sampling_start_time;
422 m : uint64 sampling_end_time;
423 :
424 : // The sampling interval, expressed in clock cycles.
425 m : uint64 sampling_interval;
426 :
427 : // There are actually |bucket_count| buckets that follow.
428 m : uint32 buckets[1];
429 m : };
430 m : COMPILE_ASSERT_IS_POD(TraceSampleData);
431 :
432 : #endif // SYZYGY_TRACE_PROTOCOL_CALL_TRACE_DEFS_H_
|