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