1// Copyright 2016 The Fuchsia 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#include <fcntl.h>
6#include <stdio.h>
7#include <unistd.h>
8
9#include <zircon/device/ktrace.h>
10
11// 1. Run:            zircon> traceme
12// 2. Stop tracing:   zircon> dm ktraceoff
13// 3. Grab trace:     host> netcp :/dev/misc/ktrace test.trace
14// 4. Examine trace:  host> tracevic test.trace
15
16int main(int argc, char** argv) {
17    int fd;
18    if ((fd = open("/dev/misc/ktrace", O_RDWR)) < 0) {
19        fprintf(stderr, "cannot open trace device\n");
20        return -1;
21    }
22
23    // obtain the handle needed to emit probes
24    zx_handle_t kth;
25    if (ioctl_ktrace_get_handle(fd, &kth) < 0) {
26        fprintf(stderr, "cannot get ktrace handle\n");
27        return -1;
28    }
29
30    // for each probe/event, register its name and get its id
31    uint32_t id;
32    if (ioctl_ktrace_add_probe(fd, "trace-me", &id) < 0) {
33        fprintf(stderr, "cannot register ktrace probe\n");
34        return -1;
35    }
36
37    // once all probes are registered, you can close the device
38    close(fd);
39
40    // use the ktrace handle to emit probes into the trace stream
41    zx_ktrace_write(kth, id, 1, 0);
42    printf("hello, ktrace! id = %u\n", id);
43    zx_ktrace_write(kth, id, 2, 0);
44
45    return 0;
46}
47
48