| 1 | /* This file is part of the Linux Trace Toolkit viewer |
| 2 | * Copyright (C) 2003-2004 Michel Dagenais |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License Version 2 as |
| 6 | * published by the Free Software Foundation; |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 16 | * MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #ifndef FILTER_H |
| 20 | #define FILTER_H |
| 21 | |
| 22 | #include <lttv/traceset.h> |
| 23 | #include <lttv/tracecontext.h> |
| 24 | #include <lttv/state.h> |
| 25 | #include <lttv/module.h> |
| 26 | #include <ltt/ltt.h> |
| 27 | #include <ltt/event.h> |
| 28 | |
| 29 | |
| 30 | /* |
| 31 | A filter expression consists in nested AND, OR and NOT expressions |
| 32 | involving boolean relation (>, >=, =, !=, <, <=) between event fields and |
| 33 | specific values. It is compiled into an efficient data structure which |
| 34 | is used in functions to check if a given event or tracefile satisfies the |
| 35 | filter. |
| 36 | |
| 37 | The grammar for filters is: |
| 38 | |
| 39 | filter = expression |
| 40 | |
| 41 | expression = "(" expression ")" | "!" expression | |
| 42 | expression "&&" expression | expression "||" expression | |
| 43 | simpleExpression |
| 44 | |
| 45 | simpleExpression = fieldPath op value |
| 46 | |
| 47 | fieldPath = fieldComponent [ "." fieldPath ] |
| 48 | |
| 49 | fieldComponent = name [ "[" integer "]" ] |
| 50 | |
| 51 | value = integer | double | string |
| 52 | */ |
| 53 | |
| 54 | /** |
| 55 | * @enum LttvStructType |
| 56 | * @brief The lttv structures |
| 57 | * |
| 58 | * the LttvStructType enumerates |
| 59 | * the possible structures for the |
| 60 | * lttv core filter |
| 61 | */ |
| 62 | enum _LttvStructType { |
| 63 | LTTV_FILTER_TRACE, |
| 64 | LTTV_FILTER_TRACESET, |
| 65 | LTTV_FILTER_TRACEFILE, |
| 66 | LTTV_FILTER_EVENT, |
| 67 | LTTV_FILTER_STATE |
| 68 | } LttvStructType; |
| 69 | |
| 70 | /** |
| 71 | * @enum LttvFieldType |
| 72 | * @brief Possible fields for the structures |
| 73 | * |
| 74 | * the LttvFieldType enum consists on |
| 75 | * all the hardcoded structures and |
| 76 | * their appropriate fields on which |
| 77 | * filters can be applied. |
| 78 | */ |
| 79 | enum _LttvFieldType { |
| 80 | LTTV_FILTER_TRACE_NAME, /** trace.name (char*) */ |
| 81 | LTTV_FILTER_TRACEFILE_NAME, /** tracefile.name (char*) */ |
| 82 | LTTV_FILTER_STATE_PID, /** state.pid (guint) */ |
| 83 | LTTV_FILTER_STATE_PPID, /** state.ppid (guint) */ |
| 84 | LTTV_FILTER_STATE_CT, /** state.creation_time (double) */ |
| 85 | LTTV_FILTER_STATE_IT, /** state.insertion_time (double) */ |
| 86 | LTTV_FILTER_STATE_P_NAME, /** state.process_name (char*) */ |
| 87 | LTTV_FILTER_STATE_EX_MODE, /** state.execution_mode (LttvExecutionMode) */ |
| 88 | LTTV_FILTER_STATE_EX_SUBMODE, /** state.execution_submode (LttvExecutionSubmode) */ |
| 89 | LTTV_FILTER_STATE_P_STATUS, /** state.process_status (LttvProcessStatus) */ |
| 90 | LTTV_FILTER_STATE_CPU, /** state.cpu (?last_cpu?) */ |
| 91 | LTTV_FILTER_EVENT_NAME, /** event.name (char*) */ |
| 92 | LTTV_FILTER_EVENT_CATEGORY, /** FIXME: not implemented */ |
| 93 | LTTV_FILTER_EVENT_TIME, /** event.time (double) */ |
| 94 | LTTV_FILTER_EVENT_TSC, /** event.tsc (double) */ |
| 95 | LTTV_FILTER_EVENT_FIELD, |
| 96 | LTTV_FILTER_UNDEFINED /** undefined field */ |
| 97 | } LttvFieldType; |
| 98 | |
| 99 | /** |
| 100 | * @enum LttvExpressionOp |
| 101 | * @brief Contains possible operators |
| 102 | * |
| 103 | * This enumeration defines the |
| 104 | * possible operator used to compare |
| 105 | * right and left member in simple |
| 106 | * expression |
| 107 | */ |
| 108 | typedef enum _LttvExpressionOp |
| 109 | { |
| 110 | LTTV_FIELD_EQ, /** equal */ |
| 111 | LTTV_FIELD_NE, /** not equal */ |
| 112 | LTTV_FIELD_LT, /** lower than */ |
| 113 | LTTV_FIELD_LE, /** lower or equal */ |
| 114 | LTTV_FIELD_GT, /** greater than */ |
| 115 | LTTV_FIELD_GE /** greater or equal */ |
| 116 | } LttvExpressionOp; |
| 117 | |
| 118 | /** |
| 119 | * @union LttvFieldValue |
| 120 | * |
| 121 | * @brief Contains possible field values |
| 122 | * This particular union defines the |
| 123 | * possible set of values taken by the |
| 124 | * right member of a simple expression. |
| 125 | * It is used for comparison whithin the |
| 126 | * 'operators' functions |
| 127 | */ |
| 128 | typedef union _LttvFieldValue { |
| 129 | guint64 v_uint64; |
| 130 | guint32 v_uint32; |
| 131 | guint16 v_uint16; |
| 132 | double v_double; |
| 133 | char* v_string; |
| 134 | } LttvFieldValue; |
| 135 | |
| 136 | /** |
| 137 | * @enum LttvTreeElement |
| 138 | * @brief element types for the tree nodes |
| 139 | * |
| 140 | * LttvTreeElement defines the possible |
| 141 | * types of nodes which build the LttvFilterTree. |
| 142 | */ |
| 143 | typedef enum _LttvTreeElement { |
| 144 | LTTV_TREE_IDLE, /** this node does nothing */ |
| 145 | LTTV_TREE_NODE, /** this node contains a logical operator */ |
| 146 | LTTV_TREE_LEAF /** this node is a leaf and contains a simple expression */ |
| 147 | } LttvTreeElement; |
| 148 | |
| 149 | |
| 150 | /** |
| 151 | * @enum LttvSimpleExpression |
| 152 | * @brief simple expression structure |
| 153 | * |
| 154 | * An LttvSimpleExpression is the base |
| 155 | * of all filtering operations. It also |
| 156 | * populates the leaves of the |
| 157 | * LttvFilterTree. Each expression |
| 158 | * consists basically in a structure |
| 159 | * field, an operator and a specific |
| 160 | * value. |
| 161 | */ |
| 162 | typedef struct _LttvSimpleExpression |
| 163 | { |
| 164 | gint field; /** left member of simple expression */ |
| 165 | gint offset; /** offset used for dynamic fields */ |
| 166 | gboolean (*op)(gpointer,LttvFieldValue); /** operator of simple expression */ |
| 167 | // char *value; |
| 168 | LttvFieldValue value; /** right member of simple expression */ |
| 169 | } LttvSimpleExpression; |
| 170 | |
| 171 | /** |
| 172 | * @enum LttvLogicalOp |
| 173 | * @brief logical operators |
| 174 | * |
| 175 | * Contains the possible values taken |
| 176 | * by logical operator used to link |
| 177 | * simple expression. Values are |
| 178 | * AND, OR, XOR or NOT |
| 179 | */ |
| 180 | typedef enum _LttvLogicalOp { |
| 181 | LTTV_LOGICAL_OR = 1, /** OR (1) */ |
| 182 | LTTV_LOGICAL_AND = 1<<1, /** AND (2) */ |
| 183 | LTTV_LOGICAL_NOT = 1<<2, /** NOT (4) */ |
| 184 | LTTV_LOGICAL_XOR = 1<<3 /** XOR (8) */ |
| 185 | } LttvLogicalOp; |
| 186 | |
| 187 | /** |
| 188 | * @struct LttvFilterTree |
| 189 | * The filtering tree is used to represent the |
| 190 | * expression string in its entire hierarchy |
| 191 | * composed of simple expressions and logical |
| 192 | * operators |
| 193 | */ |
| 194 | typedef struct _LttvFilterTree { |
| 195 | int node; /** value of LttvLogicalOp */ |
| 196 | LttvTreeElement left; |
| 197 | LttvTreeElement right; |
| 198 | union { |
| 199 | struct LttvFilterTree* t; |
| 200 | LttvSimpleExpression* leaf; |
| 201 | } l_child; |
| 202 | union { |
| 203 | struct LttvFilterTree* t; |
| 204 | LttvSimpleExpression* leaf; |
| 205 | } r_child; |
| 206 | } LttvFilterTree; |
| 207 | |
| 208 | /** |
| 209 | * @struct lttv_filter |
| 210 | * Contains a binary tree of filtering options along |
| 211 | * with the expression itself. |
| 212 | */ |
| 213 | typedef struct _LttvFilter { |
| 214 | char *expression; |
| 215 | LttvFilterTree *head; |
| 216 | } LttvFilter; |
| 217 | |
| 218 | /* |
| 219 | * General Data Handling functions |
| 220 | */ |
| 221 | |
| 222 | void lttv_filter_tree_add_node(GPtrArray* stack, LttvFilterTree* subtree, LttvLogicalOp op); |
| 223 | |
| 224 | /* |
| 225 | * Simple Expression |
| 226 | */ |
| 227 | LttvSimpleExpression* lttv_simple_expression_new(); |
| 228 | |
| 229 | gboolean lttv_simple_expression_add_field(GPtrArray* fp, LttvSimpleExpression* se); |
| 230 | |
| 231 | gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression* se, LttvExpressionOp op); |
| 232 | |
| 233 | gboolean lttv_simple_expression_assign_value(LttvSimpleExpression* se, char* value); |
| 234 | |
| 235 | void lttv_simple_expression_destroy(LttvSimpleExpression* se); |
| 236 | |
| 237 | |
| 238 | /* |
| 239 | * Logical operators functions |
| 240 | */ |
| 241 | |
| 242 | gboolean lttv_apply_op_eq_uint64(gpointer v1, LttvFieldValue v2); |
| 243 | gboolean lttv_apply_op_eq_uint32(gpointer v1, LttvFieldValue v2); |
| 244 | gboolean lttv_apply_op_eq_uint16(gpointer v1, LttvFieldValue v2); |
| 245 | gboolean lttv_apply_op_eq_double(gpointer v1, LttvFieldValue v2); |
| 246 | gboolean lttv_apply_op_eq_string(gpointer v1, LttvFieldValue v2); |
| 247 | |
| 248 | gboolean lttv_apply_op_ne_uint64(gpointer v1, LttvFieldValue v2); |
| 249 | gboolean lttv_apply_op_ne_uint32(gpointer v1, LttvFieldValue v2); |
| 250 | gboolean lttv_apply_op_ne_uint16(gpointer v1, LttvFieldValue v2); |
| 251 | gboolean lttv_apply_op_ne_double(gpointer v1, LttvFieldValue v2); |
| 252 | gboolean lttv_apply_op_ne_string(gpointer v1, LttvFieldValue v2); |
| 253 | |
| 254 | gboolean lttv_apply_op_lt_uint64(gpointer v1, LttvFieldValue v2); |
| 255 | gboolean lttv_apply_op_lt_uint32(gpointer v1, LttvFieldValue v2); |
| 256 | gboolean lttv_apply_op_lt_uint16(gpointer v1, LttvFieldValue v2); |
| 257 | gboolean lttv_apply_op_lt_double(gpointer v1, LttvFieldValue v2); |
| 258 | |
| 259 | gboolean lttv_apply_op_le_uint64(gpointer v1, LttvFieldValue v2); |
| 260 | gboolean lttv_apply_op_le_uint32(gpointer v1, LttvFieldValue v2); |
| 261 | gboolean lttv_apply_op_le_uint16(gpointer v1, LttvFieldValue v2); |
| 262 | gboolean lttv_apply_op_le_double(gpointer v1, LttvFieldValue v2); |
| 263 | |
| 264 | gboolean lttv_apply_op_gt_uint64(gpointer v1, LttvFieldValue v2); |
| 265 | gboolean lttv_apply_op_gt_uint32(gpointer v1, LttvFieldValue v2); |
| 266 | gboolean lttv_apply_op_gt_uint16(gpointer v1, LttvFieldValue v2); |
| 267 | gboolean lttv_apply_op_gt_double(gpointer v1, LttvFieldValue v2); |
| 268 | |
| 269 | gboolean lttv_apply_op_ge_uint64(gpointer v1, LttvFieldValue v2); |
| 270 | gboolean lttv_apply_op_ge_uint32(gpointer v1, LttvFieldValue v2); |
| 271 | gboolean lttv_apply_op_ge_uint16(gpointer v1, LttvFieldValue v2); |
| 272 | gboolean lttv_apply_op_ge_double(gpointer v1, LttvFieldValue v2); |
| 273 | |
| 274 | /* |
| 275 | * Cloning |
| 276 | */ |
| 277 | |
| 278 | LttvFilterTree* lttv_filter_tree_clone(LttvFilterTree* tree); |
| 279 | |
| 280 | LttvFilter* lttv_filter_clone(LttvFilter* filter); |
| 281 | |
| 282 | /* |
| 283 | * LttvFilter |
| 284 | */ |
| 285 | LttvFilter *lttv_filter_new(); |
| 286 | |
| 287 | gboolean lttv_filter_update(LttvFilter* filter); |
| 288 | |
| 289 | void lttv_filter_destroy(LttvFilter* filter); |
| 290 | |
| 291 | gboolean lttv_filter_append_expression(LttvFilter* filter, char *expression); |
| 292 | |
| 293 | void lttv_filter_clear_expression(LttvFilter* filter); |
| 294 | |
| 295 | /* |
| 296 | * LttvFilterTree |
| 297 | */ |
| 298 | LttvFilterTree* lttv_filter_tree_new(); |
| 299 | |
| 300 | void lttv_filter_tree_destroy(LttvFilterTree* tree); |
| 301 | |
| 302 | gboolean lttv_filter_tree_parse( |
| 303 | LttvFilterTree* t, |
| 304 | LttEvent* event, |
| 305 | LttTracefile* tracefile, |
| 306 | LttTrace* trace, |
| 307 | LttvProcessState* state); |
| 308 | |
| 309 | /* |
| 310 | * Hook functions |
| 311 | * |
| 312 | * These hook functions will be the one called when filtering |
| 313 | * an event, a trace, a state, etc. |
| 314 | */ |
| 315 | |
| 316 | /* Check if the tracefile or event satisfies the filter. The arguments are |
| 317 | declared as void * to allow these functions to be used as hooks. */ |
| 318 | |
| 319 | gboolean lttv_filter_tracefile(LttvFilter *filter, LttTracefile *tracefile); |
| 320 | |
| 321 | gboolean lttv_filter_tracestate(LttvFilter *filter, LttvTraceState *tracestate); |
| 322 | |
| 323 | gboolean lttv_filter_event(LttvFilter *filter, LttEvent *event); |
| 324 | |
| 325 | /* |
| 326 | * Debug functions |
| 327 | */ |
| 328 | void lttv_print_tree(LttvFilterTree* t); |
| 329 | |
| 330 | #endif // FILTER_H |
| 331 | |