| 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-2014 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 <linux/kernel.h> |
| 27 | |
| 28 | #include <lttng-events.h> |
| 29 | #include <filter-bytecode.h> |
| 30 | |
| 31 | /* Filter stack length, in number of entries */ |
| 32 | #define FILTER_STACK_LEN 10 /* includes 2 dummy */ |
| 33 | #define FILTER_STACK_EMPTY 1 |
| 34 | |
| 35 | #ifdef DEBUG |
| 36 | #define dbg_printk(fmt, args...) \ |
| 37 | printk(KERN_DEBUG "[debug bytecode in %s:%s@%u] " fmt, \ |
| 38 | __FILE__, __func__, __LINE__, ## args) |
| 39 | #else |
| 40 | #define dbg_printk(fmt, args...) \ |
| 41 | do { \ |
| 42 | /* do nothing but check printf format */ \ |
| 43 | if (0) \ |
| 44 | printk(KERN_DEBUG "[debug bytecode in %s:%s@%u] " fmt, \ |
| 45 | __FILE__, __func__, __LINE__, ## args); \ |
| 46 | } while (0) |
| 47 | #endif |
| 48 | |
| 49 | /* Linked bytecode. Child of struct lttng_bytecode_runtime. */ |
| 50 | struct bytecode_runtime { |
| 51 | struct lttng_bytecode_runtime p; |
| 52 | uint16_t len; |
| 53 | char data[0]; |
| 54 | }; |
| 55 | |
| 56 | enum entry_type { |
| 57 | REG_S64, |
| 58 | REG_DOUBLE, |
| 59 | REG_STRING, |
| 60 | REG_TYPE_UNKNOWN, |
| 61 | }; |
| 62 | |
| 63 | /* Validation stack */ |
| 64 | struct vstack_entry { |
| 65 | enum entry_type type; |
| 66 | }; |
| 67 | |
| 68 | struct vstack { |
| 69 | int top; /* top of stack */ |
| 70 | struct vstack_entry e[FILTER_STACK_LEN]; |
| 71 | }; |
| 72 | |
| 73 | static inline |
| 74 | void vstack_init(struct vstack *stack) |
| 75 | { |
| 76 | stack->top = -1; |
| 77 | } |
| 78 | |
| 79 | static inline |
| 80 | struct vstack_entry *vstack_ax(struct vstack *stack) |
| 81 | { |
| 82 | if (unlikely(stack->top < 0)) |
| 83 | return NULL; |
| 84 | return &stack->e[stack->top]; |
| 85 | } |
| 86 | |
| 87 | static inline |
| 88 | struct vstack_entry *vstack_bx(struct vstack *stack) |
| 89 | { |
| 90 | if (unlikely(stack->top < 1)) |
| 91 | return NULL; |
| 92 | return &stack->e[stack->top - 1]; |
| 93 | } |
| 94 | |
| 95 | static inline |
| 96 | int vstack_push(struct vstack *stack) |
| 97 | { |
| 98 | if (stack->top >= FILTER_STACK_LEN - 1) { |
| 99 | printk(KERN_WARNING "Stack full\n"); |
| 100 | return -EINVAL; |
| 101 | } |
| 102 | ++stack->top; |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static inline |
| 107 | int vstack_pop(struct vstack *stack) |
| 108 | { |
| 109 | if (unlikely(stack->top < 0)) { |
| 110 | printk(KERN_WARNING "Stack empty\n"); |
| 111 | return -EINVAL; |
| 112 | } |
| 113 | stack->top--; |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | /* Execution stack */ |
| 118 | struct estack_entry { |
| 119 | union { |
| 120 | int64_t v; |
| 121 | |
| 122 | struct { |
| 123 | const char *str; |
| 124 | const char __user *user_str; |
| 125 | size_t seq_len; |
| 126 | int literal; /* is string literal ? */ |
| 127 | int user; /* is string from userspace ? */ |
| 128 | } s; |
| 129 | } u; |
| 130 | }; |
| 131 | |
| 132 | struct estack { |
| 133 | int top; /* top of stack */ |
| 134 | struct estack_entry e[FILTER_STACK_LEN]; |
| 135 | }; |
| 136 | |
| 137 | #define estack_ax_v ax |
| 138 | #define estack_bx_v bx |
| 139 | |
| 140 | #define estack_ax(stack, top) \ |
| 141 | ({ \ |
| 142 | WARN_ON_ONCE((top) <= FILTER_STACK_EMPTY); \ |
| 143 | &(stack)->e[top]; \ |
| 144 | }) |
| 145 | |
| 146 | #define estack_bx(stack, top) \ |
| 147 | ({ \ |
| 148 | WARN_ON_ONCE((top) <= FILTER_STACK_EMPTY + 1); \ |
| 149 | &(stack)->e[(top) - 1]; \ |
| 150 | }) |
| 151 | |
| 152 | #define estack_push(stack, top, ax, bx) \ |
| 153 | do { \ |
| 154 | WARN_ON_ONCE((top) >= FILTER_STACK_LEN - 1); \ |
| 155 | (stack)->e[(top) - 1].u.v = (bx); \ |
| 156 | (bx) = (ax); \ |
| 157 | ++(top); \ |
| 158 | } while (0) |
| 159 | |
| 160 | #define estack_pop(stack, top, ax, bx) \ |
| 161 | do { \ |
| 162 | WARN_ON_ONCE((top) <= FILTER_STACK_EMPTY); \ |
| 163 | (ax) = (bx); \ |
| 164 | (bx) = (stack)->e[(top) - 2].u.v; \ |
| 165 | (top)--; \ |
| 166 | } while (0) |
| 167 | |
| 168 | const char *lttng_filter_print_op(enum filter_op op); |
| 169 | |
| 170 | int lttng_filter_validate_bytecode(struct bytecode_runtime *bytecode); |
| 171 | int lttng_filter_specialize_bytecode(struct bytecode_runtime *bytecode); |
| 172 | |
| 173 | uint64_t lttng_filter_false(void *filter_data, |
| 174 | struct lttng_probe_ctx *lttng_probe_ctx, |
| 175 | const char *filter_stack_data); |
| 176 | uint64_t lttng_filter_interpret_bytecode(void *filter_data, |
| 177 | struct lttng_probe_ctx *lttng_probe_ctx, |
| 178 | const char *filter_stack_data); |
| 179 | |
| 180 | #endif /* _LTTNG_FILTER_H */ |