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