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