Add `lttng_bytecode_interpret_format_output()` for top of stack extraction
[lttng-modules.git] / include / lttng / filter.h
CommitLineData
9f36eaed
MJ
1/* SPDX-License-Identifier: MIT
2 *
2df37e95 3 * lttng/filter.h
07dfc1d0
MD
4 *
5 * LTTng modules filter header.
6 *
bbf3aef5 7 * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
07dfc1d0
MD
8 */
9
9f36eaed
MJ
10#ifndef _LTTNG_FILTER_H
11#define _LTTNG_FILTER_H
12
07dfc1d0
MD
13#include <linux/kernel.h>
14
2df37e95
MD
15#include <lttng/events.h>
16#include <lttng/filter-bytecode.h>
07dfc1d0
MD
17
18/* Filter stack length, in number of entries */
19#define FILTER_STACK_LEN 10 /* includes 2 dummy */
20#define FILTER_STACK_EMPTY 1
21
3834b99f
MD
22#define FILTER_MAX_DATA_LEN 65536
23
07dfc1d0
MD
24#ifdef DEBUG
25#define dbg_printk(fmt, args...) \
5a15f70c 26 printk(KERN_DEBUG "LTTng: [debug bytecode in %s:%s@%u] " fmt, \
07dfc1d0
MD
27 __FILE__, __func__, __LINE__, ## args)
28#else
29#define dbg_printk(fmt, args...) \
30do { \
31 /* do nothing but check printf format */ \
32 if (0) \
5a15f70c 33 printk(KERN_DEBUG "LTTng: [debug bytecode in %s:%s@%u] " fmt, \
07dfc1d0
MD
34 __FILE__, __func__, __LINE__, ## args); \
35} while (0)
36#endif
37
38/* Linked bytecode. Child of struct lttng_bytecode_runtime. */
39struct bytecode_runtime {
40 struct lttng_bytecode_runtime p;
3834b99f
MD
41 size_t data_len;
42 size_t data_alloc_len;
43 char *data;
07dfc1d0 44 uint16_t len;
3834b99f 45 char code[0];
07dfc1d0
MD
46};
47
48enum entry_type {
49 REG_S64,
1242217a 50 REG_U64,
07dfc1d0
MD
51 REG_DOUBLE,
52 REG_STRING,
02aca193 53 REG_STAR_GLOB_STRING,
07dfc1d0 54 REG_TYPE_UNKNOWN,
3834b99f
MD
55 REG_PTR,
56};
57
58enum load_type {
59 LOAD_ROOT_CONTEXT,
60 LOAD_ROOT_APP_CONTEXT,
61 LOAD_ROOT_PAYLOAD,
62 LOAD_OBJECT,
63};
64
65enum object_type {
66 OBJECT_TYPE_S8,
67 OBJECT_TYPE_S16,
68 OBJECT_TYPE_S32,
69 OBJECT_TYPE_S64,
70 OBJECT_TYPE_U8,
71 OBJECT_TYPE_U16,
72 OBJECT_TYPE_U32,
73 OBJECT_TYPE_U64,
74
75 OBJECT_TYPE_DOUBLE,
76 OBJECT_TYPE_STRING,
77 OBJECT_TYPE_STRING_SEQUENCE,
78
79 OBJECT_TYPE_SEQUENCE,
80 OBJECT_TYPE_ARRAY,
81 OBJECT_TYPE_STRUCT,
82 OBJECT_TYPE_VARIANT,
83
84 OBJECT_TYPE_DYNAMIC,
85};
86
87struct filter_get_index_data {
88 uint64_t offset; /* in bytes */
89 size_t ctx_index;
90 size_t array_len;
03cb0cdd
FD
91 /*
92 * Field is only populated for LOAD_ROOT_CONTEXT, LOAD_ROOT_APP_CONTEXT
93 * and LOAD_ROOT_PAYLOAD. Left NULL for LOAD_OBJECT, considering that the
94 * interpreter needs to find it from the event fields and types to
95 * support variants.
96 */
97 const struct lttng_event_field *field;
3834b99f
MD
98 struct {
99 size_t len;
100 enum object_type type;
101 bool rev_bo; /* reverse byte order */
102 } elem;
07dfc1d0
MD
103};
104
105/* Validation stack */
3834b99f
MD
106struct vstack_load {
107 enum load_type type;
108 enum object_type object_type;
109 const struct lttng_event_field *field;
110 bool rev_bo; /* reverse byte order */
111};
112
07dfc1d0
MD
113struct vstack_entry {
114 enum entry_type type;
3834b99f 115 struct vstack_load load;
07dfc1d0
MD
116};
117
118struct vstack {
119 int top; /* top of stack */
120 struct vstack_entry e[FILTER_STACK_LEN];
121};
122
123static inline
124void vstack_init(struct vstack *stack)
125{
126 stack->top = -1;
127}
128
129static inline
130struct vstack_entry *vstack_ax(struct vstack *stack)
131{
132 if (unlikely(stack->top < 0))
133 return NULL;
134 return &stack->e[stack->top];
135}
136
137static inline
138struct vstack_entry *vstack_bx(struct vstack *stack)
139{
140 if (unlikely(stack->top < 1))
141 return NULL;
142 return &stack->e[stack->top - 1];
143}
144
145static inline
146int vstack_push(struct vstack *stack)
147{
148 if (stack->top >= FILTER_STACK_LEN - 1) {
5a15f70c 149 printk(KERN_WARNING "LTTng: filter: Stack full\n");
07dfc1d0
MD
150 return -EINVAL;
151 }
152 ++stack->top;
153 return 0;
154}
155
156static inline
157int vstack_pop(struct vstack *stack)
158{
159 if (unlikely(stack->top < 0)) {
5a15f70c 160 printk(KERN_WARNING "LTTng: filter: Stack empty\n");
07dfc1d0
MD
161 return -EINVAL;
162 }
163 stack->top--;
164 return 0;
165}
166
167/* Execution stack */
02aca193
PP
168enum estack_string_literal_type {
169 ESTACK_STRING_LITERAL_TYPE_NONE,
170 ESTACK_STRING_LITERAL_TYPE_PLAIN,
171 ESTACK_STRING_LITERAL_TYPE_STAR_GLOB,
172};
173
3834b99f
MD
174struct load_ptr {
175 enum load_type type;
176 enum object_type object_type;
177 const void *ptr;
03cb0cdd 178 size_t nr_elem;
3834b99f
MD
179 bool rev_bo;
180 /* Temporary place-holders for contexts. */
181 union {
182 int64_t s64;
183 uint64_t u64;
184 double d;
185 } u;
3834b99f
MD
186 const struct lttng_event_field *field;
187};
188
07dfc1d0 189struct estack_entry {
d644d1df 190 enum entry_type type;
07dfc1d0
MD
191 union {
192 int64_t v;
193
194 struct {
195 const char *str;
f127e61e 196 const char __user *user_str;
07dfc1d0 197 size_t seq_len;
02aca193 198 enum estack_string_literal_type literal_type;
f127e61e 199 int user; /* is string from userspace ? */
07dfc1d0 200 } s;
3834b99f 201 struct load_ptr ptr;
07dfc1d0
MD
202 } u;
203};
204
205struct estack {
206 int top; /* top of stack */
207 struct estack_entry e[FILTER_STACK_LEN];
208};
209
210#define estack_ax_v ax
211#define estack_bx_v bx
212
d644d1df
FD
213#define estack_ax_t ax_t
214#define estack_bx_t bx_t
215
07dfc1d0
MD
216#define estack_ax(stack, top) \
217 ({ \
5f12cb8d 218 BUG_ON((top) <= FILTER_STACK_EMPTY); \
07dfc1d0
MD
219 &(stack)->e[top]; \
220 })
221
222#define estack_bx(stack, top) \
223 ({ \
5f12cb8d 224 BUG_ON((top) <= FILTER_STACK_EMPTY + 1); \
07dfc1d0
MD
225 &(stack)->e[(top) - 1]; \
226 })
227
d644d1df 228#define estack_push(stack, top, ax, bx, ax_t, bx_t) \
07dfc1d0 229 do { \
5f12cb8d 230 BUG_ON((top) >= FILTER_STACK_LEN - 1); \
07dfc1d0 231 (stack)->e[(top) - 1].u.v = (bx); \
d644d1df 232 (stack)->e[(top) - 1].type = (bx_t); \
07dfc1d0 233 (bx) = (ax); \
d644d1df 234 (bx_t) = (ax_t); \
07dfc1d0
MD
235 ++(top); \
236 } while (0)
237
d644d1df 238#define estack_pop(stack, top, ax, bx, ax_t, bx_t) \
07dfc1d0 239 do { \
5f12cb8d 240 BUG_ON((top) <= FILTER_STACK_EMPTY); \
07dfc1d0 241 (ax) = (bx); \
d644d1df 242 (ax_t) = (bx_t); \
07dfc1d0 243 (bx) = (stack)->e[(top) - 2].u.v; \
d644d1df 244 (bx_t) = (stack)->e[(top) - 2].type; \
07dfc1d0
MD
245 (top)--; \
246 } while (0)
247
03cb0cdd
FD
248enum lttng_interpreter_type {
249 LTTNG_INTERPRETER_TYPE_S64,
250 LTTNG_INTERPRETER_TYPE_U64,
251 LTTNG_INTERPRETER_TYPE_SIGNED_ENUM,
252 LTTNG_INTERPRETER_TYPE_UNSIGNED_ENUM,
253 LTTNG_INTERPRETER_TYPE_DOUBLE,
254 LTTNG_INTERPRETER_TYPE_STRING,
255 LTTNG_INTERPRETER_TYPE_SEQUENCE,
256};
257
258/*
259 * Represents the output parameter of the lttng interpreter.
260 * Currently capturable field classes are integer, double, string and sequence
261 * of integer.
262 */
263struct lttng_interpreter_output {
264 enum lttng_interpreter_type type;
265 union {
266 int64_t s;
267 uint64_t u;
268
269 struct {
270 const char *str;
271 size_t len;
272 } str;
273 struct {
274 const void *ptr;
275 size_t nr_elem;
276
277 /* Inner type. */
278 const struct lttng_type *nested_type;
279 } sequence;
280 } u;
281};
282
07dfc1d0
MD
283const char *lttng_filter_print_op(enum filter_op op);
284
285int lttng_filter_validate_bytecode(struct bytecode_runtime *bytecode);
2dfda770 286int lttng_filter_specialize_bytecode(const struct lttng_event_desc *event_desc,
3834b99f 287 struct bytecode_runtime *bytecode);
07dfc1d0 288
03cb0cdd 289uint64_t lttng_filter_interpret_bytecode_false(void *filter_data,
79150a49 290 struct lttng_probe_ctx *lttng_probe_ctx,
07dfc1d0
MD
291 const char *filter_stack_data);
292uint64_t lttng_filter_interpret_bytecode(void *filter_data,
79150a49 293 struct lttng_probe_ctx *lttng_probe_ctx,
07dfc1d0
MD
294 const char *filter_stack_data);
295
296#endif /* _LTTNG_FILTER_H */
This page took 0.047909 seconds and 4 git commands to generate.