bt_parser.h revision 1.2
1/*	$OpenBSD: bt_parser.h,v 1.2 2020/01/27 14:15:25 mpi Exp $	*/
2
3/*
4 * Copyright (c) 2019 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 * Representation of a filter (aka predicate).
43 */
44struct bt_filter {
45	enum bt_operand {
46		B_OP_NONE = 1,
47		B_OP_EQ,
48		B_OP_NE,
49	}			 bf_op;
50	enum  bt_filtervar {
51		B_FV_NONE = 1,
52		B_FV_PID,
53		B_FV_TID
54	}			 bf_var;
55	uint32_t		 bf_val;
56};
57
58TAILQ_HEAD(bt_ruleq, bt_rule);
59
60/*
61 * A rule is the language representation of which 'action' to attach to
62 * which 'probe' under which conditions ('filter').  In other words it
63 * represents the following:
64 *
65 *	probe / filter / { action }
66 */
67struct bt_rule {
68	TAILQ_ENTRY(bt_rule)	 br_next;	/* linkage in global list */
69	struct bt_probe		*br_probe;
70	struct bt_filter	*br_filter;
71	SLIST_HEAD(, bt_stmt)	 br_action;
72
73	enum bt_rtype {
74		 B_RT_BEGIN = 1,
75		 B_RT_END,
76		 B_RT_PROBE,
77	}			 br_type;	/* BEGIN, END or 'probe' */
78
79	uint32_t		 br_pbn;	/* ID assigned by the kernel */
80	void			*br_cookie;
81};
82
83/*
84 * Global variable representation.
85 */
86struct bt_var {
87	SLIST_ENTRY(bt_var)	 bv_next;	/* linkage in global list */
88	const char		*bv_name;	/* name of the variable */
89	struct bt_arg		*bv_value;	/* corresponding value */
90};
91
92/*
93 * Respresentation of an argument.
94 *
95 * A so called "argument" can be any symbol representing a value or
96 * a combination of those through an operation.
97 */
98struct bt_arg {
99	SLIST_ENTRY(bt_arg)	 ba_next;
100	void			*ba_value;
101	enum  bt_argtype {
102		B_AT_STR = 1,			/* C-style string */
103		B_AT_LONG,			/* Number (integer) */
104		B_AT_VAR,			/* global variable (@var) */
105
106		B_AT_BI_PID,
107		B_AT_BI_TID,
108		B_AT_BI_COMM,
109		B_AT_BI_NSECS,
110		B_AT_BI_KSTACK,
111		B_AT_BI_USTACK,
112		B_AT_BI_ARG0,
113		B_AT_BI_ARG1,
114		B_AT_BI_ARG2,
115		B_AT_BI_ARG3,
116		B_AT_BI_ARG4,
117		B_AT_BI_ARG5,
118		B_AT_BI_ARG6,
119		B_AT_BI_ARG7,
120		B_AT_BI_ARG8,
121		B_AT_BI_ARG9,
122		B_AT_BI_ARGS,
123		B_AT_BI_RETVAL,
124
125		B_AT_MF_COUNT,			/* count() */
126
127		B_AT_OP_ADD,
128		B_AT_OP_MINUS,
129		B_AT_OP_MULT,
130		B_AT_OP_DIVIDE,
131	}			 ba_type;
132};
133
134/*
135 * Statements define what should be done with each event recorded
136 * by the corresponding probe.
137 */
138struct bt_stmt {
139	SLIST_ENTRY(bt_stmt)	 bs_next;
140	struct bt_var		*bs_var;	/* for STOREs */
141	SLIST_HEAD(, bt_arg)	 bs_args;
142	enum bt_action {
143		B_AC_STORE = 1,			/* @a = 3 */
144		B_AC_INSERT,			/* @map[key] = 42 */
145		B_AC_CLEAR,			/* clear(@map) */
146		B_AC_DELETE,			/* delete(@map[key]) */
147		B_AC_EXIT,			/* exit() */
148		B_AC_PRINT,			/* print(@map, 10) */
149		B_AC_PRINTF,			/* printf("hello!\n") */
150		B_AC_TIME,			/* time("%H:%M:%S  ") */
151		B_AC_ZERO,			/* zero(@map) */
152	}			 bs_act;
153};
154
155struct bt_ruleq		 g_rules;	/* Successfully parsed rules. */
156int			 g_nprobes;	/* # of probes to attach */
157
158int			 btparse(const char *, size_t, const char *, int);
159
160#define ba_new(v, t)	 ba_new0((void *)(v), (t))
161struct bt_arg		*ba_new0(void *, enum bt_argtype);
162
163void			 bm_insert(struct bt_var *, struct bt_arg *,
164			     struct bt_arg *);
165
166#endif /* BT_PARSER_H */
167