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