1 /* $OpenBSD: bt_parser.h,v 1.18 2021/08/31 08:39:26 mpi Exp $ */ 2 3 /* 4 * Copyright (c) 2019-2021 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 * Probes represent entry points where events can be recorded. 28 * 29 * Those specified in a given bt(5) script are enabled at runtime. They 30 * are represented as: 31 * 32 * "provider:function:name" 33 * or 34 * "provider:time_unit:rate" 35 */ 36 struct bt_probe { 37 const char *bp_prov; /* provider */ 38 const char *bp_func; /* function or time unit */ 39 const char *bp_name; 40 uint32_t bp_rate; 41 #define bp_unit bp_func 42 }; 43 44 45 /* 46 * Event filters correspond to checks performed in-kernel. 47 */ 48 struct bt_evtfilter { 49 int 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 58 /* 59 * Filters, also known as predicates, describe under which set of 60 * conditions a rule is executed. 61 * 62 * They are performed when a rule is evaluated and events might be 63 * discarded at runtime. 64 */ 65 struct bt_filter { 66 struct bt_stmt *bf_condition; /* per event condition */ 67 }; 68 69 TAILQ_HEAD(bt_ruleq, bt_rule); 70 71 /* 72 * A rule is the language representation of which 'action' to attach to 73 * which 'probe' under which conditions ('filter'). In other words it 74 * represents the following: 75 * 76 * probe / filter / { action } 77 */ 78 struct bt_rule { 79 TAILQ_ENTRY(bt_rule) br_next; /* linkage in global list */ 80 struct bt_probe *br_probe; 81 struct bt_filter *br_filter; 82 SLIST_HEAD(, bt_stmt) br_action; 83 SLIST_HEAD(, bt_var) br_variables; /* local variables */ 84 85 enum bt_rtype { 86 B_RT_BEGIN = 1, 87 B_RT_END, 88 B_RT_PROBE, 89 } br_type; /* BEGIN, END or 'probe' */ 90 91 uint32_t br_pbn; /* ID assigned by the kernel */ 92 void *br_cookie; 93 }; 94 95 /* 96 * Global variable representation. 97 * 98 * Variables are untyped and also include maps and histograms. 99 */ 100 struct bt_var { 101 SLIST_ENTRY(bt_var) bv_next; /* linkage in global list */ 102 const char *bv_name; /* name of the variable */ 103 struct bt_arg *bv_value; /* corresponding value */ 104 enum bt_vartype { 105 B_VT_STR = 1, 106 B_VT_LONG, 107 B_VT_MAP, 108 B_VT_HIST, 109 } bv_type; 110 }; 111 112 /* 113 * Respresentation of an argument. 114 * 115 * A so called "argument" can be any symbol representing a value or 116 * a combination of those through an operation. 117 */ 118 struct bt_arg { 119 SLIST_ENTRY(bt_arg) ba_next; 120 void *ba_value; 121 struct bt_arg *ba_key; /* key for maps/histograms */ 122 enum bt_argtype { 123 B_AT_STR = 1, /* C-style string */ 124 B_AT_LONG, /* Number (integer) */ 125 B_AT_VAR, /* global/local variable */ 126 B_AT_MAP, /* global map (@map[]) */ 127 B_AT_HIST, /* histogram */ 128 129 B_AT_BI_PID, 130 B_AT_BI_TID, 131 B_AT_BI_COMM, 132 B_AT_BI_CPU, 133 B_AT_BI_NSECS, 134 B_AT_BI_KSTACK, 135 B_AT_BI_USTACK, 136 B_AT_BI_ARG0, 137 B_AT_BI_ARG1, 138 B_AT_BI_ARG2, 139 B_AT_BI_ARG3, 140 B_AT_BI_ARG4, 141 B_AT_BI_ARG5, 142 B_AT_BI_ARG6, 143 B_AT_BI_ARG7, 144 B_AT_BI_ARG8, 145 B_AT_BI_ARG9, 146 B_AT_BI_ARGS, 147 B_AT_BI_RETVAL, 148 149 B_AT_MF_COUNT, /* @map[key] = count() */ 150 B_AT_MF_MAX, /* @map[key] = max(nsecs) */ 151 B_AT_MF_MIN, /* @map[key] = min(pid) */ 152 B_AT_MF_SUM, /* @map[key] = sum(@elapsed) */ 153 154 B_AT_OP_PLUS, 155 B_AT_OP_MINUS, 156 B_AT_OP_MULT, 157 B_AT_OP_DIVIDE, 158 B_AT_OP_BAND, 159 B_AT_OP_XOR, 160 B_AT_OP_BOR, 161 B_AT_OP_EQ, 162 B_AT_OP_NE, 163 B_AT_OP_LE, 164 B_AT_OP_LT, 165 B_AT_OP_GE, 166 B_AT_OP_GT, 167 B_AT_OP_LAND, 168 B_AT_OP_LOR, 169 } ba_type; 170 }; 171 172 #define BA_INITIALIZER(v, t) { { NULL }, (void *)(v), NULL, (t) } 173 174 /* 175 * Each action associated with a given probe is made of at least one 176 * statement. 177 * 178 * Statements are interpreted linearly in userland to format data 179 * recorded in the form of events. 180 */ 181 struct bt_stmt { 182 SLIST_ENTRY(bt_stmt) bs_next; 183 struct bt_var *bs_var; /* for STOREs */ 184 SLIST_HEAD(, bt_arg) bs_args; 185 enum bt_action { 186 B_AC_BUCKETIZE, /* @h = hist(42) */ 187 B_AC_CLEAR, /* clear(@map) */ 188 B_AC_DELETE, /* delete(@map[key]) */ 189 B_AC_EXIT, /* exit() */ 190 B_AC_INSERT, /* @map[key] = 42 */ 191 B_AC_PRINT, /* print(@map, 10) */ 192 B_AC_PRINTF, /* printf("hello!\n") */ 193 B_AC_STORE, /* @a = 3 */ 194 B_AC_TEST, /* if (@a) */ 195 B_AC_TIME, /* time("%H:%M:%S ") */ 196 B_AC_ZERO, /* zero(@map) */ 197 } bs_act; 198 }; 199 200 extern struct bt_ruleq g_rules; /* Successfully parsed rules. */ 201 extern int g_nprobes; /* # of probes to attach */ 202 extern struct bt_arg g_nullba; 203 extern struct bt_arg g_maxba; 204 205 int btparse(const char *, size_t, const char *, int); 206 207 #define ba_new(v, t) ba_new0((void *)(v), (t)) 208 struct bt_arg *ba_new0(void *, enum bt_argtype); 209 210 const char *bv_name(struct bt_var *); 211 212 #endif /* BT_PARSER_H */ 213