| 1 | #ifndef _LTTNG_FILTER_H |
| 2 | #define _LTTNG_FILTER_H |
| 3 | |
| 4 | /* |
| 5 | * lttng-filter.h |
| 6 | * |
| 7 | * LTTng UST filter header. |
| 8 | * |
| 9 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 10 | * |
| 11 | * This library is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU Lesser General Public |
| 13 | * License as published by the Free Software Foundation; only |
| 14 | * version 2.1 of the License. |
| 15 | * |
| 16 | * This library is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * Lesser General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU Lesser General Public |
| 22 | * License along with this library; if not, write to the Free Software |
| 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 24 | */ |
| 25 | |
| 26 | #include <errno.h> |
| 27 | #include <stdio.h> |
| 28 | #include <helper.h> |
| 29 | #include <lttng/ust-events.h> |
| 30 | #include <lttng/ust-context-provider.h> |
| 31 | #include <stdint.h> |
| 32 | #include <assert.h> |
| 33 | #include <errno.h> |
| 34 | #include <string.h> |
| 35 | #include <inttypes.h> |
| 36 | #include <limits.h> |
| 37 | #include <usterr-signal-safe.h> |
| 38 | #include "filter-bytecode.h" |
| 39 | |
| 40 | /* Filter stack length, in number of entries */ |
| 41 | #define FILTER_STACK_LEN 10 /* includes 2 dummy */ |
| 42 | #define FILTER_STACK_EMPTY 1 |
| 43 | |
| 44 | #ifndef min_t |
| 45 | #define min_t(type, a, b) \ |
| 46 | ((type) (a) < (type) (b) ? (type) (a) : (type) (b)) |
| 47 | #endif |
| 48 | |
| 49 | #ifndef likely |
| 50 | #define likely(x) __builtin_expect(!!(x), 1) |
| 51 | #endif |
| 52 | |
| 53 | #ifndef unlikely |
| 54 | #define unlikely(x) __builtin_expect(!!(x), 0) |
| 55 | #endif |
| 56 | |
| 57 | #ifdef DEBUG |
| 58 | #define dbg_printf(fmt, args...) \ |
| 59 | printf("[debug bytecode in %s:%s@%u] " fmt, \ |
| 60 | __FILE__, __func__, __LINE__, ## args) |
| 61 | #else |
| 62 | #define dbg_printf(fmt, args...) \ |
| 63 | do { \ |
| 64 | /* do nothing but check printf format */ \ |
| 65 | if (0) \ |
| 66 | printf("[debug bytecode in %s:%s@%u] " fmt, \ |
| 67 | __FILE__, __func__, __LINE__, ## args); \ |
| 68 | } while (0) |
| 69 | #endif |
| 70 | |
| 71 | /* Linked bytecode. Child of struct lttng_bytecode_runtime. */ |
| 72 | struct bytecode_runtime { |
| 73 | struct lttng_bytecode_runtime p; |
| 74 | uint16_t len; |
| 75 | char data[0]; |
| 76 | }; |
| 77 | |
| 78 | enum entry_type { |
| 79 | REG_S64, |
| 80 | REG_DOUBLE, |
| 81 | REG_STRING, |
| 82 | REG_UNKNOWN, |
| 83 | }; |
| 84 | |
| 85 | /* Validation stack */ |
| 86 | struct vstack_entry { |
| 87 | enum entry_type type; |
| 88 | }; |
| 89 | |
| 90 | struct vstack { |
| 91 | int top; /* top of stack */ |
| 92 | struct vstack_entry e[FILTER_STACK_LEN]; |
| 93 | }; |
| 94 | |
| 95 | static inline |
| 96 | void vstack_init(struct vstack *stack) |
| 97 | { |
| 98 | stack->top = -1; |
| 99 | } |
| 100 | |
| 101 | static inline |
| 102 | struct vstack_entry *vstack_ax(struct vstack *stack) |
| 103 | { |
| 104 | if (unlikely(stack->top < 0)) |
| 105 | return NULL; |
| 106 | return &stack->e[stack->top]; |
| 107 | } |
| 108 | |
| 109 | static inline |
| 110 | struct vstack_entry *vstack_bx(struct vstack *stack) |
| 111 | { |
| 112 | if (unlikely(stack->top < 1)) |
| 113 | return NULL; |
| 114 | return &stack->e[stack->top - 1]; |
| 115 | } |
| 116 | |
| 117 | static inline |
| 118 | int vstack_push(struct vstack *stack) |
| 119 | { |
| 120 | if (stack->top >= FILTER_STACK_LEN - 1) { |
| 121 | ERR("Stack full\n"); |
| 122 | return -EINVAL; |
| 123 | } |
| 124 | ++stack->top; |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static inline |
| 129 | int vstack_pop(struct vstack *stack) |
| 130 | { |
| 131 | if (unlikely(stack->top < 0)) { |
| 132 | ERR("Stack empty\n"); |
| 133 | return -EINVAL; |
| 134 | } |
| 135 | stack->top--; |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | /* Execution stack */ |
| 140 | struct estack_entry { |
| 141 | enum entry_type type; /* For dynamic typing. */ |
| 142 | union { |
| 143 | int64_t v; |
| 144 | double d; |
| 145 | |
| 146 | struct { |
| 147 | const char *str; |
| 148 | size_t seq_len; |
| 149 | int literal; /* is string literal ? */ |
| 150 | } s; |
| 151 | } u; |
| 152 | }; |
| 153 | |
| 154 | struct estack { |
| 155 | int top; /* top of stack */ |
| 156 | struct estack_entry e[FILTER_STACK_LEN]; |
| 157 | }; |
| 158 | |
| 159 | /* |
| 160 | * Always use aliased type for ax/bx (top of stack). |
| 161 | * When ax/bx are S64, use aliased value. |
| 162 | */ |
| 163 | #define estack_ax_v ax |
| 164 | #define estack_bx_v bx |
| 165 | #define estack_ax_t ax_t |
| 166 | #define estack_bx_t bx_t |
| 167 | |
| 168 | /* |
| 169 | * ax and bx registers can hold either integer, double or string. |
| 170 | */ |
| 171 | #define estack_ax(stack, top) \ |
| 172 | ({ \ |
| 173 | assert((top) > FILTER_STACK_EMPTY); \ |
| 174 | &(stack)->e[top]; \ |
| 175 | }) |
| 176 | |
| 177 | #define estack_bx(stack, top) \ |
| 178 | ({ \ |
| 179 | assert((top) > FILTER_STACK_EMPTY + 1); \ |
| 180 | &(stack)->e[(top) - 1]; \ |
| 181 | }) |
| 182 | |
| 183 | /* |
| 184 | * Currently, only integers (REG_S64) can be pushed into the stack. |
| 185 | */ |
| 186 | #define estack_push(stack, top, ax, bx, ax_t, bx_t) \ |
| 187 | do { \ |
| 188 | assert((top) < FILTER_STACK_LEN - 1); \ |
| 189 | (stack)->e[(top) - 1].u.v = (bx); \ |
| 190 | (stack)->e[(top) - 1].type = (bx_t); \ |
| 191 | (bx) = (ax); \ |
| 192 | (bx_t) = (ax_t); \ |
| 193 | ++(top); \ |
| 194 | } while (0) |
| 195 | |
| 196 | #define estack_pop(stack, top, ax, bx, ax_t, bx_t) \ |
| 197 | do { \ |
| 198 | assert((top) > FILTER_STACK_EMPTY); \ |
| 199 | (ax) = (bx); \ |
| 200 | (ax_t) = (bx_t); \ |
| 201 | (bx) = (stack)->e[(top) - 2].u.v; \ |
| 202 | (bx_t) = (stack)->e[(top) - 2].type; \ |
| 203 | (top)--; \ |
| 204 | } while (0) |
| 205 | |
| 206 | const char *print_op(enum filter_op op); |
| 207 | |
| 208 | int lttng_filter_validate_bytecode(struct bytecode_runtime *bytecode); |
| 209 | int lttng_filter_specialize_bytecode(struct bytecode_runtime *bytecode); |
| 210 | |
| 211 | uint64_t lttng_filter_false(void *filter_data, |
| 212 | const char *filter_stack_data); |
| 213 | uint64_t lttng_filter_interpret_bytecode(void *filter_data, |
| 214 | const char *filter_stack_data); |
| 215 | |
| 216 | #endif /* _LTTNG_FILTER_H */ |