| 1 | #ifndef _LTTNG_FILTER_H |
| 2 | #define _LTTNG_FILTER_H |
| 3 | |
| 4 | /* |
| 5 | * lttng-filter.h |
| 6 | * |
| 7 | * LTTng modules filter header. |
| 8 | * |
| 9 | * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 10 | * |
| 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 27 | * SOFTWARE. |
| 28 | */ |
| 29 | |
| 30 | #include <linux/kernel.h> |
| 31 | |
| 32 | #include <lttng-events.h> |
| 33 | #include <filter-bytecode.h> |
| 34 | |
| 35 | /* Filter stack length, in number of entries */ |
| 36 | #define FILTER_STACK_LEN 10 /* includes 2 dummy */ |
| 37 | #define FILTER_STACK_EMPTY 1 |
| 38 | |
| 39 | #ifdef DEBUG |
| 40 | #define dbg_printk(fmt, args...) \ |
| 41 | printk(KERN_DEBUG "[debug bytecode in %s:%s@%u] " fmt, \ |
| 42 | __FILE__, __func__, __LINE__, ## args) |
| 43 | #else |
| 44 | #define dbg_printk(fmt, args...) \ |
| 45 | do { \ |
| 46 | /* do nothing but check printf format */ \ |
| 47 | if (0) \ |
| 48 | printk(KERN_DEBUG "[debug bytecode in %s:%s@%u] " fmt, \ |
| 49 | __FILE__, __func__, __LINE__, ## args); \ |
| 50 | } while (0) |
| 51 | #endif |
| 52 | |
| 53 | /* Linked bytecode. Child of struct lttng_bytecode_runtime. */ |
| 54 | struct bytecode_runtime { |
| 55 | struct lttng_bytecode_runtime p; |
| 56 | uint16_t len; |
| 57 | char data[0]; |
| 58 | }; |
| 59 | |
| 60 | enum entry_type { |
| 61 | REG_S64, |
| 62 | REG_DOUBLE, |
| 63 | REG_STRING, |
| 64 | REG_STAR_GLOB_STRING, |
| 65 | REG_TYPE_UNKNOWN, |
| 66 | }; |
| 67 | |
| 68 | /* Validation stack */ |
| 69 | struct vstack_entry { |
| 70 | enum entry_type type; |
| 71 | }; |
| 72 | |
| 73 | struct vstack { |
| 74 | int top; /* top of stack */ |
| 75 | struct vstack_entry e[FILTER_STACK_LEN]; |
| 76 | }; |
| 77 | |
| 78 | static inline |
| 79 | void vstack_init(struct vstack *stack) |
| 80 | { |
| 81 | stack->top = -1; |
| 82 | } |
| 83 | |
| 84 | static inline |
| 85 | struct vstack_entry *vstack_ax(struct vstack *stack) |
| 86 | { |
| 87 | if (unlikely(stack->top < 0)) |
| 88 | return NULL; |
| 89 | return &stack->e[stack->top]; |
| 90 | } |
| 91 | |
| 92 | static inline |
| 93 | struct vstack_entry *vstack_bx(struct vstack *stack) |
| 94 | { |
| 95 | if (unlikely(stack->top < 1)) |
| 96 | return NULL; |
| 97 | return &stack->e[stack->top - 1]; |
| 98 | } |
| 99 | |
| 100 | static inline |
| 101 | int vstack_push(struct vstack *stack) |
| 102 | { |
| 103 | if (stack->top >= FILTER_STACK_LEN - 1) { |
| 104 | printk(KERN_WARNING "Stack full\n"); |
| 105 | return -EINVAL; |
| 106 | } |
| 107 | ++stack->top; |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | static inline |
| 112 | int vstack_pop(struct vstack *stack) |
| 113 | { |
| 114 | if (unlikely(stack->top < 0)) { |
| 115 | printk(KERN_WARNING "Stack empty\n"); |
| 116 | return -EINVAL; |
| 117 | } |
| 118 | stack->top--; |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | /* Execution stack */ |
| 123 | enum estack_string_literal_type { |
| 124 | ESTACK_STRING_LITERAL_TYPE_NONE, |
| 125 | ESTACK_STRING_LITERAL_TYPE_PLAIN, |
| 126 | ESTACK_STRING_LITERAL_TYPE_STAR_GLOB, |
| 127 | }; |
| 128 | |
| 129 | struct estack_entry { |
| 130 | union { |
| 131 | int64_t v; |
| 132 | |
| 133 | struct { |
| 134 | const char *str; |
| 135 | const char __user *user_str; |
| 136 | size_t seq_len; |
| 137 | enum estack_string_literal_type literal_type; |
| 138 | int user; /* is string from userspace ? */ |
| 139 | } s; |
| 140 | } u; |
| 141 | }; |
| 142 | |
| 143 | struct estack { |
| 144 | int top; /* top of stack */ |
| 145 | struct estack_entry e[FILTER_STACK_LEN]; |
| 146 | }; |
| 147 | |
| 148 | #define estack_ax_v ax |
| 149 | #define estack_bx_v bx |
| 150 | |
| 151 | #define estack_ax(stack, top) \ |
| 152 | ({ \ |
| 153 | BUG_ON((top) <= FILTER_STACK_EMPTY); \ |
| 154 | &(stack)->e[top]; \ |
| 155 | }) |
| 156 | |
| 157 | #define estack_bx(stack, top) \ |
| 158 | ({ \ |
| 159 | BUG_ON((top) <= FILTER_STACK_EMPTY + 1); \ |
| 160 | &(stack)->e[(top) - 1]; \ |
| 161 | }) |
| 162 | |
| 163 | #define estack_push(stack, top, ax, bx) \ |
| 164 | do { \ |
| 165 | BUG_ON((top) >= FILTER_STACK_LEN - 1); \ |
| 166 | (stack)->e[(top) - 1].u.v = (bx); \ |
| 167 | (bx) = (ax); \ |
| 168 | ++(top); \ |
| 169 | } while (0) |
| 170 | |
| 171 | #define estack_pop(stack, top, ax, bx) \ |
| 172 | do { \ |
| 173 | BUG_ON((top) <= FILTER_STACK_EMPTY); \ |
| 174 | (ax) = (bx); \ |
| 175 | (bx) = (stack)->e[(top) - 2].u.v; \ |
| 176 | (top)--; \ |
| 177 | } while (0) |
| 178 | |
| 179 | const char *lttng_filter_print_op(enum filter_op op); |
| 180 | |
| 181 | int lttng_filter_validate_bytecode(struct bytecode_runtime *bytecode); |
| 182 | int lttng_filter_specialize_bytecode(struct bytecode_runtime *bytecode); |
| 183 | |
| 184 | uint64_t lttng_filter_false(void *filter_data, |
| 185 | struct lttng_probe_ctx *lttng_probe_ctx, |
| 186 | const char *filter_stack_data); |
| 187 | uint64_t lttng_filter_interpret_bytecode(void *filter_data, |
| 188 | struct lttng_probe_ctx *lttng_probe_ctx, |
| 189 | const char *filter_stack_data); |
| 190 | |
| 191 | #endif /* _LTTNG_FILTER_H */ |