Remove lttng_event_rule_tracepoint
[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/string-utils/string-utils.h"
19 #include "common/utils.h"
20 /* For lttng_event_rule_type_str(). */
21 #include <lttng/event-rule/event-rule-internal.h>
22 #include <lttng/lttng.h>
23 #include "common/filter/filter-ast.h"
24 #include "common/filter/filter-ir.h"
25 #include "common/dynamic-array.h"
26
27 #if (LTTNG_SYMBOL_NAME_LEN == 256)
28 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "255"
29 #endif
30
31 #ifdef LTTNG_EMBED_HELP
32 static const char help_msg[] =
33 #include <lttng-add-trigger.1.h>
34 ;
35 #endif
36
37 enum {
38 OPT_HELP,
39 OPT_LIST_OPTIONS,
40
41 OPT_CONDITION,
42 OPT_ACTION,
43 OPT_ID,
44 OPT_OWNER_UID,
45 OPT_RATE_POLICY,
46
47 OPT_NAME,
48 OPT_FILTER,
49 OPT_EXCLUDE_NAME,
50 OPT_EVENT_NAME,
51 OPT_LOG_LEVEL,
52
53 OPT_TYPE,
54 OPT_LOCATION,
55
56 OPT_MAX_SIZE,
57 OPT_DATA_URL,
58 OPT_CTRL_URL,
59 OPT_URL,
60 OPT_PATH,
61
62 OPT_CAPTURE,
63 };
64
65 static const struct argpar_opt_descr event_rule_opt_descrs[] = {
66 { OPT_FILTER, 'f', "filter", true },
67 { OPT_NAME, 'n', "name", true },
68 { OPT_EXCLUDE_NAME, 'x', "exclude-name", true },
69 { OPT_LOG_LEVEL, 'l', "log-level", true },
70 { OPT_EVENT_NAME, 'E', "event-name", true },
71
72 { OPT_TYPE, 't', "type", true },
73 { OPT_LOCATION, 'L', "location", true },
74
75 /* Capture descriptor */
76 { OPT_CAPTURE, '\0', "capture", true },
77
78 ARGPAR_OPT_DESCR_SENTINEL
79 };
80
81 static
82 bool has_syscall_prefix(const char *arg)
83 {
84 bool matches = false;
85 const char kernel_syscall_type_opt_prefix[] = "kernel:syscall";
86 const size_t kernel_syscall_type_opt_prefix_len =
87 sizeof(kernel_syscall_type_opt_prefix) - 1;
88 const char syscall_type_opt_prefix[] = "syscall";
89 const size_t syscall_type_opt_prefix_len =
90 sizeof(syscall_type_opt_prefix) - 1;
91
92 if (strncmp(arg, syscall_type_opt_prefix,
93 syscall_type_opt_prefix_len) == 0) {
94 matches = true;
95 } else if (strncmp(arg, kernel_syscall_type_opt_prefix,
96 kernel_syscall_type_opt_prefix_len) == 0) {
97 matches = true;
98 } else {
99 matches = false;
100 }
101
102 return matches;
103 }
104
105 static
106 bool assign_event_rule_type(enum lttng_event_rule_type *dest, const char *arg)
107 {
108 bool ret;
109
110 if (*dest != LTTNG_EVENT_RULE_TYPE_UNKNOWN) {
111 ERR("More than one `--type` was specified.");
112 goto error;
113 }
114
115 if (strcmp(arg, "user") == 0 || strcmp(arg, "user:tracepoint") == 0) {
116 *dest = LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT;
117 } else if (strcmp(arg, "kernel") == 0 ||
118 strcmp(arg, "kernel:tracepoint") == 0) {
119 *dest = LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT;
120 } else if (strcmp(arg, "jul") == 0 || strcmp(arg, "jul:logging") == 0) {
121 *dest = LTTNG_EVENT_RULE_TYPE_JUL_LOGGING;
122 } else if (strcmp(arg, "log4j") == 0 ||
123 strcmp(arg, "log4j:logging") == 0) {
124 *dest = LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING;
125 } else if (strcmp(arg, "python") == 0 ||
126 strcmp(arg, "python:logging") == 0) {
127 *dest = LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING;
128 } else if (strcmp(arg, "kprobe") == 0 ||
129 strcmp(arg, "kernel:kprobe") == 0) {
130 *dest = LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE;
131 } else if (strcmp(arg, "kernel:uprobe") == 0) {
132 *dest = LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE;
133 } else if (strcmp(arg, "function") == 0) {
134 *dest = LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION;
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 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION:
882 if (!location) {
883 ERR("Event rule of type %s requires a --location.",
884 lttng_event_rule_type_str(event_rule_type));
885 goto error;
886 }
887
888 if (!event_name) {
889 event_name = strdup(location);
890 }
891
892 break;
893
894 default:
895 if (location) {
896 ERR("Can't use --location with %s event rules.",
897 lttng_event_rule_type_str(event_rule_type));
898 goto error;
899 }
900
901 if (event_name) {
902 ERR("Can't use --event-name with %s event rules.",
903 lttng_event_rule_type_str(
904 event_rule_type));
905 goto error;
906 }
907 }
908
909 /*
910 * Update *argc and *argv so our caller can keep parsing what follows.
911 */
912 consumed_args = argpar_state_get_ingested_orig_args(state);
913 assert(consumed_args >= 0);
914 *argc -= consumed_args;
915 *argv += consumed_args;
916
917 /*
918 * Adding a filter to a probe, function or userspace-probe would be
919 * denied by the kernel tracer as it's not supported at the moment. We
920 * do an early check here to warn the user.
921 */
922 if (filter) {
923 switch (event_rule_type) {
924 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
925 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT:
926 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
927 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
928 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
929 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL:
930 break;
931 default:
932 ERR("Filter expressions are not supported for %s event rules.",
933 lttng_event_rule_type_str(event_rule_type));
934 goto error;
935 }
936 }
937
938 /*
939 * If --exclude-name/-x was passed, split it into an exclusion list.
940 * Exclusions are only supported by
941 * LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT for now.
942 */
943 if (lttng_dynamic_pointer_array_get_count(&exclude_names) > 0) {
944 if (event_rule_type != LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT) {
945 ERR("Event name exclusions are not yet implemented for %s event rules.",
946 lttng_event_rule_type_str(event_rule_type));
947 goto error;
948 }
949
950 if (validate_exclusion_list(name, &exclude_names) != 0) {
951 /*
952 * Assume validate_exclusion_list already prints an
953 * error message.
954 */
955 goto error;
956 }
957 }
958
959 if (log_level_str) {
960 switch (event_rule_type) {
961 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
962 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
963 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
964 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
965 {
966 int log_level;
967 bool log_level_only;
968
969 if (strcmp(log_level_str, "..") == 0) {
970 /*
971 * ".." is the same as passing no log level
972 * option and correspond to the "ANY" case.
973 */
974 break;
975 }
976
977 if (!parse_log_level_string(log_level_str, event_rule_type,
978 &log_level, &log_level_only)) {
979 ERR("Failed to parse log level string `%s`.",
980 log_level_str);
981 goto error;
982 }
983
984 if (log_level_only) {
985 log_level_rule = lttng_log_level_rule_exactly_create(log_level);
986 } else {
987 log_level_rule = lttng_log_level_rule_at_least_as_severe_as_create(log_level);
988 }
989
990 if (log_level_rule == NULL) {
991 ERR("Failed to create log level rule object.");
992 goto error;
993 }
994 break;
995 }
996 default:
997 ERR("Log levels are not supported for %s event rules.",
998 lttng_event_rule_type_str(event_rule_type));
999 goto error;
1000 }
1001 }
1002
1003 /* Finally, create the event rule object. */
1004 switch (event_rule_type) {
1005 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
1006 {
1007 enum lttng_event_rule_status event_rule_status;
1008
1009 res.er = lttng_event_rule_user_tracepoint_create();
1010 if (!res.er) {
1011 ERR("Failed to create user_tracepoint event rule.");
1012 goto error;
1013 }
1014
1015 /* Set pattern. */
1016 event_rule_status = lttng_event_rule_user_tracepoint_set_name_pattern(
1017 res.er, name);
1018 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1019 ERR("Failed to set user_tracepoint event rule's pattern to '%s'.",
1020 name);
1021 goto error;
1022 }
1023
1024 /* Set filter. */
1025 if (filter) {
1026 event_rule_status = lttng_event_rule_user_tracepoint_set_filter(
1027 res.er, filter);
1028 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1029 ERR("Failed to set user_tracepoint event rule's filter to '%s'.",
1030 filter);
1031 goto error;
1032 }
1033 }
1034
1035 /* Set exclusion list. */
1036 if (lttng_dynamic_pointer_array_get_count(&exclude_names) > 0) {
1037 int n;
1038 int count = lttng_dynamic_pointer_array_get_count(
1039 &exclude_names);
1040
1041 for (n = 0; n < count; n++) {
1042 const char *exclude_name =
1043 lttng_dynamic_pointer_array_get_pointer(
1044 &exclude_names,
1045 n);
1046
1047 event_rule_status =
1048 lttng_event_rule_user_tracepoint_add_name_pattern_exclusion(
1049 res.er,
1050 exclude_name);
1051 if (event_rule_status !=
1052 LTTNG_EVENT_RULE_STATUS_OK) {
1053 ERR("Failed to set user_tracepoint exclusion list element '%s'",
1054 exclude_name);
1055 goto error;
1056 }
1057 }
1058 }
1059
1060 if (log_level_rule) {
1061 event_rule_status =
1062 lttng_event_rule_user_tracepoint_set_log_level_rule(
1063 res.er, log_level_rule);
1064
1065 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1066 ERR("Failed to set log level on event fule.");
1067 goto error;
1068 }
1069 }
1070
1071 break;
1072 }
1073 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT:
1074 {
1075 enum lttng_event_rule_status event_rule_status;
1076
1077 res.er = lttng_event_rule_kernel_tracepoint_create();
1078 if (!res.er) {
1079 ERR("Failed to create kernel_tracepoint event rule.");
1080 goto error;
1081 }
1082
1083 /* Set pattern. */
1084 event_rule_status = lttng_event_rule_kernel_tracepoint_set_name_pattern(
1085 res.er, name);
1086 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1087 ERR("Failed to set kernel_tracepoint event rule's pattern to '%s'.",
1088 name);
1089 goto error;
1090 }
1091
1092 /* Set filter. */
1093 if (filter) {
1094 event_rule_status = lttng_event_rule_kernel_tracepoint_set_filter(
1095 res.er, filter);
1096 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1097 ERR("Failed to set kernel_tracepoint event rule's filter to '%s'.",
1098 filter);
1099 goto error;
1100 }
1101 }
1102 break;
1103 }
1104 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
1105 {
1106 enum lttng_event_rule_status event_rule_status;
1107
1108 res.er = lttng_event_rule_jul_logging_create();
1109 if (!res.er) {
1110 ERR("Failed to create jul_logging event rule.");
1111 goto error;
1112 }
1113
1114 /* Set pattern. */
1115 event_rule_status = lttng_event_rule_jul_logging_set_name_pattern(
1116 res.er, name);
1117 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1118 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1119 name);
1120 goto error;
1121 }
1122
1123 /* Set filter. */
1124 if (filter) {
1125 event_rule_status = lttng_event_rule_jul_logging_set_filter(
1126 res.er, filter);
1127 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1128 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1129 filter);
1130 goto error;
1131 }
1132 }
1133
1134 if (log_level_rule) {
1135 event_rule_status =
1136 lttng_event_rule_jul_logging_set_log_level_rule(
1137 res.er, log_level_rule);
1138
1139 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1140 ERR("Failed to set log level on event fule.");
1141 goto error;
1142 }
1143 }
1144 break;
1145 }
1146 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
1147 {
1148 enum lttng_event_rule_status event_rule_status;
1149
1150 res.er = lttng_event_rule_log4j_logging_create();
1151 if (!res.er) {
1152 ERR("Failed to create jul_logging event rule.");
1153 goto error;
1154 }
1155
1156 /* Set pattern. */
1157 event_rule_status = lttng_event_rule_log4j_logging_set_name_pattern(
1158 res.er, name);
1159 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1160 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1161 name);
1162 goto error;
1163 }
1164
1165 /* Set filter. */
1166 if (filter) {
1167 event_rule_status = lttng_event_rule_log4j_logging_set_filter(
1168 res.er, filter);
1169 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1170 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1171 filter);
1172 goto error;
1173 }
1174 }
1175
1176 if (log_level_rule) {
1177 event_rule_status =
1178 lttng_event_rule_log4j_logging_set_log_level_rule(
1179 res.er, log_level_rule);
1180
1181 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1182 ERR("Failed to set log level on event fule.");
1183 goto error;
1184 }
1185 }
1186 break;
1187 }
1188 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
1189 {
1190 enum lttng_event_rule_status event_rule_status;
1191
1192 res.er = lttng_event_rule_python_logging_create();
1193 if (!res.er) {
1194 ERR("Failed to create jul_logging event rule.");
1195 goto error;
1196 }
1197
1198 /* Set pattern. */
1199 event_rule_status = lttng_event_rule_python_logging_set_name_pattern(
1200 res.er, name);
1201 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1202 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1203 name);
1204 goto error;
1205 }
1206
1207 /* Set filter. */
1208 if (filter) {
1209 event_rule_status = lttng_event_rule_python_logging_set_filter(
1210 res.er, filter);
1211 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1212 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1213 filter);
1214 goto error;
1215 }
1216 }
1217
1218 if (log_level_rule) {
1219 event_rule_status =
1220 lttng_event_rule_python_logging_set_log_level_rule(
1221 res.er, log_level_rule);
1222
1223 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1224 ERR("Failed to set log level on event fule.");
1225 goto error;
1226 }
1227 }
1228 break;
1229 }
1230 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE:
1231 {
1232 int ret;
1233 enum lttng_event_rule_status event_rule_status;
1234
1235 ret = parse_kernel_probe_opts(
1236 location, &kernel_probe_location);
1237 if (ret) {
1238 ERR("Failed to parse kernel probe location.");
1239 goto error;
1240 }
1241
1242 assert(kernel_probe_location);
1243 res.er = lttng_event_rule_kernel_kprobe_create(kernel_probe_location);
1244 if (!res.er) {
1245 ERR("Failed to create kprobe event rule.");
1246 goto error;
1247 }
1248
1249 event_rule_status =
1250 lttng_event_rule_kernel_kprobe_set_event_name(
1251 res.er, event_name);
1252 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1253 ERR("Failed to set kprobe event rule's name to '%s'.",
1254 event_name);
1255 goto error;
1256 }
1257
1258 break;
1259 }
1260 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE:
1261 {
1262 int ret;
1263 enum lttng_event_rule_status event_rule_status;
1264
1265 ret = parse_userspace_probe_opts(
1266 location, &userspace_probe_location);
1267 if (ret) {
1268 ERR("Failed to parse user space probe location.");
1269 goto error;
1270 }
1271
1272 res.er = lttng_event_rule_kernel_uprobe_create(userspace_probe_location);
1273 if (!res.er) {
1274 ERR("Failed to create userspace probe event rule.");
1275 goto error;
1276 }
1277
1278 event_rule_status =
1279 lttng_event_rule_kernel_uprobe_set_event_name(
1280 res.er, event_name);
1281 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1282 ERR("Failed to set user space probe event rule's name to '%s'.",
1283 event_name);
1284 goto error;
1285 }
1286
1287 break;
1288 }
1289 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL:
1290 {
1291 enum lttng_event_rule_status event_rule_status;
1292 enum lttng_event_rule_kernel_syscall_emission_site emission_site;
1293
1294 if (!parse_syscall_emission_site_from_type(
1295 event_rule_type_str, &emission_site)) {
1296 ERR("Failed to parse syscall type '%s'.", event_rule_type_str);
1297 goto error;
1298 }
1299
1300 res.er = lttng_event_rule_kernel_syscall_create(emission_site);
1301 if (!res.er) {
1302 ERR("Failed to create syscall event rule.");
1303 goto error;
1304 }
1305
1306 event_rule_status = lttng_event_rule_kernel_syscall_set_name_pattern(
1307 res.er, name);
1308 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1309 ERR("Failed to set syscall event rule's pattern to '%s'.",
1310 name);
1311 goto error;
1312 }
1313
1314 if (filter) {
1315 event_rule_status = lttng_event_rule_kernel_syscall_set_filter(
1316 res.er, filter);
1317 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1318 ERR("Failed to set syscall event rule's filter to '%s'.",
1319 filter);
1320 goto error;
1321 }
1322 }
1323
1324 break;
1325 }
1326 default:
1327 abort();
1328 goto error;
1329 }
1330
1331 goto end;
1332
1333 error:
1334 lttng_event_rule_destroy(res.er);
1335 res.er = NULL;
1336 lttng_dynamic_pointer_array_reset(&res.capture_descriptors);
1337
1338 end:
1339 if (parser_ctx) {
1340 filter_parser_ctx_free(parser_ctx);
1341 }
1342
1343 lttng_event_expr_destroy(event_expr);
1344 argpar_item_destroy(item);
1345 free(error);
1346 argpar_state_destroy(state);
1347 free(filter);
1348 free(name);
1349 lttng_dynamic_pointer_array_reset(&exclude_names);
1350 free(log_level_str);
1351 free(location);
1352 free(event_name);
1353 free(event_rule_type_str);
1354
1355 lttng_kernel_probe_location_destroy(kernel_probe_location);
1356 lttng_userspace_probe_location_destroy(userspace_probe_location);
1357 lttng_log_level_rule_destroy(log_level_rule);
1358 return res;
1359 }
1360
1361 static
1362 struct lttng_condition *handle_condition_event(int *argc, const char ***argv)
1363 {
1364 struct parse_event_rule_res res;
1365 struct lttng_condition *c;
1366 size_t i;
1367
1368 res = parse_event_rule(argc, argv);
1369 if (!res.er) {
1370 c = NULL;
1371 goto error;
1372 }
1373
1374 c = lttng_condition_event_rule_matches_create(res.er);
1375 lttng_event_rule_destroy(res.er);
1376 res.er = NULL;
1377 if (!c) {
1378 goto error;
1379 }
1380
1381 for (i = 0; i < lttng_dynamic_pointer_array_get_count(&res.capture_descriptors);
1382 i++) {
1383 enum lttng_condition_status status;
1384 struct lttng_event_expr **expr =
1385 lttng_dynamic_array_get_element(
1386 &res.capture_descriptors.array, i);
1387
1388 assert(expr);
1389 assert(*expr);
1390 status = lttng_condition_event_rule_matches_append_capture_descriptor(
1391 c, *expr);
1392 if (status != LTTNG_CONDITION_STATUS_OK) {
1393 if (status == LTTNG_CONDITION_STATUS_UNSUPPORTED) {
1394 ERR("The capture feature is unsupported by the event-rule condition type");
1395 }
1396
1397 goto error;
1398 }
1399
1400 /* Ownership of event expression moved to `c` */
1401 *expr = NULL;
1402 }
1403
1404 goto end;
1405
1406 error:
1407 lttng_condition_destroy(c);
1408 c = NULL;
1409
1410 end:
1411 lttng_dynamic_pointer_array_reset(&res.capture_descriptors);
1412 lttng_event_rule_destroy(res.er);
1413 return c;
1414 }
1415
1416 struct condition_descr {
1417 const char *name;
1418 struct lttng_condition *(*handler) (int *argc, const char ***argv);
1419 };
1420
1421 static const
1422 struct condition_descr condition_descrs[] = {
1423 { "event-rule-matches", handle_condition_event },
1424 };
1425
1426 static
1427 struct lttng_condition *parse_condition(const char *condition_name, int *argc,
1428 const char ***argv)
1429 {
1430 int i;
1431 struct lttng_condition *cond;
1432 const struct condition_descr *descr = NULL;
1433
1434 for (i = 0; i < ARRAY_SIZE(condition_descrs); i++) {
1435 if (strcmp(condition_name, condition_descrs[i].name) == 0) {
1436 descr = &condition_descrs[i];
1437 break;
1438 }
1439 }
1440
1441 if (!descr) {
1442 ERR("Unknown condition name '%s'", condition_name);
1443 goto error;
1444 }
1445
1446 cond = descr->handler(argc, argv);
1447 if (!cond) {
1448 /* The handler has already printed an error message. */
1449 goto error;
1450 }
1451
1452 goto end;
1453 error:
1454 cond = NULL;
1455 end:
1456 return cond;
1457 }
1458
1459 static struct lttng_rate_policy *parse_rate_policy(const char *policy_str)
1460 {
1461 int ret;
1462 size_t num_token = 0;
1463 struct lttng_dynamic_pointer_array tokens;
1464 struct lttng_rate_policy *policy = NULL;
1465 enum lttng_rate_policy_type policy_type;
1466 unsigned long long value;
1467 char *policy_type_str;
1468 char *policy_value_str;
1469
1470 assert(policy_str);
1471 lttng_dynamic_pointer_array_init(&tokens, NULL);
1472
1473 /* Rate policy fields are separated by ':'. */
1474 ret = strutils_split(policy_str, ':', 1, &tokens);
1475 if (ret == 0) {
1476 num_token = lttng_dynamic_pointer_array_get_count(&tokens);
1477 }
1478
1479 /*
1480 * Early sanity check that the number of parameter is exactly 2.
1481 * i.e : type:value
1482 */
1483 if (num_token != 2) {
1484 ERR("Rate policy format is invalid.");
1485 goto end;
1486 }
1487
1488 policy_type_str = lttng_dynamic_pointer_array_get_pointer(&tokens, 0);
1489 policy_value_str = lttng_dynamic_pointer_array_get_pointer(&tokens, 1);
1490
1491 /* Parse the type. */
1492 if (strcmp(policy_type_str, "once-after") == 0) {
1493 policy_type = LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N;
1494 } else if (strcmp(policy_type_str, "every") == 0) {
1495 policy_type = LTTNG_RATE_POLICY_TYPE_EVERY_N;
1496 } else {
1497 ERR("Rate policy type `%s` unknown.", policy_type_str);
1498 goto end;
1499 }
1500
1501 /* Parse the value. */
1502 if (utils_parse_unsigned_long_long(policy_value_str, &value) != 0) {
1503 ERR("Failed to parse rate policy value `%s` as an integer.",
1504 policy_value_str);
1505 goto end;
1506 }
1507
1508 if (value == 0) {
1509 ERR("Rate policy value `%s` must be > 0.", policy_value_str);
1510 goto end;
1511 }
1512
1513 switch (policy_type) {
1514 case LTTNG_RATE_POLICY_TYPE_EVERY_N:
1515 policy = lttng_rate_policy_every_n_create(value);
1516 break;
1517 case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N:
1518 policy = lttng_rate_policy_once_after_n_create(value);
1519 break;
1520 default:
1521 abort();
1522 }
1523
1524 if (policy == NULL) {
1525 ERR("Failed to create rate policy `%s`.", policy_str);
1526 }
1527
1528 end:
1529 lttng_dynamic_pointer_array_reset(&tokens);
1530 return policy;
1531 }
1532
1533 static const struct argpar_opt_descr notify_action_opt_descrs[] = {
1534 { OPT_RATE_POLICY, '\0', "rate-policy", true },
1535 ARGPAR_OPT_DESCR_SENTINEL
1536 };
1537
1538 static
1539 struct lttng_action *handle_action_notify(int *argc, const char ***argv)
1540 {
1541 struct lttng_action *action = NULL;
1542 struct argpar_state *state = NULL;
1543 struct argpar_item *item = NULL;
1544 char *error = NULL;
1545 struct lttng_rate_policy *policy = NULL;
1546
1547 state = argpar_state_create(*argc, *argv, notify_action_opt_descrs);
1548 if (!state) {
1549 ERR("Failed to allocate an argpar state.");
1550 goto error;
1551 }
1552
1553 while (true) {
1554 enum argpar_state_parse_next_status status;
1555
1556 ARGPAR_ITEM_DESTROY_AND_RESET(item);
1557 status = argpar_state_parse_next(state, &item, &error);
1558 if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR) {
1559 ERR("%s", error);
1560 goto error;
1561 } else if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT) {
1562 /* Just stop parsing here. */
1563 break;
1564 } else if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_END) {
1565 break;
1566 }
1567
1568 assert(status == ARGPAR_STATE_PARSE_NEXT_STATUS_OK);
1569
1570 if (item->type == ARGPAR_ITEM_TYPE_OPT) {
1571 const struct argpar_item_opt *item_opt =
1572 (const struct argpar_item_opt *) item;
1573
1574 switch (item_opt->descr->id) {
1575 case OPT_RATE_POLICY:
1576 {
1577 policy = parse_rate_policy(item_opt->arg);
1578 if (!policy) {
1579 goto error;
1580 }
1581 break;
1582 }
1583 default:
1584 abort();
1585 }
1586 } else {
1587 const struct argpar_item_non_opt *item_non_opt;
1588
1589 assert(item->type == ARGPAR_ITEM_TYPE_NON_OPT);
1590
1591 item_non_opt = (const struct argpar_item_non_opt *) item;
1592
1593 switch (item_non_opt->non_opt_index) {
1594 default:
1595 ERR("Unexpected argument `%s`.",
1596 item_non_opt->arg);
1597 goto error;
1598 }
1599 }
1600 }
1601
1602 *argc -= argpar_state_get_ingested_orig_args(state);
1603 *argv += argpar_state_get_ingested_orig_args(state);
1604
1605 action = lttng_action_notify_create();
1606 if (!action) {
1607 ERR("Failed to create notify action");
1608 goto error;
1609 }
1610
1611 if (policy) {
1612 enum lttng_action_status status;
1613 status = lttng_action_notify_set_rate_policy(action, policy);
1614 if (status != LTTNG_ACTION_STATUS_OK) {
1615 ERR("Failed to set rate policy");
1616 goto error;
1617 }
1618 }
1619
1620 goto end;
1621
1622 error:
1623 lttng_action_destroy(action);
1624 action = NULL;
1625 end:
1626 free(error);
1627 lttng_rate_policy_destroy(policy);
1628 argpar_state_destroy(state);
1629 argpar_item_destroy(item);
1630 return action;
1631 }
1632
1633 /*
1634 * Generic handler for a kind of action that takes a session name and an
1635 * optional rate policy.
1636 */
1637
1638 static struct lttng_action *handle_action_simple_session_with_policy(int *argc,
1639 const char ***argv,
1640 struct lttng_action *(*create_action_cb)(void),
1641 enum lttng_action_status (*set_session_name_cb)(
1642 struct lttng_action *, const char *),
1643 enum lttng_action_status (*set_rate_policy_cb)(
1644 struct lttng_action *,
1645 const struct lttng_rate_policy *),
1646 const char *action_name)
1647 {
1648 struct lttng_action *action = NULL;
1649 struct argpar_state *state = NULL;
1650 struct argpar_item *item = NULL;
1651 const char *session_name_arg = NULL;
1652 char *error = NULL;
1653 enum lttng_action_status action_status;
1654 struct lttng_rate_policy *policy = NULL;
1655
1656 assert(set_session_name_cb);
1657 assert(set_rate_policy_cb);
1658
1659 const struct argpar_opt_descr rate_policy_opt_descrs[] = {
1660 { OPT_RATE_POLICY, '\0', "rate-policy", true },
1661 ARGPAR_OPT_DESCR_SENTINEL
1662 };
1663
1664 state = argpar_state_create(*argc, *argv, rate_policy_opt_descrs);
1665 if (!state) {
1666 ERR("Failed to allocate an argpar state.");
1667 goto error;
1668 }
1669
1670 while (true) {
1671 enum argpar_state_parse_next_status status;
1672
1673 ARGPAR_ITEM_DESTROY_AND_RESET(item);
1674 status = argpar_state_parse_next(state, &item, &error);
1675 if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR) {
1676 ERR("%s", error);
1677 goto error;
1678 } else if (status ==
1679 ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT) {
1680 /* Just stop parsing here. */
1681 break;
1682 } else if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_END) {
1683 break;
1684 }
1685
1686 assert(status == ARGPAR_STATE_PARSE_NEXT_STATUS_OK);
1687 if (item->type == ARGPAR_ITEM_TYPE_OPT) {
1688 const struct argpar_item_opt *item_opt =
1689 (const struct argpar_item_opt *) item;
1690
1691 switch (item_opt->descr->id) {
1692 case OPT_RATE_POLICY:
1693 {
1694 policy = parse_rate_policy(item_opt->arg);
1695 if (!policy) {
1696 goto error;
1697 }
1698 break;
1699 }
1700 default:
1701 abort();
1702 }
1703 } else {
1704 const struct argpar_item_non_opt *item_non_opt;
1705 item_non_opt = (const struct argpar_item_non_opt *) item;
1706
1707 switch (item_non_opt->non_opt_index) {
1708 case 0:
1709 session_name_arg = item_non_opt->arg;
1710 break;
1711 default:
1712 ERR("Unexpected argument `%s`.",
1713 item_non_opt->arg);
1714 goto error;
1715 }
1716 }
1717 }
1718
1719 *argc -= argpar_state_get_ingested_orig_args(state);
1720 *argv += argpar_state_get_ingested_orig_args(state);
1721
1722 if (!session_name_arg) {
1723 ERR("Missing session name.");
1724 goto error;
1725 }
1726
1727 action = create_action_cb();
1728 if (!action) {
1729 ERR("Failed to allocate %s session action.", action_name);
1730 goto error;
1731 }
1732
1733 action_status = set_session_name_cb(action, session_name_arg);
1734 if (action_status != LTTNG_ACTION_STATUS_OK) {
1735 ERR("Failed to set action %s session's session name to '%s'.",
1736 action_name, session_name_arg);
1737 goto error;
1738 }
1739
1740 if (policy) {
1741 action_status = set_rate_policy_cb(action, policy);
1742 if (action_status != LTTNG_ACTION_STATUS_OK) {
1743 ERR("Failed to set rate policy");
1744 goto error;
1745 }
1746 }
1747
1748 goto end;
1749
1750 error:
1751 lttng_action_destroy(action);
1752 action = NULL;
1753 argpar_item_destroy(item);
1754 end:
1755 lttng_rate_policy_destroy(policy);
1756 free(error);
1757 argpar_state_destroy(state);
1758 return action;
1759 }
1760
1761 static
1762 struct lttng_action *handle_action_start_session(int *argc,
1763 const char ***argv)
1764 {
1765 return handle_action_simple_session_with_policy(argc, argv,
1766 lttng_action_start_session_create,
1767 lttng_action_start_session_set_session_name,
1768 lttng_action_start_session_set_rate_policy, "start");
1769 }
1770
1771 static
1772 struct lttng_action *handle_action_stop_session(int *argc,
1773 const char ***argv)
1774 {
1775 return handle_action_simple_session_with_policy(argc, argv,
1776 lttng_action_stop_session_create,
1777 lttng_action_stop_session_set_session_name,
1778 lttng_action_stop_session_set_rate_policy, "stop");
1779 }
1780
1781 static
1782 struct lttng_action *handle_action_rotate_session(int *argc,
1783 const char ***argv)
1784 {
1785 return handle_action_simple_session_with_policy(argc, argv,
1786 lttng_action_rotate_session_create,
1787 lttng_action_rotate_session_set_session_name,
1788 lttng_action_rotate_session_set_rate_policy,
1789 "rotate");
1790 }
1791
1792 static const struct argpar_opt_descr snapshot_action_opt_descrs[] = {
1793 { OPT_NAME, 'n', "name", true },
1794 { OPT_MAX_SIZE, 'm', "max-size", true },
1795 { OPT_CTRL_URL, '\0', "ctrl-url", true },
1796 { OPT_DATA_URL, '\0', "data-url", true },
1797 { OPT_URL, '\0', "url", true },
1798 { OPT_PATH, '\0', "path", true },
1799 { OPT_RATE_POLICY, '\0', "rate-policy", true },
1800 ARGPAR_OPT_DESCR_SENTINEL
1801 };
1802
1803 static
1804 struct lttng_action *handle_action_snapshot_session(int *argc,
1805 const char ***argv)
1806 {
1807 struct lttng_action *action = NULL;
1808 struct argpar_state *state = NULL;
1809 struct argpar_item *item = NULL;
1810 const char *session_name_arg = NULL;
1811 char *snapshot_name_arg = NULL;
1812 char *ctrl_url_arg = NULL;
1813 char *data_url_arg = NULL;
1814 char *max_size_arg = NULL;
1815 char *url_arg = NULL;
1816 char *path_arg = NULL;
1817 char *error = NULL;
1818 enum lttng_action_status action_status;
1819 struct lttng_snapshot_output *snapshot_output = NULL;
1820 struct lttng_rate_policy *policy = NULL;
1821 int ret;
1822 unsigned int locations_specified = 0;
1823
1824 state = argpar_state_create(*argc, *argv, snapshot_action_opt_descrs);
1825 if (!state) {
1826 ERR("Failed to allocate an argpar state.");
1827 goto error;
1828 }
1829
1830 while (true) {
1831 enum argpar_state_parse_next_status status;
1832
1833 ARGPAR_ITEM_DESTROY_AND_RESET(item);
1834 status = argpar_state_parse_next(state, &item, &error);
1835 if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR) {
1836 ERR("%s", error);
1837 goto error;
1838 } else if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT) {
1839 /* Just stop parsing here. */
1840 break;
1841 } else if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_END) {
1842 break;
1843 }
1844
1845 assert(status == ARGPAR_STATE_PARSE_NEXT_STATUS_OK);
1846
1847 if (item->type == ARGPAR_ITEM_TYPE_OPT) {
1848 const struct argpar_item_opt *item_opt =
1849 (const struct argpar_item_opt *) item;
1850
1851 switch (item_opt->descr->id) {
1852 case OPT_NAME:
1853 if (!assign_string(&snapshot_name_arg, item_opt->arg, "--name/-n")) {
1854 goto error;
1855 }
1856
1857 break;
1858 case OPT_MAX_SIZE:
1859 if (!assign_string(&max_size_arg, item_opt->arg, "--max-size/-m")) {
1860 goto error;
1861 }
1862
1863 break;
1864 case OPT_CTRL_URL:
1865 if (!assign_string(&ctrl_url_arg, item_opt->arg, "--ctrl-url")) {
1866 goto error;
1867 }
1868
1869 break;
1870 case OPT_DATA_URL:
1871 if (!assign_string(&data_url_arg, item_opt->arg, "--data-url")) {
1872 goto error;
1873 }
1874
1875 break;
1876 case OPT_URL:
1877 if (!assign_string(&url_arg, item_opt->arg, "--url")) {
1878 goto error;
1879 }
1880
1881 break;
1882 case OPT_PATH:
1883 if (!assign_string(&path_arg, item_opt->arg, "--path")) {
1884 goto error;
1885 }
1886
1887 break;
1888 case OPT_RATE_POLICY:
1889 {
1890 policy = parse_rate_policy(item_opt->arg);
1891 if (!policy) {
1892 goto error;
1893 }
1894 break;
1895 }
1896 default:
1897 abort();
1898 }
1899 } else {
1900 const struct argpar_item_non_opt *item_non_opt;
1901
1902 assert(item->type == ARGPAR_ITEM_TYPE_NON_OPT);
1903
1904 item_non_opt = (const struct argpar_item_non_opt *) item;
1905
1906 switch (item_non_opt->non_opt_index) {
1907 case 0:
1908 session_name_arg = item_non_opt->arg;
1909 break;
1910 default:
1911 ERR("Unexpected argument `%s`.",
1912 item_non_opt->arg);
1913 goto error;
1914 }
1915 }
1916 }
1917
1918 *argc -= argpar_state_get_ingested_orig_args(state);
1919 *argv += argpar_state_get_ingested_orig_args(state);
1920
1921 if (!session_name_arg) {
1922 ERR("Missing session name.");
1923 goto error;
1924 }
1925
1926 /* --ctrl-url and --data-url must come in pair. */
1927 if (ctrl_url_arg && !data_url_arg) {
1928 ERR("--ctrl-url is specified, but --data-url is missing.");
1929 goto error;
1930 }
1931
1932 if (!ctrl_url_arg && data_url_arg) {
1933 ERR("--data-url is specified, but --ctrl-url is missing.");
1934 goto error;
1935 }
1936
1937 locations_specified += !!(ctrl_url_arg || data_url_arg);
1938 locations_specified += !!url_arg;
1939 locations_specified += !!path_arg;
1940
1941 /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */
1942 if (locations_specified > 1) {
1943 ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together.");
1944 goto error;
1945 }
1946
1947 /*
1948 * Did the user specify an option that implies using a
1949 * custom/unregistered output?
1950 */
1951 if (url_arg || ctrl_url_arg || path_arg) {
1952 snapshot_output = lttng_snapshot_output_create();
1953 if (!snapshot_output) {
1954 ERR("Failed to allocate a snapshot output.");
1955 goto error;
1956 }
1957 }
1958
1959 action = lttng_action_snapshot_session_create();
1960 if (!action) {
1961 ERR("Failed to allocate snapshot session action.");
1962 goto error;
1963 }
1964
1965 action_status = lttng_action_snapshot_session_set_session_name(
1966 action, session_name_arg);
1967 if (action_status != LTTNG_ACTION_STATUS_OK) {
1968 ERR("Failed to set action snapshot session's session name to '%s'.",
1969 session_name_arg);
1970 goto error;
1971 }
1972
1973 if (snapshot_name_arg) {
1974 if (!snapshot_output) {
1975 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1976 goto error;
1977 }
1978
1979 ret = lttng_snapshot_output_set_name(
1980 snapshot_name_arg, snapshot_output);
1981 if (ret != 0) {
1982 ERR("Failed to set name of snapshot output.");
1983 goto error;
1984 }
1985 }
1986
1987 if (max_size_arg) {
1988 uint64_t max_size;
1989
1990 if (!snapshot_output) {
1991 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1992 goto error;
1993 }
1994
1995 ret = utils_parse_size_suffix(max_size_arg, &max_size);
1996 if (ret != 0) {
1997 ERR("Failed to parse `%s` as a size.", max_size_arg);
1998 goto error;
1999 }
2000
2001 ret = lttng_snapshot_output_set_size(max_size, snapshot_output);
2002 if (ret != 0) {
2003 ERR("Failed to set snapshot output's max size to %" PRIu64 " bytes.",
2004 max_size);
2005 goto error;
2006 }
2007 }
2008
2009 if (url_arg) {
2010 int num_uris;
2011 struct lttng_uri *uris;
2012
2013 if (!strstr(url_arg, "://")) {
2014 ERR("Failed to parse '%s' as an URL.", url_arg);
2015 goto error;
2016 }
2017
2018 num_uris = uri_parse_str_urls(url_arg, NULL, &uris);
2019 if (num_uris < 1) {
2020 ERR("Failed to parse '%s' as an URL.", url_arg);
2021 goto error;
2022 }
2023
2024 if (uris[0].dtype == LTTNG_DST_PATH) {
2025 ret = lttng_snapshot_output_set_local_path(
2026 uris[0].dst.path, snapshot_output);
2027 free(uris);
2028 if (ret != 0) {
2029 ERR("Failed to assign '%s' as a local destination.",
2030 url_arg);
2031 goto error;
2032 }
2033 } else {
2034 ret = lttng_snapshot_output_set_network_url(
2035 url_arg, snapshot_output);
2036 free(uris);
2037 if (ret != 0) {
2038 ERR("Failed to assign '%s' as a network URL.",
2039 url_arg);
2040 goto error;
2041 }
2042 }
2043 }
2044
2045 if (path_arg) {
2046 ret = lttng_snapshot_output_set_local_path(
2047 path_arg, snapshot_output);
2048 if (ret != 0) {
2049 ERR("Failed to parse '%s' as a local path.", path_arg);
2050 goto error;
2051 }
2052 }
2053
2054 if (ctrl_url_arg) {
2055 /*
2056 * Two argument form, network output with separate control and
2057 * data URLs.
2058 */
2059 ret = lttng_snapshot_output_set_network_urls(
2060 ctrl_url_arg, data_url_arg, snapshot_output);
2061 if (ret != 0) {
2062 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
2063 ctrl_url_arg, data_url_arg);
2064 goto error;
2065 }
2066 }
2067
2068 if (snapshot_output) {
2069 action_status = lttng_action_snapshot_session_set_output(
2070 action, snapshot_output);
2071 if (action_status != LTTNG_ACTION_STATUS_OK) {
2072 ERR("Failed to set snapshot session action's output.");
2073 goto error;
2074 }
2075
2076 /* Ownership of `snapshot_output` has been transferred to the action. */
2077 snapshot_output = NULL;
2078 }
2079
2080 if (policy) {
2081 enum lttng_action_status status;
2082 status = lttng_action_snapshot_session_set_rate_policy(
2083 action, policy);
2084 if (status != LTTNG_ACTION_STATUS_OK) {
2085 ERR("Failed to set rate policy");
2086 goto error;
2087 }
2088 }
2089
2090 goto end;
2091
2092 error:
2093 lttng_action_destroy(action);
2094 action = NULL;
2095 free(error);
2096 end:
2097 free(snapshot_name_arg);
2098 free(path_arg);
2099 free(url_arg);
2100 free(ctrl_url_arg);
2101 free(data_url_arg);
2102 free(snapshot_output);
2103 free(max_size_arg);
2104 lttng_rate_policy_destroy(policy);
2105 argpar_state_destroy(state);
2106 argpar_item_destroy(item);
2107 return action;
2108 }
2109
2110 struct action_descr {
2111 const char *name;
2112 struct lttng_action *(*handler) (int *argc, const char ***argv);
2113 };
2114
2115 static const
2116 struct action_descr action_descrs[] = {
2117 { "notify", handle_action_notify },
2118 { "start-session", handle_action_start_session },
2119 { "stop-session", handle_action_stop_session },
2120 { "rotate-session", handle_action_rotate_session },
2121 { "snapshot-session", handle_action_snapshot_session },
2122 };
2123
2124 static
2125 struct lttng_action *parse_action(const char *action_name, int *argc, const char ***argv)
2126 {
2127 int i;
2128 struct lttng_action *action;
2129 const struct action_descr *descr = NULL;
2130
2131 for (i = 0; i < ARRAY_SIZE(action_descrs); i++) {
2132 if (strcmp(action_name, action_descrs[i].name) == 0) {
2133 descr = &action_descrs[i];
2134 break;
2135 }
2136 }
2137
2138 if (!descr) {
2139 ERR("Unknown action name: %s", action_name);
2140 goto error;
2141 }
2142
2143 action = descr->handler(argc, argv);
2144 if (!action) {
2145 /* The handler has already printed an error message. */
2146 goto error;
2147 }
2148
2149 goto end;
2150 error:
2151 action = NULL;
2152 end:
2153 return action;
2154 }
2155
2156 static const
2157 struct argpar_opt_descr add_trigger_options[] = {
2158 { OPT_HELP, 'h', "help", false },
2159 { OPT_LIST_OPTIONS, '\0', "list-options", false },
2160 { OPT_CONDITION, '\0', "condition", true },
2161 { OPT_ACTION, '\0', "action", true },
2162 { OPT_NAME, '\0', "name", true },
2163 { OPT_OWNER_UID, '\0', "owner-uid", true },
2164 ARGPAR_OPT_DESCR_SENTINEL,
2165 };
2166
2167 static
2168 void lttng_actions_destructor(void *p)
2169 {
2170 struct lttng_action *action = p;
2171
2172 lttng_action_destroy(action);
2173 }
2174
2175 int cmd_add_trigger(int argc, const char **argv)
2176 {
2177 int ret;
2178 int my_argc = argc - 1;
2179 const char **my_argv = argv + 1;
2180 struct lttng_condition *condition = NULL;
2181 struct lttng_dynamic_pointer_array actions;
2182 struct argpar_state *argpar_state = NULL;
2183 struct argpar_item *argpar_item = NULL;
2184 struct lttng_action *action_list = NULL;
2185 struct lttng_action *action = NULL;
2186 struct lttng_trigger *trigger = NULL;
2187 char *error = NULL;
2188 char *name = NULL;
2189 int i;
2190 char *owner_uid = NULL;
2191 enum lttng_error_code ret_code;
2192
2193 lttng_dynamic_pointer_array_init(&actions, lttng_actions_destructor);
2194
2195 while (true) {
2196 enum argpar_state_parse_next_status status;
2197 const struct argpar_item_opt *item_opt;
2198 int ingested_args;
2199
2200 argpar_state_destroy(argpar_state);
2201 argpar_state = argpar_state_create(my_argc, my_argv,
2202 add_trigger_options);
2203 if (!argpar_state) {
2204 ERR("Failed to create argpar state.");
2205 goto error;
2206 }
2207
2208 ARGPAR_ITEM_DESTROY_AND_RESET(argpar_item);
2209 status = argpar_state_parse_next(argpar_state, &argpar_item, &error);
2210 if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR) {
2211 ERR("%s", error);
2212 goto error;
2213 } else if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_ERROR_UNKNOWN_OPT) {
2214 ERR("%s", error);
2215 goto error;
2216 } else if (status == ARGPAR_STATE_PARSE_NEXT_STATUS_END) {
2217 break;
2218 }
2219
2220 assert(status == ARGPAR_STATE_PARSE_NEXT_STATUS_OK);
2221
2222 if (argpar_item->type == ARGPAR_ITEM_TYPE_NON_OPT) {
2223 const struct argpar_item_non_opt *item_non_opt =
2224 (const struct argpar_item_non_opt *)
2225 argpar_item;
2226
2227 ERR("Unexpected argument `%s`.", item_non_opt->arg);
2228 goto error;
2229 }
2230
2231 item_opt = (const struct argpar_item_opt *) argpar_item;
2232
2233 ingested_args = argpar_state_get_ingested_orig_args(
2234 argpar_state);
2235
2236 my_argc -= ingested_args;
2237 my_argv += ingested_args;
2238
2239 switch (item_opt->descr->id) {
2240 case OPT_HELP:
2241 SHOW_HELP();
2242 ret = 0;
2243 goto end;
2244 case OPT_LIST_OPTIONS:
2245 list_cmd_options_argpar(stdout, add_trigger_options);
2246 ret = 0;
2247 goto end;
2248 case OPT_CONDITION:
2249 {
2250 if (condition) {
2251 ERR("A --condition was already given.");
2252 goto error;
2253 }
2254
2255 condition = parse_condition(item_opt->arg, &my_argc, &my_argv);
2256 if (!condition) {
2257 /*
2258 * An error message was already printed by
2259 * parse_condition.
2260 */
2261 goto error;
2262 }
2263
2264 break;
2265 }
2266 case OPT_ACTION:
2267 {
2268 action = parse_action(item_opt->arg, &my_argc, &my_argv);
2269 if (!action) {
2270 /*
2271 * An error message was already printed by
2272 * parse_condition.
2273 */
2274 goto error;
2275 }
2276
2277 ret = lttng_dynamic_pointer_array_add_pointer(
2278 &actions, action);
2279 if (ret) {
2280 ERR("Failed to add pointer to pointer array.");
2281 goto error;
2282 }
2283
2284 /* Ownership of the action was transferred to the group. */
2285 action = NULL;
2286
2287 break;
2288 }
2289 case OPT_NAME:
2290 {
2291 if (!assign_string(&name, item_opt->arg, "--name")) {
2292 goto error;
2293 }
2294
2295 break;
2296 }
2297 case OPT_OWNER_UID:
2298 {
2299 if (!assign_string(&owner_uid, item_opt->arg,
2300 "--owner-uid")) {
2301 goto error;
2302 }
2303
2304 break;
2305 }
2306 default:
2307 abort();
2308 }
2309 }
2310
2311 if (!condition) {
2312 ERR("Missing --condition.");
2313 goto error;
2314 }
2315
2316 if (lttng_dynamic_pointer_array_get_count(&actions) == 0) {
2317 ERR("Need at least one --action.");
2318 goto error;
2319 }
2320
2321 action_list = lttng_action_list_create();
2322 if (!action_list) {
2323 goto error;
2324 }
2325
2326 for (i = 0; i < lttng_dynamic_pointer_array_get_count(&actions); i++) {
2327 enum lttng_action_status status;
2328
2329 action = lttng_dynamic_pointer_array_steal_pointer(&actions, i);
2330
2331 status = lttng_action_list_add_action(action_list, action);
2332 if (status != LTTNG_ACTION_STATUS_OK) {
2333 goto error;
2334 }
2335
2336 /*
2337 * The `lttng_action_list_add_action()` takes a reference to
2338 * the action. We can destroy ours.
2339 */
2340 lttng_action_destroy(action);
2341 action = NULL;
2342 }
2343
2344 trigger = lttng_trigger_create(condition, action_list);
2345 if (!trigger) {
2346 goto error;
2347 }
2348
2349 if (owner_uid) {
2350 enum lttng_trigger_status trigger_status;
2351 char *end;
2352 long long uid;
2353
2354 errno = 0;
2355 uid = strtol(owner_uid, &end, 10);
2356 if (end == owner_uid || *end != '\0' || errno != 0) {
2357 ERR("Failed to parse `%s` as a user id.", owner_uid);
2358 goto error;
2359 }
2360
2361 trigger_status = lttng_trigger_set_owner_uid(trigger, uid);
2362 if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
2363 ERR("Failed to set trigger's user identity.");
2364 goto error;
2365 }
2366 }
2367
2368 if (name) {
2369 ret_code = lttng_register_trigger_with_name(trigger, name);
2370 } else {
2371 ret_code = lttng_register_trigger_with_automatic_name(trigger);
2372 }
2373
2374 if (ret_code != LTTNG_OK) {
2375 ERR("Failed to register trigger: %s.",
2376 lttng_strerror(-ret_code));
2377 goto error;
2378 }
2379
2380 MSG("Trigger registered successfully.");
2381 ret = 0;
2382
2383 goto end;
2384
2385 error:
2386 ret = 1;
2387
2388 end:
2389 argpar_state_destroy(argpar_state);
2390 argpar_item_destroy(argpar_item);
2391 lttng_dynamic_pointer_array_reset(&actions);
2392 lttng_condition_destroy(condition);
2393 lttng_action_destroy(action_list);
2394 lttng_action_destroy(action);
2395 lttng_trigger_destroy(trigger);
2396 free(error);
2397 free(name);
2398 free(owner_uid);
2399 return ret;
2400 }
This page took 0.117179 seconds and 5 git commands to generate.