Move bytecode structures to internal header
[lttng-modules.git] / include / lttng / lttng-bytecode.h
CommitLineData
9f36eaed
MJ
1/* SPDX-License-Identifier: MIT
2 *
80c2a69a 3 * lttng/lttng-bytecode.h
07dfc1d0 4 *
80c2a69a 5 * LTTng modules bytecode header.
07dfc1d0 6 *
bbf3aef5 7 * Copyright (C) 2010-2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
07dfc1d0
MD
8 */
9
80c2a69a
FD
10#ifndef _LTTNG_BYTECODE_H
11#define _LTTNG_BYTECODE_H
9f36eaed 12
07dfc1d0
MD
13#include <linux/kernel.h>
14
2df37e95 15#include <lttng/events.h>
1efb0502 16#include <lttng/events-internal.h>
80c2a69a 17#include <lttng/bytecode.h>
07dfc1d0 18
80c2a69a
FD
19/* Interpreter stack length, in number of entries */
20#define INTERPRETER_STACK_LEN 10 /* includes 2 dummy */
21#define INTERPRETER_STACK_EMPTY 1
22#define INTERPRETER_MAX_DATA_LEN 65536
3834b99f 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
218585b9 38/* Linked bytecode. Child of struct lttng_kernel_bytecode_runtime. */
07dfc1d0 39struct bytecode_runtime {
218585b9 40 struct lttng_kernel_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
29a574c3
FD
75 OBJECT_TYPE_SIGNED_ENUM,
76 OBJECT_TYPE_UNSIGNED_ENUM,
77
3834b99f
MD
78 OBJECT_TYPE_DOUBLE,
79 OBJECT_TYPE_STRING,
80 OBJECT_TYPE_STRING_SEQUENCE,
81
82 OBJECT_TYPE_SEQUENCE,
83 OBJECT_TYPE_ARRAY,
84 OBJECT_TYPE_STRUCT,
85 OBJECT_TYPE_VARIANT,
86
87 OBJECT_TYPE_DYNAMIC,
88};
89
80c2a69a 90struct bytecode_get_index_data {
3834b99f
MD
91 uint64_t offset; /* in bytes */
92 size_t ctx_index;
93 size_t array_len;
03cb0cdd
FD
94 /*
95 * Field is only populated for LOAD_ROOT_CONTEXT, LOAD_ROOT_APP_CONTEXT
96 * and LOAD_ROOT_PAYLOAD. Left NULL for LOAD_OBJECT, considering that the
97 * interpreter needs to find it from the event fields and types to
98 * support variants.
99 */
437d5aa5 100 const struct lttng_kernel_event_field *field;
3834b99f
MD
101 struct {
102 size_t len;
103 enum object_type type;
104 bool rev_bo; /* reverse byte order */
105 } elem;
07dfc1d0
MD
106};
107
108/* Validation stack */
3834b99f
MD
109struct vstack_load {
110 enum load_type type;
111 enum object_type object_type;
437d5aa5 112 const struct lttng_kernel_event_field *field;
3834b99f
MD
113 bool rev_bo; /* reverse byte order */
114};
115
07dfc1d0
MD
116struct vstack_entry {
117 enum entry_type type;
3834b99f 118 struct vstack_load load;
07dfc1d0
MD
119};
120
121struct vstack {
122 int top; /* top of stack */
80c2a69a 123 struct vstack_entry e[INTERPRETER_STACK_LEN];
07dfc1d0
MD
124};
125
126static inline
127void vstack_init(struct vstack *stack)
128{
129 stack->top = -1;
130}
131
132static inline
133struct vstack_entry *vstack_ax(struct vstack *stack)
134{
135 if (unlikely(stack->top < 0))
136 return NULL;
137 return &stack->e[stack->top];
138}
139
140static inline
141struct vstack_entry *vstack_bx(struct vstack *stack)
142{
143 if (unlikely(stack->top < 1))
144 return NULL;
145 return &stack->e[stack->top - 1];
146}
147
148static inline
149int vstack_push(struct vstack *stack)
150{
80c2a69a 151 if (stack->top >= INTERPRETER_STACK_LEN - 1) {
5a15f70c 152 printk(KERN_WARNING "LTTng: filter: Stack full\n");
07dfc1d0
MD
153 return -EINVAL;
154 }
155 ++stack->top;
156 return 0;
157}
158
159static inline
160int vstack_pop(struct vstack *stack)
161{
162 if (unlikely(stack->top < 0)) {
5a15f70c 163 printk(KERN_WARNING "LTTng: filter: Stack empty\n");
07dfc1d0
MD
164 return -EINVAL;
165 }
166 stack->top--;
167 return 0;
168}
169
170/* Execution stack */
02aca193
PP
171enum estack_string_literal_type {
172 ESTACK_STRING_LITERAL_TYPE_NONE,
173 ESTACK_STRING_LITERAL_TYPE_PLAIN,
174 ESTACK_STRING_LITERAL_TYPE_STAR_GLOB,
175};
176
3834b99f
MD
177struct load_ptr {
178 enum load_type type;
179 enum object_type object_type;
180 const void *ptr;
03cb0cdd 181 size_t nr_elem;
3834b99f
MD
182 bool rev_bo;
183 /* Temporary place-holders for contexts. */
184 union {
185 int64_t s64;
186 uint64_t u64;
187 double d;
188 } u;
437d5aa5 189 const struct lttng_kernel_event_field *field;
3834b99f
MD
190};
191
07dfc1d0 192struct estack_entry {
d644d1df 193 enum entry_type type;
07dfc1d0
MD
194 union {
195 int64_t v;
196
197 struct {
198 const char *str;
f127e61e 199 const char __user *user_str;
07dfc1d0 200 size_t seq_len;
02aca193 201 enum estack_string_literal_type literal_type;
f127e61e 202 int user; /* is string from userspace ? */
07dfc1d0 203 } s;
3834b99f 204 struct load_ptr ptr;
07dfc1d0
MD
205 } u;
206};
207
208struct estack {
209 int top; /* top of stack */
80c2a69a 210 struct estack_entry e[INTERPRETER_STACK_LEN];
07dfc1d0
MD
211};
212
213#define estack_ax_v ax
214#define estack_bx_v bx
215
d644d1df
FD
216#define estack_ax_t ax_t
217#define estack_bx_t bx_t
218
07dfc1d0
MD
219#define estack_ax(stack, top) \
220 ({ \
80c2a69a 221 BUG_ON((top) <= INTERPRETER_STACK_EMPTY); \
07dfc1d0
MD
222 &(stack)->e[top]; \
223 })
224
225#define estack_bx(stack, top) \
226 ({ \
80c2a69a 227 BUG_ON((top) <= INTERPRETER_STACK_EMPTY + 1); \
07dfc1d0
MD
228 &(stack)->e[(top) - 1]; \
229 })
230
d644d1df 231#define estack_push(stack, top, ax, bx, ax_t, bx_t) \
07dfc1d0 232 do { \
80c2a69a 233 BUG_ON((top) >= INTERPRETER_STACK_LEN - 1); \
07dfc1d0 234 (stack)->e[(top) - 1].u.v = (bx); \
d644d1df 235 (stack)->e[(top) - 1].type = (bx_t); \
07dfc1d0 236 (bx) = (ax); \
d644d1df 237 (bx_t) = (ax_t); \
07dfc1d0
MD
238 ++(top); \
239 } while (0)
240
d644d1df 241#define estack_pop(stack, top, ax, bx, ax_t, bx_t) \
07dfc1d0 242 do { \
80c2a69a 243 BUG_ON((top) <= INTERPRETER_STACK_EMPTY); \
07dfc1d0 244 (ax) = (bx); \
d644d1df 245 (ax_t) = (bx_t); \
07dfc1d0 246 (bx) = (stack)->e[(top) - 2].u.v; \
d644d1df 247 (bx_t) = (stack)->e[(top) - 2].type; \
07dfc1d0
MD
248 (top)--; \
249 } while (0)
250
03cb0cdd
FD
251enum lttng_interpreter_type {
252 LTTNG_INTERPRETER_TYPE_S64,
253 LTTNG_INTERPRETER_TYPE_U64,
254 LTTNG_INTERPRETER_TYPE_SIGNED_ENUM,
255 LTTNG_INTERPRETER_TYPE_UNSIGNED_ENUM,
256 LTTNG_INTERPRETER_TYPE_DOUBLE,
257 LTTNG_INTERPRETER_TYPE_STRING,
258 LTTNG_INTERPRETER_TYPE_SEQUENCE,
259};
260
261/*
262 * Represents the output parameter of the lttng interpreter.
263 * Currently capturable field classes are integer, double, string and sequence
264 * of integer.
265 */
266struct lttng_interpreter_output {
267 enum lttng_interpreter_type type;
268 union {
269 int64_t s;
270 uint64_t u;
271
272 struct {
273 const char *str;
274 size_t len;
275 } str;
276 struct {
277 const void *ptr;
278 size_t nr_elem;
279
280 /* Inner type. */
437d5aa5 281 const struct lttng_kernel_type_common *nested_type;
03cb0cdd
FD
282 } sequence;
283 } u;
284};
285
80c2a69a 286const char *lttng_bytecode_print_op(enum bytecode_op op);
07dfc1d0 287
218585b9 288void lttng_bytecode_sync_state(struct lttng_kernel_bytecode_runtime *runtime);
80c2a69a 289int lttng_bytecode_validate(struct bytecode_runtime *bytecode);
437d5aa5 290int lttng_bytecode_specialize(const struct lttng_kernel_event_desc *event_desc,
3834b99f 291 struct bytecode_runtime *bytecode);
07dfc1d0 292
218585b9 293int lttng_bytecode_interpret_error(struct lttng_kernel_bytecode_runtime *bytecode_runtime,
8a445457 294 const char *stack_data,
a92e844e 295 struct lttng_kernel_probe_ctx *probe_ctx,
8a445457 296 void *ctx);
07dfc1d0 297
218585b9 298int lttng_bytecode_interpret(struct lttng_kernel_bytecode_runtime *kernel_bytecode,
8a445457 299 const char *interpreter_stack_data,
a92e844e 300 struct lttng_kernel_probe_ctx *lttng_probe_ctx,
8a445457 301 void *caller_ctx);
99d223ad 302
07dfc1d0 303#endif /* _LTTNG_FILTER_H */
This page took 0.054327 seconds and 4 git commands to generate.