| 1 | /* This file is part of the Linux Trace Toolkit viewer |
| 2 | * Copyright (C) 2003-2005 Michel Dagenais and Simon Bouvier-Zappa |
| 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 | /*! \file lttv/lttv/filter.h |
| 23 | * \brief Defines the core filter of application |
| 24 | * |
| 25 | * A filter expression consists in nested AND, OR and NOT expressions |
| 26 | * involving boolean relation (>, >=, =, !=, <, <=) between event fields and |
| 27 | * specific values. It is compiled into an efficient data structure which |
| 28 | * is used in functions to check if a given event or tracefile satisfies the |
| 29 | * filter. |
| 30 | * |
| 31 | * The grammar for filters is: |
| 32 | * |
| 33 | * filter = expression |
| 34 | * |
| 35 | * expression = "(" expression ")" | "!" expression | |
| 36 | * expression "&&" expression | expression "||" expression | |
| 37 | * simpleExpression |
| 38 | * |
| 39 | * simpleExpression = fieldPath op value |
| 40 | * |
| 41 | * fieldPath = fieldComponent [ "." fieldPath ] |
| 42 | * |
| 43 | * fieldComponent = name [ "[" integer "]" ] |
| 44 | * |
| 45 | * value = integer | double | string |
| 46 | */ |
| 47 | |
| 48 | |
| 49 | #include <lttv/traceset.h> |
| 50 | #include <lttv/tracecontext.h> |
| 51 | #include <lttv/state.h> |
| 52 | #include <lttv/module.h> |
| 53 | #include <ltt/ltt.h> |
| 54 | #include <ltt/time.h> |
| 55 | #include <ltt/event.h> |
| 56 | |
| 57 | /* structures prototypes */ |
| 58 | typedef enum _LttvStructType LttvStructType; |
| 59 | typedef enum _LttvFieldType LttvFieldType; |
| 60 | typedef enum _LttvExpressionOp LttvExpressionOp; |
| 61 | typedef enum _LttvTreeElement LttvTreeElement; |
| 62 | typedef enum _LttvLogicalOp LttvLogicalOp; |
| 63 | |
| 64 | typedef union _LttvFieldValue LttvFieldValue; |
| 65 | |
| 66 | typedef struct _LttvSimpleExpression LttvSimpleExpression; |
| 67 | typedef struct _LttvFilterTree LttvFilterTree; |
| 68 | |
| 69 | #ifndef LTTVFILTER_TYPE_DEFINED |
| 70 | typedef struct _LttvFilter LttvFilter; |
| 71 | #define LTTVFILTER_TYPE_DEFINED |
| 72 | #endif |
| 73 | |
| 74 | /** |
| 75 | * @enum _LttvStructType |
| 76 | * @brief The lttv structures |
| 77 | * |
| 78 | * the LttvStructType enumerates |
| 79 | * the possible structures for the |
| 80 | * lttv core filter |
| 81 | */ |
| 82 | enum _LttvStructType { |
| 83 | LTTV_FILTER_TRACE, /**< trace (LttTrace) */ |
| 84 | LTTV_FILTER_TRACESET, /**< traceset */ |
| 85 | LTTV_FILTER_TRACEFILE, /**< tracefile (LttTracefile) */ |
| 86 | LTTV_FILTER_EVENT, /**< event (LttEvent) */ |
| 87 | LTTV_FILTER_STATE /**< state (LttvProcessState) */ |
| 88 | }; |
| 89 | |
| 90 | /** |
| 91 | * @enum _LttvFieldType |
| 92 | * @brief Possible fields for the structures |
| 93 | * |
| 94 | * the LttvFieldType enum consists on |
| 95 | * all the hardcoded structures and |
| 96 | * their appropriate fields on which |
| 97 | * filters can be applied. |
| 98 | */ |
| 99 | enum _LttvFieldType { |
| 100 | LTTV_FILTER_TRACE_NAME, /**< trace.name (char*) */ |
| 101 | LTTV_FILTER_TRACEFILE_NAME, /**< tracefile.name (char*) */ |
| 102 | LTTV_FILTER_STATE_PID, /**< state.pid (guint) */ |
| 103 | LTTV_FILTER_STATE_PPID, /**< state.ppid (guint) */ |
| 104 | LTTV_FILTER_STATE_CT, /**< state.creation_time (double) */ |
| 105 | LTTV_FILTER_STATE_IT, /**< state.insertion_time (double) */ |
| 106 | LTTV_FILTER_STATE_P_NAME, /**< state.process_name (char*) */ |
| 107 | LTTV_FILTER_STATE_EX_MODE, /**< state.execution_mode (LttvExecutionMode) */ |
| 108 | LTTV_FILTER_STATE_EX_SUBMODE, /**< state.execution_submode (LttvExecutionSubmode) */ |
| 109 | LTTV_FILTER_STATE_P_STATUS, /**< state.process_status (LttvProcessStatus) */ |
| 110 | LTTV_FILTER_STATE_CPU, /**< state.cpu (?last_cpu?) */ |
| 111 | LTTV_FILTER_EVENT_NAME, /**< event.name (char*) */ |
| 112 | LTTV_FILTER_EVENT_CATEGORY, /**< FIXME: not implemented */ |
| 113 | LTTV_FILTER_EVENT_TIME, /**< event.time (double) */ |
| 114 | LTTV_FILTER_EVENT_TSC, /**< event.tsc (double) */ |
| 115 | LTTV_FILTER_EVENT_FIELD, /**< dynamic field, specified in core.xml */ |
| 116 | LTTV_FILTER_UNDEFINED /**< undefined field */ |
| 117 | }; |
| 118 | |
| 119 | /** |
| 120 | * @enum _LttvExpressionOp |
| 121 | * @brief Contains possible operators |
| 122 | * |
| 123 | * This enumeration defines the |
| 124 | * possible operator used to compare |
| 125 | * right and left member in simple |
| 126 | * expression |
| 127 | */ |
| 128 | enum _LttvExpressionOp |
| 129 | { |
| 130 | LTTV_FIELD_EQ, /**< equal */ |
| 131 | LTTV_FIELD_NE, /**< not equal */ |
| 132 | LTTV_FIELD_LT, /**< lower than */ |
| 133 | LTTV_FIELD_LE, /**< lower or equal */ |
| 134 | LTTV_FIELD_GT, /**< greater than */ |
| 135 | LTTV_FIELD_GE /**< greater or equal */ |
| 136 | }; |
| 137 | |
| 138 | /** |
| 139 | * @union _LttvFieldValue |
| 140 | * @brief Contains possible field values |
| 141 | * |
| 142 | * This particular union defines the |
| 143 | * possible set of values taken by the |
| 144 | * right member of a simple expression. |
| 145 | * It is used for comparison whithin the |
| 146 | * 'operators' functions |
| 147 | */ |
| 148 | union _LttvFieldValue { |
| 149 | guint64 v_uint64; /**< unsigned int of 64 bytes */ |
| 150 | guint32 v_uint32; /**< unsigned int of 32 bytes */ |
| 151 | guint16 v_uint16; /**< unsigned int of 16 bytes */ |
| 152 | guint16 v_uint; /**< unsigned int */ |
| 153 | double v_double; /**< double */ |
| 154 | char* v_string; /**< string */ |
| 155 | LttTime v_ltttime; /**< LttTime */ |
| 156 | }; |
| 157 | |
| 158 | /** |
| 159 | * @enum _LttvTreeElement |
| 160 | * @brief element types for the tree nodes |
| 161 | * |
| 162 | * LttvTreeElement defines the possible |
| 163 | * types of nodes which build the LttvFilterTree. |
| 164 | */ |
| 165 | enum _LttvTreeElement { |
| 166 | LTTV_TREE_IDLE, /**< this node does nothing */ |
| 167 | LTTV_TREE_NODE, /**< this node contains a logical operator */ |
| 168 | LTTV_TREE_LEAF /**< this node is a leaf and contains a simple expression */ |
| 169 | }; |
| 170 | |
| 171 | |
| 172 | /** |
| 173 | * @struct _LttvSimpleExpression |
| 174 | * @brief simple expression structure |
| 175 | * |
| 176 | * An LttvSimpleExpression is the base |
| 177 | * of all filtering operations. It also |
| 178 | * populates the leaves of the |
| 179 | * LttvFilterTree. Each expression |
| 180 | * consists basically in a structure |
| 181 | * field, an operator and a specific |
| 182 | * value. |
| 183 | */ |
| 184 | struct _LttvSimpleExpression |
| 185 | { |
| 186 | gint field; /**< left member of simple expression */ |
| 187 | gint offset; /**< offset used for dynamic fields */ |
| 188 | gboolean (*op)(gpointer,LttvFieldValue); /**< operator of simple expression */ |
| 189 | LttvFieldValue value; /**< right member of simple expression */ |
| 190 | }; |
| 191 | |
| 192 | /** |
| 193 | * @enum _LttvLogicalOp |
| 194 | * @brief logical operators |
| 195 | * |
| 196 | * Contains the possible values taken |
| 197 | * by logical operator used to link |
| 198 | * simple expression. Values are |
| 199 | * AND, OR, XOR or NOT |
| 200 | */ |
| 201 | enum _LttvLogicalOp { |
| 202 | LTTV_LOGICAL_OR = 1, /**< OR (1) */ |
| 203 | LTTV_LOGICAL_AND = 1<<1, /**< AND (2) */ |
| 204 | LTTV_LOGICAL_NOT = 1<<2, /**< NOT (4) */ |
| 205 | LTTV_LOGICAL_XOR = 1<<3 /**< XOR (8) */ |
| 206 | }; |
| 207 | |
| 208 | /** |
| 209 | * @struct _LttvFilterTree |
| 210 | * @brief The filtering tree |
| 211 | * |
| 212 | * The filtering tree is used to represent the |
| 213 | * expression string in its entire hierarchy |
| 214 | * composed of simple expressions and logical |
| 215 | * operators |
| 216 | */ |
| 217 | struct _LttvFilterTree { |
| 218 | int node; /**< value of LttvLogicalOp */ |
| 219 | LttvTreeElement left; /**< nature of left branch (node/leaf) */ |
| 220 | LttvTreeElement right; /**< nature of right branch (node/leaf) */ |
| 221 | union { |
| 222 | LttvFilterTree* t; |
| 223 | LttvSimpleExpression* leaf; |
| 224 | } l_child; /**< left branch of tree */ |
| 225 | union { |
| 226 | LttvFilterTree* t; |
| 227 | LttvSimpleExpression* leaf; |
| 228 | } r_child; /**< right branch of tree */ |
| 229 | }; |
| 230 | |
| 231 | /** |
| 232 | * @struct _LttvFilter |
| 233 | * @brief The filter |
| 234 | * |
| 235 | * Contains a binary tree of filtering options along |
| 236 | * with the expression itself. |
| 237 | */ |
| 238 | struct _LttvFilter { |
| 239 | char *expression; /**< filtering expression string */ |
| 240 | LttvFilterTree *head; /**< tree associated to expression */ |
| 241 | }; |
| 242 | |
| 243 | /* |
| 244 | * Simple Expression |
| 245 | */ |
| 246 | LttvSimpleExpression* lttv_simple_expression_new(); |
| 247 | |
| 248 | gboolean lttv_simple_expression_assign_field(GPtrArray* fp, LttvSimpleExpression* se); |
| 249 | |
| 250 | gboolean lttv_simple_expression_assign_operator(LttvSimpleExpression* se, LttvExpressionOp op); |
| 251 | |
| 252 | gboolean lttv_simple_expression_assign_value(LttvSimpleExpression* se, char* value); |
| 253 | |
| 254 | void lttv_simple_expression_destroy(LttvSimpleExpression* se); |
| 255 | |
| 256 | |
| 257 | /* |
| 258 | * Logical operators functions |
| 259 | */ |
| 260 | |
| 261 | gboolean lttv_apply_op_eq_uint(const gpointer v1, LttvFieldValue v2); |
| 262 | gboolean lttv_apply_op_eq_uint64(const gpointer v1, LttvFieldValue v2); |
| 263 | gboolean lttv_apply_op_eq_uint32(const gpointer v1, LttvFieldValue v2); |
| 264 | gboolean lttv_apply_op_eq_uint16(const gpointer v1, LttvFieldValue v2); |
| 265 | gboolean lttv_apply_op_eq_double(const gpointer v1, LttvFieldValue v2); |
| 266 | gboolean lttv_apply_op_eq_string(const gpointer v1, LttvFieldValue v2); |
| 267 | gboolean lttv_apply_op_eq_quark(const gpointer v1, LttvFieldValue v2); |
| 268 | gboolean lttv_apply_op_eq_ltttime(const gpointer v1, LttvFieldValue v2); |
| 269 | |
| 270 | gboolean lttv_apply_op_ne_uint(const gpointer v1, LttvFieldValue v2); |
| 271 | gboolean lttv_apply_op_ne_uint64(const gpointer v1, LttvFieldValue v2); |
| 272 | gboolean lttv_apply_op_ne_uint32(const gpointer v1, LttvFieldValue v2); |
| 273 | gboolean lttv_apply_op_ne_uint16(const gpointer v1, LttvFieldValue v2); |
| 274 | gboolean lttv_apply_op_ne_double(const gpointer v1, LttvFieldValue v2); |
| 275 | gboolean lttv_apply_op_ne_string(const gpointer v1, LttvFieldValue v2); |
| 276 | gboolean lttv_apply_op_ne_quark(const gpointer v1, LttvFieldValue v2); |
| 277 | gboolean lttv_apply_op_ne_ltttime(const gpointer v1, LttvFieldValue v2); |
| 278 | |
| 279 | gboolean lttv_apply_op_lt_uint(const gpointer v1, LttvFieldValue v2); |
| 280 | gboolean lttv_apply_op_lt_uint64(const gpointer v1, LttvFieldValue v2); |
| 281 | gboolean lttv_apply_op_lt_uint32(const gpointer v1, LttvFieldValue v2); |
| 282 | gboolean lttv_apply_op_lt_uint16(const gpointer v1, LttvFieldValue v2); |
| 283 | gboolean lttv_apply_op_lt_double(const gpointer v1, LttvFieldValue v2); |
| 284 | gboolean lttv_apply_op_lt_ltttime(const gpointer v1, LttvFieldValue v2); |
| 285 | |
| 286 | gboolean lttv_apply_op_le_uint(const gpointer v1, LttvFieldValue v2); |
| 287 | gboolean lttv_apply_op_le_uint64(const gpointer v1, LttvFieldValue v2); |
| 288 | gboolean lttv_apply_op_le_uint32(const gpointer v1, LttvFieldValue v2); |
| 289 | gboolean lttv_apply_op_le_uint16(const gpointer v1, LttvFieldValue v2); |
| 290 | gboolean lttv_apply_op_le_double(const gpointer v1, LttvFieldValue v2); |
| 291 | gboolean lttv_apply_op_le_ltttime(const gpointer v1, LttvFieldValue v2); |
| 292 | |
| 293 | gboolean lttv_apply_op_gt_uint(const gpointer v1, LttvFieldValue v2); |
| 294 | gboolean lttv_apply_op_gt_uint64(const gpointer v1, LttvFieldValue v2); |
| 295 | gboolean lttv_apply_op_gt_uint32(const gpointer v1, LttvFieldValue v2); |
| 296 | gboolean lttv_apply_op_gt_uint16(const gpointer v1, LttvFieldValue v2); |
| 297 | gboolean lttv_apply_op_gt_double(const gpointer v1, LttvFieldValue v2); |
| 298 | gboolean lttv_apply_op_gt_ltttime(const gpointer v1, LttvFieldValue v2); |
| 299 | |
| 300 | gboolean lttv_apply_op_ge_uint(const gpointer v1, LttvFieldValue v2); |
| 301 | gboolean lttv_apply_op_ge_uint64(const gpointer v1, LttvFieldValue v2); |
| 302 | gboolean lttv_apply_op_ge_uint32(const gpointer v1, LttvFieldValue v2); |
| 303 | gboolean lttv_apply_op_ge_uint16(const gpointer v1, LttvFieldValue v2); |
| 304 | gboolean lttv_apply_op_ge_double(const gpointer v1, LttvFieldValue v2); |
| 305 | gboolean lttv_apply_op_ge_ltttime(const gpointer v1, LttvFieldValue v2); |
| 306 | |
| 307 | /* |
| 308 | * Cloning |
| 309 | */ |
| 310 | |
| 311 | LttvFilterTree* lttv_filter_tree_clone(const LttvFilterTree* tree); |
| 312 | |
| 313 | LttvFilter* lttv_filter_clone(const LttvFilter* filter); |
| 314 | |
| 315 | /* |
| 316 | * LttvFilter |
| 317 | */ |
| 318 | LttvFilter *lttv_filter_new(); |
| 319 | |
| 320 | gboolean lttv_filter_update(LttvFilter* filter); |
| 321 | |
| 322 | void lttv_filter_destroy(LttvFilter* filter); |
| 323 | |
| 324 | gboolean lttv_filter_append_expression(LttvFilter* filter, const char *expression); |
| 325 | |
| 326 | void lttv_filter_clear_expression(LttvFilter* filter); |
| 327 | |
| 328 | /* |
| 329 | * LttvFilterTree |
| 330 | */ |
| 331 | LttvFilterTree* lttv_filter_tree_new(); |
| 332 | |
| 333 | void lttv_filter_tree_destroy(LttvFilterTree* tree); |
| 334 | |
| 335 | gboolean lttv_filter_tree_parse( |
| 336 | const LttvFilterTree* t, |
| 337 | const LttEvent* event, |
| 338 | const LttTracefile* tracefile, |
| 339 | const LttTrace* trace, |
| 340 | const LttvTracefileContext* context); |
| 341 | |
| 342 | gboolean lttv_filter_tree_parse_branch( |
| 343 | const LttvSimpleExpression* se, |
| 344 | const LttEvent* event, |
| 345 | const LttTracefile* tracefile, |
| 346 | const LttTrace* trace, |
| 347 | const LttvProcessState* state, |
| 348 | const LttvTracefileContext* context); |
| 349 | |
| 350 | /* |
| 351 | * Debug functions |
| 352 | */ |
| 353 | void lttv_print_tree(const LttvFilterTree* t, const int count); |
| 354 | |
| 355 | #endif // FILTER_H |
| 356 | |