1 // Copyright 2016 The Fuchsia Authors
2 //
3 // Use of this source code is governed by a MIT-style
4 // license that can be found in the LICENSE file or at
5 // https://opensource.org/licenses/MIT
6 
7 #pragma once
8 
9 #include <err.h>
10 #include <lib/zircon-internal/ktrace.h>
11 #include <zircon/compiler.h>
12 #include <zircon/types.h>
13 
14 __BEGIN_CDECLS
15 
16 typedef struct ktrace_probe_info ktrace_probe_info_t;
17 
18 struct ktrace_probe_info {
19     ktrace_probe_info_t* next;
20     const char* name;
21     uint32_t num;
22 } __ALIGNED(16); // align on multiple of 16 to match linker packing of the ktrace_probe section
23 
24 void* ktrace_open(uint32_t tag);
25 void ktrace_tiny(uint32_t tag, uint32_t arg);
ktrace(uint32_t tag,uint32_t a,uint32_t b,uint32_t c,uint32_t d)26 static inline void ktrace(uint32_t tag, uint32_t a, uint32_t b, uint32_t c, uint32_t d) {
27     uint32_t* data = (uint32_t*) ktrace_open(tag);
28     if (data) {
29         data[0] = a; data[1] = b; data[2] = c; data[3] = d;
30     }
31 }
32 
ktrace_ptr(uint32_t tag,const void * ptr,uint32_t c,uint32_t d)33 static inline void ktrace_ptr(uint32_t tag, const void* ptr, uint32_t c, uint32_t d) {
34     ktrace(tag, (uint32_t)((uintptr_t)ptr >> 32), (uint32_t)((uintptr_t)ptr), c, d);
35 }
36 
37 #define _ktrace_probe_prologue(_name) \
38     static ktrace_probe_info_t info = { NULL, _name, 0 };       \
39     __USED __SECTION(".data.rel.ro.ktrace_probe")               \
40     static ktrace_probe_info_t *const register_info = &info
41 
42 #define ktrace_probe0(_name) do {                               \
43     _ktrace_probe_prologue(_name);                              \
44     ktrace_open(TAG_PROBE_16(info.num));                        \
45 } while (0)
46 
47 #define ktrace_probe2(_name,arg0,arg1) do {                  \
48     _ktrace_probe_prologue(_name);                           \
49     uint32_t* args = (uint32_t*)ktrace_open(TAG_PROBE_24(info.num));    \
50     if (args) {                                              \
51       args[0] = arg0;                                        \
52       args[1] = arg1;                                        \
53     }                                                        \
54 } while (0)
55 
56 #define ktrace_probe64(_name,arg) do {                  \
57     _ktrace_probe_prologue(_name);                           \
58     uint64_t* args = (uint64_t*)ktrace_open(TAG_PROBE_24(info.num));    \
59     if (args) {                                              \
60       *args = arg;                                           \
61     }                                                        \
62 } while (0)
63 
64 void ktrace_name_etc(uint32_t tag, uint32_t id, uint32_t arg, const char* name, bool always);
ktrace_name(uint32_t tag,uint32_t id,uint32_t arg,const char * name)65 static inline void ktrace_name(uint32_t tag, uint32_t id, uint32_t arg, const char* name) {
66     ktrace_name_etc(tag, id, arg, name, false);
67 }
68 
69 ssize_t ktrace_read_user(void* ptr, uint32_t off, size_t len);
70 zx_status_t ktrace_control(uint32_t action, uint32_t options, void* ptr);
71 
72 #define KTRACE_DEFAULT_BUFSIZE 32 // MB
73 #define KTRACE_DEFAULT_GRPMASK 0xFFF
74 
75 void ktrace_report_live_threads(void);
76 void ktrace_report_live_processes(void);
77 
78 __END_CDECLS
79