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 // This header is designed to give you trace_event macros without specifying | 5 // This header defines the set of functions necessary for trace_event_internal.h |
6 // how the events actually get collected and stored. If you need to expose trace | 6 // to remain platform and context neutral. To use tracing in another context, |
7 // event to some other universe, you can copy-and-paste this file, | 7 // copy this header (and trace_event_internal.h) and implement the |
8 // implement the TRACE_EVENT_API macros, and do any other necessary fixup for | 8 // TRACE_EVENT_API_* macros below for the new environment. |
9 // the target platform. The end result is that multiple libraries can funnel | |
10 // events through to a shared trace event collector. | |
11 | |
12 // Trace events are for tracking application performance and resource usage. | |
13 // Macros are provided to track: | |
14 // Begin and end of function calls | |
15 // Counters | |
16 // | |
17 // Events are issued against categories. Whereas LOG's | |
18 // categories are statically defined, TRACE categories are created | |
19 // implicitly with a string. For example: | |
20 // TRACE_EVENT_INSTANT0("MY_SUBSYSTEM", "SomeImportantEvent") | |
21 // | |
22 // Events can be INSTANT, or can be pairs of BEGIN and END in the same scope: | |
23 // TRACE_EVENT_BEGIN0("MY_SUBSYSTEM", "SomethingCostly") | |
24 // doSomethingCostly() | |
25 // TRACE_EVENT_END0("MY_SUBSYSTEM", "SomethingCostly") | |
26 // Note: our tools can't always determine the correct BEGIN/END pairs unless | |
27 // these are used in the same scope. Use ASYNC_BEGIN/ASYNC_END macros if you | |
28 // need them to be in separate scopes. | |
29 // | |
30 // A common use case is to trace entire function scopes. This | |
31 // issues a trace BEGIN and END automatically: | |
32 // void doSomethingCostly() { | |
33 // TRACE_EVENT0("MY_SUBSYSTEM", "doSomethingCostly"); | |
34 // ... | |
35 // } | |
36 // | |
37 // Additional parameters can be associated with an event: | |
38 // void doSomethingCostly2(int howMuch) { | |
39 // TRACE_EVENT1("MY_SUBSYSTEM", "doSomethingCostly", | |
40 // "howMuch", howMuch); | |
41 // ... | |
42 // } | |
43 // | |
44 // The trace system will automatically add to this information the | |
45 // current process id, thread id, and a timestamp in microseconds. | |
46 // | |
47 // To trace an asynchronous procedure such as an IPC send/receive, use | |
48 // ASYNC_BEGIN and ASYNC_END: | |
49 // [single threaded sender code] | |
50 // static int send_count = 0; | |
51 // ++send_count; | |
52 // TRACE_EVENT_ASYNC_BEGIN0("ipc", "message", send_count); | |
53 // Send(new MyMessage(send_count)); | |
54 // [receive code] | |
55 // void OnMyMessage(send_count) { | |
56 // TRACE_EVENT_ASYNC_END0("ipc", "message", send_count); | |
57 // } | |
58 // The third parameter is a unique ID to match ASYNC_BEGIN/ASYNC_END pairs. | |
59 // ASYNC_BEGIN and ASYNC_END can occur on any thread of any traced process. | |
60 // Pointers can be used for the ID parameter, and they will be mangled | |
61 // internally so that the same pointer on two different processes will not | |
62 // match. For example: | |
63 // class MyTracedClass { | |
64 // public: | |
65 // MyTracedClass() { | |
66 // TRACE_EVENT_ASYNC_BEGIN0("category", "MyTracedClass", this); | |
67 // } | |
68 // ~MyTracedClass() { | |
69 // TRACE_EVENT_ASYNC_END0("category", "MyTracedClass", this); | |
70 // } | |
71 // } | |
72 // | |
73 // Trace event also supports counters, which is a way to track a quantity | |
74 // as it varies over time. Counters are created with the following macro: | |
75 // TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter", g_myCounterValue); | |
76 // | |
77 // Counters are process-specific. The macro itself can be issued from any | |
78 // thread, however. | |
79 // | |
80 // Sometimes, you want to track two counters at once. You can do this with two | |
81 // counter macros: | |
82 // TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter0", g_myCounterValue[0]); | |
83 // TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter1", g_myCounterValue[1]); | |
84 // Or you can do it with a combined macro: | |
85 // TRACE_COUNTER2("MY_SUBSYSTEM", "myCounter", | |
86 // "bytesPinned", g_myCounterValue[0], | |
87 // "bytesAllocated", g_myCounterValue[1]); | |
88 // This indicates to the tracing UI that these counters should be displayed | |
89 // in a single graph, as a summed area chart. | |
90 // | |
91 // Since counters are in a global namespace, you may want to disembiguate with a | |
92 // unique ID, by using the TRACE_COUNTER_ID* variations. | |
93 // | |
94 // By default, trace collection is compiled in, but turned off at runtime. | |
95 // Collecting trace data is the responsibility of the embedding | |
96 // application. In Chrome's case, navigating to about:tracing will turn on | |
97 // tracing and display data collected across all active processes. | |
98 // | |
99 // | |
100 // Memory scoping note: | |
101 // Tracing copies the pointers, not the string content, of the strings passed | |
102 // in for category, name, and arg_names. Thus, the following code will | |
103 // cause problems: | |
104 // char* str = strdup("impprtantName"); | |
105 // TRACE_EVENT_INSTANT0("SUBSYSTEM", str); // BAD! | |
106 // free(str); // Trace system now has dangling pointer | |
107 // | |
108 // To avoid this issue with the |name| and |arg_name| parameters, use the | |
109 // TRACE_EVENT_COPY_XXX overloads of the macros at additional runtime overhead. | |
110 // Notes: The category must always be in a long-lived char* (i.e. static const). | |
111 // The |arg_values|, when used, are always deep copied with the _COPY | |
112 // macros. | |
113 // | |
114 // When are string argument values copied: | |
115 // const char* arg_values are only referenced by default: | |
116 // TRACE_EVENT1("category", "name", | |
117 // "arg1", "literal string is only referenced"); | |
118 // Use TRACE_STR_COPY to force copying of a const char*: | |
119 // TRACE_EVENT1("category", "name", | |
120 // "arg1", TRACE_STR_COPY("string will be copied")); | |
121 // std::string arg_values are always copied: | |
122 // TRACE_EVENT1("category", "name", | |
123 // "arg1", std::string("string will be copied")); | |
124 // | |
125 // | |
126 // Thread Safety: | |
127 // A thread safe singleton and mutex are used for thread safety. Category | |
128 // enabled flags are used to limit the performance impact when the system | |
129 // is not enabled. | |
130 // | |
131 // TRACE_EVENT macros first cache a pointer to a category. The categories are | |
132 // statically allocated and safe at all times, even after exit. Fetching a | |
133 // category is protected by the TraceLog::lock_. Multiple threads initializing | |
134 // the static variable is safe, as they will be serialized by the lock and | |
135 // multiple calls will return the same pointer to the category. | |
136 // | |
137 // Then the category_enabled flag is checked. This is a unsigned char, and | |
138 // not intended to be multithread safe. It optimizes access to AddTraceEvent | |
139 // which is threadsafe internally via TraceLog::lock_. The enabled flag may | |
140 // cause some threads to incorrectly call or skip calling AddTraceEvent near | |
141 // the time of the system being enabled or disabled. This is acceptable as | |
142 // we tolerate some data loss while the system is being enabled/disabled and | |
143 // because AddTraceEvent is threadsafe internally and checks the enabled state | |
144 // again under lock. | |
145 // | |
146 // Without the use of these static category pointers and enabled flags all | |
147 // trace points would carry a significant performance cost of aquiring a lock | |
148 // and resolving the category. | |
149 | |
150 | 9 |
151 #ifndef BASE_DEBUG_TRACE_EVENT_H_ | 10 #ifndef BASE_DEBUG_TRACE_EVENT_H_ |
152 #define BASE_DEBUG_TRACE_EVENT_H_ | 11 #define BASE_DEBUG_TRACE_EVENT_H_ |
153 | 12 |
154 #include <string> | |
155 | |
156 #include "base/atomicops.h" | 13 #include "base/atomicops.h" |
157 #include "base/debug/trace_event_impl.h" | 14 #include "base/debug/trace_event_impl.h" |
158 #include "build/build_config.h" | 15 #include "build/build_config.h" |
159 | 16 |
160 // By default, const char* argument values are assumed to have long-lived scope | |
161 // and will not be copied. Use this macro to force a const char* to be copied. | |
162 #define TRACE_STR_COPY(str) \ | |
163 trace_event_internal::TraceStringWithCopy(str) | |
164 | |
165 // By default, uint64 ID argument values are not mangled with the Process ID in | |
166 // TRACE_EVENT_ASYNC macros. Use this macro to force Process ID mangling. | |
167 #define TRACE_ID_MANGLE(id) \ | |
168 trace_event_internal::TraceID::ForceMangle(id) | |
169 | |
170 // Records a pair of begin and end events called "name" for the current | |
171 // scope, with 0, 1 or 2 associated arguments. If the category is not | |
172 // enabled, then this does nothing. | |
173 // - category and name strings must have application lifetime (statics or | |
174 // literals). They may not include " chars. | |
175 #define TRACE_EVENT0(category, name) \ | |
176 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name) | |
177 #define TRACE_EVENT1(category, name, arg1_name, arg1_val) \ | |
178 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val) | |
179 #define TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, arg2_val) \ | |
180 INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, arg1_name, arg1_val, \ | |
181 arg2_name, arg2_val) | |
182 | |
183 // Same as TRACE_EVENT except that they are not included in official builds. | |
184 #ifdef OFFICIAL_BUILD | |
185 #define UNSHIPPED_TRACE_EVENT0(category, name) (void)0 | |
186 #define UNSHIPPED_TRACE_EVENT1(category, name, arg1_name, arg1_val) (void)0 | |
187 #define UNSHIPPED_TRACE_EVENT2(category, name, arg1_name, arg1_val, \ | |
188 arg2_name, arg2_val) (void)0 | |
189 #define UNSHIPPED_TRACE_EVENT_INSTANT0(category, name) (void)0 | |
190 #define UNSHIPPED_TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \ | |
191 (void)0 | |
192 #define UNSHIPPED_TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \ | |
193 arg2_name, arg2_val) (void)0 | |
194 #else | |
195 #define UNSHIPPED_TRACE_EVENT0(category, name) \ | |
196 TRACE_EVENT0(category, name) | |
197 #define UNSHIPPED_TRACE_EVENT1(category, name, arg1_name, arg1_val) \ | |
198 TRACE_EVENT1(category, name, arg1_name, arg1_val) | |
199 #define UNSHIPPED_TRACE_EVENT2(category, name, arg1_name, arg1_val, \ | |
200 arg2_name, arg2_val) \ | |
201 TRACE_EVENT2(category, name, arg1_name, arg1_val, arg2_name, arg2_val) | |
202 #define UNSHIPPED_TRACE_EVENT_INSTANT0(category, name) \ | |
203 TRACE_EVENT_INSTANT0(category, name) | |
204 #define UNSHIPPED_TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \ | |
205 TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) | |
206 #define UNSHIPPED_TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \ | |
207 arg2_name, arg2_val) \ | |
208 TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \ | |
209 arg2_name, arg2_val) | |
210 #endif | |
211 | |
212 // Records a single event called "name" immediately, with 0, 1 or 2 | |
213 // associated arguments. If the category is not enabled, then this | |
214 // does nothing. | |
215 // - category and name strings must have application lifetime (statics or | |
216 // literals). They may not include " chars. | |
217 #define TRACE_EVENT_INSTANT0(category, name) \ | |
218 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ | |
219 category, name, TRACE_EVENT_FLAG_NONE) | |
220 #define TRACE_EVENT_INSTANT1(category, name, arg1_name, arg1_val) \ | |
221 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ | |
222 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) | |
223 #define TRACE_EVENT_INSTANT2(category, name, arg1_name, arg1_val, \ | |
224 arg2_name, arg2_val) \ | |
225 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ | |
226 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \ | |
227 arg2_name, arg2_val) | |
228 #define TRACE_EVENT_COPY_INSTANT0(category, name) \ | |
229 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ | |
230 category, name, TRACE_EVENT_FLAG_COPY) | |
231 #define TRACE_EVENT_COPY_INSTANT1(category, name, arg1_name, arg1_val) \ | |
232 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ | |
233 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val) | |
234 #define TRACE_EVENT_COPY_INSTANT2(category, name, arg1_name, arg1_val, \ | |
235 arg2_name, arg2_val) \ | |
236 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \ | |
237 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \ | |
238 arg2_name, arg2_val) | |
239 | |
240 // Records a single BEGIN event called "name" immediately, with 0, 1 or 2 | |
241 // associated arguments. If the category is not enabled, then this | |
242 // does nothing. | |
243 // - category and name strings must have application lifetime (statics or | |
244 // literals). They may not include " chars. | |
245 #define TRACE_EVENT_BEGIN0(category, name) \ | |
246 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ | |
247 category, name, TRACE_EVENT_FLAG_NONE) | |
248 #define TRACE_EVENT_BEGIN1(category, name, arg1_name, arg1_val) \ | |
249 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ | |
250 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) | |
251 #define TRACE_EVENT_BEGIN2(category, name, arg1_name, arg1_val, \ | |
252 arg2_name, arg2_val) \ | |
253 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ | |
254 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \ | |
255 arg2_name, arg2_val) | |
256 #define TRACE_EVENT_COPY_BEGIN0(category, name) \ | |
257 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ | |
258 category, name, TRACE_EVENT_FLAG_COPY) | |
259 #define TRACE_EVENT_COPY_BEGIN1(category, name, arg1_name, arg1_val) \ | |
260 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ | |
261 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val) | |
262 #define TRACE_EVENT_COPY_BEGIN2(category, name, arg1_name, arg1_val, \ | |
263 arg2_name, arg2_val) \ | |
264 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \ | |
265 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \ | |
266 arg2_name, arg2_val) | |
267 | |
268 // Records a single END event for "name" immediately. If the category | |
269 // is not enabled, then this does nothing. | |
270 // - category and name strings must have application lifetime (statics or | |
271 // literals). They may not include " chars. | |
272 #define TRACE_EVENT_END0(category, name) \ | |
273 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | |
274 category, name, TRACE_EVENT_FLAG_NONE) | |
275 #define TRACE_EVENT_END1(category, name, arg1_name, arg1_val) \ | |
276 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | |
277 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) | |
278 #define TRACE_EVENT_END2(category, name, arg1_name, arg1_val, \ | |
279 arg2_name, arg2_val) \ | |
280 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | |
281 category, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \ | |
282 arg2_name, arg2_val) | |
283 #define TRACE_EVENT_COPY_END0(category, name) \ | |
284 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | |
285 category, name, TRACE_EVENT_FLAG_COPY) | |
286 #define TRACE_EVENT_COPY_END1(category, name, arg1_name, arg1_val) \ | |
287 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | |
288 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val) | |
289 #define TRACE_EVENT_COPY_END2(category, name, arg1_name, arg1_val, \ | |
290 arg2_name, arg2_val) \ | |
291 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \ | |
292 category, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \ | |
293 arg2_name, arg2_val) | |
294 | |
295 // Records the value of a counter called "name" immediately. Value | |
296 // must be representable as a 32 bit integer. | |
297 // - category and name strings must have application lifetime (statics or | |
298 // literals). They may not include " chars. | |
299 #define TRACE_COUNTER1(category, name, value) \ | |
300 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ | |
301 category, name, TRACE_EVENT_FLAG_NONE, \ | |
302 "value", static_cast<int>(value)) | |
303 #define TRACE_COPY_COUNTER1(category, name, value) \ | |
304 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ | |
305 category, name, TRACE_EVENT_FLAG_COPY, \ | |
306 "value", static_cast<int>(value)) | |
307 | |
308 // Records the values of a multi-parted counter called "name" immediately. | |
309 // The UI will treat value1 and value2 as parts of a whole, displaying their | |
310 // values as a stacked-bar chart. | |
311 // - category and name strings must have application lifetime (statics or | |
312 // literals). They may not include " chars. | |
313 #define TRACE_COUNTER2(category, name, value1_name, value1_val, \ | |
314 value2_name, value2_val) \ | |
315 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ | |
316 category, name, TRACE_EVENT_FLAG_NONE, \ | |
317 value1_name, static_cast<int>(value1_val), \ | |
318 value2_name, static_cast<int>(value2_val)) | |
319 #define TRACE_COPY_COUNTER2(category, name, value1_name, value1_val, \ | |
320 value2_name, value2_val) \ | |
321 INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \ | |
322 category, name, TRACE_EVENT_FLAG_COPY, \ | |
323 value1_name, static_cast<int>(value1_val), \ | |
324 value2_name, static_cast<int>(value2_val)) | |
325 | |
326 // Records the value of a counter called "name" immediately. Value | |
327 // must be representable as a 32 bit integer. | |
328 // - category and name strings must have application lifetime (statics or | |
329 // literals). They may not include " chars. | |
330 // - |id| is used to disambiguate counters with the same name. It must either | |
331 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits | |
332 // will be xored with a hash of the process ID so that the same pointer on | |
333 // two different processes will not collide. | |
334 #define TRACE_COUNTER_ID1(category, name, id, value) \ | |
335 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \ | |
336 category, name, id, TRACE_EVENT_FLAG_NONE, \ | |
337 "value", static_cast<int>(value)) | |
338 #define TRACE_COPY_COUNTER_ID1(category, name, id, value) \ | |
339 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \ | |
340 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
341 "value", static_cast<int>(value)) | |
342 | |
343 // Records the values of a multi-parted counter called "name" immediately. | |
344 // The UI will treat value1 and value2 as parts of a whole, displaying their | |
345 // values as a stacked-bar chart. | |
346 // - category and name strings must have application lifetime (statics or | |
347 // literals). They may not include " chars. | |
348 // - |id| is used to disambiguate counters with the same name. It must either | |
349 // be a pointer or an integer value up to 64 bits. If it's a pointer, the bits | |
350 // will be xored with a hash of the process ID so that the same pointer on | |
351 // two different processes will not collide. | |
352 #define TRACE_COUNTER_ID2(category, name, id, value1_name, value1_val, \ | |
353 value2_name, value2_val) \ | |
354 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \ | |
355 category, name, id, TRACE_EVENT_FLAG_NONE, \ | |
356 value1_name, static_cast<int>(value1_val), \ | |
357 value2_name, static_cast<int>(value2_val)) | |
358 #define TRACE_COPY_COUNTER_ID2(category, name, id, value1_name, value1_val, \ | |
359 value2_name, value2_val) \ | |
360 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \ | |
361 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
362 value1_name, static_cast<int>(value1_val), \ | |
363 value2_name, static_cast<int>(value2_val)) | |
364 | |
365 | |
366 // Records a single ASYNC_BEGIN event called "name" immediately, with 0, 1 or 2 | |
367 // associated arguments. If the category is not enabled, then this | |
368 // does nothing. | |
369 // - category and name strings must have application lifetime (statics or | |
370 // literals). They may not include " chars. | |
371 // - |id| is used to match the ASYNC_BEGIN event with the ASYNC_END event. ASYNC | |
372 // events are considered to match if their category, name and id values all | |
373 // match. |id| must either be a pointer or an integer value up to 64 bits. If | |
374 // it's a pointer, the bits will be xored with a hash of the process ID so | |
375 // that the same pointer on two different processes will not collide. | |
376 // An asynchronous operation can consist of multiple phases. The first phase is | |
377 // defined by the ASYNC_BEGIN calls. Additional phases can be defined using the | |
378 // ASYNC_STEP macros. When the operation completes, call ASYNC_END. | |
379 // An ASYNC trace typically occur on a single thread (if not, they will only be | |
380 // drawn on the thread defined in the ASYNC_BEGIN event), but all events in that | |
381 // operation must use the same |name| and |id|. Each event can have its own | |
382 // args. | |
383 #define TRACE_EVENT_ASYNC_BEGIN0(category, name, id) \ | |
384 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ | |
385 category, name, id, TRACE_EVENT_FLAG_NONE) | |
386 #define TRACE_EVENT_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val) \ | |
387 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ | |
388 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) | |
389 #define TRACE_EVENT_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, \ | |
390 arg2_name, arg2_val) \ | |
391 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ | |
392 category, name, id, TRACE_EVENT_FLAG_NONE, \ | |
393 arg1_name, arg1_val, arg2_name, arg2_val) | |
394 #define TRACE_EVENT_COPY_ASYNC_BEGIN0(category, name, id) \ | |
395 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ | |
396 category, name, id, TRACE_EVENT_FLAG_COPY) | |
397 #define TRACE_EVENT_COPY_ASYNC_BEGIN1(category, name, id, arg1_name, arg1_val) \ | |
398 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ | |
399 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
400 arg1_name, arg1_val) | |
401 #define TRACE_EVENT_COPY_ASYNC_BEGIN2(category, name, id, arg1_name, arg1_val, \ | |
402 arg2_name, arg2_val) \ | |
403 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \ | |
404 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
405 arg1_name, arg1_val, arg2_name, arg2_val) | |
406 | |
407 // Records a single ASYNC_STEP event for |step| immediately. If the category | |
408 // is not enabled, then this does nothing. The |name| and |id| must match the | |
409 // ASYNC_BEGIN event above. The |step| param identifies this step within the | |
410 // async event. This should be called at the beginning of the next phase of an | |
411 // asynchronous operation. | |
412 #define TRACE_EVENT_ASYNC_STEP0(category, name, id, step) \ | |
413 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \ | |
414 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step) | |
415 #define TRACE_EVENT_ASYNC_STEP1(category, name, id, step, \ | |
416 arg1_name, arg1_val) \ | |
417 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \ | |
418 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \ | |
419 arg1_name, arg1_val) | |
420 #define TRACE_EVENT_COPY_ASYNC_STEP0(category, name, id, step) \ | |
421 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \ | |
422 category, name, id, TRACE_EVENT_FLAG_COPY, "step", step) | |
423 #define TRACE_EVENT_COPY_ASYNC_STEP1(category, name, id, step, \ | |
424 arg1_name, arg1_val) \ | |
425 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \ | |
426 category, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \ | |
427 arg1_name, arg1_val) | |
428 | |
429 // Records a single ASYNC_END event for "name" immediately. If the category | |
430 // is not enabled, then this does nothing. | |
431 #define TRACE_EVENT_ASYNC_END0(category, name, id) \ | |
432 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ | |
433 category, name, id, TRACE_EVENT_FLAG_NONE) | |
434 #define TRACE_EVENT_ASYNC_END1(category, name, id, arg1_name, arg1_val) \ | |
435 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ | |
436 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) | |
437 #define TRACE_EVENT_ASYNC_END2(category, name, id, arg1_name, arg1_val, \ | |
438 arg2_name, arg2_val) \ | |
439 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ | |
440 category, name, id, TRACE_EVENT_FLAG_NONE, \ | |
441 arg1_name, arg1_val, arg2_name, arg2_val) | |
442 #define TRACE_EVENT_COPY_ASYNC_END0(category, name, id) \ | |
443 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ | |
444 category, name, id, TRACE_EVENT_FLAG_COPY) | |
445 #define TRACE_EVENT_COPY_ASYNC_END1(category, name, id, arg1_name, arg1_val) \ | |
446 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ | |
447 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
448 arg1_name, arg1_val) | |
449 #define TRACE_EVENT_COPY_ASYNC_END2(category, name, id, arg1_name, arg1_val, \ | |
450 arg2_name, arg2_val) \ | |
451 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \ | |
452 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
453 arg1_name, arg1_val, arg2_name, arg2_val) | |
454 | |
455 | |
456 // Records a single FLOW_BEGIN event called "name" immediately, with 0, 1 or 2 | |
457 // associated arguments. If the category is not enabled, then this | |
458 // does nothing. | |
459 // - category and name strings must have application lifetime (statics or | |
460 // literals). They may not include " chars. | |
461 // - |id| is used to match the FLOW_BEGIN event with the FLOW_END event. FLOW | |
462 // events are considered to match if their category, name and id values all | |
463 // match. |id| must either be a pointer or an integer value up to 64 bits. If | |
464 // it's a pointer, the bits will be xored with a hash of the process ID so | |
465 // that the same pointer on two different processes will not collide. | |
466 // FLOW events are different from ASYNC events in how they are drawn by the | |
467 // tracing UI. A FLOW defines asynchronous data flow, such as posting a task | |
468 // (FLOW_BEGIN) and later executing that task (FLOW_END). Expect FLOWs to be | |
469 // drawn as lines or arrows from FLOW_BEGIN scopes to FLOW_END scopes. Similar | |
470 // to ASYNC, a FLOW can consist of multiple phases. The first phase is defined | |
471 // by the FLOW_BEGIN calls. Additional phases can be defined using the FLOW_STEP | |
472 // macros. When the operation completes, call FLOW_END. An async operation can | |
473 // span threads and processes, but all events in that operation must use the | |
474 // same |name| and |id|. Each event can have its own args. | |
475 #define TRACE_EVENT_FLOW_BEGIN0(category, name, id) \ | |
476 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \ | |
477 category, name, id, TRACE_EVENT_FLAG_NONE) | |
478 #define TRACE_EVENT_FLOW_BEGIN1(category, name, id, arg1_name, arg1_val) \ | |
479 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \ | |
480 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) | |
481 #define TRACE_EVENT_FLOW_BEGIN2(category, name, id, arg1_name, arg1_val, \ | |
482 arg2_name, arg2_val) \ | |
483 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \ | |
484 category, name, id, TRACE_EVENT_FLAG_NONE, \ | |
485 arg1_name, arg1_val, arg2_name, arg2_val) | |
486 #define TRACE_EVENT_COPY_FLOW_BEGIN0(category, name, id) \ | |
487 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \ | |
488 category, name, id, TRACE_EVENT_FLAG_COPY) | |
489 #define TRACE_EVENT_COPY_FLOW_BEGIN1(category, name, id, arg1_name, arg1_val) \ | |
490 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \ | |
491 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
492 arg1_name, arg1_val) | |
493 #define TRACE_EVENT_COPY_FLOW_BEGIN2(category, name, id, arg1_name, arg1_val, \ | |
494 arg2_name, arg2_val) \ | |
495 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \ | |
496 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
497 arg1_name, arg1_val, arg2_name, arg2_val) | |
498 | |
499 // Records a single FLOW_STEP event for |step| immediately. If the category | |
500 // is not enabled, then this does nothing. The |name| and |id| must match the | |
501 // FLOW_BEGIN event above. The |step| param identifies this step within the | |
502 // async event. This should be called at the beginning of the next phase of an | |
503 // asynchronous operation. | |
504 #define TRACE_EVENT_FLOW_STEP0(category, name, id, step) \ | |
505 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \ | |
506 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step) | |
507 #define TRACE_EVENT_FLOW_STEP1(category, name, id, step, \ | |
508 arg1_name, arg1_val) \ | |
509 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \ | |
510 category, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \ | |
511 arg1_name, arg1_val) | |
512 #define TRACE_EVENT_COPY_FLOW_STEP0(category, name, id, step) \ | |
513 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \ | |
514 category, name, id, TRACE_EVENT_FLAG_COPY, "step", step) | |
515 #define TRACE_EVENT_COPY_FLOW_STEP1(category, name, id, step, \ | |
516 arg1_name, arg1_val) \ | |
517 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \ | |
518 category, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \ | |
519 arg1_name, arg1_val) | |
520 | |
521 // Records a single FLOW_END event for "name" immediately. If the category | |
522 // is not enabled, then this does nothing. | |
523 #define TRACE_EVENT_FLOW_END0(category, name, id) \ | |
524 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ | |
525 category, name, id, TRACE_EVENT_FLAG_NONE) | |
526 #define TRACE_EVENT_FLOW_END1(category, name, id, arg1_name, arg1_val) \ | |
527 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ | |
528 category, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val) | |
529 #define TRACE_EVENT_FLOW_END2(category, name, id, arg1_name, arg1_val, \ | |
530 arg2_name, arg2_val) \ | |
531 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ | |
532 category, name, id, TRACE_EVENT_FLAG_NONE, \ | |
533 arg1_name, arg1_val, arg2_name, arg2_val) | |
534 #define TRACE_EVENT_COPY_FLOW_END0(category, name, id) \ | |
535 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ | |
536 category, name, id, TRACE_EVENT_FLAG_COPY) | |
537 #define TRACE_EVENT_COPY_FLOW_END1(category, name, id, arg1_name, arg1_val) \ | |
538 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ | |
539 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
540 arg1_name, arg1_val) | |
541 #define TRACE_EVENT_COPY_FLOW_END2(category, name, id, arg1_name, arg1_val, \ | |
542 arg2_name, arg2_val) \ | |
543 INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \ | |
544 category, name, id, TRACE_EVENT_FLAG_COPY, \ | |
545 arg1_name, arg1_val, arg2_name, arg2_val) | |
546 | |
547 | |
548 //////////////////////////////////////////////////////////////////////////////// | 17 //////////////////////////////////////////////////////////////////////////////// |
549 // Implementation specific tracing API definitions. | 18 // Implementation specific tracing API definitions. |
550 | 19 |
551 // Get a pointer to the enabled state of the given trace category. Only | 20 // Get a pointer to the enabled state of the given trace category. Only |
552 // long-lived literal strings should be given as the category name. The returned | 21 // long-lived literal strings should be given as the category name. The returned |
553 // pointer can be held permanently in a local static for example. If the | 22 // pointer can be held permanently in a local static for example. If the |
554 // unsigned char is non-zero, tracing is enabled. If tracing is enabled, | 23 // unsigned char is non-zero, tracing is enabled. If tracing is enabled, |
555 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled | 24 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled |
556 // between the load of the tracing state and the call to | 25 // between the load of the tracing state and the call to |
557 // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out | 26 // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out |
(...skipping 10 matching lines...) Expand all Loading... |
568 // const char* name, | 37 // const char* name, |
569 // unsigned long long id, | 38 // unsigned long long id, |
570 // int num_args, | 39 // int num_args, |
571 // const char** arg_names, | 40 // const char** arg_names, |
572 // const unsigned char* arg_types, | 41 // const unsigned char* arg_types, |
573 // const unsigned long long* arg_values, | 42 // const unsigned long long* arg_values, |
574 // unsigned char flags) | 43 // unsigned char flags) |
575 #define TRACE_EVENT_API_ADD_TRACE_EVENT \ | 44 #define TRACE_EVENT_API_ADD_TRACE_EVENT \ |
576 base::debug::TraceLog::GetInstance()->AddTraceEvent | 45 base::debug::TraceLog::GetInstance()->AddTraceEvent |
577 | 46 |
| 47 // Defines atomic operations used internally by the tracing system. |
| 48 #define TRACE_EVENT_API_ATOMIC_WORD base::subtle::AtomicWord |
| 49 #define TRACE_EVENT_API_ATOMIC_LOAD(var) base::subtle::NoBarrier_Load(&(var)) |
| 50 #define TRACE_EVENT_API_ATOMIC_STORE(var, value) \ |
| 51 base::subtle::NoBarrier_Store(&(var), (value)) |
| 52 |
| 53 // Defines visibility for classes in trace_event_internal.h |
| 54 #define TRACE_EVENT_API_CLASS_EXPORT BASE_EXPORT |
| 55 |
578 //////////////////////////////////////////////////////////////////////////////// | 56 //////////////////////////////////////////////////////////////////////////////// |
579 | 57 |
580 // Implementation detail: trace event macros create temporary variables | 58 #include "base/debug/trace_event_internal.h" |
581 // to keep instrumentation overhead low. These macros give each temporary | |
582 // variable a unique name based on the line number to prevent name collissions. | |
583 #define INTERNAL_TRACE_EVENT_UID3(a,b) \ | |
584 trace_event_unique_##a##b | |
585 #define INTERNAL_TRACE_EVENT_UID2(a,b) \ | |
586 INTERNAL_TRACE_EVENT_UID3(a,b) | |
587 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \ | |
588 INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__) | |
589 | 59 |
590 // Implementation detail: internal macro to create static category. | 60 #endif /* BASE_DEBUG_TRACE_EVENT_H_ */ |
591 // No barriers are needed, because this code is designed to operate safely | |
592 // even when the unsigned char* points to garbage data (which may be the case | |
593 // on processors without cache coherency). | |
594 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category) \ | |
595 static base::subtle::AtomicWord INTERNAL_TRACE_EVENT_UID(atomic) = 0; \ | |
596 const uint8* INTERNAL_TRACE_EVENT_UID(catstatic) = \ | |
597 reinterpret_cast<const uint8*>( \ | |
598 base::subtle::NoBarrier_Load(&INTERNAL_TRACE_EVENT_UID(atomic))); \ | |
599 if (!INTERNAL_TRACE_EVENT_UID(catstatic)) { \ | |
600 INTERNAL_TRACE_EVENT_UID(catstatic) = \ | |
601 TRACE_EVENT_API_GET_CATEGORY_ENABLED(category); \ | |
602 base::subtle::NoBarrier_Store(&INTERNAL_TRACE_EVENT_UID(atomic), \ | |
603 reinterpret_cast<base::subtle::AtomicWord>( \ | |
604 INTERNAL_TRACE_EVENT_UID(catstatic))); \ | |
605 } | |
606 | |
607 // Implementation detail: internal macro to create static category and add | |
608 // event if the category is enabled. | |
609 #define INTERNAL_TRACE_EVENT_ADD(phase, category, name, flags, ...) \ | |
610 do { \ | |
611 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | |
612 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ | |
613 trace_event_internal::AddTraceEvent( \ | |
614 phase, INTERNAL_TRACE_EVENT_UID(catstatic), name, \ | |
615 trace_event_internal::kNoEventId, flags, ##__VA_ARGS__); \ | |
616 } \ | |
617 } while (0) | |
618 | |
619 // Implementation detail: internal macro to create static category and add begin | |
620 // event if the category is enabled. Also adds the end event when the scope | |
621 // ends. | |
622 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category, name, ...) \ | |
623 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | |
624 trace_event_internal::TraceEndOnScopeClose \ | |
625 INTERNAL_TRACE_EVENT_UID(profileScope); \ | |
626 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ | |
627 trace_event_internal::AddTraceEvent( \ | |
628 TRACE_EVENT_PHASE_BEGIN, \ | |
629 INTERNAL_TRACE_EVENT_UID(catstatic), \ | |
630 name, trace_event_internal::kNoEventId, \ | |
631 TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__); \ | |
632 INTERNAL_TRACE_EVENT_UID(profileScope).Initialize( \ | |
633 INTERNAL_TRACE_EVENT_UID(catstatic), name); \ | |
634 } | |
635 | |
636 // Implementation detail: internal macro to create static category and add | |
637 // event if the category is enabled. | |
638 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category, name, id, flags, \ | |
639 ...) \ | |
640 do { \ | |
641 INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category); \ | |
642 if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \ | |
643 unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \ | |
644 trace_event_internal::TraceID trace_event_trace_id( \ | |
645 id, &trace_event_flags); \ | |
646 trace_event_internal::AddTraceEvent( \ | |
647 phase, INTERNAL_TRACE_EVENT_UID(catstatic), \ | |
648 name, trace_event_trace_id.data(), trace_event_flags, \ | |
649 ##__VA_ARGS__); \ | |
650 } \ | |
651 } while (0) | |
652 | |
653 // Notes regarding the following definitions: | |
654 // New values can be added and propagated to third party libraries, but existing | |
655 // definitions must never be changed, because third party libraries may use old | |
656 // definitions. | |
657 | |
658 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair. | |
659 #define TRACE_EVENT_PHASE_BEGIN ('B') | |
660 #define TRACE_EVENT_PHASE_END ('E') | |
661 #define TRACE_EVENT_PHASE_INSTANT ('I') | |
662 #define TRACE_EVENT_PHASE_ASYNC_BEGIN ('S') | |
663 #define TRACE_EVENT_PHASE_ASYNC_STEP ('T') | |
664 #define TRACE_EVENT_PHASE_ASYNC_END ('F') | |
665 #define TRACE_EVENT_PHASE_FLOW_BEGIN ('s') | |
666 #define TRACE_EVENT_PHASE_FLOW_STEP ('t') | |
667 #define TRACE_EVENT_PHASE_FLOW_END ('f') | |
668 #define TRACE_EVENT_PHASE_METADATA ('M') | |
669 #define TRACE_EVENT_PHASE_COUNTER ('C') | |
670 | |
671 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT. | |
672 #define TRACE_EVENT_FLAG_NONE (static_cast<unsigned char>(0)) | |
673 #define TRACE_EVENT_FLAG_COPY (static_cast<unsigned char>(1 << 0)) | |
674 #define TRACE_EVENT_FLAG_HAS_ID (static_cast<unsigned char>(1 << 1)) | |
675 #define TRACE_EVENT_FLAG_MANGLE_ID (static_cast<unsigned char>(1 << 2)) | |
676 | |
677 // Type values for identifying types in the TraceValue union. | |
678 #define TRACE_VALUE_TYPE_BOOL (static_cast<unsigned char>(1)) | |
679 #define TRACE_VALUE_TYPE_UINT (static_cast<unsigned char>(2)) | |
680 #define TRACE_VALUE_TYPE_INT (static_cast<unsigned char>(3)) | |
681 #define TRACE_VALUE_TYPE_DOUBLE (static_cast<unsigned char>(4)) | |
682 #define TRACE_VALUE_TYPE_POINTER (static_cast<unsigned char>(5)) | |
683 #define TRACE_VALUE_TYPE_STRING (static_cast<unsigned char>(6)) | |
684 #define TRACE_VALUE_TYPE_COPY_STRING (static_cast<unsigned char>(7)) | |
685 | |
686 namespace trace_event_internal { | |
687 | |
688 // Specify these values when the corresponding argument of AddTraceEvent is not | |
689 // used. | |
690 const int kZeroNumArgs = 0; | |
691 const unsigned long long kNoEventId = 0; | |
692 | |
693 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers | |
694 // are mangled with the Process ID so that they are unlikely to collide when the | |
695 // same pointer is used on different processes. | |
696 class TraceID { | |
697 public: | |
698 class ForceMangle { | |
699 public: | |
700 explicit ForceMangle(unsigned long long id) : data_(id) {} | |
701 explicit ForceMangle(unsigned long id) : data_(id) {} | |
702 explicit ForceMangle(unsigned int id) : data_(id) {} | |
703 explicit ForceMangle(unsigned short id) : data_(id) {} | |
704 explicit ForceMangle(unsigned char id) : data_(id) {} | |
705 explicit ForceMangle(long long id) | |
706 : data_(static_cast<unsigned long long>(id)) {} | |
707 explicit ForceMangle(long id) | |
708 : data_(static_cast<unsigned long long>(id)) {} | |
709 explicit ForceMangle(int id) | |
710 : data_(static_cast<unsigned long long>(id)) {} | |
711 explicit ForceMangle(short id) | |
712 : data_(static_cast<unsigned long long>(id)) {} | |
713 explicit ForceMangle(signed char id) | |
714 : data_(static_cast<unsigned long long>(id)) {} | |
715 | |
716 unsigned long long data() const { return data_; } | |
717 | |
718 private: | |
719 unsigned long long data_; | |
720 }; | |
721 | |
722 TraceID(const void* id, unsigned char* flags) | |
723 : data_(static_cast<unsigned long long>( | |
724 reinterpret_cast<unsigned long>(id))) { | |
725 *flags |= TRACE_EVENT_FLAG_MANGLE_ID; | |
726 } | |
727 TraceID(ForceMangle id, unsigned char* flags) : data_(id.data()) { | |
728 *flags |= TRACE_EVENT_FLAG_MANGLE_ID; | |
729 } | |
730 TraceID(unsigned long long id, unsigned char* flags) | |
731 : data_(id) { (void)flags; } | |
732 TraceID(unsigned long id, unsigned char* flags) | |
733 : data_(id) { (void)flags; } | |
734 TraceID(unsigned int id, unsigned char* flags) | |
735 : data_(id) { (void)flags; } | |
736 TraceID(unsigned short id, unsigned char* flags) | |
737 : data_(id) { (void)flags; } | |
738 TraceID(unsigned char id, unsigned char* flags) | |
739 : data_(id) { (void)flags; } | |
740 TraceID(long long id, unsigned char* flags) | |
741 : data_(static_cast<unsigned long long>(id)) { (void)flags; } | |
742 TraceID(long id, unsigned char* flags) | |
743 : data_(static_cast<unsigned long long>(id)) { (void)flags; } | |
744 TraceID(int id, unsigned char* flags) | |
745 : data_(static_cast<unsigned long long>(id)) { (void)flags; } | |
746 TraceID(short id, unsigned char* flags) | |
747 : data_(static_cast<unsigned long long>(id)) { (void)flags; } | |
748 TraceID(signed char id, unsigned char* flags) | |
749 : data_(static_cast<unsigned long long>(id)) { (void)flags; } | |
750 | |
751 unsigned long long data() const { return data_; } | |
752 | |
753 private: | |
754 unsigned long long data_; | |
755 }; | |
756 | |
757 // Simple union to store various types as unsigned long long. | |
758 union TraceValueUnion { | |
759 bool as_bool; | |
760 unsigned long long as_uint; | |
761 long long as_int; | |
762 double as_double; | |
763 const void* as_pointer; | |
764 const char* as_string; | |
765 }; | |
766 | |
767 // Simple container for const char* that should be copied instead of retained. | |
768 class TraceStringWithCopy { | |
769 public: | |
770 explicit TraceStringWithCopy(const char* str) : str_(str) {} | |
771 operator const char* () const { return str_; } | |
772 private: | |
773 const char* str_; | |
774 }; | |
775 | |
776 // Define SetTraceValue for each allowed type. It stores the type and | |
777 // value in the return arguments. This allows this API to avoid declaring any | |
778 // structures so that it is portable to third_party libraries. | |
779 #define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, \ | |
780 union_member, \ | |
781 value_type_id) \ | |
782 static inline void SetTraceValue(actual_type arg, \ | |
783 unsigned char* type, \ | |
784 unsigned long long* value) { \ | |
785 TraceValueUnion type_value; \ | |
786 type_value.union_member = arg; \ | |
787 *type = value_type_id; \ | |
788 *value = type_value.as_uint; \ | |
789 } | |
790 // Simpler form for int types that can be safely casted. | |
791 #define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actual_type, \ | |
792 value_type_id) \ | |
793 static inline void SetTraceValue(actual_type arg, \ | |
794 unsigned char* type, \ | |
795 unsigned long long* value) { \ | |
796 *type = value_type_id; \ | |
797 *value = static_cast<unsigned long long>(arg); \ | |
798 } | |
799 | |
800 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long, TRACE_VALUE_TYPE_UINT) | |
801 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long, TRACE_VALUE_TYPE_UINT) | |
802 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned int, TRACE_VALUE_TYPE_UINT) | |
803 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT) | |
804 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT) | |
805 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT) | |
806 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long, TRACE_VALUE_TYPE_INT) | |
807 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT) | |
808 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT) | |
809 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT) | |
810 INTERNAL_DECLARE_SET_TRACE_VALUE(bool, as_bool, TRACE_VALUE_TYPE_BOOL) | |
811 INTERNAL_DECLARE_SET_TRACE_VALUE(double, as_double, TRACE_VALUE_TYPE_DOUBLE) | |
812 INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, as_pointer, | |
813 TRACE_VALUE_TYPE_POINTER) | |
814 INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, as_string, | |
815 TRACE_VALUE_TYPE_STRING) | |
816 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy&, as_string, | |
817 TRACE_VALUE_TYPE_COPY_STRING) | |
818 | |
819 #undef INTERNAL_DECLARE_SET_TRACE_VALUE | |
820 #undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT | |
821 | |
822 // std::string version of SetTraceValue so that trace arguments can be strings. | |
823 static inline void SetTraceValue(const std::string& arg, | |
824 unsigned char* type, | |
825 unsigned long long* value) { | |
826 TraceValueUnion type_value; | |
827 type_value.as_string = arg.c_str(); | |
828 *type = TRACE_VALUE_TYPE_COPY_STRING; | |
829 *value = type_value.as_uint; | |
830 } | |
831 | |
832 // These AddTraceEvent template functions are defined here instead of in the | |
833 // macro, because the arg_values could be temporary objects, such as | |
834 // std::string. In order to store pointers to the internal c_str and pass | |
835 // through to the tracing API, the arg_values must live throughout | |
836 // these procedures. | |
837 | |
838 static inline void AddTraceEvent(char phase, | |
839 const unsigned char* category_enabled, | |
840 const char* name, | |
841 unsigned long long id, | |
842 unsigned char flags) { | |
843 TRACE_EVENT_API_ADD_TRACE_EVENT( | |
844 phase, category_enabled, name, id, | |
845 kZeroNumArgs, NULL, NULL, NULL, | |
846 flags); | |
847 } | |
848 | |
849 template<class ARG1_TYPE> | |
850 static inline void AddTraceEvent(char phase, | |
851 const unsigned char* category_enabled, | |
852 const char* name, | |
853 unsigned long long id, | |
854 unsigned char flags, | |
855 const char* arg1_name, | |
856 const ARG1_TYPE& arg1_val) { | |
857 const int num_args = 1; | |
858 unsigned char arg_types[1]; | |
859 unsigned long long arg_values[1]; | |
860 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]); | |
861 TRACE_EVENT_API_ADD_TRACE_EVENT( | |
862 phase, category_enabled, name, id, | |
863 num_args, &arg1_name, arg_types, arg_values, | |
864 flags); | |
865 } | |
866 | |
867 template<class ARG1_TYPE, class ARG2_TYPE> | |
868 static inline void AddTraceEvent(char phase, | |
869 const unsigned char* category_enabled, | |
870 const char* name, | |
871 unsigned long long id, | |
872 unsigned char flags, | |
873 const char* arg1_name, | |
874 const ARG1_TYPE& arg1_val, | |
875 const char* arg2_name, | |
876 const ARG2_TYPE& arg2_val) { | |
877 const int num_args = 2; | |
878 const char* arg_names[2] = { arg1_name, arg2_name }; | |
879 unsigned char arg_types[2]; | |
880 unsigned long long arg_values[2]; | |
881 SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]); | |
882 SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]); | |
883 TRACE_EVENT_API_ADD_TRACE_EVENT( | |
884 phase, category_enabled, name, id, | |
885 num_args, arg_names, arg_types, arg_values, | |
886 flags); | |
887 } | |
888 | |
889 // Used by TRACE_EVENTx macro. Do not use directly. | |
890 class BASE_EXPORT TraceEndOnScopeClose { | |
891 public: | |
892 // Note: members of data_ intentionally left uninitialized. See Initialize. | |
893 TraceEndOnScopeClose() : p_data_(NULL) {} | |
894 ~TraceEndOnScopeClose() { | |
895 if (p_data_) | |
896 AddEventIfEnabled(); | |
897 } | |
898 | |
899 void Initialize(const unsigned char* category_enabled, | |
900 const char* name); | |
901 | |
902 private: | |
903 // Add the end event if the category is still enabled. | |
904 void AddEventIfEnabled(); | |
905 | |
906 // This Data struct workaround is to avoid initializing all the members | |
907 // in Data during construction of this object, since this object is always | |
908 // constructed, even when tracing is disabled. If the members of Data were | |
909 // members of this class instead, compiler warnings occur about potential | |
910 // uninitialized accesses. | |
911 struct Data { | |
912 const unsigned char* category_enabled; | |
913 const char* name; | |
914 }; | |
915 Data* p_data_; | |
916 Data data_; | |
917 }; | |
918 | |
919 | |
920 } // namespace trace_event_internal | |
921 | |
922 #endif // BASE_DEBUG_TRACE_EVENT_H_ | |
OLD | NEW |