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 #pragma once
6 
7 #include <stdint.h>
8 #include <assert.h>
9 #include <zircon/compiler.h>
10 
11 __BEGIN_CDECLS
12 
13 // clang-format off
14 
15 // KTRACE TAG 0xGGGEEEXZ
16 //
17 // 12 group flags
18 // 12 event id bits
19 //  4 spare bits
20 //  4 bit size (in uint64_t units)
21 
22 #define KTRACE_TAG(evt,grp,siz)   ((((grp)&0xFFF)<<20)|(((evt)&0xFFF)<<8)|(((siz)>>3)&0x0F))
23 
24 #define KTRACE_TAG_16B(e,g)       KTRACE_TAG(e,g,16)
25 #define KTRACE_TAG_32B(e,g)       KTRACE_TAG(e,g,32)
26 #define KTRACE_TAG_NAME(e,g)      KTRACE_TAG(e,g,48)
27 
28 #define KTRACE_LEN(tag)           (((tag)&0xF)<<3)
29 #define KTRACE_GROUP(tag)         (((tag)>>20)&0xFFF)
30 #define KTRACE_EVENT(tag)         (((tag)>>8)&0xFFF)
31 
32 #define KTRACE_HDRSIZE            (16)
33 #define KTRACE_RECSIZE            (32)
34 #define KTRACE_NAMESIZE           (12)
35 #define KTRACE_NAMEOFF            (8)
36 
37 #define KTRACE_VERSION            (0x00020000)
38 
39 // Filter Groups
40 #define KTRACE_GRP_ALL            0xFFF
41 #define KTRACE_GRP_META           0x001
42 #define KTRACE_GRP_LIFECYCLE      0x002
43 #define KTRACE_GRP_SCHEDULER      0x004
44 #define KTRACE_GRP_TASKS          0x008
45 #define KTRACE_GRP_IPC            0x010
46 #define KTRACE_GRP_IRQ            0x020
47 #define KTRACE_GRP_PROBE          0x040
48 #define KTRACE_GRP_ARCH           0x080
49 
50 #define KTRACE_GRP_TO_MASK(grp)   ((grp) << 20)
51 
52 typedef struct ktrace_header {
53     uint32_t tag;
54     uint32_t tid;
55     uint64_t ts;
56 } ktrace_header_t;
57 
58 static_assert(sizeof(ktrace_header_t) == KTRACE_HDRSIZE,
59               "ktrace_header_t is not KTRACE_HDRSIZE bytes");
60 
61 typedef struct ktrace_rec_32b {
62     uint32_t tag;
63     uint32_t tid;
64     uint64_t ts;
65     uint32_t a;
66     uint32_t b;
67     uint32_t c;
68     uint32_t d;
69 } ktrace_rec_32b_t;
70 
71 typedef struct ktrace_rec_name {
72     uint32_t tag;
73     uint32_t id;
74     uint32_t arg;
75     char name[1];
76 } ktrace_rec_name_t;
77 
78 #define KTRACE_DEF(num,type,name,group) TAG_##name = KTRACE_TAG_##type(num,KTRACE_GRP_##group),
79 enum {
80 #include <lib/zircon-internal/ktrace-def.h>
81 };
82 
83 #define TAG_PROBE_16(n) KTRACE_TAG(((n)|0x800),KTRACE_GRP_PROBE,16)
84 #define TAG_PROBE_24(n) KTRACE_TAG(((n)|0x800),KTRACE_GRP_PROBE,24)
85 
86 // Actions for ktrace control
87 #define KTRACE_ACTION_START     1 // options = grpmask, 0 = all
88 #define KTRACE_ACTION_STOP      2 // options ignored
89 #define KTRACE_ACTION_REWIND    3 // options ignored
90 #define KTRACE_ACTION_NEW_PROBE 4 // options ignored, ptr = name
91 
92 __END_CDECLS
93