跟踪:C 和 C++ 宏

之后,您可以使用这些 C 和 C++ 宏 您已将自己的组件设为跟踪提供程序。如需更多信息 请参阅在代码中添加跟踪功能

这些宏在 //zircon/system/ulib/trace/include/lib/trace/internal/event_common.h

对宏进行编码

如果要使用跟踪宏录制 value,请务必 正确编码每个值。根据以下内容使用相应的宏: 要记录的数据类型。

微距 说明
TA_NULL

对于 C++ 是可选的

一个 NULL 值。

此宏不接受任何参数。你可以将其用作 TA_NULL()

在 C++ 中,您也可以使用 nullptr

TA_BOOL

对于 C++ 是可选的

布尔值。

TA_INT32

对于 C++ 是可选的

一个带符号的 32 位整数值。

TA_UINT32

对于 C++ 是可选的

一个无符号的 32 位整数值。

TA_INT64

对于 C++ 是可选的

一个带符号的 64 位整数值。

TA_UINT64

对于 C++ 是可选的

一个无符号的 32 位整数值。

TA_DOUBLE

对于 C++ 是可选的

双精度浮点值。

TA_CHAR_ARRAY

对于 C++ 是必需的

具有一定长度的字符数组,用于复制而不是缓存。

该宏有两个参数。第一个参数是指向 字符数组。第二个参数是数组的长度。

TA_STRING

对于 C++ 是可选的

以 NULL 结尾的动态字符串,系统会复制它,而不是缓存它。

TA_STRING_LITERAL

对于 C++ 是必需的

以 NULL 结尾的动态字符串(已缓存)。

TA_POINTER

对于 C++ 是可选的

用于记录内存地址的指针值 而非目标。

TA_KOID

对于 C++ 是必需的

内核对象 ID。如需更多信息 请参阅 Zircon 内核对象

C++ 备注

在 C++ 中,当您使用字面量常量时,类型推断需要提示 以确保文字大小、符号和类型都正确无误。

例如,值 77 是否为 32 位有符号整数?无符号 32 位 整数?甚至可能是某种类型的 64 位整数?

跟踪宏中的类型推断根据标准 C++ 规则运作:

  • 77 是一个有符号的 32 位整数 TA_INT32
  • 77U 是一个无符号 32 位整数 TA_UINT32
  • 77L 是一个有符号的 64 位整数 TA_INT64
  • 77LU 是一个无符号 64 位整数 TA_UINT64

这也意味着,如果浮点是 整数值。

例如:

  • 77的会员级别为TA_INT32
  • 77.的会员级别为TA_DOUBLE

如果使用常量,则应考虑保留 如果您要直接表示这些值,则应使用 相应的 const 类型。以下示例都是一样的 而是展示如何使用明确定义的类型、类型宏以及不推荐的做法。

定义的类型

const int32_t my_id = 77;                       // well defined type
TRACE_INSTANT("category", "name", "int", my_id);

类型宏

#define MY_ID   (TA_INT32(77))                  // uses the typing macro
TRACE_INSTANT("category", "name", "int", MY_ID);

不建议使用

TRACE_INSTANT("category", "name", "int", 77);   // discouraged

TRACE_ENABLED

 Returns true if tracing is enabled.

 Usage:

     if (TRACE_ENABLED()) {
         // do something possibly expensive only when tracing is enabled
     }

TRACE_CATEGORY_ENABLED

 Returns true if tracing of the specified category has been enabled (which
 implies that |TRACE_ENABLED()| is also true).

 |category_literal| must be a null-terminated static string constant.

 Usage:

     if (TRACE_CATEGORY_ENABLED("category")) {
         // do something possibly expensive only when tracing this category
     }

TRACE_NONCE

 Returns a new unique 64-bit unsigned integer (within this process).
 Each invocation returns a different non-zero value.
 Useful for generating identifiers for async and flow events.

 Usage:

     trace_async_id_t async_id = TRACE_NONCE();
     TRACE_ASYNC_BEGIN("category", "name", async_id);
     // a little while later...
     TRACE_ASYNC_END("category", "name", async_id);

TRACE_INSTANT

 Writes an instant event representing a single moment in time (a probe).

 0 to 15 arguments can be associated with the event, each of which is used
 to annotate the moment with additional information.

 |category_literal| and |name_literal| must be null-terminated static string constants.
 |scope| is |TRACE_SCOPE_THREAD|, |TRACE_SCOPE_PROCESS|, or |TRACE_SCOPE_GLOBAL|.
 |args| is the list of argument key/value pairs.

 Usage:

     TRACE_INSTANT("category", "name", TRACE_SCOPE_PROCESS, "x", TA_INT32(42));

TRACE_COUNTER

 Writes a counter event with the specified id.

 The arguments to this event are numeric samples are typically represented by
 the visualizer as a stacked area chart.  The id serves to distinguish multiple
 instances of counters that share the same category and name within the
 same process.

 1 to 15 numeric arguments can be associated with the event, each of which is
 interpreted as a distinct time series.

 |category_literal| and |name_literal| must be null-terminated static string constants.
 |counter_id| is the correlation id of the counter.
              Must be unique for a given process, category, and name combination.
 |args| is the list of argument key/value pairs.

 Usage:

     trace_counter_id_t counter_id = 555;
     TRACE_COUNTER("category", "name", counter_id, "x", TA_INT32(42), "y", TA_DOUBLE(2.0))

TRACE_DURATION

 Writes a duration event that ends when the current scope exits.

 Durations describe work that is happening synchronously on one thread.
 They can be nested to represent a control flow stack.

 0 to 15 arguments can be associated with the event, each of which is used
 to annotate the duration with additional information.

 |category_literal| and |name_literal| must be null-terminated static string constants.
 |args| is the list of argument key/value pairs.

 Usage:

     void function(int arg) {
         TRACE_DURATION("category", "name", "arg", TA_INT32(arg));
         // do something useful here
     }

TRACE_DURATION_BEGIN

 Writes a duration begin event only.
 This event must be matched by a duration end event with the same category and name.

 Durations describe work that is happening synchronously on one thread.
 They can be nested to represent a control flow stack.

 0 to 15 arguments can be associated with the event, each of which is used
 to annotate the duration with additional information.  The arguments provided
 to matching duration begin and duration end events are combined together in
 the trace; it is not necessary to repeat them.

 |category_literal| and |name_literal| must be null-terminated static string constants.
 |args| is the list of argument key/value pairs.

 Usage:

     TRACE_DURATION_BEGIN("category", "name", "x", TA_INT32(42));

TRACE_DURATION_END

 Writes a duration end event only.

 Durations describe work that is happening synchronously on one thread.
 They can be nested to represent a control flow stack.

 0 to 15 arguments can be associated with the event, each of which is used
 to annotate the duration with additional information.  The arguments provided
 to matching duration begin and duration end events are combined together in
 the trace; it is not necessary to repeat them.

 |category_literal| and |name_literal| must be null-terminated static string constants.
 |args| is the list of argument key/value pairs.

 Usage:

     TRACE_DURATION_END("category", "name", "x", TA_INT32(42));

TRACE_ASYNC_BEGIN

 Writes an asynchronous begin event with the specified id.
 This event may be followed by async instant events and must be matched by
 an async end event with the same category, name, and id.

 Asynchronous events describe work that is happening asynchronously and that
 may span multiple threads.  Asynchronous events do not nest.  The id serves
 to correlate the progress of distinct asynchronous operations that share
 the same category and name within the same process.

 0 to 15 arguments can be associated with the event, each of which is used
 to annotate the asynchronous operation with additional information.  The
 arguments provided to matching async begin, async instant, and async end
 events are combined together in the trace; it is not necessary to repeat them.

 |category_literal| and |name_literal| must be null-terminated static string constants.
 |async_id| is the correlation id of the asynchronous operation.
            Must be unique for a given process, category, and name combination.
 |args| is the list of argument key/value pairs.

 Usage:

     trace_async_id_t async_id = 555;
     TRACE_ASYNC_BEGIN("category", "name", async_id, "x", TA_INT32(42));

TRACE_ASYNC_INSTANT

 Writes an asynchronous instant event with the specified id.

 Asynchronous events describe work that is happening asynchronously and that
 may span multiple threads.  Asynchronous events do not nest.  The id serves
 to correlate the progress of distinct asynchronous operations that share
 the same category and name within the same process.

 0 to 15 arguments can be associated with the event, each of which is used
 to annotate the asynchronous operation with additional information.  The
 arguments provided to matching async begin, async instant, and async end
 events are combined together in the trace; it is not necessary to repeat them.

 |category_literal| and |name_literal| must be null-terminated static string constants.
 |async_id| is the correlation id of the asynchronous operation.
            Must be unique for a given process, category, and name combination.
 |args| is the list of argument key/value pairs.

 Usage:

     trace_async_id_t async_id = 555;
     TRACE_ASYNC_INSTANT("category", "name", async_id, "x", TA_INT32(42));

TRACE_ASYNC_END

 Writes an asynchronous end event with the specified id.

 Asynchronous events describe work that is happening asynchronously and that
 may span multiple threads.  Asynchronous events do not nest.  The id serves
 to correlate the progress of distinct asynchronous operations that share
 the same category and name within the same process.

 0 to 15 arguments can be associated with the event, each of which is used
 to annotate the asynchronous operation with additional information.  The
 arguments provided to matching async begin, async instant, and async end
 events are combined together in the trace; it is not necessary to repeat them.

 |category_literal| and |name_literal| must be null-terminated static string constants.
 |async_id| is the correlation id of the asynchronous operation.
            Must be unique for a given process, category, and name combination.
 |args| is the list of argument key/value pairs.

 Usage:

     trace_async_id_t async_id = 555;
     TRACE_ASYNC_END("category", "name", async_id, "x", TA_INT32(42));

TRACE_FLOW_BEGIN

 Writes a flow begin event with the specified id.
 This event may be followed by flow steps events and must be matched by
 a flow end event with the same category, name, and id.

 Flow events describe control flow handoffs between threads or across processes.
 They are typically represented as arrows in a visualizer.  Flow arrows are
 from the end of the duration event that encloses the beginning of the flow
 to the beginning of the duration event that encloses the next step or the
 end of the flow.  The id serves to correlate flows that share the same
 category and name across processes.

 This event must be enclosed in a duration event that represents where
 the flow handoff occurs.

 0 to 15 arguments can be associated with the event, each of which is used
 to annotate the flow with additional information.  The arguments provided
 to matching flow begin, flow step, and flow end events are combined together
 in the trace; it is not necessary to repeat them.

 |category_literal| and |name_literal| must be null-terminated static string constants.
 |flow_id| is the correlation id of the flow.
           Must be unique for a given category and name combination.
 |args| is the list of argument key/value pairs.

 Usage:

     trace_flow_id_t flow_id = 555;
     TRACE_FLOW_BEGIN("category", "name", flow_id, "x", TA_INT32(42));

TRACE_FLOW_STEP

 Writes a flow step event with the specified id.

 Flow events describe control flow handoffs between threads or across processes.
 They are typically represented as arrows in a visualizer.  Flow arrows are
 from the end of the duration event that encloses the beginning of the flow
 to the beginning of the duration event that encloses the next step or the
 end of the flow.  The id serves to correlate flows that share the same
 category and name across processes.

 This event must be enclosed in a duration event that represents where
 the flow handoff occurs.

 0 to 15 arguments can be associated with the event, each of which is used
 to annotate the flow with additional information.  The arguments provided
 to matching flow begin, flow step, and flow end events are combined together
 in the trace; it is not necessary to repeat them.

 |category_literal| and |name_literal| must be null-terminated static string constants.
 |flow_id| is the correlation id of the flow.
           Must be unique for a given category and name combination.
 |args| is the list of argument key/value pairs.

 Usage:

     trace_flow_id_t flow_id = 555;
     TRACE_FLOW_STEP("category", "name", flow_id, "x", TA_INT32(42));

TRACE_FLOW_END

 Writes a flow end event with the specified id.

 Flow events describe control flow handoffs between threads or across processes.
 They are typically represented as arrows in a visualizer.  Flow arrows are
 from the end of the duration event that encloses the beginning of the flow
 to the beginning of the duration event that encloses the next step or the
 end of the flow.  The id serves to correlate flows that share the same
 category and name across processes.

 This event must be enclosed in a duration event that represents where
 the flow handoff occurs.

 0 to 15 arguments can be associated with the event, each of which is used
 to annotate the flow with additional information.  The arguments provided
 to matching flow begin, flow step, and flow end events are combined together
 in the trace; it is not necessary to repeat them.

 |category_literal| and |name_literal| must be null-terminated static string constants.
 |flow_id| is the correlation id of the flow.
           Must be unique for a given category and name combination.
 |args| is the list of argument key/value pairs.

 Usage:

     trace_flow_id_t id = 555;
     TRACE_FLOW_END("category", "name", flow_id, "x", TA_INT32(42));

TRACE_BLOB_EVENT

 Writes a large blob record with the given blob data and metadata.
 Here metadata includes timestamp, thread and process information, and arguments,
 which is what most event records contain.

 Blobs that exceed |TRACE_ENCODED_RECORD_MAX_TOTAL_LENGTH| will be silently
 ignored, as will blobs that cannot fit within the remaining space in the
 trace buffer.

 |category_literal| and |name_literal| must be null-terminated static string constants.
 |blob| is a pointer to the data.
 |blob_size| is the size, in bytes, of the data.

TRACE_BLOB_ATTACHMENT

 Writes a large blob record with the given blob data, with only a
 category and name associated with the blob. This will not contain much
 additional metadata. This means timestamp, thread and process information,
 and arguments are not included with the record.

 Blobs that exceed |TRACE_ENCODED_RECORD_MAX_TOTAL_LENGTH| will be silently
 ignored, as will blobs that cannot fit within the remaining space in the
 trace buffer.

 |category_literal| and |name_literal| must be null-terminated static string constants.
 |blob| is a pointer to the data.
 |blob_size| is the size, in bytes, of the data.

TRACE_KERNEL_OBJECT

 Writes a description of a kernel object indicated by |handle|,
 including its koid, name, and the supplied arguments.

 0 to 15 arguments can be associated with the record, each of which is used
 to annotate the handle with additional information.

 |handle| is the handle of the object being described.
 |args| is the list of argument key/value pairs.

 Usage:

     zx_handle_t handle = ...;
     TRACE_KERNEL_OBJECT(handle, "description", TA_STRING("some object"));