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