bt_parser.h revision 1.7
1/*	$OpenBSD: bt_parser.h,v 1.7 2020/04/23 18:36:51 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 */
33struct 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 */
47struct 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
61TAILQ_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 */
70struct 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 */
89struct 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 */
101struct bt_arg {
102	SLIST_ENTRY(bt_arg)	 ba_next;
103	void			*ba_value;
104	struct bt_arg		*ba_key;	/* key for maps */
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
111		B_AT_BI_PID,
112		B_AT_BI_TID,
113		B_AT_BI_COMM,
114		B_AT_BI_CPU,
115		B_AT_BI_NSECS,
116		B_AT_BI_KSTACK,
117		B_AT_BI_USTACK,
118		B_AT_BI_ARG0,
119		B_AT_BI_ARG1,
120		B_AT_BI_ARG2,
121		B_AT_BI_ARG3,
122		B_AT_BI_ARG4,
123		B_AT_BI_ARG5,
124		B_AT_BI_ARG6,
125		B_AT_BI_ARG7,
126		B_AT_BI_ARG8,
127		B_AT_BI_ARG9,
128		B_AT_BI_ARGS,
129		B_AT_BI_RETVAL,
130
131		B_AT_MF_COUNT,			/* @map[key] = count() */
132		B_AT_MF_MAX,			/* @map[key] = max(nsecs) */
133		B_AT_MF_MIN,			/* @map[key] = min(pid) */
134		B_AT_MF_SUM,			/* @map[key] = sum(@elapsed) */
135
136		B_AT_OP_ADD,
137		B_AT_OP_MINUS,
138		B_AT_OP_MULT,
139		B_AT_OP_DIVIDE,
140	}			 ba_type;
141};
142
143/*
144 * Statements define what should be done with each event recorded
145 * by the corresponding probe.
146 */
147struct bt_stmt {
148	SLIST_ENTRY(bt_stmt)	 bs_next;
149	struct bt_var		*bs_var;	/* for STOREs */
150	SLIST_HEAD(, bt_arg)	 bs_args;
151	enum bt_action {
152		B_AC_STORE = 1,			/* @a = 3 */
153		B_AC_INSERT,			/* @map[key] = 42 */
154		B_AC_CLEAR,			/* clear(@map) */
155		B_AC_DELETE,			/* delete(@map[key]) */
156		B_AC_EXIT,			/* exit() */
157		B_AC_PRINT,			/* print(@map, 10) */
158		B_AC_PRINTF,			/* printf("hello!\n") */
159		B_AC_TIME,			/* time("%H:%M:%S  ") */
160		B_AC_ZERO,			/* zero(@map) */
161	}			 bs_act;
162};
163
164struct bt_ruleq		 g_rules;	/* Successfully parsed rules. */
165int			 g_nprobes;	/* # of probes to attach */
166
167int			 btparse(const char *, size_t, const char *, int);
168
169#define ba_new(v, t)	 ba_new0((void *)(v), (t))
170struct bt_arg		*ba_new0(void *, enum bt_argtype);
171
172const char		*bv_name(struct bt_var *);
173
174void			 bm_insert(struct bt_var *, struct bt_arg *,
175			     struct bt_arg *);
176
177#endif /* BT_PARSER_H */
178