Home | History | Annotate | Download | only in debug
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // This header file defines the set of trace_event macros without specifying
      6 // how the events actually get collected and stored. If you need to expose trace
      7 // events to some other universe, you can copy-and-paste this file as well as
      8 // trace_event.h, modifying the macros contained there as necessary for the
      9 // target platform. The end result is that multiple libraries can funnel events
     10 // 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 //                        TRACE_EVENT_SCOPE_THREAD)
     22 //
     23 // It is often the case that one trace may belong in multiple categories at the
     24 // same time. The first argument to the trace can be a comma-separated list of
     25 // categories, forming a category group, like:
     26 //
     27 // TRACE_EVENT_INSTANT0("input,views", "OnMouseOver", TRACE_EVENT_SCOPE_THREAD)
     28 //
     29 // We can enable/disable tracing of OnMouseOver by enabling/disabling either
     30 // category.
     31 //
     32 // Events can be INSTANT, or can be pairs of BEGIN and END in the same scope:
     33 //   TRACE_EVENT_BEGIN0("MY_SUBSYSTEM", "SomethingCostly")
     34 //   doSomethingCostly()
     35 //   TRACE_EVENT_END0("MY_SUBSYSTEM", "SomethingCostly")
     36 // Note: our tools can't always determine the correct BEGIN/END pairs unless
     37 // these are used in the same scope. Use ASYNC_BEGIN/ASYNC_END macros if you
     38 // need them to be in separate scopes.
     39 //
     40 // A common use case is to trace entire function scopes. This
     41 // issues a trace BEGIN and END automatically:
     42 //   void doSomethingCostly() {
     43 //     TRACE_EVENT0("MY_SUBSYSTEM", "doSomethingCostly");
     44 //     ...
     45 //   }
     46 //
     47 // Additional parameters can be associated with an event:
     48 //   void doSomethingCostly2(int howMuch) {
     49 //     TRACE_EVENT1("MY_SUBSYSTEM", "doSomethingCostly",
     50 //         "howMuch", howMuch);
     51 //     ...
     52 //   }
     53 //
     54 // The trace system will automatically add to this information the
     55 // current process id, thread id, and a timestamp in microseconds.
     56 //
     57 // To trace an asynchronous procedure such as an IPC send/receive, use
     58 // ASYNC_BEGIN and ASYNC_END:
     59 //   [single threaded sender code]
     60 //     static int send_count = 0;
     61 //     ++send_count;
     62 //     TRACE_EVENT_ASYNC_BEGIN0("ipc", "message", send_count);
     63 //     Send(new MyMessage(send_count));
     64 //   [receive code]
     65 //     void OnMyMessage(send_count) {
     66 //       TRACE_EVENT_ASYNC_END0("ipc", "message", send_count);
     67 //     }
     68 // The third parameter is a unique ID to match ASYNC_BEGIN/ASYNC_END pairs.
     69 // ASYNC_BEGIN and ASYNC_END can occur on any thread of any traced process.
     70 // Pointers can be used for the ID parameter, and they will be mangled
     71 // internally so that the same pointer on two different processes will not
     72 // match. For example:
     73 //   class MyTracedClass {
     74 //    public:
     75 //     MyTracedClass() {
     76 //       TRACE_EVENT_ASYNC_BEGIN0("category", "MyTracedClass", this);
     77 //     }
     78 //     ~MyTracedClass() {
     79 //       TRACE_EVENT_ASYNC_END0("category", "MyTracedClass", this);
     80 //     }
     81 //   }
     82 //
     83 // Trace event also supports counters, which is a way to track a quantity
     84 // as it varies over time. Counters are created with the following macro:
     85 //   TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter", g_myCounterValue);
     86 //
     87 // Counters are process-specific. The macro itself can be issued from any
     88 // thread, however.
     89 //
     90 // Sometimes, you want to track two counters at once. You can do this with two
     91 // counter macros:
     92 //   TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter0", g_myCounterValue[0]);
     93 //   TRACE_COUNTER1("MY_SUBSYSTEM", "myCounter1", g_myCounterValue[1]);
     94 // Or you can do it with a combined macro:
     95 //   TRACE_COUNTER2("MY_SUBSYSTEM", "myCounter",
     96 //       "bytesPinned", g_myCounterValue[0],
     97 //       "bytesAllocated", g_myCounterValue[1]);
     98 // This indicates to the tracing UI that these counters should be displayed
     99 // in a single graph, as a summed area chart.
    100 //
    101 // Since counters are in a global namespace, you may want to disambiguate with a
    102 // unique ID, by using the TRACE_COUNTER_ID* variations.
    103 //
    104 // By default, trace collection is compiled in, but turned off at runtime.
    105 // Collecting trace data is the responsibility of the embedding
    106 // application. In Chrome's case, navigating to about:tracing will turn on
    107 // tracing and display data collected across all active processes.
    108 //
    109 //
    110 // Memory scoping note:
    111 // Tracing copies the pointers, not the string content, of the strings passed
    112 // in for category_group, name, and arg_names.  Thus, the following code will
    113 // cause problems:
    114 //     char* str = strdup("importantName");
    115 //     TRACE_EVENT_INSTANT0("SUBSYSTEM", str);  // BAD!
    116 //     free(str);                   // Trace system now has dangling pointer
    117 //
    118 // To avoid this issue with the |name| and |arg_name| parameters, use the
    119 // TRACE_EVENT_COPY_XXX overloads of the macros at additional runtime overhead.
    120 // Notes: The category must always be in a long-lived char* (i.e. static const).
    121 //        The |arg_values|, when used, are always deep copied with the _COPY
    122 //        macros.
    123 //
    124 // When are string argument values copied:
    125 // const char* arg_values are only referenced by default:
    126 //     TRACE_EVENT1("category", "name",
    127 //                  "arg1", "literal string is only referenced");
    128 // Use TRACE_STR_COPY to force copying of a const char*:
    129 //     TRACE_EVENT1("category", "name",
    130 //                  "arg1", TRACE_STR_COPY("string will be copied"));
    131 // std::string arg_values are always copied:
    132 //     TRACE_EVENT1("category", "name",
    133 //                  "arg1", std::string("string will be copied"));
    134 //
    135 //
    136 // Convertible notes:
    137 // Converting a large data type to a string can be costly. To help with this,
    138 // the trace framework provides an interface ConvertableToTraceFormat. If you
    139 // inherit from it and implement the AppendAsTraceFormat method the trace
    140 // framework will call back to your object to convert a trace output time. This
    141 // means, if the category for the event is disabled, the conversion will not
    142 // happen.
    143 //
    144 //   class MyData : public base::debug::ConvertableToTraceFormat {
    145 //    public:
    146 //     MyData() {}
    147 //     virtual ~MyData() {}
    148 //     virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE {
    149 //       out->append("{\"foo\":1}");
    150 //     }
    151 //    private:
    152 //     DISALLOW_COPY_AND_ASSIGN(MyData);
    153 //   };
    154 //
    155 //   scoped_ptr<MyData> data(new MyData());
    156 //   TRACE_EVENT1("foo", "bar", "data",
    157 //                data.PassAs<base::debug::ConvertableToTraceFormat>());
    158 //
    159 // The trace framework will take ownership if the passed pointer and it will
    160 // be free'd when the trace buffer is flushed.
    161 //
    162 // Note, we only do the conversion when the buffer is flushed, so the provided
    163 // data object should not be modified after it's passed to the trace framework.
    164 //
    165 //
    166 // Thread Safety:
    167 // A thread safe singleton and mutex are used for thread safety. Category
    168 // enabled flags are used to limit the performance impact when the system
    169 // is not enabled.
    170 //
    171 // TRACE_EVENT macros first cache a pointer to a category. The categories are
    172 // statically allocated and safe at all times, even after exit. Fetching a
    173 // category is protected by the TraceLog::lock_. Multiple threads initializing
    174 // the static variable is safe, as they will be serialized by the lock and
    175 // multiple calls will return the same pointer to the category.
    176 //
    177 // Then the category_group_enabled flag is checked. This is a unsigned char, and
    178 // not intended to be multithread safe. It optimizes access to AddTraceEvent
    179 // which is threadsafe internally via TraceLog::lock_. The enabled flag may
    180 // cause some threads to incorrectly call or skip calling AddTraceEvent near
    181 // the time of the system being enabled or disabled. This is acceptable as
    182 // we tolerate some data loss while the system is being enabled/disabled and
    183 // because AddTraceEvent is threadsafe internally and checks the enabled state
    184 // again under lock.
    185 //
    186 // Without the use of these static category pointers and enabled flags all
    187 // trace points would carry a significant performance cost of acquiring a lock
    188 // and resolving the category.
    189 
    190 #ifndef BASE_DEBUG_TRACE_EVENT_H_
    191 #define BASE_DEBUG_TRACE_EVENT_H_
    192 
    193 #include <string>
    194 
    195 #include "base/atomicops.h"
    196 #include "base/debug/trace_event_impl.h"
    197 #include "base/debug/trace_event_memory.h"
    198 #include "build/build_config.h"
    199 
    200 // By default, const char* argument values are assumed to have long-lived scope
    201 // and will not be copied. Use this macro to force a const char* to be copied.
    202 #define TRACE_STR_COPY(str) \
    203     trace_event_internal::TraceStringWithCopy(str)
    204 
    205 // This will mark the trace event as disabled by default. The user will need
    206 // to explicitly enable the event.
    207 #define TRACE_DISABLED_BY_DEFAULT(name) "disabled-by-default-" name
    208 
    209 // By default, uint64 ID argument values are not mangled with the Process ID in
    210 // TRACE_EVENT_ASYNC macros. Use this macro to force Process ID mangling.
    211 #define TRACE_ID_MANGLE(id) \
    212     trace_event_internal::TraceID::ForceMangle(id)
    213 
    214 // By default, pointers are mangled with the Process ID in TRACE_EVENT_ASYNC
    215 // macros. Use this macro to prevent Process ID mangling.
    216 #define TRACE_ID_DONT_MANGLE(id) \
    217     trace_event_internal::TraceID::DontMangle(id)
    218 
    219 // Records a pair of begin and end events called "name" for the current
    220 // scope, with 0, 1 or 2 associated arguments. If the category is not
    221 // enabled, then this does nothing.
    222 // - category and name strings must have application lifetime (statics or
    223 //   literals). They may not include " chars.
    224 #define TRACE_EVENT0(category_group, name) \
    225     INTERNAL_TRACE_MEMORY(category_group, name) \
    226     INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name)
    227 #define TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \
    228     INTERNAL_TRACE_MEMORY(category_group, name) \
    229     INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, arg1_name, arg1_val)
    230 #define TRACE_EVENT2( \
    231     category_group, name, arg1_name, arg1_val, arg2_name, arg2_val) \
    232   INTERNAL_TRACE_MEMORY(category_group, name) \
    233   INTERNAL_TRACE_EVENT_ADD_SCOPED( \
    234       category_group, name, arg1_name, arg1_val, arg2_name, arg2_val)
    235 
    236 // UNSHIPPED_TRACE_EVENT* are like TRACE_EVENT* except that they are not
    237 // included in official builds.
    238 
    239 #if OFFICIAL_BUILD
    240 #undef TRACING_IS_OFFICIAL_BUILD
    241 #define TRACING_IS_OFFICIAL_BUILD 1
    242 #elif !defined(TRACING_IS_OFFICIAL_BUILD)
    243 #define TRACING_IS_OFFICIAL_BUILD 0
    244 #endif
    245 
    246 #if TRACING_IS_OFFICIAL_BUILD
    247 #define UNSHIPPED_TRACE_EVENT0(category_group, name) (void)0
    248 #define UNSHIPPED_TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \
    249     (void)0
    250 #define UNSHIPPED_TRACE_EVENT2(category_group, name, arg1_name, arg1_val, \
    251                                arg2_name, arg2_val) (void)0
    252 #define UNSHIPPED_TRACE_EVENT_INSTANT0(category_group, name, scope) (void)0
    253 #define UNSHIPPED_TRACE_EVENT_INSTANT1(category_group, name, scope, \
    254                                        arg1_name, arg1_val) (void)0
    255 #define UNSHIPPED_TRACE_EVENT_INSTANT2(category_group, name, scope, \
    256                                        arg1_name, arg1_val, \
    257                                        arg2_name, arg2_val) (void)0
    258 #else
    259 #define UNSHIPPED_TRACE_EVENT0(category_group, name) \
    260     TRACE_EVENT0(category_group, name)
    261 #define UNSHIPPED_TRACE_EVENT1(category_group, name, arg1_name, arg1_val) \
    262     TRACE_EVENT1(category_group, name, arg1_name, arg1_val)
    263 #define UNSHIPPED_TRACE_EVENT2(category_group, name, arg1_name, arg1_val, \
    264                                arg2_name, arg2_val) \
    265     TRACE_EVENT2(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val)
    266 #define UNSHIPPED_TRACE_EVENT_INSTANT0(category_group, name, scope) \
    267     TRACE_EVENT_INSTANT0(category_group, name, scope)
    268 #define UNSHIPPED_TRACE_EVENT_INSTANT1(category_group, name, scope, \
    269                                        arg1_name, arg1_val) \
    270     TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, arg1_val)
    271 #define UNSHIPPED_TRACE_EVENT_INSTANT2(category_group, name, scope, \
    272                                        arg1_name, arg1_val, \
    273                                        arg2_name, arg2_val) \
    274     TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, arg1_val, \
    275                          arg2_name, arg2_val)
    276 #endif
    277 
    278 // Records a single event called "name" immediately, with 0, 1 or 2
    279 // associated arguments. If the category is not enabled, then this
    280 // does nothing.
    281 // - category and name strings must have application lifetime (statics or
    282 //   literals). They may not include " chars.
    283 #define TRACE_EVENT_INSTANT0(category_group, name, scope) \
    284     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
    285         category_group, name, TRACE_EVENT_FLAG_NONE | scope)
    286 #define TRACE_EVENT_INSTANT1(category_group, name, scope, arg1_name, arg1_val) \
    287     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
    288         category_group, name, TRACE_EVENT_FLAG_NONE | scope, \
    289         arg1_name, arg1_val)
    290 #define TRACE_EVENT_INSTANT2(category_group, name, scope, arg1_name, arg1_val, \
    291                              arg2_name, arg2_val) \
    292     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
    293         category_group, name, TRACE_EVENT_FLAG_NONE | scope, \
    294         arg1_name, arg1_val, arg2_name, arg2_val)
    295 #define TRACE_EVENT_COPY_INSTANT0(category_group, name, scope) \
    296     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
    297         category_group, name, TRACE_EVENT_FLAG_COPY | scope)
    298 #define TRACE_EVENT_COPY_INSTANT1(category_group, name, scope, \
    299                                   arg1_name, arg1_val) \
    300     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
    301         category_group, name, TRACE_EVENT_FLAG_COPY | scope, arg1_name, \
    302         arg1_val)
    303 #define TRACE_EVENT_COPY_INSTANT2(category_group, name, scope, \
    304                                   arg1_name, arg1_val, \
    305                                   arg2_name, arg2_val) \
    306     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_INSTANT, \
    307         category_group, name, TRACE_EVENT_FLAG_COPY | scope, \
    308         arg1_name, arg1_val, arg2_name, arg2_val)
    309 
    310 // Sets the current sample state to the given category and name (both must be
    311 // constant strings). These states are intended for a sampling profiler.
    312 // Implementation note: we store category and name together because we don't
    313 // want the inconsistency/expense of storing two pointers.
    314 // |thread_bucket| is [0..2] and is used to statically isolate samples in one
    315 // thread from others.
    316 #define TRACE_EVENT_SET_SAMPLING_STATE_FOR_BUCKET( \
    317     bucket_number, category, name)                 \
    318         trace_event_internal::                     \
    319         TraceEventSamplingStateScope<bucket_number>::Set(category "\0" name)
    320 
    321 // Returns a current sampling state of the given bucket.
    322 #define TRACE_EVENT_GET_SAMPLING_STATE_FOR_BUCKET(bucket_number) \
    323     trace_event_internal::TraceEventSamplingStateScope<bucket_number>::Current()
    324 
    325 // Creates a scope of a sampling state of the given bucket.
    326 //
    327 // {  // The sampling state is set within this scope.
    328 //    TRACE_EVENT_SAMPLING_STATE_SCOPE_FOR_BUCKET(0, "category", "name");
    329 //    ...;
    330 // }
    331 #define TRACE_EVENT_SCOPED_SAMPLING_STATE_FOR_BUCKET(                   \
    332     bucket_number, category, name)                                      \
    333     trace_event_internal::TraceEventSamplingStateScope<bucket_number>   \
    334         traceEventSamplingScope(category "\0" name);
    335 
    336 // Syntactic sugars for the sampling tracing in the main thread.
    337 #define TRACE_EVENT_SCOPED_SAMPLING_STATE(category, name) \
    338     TRACE_EVENT_SCOPED_SAMPLING_STATE_FOR_BUCKET(0, category, name)
    339 #define TRACE_EVENT_GET_SAMPLING_STATE() \
    340     TRACE_EVENT_GET_SAMPLING_STATE_FOR_BUCKET(0)
    341 #define TRACE_EVENT_SET_SAMPLING_STATE(category, name) \
    342     TRACE_EVENT_SET_SAMPLING_STATE_FOR_BUCKET(0, category, name)
    343 
    344 
    345 // Records a single BEGIN event called "name" immediately, with 0, 1 or 2
    346 // associated arguments. If the category is not enabled, then this
    347 // does nothing.
    348 // - category and name strings must have application lifetime (statics or
    349 //   literals). They may not include " chars.
    350 #define TRACE_EVENT_BEGIN0(category_group, name) \
    351     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
    352         category_group, name, TRACE_EVENT_FLAG_NONE)
    353 #define TRACE_EVENT_BEGIN1(category_group, name, arg1_name, arg1_val) \
    354     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
    355         category_group, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
    356 #define TRACE_EVENT_BEGIN2(category_group, name, arg1_name, arg1_val, \
    357         arg2_name, arg2_val) \
    358     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
    359         category_group, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
    360         arg2_name, arg2_val)
    361 #define TRACE_EVENT_COPY_BEGIN0(category_group, name) \
    362     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
    363         category_group, name, TRACE_EVENT_FLAG_COPY)
    364 #define TRACE_EVENT_COPY_BEGIN1(category_group, name, arg1_name, arg1_val) \
    365     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
    366         category_group, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
    367 #define TRACE_EVENT_COPY_BEGIN2(category_group, name, arg1_name, arg1_val, \
    368         arg2_name, arg2_val) \
    369     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_BEGIN, \
    370         category_group, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
    371         arg2_name, arg2_val)
    372 
    373 // Similar to TRACE_EVENT_BEGINx but with a custom |at| timestamp provided.
    374 // - |id| is used to match the _BEGIN event with the _END event.
    375 //   Events are considered to match if their category_group, name and id values
    376 //   all match. |id| must either be a pointer or an integer value up to 64 bits.
    377 //   If it's a pointer, the bits will be xored with a hash of the process ID so
    378 //   that the same pointer on two different processes will not collide.
    379 #define TRACE_EVENT_BEGIN_WITH_ID_TID_AND_TIMESTAMP0(category_group, \
    380         name, id, thread_id, timestamp) \
    381     INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
    382         TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, thread_id, \
    383         timestamp, TRACE_EVENT_FLAG_NONE)
    384 #define TRACE_EVENT_COPY_BEGIN_WITH_ID_TID_AND_TIMESTAMP0( \
    385         category_group, name, id, thread_id, timestamp) \
    386     INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
    387         TRACE_EVENT_PHASE_ASYNC_BEGIN, category_group, name, id, thread_id, \
    388         timestamp, TRACE_EVENT_FLAG_COPY)
    389 
    390 // Records a single END event for "name" immediately. If the category
    391 // is not enabled, then this does nothing.
    392 // - category and name strings must have application lifetime (statics or
    393 //   literals). They may not include " chars.
    394 #define TRACE_EVENT_END0(category_group, name) \
    395     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
    396         category_group, name, TRACE_EVENT_FLAG_NONE)
    397 #define TRACE_EVENT_END1(category_group, name, arg1_name, arg1_val) \
    398     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
    399         category_group, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
    400 #define TRACE_EVENT_END2(category_group, name, arg1_name, arg1_val, \
    401         arg2_name, arg2_val) \
    402     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
    403         category_group, name, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val, \
    404         arg2_name, arg2_val)
    405 #define TRACE_EVENT_COPY_END0(category_group, name) \
    406     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
    407         category_group, name, TRACE_EVENT_FLAG_COPY)
    408 #define TRACE_EVENT_COPY_END1(category_group, name, arg1_name, arg1_val) \
    409     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
    410         category_group, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val)
    411 #define TRACE_EVENT_COPY_END2(category_group, name, arg1_name, arg1_val, \
    412         arg2_name, arg2_val) \
    413     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_END, \
    414         category_group, name, TRACE_EVENT_FLAG_COPY, arg1_name, arg1_val, \
    415         arg2_name, arg2_val)
    416 
    417 // Similar to TRACE_EVENT_ENDx but with a custom |at| timestamp provided.
    418 // - |id| is used to match the _BEGIN event with the _END event.
    419 //   Events are considered to match if their category_group, name and id values
    420 //   all match. |id| must either be a pointer or an integer value up to 64 bits.
    421 //   If it's a pointer, the bits will be xored with a hash of the process ID so
    422 //   that the same pointer on two different processes will not collide.
    423 #define TRACE_EVENT_END_WITH_ID_TID_AND_TIMESTAMP0(category_group, \
    424         name, id, thread_id, timestamp) \
    425     INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
    426         TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, thread_id, \
    427         timestamp, TRACE_EVENT_FLAG_NONE)
    428 #define TRACE_EVENT_COPY_END_WITH_ID_TID_AND_TIMESTAMP0( \
    429         category_group, name, id, thread_id, timestamp) \
    430     INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP( \
    431         TRACE_EVENT_PHASE_ASYNC_END, category_group, name, id, thread_id, \
    432         timestamp, TRACE_EVENT_FLAG_COPY)
    433 
    434 // Records the value of a counter called "name" immediately. Value
    435 // must be representable as a 32 bit integer.
    436 // - category and name strings must have application lifetime (statics or
    437 //   literals). They may not include " chars.
    438 #define TRACE_COUNTER1(category_group, name, value) \
    439     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
    440         category_group, name, TRACE_EVENT_FLAG_NONE, \
    441         "value", static_cast<int>(value))
    442 #define TRACE_COPY_COUNTER1(category_group, name, value) \
    443     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
    444         category_group, name, TRACE_EVENT_FLAG_COPY, \
    445         "value", static_cast<int>(value))
    446 
    447 // Records the values of a multi-parted counter called "name" immediately.
    448 // The UI will treat value1 and value2 as parts of a whole, displaying their
    449 // values as a stacked-bar chart.
    450 // - category and name strings must have application lifetime (statics or
    451 //   literals). They may not include " chars.
    452 #define TRACE_COUNTER2(category_group, name, value1_name, value1_val, \
    453         value2_name, value2_val) \
    454     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
    455         category_group, name, TRACE_EVENT_FLAG_NONE, \
    456         value1_name, static_cast<int>(value1_val), \
    457         value2_name, static_cast<int>(value2_val))
    458 #define TRACE_COPY_COUNTER2(category_group, name, value1_name, value1_val, \
    459         value2_name, value2_val) \
    460     INTERNAL_TRACE_EVENT_ADD(TRACE_EVENT_PHASE_COUNTER, \
    461         category_group, name, TRACE_EVENT_FLAG_COPY, \
    462         value1_name, static_cast<int>(value1_val), \
    463         value2_name, static_cast<int>(value2_val))
    464 
    465 // Records the value of a counter called "name" immediately. Value
    466 // must be representable as a 32 bit integer.
    467 // - category and name strings must have application lifetime (statics or
    468 //   literals). They may not include " chars.
    469 // - |id| is used to disambiguate counters with the same name. It must either
    470 //   be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
    471 //   will be xored with a hash of the process ID so that the same pointer on
    472 //   two different processes will not collide.
    473 #define TRACE_COUNTER_ID1(category_group, name, id, value) \
    474     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
    475         category_group, name, id, TRACE_EVENT_FLAG_NONE, \
    476         "value", static_cast<int>(value))
    477 #define TRACE_COPY_COUNTER_ID1(category_group, name, id, value) \
    478     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
    479         category_group, name, id, TRACE_EVENT_FLAG_COPY, \
    480         "value", static_cast<int>(value))
    481 
    482 // Records the values of a multi-parted counter called "name" immediately.
    483 // The UI will treat value1 and value2 as parts of a whole, displaying their
    484 // values as a stacked-bar chart.
    485 // - category and name strings must have application lifetime (statics or
    486 //   literals). They may not include " chars.
    487 // - |id| is used to disambiguate counters with the same name. It must either
    488 //   be a pointer or an integer value up to 64 bits. If it's a pointer, the bits
    489 //   will be xored with a hash of the process ID so that the same pointer on
    490 //   two different processes will not collide.
    491 #define TRACE_COUNTER_ID2(category_group, name, id, value1_name, value1_val, \
    492         value2_name, value2_val) \
    493     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
    494         category_group, name, id, TRACE_EVENT_FLAG_NONE, \
    495         value1_name, static_cast<int>(value1_val), \
    496         value2_name, static_cast<int>(value2_val))
    497 #define TRACE_COPY_COUNTER_ID2(category_group, name, id, value1_name, \
    498         value1_val, value2_name, value2_val) \
    499     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_COUNTER, \
    500         category_group, name, id, TRACE_EVENT_FLAG_COPY, \
    501         value1_name, static_cast<int>(value1_val), \
    502         value2_name, static_cast<int>(value2_val))
    503 
    504 
    505 // Records a single ASYNC_BEGIN event called "name" immediately, with 0, 1 or 2
    506 // associated arguments. If the category is not enabled, then this
    507 // does nothing.
    508 // - category and name strings must have application lifetime (statics or
    509 //   literals). They may not include " chars.
    510 // - |id| is used to match the ASYNC_BEGIN event with the ASYNC_END event. ASYNC
    511 //   events are considered to match if their category_group, name and id values
    512 //   all match. |id| must either be a pointer or an integer value up to 64 bits.
    513 //   If it's a pointer, the bits will be xored with a hash of the process ID so
    514 //   that the same pointer on two different processes will not collide.
    515 // An asynchronous operation can consist of multiple phases. The first phase is
    516 // defined by the ASYNC_BEGIN calls. Additional phases can be defined using the
    517 // ASYNC_STEP macros. When the operation completes, call ASYNC_END.
    518 // An ASYNC trace typically occur on a single thread (if not, they will only be
    519 // drawn on the thread defined in the ASYNC_BEGIN event), but all events in that
    520 // operation must use the same |name| and |id|. Each event can have its own
    521 // args.
    522 #define TRACE_EVENT_ASYNC_BEGIN0(category_group, name, id) \
    523     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
    524         category_group, name, id, TRACE_EVENT_FLAG_NONE)
    525 #define TRACE_EVENT_ASYNC_BEGIN1(category_group, name, id, arg1_name, \
    526         arg1_val) \
    527     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
    528         category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
    529 #define TRACE_EVENT_ASYNC_BEGIN2(category_group, name, id, arg1_name, \
    530         arg1_val, arg2_name, arg2_val) \
    531     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
    532         category_group, name, id, TRACE_EVENT_FLAG_NONE, \
    533         arg1_name, arg1_val, arg2_name, arg2_val)
    534 #define TRACE_EVENT_COPY_ASYNC_BEGIN0(category_group, name, id) \
    535     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
    536         category_group, name, id, TRACE_EVENT_FLAG_COPY)
    537 #define TRACE_EVENT_COPY_ASYNC_BEGIN1(category_group, name, id, arg1_name, \
    538         arg1_val) \
    539     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
    540         category_group, name, id, TRACE_EVENT_FLAG_COPY, \
    541         arg1_name, arg1_val)
    542 #define TRACE_EVENT_COPY_ASYNC_BEGIN2(category_group, name, id, arg1_name, \
    543         arg1_val, arg2_name, arg2_val) \
    544     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_BEGIN, \
    545         category_group, name, id, TRACE_EVENT_FLAG_COPY, \
    546         arg1_name, arg1_val, arg2_name, arg2_val)
    547 
    548 // Records a single ASYNC_STEP event for |step| immediately. If the category
    549 // is not enabled, then this does nothing. The |name| and |id| must match the
    550 // ASYNC_BEGIN event above. The |step| param identifies this step within the
    551 // async event. This should be called at the beginning of the next phase of an
    552 // asynchronous operation.
    553 #define TRACE_EVENT_ASYNC_STEP0(category_group, name, id, step) \
    554     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
    555         category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
    556 #define TRACE_EVENT_ASYNC_STEP1(category_group, name, id, step, \
    557                                       arg1_name, arg1_val) \
    558     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
    559         category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
    560         arg1_name, arg1_val)
    561 
    562 #define TRACE_EVENT_COPY_ASYNC_STEP0(category_group, name, id, step) \
    563     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
    564         category_group, name, id, TRACE_EVENT_FLAG_COPY, "step", step)
    565 #define TRACE_EVENT_COPY_ASYNC_STEP1(category_group, name, id, step, \
    566         arg1_name, arg1_val) \
    567     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_STEP, \
    568         category_group, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \
    569         arg1_name, arg1_val)
    570 
    571 // Records a single ASYNC_END event for "name" immediately. If the category
    572 // is not enabled, then this does nothing.
    573 #define TRACE_EVENT_ASYNC_END0(category_group, name, id) \
    574     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
    575         category_group, name, id, TRACE_EVENT_FLAG_NONE)
    576 #define TRACE_EVENT_ASYNC_END1(category_group, name, id, arg1_name, arg1_val) \
    577     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
    578         category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
    579 #define TRACE_EVENT_ASYNC_END2(category_group, name, id, arg1_name, arg1_val, \
    580         arg2_name, arg2_val) \
    581     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
    582         category_group, name, id, TRACE_EVENT_FLAG_NONE, \
    583         arg1_name, arg1_val, arg2_name, arg2_val)
    584 #define TRACE_EVENT_COPY_ASYNC_END0(category_group, name, id) \
    585     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
    586         category_group, name, id, TRACE_EVENT_FLAG_COPY)
    587 #define TRACE_EVENT_COPY_ASYNC_END1(category_group, name, id, arg1_name, \
    588         arg1_val) \
    589     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
    590         category_group, name, id, TRACE_EVENT_FLAG_COPY, \
    591         arg1_name, arg1_val)
    592 #define TRACE_EVENT_COPY_ASYNC_END2(category_group, name, id, arg1_name, \
    593         arg1_val, arg2_name, arg2_val) \
    594     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_ASYNC_END, \
    595         category_group, name, id, TRACE_EVENT_FLAG_COPY, \
    596         arg1_name, arg1_val, arg2_name, arg2_val)
    597 
    598 
    599 // Records a single FLOW_BEGIN event called "name" immediately, with 0, 1 or 2
    600 // associated arguments. If the category is not enabled, then this
    601 // does nothing.
    602 // - category and name strings must have application lifetime (statics or
    603 //   literals). They may not include " chars.
    604 // - |id| is used to match the FLOW_BEGIN event with the FLOW_END event. FLOW
    605 //   events are considered to match if their category_group, name and id values
    606 //   all match. |id| must either be a pointer or an integer value up to 64 bits.
    607 //   If it's a pointer, the bits will be xored with a hash of the process ID so
    608 //   that the same pointer on two different processes will not collide.
    609 // FLOW events are different from ASYNC events in how they are drawn by the
    610 // tracing UI. A FLOW defines asynchronous data flow, such as posting a task
    611 // (FLOW_BEGIN) and later executing that task (FLOW_END). Expect FLOWs to be
    612 // drawn as lines or arrows from FLOW_BEGIN scopes to FLOW_END scopes. Similar
    613 // to ASYNC, a FLOW can consist of multiple phases. The first phase is defined
    614 // by the FLOW_BEGIN calls. Additional phases can be defined using the FLOW_STEP
    615 // macros. When the operation completes, call FLOW_END. An async operation can
    616 // span threads and processes, but all events in that operation must use the
    617 // same |name| and |id|. Each event can have its own args.
    618 #define TRACE_EVENT_FLOW_BEGIN0(category_group, name, id) \
    619     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
    620         category_group, name, id, TRACE_EVENT_FLAG_NONE)
    621 #define TRACE_EVENT_FLOW_BEGIN1(category_group, name, id, arg1_name, arg1_val) \
    622     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
    623         category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
    624 #define TRACE_EVENT_FLOW_BEGIN2(category_group, name, id, arg1_name, arg1_val, \
    625         arg2_name, arg2_val) \
    626     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
    627         category_group, name, id, TRACE_EVENT_FLAG_NONE, \
    628         arg1_name, arg1_val, arg2_name, arg2_val)
    629 #define TRACE_EVENT_COPY_FLOW_BEGIN0(category_group, name, id) \
    630     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
    631         category_group, name, id, TRACE_EVENT_FLAG_COPY)
    632 #define TRACE_EVENT_COPY_FLOW_BEGIN1(category_group, name, id, arg1_name, \
    633         arg1_val) \
    634     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
    635         category_group, name, id, TRACE_EVENT_FLAG_COPY, \
    636         arg1_name, arg1_val)
    637 #define TRACE_EVENT_COPY_FLOW_BEGIN2(category_group, name, id, arg1_name, \
    638         arg1_val, arg2_name, arg2_val) \
    639     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_BEGIN, \
    640         category_group, name, id, TRACE_EVENT_FLAG_COPY, \
    641         arg1_name, arg1_val, arg2_name, arg2_val)
    642 
    643 // Records a single FLOW_STEP event for |step| immediately. If the category
    644 // is not enabled, then this does nothing. The |name| and |id| must match the
    645 // FLOW_BEGIN event above. The |step| param identifies this step within the
    646 // async event. This should be called at the beginning of the next phase of an
    647 // asynchronous operation.
    648 #define TRACE_EVENT_FLOW_STEP0(category_group, name, id, step) \
    649     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
    650         category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step)
    651 #define TRACE_EVENT_FLOW_STEP1(category_group, name, id, step, \
    652         arg1_name, arg1_val) \
    653     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
    654         category_group, name, id, TRACE_EVENT_FLAG_NONE, "step", step, \
    655         arg1_name, arg1_val)
    656 #define TRACE_EVENT_COPY_FLOW_STEP0(category_group, name, id, step) \
    657     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
    658         category_group, name, id, TRACE_EVENT_FLAG_COPY, "step", step)
    659 #define TRACE_EVENT_COPY_FLOW_STEP1(category_group, name, id, step, \
    660         arg1_name, arg1_val) \
    661     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_STEP, \
    662         category_group, name, id, TRACE_EVENT_FLAG_COPY, "step", step, \
    663         arg1_name, arg1_val)
    664 
    665 // Records a single FLOW_END event for "name" immediately. If the category
    666 // is not enabled, then this does nothing.
    667 #define TRACE_EVENT_FLOW_END0(category_group, name, id) \
    668     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
    669         category_group, name, id, TRACE_EVENT_FLAG_NONE)
    670 #define TRACE_EVENT_FLOW_END1(category_group, name, id, arg1_name, arg1_val) \
    671     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
    672         category_group, name, id, TRACE_EVENT_FLAG_NONE, arg1_name, arg1_val)
    673 #define TRACE_EVENT_FLOW_END2(category_group, name, id, arg1_name, arg1_val, \
    674         arg2_name, arg2_val) \
    675     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
    676         category_group, name, id, TRACE_EVENT_FLAG_NONE, \
    677         arg1_name, arg1_val, arg2_name, arg2_val)
    678 #define TRACE_EVENT_COPY_FLOW_END0(category_group, name, id) \
    679     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
    680         category_group, name, id, TRACE_EVENT_FLAG_COPY)
    681 #define TRACE_EVENT_COPY_FLOW_END1(category_group, name, id, arg1_name, \
    682         arg1_val) \
    683     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
    684         category_group, name, id, TRACE_EVENT_FLAG_COPY, \
    685         arg1_name, arg1_val)
    686 #define TRACE_EVENT_COPY_FLOW_END2(category_group, name, id, arg1_name, \
    687         arg1_val, arg2_name, arg2_val) \
    688     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_FLOW_END, \
    689         category_group, name, id, TRACE_EVENT_FLAG_COPY, \
    690         arg1_name, arg1_val, arg2_name, arg2_val)
    691 
    692 // Macros to track the life time and value of arbitrary client objects.
    693 // See also TraceTrackableObject.
    694 #define TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group, name, id) \
    695     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_CREATE_OBJECT, \
    696         category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE)
    697 
    698 #define TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group, name, id, snapshot) \
    699     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, \
    700         category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE,\
    701         "snapshot", snapshot)
    702 
    703 #define TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group, name, id) \
    704     INTERNAL_TRACE_EVENT_ADD_WITH_ID(TRACE_EVENT_PHASE_DELETE_OBJECT, \
    705         category_group, name, TRACE_ID_DONT_MANGLE(id), TRACE_EVENT_FLAG_NONE)
    706 
    707 
    708 // Macro to efficiently determine if a given category group is enabled.
    709 #define TRACE_EVENT_CATEGORY_GROUP_ENABLED(category_group, ret) \
    710     do { \
    711       INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
    712       if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
    713         *ret = true; \
    714       } else { \
    715         *ret = false; \
    716       } \
    717     } while (0)
    718 
    719 // Macro to efficiently determine, through polling, if a new trace has begun.
    720 #define TRACE_EVENT_IS_NEW_TRACE(ret) \
    721     do { \
    722       static int INTERNAL_TRACE_EVENT_UID(lastRecordingNumber) = 0; \
    723       int num_traces_recorded = TRACE_EVENT_API_GET_NUM_TRACES_RECORDED(); \
    724       if (num_traces_recorded != -1 && \
    725           num_traces_recorded != \
    726           INTERNAL_TRACE_EVENT_UID(lastRecordingNumber)) { \
    727         INTERNAL_TRACE_EVENT_UID(lastRecordingNumber) = \
    728             num_traces_recorded; \
    729         *ret = true; \
    730       } else { \
    731         *ret = false; \
    732       } \
    733     } while (0)
    734 
    735 ////////////////////////////////////////////////////////////////////////////////
    736 // Implementation specific tracing API definitions.
    737 
    738 // Get a pointer to the enabled state of the given trace category. Only
    739 // long-lived literal strings should be given as the category group. The
    740 // returned pointer can be held permanently in a local static for example. If
    741 // the unsigned char is non-zero, tracing is enabled. If tracing is enabled,
    742 // TRACE_EVENT_API_ADD_TRACE_EVENT can be called. It's OK if tracing is disabled
    743 // between the load of the tracing state and the call to
    744 // TRACE_EVENT_API_ADD_TRACE_EVENT, because this flag only provides an early out
    745 // for best performance when tracing is disabled.
    746 // const unsigned char*
    747 //     TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(const char* category_group)
    748 #define TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED \
    749     base::debug::TraceLog::GetCategoryGroupEnabled
    750 
    751 // Get the number of times traces have been recorded. This is used to implement
    752 // the TRACE_EVENT_IS_NEW_TRACE facility.
    753 // unsigned int TRACE_EVENT_API_GET_NUM_TRACES_RECORDED()
    754 #define TRACE_EVENT_API_GET_NUM_TRACES_RECORDED \
    755     base::debug::TraceLog::GetInstance()->GetNumTracesRecorded
    756 
    757 // Add a trace event to the platform tracing system.
    758 // void TRACE_EVENT_API_ADD_TRACE_EVENT(
    759 //                    char phase,
    760 //                    const unsigned char* category_group_enabled,
    761 //                    const char* name,
    762 //                    unsigned long long id,
    763 //                    int num_args,
    764 //                    const char** arg_names,
    765 //                    const unsigned char* arg_types,
    766 //                    const unsigned long long* arg_values,
    767 //                    unsigned char flags)
    768 #define TRACE_EVENT_API_ADD_TRACE_EVENT \
    769     base::debug::TraceLog::GetInstance()->AddTraceEvent
    770 
    771 // Add a trace event to the platform tracing system.
    772 // void TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_TIMESTAMP(
    773 //                    char phase,
    774 //                    const unsigned char* category_group_enabled,
    775 //                    const char* name,
    776 //                    unsigned long long id,
    777 //                    int thread_id,
    778 //                    const TimeTicks& timestamp,
    779 //                    int num_args,
    780 //                    const char** arg_names,
    781 //                    const unsigned char* arg_types,
    782 //                    const unsigned long long* arg_values,
    783 //                    unsigned char flags)
    784 #define TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP \
    785     base::debug::TraceLog::GetInstance()->AddTraceEventWithThreadIdAndTimestamp
    786 
    787 // Defines atomic operations used internally by the tracing system.
    788 #define TRACE_EVENT_API_ATOMIC_WORD base::subtle::AtomicWord
    789 #define TRACE_EVENT_API_ATOMIC_LOAD(var) base::subtle::NoBarrier_Load(&(var))
    790 #define TRACE_EVENT_API_ATOMIC_STORE(var, value) \
    791     base::subtle::NoBarrier_Store(&(var), (value))
    792 
    793 // Defines visibility for classes in trace_event.h
    794 #define TRACE_EVENT_API_CLASS_EXPORT BASE_EXPORT
    795 
    796 // The thread buckets for the sampling profiler.
    797 TRACE_EVENT_API_CLASS_EXPORT extern \
    798     TRACE_EVENT_API_ATOMIC_WORD g_trace_state[3];
    799 
    800 #define TRACE_EVENT_API_THREAD_BUCKET(thread_bucket)                           \
    801     g_trace_state[thread_bucket]
    802 
    803 ////////////////////////////////////////////////////////////////////////////////
    804 
    805 // Implementation detail: trace event macros create temporary variables
    806 // to keep instrumentation overhead low. These macros give each temporary
    807 // variable a unique name based on the line number to prevent name collisions.
    808 #define INTERNAL_TRACE_EVENT_UID3(a,b) \
    809     trace_event_unique_##a##b
    810 #define INTERNAL_TRACE_EVENT_UID2(a,b) \
    811     INTERNAL_TRACE_EVENT_UID3(a,b)
    812 #define INTERNAL_TRACE_EVENT_UID(name_prefix) \
    813     INTERNAL_TRACE_EVENT_UID2(name_prefix, __LINE__)
    814 
    815 // Implementation detail: internal macro to create static category.
    816 // No barriers are needed, because this code is designed to operate safely
    817 // even when the unsigned char* points to garbage data (which may be the case
    818 // on processors without cache coherency).
    819 #define INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group) \
    820     static TRACE_EVENT_API_ATOMIC_WORD INTERNAL_TRACE_EVENT_UID(atomic) = 0; \
    821     const unsigned char* INTERNAL_TRACE_EVENT_UID(catstatic) = \
    822         reinterpret_cast<const unsigned char*>(TRACE_EVENT_API_ATOMIC_LOAD( \
    823             INTERNAL_TRACE_EVENT_UID(atomic))); \
    824     if (!INTERNAL_TRACE_EVENT_UID(catstatic)) { \
    825       INTERNAL_TRACE_EVENT_UID(catstatic) = \
    826           TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_group); \
    827       TRACE_EVENT_API_ATOMIC_STORE(INTERNAL_TRACE_EVENT_UID(atomic), \
    828           reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>( \
    829               INTERNAL_TRACE_EVENT_UID(catstatic))); \
    830     }
    831 
    832 // Implementation detail: internal macro to create static category and add
    833 // event if the category is enabled.
    834 #define INTERNAL_TRACE_EVENT_ADD(phase, category_group, name, flags, ...) \
    835     do { \
    836       INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
    837       if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
    838         trace_event_internal::AddTraceEvent( \
    839             phase, INTERNAL_TRACE_EVENT_UID(catstatic), name, \
    840             trace_event_internal::kNoEventId, flags, ##__VA_ARGS__); \
    841       } \
    842     } while (0)
    843 
    844 // Implementation detail: internal macro to create static category and add begin
    845 // event if the category is enabled. Also adds the end event when the scope
    846 // ends.
    847 #define INTERNAL_TRACE_EVENT_ADD_SCOPED(category_group, name, ...) \
    848     INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
    849     trace_event_internal::TraceEndOnScopeClose \
    850         INTERNAL_TRACE_EVENT_UID(profileScope); \
    851     if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
    852       trace_event_internal::AddTraceEvent( \
    853           TRACE_EVENT_PHASE_BEGIN, \
    854           INTERNAL_TRACE_EVENT_UID(catstatic), \
    855           name, trace_event_internal::kNoEventId, \
    856           TRACE_EVENT_FLAG_NONE, ##__VA_ARGS__); \
    857       INTERNAL_TRACE_EVENT_UID(profileScope).Initialize( \
    858           INTERNAL_TRACE_EVENT_UID(catstatic), name); \
    859     }
    860 
    861 // Implementation detail: internal macro to create static category and add
    862 // event if the category is enabled.
    863 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID(phase, category_group, name, id, \
    864                                          flags, ...) \
    865     do { \
    866       INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
    867       if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
    868         unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
    869         trace_event_internal::TraceID trace_event_trace_id( \
    870             id, &trace_event_flags); \
    871         trace_event_internal::AddTraceEvent( \
    872             phase, INTERNAL_TRACE_EVENT_UID(catstatic), \
    873             name, trace_event_trace_id.data(), trace_event_flags, \
    874             ##__VA_ARGS__); \
    875       } \
    876     } while (0)
    877 
    878 // Implementation detail: internal macro to create static category and add
    879 // event if the category is enabled.
    880 #define INTERNAL_TRACE_EVENT_ADD_WITH_ID_TID_AND_TIMESTAMP(phase, \
    881         category_group, name, id, thread_id, timestamp, flags, ...) \
    882     do { \
    883       INTERNAL_TRACE_EVENT_GET_CATEGORY_INFO(category_group); \
    884       if (*INTERNAL_TRACE_EVENT_UID(catstatic)) { \
    885         unsigned char trace_event_flags = flags | TRACE_EVENT_FLAG_HAS_ID; \
    886         trace_event_internal::TraceID trace_event_trace_id( \
    887             id, &trace_event_flags); \
    888         trace_event_internal::AddTraceEventWithThreadIdAndTimestamp( \
    889             phase, INTERNAL_TRACE_EVENT_UID(catstatic), \
    890             name, trace_event_trace_id.data(), \
    891             thread_id, base::TimeTicks::FromInternalValue(timestamp), \
    892             trace_event_flags, ##__VA_ARGS__); \
    893       } \
    894     } while (0)
    895 
    896 // Notes regarding the following definitions:
    897 // New values can be added and propagated to third party libraries, but existing
    898 // definitions must never be changed, because third party libraries may use old
    899 // definitions.
    900 
    901 // Phase indicates the nature of an event entry. E.g. part of a begin/end pair.
    902 #define TRACE_EVENT_PHASE_BEGIN    ('B')
    903 #define TRACE_EVENT_PHASE_END      ('E')
    904 #define TRACE_EVENT_PHASE_INSTANT  ('i')
    905 #define TRACE_EVENT_PHASE_ASYNC_BEGIN ('S')
    906 #define TRACE_EVENT_PHASE_ASYNC_STEP  ('T')
    907 #define TRACE_EVENT_PHASE_ASYNC_END   ('F')
    908 #define TRACE_EVENT_PHASE_FLOW_BEGIN ('s')
    909 #define TRACE_EVENT_PHASE_FLOW_STEP  ('t')
    910 #define TRACE_EVENT_PHASE_FLOW_END   ('f')
    911 #define TRACE_EVENT_PHASE_METADATA ('M')
    912 #define TRACE_EVENT_PHASE_COUNTER  ('C')
    913 #define TRACE_EVENT_PHASE_SAMPLE  ('P')
    914 #define TRACE_EVENT_PHASE_CREATE_OBJECT ('N')
    915 #define TRACE_EVENT_PHASE_SNAPSHOT_OBJECT ('O')
    916 #define TRACE_EVENT_PHASE_DELETE_OBJECT ('D')
    917 
    918 // Flags for changing the behavior of TRACE_EVENT_API_ADD_TRACE_EVENT.
    919 #define TRACE_EVENT_FLAG_NONE         (static_cast<unsigned char>(0))
    920 #define TRACE_EVENT_FLAG_COPY         (static_cast<unsigned char>(1 << 0))
    921 #define TRACE_EVENT_FLAG_HAS_ID       (static_cast<unsigned char>(1 << 1))
    922 #define TRACE_EVENT_FLAG_MANGLE_ID    (static_cast<unsigned char>(1 << 2))
    923 #define TRACE_EVENT_FLAG_SCOPE_OFFSET (static_cast<unsigned char>(1 << 3))
    924 
    925 #define TRACE_EVENT_FLAG_SCOPE_MASK   (static_cast<unsigned char>( \
    926     TRACE_EVENT_FLAG_SCOPE_OFFSET | (TRACE_EVENT_FLAG_SCOPE_OFFSET << 1)))
    927 
    928 // Type values for identifying types in the TraceValue union.
    929 #define TRACE_VALUE_TYPE_BOOL         (static_cast<unsigned char>(1))
    930 #define TRACE_VALUE_TYPE_UINT         (static_cast<unsigned char>(2))
    931 #define TRACE_VALUE_TYPE_INT          (static_cast<unsigned char>(3))
    932 #define TRACE_VALUE_TYPE_DOUBLE       (static_cast<unsigned char>(4))
    933 #define TRACE_VALUE_TYPE_POINTER      (static_cast<unsigned char>(5))
    934 #define TRACE_VALUE_TYPE_STRING       (static_cast<unsigned char>(6))
    935 #define TRACE_VALUE_TYPE_COPY_STRING  (static_cast<unsigned char>(7))
    936 #define TRACE_VALUE_TYPE_CONVERTABLE  (static_cast<unsigned char>(8))
    937 
    938 // Enum reflecting the scope of an INSTANT event. Must fit within
    939 // TRACE_EVENT_FLAG_SCOPE_MASK.
    940 #define TRACE_EVENT_SCOPE_GLOBAL  (static_cast<unsigned char>(0 << 3))
    941 #define TRACE_EVENT_SCOPE_PROCESS (static_cast<unsigned char>(1 << 3))
    942 #define TRACE_EVENT_SCOPE_THREAD  (static_cast<unsigned char>(2 << 3))
    943 
    944 #define TRACE_EVENT_SCOPE_NAME_GLOBAL  ('g')
    945 #define TRACE_EVENT_SCOPE_NAME_PROCESS ('p')
    946 #define TRACE_EVENT_SCOPE_NAME_THREAD  ('t')
    947 
    948 namespace trace_event_internal {
    949 
    950 // Specify these values when the corresponding argument of AddTraceEvent is not
    951 // used.
    952 const int kZeroNumArgs = 0;
    953 const unsigned long long kNoEventId = 0;
    954 
    955 // TraceID encapsulates an ID that can either be an integer or pointer. Pointers
    956 // are by default mangled with the Process ID so that they are unlikely to
    957 // collide when the same pointer is used on different processes.
    958 class TraceID {
    959  public:
    960   class DontMangle {
    961    public:
    962     explicit DontMangle(void* id)
    963         : data_(static_cast<unsigned long long>(
    964               reinterpret_cast<unsigned long>(id))) {}
    965     explicit DontMangle(unsigned long long id) : data_(id) {}
    966     explicit DontMangle(unsigned long id) : data_(id) {}
    967     explicit DontMangle(unsigned int id) : data_(id) {}
    968     explicit DontMangle(unsigned short id) : data_(id) {}
    969     explicit DontMangle(unsigned char id) : data_(id) {}
    970     explicit DontMangle(long long id)
    971         : data_(static_cast<unsigned long long>(id)) {}
    972     explicit DontMangle(long id)
    973         : data_(static_cast<unsigned long long>(id)) {}
    974     explicit DontMangle(int id)
    975         : data_(static_cast<unsigned long long>(id)) {}
    976     explicit DontMangle(short id)
    977         : data_(static_cast<unsigned long long>(id)) {}
    978     explicit DontMangle(signed char id)
    979         : data_(static_cast<unsigned long long>(id)) {}
    980     unsigned long long data() const { return data_; }
    981    private:
    982     unsigned long long data_;
    983   };
    984 
    985   class ForceMangle {
    986    public:
    987     explicit ForceMangle(unsigned long long id) : data_(id) {}
    988     explicit ForceMangle(unsigned long id) : data_(id) {}
    989     explicit ForceMangle(unsigned int id) : data_(id) {}
    990     explicit ForceMangle(unsigned short id) : data_(id) {}
    991     explicit ForceMangle(unsigned char id) : data_(id) {}
    992     explicit ForceMangle(long long id)
    993         : data_(static_cast<unsigned long long>(id)) {}
    994     explicit ForceMangle(long id)
    995         : data_(static_cast<unsigned long long>(id)) {}
    996     explicit ForceMangle(int id)
    997         : data_(static_cast<unsigned long long>(id)) {}
    998     explicit ForceMangle(short id)
    999         : data_(static_cast<unsigned long long>(id)) {}
   1000     explicit ForceMangle(signed char id)
   1001         : data_(static_cast<unsigned long long>(id)) {}
   1002     unsigned long long data() const { return data_; }
   1003    private:
   1004     unsigned long long data_;
   1005   };
   1006 
   1007   TraceID(const void* id, unsigned char* flags)
   1008       : data_(static_cast<unsigned long long>(
   1009               reinterpret_cast<unsigned long>(id))) {
   1010     *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
   1011   }
   1012   TraceID(ForceMangle id, unsigned char* flags) : data_(id.data()) {
   1013     *flags |= TRACE_EVENT_FLAG_MANGLE_ID;
   1014   }
   1015   TraceID(DontMangle id, unsigned char* flags) : data_(id.data()) {
   1016   }
   1017   TraceID(unsigned long long id, unsigned char* flags)
   1018       : data_(id) { (void)flags; }
   1019   TraceID(unsigned long id, unsigned char* flags)
   1020       : data_(id) { (void)flags; }
   1021   TraceID(unsigned int id, unsigned char* flags)
   1022       : data_(id) { (void)flags; }
   1023   TraceID(unsigned short id, unsigned char* flags)
   1024       : data_(id) { (void)flags; }
   1025   TraceID(unsigned char id, unsigned char* flags)
   1026       : data_(id) { (void)flags; }
   1027   TraceID(long long id, unsigned char* flags)
   1028       : data_(static_cast<unsigned long long>(id)) { (void)flags; }
   1029   TraceID(long id, unsigned char* flags)
   1030       : data_(static_cast<unsigned long long>(id)) { (void)flags; }
   1031   TraceID(int id, unsigned char* flags)
   1032       : data_(static_cast<unsigned long long>(id)) { (void)flags; }
   1033   TraceID(short id, unsigned char* flags)
   1034       : data_(static_cast<unsigned long long>(id)) { (void)flags; }
   1035   TraceID(signed char id, unsigned char* flags)
   1036       : data_(static_cast<unsigned long long>(id)) { (void)flags; }
   1037 
   1038   unsigned long long data() const { return data_; }
   1039 
   1040  private:
   1041   unsigned long long data_;
   1042 };
   1043 
   1044 // Simple union to store various types as unsigned long long.
   1045 union TraceValueUnion {
   1046   bool as_bool;
   1047   unsigned long long as_uint;
   1048   long long as_int;
   1049   double as_double;
   1050   const void* as_pointer;
   1051   const char* as_string;
   1052 };
   1053 
   1054 // Simple container for const char* that should be copied instead of retained.
   1055 class TraceStringWithCopy {
   1056  public:
   1057   explicit TraceStringWithCopy(const char* str) : str_(str) {}
   1058   operator const char* () const { return str_; }
   1059  private:
   1060   const char* str_;
   1061 };
   1062 
   1063 // Define SetTraceValue for each allowed type. It stores the type and
   1064 // value in the return arguments. This allows this API to avoid declaring any
   1065 // structures so that it is portable to third_party libraries.
   1066 #define INTERNAL_DECLARE_SET_TRACE_VALUE(actual_type, \
   1067                                          union_member, \
   1068                                          value_type_id) \
   1069     static inline void SetTraceValue( \
   1070         actual_type arg, \
   1071         unsigned char* type, \
   1072         unsigned long long* value) { \
   1073       TraceValueUnion type_value; \
   1074       type_value.union_member = arg; \
   1075       *type = value_type_id; \
   1076       *value = type_value.as_uint; \
   1077     }
   1078 // Simpler form for int types that can be safely casted.
   1079 #define INTERNAL_DECLARE_SET_TRACE_VALUE_INT(actual_type, \
   1080                                              value_type_id) \
   1081     static inline void SetTraceValue( \
   1082         actual_type arg, \
   1083         unsigned char* type, \
   1084         unsigned long long* value) { \
   1085       *type = value_type_id; \
   1086       *value = static_cast<unsigned long long>(arg); \
   1087     }
   1088 
   1089 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long long, TRACE_VALUE_TYPE_UINT)
   1090 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned long, TRACE_VALUE_TYPE_UINT)
   1091 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned int, TRACE_VALUE_TYPE_UINT)
   1092 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned short, TRACE_VALUE_TYPE_UINT)
   1093 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(unsigned char, TRACE_VALUE_TYPE_UINT)
   1094 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long long, TRACE_VALUE_TYPE_INT)
   1095 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(long, TRACE_VALUE_TYPE_INT)
   1096 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(int, TRACE_VALUE_TYPE_INT)
   1097 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(short, TRACE_VALUE_TYPE_INT)
   1098 INTERNAL_DECLARE_SET_TRACE_VALUE_INT(signed char, TRACE_VALUE_TYPE_INT)
   1099 INTERNAL_DECLARE_SET_TRACE_VALUE(bool, as_bool, TRACE_VALUE_TYPE_BOOL)
   1100 INTERNAL_DECLARE_SET_TRACE_VALUE(double, as_double, TRACE_VALUE_TYPE_DOUBLE)
   1101 INTERNAL_DECLARE_SET_TRACE_VALUE(const void*, as_pointer,
   1102                                  TRACE_VALUE_TYPE_POINTER)
   1103 INTERNAL_DECLARE_SET_TRACE_VALUE(const char*, as_string,
   1104                                  TRACE_VALUE_TYPE_STRING)
   1105 INTERNAL_DECLARE_SET_TRACE_VALUE(const TraceStringWithCopy&, as_string,
   1106                                  TRACE_VALUE_TYPE_COPY_STRING)
   1107 
   1108 #undef INTERNAL_DECLARE_SET_TRACE_VALUE
   1109 #undef INTERNAL_DECLARE_SET_TRACE_VALUE_INT
   1110 
   1111 // std::string version of SetTraceValue so that trace arguments can be strings.
   1112 static inline void SetTraceValue(const std::string& arg,
   1113                                  unsigned char* type,
   1114                                  unsigned long long* value) {
   1115   TraceValueUnion type_value;
   1116   type_value.as_string = arg.c_str();
   1117   *type = TRACE_VALUE_TYPE_COPY_STRING;
   1118   *value = type_value.as_uint;
   1119 }
   1120 
   1121 // These AddTraceEvent and AddTraceEventWithThreadIdAndTimestamp template
   1122 // functions are defined here instead of in the macro, because the arg_values
   1123 // could be temporary objects, such as std::string. In order to store
   1124 // pointers to the internal c_str and pass through to the tracing API,
   1125 // the arg_values must live throughout these procedures.
   1126 
   1127 static inline void AddTraceEventWithThreadIdAndTimestamp(
   1128     char phase,
   1129     const unsigned char* category_group_enabled,
   1130     const char* name,
   1131     unsigned long long id,
   1132     int thread_id,
   1133     const base::TimeTicks& timestamp,
   1134     unsigned char flags,
   1135     const char* arg1_name,
   1136     scoped_ptr<base::debug::ConvertableToTraceFormat> arg1_val) {
   1137   const int num_args = 1;
   1138   unsigned char arg_types[1] = { TRACE_VALUE_TYPE_CONVERTABLE };
   1139   scoped_ptr<base::debug::ConvertableToTraceFormat> convertable_values[1];
   1140   convertable_values[0].reset(arg1_val.release());
   1141 
   1142   TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
   1143       phase, category_group_enabled, name, id, thread_id, timestamp,
   1144       num_args, &arg1_name, arg_types, NULL, convertable_values, flags);
   1145 }
   1146 
   1147 static inline void AddTraceEvent(
   1148     char phase,
   1149     const unsigned char* category_group_enabled,
   1150     const char* name,
   1151     unsigned long long id,
   1152     unsigned char flags,
   1153     const char* arg1_name,
   1154     scoped_ptr<base::debug::ConvertableToTraceFormat> arg1_val) {
   1155   int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
   1156   base::TimeTicks now = base::TimeTicks::NowFromSystemTraceTime();
   1157   AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled, name, id,
   1158                                         thread_id, now, flags, arg1_name,
   1159                                         arg1_val.Pass());
   1160 }
   1161 
   1162 template<class ARG1_TYPE>
   1163 static inline void AddTraceEventWithThreadIdAndTimestamp(
   1164     char phase,
   1165     const unsigned char* category_group_enabled,
   1166     const char* name,
   1167     unsigned long long id,
   1168     int thread_id,
   1169     const base::TimeTicks& timestamp,
   1170     unsigned char flags,
   1171     const char* arg1_name,
   1172     ARG1_TYPE arg1_val,
   1173     const char* arg2_name,
   1174     scoped_ptr<base::debug::ConvertableToTraceFormat> arg2_val) {
   1175   const int num_args = 2;
   1176   const char* arg_names[2] = { arg1_name, arg2_name };
   1177 
   1178   unsigned char arg_types[2];
   1179   unsigned long long arg_values[2];
   1180   SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
   1181   arg_types[1] = TRACE_VALUE_TYPE_CONVERTABLE;
   1182 
   1183   scoped_ptr<base::debug::ConvertableToTraceFormat> convertable_values[2];
   1184   convertable_values[1].reset(arg2_val.release());
   1185 
   1186   TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
   1187       phase, category_group_enabled, name, id, thread_id, timestamp,
   1188       num_args, arg_names, arg_types, arg_values, convertable_values, flags);
   1189 }
   1190 
   1191 template<class ARG1_TYPE>
   1192 static inline void AddTraceEvent(
   1193     char phase,
   1194     const unsigned char* category_group_enabled,
   1195     const char* name,
   1196     unsigned long long id,
   1197     unsigned char flags,
   1198     const char* arg1_name,
   1199     ARG1_TYPE arg1_val,
   1200     const char* arg2_name,
   1201     scoped_ptr<base::debug::ConvertableToTraceFormat> arg2_val) {
   1202   int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
   1203   base::TimeTicks now = base::TimeTicks::NowFromSystemTraceTime();
   1204   AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled, name, id,
   1205                                         thread_id, now, flags,
   1206                                         arg1_name, arg1_val,
   1207                                         arg2_name, arg2_val.Pass());
   1208 }
   1209 
   1210 template<class ARG2_TYPE>
   1211 static inline void AddTraceEventWithThreadIdAndTimestamp(
   1212     char phase,
   1213     const unsigned char* category_group_enabled,
   1214     const char* name,
   1215     unsigned long long id,
   1216     int thread_id,
   1217     const base::TimeTicks& timestamp,
   1218     unsigned char flags,
   1219     const char* arg1_name,
   1220     scoped_ptr<base::debug::ConvertableToTraceFormat> arg1_val,
   1221     const char* arg2_name,
   1222     ARG2_TYPE arg2_val) {
   1223   const int num_args = 2;
   1224   const char* arg_names[2] = { arg1_name, arg2_name };
   1225 
   1226   unsigned char arg_types[2];
   1227   unsigned long long arg_values[2];
   1228   arg_types[0] = TRACE_VALUE_TYPE_CONVERTABLE;
   1229   arg_values[0] = 0;
   1230   SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]);
   1231 
   1232   scoped_ptr<base::debug::ConvertableToTraceFormat> convertable_values[2];
   1233   convertable_values[0].reset(arg1_val.release());
   1234 
   1235   TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
   1236       phase, category_group_enabled, name, id, thread_id, timestamp,
   1237       num_args, arg_names, arg_types, arg_values, convertable_values, flags);
   1238 }
   1239 
   1240 template<class ARG2_TYPE>
   1241 static inline void AddTraceEvent(
   1242     char phase,
   1243     const unsigned char* category_group_enabled,
   1244     const char* name,
   1245     unsigned long long id,
   1246     unsigned char flags,
   1247     const char* arg1_name,
   1248     scoped_ptr<base::debug::ConvertableToTraceFormat> arg1_val,
   1249     const char* arg2_name,
   1250     ARG2_TYPE arg2_val) {
   1251   int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
   1252   base::TimeTicks now = base::TimeTicks::NowFromSystemTraceTime();
   1253   AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled, name, id,
   1254                                         thread_id, now, flags,
   1255                                         arg1_name, arg1_val.Pass(),
   1256                                         arg2_name, arg2_val);
   1257 }
   1258 
   1259 static inline void AddTraceEventWithThreadIdAndTimestamp(
   1260     char phase,
   1261     const unsigned char* category_group_enabled,
   1262     const char* name,
   1263     unsigned long long id,
   1264     int thread_id,
   1265     const base::TimeTicks& timestamp,
   1266     unsigned char flags,
   1267     const char* arg1_name,
   1268     scoped_ptr<base::debug::ConvertableToTraceFormat> arg1_val,
   1269     const char* arg2_name,
   1270     scoped_ptr<base::debug::ConvertableToTraceFormat> arg2_val) {
   1271   const int num_args = 2;
   1272   const char* arg_names[2] = { arg1_name, arg2_name };
   1273   unsigned char arg_types[2] =
   1274       { TRACE_VALUE_TYPE_CONVERTABLE, TRACE_VALUE_TYPE_CONVERTABLE };
   1275   scoped_ptr<base::debug::ConvertableToTraceFormat> convertable_values[2];
   1276   convertable_values[0].reset(arg1_val.release());
   1277   convertable_values[1].reset(arg2_val.release());
   1278 
   1279   TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
   1280       phase, category_group_enabled, name, id, thread_id, timestamp,
   1281       num_args, arg_names, arg_types, NULL, convertable_values, flags);
   1282 }
   1283 
   1284 static inline void AddTraceEvent(
   1285     char phase,
   1286     const unsigned char* category_group_enabled,
   1287     const char* name,
   1288     unsigned long long id,
   1289     unsigned char flags,
   1290     const char* arg1_name,
   1291     scoped_ptr<base::debug::ConvertableToTraceFormat> arg1_val,
   1292     const char* arg2_name,
   1293     scoped_ptr<base::debug::ConvertableToTraceFormat> arg2_val) {
   1294   int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
   1295   base::TimeTicks now = base::TimeTicks::NowFromSystemTraceTime();
   1296   AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled, name, id,
   1297                                         thread_id, now, flags,
   1298                                         arg1_name, arg1_val.Pass(),
   1299                                         arg2_name, arg2_val.Pass());
   1300 }
   1301 
   1302 static inline void AddTraceEventWithThreadIdAndTimestamp(
   1303     char phase,
   1304     const unsigned char* category_group_enabled,
   1305     const char* name,
   1306     unsigned long long id,
   1307     int thread_id,
   1308     const base::TimeTicks& timestamp,
   1309     unsigned char flags) {
   1310   TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
   1311       phase, category_group_enabled, name, id, thread_id, timestamp,
   1312       kZeroNumArgs, NULL, NULL, NULL, NULL, flags);
   1313 }
   1314 
   1315 static inline void AddTraceEvent(char phase,
   1316                                  const unsigned char* category_group_enabled,
   1317                                  const char* name,
   1318                                  unsigned long long id,
   1319                                  unsigned char flags) {
   1320   int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
   1321   base::TimeTicks now = base::TimeTicks::NowFromSystemTraceTime();
   1322   AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled, name, id,
   1323       thread_id, now, flags);
   1324 }
   1325 
   1326 template<class ARG1_TYPE>
   1327 static inline void AddTraceEventWithThreadIdAndTimestamp(
   1328     char phase,
   1329     const unsigned char* category_group_enabled,
   1330     const char* name,
   1331     unsigned long long id,
   1332     int thread_id,
   1333     const base::TimeTicks& timestamp,
   1334     unsigned char flags,
   1335     const char* arg1_name,
   1336     const ARG1_TYPE& arg1_val) {
   1337   const int num_args = 1;
   1338   unsigned char arg_types[1];
   1339   unsigned long long arg_values[1];
   1340   SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
   1341   TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
   1342       phase, category_group_enabled, name, id, thread_id, timestamp,
   1343       num_args, &arg1_name, arg_types, arg_values, NULL, flags);
   1344 }
   1345 
   1346 template<class ARG1_TYPE>
   1347 static inline void AddTraceEvent(char phase,
   1348                                  const unsigned char* category_group_enabled,
   1349                                  const char* name,
   1350                                  unsigned long long id,
   1351                                  unsigned char flags,
   1352                                  const char* arg1_name,
   1353                                  const ARG1_TYPE& arg1_val) {
   1354   int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
   1355   base::TimeTicks now = base::TimeTicks::NowFromSystemTraceTime();
   1356   AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled, name, id,
   1357                                         thread_id, now, flags, arg1_name,
   1358                                         arg1_val);
   1359 }
   1360 
   1361 template<class ARG1_TYPE, class ARG2_TYPE>
   1362 static inline void AddTraceEventWithThreadIdAndTimestamp(
   1363     char phase,
   1364     const unsigned char* category_group_enabled,
   1365     const char* name,
   1366     unsigned long long id,
   1367     int thread_id,
   1368     const base::TimeTicks& timestamp,
   1369     unsigned char flags,
   1370     const char* arg1_name,
   1371     const ARG1_TYPE& arg1_val,
   1372     const char* arg2_name,
   1373     const ARG2_TYPE& arg2_val) {
   1374   const int num_args = 2;
   1375   const char* arg_names[2] = { arg1_name, arg2_name };
   1376   unsigned char arg_types[2];
   1377   unsigned long long arg_values[2];
   1378   SetTraceValue(arg1_val, &arg_types[0], &arg_values[0]);
   1379   SetTraceValue(arg2_val, &arg_types[1], &arg_values[1]);
   1380   TRACE_EVENT_API_ADD_TRACE_EVENT_WITH_THREAD_ID_AND_TIMESTAMP(
   1381       phase, category_group_enabled, name, id, thread_id, timestamp,
   1382       num_args, arg_names, arg_types, arg_values, NULL, flags);
   1383 }
   1384 
   1385 template<class ARG1_TYPE, class ARG2_TYPE>
   1386 static inline void AddTraceEvent(char phase,
   1387                                 const unsigned char* category_group_enabled,
   1388                                 const char* name,
   1389                                 unsigned long long id,
   1390                                 unsigned char flags,
   1391                                 const char* arg1_name,
   1392                                 const ARG1_TYPE& arg1_val,
   1393                                 const char* arg2_name,
   1394                                 const ARG2_TYPE& arg2_val) {
   1395   int thread_id = static_cast<int>(base::PlatformThread::CurrentId());
   1396   base::TimeTicks now = base::TimeTicks::NowFromSystemTraceTime();
   1397   AddTraceEventWithThreadIdAndTimestamp(phase, category_group_enabled, name, id,
   1398                                         thread_id, now, flags, arg1_name,
   1399                                         arg1_val, arg2_name, arg2_val);
   1400 }
   1401 
   1402 // Used by TRACE_EVENTx macro. Do not use directly.
   1403 class TRACE_EVENT_API_CLASS_EXPORT TraceEndOnScopeClose {
   1404  public:
   1405   // Note: members of data_ intentionally left uninitialized. See Initialize.
   1406   TraceEndOnScopeClose() : p_data_(NULL) {}
   1407   ~TraceEndOnScopeClose() {
   1408     if (p_data_)
   1409       AddEventIfEnabled();
   1410   }
   1411 
   1412   void Initialize(const unsigned char* category_group_enabled,
   1413                   const char* name) {
   1414     data_.category_group_enabled = category_group_enabled;
   1415     data_.name = name;
   1416     p_data_ = &data_;
   1417   }
   1418 
   1419  private:
   1420   // Add the end event if the category is still enabled.
   1421   void AddEventIfEnabled() {
   1422     // Only called when p_data_ is non-null.
   1423     if (*p_data_->category_group_enabled) {
   1424       TRACE_EVENT_API_ADD_TRACE_EVENT(
   1425           TRACE_EVENT_PHASE_END,            // phase
   1426           p_data_->category_group_enabled,  // category enabled
   1427           p_data_->name,                    // name
   1428           kNoEventId,                       // id
   1429           kZeroNumArgs,                     // num_args
   1430           NULL,                             // arg_names
   1431           NULL,                             // arg_types
   1432           NULL,                             // arg_values
   1433           NULL,                             // convertable_values
   1434           TRACE_EVENT_FLAG_NONE);           // flags
   1435     }
   1436   }
   1437 
   1438   // This Data struct workaround is to avoid initializing all the members
   1439   // in Data during construction of this object, since this object is always
   1440   // constructed, even when tracing is disabled. If the members of Data were
   1441   // members of this class instead, compiler warnings occur about potential
   1442   // uninitialized accesses.
   1443   struct Data {
   1444     const unsigned char* category_group_enabled;
   1445     const char* name;
   1446   };
   1447   Data* p_data_;
   1448   Data data_;
   1449 };
   1450 
   1451 // Used by TRACE_EVENT_BINARY_EFFICIENTx macro. Do not use directly.
   1452 class TRACE_EVENT_API_CLASS_EXPORT ScopedTrace {
   1453  public:
   1454   ScopedTrace(TRACE_EVENT_API_ATOMIC_WORD* event_uid, const char* name);
   1455   ~ScopedTrace();
   1456 
   1457  private:
   1458   const unsigned char* category_group_enabled_;
   1459   const char* name_;
   1460 };
   1461 
   1462 // A support macro for TRACE_EVENT_BINARY_EFFICIENTx
   1463 #define INTERNAL_TRACE_EVENT_BINARY_EFFICIENT_ADD_SCOPED( \
   1464     category_group, name, ...) \
   1465     static TRACE_EVENT_API_ATOMIC_WORD INTERNAL_TRACE_EVENT_UID(atomic) = 0; \
   1466     trace_event_internal::ScopedTrace \
   1467         INTERNAL_TRACE_EVENT_UID(profileScope)( \
   1468             &INTERNAL_TRACE_EVENT_UID(atomic), name); \
   1469 
   1470 // This macro generates less code then TRACE_EVENT0 but is also
   1471 // slower to execute when tracing is off. It should generally only be
   1472 // used with code that is seldom executed or conditionally executed
   1473 // when debugging.
   1474 #define TRACE_EVENT_BINARY_EFFICIENT0(category_group, name) \
   1475     INTERNAL_TRACE_EVENT_BINARY_EFFICIENT_ADD_SCOPED(category_group, name)
   1476 
   1477 // TraceEventSamplingStateScope records the current sampling state
   1478 // and sets a new sampling state. When the scope exists, it restores
   1479 // the sampling state having recorded.
   1480 template<size_t BucketNumber>
   1481 class TraceEventSamplingStateScope {
   1482  public:
   1483   TraceEventSamplingStateScope(const char* category_and_name) {
   1484     previous_state_ = TraceEventSamplingStateScope<BucketNumber>::Current();
   1485     TraceEventSamplingStateScope<BucketNumber>::Set(category_and_name);
   1486   }
   1487 
   1488   ~TraceEventSamplingStateScope() {
   1489     TraceEventSamplingStateScope<BucketNumber>::Set(previous_state_);
   1490   }
   1491 
   1492   static inline const char* Current() {
   1493     return reinterpret_cast<const char*>(TRACE_EVENT_API_ATOMIC_LOAD(
   1494       g_trace_state[BucketNumber]));
   1495   }
   1496 
   1497   static inline void Set(const char* category_and_name) {
   1498     TRACE_EVENT_API_ATOMIC_STORE(
   1499       g_trace_state[BucketNumber],
   1500       reinterpret_cast<TRACE_EVENT_API_ATOMIC_WORD>(
   1501         const_cast<char*>(category_and_name)));
   1502   }
   1503 
   1504  private:
   1505   const char* previous_state_;
   1506 };
   1507 
   1508 }  // namespace trace_event_internal
   1509 
   1510 namespace base {
   1511 namespace debug {
   1512 
   1513 template<typename IDType> class TraceScopedTrackableObject {
   1514  public:
   1515   TraceScopedTrackableObject(const char* category_group, const char* name,
   1516       IDType id)
   1517     : category_group_(category_group),
   1518       name_(name),
   1519       id_(id) {
   1520     TRACE_EVENT_OBJECT_CREATED_WITH_ID(category_group_, name_, id_);
   1521   }
   1522 
   1523   template <typename ArgType> void snapshot(ArgType snapshot) {
   1524     TRACE_EVENT_OBJECT_SNAPSHOT_WITH_ID(category_group_, name_, id_, snapshot);
   1525   }
   1526 
   1527   ~TraceScopedTrackableObject() {
   1528     TRACE_EVENT_OBJECT_DELETED_WITH_ID(category_group_, name_, id_);
   1529   }
   1530 
   1531  private:
   1532   const char* category_group_;
   1533   const char* name_;
   1534   IDType id_;
   1535 
   1536   DISALLOW_COPY_AND_ASSIGN(TraceScopedTrackableObject);
   1537 };
   1538 
   1539 } // namespace debug
   1540 } // namespace base
   1541 
   1542 #endif /* BASE_DEBUG_TRACE_EVENT_H_ */
   1543