xref: /openbsd/usr.sbin/btrace/bt_parser.h (revision 09467b48)
1 /*	$OpenBSD: bt_parser.h,v 1.8 2020/07/11 14:52:14 mpi Exp $	*/
2 
3 /*
4  * Copyright (c) 2019-2020 Martin Pieuchot <mpi@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef BT_PARSER_H
20 #define BT_PARSER_H
21 
22 #ifndef nitems
23 #define nitems(_a)	(sizeof((_a)) / sizeof((_a)[0]))
24 #endif
25 
26 /*
27  * Representation of a probe.
28  *
29  *	"provider:function:name"
30  * or
31  *	"provider:time_unit:rate"
32  */
33 struct bt_probe {
34 	const char		*bp_prov;	/* provider */
35 	const char		*bp_func;	/* function or time unit */
36 	const char		*bp_name;
37 	uint32_t		 bp_rate;
38 #define bp_unit	bp_func
39 };
40 
41 /*
42  * Filters correspond to predicates performed in kernel.
43  *
44  * When a probe fires the check is performed, if it isn't true no event
45  * is recorded.
46  */
47 struct bt_filter {
48 	enum bt_operand {
49 		B_OP_NONE = 1,
50 		B_OP_EQ,
51 		B_OP_NE,
52 	}			 bf_op;
53 	enum  bt_filtervar {
54 		B_FV_NONE = 1,
55 		B_FV_PID,
56 		B_FV_TID
57 	}			 bf_var;
58 	uint32_t		 bf_val;
59 };
60 
61 TAILQ_HEAD(bt_ruleq, bt_rule);
62 
63 /*
64  * A rule is the language representation of which 'action' to attach to
65  * which 'probe' under which conditions ('filter').  In other words it
66  * represents the following:
67  *
68  *	probe / filter / { action }
69  */
70 struct bt_rule {
71 	TAILQ_ENTRY(bt_rule)	 br_next;	/* linkage in global list */
72 	struct bt_probe		*br_probe;
73 	struct bt_filter	*br_filter;
74 	SLIST_HEAD(, bt_stmt)	 br_action;
75 
76 	enum bt_rtype {
77 		 B_RT_BEGIN = 1,
78 		 B_RT_END,
79 		 B_RT_PROBE,
80 	}			 br_type;	/* BEGIN, END or 'probe' */
81 
82 	uint32_t		 br_pbn;	/* ID assigned by the kernel */
83 	void			*br_cookie;
84 };
85 
86 /*
87  * Global variable representation.
88  */
89 struct bt_var {
90 	SLIST_ENTRY(bt_var)	 bv_next;	/* linkage in global list */
91 	const char		*bv_name;	/* name of the variable */
92 	struct bt_arg		*bv_value;	/* corresponding value */
93 };
94 
95 /*
96  * Respresentation of an argument.
97  *
98  * A so called "argument" can be any symbol representing a value or
99  * a combination of those through an operation.
100  */
101 struct bt_arg {
102 	SLIST_ENTRY(bt_arg)	 ba_next;
103 	void			*ba_value;
104 	struct bt_arg		*ba_key;	/* key for maps/histograms */
105 	enum  bt_argtype {
106 		B_AT_STR = 1,			/* C-style string */
107 		B_AT_LONG,			/* Number (integer) */
108 		B_AT_VAR,			/* global variable (@var) */
109 		B_AT_MAP,			/* global map (@map[]) */
110 		B_AT_HIST,			/* histogram */
111 
112 		B_AT_BI_PID,
113 		B_AT_BI_TID,
114 		B_AT_BI_COMM,
115 		B_AT_BI_CPU,
116 		B_AT_BI_NSECS,
117 		B_AT_BI_KSTACK,
118 		B_AT_BI_USTACK,
119 		B_AT_BI_ARG0,
120 		B_AT_BI_ARG1,
121 		B_AT_BI_ARG2,
122 		B_AT_BI_ARG3,
123 		B_AT_BI_ARG4,
124 		B_AT_BI_ARG5,
125 		B_AT_BI_ARG6,
126 		B_AT_BI_ARG7,
127 		B_AT_BI_ARG8,
128 		B_AT_BI_ARG9,
129 		B_AT_BI_ARGS,
130 		B_AT_BI_RETVAL,
131 
132 		B_AT_MF_COUNT,			/* @map[key] = count() */
133 		B_AT_MF_MAX,			/* @map[key] = max(nsecs) */
134 		B_AT_MF_MIN,			/* @map[key] = min(pid) */
135 		B_AT_MF_SUM,			/* @map[key] = sum(@elapsed) */
136 
137 		B_AT_OP_ADD,
138 		B_AT_OP_MINUS,
139 		B_AT_OP_MULT,
140 		B_AT_OP_DIVIDE,
141 	}			 ba_type;
142 };
143 
144 #define BA_INITIALIZER(v, t)	{ { NULL }, (void *)(v), NULL, (t) }
145 
146 /*
147  * Statements define what should be done with each event recorded
148  * by the corresponding probe.
149  */
150 struct bt_stmt {
151 	SLIST_ENTRY(bt_stmt)	 bs_next;
152 	struct bt_var		*bs_var;	/* for STOREs */
153 	SLIST_HEAD(, bt_arg)	 bs_args;
154 	enum bt_action {
155 		B_AC_BUCKETIZE,			/* @h = hist(42) */
156 		B_AC_CLEAR,			/* clear(@map) */
157 		B_AC_DELETE,			/* delete(@map[key]) */
158 		B_AC_EXIT,			/* exit() */
159 		B_AC_INSERT,			/* @map[key] = 42 */
160 		B_AC_PRINT,			/* print(@map, 10) */
161 		B_AC_PRINTF,			/* printf("hello!\n") */
162 		B_AC_STORE,			/* @a = 3 */
163 		B_AC_TIME,			/* time("%H:%M:%S  ") */
164 		B_AC_ZERO,			/* zero(@map) */
165 	}			 bs_act;
166 };
167 
168 struct bt_ruleq		 g_rules;	/* Successfully parsed rules. */
169 int			 g_nprobes;	/* # of probes to attach */
170 
171 int			 btparse(const char *, size_t, const char *, int);
172 
173 #define ba_new(v, t)	 ba_new0((void *)(v), (t))
174 struct bt_arg		*ba_new0(void *, enum bt_argtype);
175 
176 const char		*bv_name(struct bt_var *);
177 
178 void			 bm_insert(struct bt_var *, struct bt_arg *,
179 			     struct bt_arg *);
180 
181 #endif /* BT_PARSER_H */
182