Registering a trace provider

Trace providers must register with the trace manager in order to participate in tracing. Drivers don’t have to register as a trace provider since the devhost process does it through libdriver.so.

To register a trace provider, you must do the following:

Note: For more information on the Fuchsia tracing system, see Fuchsia tracing system.

Register with the trace manager {#register-with-the-trace-manager}

To register as a trace provider, you can use the libtrace-provider library to provide an asynchronous loop in your component’s code.

Note: For more information on tracing libraries, see Tracing libraries.

For example:

  • {C++}

    Note: This example uses fdio to set up the FIDL channel with Trace Manager. For more information, see fdio.

    1. #include <lib/async-loop/cpp/loop.h>
    2. #include <lib/async-loop/default.h>
    3. #include <lib/trace-provider/provider.h>
    4. // further includes
    5. int main(int argc, const char** argv) {
    6. // process argv
    7. async::Loop loop(&kAsyncLoopConfigAttachToCurrentThread);
    8. trace::TraceProviderWithFdio trace_provider(
    9. loop.dispatcher(), "my_trace_provider");
    10. // further setup
    11. loop.Run();
    12. return 0;
    13. }
  • {C }

    1. #include <lib/async-loop/cpp/loop.h>
    2. #include <lib/async-loop/default.h>
    3. #include <lib/trace-provider/provider.h>
    4. int main(int argc, char** argv) {
    5. zx_status_t status;
    6. async_loop_t* loop;
    7. trace_provider_t* trace_provider;
    8. // Create a message loop.
    9. status = async_loop_create(&kAsyncLoopConfigNoAttachToCurrentThread, &loop);
    10. if (status != ZX_OK) exit(1);
    11. // Start a thread for the loop to run on.
    12. // Alternatively, use async_loop_run() to run on the current thread.
    13. status = async_loop_start_thread(loop, "loop", NULL);
    14. if (status != ZX_OK) exit(1);
    15. // Create the trace provider.
    16. async_dispatcher_t* dispatcher = async_loop_get_dispatcher(loop);
    17. trace_provider = trace_provider_create(dispatcher);
    18. if (!trace_provider) exit(1);
    19. // Do something...
    20. // Tear down.
    21. trace_provider_destroy(trace_provider);
    22. async_loop_shutdown(loop);
    23. return 0;
    24. }

Give the trace manager component access {#give-trace-manager-component-access}

In the component manifest file (a .cmx file) of your component, you must specify that it needs to communicate with the Fuchsia trace manager.

Note: For information on component manifests, see Component Manifests.

To give the trace manager component access, specify fuchsia.tracing.provider.Registry as a “services”. For example:

  • {.cmx file}

    1. {
    2. "program": {
    3. "binary": "bin/app"
    4. },
    5. "sandbox": {
    6. "services": [
    7. "fuchsia.tracing.provider.Registry"
    8. ]
    9. }
    10. }

Once you have registered your component as a trace provider, you can enable tracing in your code. For more information, see Adding tracing in your code.