Commit | Line | Data |
---|---|---|
4624dad0 SM |
1 | /* |
2 | * Copyright (C) 2021 Simon Marchi <simon.marchi@efficios.com> | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0-only | |
5 | * | |
6 | */ | |
7 | ||
8 | #include <ctype.h> | |
9 | #include <stdio.h> | |
57739a6b | 10 | #include <string.h> |
d50d200a | 11 | #include <stdarg.h> |
4624dad0 | 12 | |
c9e313bc SM |
13 | #include "../command.hpp" |
14 | #include "../loglevel.hpp" | |
15 | #include "../uprobe.hpp" | |
4624dad0 SM |
16 | |
17 | #include "common/argpar/argpar.h" | |
c9e313bc SM |
18 | #include "common/argpar-utils/argpar-utils.hpp" |
19 | #include "common/dynamic-array.hpp" | |
20 | #include "common/mi-lttng.hpp" | |
21 | #include "common/string-utils/string-utils.hpp" | |
22 | #include "common/utils.hpp" | |
23 | #include <lttng/domain-internal.hpp> | |
4624dad0 | 24 | /* For lttng_event_rule_type_str(). */ |
c9e313bc | 25 | #include <lttng/event-rule/event-rule-internal.hpp> |
4624dad0 | 26 | #include <lttng/lttng.h> |
c9e313bc SM |
27 | #include "common/filter/filter-ast.hpp" |
28 | #include "common/filter/filter-ir.hpp" | |
29 | #include "common/dynamic-array.hpp" | |
4624dad0 SM |
30 | |
31 | #if (LTTNG_SYMBOL_NAME_LEN == 256) | |
32 | #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255" | |
33 | #endif | |
34 | ||
35 | #ifdef LTTNG_EMBED_HELP | |
36 | static const char help_msg[] = | |
37 | #include <lttng-add-trigger.1.h> | |
38 | ; | |
39 | #endif | |
40 | ||
41 | enum { | |
42 | OPT_HELP, | |
43 | OPT_LIST_OPTIONS, | |
44 | ||
45 | OPT_CONDITION, | |
46 | OPT_ACTION, | |
47 | OPT_ID, | |
481c5310 | 48 | OPT_OWNER_UID, |
bbadb5e0 | 49 | OPT_RATE_POLICY, |
4624dad0 | 50 | |
50ad0862 | 51 | OPT_NAME, |
4624dad0 | 52 | OPT_FILTER, |
b03a81fb | 53 | OPT_EXCLUDE_NAME, |
50ad0862 | 54 | OPT_EVENT_NAME, |
949f049b | 55 | OPT_LOG_LEVEL, |
4624dad0 | 56 | |
24de704e SM |
57 | OPT_TYPE, |
58 | OPT_LOCATION, | |
4624dad0 | 59 | |
4624dad0 SM |
60 | OPT_MAX_SIZE, |
61 | OPT_DATA_URL, | |
62 | OPT_CTRL_URL, | |
63 | OPT_URL, | |
64 | OPT_PATH, | |
20a01d15 SM |
65 | |
66 | OPT_CAPTURE, | |
4624dad0 SM |
67 | }; |
68 | ||
69 | static const struct argpar_opt_descr event_rule_opt_descrs[] = { | |
4624dad0 | 70 | { OPT_FILTER, 'f', "filter", true }, |
50ad0862 | 71 | { OPT_NAME, 'n', "name", true }, |
b03a81fb | 72 | { OPT_EXCLUDE_NAME, 'x', "exclude-name", true }, |
949f049b | 73 | { OPT_LOG_LEVEL, 'l', "log-level", true }, |
50ad0862 | 74 | { OPT_EVENT_NAME, 'E', "event-name", true }, |
4624dad0 | 75 | |
24de704e SM |
76 | { OPT_TYPE, 't', "type", true }, |
77 | { OPT_LOCATION, 'L', "location", true }, | |
4624dad0 | 78 | |
20a01d15 SM |
79 | /* Capture descriptor */ |
80 | { OPT_CAPTURE, '\0', "capture", true }, | |
81 | ||
4624dad0 SM |
82 | ARGPAR_OPT_DESCR_SENTINEL |
83 | }; | |
84 | ||
85 | static | |
695f7044 | 86 | bool has_syscall_prefix(const char *arg) |
4624dad0 | 87 | { |
695f7044 JR |
88 | bool matches = false; |
89 | const char kernel_syscall_type_opt_prefix[] = "kernel:syscall"; | |
90 | const size_t kernel_syscall_type_opt_prefix_len = | |
91 | sizeof(kernel_syscall_type_opt_prefix) - 1; | |
92 | const char syscall_type_opt_prefix[] = "syscall"; | |
93 | const size_t syscall_type_opt_prefix_len = | |
94 | sizeof(syscall_type_opt_prefix) - 1; | |
95 | ||
96 | if (strncmp(arg, syscall_type_opt_prefix, | |
97 | syscall_type_opt_prefix_len) == 0) { | |
98 | matches = true; | |
99 | } else if (strncmp(arg, kernel_syscall_type_opt_prefix, | |
100 | kernel_syscall_type_opt_prefix_len) == 0) { | |
101 | matches = true; | |
4624dad0 | 102 | } else { |
695f7044 | 103 | matches = false; |
4624dad0 SM |
104 | } |
105 | ||
695f7044 | 106 | return matches; |
4624dad0 SM |
107 | } |
108 | ||
109 | static | |
24de704e | 110 | bool assign_event_rule_type(enum lttng_event_rule_type *dest, const char *arg) |
4624dad0 SM |
111 | { |
112 | bool ret; | |
113 | ||
24de704e SM |
114 | if (*dest != LTTNG_EVENT_RULE_TYPE_UNKNOWN) { |
115 | ERR("More than one `--type` was specified."); | |
116 | goto error; | |
117 | } | |
118 | ||
695f7044 JR |
119 | if (strcmp(arg, "user") == 0 || strcmp(arg, "user:tracepoint") == 0) { |
120 | *dest = LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT; | |
121 | } else if (strcmp(arg, "kernel") == 0 || | |
122 | strcmp(arg, "kernel:tracepoint") == 0) { | |
123 | *dest = LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT; | |
124 | } else if (strcmp(arg, "jul") == 0 || strcmp(arg, "jul:logging") == 0) { | |
125 | *dest = LTTNG_EVENT_RULE_TYPE_JUL_LOGGING; | |
126 | } else if (strcmp(arg, "log4j") == 0 || | |
127 | strcmp(arg, "log4j:logging") == 0) { | |
128 | *dest = LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING; | |
129 | } else if (strcmp(arg, "python") == 0 || | |
130 | strcmp(arg, "python:logging") == 0) { | |
131 | *dest = LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING; | |
57739a6b | 132 | } else if (strcmp(arg, "kprobe") == 0 || |
85522de5 JR |
133 | strcmp(arg, "kernel:kprobe") == 0) { |
134 | *dest = LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE; | |
46fd07ac JR |
135 | } else if (strcmp(arg, "kernel:uprobe") == 0) { |
136 | *dest = LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE; | |
695f7044 | 137 | } else if (has_syscall_prefix(arg)) { |
57739a6b JR |
138 | /* |
139 | * Matches the following: | |
140 | * - syscall | |
141 | * - syscall:entry | |
142 | * - syscall:exit | |
143 | * - syscall:entry+exit | |
144 | * - syscall:* | |
4f7da553 JR |
145 | * - kernel:syscall |
146 | * - kernel:syscall:entry | |
147 | * - kernel:syscall:exit | |
148 | * - kernel:syscall:entry+exit | |
149 | * - kernel:syscall:* | |
57739a6b JR |
150 | * |
151 | * Validation for the right side is left to further usage sites. | |
152 | */ | |
4f7da553 | 153 | *dest = LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL; |
4624dad0 | 154 | } else { |
24de704e SM |
155 | ERR("Invalid `--type` value: %s", arg); |
156 | goto error; | |
4624dad0 SM |
157 | } |
158 | ||
24de704e SM |
159 | ret = true; |
160 | goto end; | |
161 | ||
162 | error: | |
163 | ret = false; | |
164 | ||
165 | end: | |
4624dad0 SM |
166 | return ret; |
167 | } | |
168 | ||
169 | static | |
170 | bool assign_string(char **dest, const char *src, const char *opt_name) | |
171 | { | |
172 | bool ret; | |
173 | ||
174 | if (*dest) { | |
175 | ERR("Duplicate '%s' given.", opt_name); | |
176 | goto error; | |
177 | } | |
178 | ||
179 | *dest = strdup(src); | |
180 | if (!*dest) { | |
181 | PERROR("Failed to allocate string '%s'.", opt_name); | |
182 | goto error; | |
183 | } | |
184 | ||
185 | ret = true; | |
186 | goto end; | |
187 | ||
188 | error: | |
189 | ret = false; | |
190 | ||
191 | end: | |
192 | return ret; | |
193 | } | |
194 | ||
57739a6b | 195 | static bool parse_syscall_emission_site_from_type(const char *str, |
4f7da553 | 196 | enum lttng_event_rule_kernel_syscall_emission_site *type) |
57739a6b JR |
197 | { |
198 | bool ret = false; | |
4f7da553 JR |
199 | const char kernel_prefix[] = "kernel:"; |
200 | const size_t kernel_prefix_len = sizeof(kernel_prefix) - 1; | |
201 | ||
202 | /* | |
203 | * If the passed string is of the form "kernel:syscall*", move the | |
204 | * pointer passed "kernel:". | |
205 | */ | |
206 | if (strncmp(str, kernel_prefix, kernel_prefix_len) == 0) { | |
207 | str = &str[kernel_prefix_len]; | |
208 | } | |
209 | ||
57739a6b JR |
210 | if (strcmp(str, "syscall") == 0 || |
211 | strcmp(str, "syscall:entry+exit") == 0) { | |
4f7da553 | 212 | *type = LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY_EXIT; |
57739a6b | 213 | } else if (strcmp(str, "syscall:entry") == 0) { |
4f7da553 | 214 | *type = LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY; |
57739a6b | 215 | } else if (strcmp(str, "syscall:exit") == 0) { |
4f7da553 | 216 | *type = LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_EXIT; |
57739a6b JR |
217 | } else { |
218 | goto error; | |
219 | } | |
220 | ||
221 | ret = true; | |
222 | ||
223 | error: | |
224 | return ret; | |
225 | } | |
226 | ||
4624dad0 | 227 | /* |
695f7044 | 228 | * Parse `str` as a log level against the passed event rule type. |
949f049b SM |
229 | * |
230 | * Return the log level in `*log_level`. Return true in `*log_level_only` if | |
231 | * the string specifies exactly this log level, false if it specifies at least | |
232 | * this log level. | |
233 | * | |
234 | * Return true if the string was successfully parsed as a log level string. | |
4624dad0 | 235 | */ |
949f049b | 236 | static bool parse_log_level_string(const char *str, |
695f7044 | 237 | enum lttng_event_rule_type event_rule_type, |
949f049b SM |
238 | int *log_level, |
239 | bool *log_level_only) | |
4624dad0 | 240 | { |
949f049b SM |
241 | bool ret; |
242 | ||
695f7044 JR |
243 | switch (event_rule_type) { |
244 | case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT: | |
4624dad0 | 245 | { |
949f049b SM |
246 | enum lttng_loglevel log_level_min, log_level_max; |
247 | if (!loglevel_parse_range_string( | |
248 | str, &log_level_min, &log_level_max)) { | |
249 | goto error; | |
250 | } | |
4624dad0 | 251 | |
949f049b SM |
252 | /* Only support VAL and VAL.. for now. */ |
253 | if (log_level_min != log_level_max && | |
254 | log_level_max != LTTNG_LOGLEVEL_EMERG) { | |
255 | goto error; | |
256 | } | |
257 | ||
258 | *log_level = (int) log_level_min; | |
259 | *log_level_only = log_level_min == log_level_max; | |
260 | break; | |
4624dad0 | 261 | } |
695f7044 | 262 | case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING: |
4624dad0 | 263 | { |
949f049b SM |
264 | enum lttng_loglevel_log4j log_level_min, log_level_max; |
265 | if (!loglevel_log4j_parse_range_string( | |
266 | str, &log_level_min, &log_level_max)) { | |
267 | goto error; | |
268 | } | |
4624dad0 | 269 | |
949f049b SM |
270 | /* Only support VAL and VAL.. for now. */ |
271 | if (log_level_min != log_level_max && | |
272 | log_level_max != LTTNG_LOGLEVEL_LOG4J_FATAL) { | |
273 | goto error; | |
274 | } | |
275 | ||
276 | *log_level = (int) log_level_min; | |
277 | *log_level_only = log_level_min == log_level_max; | |
278 | break; | |
4624dad0 | 279 | } |
695f7044 | 280 | case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING: |
4624dad0 | 281 | { |
949f049b SM |
282 | enum lttng_loglevel_jul log_level_min, log_level_max; |
283 | if (!loglevel_jul_parse_range_string( | |
284 | str, &log_level_min, &log_level_max)) { | |
285 | goto error; | |
286 | } | |
4624dad0 | 287 | |
949f049b SM |
288 | /* Only support VAL and VAL.. for now. */ |
289 | if (log_level_min != log_level_max && | |
290 | log_level_max != LTTNG_LOGLEVEL_JUL_SEVERE) { | |
291 | goto error; | |
292 | } | |
293 | ||
294 | *log_level = (int) log_level_min; | |
295 | *log_level_only = log_level_min == log_level_max; | |
296 | break; | |
4624dad0 | 297 | } |
695f7044 | 298 | case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING: |
4624dad0 | 299 | { |
949f049b SM |
300 | enum lttng_loglevel_python log_level_min, log_level_max; |
301 | if (!loglevel_python_parse_range_string( | |
302 | str, &log_level_min, &log_level_max)) { | |
303 | goto error; | |
304 | } | |
4624dad0 | 305 | |
949f049b SM |
306 | /* Only support VAL and VAL.. for now. */ |
307 | if (log_level_min != log_level_max && | |
308 | log_level_max != | |
309 | LTTNG_LOGLEVEL_PYTHON_CRITICAL) { | |
310 | goto error; | |
311 | } | |
312 | ||
313 | *log_level = (int) log_level_min; | |
314 | *log_level_only = log_level_min == log_level_max; | |
315 | break; | |
4624dad0 SM |
316 | } |
317 | default: | |
318 | /* Invalid domain type. */ | |
319 | abort(); | |
320 | } | |
949f049b SM |
321 | |
322 | ret = true; | |
323 | goto end; | |
324 | ||
325 | error: | |
326 | ret = false; | |
327 | ||
328 | end: | |
329 | return ret; | |
4624dad0 SM |
330 | } |
331 | ||
332 | static int parse_kernel_probe_opts(const char *source, | |
333 | struct lttng_kernel_probe_location **location) | |
334 | { | |
335 | int ret = 0; | |
336 | int match; | |
337 | char s_hex[19]; | |
338 | char name[LTTNG_SYMBOL_NAME_LEN]; | |
339 | char *symbol_name = NULL; | |
340 | uint64_t offset; | |
341 | ||
342 | /* Check for symbol+offset. */ | |
343 | match = sscanf(source, | |
344 | "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API | |
345 | "[^'+']+%18s", | |
346 | name, s_hex); | |
347 | if (match == 2) { | |
348 | if (*s_hex == '\0') { | |
349 | ERR("Kernel probe symbol offset is missing."); | |
350 | goto error; | |
351 | } | |
352 | ||
353 | symbol_name = strndup(name, LTTNG_SYMBOL_NAME_LEN); | |
354 | if (!symbol_name) { | |
355 | PERROR("Failed to copy kernel probe location symbol name."); | |
356 | goto error; | |
357 | } | |
44e63aa7 | 358 | offset = strtoull(s_hex, NULL, 0); |
4624dad0 SM |
359 | |
360 | *location = lttng_kernel_probe_location_symbol_create( | |
361 | symbol_name, offset); | |
f6e30a8a | 362 | if (!*location) { |
4624dad0 SM |
363 | ERR("Failed to create symbol kernel probe location."); |
364 | goto error; | |
365 | } | |
366 | ||
367 | goto end; | |
368 | } | |
369 | ||
370 | /* Check for symbol. */ | |
371 | if (isalpha(name[0]) || name[0] == '_') { | |
372 | match = sscanf(source, | |
373 | "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API | |
374 | "s", | |
375 | name); | |
376 | if (match == 1) { | |
377 | symbol_name = strndup(name, LTTNG_SYMBOL_NAME_LEN); | |
378 | if (!symbol_name) { | |
379 | ERR("Failed to copy kernel probe location symbol name."); | |
380 | goto error; | |
381 | } | |
382 | ||
383 | *location = lttng_kernel_probe_location_symbol_create( | |
384 | symbol_name, 0); | |
14f27f79 | 385 | if (!*location) { |
4624dad0 SM |
386 | ERR("Failed to create symbol kernel probe location."); |
387 | goto error; | |
388 | } | |
389 | ||
390 | goto end; | |
391 | } | |
392 | } | |
393 | ||
394 | /* Check for address. */ | |
395 | match = sscanf(source, "%18s", s_hex); | |
396 | if (match > 0) { | |
397 | uint64_t address; | |
398 | ||
399 | if (*s_hex == '\0') { | |
400 | ERR("Invalid kernel probe location address."); | |
401 | goto error; | |
402 | } | |
403 | ||
44e63aa7 | 404 | address = strtoull(s_hex, NULL, 0); |
4624dad0 | 405 | *location = lttng_kernel_probe_location_address_create(address); |
5c6ca809 | 406 | if (!*location) { |
4624dad0 SM |
407 | ERR("Failed to create symbol kernel probe location."); |
408 | goto error; | |
409 | } | |
410 | ||
411 | goto end; | |
412 | } | |
413 | ||
414 | error: | |
415 | /* No match */ | |
416 | ret = -1; | |
417 | *location = NULL; | |
418 | ||
419 | end: | |
420 | free(symbol_name); | |
421 | return ret; | |
422 | } | |
423 | ||
20a01d15 SM |
424 | static |
425 | struct lttng_event_expr *ir_op_load_expr_to_event_expr( | |
3363a4c3 JG |
426 | const struct ir_load_expression *load_expr, |
427 | const char *capture_str) | |
20a01d15 SM |
428 | { |
429 | char *provider_name = NULL; | |
430 | struct lttng_event_expr *event_expr = NULL; | |
3363a4c3 JG |
431 | const struct ir_load_expression_op *load_expr_op = load_expr->child; |
432 | const enum ir_load_expression_type load_expr_child_type = | |
433 | load_expr_op->type; | |
20a01d15 | 434 | |
3363a4c3 | 435 | switch (load_expr_child_type) { |
20a01d15 SM |
436 | case IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT: |
437 | case IR_LOAD_EXPRESSION_GET_CONTEXT_ROOT: | |
438 | { | |
439 | const char *field_name; | |
440 | ||
441 | load_expr_op = load_expr_op->next; | |
a0377dfe FD |
442 | LTTNG_ASSERT(load_expr_op); |
443 | LTTNG_ASSERT(load_expr_op->type == IR_LOAD_EXPRESSION_GET_SYMBOL); | |
20a01d15 | 444 | field_name = load_expr_op->u.symbol; |
a0377dfe | 445 | LTTNG_ASSERT(field_name); |
20a01d15 | 446 | |
3363a4c3 | 447 | event_expr = load_expr_child_type == IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT ? |
20a01d15 SM |
448 | lttng_event_expr_event_payload_field_create(field_name) : |
449 | lttng_event_expr_channel_context_field_create(field_name); | |
450 | if (!event_expr) { | |
451 | ERR("Failed to create %s event expression: field name = `%s`.", | |
3363a4c3 | 452 | load_expr_child_type == IR_LOAD_EXPRESSION_GET_PAYLOAD_ROOT ? |
20a01d15 SM |
453 | "payload field" : "channel context", |
454 | field_name); | |
455 | goto error; | |
456 | } | |
457 | ||
458 | break; | |
459 | } | |
460 | case IR_LOAD_EXPRESSION_GET_APP_CONTEXT_ROOT: | |
461 | { | |
462 | const char *colon; | |
463 | const char *type_name; | |
464 | const char *field_name; | |
465 | ||
466 | load_expr_op = load_expr_op->next; | |
a0377dfe FD |
467 | LTTNG_ASSERT(load_expr_op); |
468 | LTTNG_ASSERT(load_expr_op->type == IR_LOAD_EXPRESSION_GET_SYMBOL); | |
20a01d15 | 469 | field_name = load_expr_op->u.symbol; |
a0377dfe | 470 | LTTNG_ASSERT(field_name); |
20a01d15 SM |
471 | |
472 | /* | |
473 | * The field name needs to be of the form PROVIDER:TYPE. We | |
474 | * split it here. | |
475 | */ | |
476 | colon = strchr(field_name, ':'); | |
477 | if (!colon) { | |
478 | ERR("Invalid app-specific context field name: missing colon in `%s`.", | |
479 | field_name); | |
480 | goto error; | |
481 | } | |
482 | ||
483 | type_name = colon + 1; | |
484 | if (*type_name == '\0') { | |
485 | ERR("Invalid app-specific context field name: missing type name after colon in `%s`.", | |
486 | field_name); | |
487 | goto error; | |
488 | } | |
489 | ||
490 | provider_name = strndup(field_name, colon - field_name); | |
491 | if (!provider_name) { | |
492 | PERROR("Failed to allocate field name string"); | |
493 | goto error; | |
494 | } | |
495 | ||
496 | event_expr = lttng_event_expr_app_specific_context_field_create( | |
497 | provider_name, type_name); | |
498 | if (!event_expr) { | |
499 | ERR("Failed to create app-specific context field event expression: provider name = `%s`, type name = `%s`", | |
500 | provider_name, type_name); | |
501 | goto error; | |
502 | } | |
503 | ||
504 | break; | |
505 | } | |
506 | default: | |
507 | ERR("%s: unexpected load expr type %d.", __func__, | |
508 | load_expr_op->type); | |
509 | abort(); | |
510 | } | |
511 | ||
512 | load_expr_op = load_expr_op->next; | |
513 | ||
514 | /* There may be a single array index after that. */ | |
515 | if (load_expr_op->type == IR_LOAD_EXPRESSION_GET_INDEX) { | |
516 | struct lttng_event_expr *index_event_expr; | |
517 | const uint64_t index = load_expr_op->u.index; | |
518 | ||
519 | index_event_expr = lttng_event_expr_array_field_element_create(event_expr, index); | |
520 | if (!index_event_expr) { | |
521 | ERR("Failed to create array field element event expression."); | |
522 | goto error; | |
523 | } | |
524 | ||
525 | event_expr = index_event_expr; | |
526 | load_expr_op = load_expr_op->next; | |
527 | } | |
528 | ||
529 | switch (load_expr_op->type) { | |
530 | case IR_LOAD_EXPRESSION_LOAD_FIELD: | |
531 | /* | |
532 | * This is what we expect, IR_LOAD_EXPRESSION_LOAD_FIELD is | |
533 | * always found at the end of the chain. | |
534 | */ | |
535 | break; | |
536 | case IR_LOAD_EXPRESSION_GET_SYMBOL: | |
537 | ERR("While parsing expression `%s`: Capturing subfields is not supported.", | |
538 | capture_str); | |
539 | goto error; | |
540 | ||
541 | default: | |
542 | ERR("%s: unexpected load expression operator %s.", __func__, | |
543 | ir_load_expression_type_str(load_expr_op->type)); | |
544 | abort(); | |
545 | } | |
546 | ||
547 | goto end; | |
548 | ||
549 | error: | |
550 | lttng_event_expr_destroy(event_expr); | |
551 | event_expr = NULL; | |
552 | ||
553 | end: | |
554 | free(provider_name); | |
555 | ||
556 | return event_expr; | |
557 | } | |
558 | ||
559 | static | |
560 | struct lttng_event_expr *ir_op_load_to_event_expr( | |
561 | const struct ir_op *ir, const char *capture_str) | |
562 | { | |
563 | struct lttng_event_expr *event_expr = NULL; | |
564 | ||
a0377dfe | 565 | LTTNG_ASSERT(ir->op == IR_OP_LOAD); |
20a01d15 SM |
566 | |
567 | switch (ir->data_type) { | |
568 | case IR_DATA_EXPRESSION: | |
569 | { | |
570 | const struct ir_load_expression *ir_load_expr = | |
571 | ir->u.load.u.expression; | |
572 | ||
573 | event_expr = ir_op_load_expr_to_event_expr( | |
574 | ir_load_expr, capture_str); | |
575 | break; | |
576 | } | |
577 | default: | |
578 | ERR("%s: unexpected data type: %s.", __func__, | |
579 | ir_data_type_str(ir->data_type)); | |
580 | abort(); | |
581 | } | |
582 | ||
583 | return event_expr; | |
584 | } | |
585 | ||
586 | static | |
587 | const char *ir_operator_type_human_str(enum ir_op_type op) | |
588 | { | |
589 | const char *name; | |
590 | ||
591 | switch (op) { | |
592 | case IR_OP_BINARY: | |
593 | name = "Binary"; | |
594 | break; | |
595 | case IR_OP_UNARY: | |
596 | name = "Unary"; | |
597 | break; | |
598 | case IR_OP_LOGICAL: | |
599 | name = "Logical"; | |
600 | break; | |
601 | default: | |
602 | abort(); | |
603 | } | |
604 | ||
605 | return name; | |
606 | } | |
607 | ||
608 | static | |
609 | struct lttng_event_expr *ir_op_root_to_event_expr(const struct ir_op *ir, | |
610 | const char *capture_str) | |
611 | { | |
612 | struct lttng_event_expr *event_expr = NULL; | |
613 | ||
a0377dfe | 614 | LTTNG_ASSERT(ir->op == IR_OP_ROOT); |
20a01d15 SM |
615 | ir = ir->u.root.child; |
616 | ||
617 | switch (ir->op) { | |
618 | case IR_OP_LOAD: | |
619 | event_expr = ir_op_load_to_event_expr(ir, capture_str); | |
620 | break; | |
621 | case IR_OP_BINARY: | |
622 | case IR_OP_UNARY: | |
623 | case IR_OP_LOGICAL: | |
624 | ERR("While parsing expression `%s`: %s operators are not allowed in capture expressions.", | |
625 | capture_str, | |
626 | ir_operator_type_human_str(ir->op)); | |
627 | break; | |
628 | default: | |
629 | ERR("%s: unexpected IR op type: %s.", __func__, | |
630 | ir_op_type_str(ir->op)); | |
631 | abort(); | |
632 | } | |
633 | ||
634 | return event_expr; | |
635 | } | |
636 | ||
637 | static | |
638 | void destroy_event_expr(void *ptr) | |
639 | { | |
48a40005 | 640 | lttng_event_expr_destroy((lttng_event_expr *) ptr); |
20a01d15 SM |
641 | } |
642 | ||
f1494934 | 643 | namespace { |
20a01d15 SM |
644 | struct parse_event_rule_res { |
645 | /* Owned by this. */ | |
646 | struct lttng_event_rule *er; | |
647 | ||
648 | /* Array of `struct lttng_event_expr *` */ | |
649 | struct lttng_dynamic_pointer_array capture_descriptors; | |
650 | }; | |
f1494934 | 651 | } /* namespace */ |
20a01d15 SM |
652 | |
653 | static | |
35c4b2b3 SM |
654 | struct parse_event_rule_res parse_event_rule(int *argc, const char ***argv, |
655 | int argc_offset) | |
4624dad0 | 656 | { |
4624dad0 SM |
657 | enum lttng_event_rule_type event_rule_type = |
658 | LTTNG_EVENT_RULE_TYPE_UNKNOWN; | |
d50d200a SM |
659 | struct argpar_iter *argpar_iter = NULL; |
660 | const struct argpar_item *argpar_item = NULL; | |
4624dad0 SM |
661 | int consumed_args = -1; |
662 | struct lttng_kernel_probe_location *kernel_probe_location = NULL; | |
663 | struct lttng_userspace_probe_location *userspace_probe_location = NULL; | |
1c9a0b0e | 664 | struct parse_event_rule_res res = {}; |
20a01d15 SM |
665 | struct lttng_event_expr *event_expr = NULL; |
666 | struct filter_parser_ctx *parser_ctx = NULL; | |
85b05318 | 667 | struct lttng_log_level_rule *log_level_rule = NULL; |
4624dad0 | 668 | |
57739a6b JR |
669 | /* Event rule type option */ |
670 | char *event_rule_type_str = NULL; | |
671 | ||
50ad0862 SM |
672 | /* Tracepoint and syscall options. */ |
673 | char *name = NULL; | |
b03a81fb SM |
674 | /* Array of strings. */ |
675 | struct lttng_dynamic_pointer_array exclude_names; | |
4624dad0 | 676 | |
24de704e SM |
677 | /* For userspace / kernel probe and function. */ |
678 | char *location = NULL; | |
679 | char *event_name = NULL; | |
4624dad0 SM |
680 | |
681 | /* Filter. */ | |
682 | char *filter = NULL; | |
683 | ||
4624dad0 | 684 | /* Log level. */ |
949f049b | 685 | char *log_level_str = NULL; |
4624dad0 | 686 | |
20a01d15 SM |
687 | lttng_dynamic_pointer_array_init(&res.capture_descriptors, |
688 | destroy_event_expr); | |
b03a81fb SM |
689 | |
690 | lttng_dynamic_pointer_array_init(&exclude_names, free); | |
691 | ||
d50d200a SM |
692 | argpar_iter = argpar_iter_create(*argc, *argv, event_rule_opt_descrs); |
693 | if (!argpar_iter) { | |
694 | ERR("Failed to allocate an argpar iter."); | |
4624dad0 SM |
695 | goto error; |
696 | } | |
697 | ||
698 | while (true) { | |
d50d200a | 699 | enum parse_next_item_status status; |
4624dad0 | 700 | |
35c4b2b3 | 701 | status = parse_next_item(argpar_iter, &argpar_item, |
ef9ff9cb SM |
702 | argc_offset, *argv, false, NULL, NULL); |
703 | if (status == PARSE_NEXT_ITEM_STATUS_ERROR || | |
704 | status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) { | |
4624dad0 | 705 | goto error; |
d50d200a | 706 | } else if (status == PARSE_NEXT_ITEM_STATUS_END) { |
4624dad0 SM |
707 | break; |
708 | } | |
709 | ||
d50d200a | 710 | LTTNG_ASSERT(status == PARSE_NEXT_ITEM_STATUS_OK); |
4624dad0 | 711 | |
d50d200a SM |
712 | if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_OPT) { |
713 | const struct argpar_opt_descr *descr = | |
714 | argpar_item_opt_descr(argpar_item); | |
715 | const char *arg = argpar_item_opt_arg(argpar_item); | |
4624dad0 | 716 | |
d50d200a | 717 | switch (descr->id) { |
24de704e | 718 | case OPT_TYPE: |
d50d200a | 719 | if (!assign_event_rule_type(&event_rule_type, arg)) { |
4624dad0 SM |
720 | goto error; |
721 | } | |
722 | ||
57739a6b | 723 | /* Save the string for later use. */ |
d50d200a SM |
724 | if (!assign_string(&event_rule_type_str, arg, |
725 | "--type/-t")) { | |
57739a6b JR |
726 | goto error; |
727 | } | |
728 | ||
4624dad0 | 729 | break; |
24de704e | 730 | case OPT_LOCATION: |
d50d200a | 731 | if (!assign_string(&location, arg, |
24de704e | 732 | "--location/-L")) { |
50ad0862 SM |
733 | goto error; |
734 | } | |
735 | ||
736 | break; | |
737 | case OPT_EVENT_NAME: | |
d50d200a SM |
738 | if (!assign_string(&event_name, arg, |
739 | "--event-name/-E")) { | |
50ad0862 | 740 | goto error; |
4624dad0 SM |
741 | } |
742 | ||
4624dad0 SM |
743 | break; |
744 | case OPT_FILTER: | |
d50d200a SM |
745 | if (!assign_string(&filter, arg, |
746 | "--filter/-f")) { | |
4624dad0 SM |
747 | goto error; |
748 | } | |
749 | ||
750 | break; | |
50ad0862 | 751 | case OPT_NAME: |
d50d200a SM |
752 | if (!assign_string(&name, arg, |
753 | "--name/-n")) { | |
50ad0862 SM |
754 | goto error; |
755 | } | |
756 | ||
757 | break; | |
b03a81fb SM |
758 | case OPT_EXCLUDE_NAME: |
759 | { | |
760 | int ret; | |
761 | ||
762 | ret = lttng_dynamic_pointer_array_add_pointer( | |
763 | &exclude_names, | |
d50d200a | 764 | strdup(arg)); |
b03a81fb SM |
765 | if (ret != 0) { |
766 | ERR("Failed to add pointer to dynamic pointer array."); | |
4624dad0 SM |
767 | goto error; |
768 | } | |
769 | ||
770 | break; | |
b03a81fb | 771 | } |
949f049b | 772 | case OPT_LOG_LEVEL: |
d50d200a SM |
773 | if (!assign_string(&log_level_str, arg, |
774 | "--log-level/-l")) { | |
4624dad0 SM |
775 | goto error; |
776 | } | |
777 | ||
4624dad0 | 778 | break; |
20a01d15 SM |
779 | case OPT_CAPTURE: |
780 | { | |
781 | int ret; | |
20a01d15 SM |
782 | |
783 | ret = filter_parser_ctx_create_from_filter_expression( | |
d50d200a | 784 | arg, &parser_ctx); |
20a01d15 | 785 | if (ret) { |
d50d200a | 786 | ERR("Failed to parse capture expression `%s`.", arg); |
20a01d15 SM |
787 | goto error; |
788 | } | |
789 | ||
790 | event_expr = ir_op_root_to_event_expr( | |
d50d200a | 791 | parser_ctx->ir_root, arg); |
ff0a1009 JG |
792 | filter_parser_ctx_free(parser_ctx); |
793 | parser_ctx = nullptr; | |
20a01d15 SM |
794 | if (!event_expr) { |
795 | /* | |
796 | * ir_op_root_to_event_expr has printed | |
797 | * an error message. | |
798 | */ | |
799 | goto error; | |
800 | } | |
801 | ||
802 | ret = lttng_dynamic_pointer_array_add_pointer( | |
803 | &res.capture_descriptors, | |
804 | event_expr); | |
805 | if (ret) { | |
806 | goto error; | |
807 | } | |
808 | ||
809 | /* | |
810 | * The ownership of event expression was | |
811 | * transferred to the dynamic array. | |
812 | */ | |
813 | event_expr = NULL; | |
814 | ||
815 | break; | |
816 | } | |
4624dad0 SM |
817 | default: |
818 | abort(); | |
819 | } | |
820 | } else { | |
d50d200a | 821 | const char *arg = argpar_item_non_opt_arg(argpar_item); |
4624dad0 | 822 | |
50ad0862 | 823 | /* Don't accept non-option arguments. */ |
d50d200a | 824 | ERR("Unexpected argument '%s'", arg); |
50ad0862 | 825 | goto error; |
4624dad0 SM |
826 | } |
827 | } | |
828 | ||
829 | if (event_rule_type == LTTNG_EVENT_RULE_TYPE_UNKNOWN) { | |
0b621ec1 JG |
830 | ERR("Event rule requires a --type."); |
831 | goto error; | |
4624dad0 SM |
832 | } |
833 | ||
834 | /* | |
695f7044 JR |
835 | * Option --name is applicable to event rules of type kernel, user, jul, |
836 | * log4j,python and syscall. If --name is omitted, it is implicitly | |
837 | * "*". | |
4624dad0 | 838 | */ |
50ad0862 | 839 | switch (event_rule_type) { |
695f7044 JR |
840 | case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT: |
841 | case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT: | |
842 | case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING: | |
843 | case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING: | |
844 | case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING: | |
4f7da553 | 845 | case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL: |
50ad0862 SM |
846 | if (!name) { |
847 | name = strdup("*"); | |
4624dad0 | 848 | } |
50ad0862 | 849 | break; |
4624dad0 | 850 | |
50ad0862 SM |
851 | default: |
852 | if (name) { | |
853 | ERR("Can't use --name with %s event rules.", | |
854 | lttng_event_rule_type_str( | |
855 | event_rule_type)); | |
4624dad0 SM |
856 | goto error; |
857 | } | |
858 | ||
b03a81fb SM |
859 | if (lttng_dynamic_pointer_array_get_count(&exclude_names) > 0) { |
860 | ERR("Can't use --exclude-name/-x with %s event rules.", | |
50ad0862 SM |
861 | lttng_event_rule_type_str( |
862 | event_rule_type)); | |
863 | goto error; | |
864 | } | |
4624dad0 SM |
865 | } |
866 | ||
867 | /* | |
24de704e SM |
868 | * Option --location is only applicable to (and mandatory for) event |
869 | * rules of type {k,u}probe and function. | |
870 | * | |
50ad0862 | 871 | * Option --event-name is only applicable to event rules of type probe. |
24de704e | 872 | * If omitted, it defaults to the location. |
4624dad0 | 873 | */ |
50ad0862 | 874 | switch (event_rule_type) { |
85522de5 | 875 | case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE: |
46fd07ac | 876 | case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE: |
24de704e SM |
877 | if (!location) { |
878 | ERR("Event rule of type %s requires a --location.", | |
879 | lttng_event_rule_type_str(event_rule_type)); | |
880 | goto error; | |
881 | } | |
882 | ||
883 | if (!event_name) { | |
884 | event_name = strdup(location); | |
50ad0862 | 885 | } |
4624dad0 | 886 | |
50ad0862 SM |
887 | break; |
888 | ||
889 | default: | |
24de704e SM |
890 | if (location) { |
891 | ERR("Can't use --location with %s event rules.", | |
892 | lttng_event_rule_type_str(event_rule_type)); | |
893 | goto error; | |
894 | } | |
895 | ||
896 | if (event_name) { | |
50ad0862 SM |
897 | ERR("Can't use --event-name with %s event rules.", |
898 | lttng_event_rule_type_str( | |
899 | event_rule_type)); | |
900 | goto error; | |
901 | } | |
4624dad0 SM |
902 | } |
903 | ||
904 | /* | |
905 | * Update *argc and *argv so our caller can keep parsing what follows. | |
906 | */ | |
d50d200a | 907 | consumed_args = argpar_iter_ingested_orig_args(argpar_iter); |
a0377dfe | 908 | LTTNG_ASSERT(consumed_args >= 0); |
4624dad0 SM |
909 | *argc -= consumed_args; |
910 | *argv += consumed_args; | |
911 | ||
4624dad0 SM |
912 | /* |
913 | * Adding a filter to a probe, function or userspace-probe would be | |
914 | * denied by the kernel tracer as it's not supported at the moment. We | |
915 | * do an early check here to warn the user. | |
916 | */ | |
695f7044 | 917 | if (filter) { |
4624dad0 | 918 | switch (event_rule_type) { |
695f7044 JR |
919 | case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT: |
920 | case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT: | |
921 | case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING: | |
922 | case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING: | |
923 | case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING: | |
4f7da553 | 924 | case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL: |
4624dad0 SM |
925 | break; |
926 | default: | |
927 | ERR("Filter expressions are not supported for %s event rules.", | |
928 | lttng_event_rule_type_str(event_rule_type)); | |
929 | goto error; | |
930 | } | |
931 | } | |
932 | ||
695f7044 JR |
933 | /* |
934 | * If --exclude-name/-x was passed, split it into an exclusion list. | |
935 | * Exclusions are only supported by | |
936 | * LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT for now. | |
937 | */ | |
b03a81fb | 938 | if (lttng_dynamic_pointer_array_get_count(&exclude_names) > 0) { |
695f7044 | 939 | if (event_rule_type != LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT) { |
4624dad0 | 940 | ERR("Event name exclusions are not yet implemented for %s event rules.", |
695f7044 | 941 | lttng_event_rule_type_str(event_rule_type)); |
4624dad0 SM |
942 | goto error; |
943 | } | |
944 | ||
e358ddd5 | 945 | if (validate_exclusion_list(name, &exclude_names) != 0) { |
b03a81fb SM |
946 | /* |
947 | * Assume validate_exclusion_list already prints an | |
948 | * error message. | |
949 | */ | |
4624dad0 SM |
950 | goto error; |
951 | } | |
952 | } | |
953 | ||
949f049b | 954 | if (log_level_str) { |
695f7044 JR |
955 | switch (event_rule_type) { |
956 | case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT: | |
957 | case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING: | |
958 | case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING: | |
959 | case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING: | |
960 | { | |
961 | int log_level; | |
962 | bool log_level_only; | |
949f049b | 963 | |
695f7044 JR |
964 | if (strcmp(log_level_str, "..") == 0) { |
965 | /* | |
966 | * ".." is the same as passing no log level | |
967 | * option and correspond to the "ANY" case. | |
968 | */ | |
969 | break; | |
970 | } | |
971 | ||
972 | if (!parse_log_level_string(log_level_str, event_rule_type, | |
973 | &log_level, &log_level_only)) { | |
974 | ERR("Failed to parse log level string `%s`.", | |
975 | log_level_str); | |
976 | goto error; | |
977 | } | |
978 | ||
979 | if (log_level_only) { | |
980 | log_level_rule = lttng_log_level_rule_exactly_create(log_level); | |
981 | } else { | |
982 | log_level_rule = lttng_log_level_rule_at_least_as_severe_as_create(log_level); | |
983 | } | |
984 | ||
985 | if (log_level_rule == NULL) { | |
986 | ERR("Failed to create log level rule object."); | |
987 | goto error; | |
988 | } | |
989 | break; | |
990 | } | |
991 | default: | |
992 | ERR("Log levels are not supported for %s event rules.", | |
993 | lttng_event_rule_type_str(event_rule_type)); | |
949f049b SM |
994 | goto error; |
995 | } | |
4624dad0 SM |
996 | } |
997 | ||
998 | /* Finally, create the event rule object. */ | |
999 | switch (event_rule_type) { | |
695f7044 | 1000 | case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT: |
4624dad0 SM |
1001 | { |
1002 | enum lttng_event_rule_status event_rule_status; | |
1003 | ||
695f7044 | 1004 | res.er = lttng_event_rule_user_tracepoint_create(); |
20a01d15 | 1005 | if (!res.er) { |
695f7044 | 1006 | ERR("Failed to create user_tracepoint event rule."); |
4624dad0 SM |
1007 | goto error; |
1008 | } | |
1009 | ||
1010 | /* Set pattern. */ | |
695f7044 | 1011 | event_rule_status = lttng_event_rule_user_tracepoint_set_name_pattern( |
50ad0862 | 1012 | res.er, name); |
4624dad0 | 1013 | if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) { |
695f7044 | 1014 | ERR("Failed to set user_tracepoint event rule's pattern to '%s'.", |
50ad0862 | 1015 | name); |
4624dad0 SM |
1016 | goto error; |
1017 | } | |
1018 | ||
1019 | /* Set filter. */ | |
1020 | if (filter) { | |
695f7044 | 1021 | event_rule_status = lttng_event_rule_user_tracepoint_set_filter( |
20a01d15 | 1022 | res.er, filter); |
4624dad0 | 1023 | if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) { |
695f7044 | 1024 | ERR("Failed to set user_tracepoint event rule's filter to '%s'.", |
4624dad0 SM |
1025 | filter); |
1026 | goto error; | |
1027 | } | |
1028 | } | |
1029 | ||
1030 | /* Set exclusion list. */ | |
b03a81fb | 1031 | if (lttng_dynamic_pointer_array_get_count(&exclude_names) > 0) { |
4624dad0 | 1032 | int n; |
b03a81fb SM |
1033 | int count = lttng_dynamic_pointer_array_get_count( |
1034 | &exclude_names); | |
1035 | ||
1036 | for (n = 0; n < count; n++) { | |
1037 | const char *exclude_name = | |
48a40005 | 1038 | (const char *) lttng_dynamic_pointer_array_get_pointer( |
b03a81fb SM |
1039 | &exclude_names, |
1040 | n); | |
1041 | ||
1042 | event_rule_status = | |
695f7044 | 1043 | lttng_event_rule_user_tracepoint_add_name_pattern_exclusion( |
b03a81fb SM |
1044 | res.er, |
1045 | exclude_name); | |
4624dad0 SM |
1046 | if (event_rule_status != |
1047 | LTTNG_EVENT_RULE_STATUS_OK) { | |
695f7044 | 1048 | ERR("Failed to set user_tracepoint exclusion list element '%s'", |
b03a81fb | 1049 | exclude_name); |
4624dad0 SM |
1050 | goto error; |
1051 | } | |
1052 | } | |
1053 | } | |
1054 | ||
695f7044 JR |
1055 | if (log_level_rule) { |
1056 | event_rule_status = | |
1057 | lttng_event_rule_user_tracepoint_set_log_level_rule( | |
1058 | res.er, log_level_rule); | |
949f049b | 1059 | |
695f7044 JR |
1060 | if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) { |
1061 | ERR("Failed to set log level on event fule."); | |
4624dad0 SM |
1062 | goto error; |
1063 | } | |
695f7044 | 1064 | } |
4624dad0 | 1065 | |
695f7044 JR |
1066 | break; |
1067 | } | |
1068 | case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT: | |
1069 | { | |
1070 | enum lttng_event_rule_status event_rule_status; | |
1071 | ||
1072 | res.er = lttng_event_rule_kernel_tracepoint_create(); | |
1073 | if (!res.er) { | |
1074 | ERR("Failed to create kernel_tracepoint event rule."); | |
1075 | goto error; | |
1076 | } | |
1077 | ||
1078 | /* Set pattern. */ | |
1079 | event_rule_status = lttng_event_rule_kernel_tracepoint_set_name_pattern( | |
1080 | res.er, name); | |
1081 | if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) { | |
1082 | ERR("Failed to set kernel_tracepoint event rule's pattern to '%s'.", | |
1083 | name); | |
1084 | goto error; | |
1085 | } | |
1086 | ||
1087 | /* Set filter. */ | |
1088 | if (filter) { | |
1089 | event_rule_status = lttng_event_rule_kernel_tracepoint_set_filter( | |
1090 | res.er, filter); | |
1091 | if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) { | |
1092 | ERR("Failed to set kernel_tracepoint event rule's filter to '%s'.", | |
1093 | filter); | |
1094 | goto error; | |
85b05318 | 1095 | } |
695f7044 JR |
1096 | } |
1097 | break; | |
1098 | } | |
1099 | case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING: | |
1100 | { | |
1101 | enum lttng_event_rule_status event_rule_status; | |
85b05318 | 1102 | |
695f7044 JR |
1103 | res.er = lttng_event_rule_jul_logging_create(); |
1104 | if (!res.er) { | |
1105 | ERR("Failed to create jul_logging event rule."); | |
1106 | goto error; | |
1107 | } | |
1108 | ||
1109 | /* Set pattern. */ | |
1110 | event_rule_status = lttng_event_rule_jul_logging_set_name_pattern( | |
1111 | res.er, name); | |
1112 | if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) { | |
1113 | ERR("Failed to set jul_logging event rule's pattern to '%s'.", | |
1114 | name); | |
1115 | goto error; | |
1116 | } | |
1117 | ||
1118 | /* Set filter. */ | |
1119 | if (filter) { | |
1120 | event_rule_status = lttng_event_rule_jul_logging_set_filter( | |
1121 | res.er, filter); | |
1122 | if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) { | |
1123 | ERR("Failed to set jul_logging event rule's filter to '%s'.", | |
1124 | filter); | |
85b05318 | 1125 | goto error; |
4624dad0 | 1126 | } |
695f7044 | 1127 | } |
4624dad0 | 1128 | |
695f7044 | 1129 | if (log_level_rule) { |
85b05318 | 1130 | event_rule_status = |
695f7044 | 1131 | lttng_event_rule_jul_logging_set_log_level_rule( |
85b05318 JR |
1132 | res.er, log_level_rule); |
1133 | ||
4624dad0 SM |
1134 | if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) { |
1135 | ERR("Failed to set log level on event fule."); | |
1136 | goto error; | |
1137 | } | |
1138 | } | |
695f7044 JR |
1139 | break; |
1140 | } | |
1141 | case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING: | |
1142 | { | |
1143 | enum lttng_event_rule_status event_rule_status; | |
4624dad0 | 1144 | |
695f7044 JR |
1145 | res.er = lttng_event_rule_log4j_logging_create(); |
1146 | if (!res.er) { | |
1147 | ERR("Failed to create jul_logging event rule."); | |
1148 | goto error; | |
1149 | } | |
1150 | ||
1151 | /* Set pattern. */ | |
1152 | event_rule_status = lttng_event_rule_log4j_logging_set_name_pattern( | |
1153 | res.er, name); | |
1154 | if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) { | |
1155 | ERR("Failed to set jul_logging event rule's pattern to '%s'.", | |
1156 | name); | |
1157 | goto error; | |
1158 | } | |
1159 | ||
1160 | /* Set filter. */ | |
1161 | if (filter) { | |
1162 | event_rule_status = lttng_event_rule_log4j_logging_set_filter( | |
1163 | res.er, filter); | |
1164 | if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) { | |
1165 | ERR("Failed to set jul_logging event rule's filter to '%s'.", | |
1166 | filter); | |
1167 | goto error; | |
1168 | } | |
1169 | } | |
1170 | ||
1171 | if (log_level_rule) { | |
1172 | event_rule_status = | |
1173 | lttng_event_rule_log4j_logging_set_log_level_rule( | |
1174 | res.er, log_level_rule); | |
1175 | ||
1176 | if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) { | |
1177 | ERR("Failed to set log level on event fule."); | |
1178 | goto error; | |
1179 | } | |
1180 | } | |
1181 | break; | |
1182 | } | |
1183 | case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING: | |
1184 | { | |
1185 | enum lttng_event_rule_status event_rule_status; | |
1186 | ||
1187 | res.er = lttng_event_rule_python_logging_create(); | |
1188 | if (!res.er) { | |
1189 | ERR("Failed to create jul_logging event rule."); | |
1190 | goto error; | |
1191 | } | |
1192 | ||
1193 | /* Set pattern. */ | |
1194 | event_rule_status = lttng_event_rule_python_logging_set_name_pattern( | |
1195 | res.er, name); | |
1196 | if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) { | |
1197 | ERR("Failed to set jul_logging event rule's pattern to '%s'.", | |
1198 | name); | |
1199 | goto error; | |
1200 | } | |
1201 | ||
1202 | /* Set filter. */ | |
1203 | if (filter) { | |
1204 | event_rule_status = lttng_event_rule_python_logging_set_filter( | |
1205 | res.er, filter); | |
1206 | if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) { | |
1207 | ERR("Failed to set jul_logging event rule's filter to '%s'.", | |
1208 | filter); | |
1209 | goto error; | |
1210 | } | |
1211 | } | |
1212 | ||
1213 | if (log_level_rule) { | |
1214 | event_rule_status = | |
1215 | lttng_event_rule_python_logging_set_log_level_rule( | |
1216 | res.er, log_level_rule); | |
1217 | ||
1218 | if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) { | |
1219 | ERR("Failed to set log level on event fule."); | |
1220 | goto error; | |
1221 | } | |
1222 | } | |
4624dad0 SM |
1223 | break; |
1224 | } | |
85522de5 | 1225 | case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE: |
4624dad0 SM |
1226 | { |
1227 | int ret; | |
1228 | enum lttng_event_rule_status event_rule_status; | |
1229 | ||
50ad0862 | 1230 | ret = parse_kernel_probe_opts( |
24de704e | 1231 | location, &kernel_probe_location); |
4624dad0 SM |
1232 | if (ret) { |
1233 | ERR("Failed to parse kernel probe location."); | |
1234 | goto error; | |
1235 | } | |
1236 | ||
a0377dfe | 1237 | LTTNG_ASSERT(kernel_probe_location); |
85522de5 | 1238 | res.er = lttng_event_rule_kernel_kprobe_create(kernel_probe_location); |
602a6d40 JR |
1239 | if (!res.er) { |
1240 | ERR("Failed to create kprobe event rule."); | |
4624dad0 SM |
1241 | goto error; |
1242 | } | |
1243 | ||
50ad0862 | 1244 | event_rule_status = |
85522de5 | 1245 | lttng_event_rule_kernel_kprobe_set_event_name( |
24de704e | 1246 | res.er, event_name); |
4624dad0 | 1247 | if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) { |
50ad0862 | 1248 | ERR("Failed to set kprobe event rule's name to '%s'.", |
24de704e | 1249 | event_name); |
4624dad0 SM |
1250 | goto error; |
1251 | } | |
1252 | ||
1253 | break; | |
1254 | } | |
46fd07ac | 1255 | case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE: |
4624dad0 SM |
1256 | { |
1257 | int ret; | |
1258 | enum lttng_event_rule_status event_rule_status; | |
1259 | ||
1260 | ret = parse_userspace_probe_opts( | |
24de704e | 1261 | location, &userspace_probe_location); |
4624dad0 SM |
1262 | if (ret) { |
1263 | ERR("Failed to parse user space probe location."); | |
1264 | goto error; | |
1265 | } | |
1266 | ||
46fd07ac | 1267 | res.er = lttng_event_rule_kernel_uprobe_create(userspace_probe_location); |
20a01d15 SM |
1268 | if (!res.er) { |
1269 | ERR("Failed to create userspace probe event rule."); | |
4624dad0 SM |
1270 | goto error; |
1271 | } | |
1272 | ||
50ad0862 | 1273 | event_rule_status = |
46fd07ac | 1274 | lttng_event_rule_kernel_uprobe_set_event_name( |
24de704e | 1275 | res.er, event_name); |
4624dad0 SM |
1276 | if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) { |
1277 | ERR("Failed to set user space probe event rule's name to '%s'.", | |
24de704e | 1278 | event_name); |
4624dad0 SM |
1279 | goto error; |
1280 | } | |
1281 | ||
1282 | break; | |
1283 | } | |
4f7da553 | 1284 | case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL: |
4624dad0 SM |
1285 | { |
1286 | enum lttng_event_rule_status event_rule_status; | |
4f7da553 | 1287 | enum lttng_event_rule_kernel_syscall_emission_site emission_site; |
57739a6b JR |
1288 | |
1289 | if (!parse_syscall_emission_site_from_type( | |
f6a5af19 | 1290 | event_rule_type_str, &emission_site)) { |
57739a6b JR |
1291 | ERR("Failed to parse syscall type '%s'.", event_rule_type_str); |
1292 | goto error; | |
1293 | } | |
4624dad0 | 1294 | |
4f7da553 | 1295 | res.er = lttng_event_rule_kernel_syscall_create(emission_site); |
20a01d15 | 1296 | if (!res.er) { |
4624dad0 SM |
1297 | ERR("Failed to create syscall event rule."); |
1298 | goto error; | |
1299 | } | |
1300 | ||
4f7da553 | 1301 | event_rule_status = lttng_event_rule_kernel_syscall_set_name_pattern( |
50ad0862 | 1302 | res.er, name); |
4624dad0 SM |
1303 | if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) { |
1304 | ERR("Failed to set syscall event rule's pattern to '%s'.", | |
50ad0862 | 1305 | name); |
4624dad0 SM |
1306 | goto error; |
1307 | } | |
1308 | ||
1309 | if (filter) { | |
4f7da553 | 1310 | event_rule_status = lttng_event_rule_kernel_syscall_set_filter( |
20a01d15 | 1311 | res.er, filter); |
4624dad0 SM |
1312 | if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) { |
1313 | ERR("Failed to set syscall event rule's filter to '%s'.", | |
1314 | filter); | |
1315 | goto error; | |
1316 | } | |
1317 | } | |
1318 | ||
1319 | break; | |
1320 | } | |
1321 | default: | |
1322 | abort(); | |
1323 | goto error; | |
1324 | } | |
1325 | ||
1326 | goto end; | |
1327 | ||
1328 | error: | |
20a01d15 SM |
1329 | lttng_event_rule_destroy(res.er); |
1330 | res.er = NULL; | |
1331 | lttng_dynamic_pointer_array_reset(&res.capture_descriptors); | |
4624dad0 SM |
1332 | |
1333 | end: | |
20a01d15 SM |
1334 | if (parser_ctx) { |
1335 | filter_parser_ctx_free(parser_ctx); | |
1336 | } | |
1337 | ||
1338 | lttng_event_expr_destroy(event_expr); | |
d50d200a SM |
1339 | argpar_item_destroy(argpar_item); |
1340 | argpar_iter_destroy(argpar_iter); | |
4624dad0 | 1341 | free(filter); |
50ad0862 | 1342 | free(name); |
b03a81fb | 1343 | lttng_dynamic_pointer_array_reset(&exclude_names); |
949f049b | 1344 | free(log_level_str); |
24de704e SM |
1345 | free(location); |
1346 | free(event_name); | |
57739a6b | 1347 | free(event_rule_type_str); |
50ad0862 | 1348 | |
4624dad0 SM |
1349 | lttng_kernel_probe_location_destroy(kernel_probe_location); |
1350 | lttng_userspace_probe_location_destroy(userspace_probe_location); | |
85b05318 | 1351 | lttng_log_level_rule_destroy(log_level_rule); |
20a01d15 | 1352 | return res; |
4624dad0 SM |
1353 | } |
1354 | ||
1355 | static | |
35c4b2b3 SM |
1356 | struct lttng_condition *handle_condition_event(int *argc, const char ***argv, |
1357 | int argc_offset) | |
4624dad0 | 1358 | { |
20a01d15 | 1359 | struct parse_event_rule_res res; |
4624dad0 | 1360 | struct lttng_condition *c; |
20a01d15 | 1361 | size_t i; |
4624dad0 | 1362 | |
35c4b2b3 | 1363 | res = parse_event_rule(argc, argv, argc_offset); |
20a01d15 | 1364 | if (!res.er) { |
4624dad0 | 1365 | c = NULL; |
20a01d15 | 1366 | goto error; |
4624dad0 SM |
1367 | } |
1368 | ||
8dbb86b8 | 1369 | c = lttng_condition_event_rule_matches_create(res.er); |
20a01d15 SM |
1370 | lttng_event_rule_destroy(res.er); |
1371 | res.er = NULL; | |
4624dad0 | 1372 | if (!c) { |
20a01d15 SM |
1373 | goto error; |
1374 | } | |
1375 | ||
1376 | for (i = 0; i < lttng_dynamic_pointer_array_get_count(&res.capture_descriptors); | |
1377 | i++) { | |
1378 | enum lttng_condition_status status; | |
1379 | struct lttng_event_expr **expr = | |
48a40005 | 1380 | (lttng_event_expr **) lttng_dynamic_array_get_element( |
20a01d15 SM |
1381 | &res.capture_descriptors.array, i); |
1382 | ||
a0377dfe FD |
1383 | LTTNG_ASSERT(expr); |
1384 | LTTNG_ASSERT(*expr); | |
8dbb86b8 | 1385 | status = lttng_condition_event_rule_matches_append_capture_descriptor( |
20a01d15 SM |
1386 | c, *expr); |
1387 | if (status != LTTNG_CONDITION_STATUS_OK) { | |
81d566c9 JR |
1388 | if (status == LTTNG_CONDITION_STATUS_UNSUPPORTED) { |
1389 | ERR("The capture feature is unsupported by the event-rule condition type"); | |
1390 | } | |
1391 | ||
20a01d15 SM |
1392 | goto error; |
1393 | } | |
1394 | ||
1395 | /* Ownership of event expression moved to `c` */ | |
1396 | *expr = NULL; | |
4624dad0 SM |
1397 | } |
1398 | ||
20a01d15 SM |
1399 | goto end; |
1400 | ||
1401 | error: | |
1402 | lttng_condition_destroy(c); | |
1403 | c = NULL; | |
1404 | ||
4624dad0 | 1405 | end: |
20a01d15 SM |
1406 | lttng_dynamic_pointer_array_reset(&res.capture_descriptors); |
1407 | lttng_event_rule_destroy(res.er); | |
4624dad0 SM |
1408 | return c; |
1409 | } | |
1410 | ||
f1494934 | 1411 | namespace { |
4624dad0 SM |
1412 | struct condition_descr { |
1413 | const char *name; | |
35c4b2b3 SM |
1414 | struct lttng_condition *(*handler) (int *argc, const char ***argv, |
1415 | int argc_offset); | |
4624dad0 | 1416 | }; |
f1494934 | 1417 | } /* namespace */ |
4624dad0 SM |
1418 | |
1419 | static const | |
1420 | struct condition_descr condition_descrs[] = { | |
665db063 | 1421 | { "event-rule-matches", handle_condition_event }, |
4624dad0 SM |
1422 | }; |
1423 | ||
ef9ff9cb SM |
1424 | static |
1425 | void print_valid_condition_names(void) | |
1426 | { | |
1427 | unsigned int i; | |
1428 | ||
1429 | ERR("Valid condition names are:"); | |
1430 | ||
1431 | for (i = 0; i < ARRAY_SIZE(condition_descrs); ++i) { | |
1432 | ERR(" %s", condition_descrs[i].name); | |
1433 | } | |
1434 | } | |
1435 | ||
4624dad0 | 1436 | static |
29bcf415 | 1437 | struct lttng_condition *parse_condition(const char *condition_name, int *argc, |
e776cf47 SM |
1438 | const char ***argv, int argc_offset, int orig_arg_index, |
1439 | const char *orig_arg) | |
4624dad0 SM |
1440 | { |
1441 | int i; | |
1442 | struct lttng_condition *cond; | |
4624dad0 SM |
1443 | const struct condition_descr *descr = NULL; |
1444 | ||
4624dad0 SM |
1445 | for (i = 0; i < ARRAY_SIZE(condition_descrs); i++) { |
1446 | if (strcmp(condition_name, condition_descrs[i].name) == 0) { | |
1447 | descr = &condition_descrs[i]; | |
1448 | break; | |
1449 | } | |
1450 | } | |
1451 | ||
1452 | if (!descr) { | |
e776cf47 SM |
1453 | ERR(WHILE_PARSING_ARG_N_ARG_FMT "Unknown condition name '%s'", |
1454 | orig_arg_index + 1, orig_arg, condition_name); | |
ef9ff9cb | 1455 | print_valid_condition_names(); |
4624dad0 SM |
1456 | goto error; |
1457 | } | |
1458 | ||
35c4b2b3 | 1459 | cond = descr->handler(argc, argv, argc_offset); |
4624dad0 SM |
1460 | if (!cond) { |
1461 | /* The handler has already printed an error message. */ | |
1462 | goto error; | |
1463 | } | |
1464 | ||
1465 | goto end; | |
1466 | error: | |
1467 | cond = NULL; | |
1468 | end: | |
1469 | return cond; | |
1470 | } | |
bbadb5e0 JR |
1471 | |
1472 | static struct lttng_rate_policy *parse_rate_policy(const char *policy_str) | |
1473 | { | |
e358ddd5 JG |
1474 | int ret; |
1475 | size_t num_token = 0; | |
1476 | struct lttng_dynamic_pointer_array tokens; | |
bbadb5e0 JR |
1477 | struct lttng_rate_policy *policy = NULL; |
1478 | enum lttng_rate_policy_type policy_type; | |
1479 | unsigned long long value; | |
1480 | char *policy_type_str; | |
1481 | char *policy_value_str; | |
1482 | ||
a0377dfe | 1483 | LTTNG_ASSERT(policy_str); |
e358ddd5 | 1484 | lttng_dynamic_pointer_array_init(&tokens, NULL); |
bbadb5e0 | 1485 | |
e358ddd5 JG |
1486 | /* Rate policy fields are separated by ':'. */ |
1487 | ret = strutils_split(policy_str, ':', 1, &tokens); | |
1488 | if (ret == 0) { | |
1489 | num_token = lttng_dynamic_pointer_array_get_count(&tokens); | |
1490 | } | |
bbadb5e0 JR |
1491 | |
1492 | /* | |
1493 | * Early sanity check that the number of parameter is exactly 2. | |
1494 | * i.e : type:value | |
1495 | */ | |
1496 | if (num_token != 2) { | |
1497 | ERR("Rate policy format is invalid."); | |
1498 | goto end; | |
1499 | } | |
1500 | ||
48a40005 SM |
1501 | policy_type_str = (char *) lttng_dynamic_pointer_array_get_pointer(&tokens, 0); |
1502 | policy_value_str = (char *) lttng_dynamic_pointer_array_get_pointer(&tokens, 1); | |
bbadb5e0 JR |
1503 | |
1504 | /* Parse the type. */ | |
1505 | if (strcmp(policy_type_str, "once-after") == 0) { | |
1506 | policy_type = LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N; | |
1507 | } else if (strcmp(policy_type_str, "every") == 0) { | |
1508 | policy_type = LTTNG_RATE_POLICY_TYPE_EVERY_N; | |
1509 | } else { | |
1510 | ERR("Rate policy type `%s` unknown.", policy_type_str); | |
1511 | goto end; | |
1512 | } | |
1513 | ||
1514 | /* Parse the value. */ | |
1515 | if (utils_parse_unsigned_long_long(policy_value_str, &value) != 0) { | |
1516 | ERR("Failed to parse rate policy value `%s` as an integer.", | |
1517 | policy_value_str); | |
1518 | goto end; | |
1519 | } | |
1520 | ||
1521 | if (value == 0) { | |
1522 | ERR("Rate policy value `%s` must be > 0.", policy_value_str); | |
1523 | goto end; | |
1524 | } | |
1525 | ||
1526 | switch (policy_type) { | |
1527 | case LTTNG_RATE_POLICY_TYPE_EVERY_N: | |
1528 | policy = lttng_rate_policy_every_n_create(value); | |
1529 | break; | |
1530 | case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N: | |
1531 | policy = lttng_rate_policy_once_after_n_create(value); | |
1532 | break; | |
1533 | default: | |
1534 | abort(); | |
1535 | } | |
1536 | ||
1537 | if (policy == NULL) { | |
1538 | ERR("Failed to create rate policy `%s`.", policy_str); | |
1539 | } | |
1540 | ||
1541 | end: | |
e358ddd5 | 1542 | lttng_dynamic_pointer_array_reset(&tokens); |
bbadb5e0 JR |
1543 | return policy; |
1544 | } | |
1545 | ||
e45dd625 | 1546 | static const struct argpar_opt_descr notify_action_opt_descrs[] = { |
bbadb5e0 | 1547 | { OPT_RATE_POLICY, '\0', "rate-policy", true }, |
e45dd625 JR |
1548 | ARGPAR_OPT_DESCR_SENTINEL |
1549 | }; | |
4624dad0 | 1550 | |
4624dad0 | 1551 | static |
35c4b2b3 SM |
1552 | struct lttng_action *handle_action_notify(int *argc, const char ***argv, |
1553 | int argc_offset) | |
4624dad0 | 1554 | { |
e45dd625 | 1555 | struct lttng_action *action = NULL; |
d50d200a SM |
1556 | struct argpar_iter *argpar_iter = NULL; |
1557 | const struct argpar_item *argpar_item = NULL; | |
7f4d5b07 | 1558 | struct lttng_rate_policy *policy = NULL; |
4624dad0 | 1559 | |
d50d200a SM |
1560 | argpar_iter = argpar_iter_create(*argc, *argv, notify_action_opt_descrs); |
1561 | if (!argpar_iter) { | |
1562 | ERR("Failed to allocate an argpar iter."); | |
e45dd625 JR |
1563 | goto error; |
1564 | } | |
1565 | ||
1566 | while (true) { | |
d50d200a | 1567 | enum parse_next_item_status status; |
e45dd625 | 1568 | |
35c4b2b3 | 1569 | status = parse_next_item(argpar_iter, &argpar_item, |
ef9ff9cb | 1570 | argc_offset, *argv, false, NULL, |
35c4b2b3 | 1571 | "While parsing `notify` action:"); |
ef9ff9cb SM |
1572 | if (status == PARSE_NEXT_ITEM_STATUS_ERROR || |
1573 | status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) { | |
e45dd625 | 1574 | goto error; |
d50d200a | 1575 | } else if (status == PARSE_NEXT_ITEM_STATUS_END) { |
e45dd625 JR |
1576 | break; |
1577 | } | |
1578 | ||
d50d200a | 1579 | LTTNG_ASSERT(status == PARSE_NEXT_ITEM_STATUS_OK); |
e45dd625 | 1580 | |
d50d200a SM |
1581 | if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_OPT) { |
1582 | const struct argpar_opt_descr *descr = | |
1583 | argpar_item_opt_descr(argpar_item); | |
1584 | const char *arg = argpar_item_opt_arg(argpar_item); | |
e45dd625 | 1585 | |
d50d200a | 1586 | switch (descr->id) { |
bbadb5e0 | 1587 | case OPT_RATE_POLICY: |
e45dd625 | 1588 | { |
d50d200a | 1589 | policy = parse_rate_policy(arg); |
bbadb5e0 | 1590 | if (!policy) { |
e45dd625 JR |
1591 | goto error; |
1592 | } | |
e45dd625 JR |
1593 | break; |
1594 | } | |
e45dd625 JR |
1595 | default: |
1596 | abort(); | |
1597 | } | |
1598 | } else { | |
d50d200a | 1599 | const char *arg = argpar_item_non_opt_arg(argpar_item); |
e45dd625 | 1600 | |
d50d200a SM |
1601 | ERR("Unexpected argument `%s`.", arg); |
1602 | goto error; | |
e45dd625 JR |
1603 | } |
1604 | } | |
1605 | ||
d50d200a SM |
1606 | *argc -= argpar_iter_ingested_orig_args(argpar_iter); |
1607 | *argv += argpar_iter_ingested_orig_args(argpar_iter); | |
e45dd625 | 1608 | |
e45dd625 JR |
1609 | action = lttng_action_notify_create(); |
1610 | if (!action) { | |
1611 | ERR("Failed to create notify action"); | |
1612 | goto error; | |
1613 | } | |
1614 | ||
1615 | if (policy) { | |
1616 | enum lttng_action_status status; | |
7f4d5b07 | 1617 | status = lttng_action_notify_set_rate_policy(action, policy); |
e45dd625 | 1618 | if (status != LTTNG_ACTION_STATUS_OK) { |
7f4d5b07 | 1619 | ERR("Failed to set rate policy"); |
e45dd625 JR |
1620 | goto error; |
1621 | } | |
1622 | } | |
1623 | ||
1624 | goto end; | |
1625 | ||
1626 | error: | |
1627 | lttng_action_destroy(action); | |
1628 | action = NULL; | |
e45dd625 | 1629 | end: |
7f4d5b07 | 1630 | lttng_rate_policy_destroy(policy); |
d50d200a SM |
1631 | argpar_item_destroy(argpar_item); |
1632 | argpar_iter_destroy(argpar_iter); | |
e45dd625 JR |
1633 | return action; |
1634 | } | |
4624dad0 SM |
1635 | |
1636 | /* | |
e45dd625 | 1637 | * Generic handler for a kind of action that takes a session name and an |
7f4d5b07 | 1638 | * optional rate policy. |
4624dad0 SM |
1639 | */ |
1640 | ||
e45dd625 JR |
1641 | static struct lttng_action *handle_action_simple_session_with_policy(int *argc, |
1642 | const char ***argv, | |
35c4b2b3 | 1643 | int argc_offset, |
4624dad0 | 1644 | struct lttng_action *(*create_action_cb)(void), |
e45dd625 JR |
1645 | enum lttng_action_status (*set_session_name_cb)( |
1646 | struct lttng_action *, const char *), | |
7f4d5b07 | 1647 | enum lttng_action_status (*set_rate_policy_cb)( |
e45dd625 | 1648 | struct lttng_action *, |
7f4d5b07 | 1649 | const struct lttng_rate_policy *), |
4624dad0 SM |
1650 | const char *action_name) |
1651 | { | |
1652 | struct lttng_action *action = NULL; | |
d50d200a SM |
1653 | struct argpar_iter *argpar_iter = NULL; |
1654 | const struct argpar_item *argpar_item = NULL; | |
4624dad0 | 1655 | const char *session_name_arg = NULL; |
4624dad0 | 1656 | enum lttng_action_status action_status; |
7f4d5b07 | 1657 | struct lttng_rate_policy *policy = NULL; |
e45dd625 | 1658 | |
a0377dfe FD |
1659 | LTTNG_ASSERT(set_session_name_cb); |
1660 | LTTNG_ASSERT(set_rate_policy_cb); | |
e45dd625 | 1661 | |
7f4d5b07 | 1662 | const struct argpar_opt_descr rate_policy_opt_descrs[] = { |
bbadb5e0 | 1663 | { OPT_RATE_POLICY, '\0', "rate-policy", true }, |
e45dd625 JR |
1664 | ARGPAR_OPT_DESCR_SENTINEL |
1665 | }; | |
b03a81fb | 1666 | |
d50d200a SM |
1667 | argpar_iter = argpar_iter_create(*argc, *argv, rate_policy_opt_descrs); |
1668 | if (!argpar_iter) { | |
1669 | ERR("Failed to allocate an argpar iter."); | |
4624dad0 SM |
1670 | goto error; |
1671 | } | |
1672 | ||
1673 | while (true) { | |
d50d200a | 1674 | enum parse_next_item_status status; |
4624dad0 | 1675 | |
35c4b2b3 | 1676 | status = parse_next_item(argpar_iter, &argpar_item, argc_offset, |
ef9ff9cb | 1677 | *argv, false, NULL, |
35c4b2b3 | 1678 | "While parsing `%s` action:", action_name); |
ef9ff9cb SM |
1679 | if (status == PARSE_NEXT_ITEM_STATUS_ERROR || |
1680 | status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) { | |
4624dad0 | 1681 | goto error; |
d50d200a | 1682 | } else if (status == PARSE_NEXT_ITEM_STATUS_END) { |
4624dad0 SM |
1683 | break; |
1684 | } | |
1685 | ||
d50d200a SM |
1686 | LTTNG_ASSERT(status == PARSE_NEXT_ITEM_STATUS_OK); |
1687 | ||
1688 | if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_OPT) { | |
1689 | const struct argpar_opt_descr *descr = | |
1690 | argpar_item_opt_descr(argpar_item); | |
1691 | const char *arg = argpar_item_opt_arg(argpar_item); | |
4624dad0 | 1692 | |
d50d200a | 1693 | switch (descr->id) { |
bbadb5e0 | 1694 | case OPT_RATE_POLICY: |
e45dd625 | 1695 | { |
d50d200a | 1696 | policy = parse_rate_policy(arg); |
bbadb5e0 | 1697 | if (!policy) { |
e45dd625 JR |
1698 | goto error; |
1699 | } | |
e45dd625 JR |
1700 | break; |
1701 | } | |
e45dd625 JR |
1702 | default: |
1703 | abort(); | |
1704 | } | |
1705 | } else { | |
d50d200a SM |
1706 | const char *arg = argpar_item_non_opt_arg(argpar_item); |
1707 | unsigned int idx = argpar_item_non_opt_non_opt_index(argpar_item); | |
e45dd625 | 1708 | |
d50d200a | 1709 | switch (idx) { |
e45dd625 | 1710 | case 0: |
d50d200a | 1711 | session_name_arg = arg; |
e45dd625 JR |
1712 | break; |
1713 | default: | |
d50d200a | 1714 | ERR("Unexpected argument `%s`.", arg); |
e45dd625 JR |
1715 | goto error; |
1716 | } | |
4624dad0 SM |
1717 | } |
1718 | } | |
1719 | ||
d50d200a SM |
1720 | *argc -= argpar_iter_ingested_orig_args(argpar_iter); |
1721 | *argv += argpar_iter_ingested_orig_args(argpar_iter); | |
4624dad0 SM |
1722 | |
1723 | if (!session_name_arg) { | |
1724 | ERR("Missing session name."); | |
1725 | goto error; | |
1726 | } | |
1727 | ||
1728 | action = create_action_cb(); | |
1729 | if (!action) { | |
1730 | ERR("Failed to allocate %s session action.", action_name); | |
1731 | goto error; | |
1732 | } | |
1733 | ||
1734 | action_status = set_session_name_cb(action, session_name_arg); | |
1735 | if (action_status != LTTNG_ACTION_STATUS_OK) { | |
1736 | ERR("Failed to set action %s session's session name to '%s'.", | |
1737 | action_name, session_name_arg); | |
1738 | goto error; | |
1739 | } | |
1740 | ||
e45dd625 | 1741 | if (policy) { |
7f4d5b07 | 1742 | action_status = set_rate_policy_cb(action, policy); |
e45dd625 | 1743 | if (action_status != LTTNG_ACTION_STATUS_OK) { |
7f4d5b07 | 1744 | ERR("Failed to set rate policy"); |
e45dd625 JR |
1745 | goto error; |
1746 | } | |
1747 | } | |
1748 | ||
4624dad0 SM |
1749 | goto end; |
1750 | ||
1751 | error: | |
1752 | lttng_action_destroy(action); | |
1753 | action = NULL; | |
d50d200a | 1754 | |
4624dad0 | 1755 | end: |
7f4d5b07 | 1756 | lttng_rate_policy_destroy(policy); |
d50d200a SM |
1757 | argpar_item_destroy(argpar_item); |
1758 | argpar_iter_destroy(argpar_iter); | |
4624dad0 SM |
1759 | return action; |
1760 | } | |
1761 | ||
1762 | static | |
1763 | struct lttng_action *handle_action_start_session(int *argc, | |
35c4b2b3 | 1764 | const char ***argv, int argc_offset) |
4624dad0 | 1765 | { |
e45dd625 | 1766 | return handle_action_simple_session_with_policy(argc, argv, |
35c4b2b3 | 1767 | argc_offset, |
e45dd625 JR |
1768 | lttng_action_start_session_create, |
1769 | lttng_action_start_session_set_session_name, | |
7f4d5b07 | 1770 | lttng_action_start_session_set_rate_policy, "start"); |
4624dad0 SM |
1771 | } |
1772 | ||
1773 | static | |
1774 | struct lttng_action *handle_action_stop_session(int *argc, | |
35c4b2b3 | 1775 | const char ***argv, int argc_offset) |
4624dad0 | 1776 | { |
e45dd625 | 1777 | return handle_action_simple_session_with_policy(argc, argv, |
35c4b2b3 | 1778 | argc_offset, |
e45dd625 JR |
1779 | lttng_action_stop_session_create, |
1780 | lttng_action_stop_session_set_session_name, | |
7f4d5b07 | 1781 | lttng_action_stop_session_set_rate_policy, "stop"); |
4624dad0 SM |
1782 | } |
1783 | ||
1784 | static | |
1785 | struct lttng_action *handle_action_rotate_session(int *argc, | |
35c4b2b3 | 1786 | const char ***argv, int argc_offset) |
4624dad0 | 1787 | { |
e45dd625 | 1788 | return handle_action_simple_session_with_policy(argc, argv, |
35c4b2b3 | 1789 | argc_offset, |
4624dad0 SM |
1790 | lttng_action_rotate_session_create, |
1791 | lttng_action_rotate_session_set_session_name, | |
7f4d5b07 | 1792 | lttng_action_rotate_session_set_rate_policy, |
4624dad0 SM |
1793 | "rotate"); |
1794 | } | |
1795 | ||
1796 | static const struct argpar_opt_descr snapshot_action_opt_descrs[] = { | |
1797 | { OPT_NAME, 'n', "name", true }, | |
1798 | { OPT_MAX_SIZE, 'm', "max-size", true }, | |
1799 | { OPT_CTRL_URL, '\0', "ctrl-url", true }, | |
1800 | { OPT_DATA_URL, '\0', "data-url", true }, | |
1801 | { OPT_URL, '\0', "url", true }, | |
1802 | { OPT_PATH, '\0', "path", true }, | |
bbadb5e0 | 1803 | { OPT_RATE_POLICY, '\0', "rate-policy", true }, |
4624dad0 SM |
1804 | ARGPAR_OPT_DESCR_SENTINEL |
1805 | }; | |
1806 | ||
1807 | static | |
1808 | struct lttng_action *handle_action_snapshot_session(int *argc, | |
35c4b2b3 | 1809 | const char ***argv, int argc_offset) |
4624dad0 SM |
1810 | { |
1811 | struct lttng_action *action = NULL; | |
d50d200a SM |
1812 | struct argpar_iter *argpar_iter = NULL; |
1813 | const struct argpar_item *argpar_item = NULL; | |
4624dad0 SM |
1814 | const char *session_name_arg = NULL; |
1815 | char *snapshot_name_arg = NULL; | |
1816 | char *ctrl_url_arg = NULL; | |
1817 | char *data_url_arg = NULL; | |
1818 | char *max_size_arg = NULL; | |
1819 | char *url_arg = NULL; | |
1820 | char *path_arg = NULL; | |
1821 | char *error = NULL; | |
1822 | enum lttng_action_status action_status; | |
1823 | struct lttng_snapshot_output *snapshot_output = NULL; | |
7f4d5b07 | 1824 | struct lttng_rate_policy *policy = NULL; |
4624dad0 SM |
1825 | int ret; |
1826 | unsigned int locations_specified = 0; | |
1827 | ||
d50d200a SM |
1828 | argpar_iter = argpar_iter_create(*argc, *argv, snapshot_action_opt_descrs); |
1829 | if (!argpar_iter) { | |
1830 | ERR("Failed to allocate an argpar iter."); | |
4624dad0 SM |
1831 | goto error; |
1832 | } | |
1833 | ||
1834 | while (true) { | |
d50d200a | 1835 | enum parse_next_item_status status; |
4624dad0 | 1836 | |
35c4b2b3 | 1837 | status = parse_next_item(argpar_iter, &argpar_item, argc_offset, |
ef9ff9cb SM |
1838 | *argv, false, NULL, "While parsing `snapshot` action:"); |
1839 | if (status == PARSE_NEXT_ITEM_STATUS_ERROR || | |
1840 | status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) { | |
4624dad0 | 1841 | goto error; |
d50d200a | 1842 | } else if (status == PARSE_NEXT_ITEM_STATUS_END) { |
4624dad0 SM |
1843 | break; |
1844 | } | |
1845 | ||
d50d200a | 1846 | LTTNG_ASSERT(status == PARSE_NEXT_ITEM_STATUS_OK); |
4624dad0 | 1847 | |
d50d200a SM |
1848 | if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_OPT) { |
1849 | const struct argpar_opt_descr *descr = | |
1850 | argpar_item_opt_descr(argpar_item); | |
1851 | const char *arg = argpar_item_opt_arg(argpar_item); | |
4624dad0 | 1852 | |
d50d200a | 1853 | switch (descr->id) { |
4624dad0 | 1854 | case OPT_NAME: |
d50d200a | 1855 | if (!assign_string(&snapshot_name_arg, arg, "--name/-n")) { |
4624dad0 SM |
1856 | goto error; |
1857 | } | |
1858 | ||
1859 | break; | |
1860 | case OPT_MAX_SIZE: | |
d50d200a | 1861 | if (!assign_string(&max_size_arg, arg, "--max-size/-m")) { |
4624dad0 SM |
1862 | goto error; |
1863 | } | |
1864 | ||
1865 | break; | |
1866 | case OPT_CTRL_URL: | |
d50d200a | 1867 | if (!assign_string(&ctrl_url_arg, arg, "--ctrl-url")) { |
4624dad0 SM |
1868 | goto error; |
1869 | } | |
1870 | ||
1871 | break; | |
1872 | case OPT_DATA_URL: | |
d50d200a | 1873 | if (!assign_string(&data_url_arg, arg, "--data-url")) { |
4624dad0 SM |
1874 | goto error; |
1875 | } | |
1876 | ||
1877 | break; | |
1878 | case OPT_URL: | |
d50d200a | 1879 | if (!assign_string(&url_arg, arg, "--url")) { |
4624dad0 SM |
1880 | goto error; |
1881 | } | |
1882 | ||
1883 | break; | |
1884 | case OPT_PATH: | |
d50d200a | 1885 | if (!assign_string(&path_arg, arg, "--path")) { |
4624dad0 SM |
1886 | goto error; |
1887 | } | |
1888 | ||
1889 | break; | |
bbadb5e0 | 1890 | case OPT_RATE_POLICY: |
e45dd625 | 1891 | { |
d50d200a | 1892 | policy = parse_rate_policy(arg); |
bbadb5e0 | 1893 | if (!policy) { |
e45dd625 JR |
1894 | goto error; |
1895 | } | |
e45dd625 JR |
1896 | break; |
1897 | } | |
4624dad0 SM |
1898 | default: |
1899 | abort(); | |
1900 | } | |
1901 | } else { | |
d50d200a SM |
1902 | const char *arg = argpar_item_non_opt_arg(argpar_item); |
1903 | const unsigned int idx = argpar_item_non_opt_non_opt_index(argpar_item); | |
4624dad0 | 1904 | |
d50d200a | 1905 | switch (idx) { |
4624dad0 | 1906 | case 0: |
d50d200a | 1907 | session_name_arg = arg; |
4624dad0 SM |
1908 | break; |
1909 | default: | |
d50d200a | 1910 | ERR("Unexpected argument `%s`.", arg); |
4624dad0 SM |
1911 | goto error; |
1912 | } | |
1913 | } | |
1914 | } | |
1915 | ||
d50d200a SM |
1916 | *argc -= argpar_iter_ingested_orig_args(argpar_iter); |
1917 | *argv += argpar_iter_ingested_orig_args(argpar_iter); | |
4624dad0 SM |
1918 | |
1919 | if (!session_name_arg) { | |
1920 | ERR("Missing session name."); | |
1921 | goto error; | |
1922 | } | |
1923 | ||
1924 | /* --ctrl-url and --data-url must come in pair. */ | |
1925 | if (ctrl_url_arg && !data_url_arg) { | |
1926 | ERR("--ctrl-url is specified, but --data-url is missing."); | |
1927 | goto error; | |
1928 | } | |
1929 | ||
1930 | if (!ctrl_url_arg && data_url_arg) { | |
1931 | ERR("--data-url is specified, but --ctrl-url is missing."); | |
1932 | goto error; | |
1933 | } | |
1934 | ||
1935 | locations_specified += !!(ctrl_url_arg || data_url_arg); | |
1936 | locations_specified += !!url_arg; | |
1937 | locations_specified += !!path_arg; | |
1938 | ||
1939 | /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */ | |
1940 | if (locations_specified > 1) { | |
1941 | ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together."); | |
1942 | goto error; | |
1943 | } | |
1944 | ||
1945 | /* | |
1946 | * Did the user specify an option that implies using a | |
1947 | * custom/unregistered output? | |
1948 | */ | |
1949 | if (url_arg || ctrl_url_arg || path_arg) { | |
1950 | snapshot_output = lttng_snapshot_output_create(); | |
1951 | if (!snapshot_output) { | |
1952 | ERR("Failed to allocate a snapshot output."); | |
1953 | goto error; | |
1954 | } | |
1955 | } | |
1956 | ||
1957 | action = lttng_action_snapshot_session_create(); | |
1958 | if (!action) { | |
1959 | ERR("Failed to allocate snapshot session action."); | |
1960 | goto error; | |
1961 | } | |
1962 | ||
1963 | action_status = lttng_action_snapshot_session_set_session_name( | |
1964 | action, session_name_arg); | |
1965 | if (action_status != LTTNG_ACTION_STATUS_OK) { | |
1966 | ERR("Failed to set action snapshot session's session name to '%s'.", | |
1967 | session_name_arg); | |
1968 | goto error; | |
1969 | } | |
1970 | ||
1971 | if (snapshot_name_arg) { | |
1972 | if (!snapshot_output) { | |
1973 | ERR("Can't provide a snapshot output name without a snapshot output destination."); | |
1974 | goto error; | |
1975 | } | |
1976 | ||
1977 | ret = lttng_snapshot_output_set_name( | |
1978 | snapshot_name_arg, snapshot_output); | |
1979 | if (ret != 0) { | |
1980 | ERR("Failed to set name of snapshot output."); | |
1981 | goto error; | |
1982 | } | |
1983 | } | |
1984 | ||
1985 | if (max_size_arg) { | |
1986 | uint64_t max_size; | |
1987 | ||
1988 | if (!snapshot_output) { | |
1989 | ERR("Can't provide a snapshot output max size without a snapshot output destination."); | |
1990 | goto error; | |
1991 | } | |
1992 | ||
1993 | ret = utils_parse_size_suffix(max_size_arg, &max_size); | |
1994 | if (ret != 0) { | |
1995 | ERR("Failed to parse `%s` as a size.", max_size_arg); | |
1996 | goto error; | |
1997 | } | |
1998 | ||
1999 | ret = lttng_snapshot_output_set_size(max_size, snapshot_output); | |
2000 | if (ret != 0) { | |
2001 | ERR("Failed to set snapshot output's max size to %" PRIu64 " bytes.", | |
2002 | max_size); | |
2003 | goto error; | |
2004 | } | |
2005 | } | |
2006 | ||
2007 | if (url_arg) { | |
2008 | int num_uris; | |
2009 | struct lttng_uri *uris; | |
2010 | ||
2011 | if (!strstr(url_arg, "://")) { | |
2012 | ERR("Failed to parse '%s' as an URL.", url_arg); | |
2013 | goto error; | |
2014 | } | |
2015 | ||
2016 | num_uris = uri_parse_str_urls(url_arg, NULL, &uris); | |
2017 | if (num_uris < 1) { | |
2018 | ERR("Failed to parse '%s' as an URL.", url_arg); | |
2019 | goto error; | |
2020 | } | |
2021 | ||
2022 | if (uris[0].dtype == LTTNG_DST_PATH) { | |
2023 | ret = lttng_snapshot_output_set_local_path( | |
2024 | uris[0].dst.path, snapshot_output); | |
2025 | free(uris); | |
2026 | if (ret != 0) { | |
2027 | ERR("Failed to assign '%s' as a local destination.", | |
2028 | url_arg); | |
2029 | goto error; | |
2030 | } | |
2031 | } else { | |
2032 | ret = lttng_snapshot_output_set_network_url( | |
2033 | url_arg, snapshot_output); | |
2034 | free(uris); | |
2035 | if (ret != 0) { | |
2036 | ERR("Failed to assign '%s' as a network URL.", | |
2037 | url_arg); | |
2038 | goto error; | |
2039 | } | |
2040 | } | |
2041 | } | |
2042 | ||
2043 | if (path_arg) { | |
2044 | ret = lttng_snapshot_output_set_local_path( | |
2045 | path_arg, snapshot_output); | |
2046 | if (ret != 0) { | |
2047 | ERR("Failed to parse '%s' as a local path.", path_arg); | |
2048 | goto error; | |
2049 | } | |
2050 | } | |
2051 | ||
2052 | if (ctrl_url_arg) { | |
2053 | /* | |
2054 | * Two argument form, network output with separate control and | |
2055 | * data URLs. | |
2056 | */ | |
2057 | ret = lttng_snapshot_output_set_network_urls( | |
2058 | ctrl_url_arg, data_url_arg, snapshot_output); | |
2059 | if (ret != 0) { | |
2060 | ERR("Failed to parse `%s` and `%s` as control and data URLs.", | |
2061 | ctrl_url_arg, data_url_arg); | |
2062 | goto error; | |
2063 | } | |
2064 | } | |
2065 | ||
2066 | if (snapshot_output) { | |
2067 | action_status = lttng_action_snapshot_session_set_output( | |
2068 | action, snapshot_output); | |
2069 | if (action_status != LTTNG_ACTION_STATUS_OK) { | |
2070 | ERR("Failed to set snapshot session action's output."); | |
2071 | goto error; | |
2072 | } | |
2073 | ||
2074 | /* Ownership of `snapshot_output` has been transferred to the action. */ | |
2075 | snapshot_output = NULL; | |
2076 | } | |
2077 | ||
e45dd625 JR |
2078 | if (policy) { |
2079 | enum lttng_action_status status; | |
7f4d5b07 | 2080 | status = lttng_action_snapshot_session_set_rate_policy( |
e45dd625 JR |
2081 | action, policy); |
2082 | if (status != LTTNG_ACTION_STATUS_OK) { | |
7f4d5b07 | 2083 | ERR("Failed to set rate policy"); |
e45dd625 JR |
2084 | goto error; |
2085 | } | |
2086 | } | |
2087 | ||
4624dad0 SM |
2088 | goto end; |
2089 | ||
2090 | error: | |
2091 | lttng_action_destroy(action); | |
2092 | action = NULL; | |
2093 | free(error); | |
2094 | end: | |
2095 | free(snapshot_name_arg); | |
2096 | free(path_arg); | |
2097 | free(url_arg); | |
2098 | free(ctrl_url_arg); | |
2099 | free(data_url_arg); | |
2100 | free(snapshot_output); | |
f6b73530 | 2101 | free(max_size_arg); |
7f4d5b07 | 2102 | lttng_rate_policy_destroy(policy); |
d50d200a SM |
2103 | argpar_item_destroy(argpar_item); |
2104 | argpar_iter_destroy(argpar_iter); | |
4624dad0 SM |
2105 | return action; |
2106 | } | |
2107 | ||
f1494934 | 2108 | namespace { |
4624dad0 SM |
2109 | struct action_descr { |
2110 | const char *name; | |
35c4b2b3 SM |
2111 | struct lttng_action *(*handler) (int *argc, const char ***argv, |
2112 | int argc_offset); | |
4624dad0 | 2113 | }; |
f1494934 | 2114 | } /* namespace */ |
4624dad0 SM |
2115 | |
2116 | static const | |
2117 | struct action_descr action_descrs[] = { | |
2118 | { "notify", handle_action_notify }, | |
2119 | { "start-session", handle_action_start_session }, | |
2120 | { "stop-session", handle_action_stop_session }, | |
2121 | { "rotate-session", handle_action_rotate_session }, | |
2122 | { "snapshot-session", handle_action_snapshot_session }, | |
2123 | }; | |
2124 | ||
ef9ff9cb SM |
2125 | static |
2126 | void print_valid_action_names(void) | |
2127 | { | |
2128 | unsigned int i; | |
2129 | ||
2130 | ERR("Valid action names are:"); | |
2131 | ||
2132 | for (i = 0; i < ARRAY_SIZE(condition_descrs); ++i) { | |
2133 | ERR(" %s", action_descrs[i].name); | |
2134 | } | |
2135 | } | |
2136 | ||
4624dad0 | 2137 | static |
35c4b2b3 | 2138 | struct lttng_action *parse_action(const char *action_name, int *argc, |
e776cf47 SM |
2139 | const char ***argv, int argc_offset, int orig_arg_index, |
2140 | const char *orig_arg) | |
4624dad0 SM |
2141 | { |
2142 | int i; | |
2143 | struct lttng_action *action; | |
4624dad0 SM |
2144 | const struct action_descr *descr = NULL; |
2145 | ||
4624dad0 SM |
2146 | for (i = 0; i < ARRAY_SIZE(action_descrs); i++) { |
2147 | if (strcmp(action_name, action_descrs[i].name) == 0) { | |
2148 | descr = &action_descrs[i]; | |
2149 | break; | |
2150 | } | |
2151 | } | |
2152 | ||
2153 | if (!descr) { | |
ef9ff9cb | 2154 | ERR(WHILE_PARSING_ARG_N_ARG_FMT "Unknown action name '%s'", |
e776cf47 | 2155 | orig_arg_index + 1, orig_arg, action_name); |
ef9ff9cb | 2156 | print_valid_action_names(); |
4624dad0 SM |
2157 | goto error; |
2158 | } | |
2159 | ||
35c4b2b3 | 2160 | action = descr->handler(argc, argv, argc_offset); |
4624dad0 SM |
2161 | if (!action) { |
2162 | /* The handler has already printed an error message. */ | |
2163 | goto error; | |
2164 | } | |
2165 | ||
2166 | goto end; | |
2167 | error: | |
2168 | action = NULL; | |
2169 | end: | |
2170 | return action; | |
2171 | } | |
2172 | ||
2173 | static const | |
2174 | struct argpar_opt_descr add_trigger_options[] = { | |
2175 | { OPT_HELP, 'h', "help", false }, | |
2176 | { OPT_LIST_OPTIONS, '\0', "list-options", false }, | |
29bcf415 SM |
2177 | { OPT_CONDITION, '\0', "condition", true }, |
2178 | { OPT_ACTION, '\0', "action", true }, | |
1d4b59f2 | 2179 | { OPT_NAME, '\0', "name", true }, |
481c5310 | 2180 | { OPT_OWNER_UID, '\0', "owner-uid", true }, |
4624dad0 SM |
2181 | ARGPAR_OPT_DESCR_SENTINEL, |
2182 | }; | |
2183 | ||
2184 | static | |
2185 | void lttng_actions_destructor(void *p) | |
2186 | { | |
48a40005 | 2187 | struct lttng_action *action = (lttng_action *) p; |
4624dad0 SM |
2188 | |
2189 | lttng_action_destroy(action); | |
2190 | } | |
2191 | ||
2192 | int cmd_add_trigger(int argc, const char **argv) | |
2193 | { | |
2194 | int ret; | |
2195 | int my_argc = argc - 1; | |
2196 | const char **my_argv = argv + 1; | |
2197 | struct lttng_condition *condition = NULL; | |
2198 | struct lttng_dynamic_pointer_array actions; | |
d50d200a SM |
2199 | struct argpar_iter *argpar_iter = NULL; |
2200 | const struct argpar_item *argpar_item = NULL; | |
ef9ff9cb | 2201 | const struct argpar_error *argpar_error = NULL; |
702f26c8 | 2202 | struct lttng_action *action_list = NULL; |
4624dad0 SM |
2203 | struct lttng_action *action = NULL; |
2204 | struct lttng_trigger *trigger = NULL; | |
1d4b59f2 | 2205 | char *name = NULL; |
4624dad0 | 2206 | int i; |
481c5310 | 2207 | char *owner_uid = NULL; |
a5c2d2a7 | 2208 | enum lttng_error_code ret_code; |
523c4f8c | 2209 | struct mi_writer *mi_writer = NULL; |
4624dad0 SM |
2210 | |
2211 | lttng_dynamic_pointer_array_init(&actions, lttng_actions_destructor); | |
2212 | ||
523c4f8c JR |
2213 | if (lttng_opt_mi) { |
2214 | mi_writer = mi_lttng_writer_create( | |
2215 | fileno(stdout), lttng_opt_mi); | |
2216 | if (!mi_writer) { | |
2217 | ret = CMD_ERROR; | |
2218 | goto error; | |
2219 | } | |
2220 | ||
2221 | /* Open command element. */ | |
2222 | ret = mi_lttng_writer_command_open(mi_writer, | |
2223 | mi_lttng_element_command_add_trigger); | |
2224 | if (ret) { | |
2225 | ret = CMD_ERROR; | |
2226 | goto error; | |
2227 | } | |
2228 | ||
2229 | /* Open output element. */ | |
2230 | ret = mi_lttng_writer_open_element( | |
2231 | mi_writer, mi_lttng_element_command_output); | |
2232 | if (ret) { | |
2233 | ret = CMD_ERROR; | |
2234 | goto error; | |
2235 | } | |
2236 | } | |
2237 | ||
4624dad0 | 2238 | while (true) { |
d50d200a | 2239 | enum parse_next_item_status status; |
4624dad0 | 2240 | int ingested_args; |
d50d200a SM |
2241 | const struct argpar_opt_descr *descr; |
2242 | const char *arg; | |
4624dad0 | 2243 | |
d50d200a SM |
2244 | argpar_iter_destroy(argpar_iter); |
2245 | argpar_iter = argpar_iter_create(my_argc, my_argv, | |
4624dad0 | 2246 | add_trigger_options); |
d50d200a SM |
2247 | if (!argpar_iter) { |
2248 | ERR("Failed to create argpar iter."); | |
4624dad0 SM |
2249 | goto error; |
2250 | } | |
2251 | ||
35c4b2b3 | 2252 | status = parse_next_item(argpar_iter, &argpar_item, |
ef9ff9cb | 2253 | argc - my_argc, my_argv, true, &argpar_error, NULL); |
d50d200a | 2254 | if (status == PARSE_NEXT_ITEM_STATUS_ERROR) { |
ef9ff9cb SM |
2255 | |
2256 | if (argpar_error_type(argpar_error) == | |
2257 | ARGPAR_ERROR_TYPE_MISSING_OPT_ARG) { | |
2258 | int opt_id = argpar_error_opt_descr(argpar_error, NULL)->id; | |
2259 | ||
2260 | if (opt_id == OPT_CONDITION) { | |
2261 | print_valid_condition_names(); | |
2262 | } else if (opt_id == OPT_ACTION) { | |
2263 | print_valid_action_names(); | |
2264 | } | |
2265 | } | |
2266 | ||
2267 | goto error; | |
2268 | } else if (status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) { | |
4624dad0 | 2269 | goto error; |
d50d200a | 2270 | } else if (status == PARSE_NEXT_ITEM_STATUS_END) { |
4624dad0 SM |
2271 | break; |
2272 | } | |
2273 | ||
d50d200a | 2274 | LTTNG_ASSERT(status == PARSE_NEXT_ITEM_STATUS_OK); |
4624dad0 | 2275 | |
d50d200a SM |
2276 | if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_NON_OPT) { |
2277 | ERR("Unexpected argument `%s`.", | |
2278 | argpar_item_non_opt_arg(argpar_item)); | |
4624dad0 SM |
2279 | goto error; |
2280 | } | |
2281 | ||
d50d200a | 2282 | ingested_args = argpar_iter_ingested_orig_args(argpar_iter); |
4624dad0 SM |
2283 | |
2284 | my_argc -= ingested_args; | |
2285 | my_argv += ingested_args; | |
2286 | ||
d50d200a SM |
2287 | descr = argpar_item_opt_descr(argpar_item); |
2288 | arg = argpar_item_opt_arg(argpar_item); | |
2289 | ||
2290 | switch (descr->id) { | |
4624dad0 SM |
2291 | case OPT_HELP: |
2292 | SHOW_HELP(); | |
2293 | ret = 0; | |
2294 | goto end; | |
2295 | case OPT_LIST_OPTIONS: | |
2296 | list_cmd_options_argpar(stdout, add_trigger_options); | |
2297 | ret = 0; | |
2298 | goto end; | |
2299 | case OPT_CONDITION: | |
2300 | { | |
2301 | if (condition) { | |
2302 | ERR("A --condition was already given."); | |
2303 | goto error; | |
2304 | } | |
2305 | ||
35c4b2b3 | 2306 | condition = parse_condition(arg, &my_argc, &my_argv, |
e776cf47 SM |
2307 | argc - my_argc, argc - my_argc - ingested_args, |
2308 | my_argv[-ingested_args]); | |
4624dad0 SM |
2309 | if (!condition) { |
2310 | /* | |
2311 | * An error message was already printed by | |
2312 | * parse_condition. | |
2313 | */ | |
2314 | goto error; | |
2315 | } | |
2316 | ||
2317 | break; | |
2318 | } | |
2319 | case OPT_ACTION: | |
2320 | { | |
35c4b2b3 | 2321 | action = parse_action(arg, &my_argc, &my_argv, |
e776cf47 SM |
2322 | argc - my_argc, argc - my_argc - ingested_args, |
2323 | my_argv[-ingested_args]); | |
4624dad0 SM |
2324 | if (!action) { |
2325 | /* | |
2326 | * An error message was already printed by | |
2327 | * parse_condition. | |
2328 | */ | |
2329 | goto error; | |
2330 | } | |
2331 | ||
2332 | ret = lttng_dynamic_pointer_array_add_pointer( | |
2333 | &actions, action); | |
2334 | if (ret) { | |
2335 | ERR("Failed to add pointer to pointer array."); | |
2336 | goto error; | |
2337 | } | |
2338 | ||
a8940c5e | 2339 | /* Ownership of the action was transferred to the list. */ |
4624dad0 SM |
2340 | action = NULL; |
2341 | ||
2342 | break; | |
2343 | } | |
1d4b59f2 | 2344 | case OPT_NAME: |
4624dad0 | 2345 | { |
d50d200a | 2346 | if (!assign_string(&name, arg, "--name")) { |
4624dad0 SM |
2347 | goto error; |
2348 | } | |
2349 | ||
2350 | break; | |
2351 | } | |
481c5310 | 2352 | case OPT_OWNER_UID: |
4624dad0 | 2353 | { |
d50d200a | 2354 | if (!assign_string(&owner_uid, arg, |
481c5310 | 2355 | "--owner-uid")) { |
4624dad0 SM |
2356 | goto error; |
2357 | } | |
2358 | ||
2359 | break; | |
2360 | } | |
2361 | default: | |
2362 | abort(); | |
2363 | } | |
2364 | } | |
2365 | ||
2366 | if (!condition) { | |
2367 | ERR("Missing --condition."); | |
2368 | goto error; | |
2369 | } | |
2370 | ||
2371 | if (lttng_dynamic_pointer_array_get_count(&actions) == 0) { | |
2372 | ERR("Need at least one --action."); | |
2373 | goto error; | |
2374 | } | |
2375 | ||
702f26c8 JR |
2376 | action_list = lttng_action_list_create(); |
2377 | if (!action_list) { | |
4624dad0 SM |
2378 | goto error; |
2379 | } | |
2380 | ||
2381 | for (i = 0; i < lttng_dynamic_pointer_array_get_count(&actions); i++) { | |
2382 | enum lttng_action_status status; | |
2383 | ||
48a40005 | 2384 | action = (lttng_action *) lttng_dynamic_pointer_array_steal_pointer(&actions, i); |
4624dad0 | 2385 | |
702f26c8 | 2386 | status = lttng_action_list_add_action(action_list, action); |
4624dad0 SM |
2387 | if (status != LTTNG_ACTION_STATUS_OK) { |
2388 | goto error; | |
2389 | } | |
2390 | ||
2391 | /* | |
702f26c8 | 2392 | * The `lttng_action_list_add_action()` takes a reference to |
4624dad0 SM |
2393 | * the action. We can destroy ours. |
2394 | */ | |
2395 | lttng_action_destroy(action); | |
2396 | action = NULL; | |
2397 | } | |
2398 | ||
702f26c8 | 2399 | trigger = lttng_trigger_create(condition, action_list); |
4624dad0 SM |
2400 | if (!trigger) { |
2401 | goto error; | |
2402 | } | |
2403 | ||
481c5310 | 2404 | if (owner_uid) { |
4624dad0 SM |
2405 | enum lttng_trigger_status trigger_status; |
2406 | char *end; | |
2407 | long long uid; | |
2408 | ||
2409 | errno = 0; | |
481c5310 SM |
2410 | uid = strtol(owner_uid, &end, 10); |
2411 | if (end == owner_uid || *end != '\0' || errno != 0) { | |
2412 | ERR("Failed to parse `%s` as a user id.", owner_uid); | |
c6c76a9d | 2413 | goto error; |
4624dad0 SM |
2414 | } |
2415 | ||
2416 | trigger_status = lttng_trigger_set_owner_uid(trigger, uid); | |
2417 | if (trigger_status != LTTNG_TRIGGER_STATUS_OK) { | |
2418 | ERR("Failed to set trigger's user identity."); | |
2419 | goto error; | |
2420 | } | |
2421 | } | |
2422 | ||
a5c2d2a7 JG |
2423 | if (name) { |
2424 | ret_code = lttng_register_trigger_with_name(trigger, name); | |
2425 | } else { | |
2426 | ret_code = lttng_register_trigger_with_automatic_name(trigger); | |
2427 | } | |
2428 | ||
2429 | if (ret_code != LTTNG_OK) { | |
2430 | ERR("Failed to register trigger: %s.", | |
2431 | lttng_strerror(-ret_code)); | |
4624dad0 SM |
2432 | goto error; |
2433 | } | |
2434 | ||
523c4f8c JR |
2435 | if (lttng_opt_mi) { |
2436 | ret_code = lttng_trigger_mi_serialize(trigger, mi_writer, NULL); | |
2437 | if (ret_code != LTTNG_OK) { | |
2438 | goto error; | |
2439 | } | |
f674361c JG |
2440 | } else { |
2441 | const char *returned_trigger_name; | |
2442 | const enum lttng_trigger_status trigger_status = | |
2443 | lttng_trigger_get_name(trigger, | |
2444 | &returned_trigger_name); | |
2445 | ||
2446 | if (trigger_status != LTTNG_TRIGGER_STATUS_OK) { | |
2447 | WARN("Failed to retrieve the added trigger's name."); | |
2448 | } else { | |
2449 | MSG("Added trigger `%s`.", returned_trigger_name); | |
2450 | } | |
523c4f8c JR |
2451 | } |
2452 | ||
a5c2d2a7 | 2453 | ret = 0; |
4624dad0 SM |
2454 | |
2455 | goto end; | |
2456 | ||
2457 | error: | |
2458 | ret = 1; | |
2459 | ||
2460 | end: | |
523c4f8c | 2461 | /* Mi closing. */ |
4627fa54 | 2462 | if (lttng_opt_mi && mi_writer) { |
523c4f8c JR |
2463 | int mi_ret; |
2464 | ||
2465 | /* Close output element. */ | |
2466 | mi_ret = mi_lttng_writer_close_element(mi_writer); | |
2467 | if (mi_ret) { | |
2468 | ret = 1; | |
2469 | goto cleanup; | |
2470 | } | |
2471 | ||
2472 | mi_ret = mi_lttng_writer_write_element_bool(mi_writer, | |
2473 | mi_lttng_element_command_success, ret ? 0 : 1); | |
2474 | if (mi_ret) { | |
2475 | ret = 1; | |
2476 | goto cleanup; | |
2477 | } | |
2478 | ||
2479 | /* Command element close. */ | |
2480 | mi_ret = mi_lttng_writer_command_close(mi_writer); | |
2481 | if (mi_ret) { | |
2482 | ret = 1; | |
2483 | goto cleanup; | |
2484 | } | |
2485 | } | |
2486 | ||
2487 | cleanup: | |
ef9ff9cb | 2488 | argpar_error_destroy(argpar_error); |
d50d200a | 2489 | argpar_iter_destroy(argpar_iter); |
4624dad0 SM |
2490 | argpar_item_destroy(argpar_item); |
2491 | lttng_dynamic_pointer_array_reset(&actions); | |
2492 | lttng_condition_destroy(condition); | |
702f26c8 | 2493 | lttng_action_destroy(action_list); |
4624dad0 SM |
2494 | lttng_action_destroy(action); |
2495 | lttng_trigger_destroy(trigger); | |
1d4b59f2 | 2496 | free(name); |
481c5310 | 2497 | free(owner_uid); |
523c4f8c JR |
2498 | if (mi_writer && mi_lttng_writer_destroy(mi_writer)) { |
2499 | /* Preserve original error code. */ | |
2500 | ret = ret ? ret : CMD_ERROR; | |
2501 | } | |
2502 | ||
4624dad0 SM |
2503 | return ret; |
2504 | } |