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