This document describes how to add tracing to a driver in a Fuchsia system.

## Overview

For an overview of tracing, see [Fuchsia tracing system](https://fuchsia.dev/docs/concepts/kernel/tracing-system).
However, drivers don't need to specify a [trace provider](https://fuchsia.dev/docs/concepts/kernel/tracing-system#trace-providers). The
driver host provides one for its drivers.

For a tutorial on tracing, see [Add tracing in your code](https://fuchsia.dev/docs/development/tracing/tutorial/add-tracing-in-code).
And for the tracing API reference, see [Tracing: C and C++ macros](https://fuchsia.dev/docs/reference/tracing/c_cpp_macros).

## Add trace records

To add tracing a driver, you need to update the driver's source code
and `BUILD.gn` file.

### Source code update

To add trace records in a driver component, update the source code to invoke
the `TRACE_*()` macros from `lib/trace/event.h`, for example:

    #include <lib/trace/event.h>

    void DoSomething(int a, std::string b) {
      TRACE_DURATION("example", "DoSomething", "a", a, "b", b);

      // Do something
    }

The first two arguments to most macros are the trace category and the
event name. In this example, they are `example` and `DoSomething`
respectively.

Trace categories let you specify what types of data the tracing system
collects. If a category is not requested, no data is collected. Categories
don't need to be unique across the driver. One typically groups several
events under the same category.

An event name is included in the trace to describe the event. It is
typically unique for each event.

### BUILD.gn update

To pick up tracing support, add the following target to your driver's
`BUILD.gn`:

    fuchsia_cc_driver("my_driver") {
      deps = [
        ...
        "//zircon/system/ulib/trace",
      ]
    }

## Use tracing

Use [`ffx trace`](https://fuchsia.dev/docs/development/tools/ffx/workflows/record-traces) to record a trace and view the result with the
[Perfetto viewer](https://ui.perfetto.dev/#!/).

The example command below uses the `example` category described in the
[Source code update](https://fuchsia.dev/fuchsia-src/development/drivers/diagnostics/tracing#source-code-update) section above:

    $ ffx trace start --categories "example,kernel:sched,kernel:meta"

The `kernel:sched,kernel:meta` categories need to be present if you
want to visualize the result. The visualizer wants to associate trace data
with threads and processes, and it needs the data provided by the kernel
through these categories.

For additional details, as well as instructions on tracing directly on a
Fuchsia device without `ffx`, see
[Record traces for performance analysis](https://fuchsia.dev/docs/development/tools/ffx/workflows/record-traces).

## Further reading

See [Fuchsia tracing guides](https://fuchsia.dev/docs/development/tracing/README) for more information.
[](https://issues.fuchsia.dev/issues/new?component=1439230&template=1881640&description=Issue+on:+https://fuchsia.dev/docs/development/drivers/diagnostics/tracing.md "Report a bug or suggest improvements on this page") [](https://cs.opensource.google/fuchsia/fuchsia/+/main:docs/development/drivers/diagnostics/tracing.md "View the source code of this page") [](https://ci.android.com/edit?repo=fuchsia/fuchsia/main&file=docs/development/drivers/diagnostics/tracing.md&cloneRepo=fuchsia/fuchsia:refs/heads/main&fullPath=fuchsia/docs/development/drivers/diagnostics/tracing.md "Edit the source code of this page")