Fix: sessiond: ODR violation results in memory corruption
[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 12
c9e313bc
SM
13#include "../command.hpp"
14#include "../loglevel.hpp"
15#include "../uprobe.hpp"
4624dad0
SM
16
17#include "common/argpar/argpar.h"
c9e313bc
SM
18#include "common/argpar-utils/argpar-utils.hpp"
19#include "common/dynamic-array.hpp"
20#include "common/mi-lttng.hpp"
21#include "common/string-utils/string-utils.hpp"
22#include "common/utils.hpp"
23#include <lttng/domain-internal.hpp>
4624dad0 24/* For lttng_event_rule_type_str(). */
c9e313bc 25#include <lttng/event-rule/event-rule-internal.hpp>
4624dad0 26#include <lttng/lttng.h>
c9e313bc
SM
27#include "common/filter/filter-ast.hpp"
28#include "common/filter/filter-ir.hpp"
29#include "common/dynamic-array.hpp"
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 }
44e63aa7 358 offset = strtoull(s_hex, NULL, 0);
4624dad0
SM
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
44e63aa7 404 address = strtoull(s_hex, NULL, 0);
4624dad0 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
f1494934 643namespace {
20a01d15
SM
644struct parse_event_rule_res {
645 /* Owned by this. */
646 struct lttng_event_rule *er;
647
648 /* Array of `struct lttng_event_expr *` */
649 struct lttng_dynamic_pointer_array capture_descriptors;
650};
f1494934 651} /* namespace */
20a01d15
SM
652
653static
35c4b2b3
SM
654struct parse_event_rule_res parse_event_rule(int *argc, const char ***argv,
655 int argc_offset)
4624dad0 656{
4624dad0
SM
657 enum lttng_event_rule_type event_rule_type =
658 LTTNG_EVENT_RULE_TYPE_UNKNOWN;
d50d200a
SM
659 struct argpar_iter *argpar_iter = NULL;
660 const struct argpar_item *argpar_item = NULL;
4624dad0
SM
661 int consumed_args = -1;
662 struct lttng_kernel_probe_location *kernel_probe_location = NULL;
663 struct lttng_userspace_probe_location *userspace_probe_location = NULL;
1c9a0b0e 664 struct parse_event_rule_res res = {};
20a01d15
SM
665 struct lttng_event_expr *event_expr = NULL;
666 struct filter_parser_ctx *parser_ctx = NULL;
85b05318 667 struct lttng_log_level_rule *log_level_rule = NULL;
4624dad0 668
57739a6b
JR
669 /* Event rule type option */
670 char *event_rule_type_str = NULL;
671
50ad0862
SM
672 /* Tracepoint and syscall options. */
673 char *name = NULL;
b03a81fb
SM
674 /* Array of strings. */
675 struct lttng_dynamic_pointer_array exclude_names;
4624dad0 676
24de704e
SM
677 /* For userspace / kernel probe and function. */
678 char *location = NULL;
679 char *event_name = NULL;
4624dad0
SM
680
681 /* Filter. */
682 char *filter = NULL;
683
4624dad0 684 /* Log level. */
949f049b 685 char *log_level_str = NULL;
4624dad0 686
20a01d15
SM
687 lttng_dynamic_pointer_array_init(&res.capture_descriptors,
688 destroy_event_expr);
b03a81fb
SM
689
690 lttng_dynamic_pointer_array_init(&exclude_names, free);
691
d50d200a
SM
692 argpar_iter = argpar_iter_create(*argc, *argv, event_rule_opt_descrs);
693 if (!argpar_iter) {
694 ERR("Failed to allocate an argpar iter.");
4624dad0
SM
695 goto error;
696 }
697
698 while (true) {
d50d200a 699 enum parse_next_item_status status;
4624dad0 700
35c4b2b3 701 status = parse_next_item(argpar_iter, &argpar_item,
ef9ff9cb
SM
702 argc_offset, *argv, false, NULL, NULL);
703 if (status == PARSE_NEXT_ITEM_STATUS_ERROR ||
704 status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) {
4624dad0 705 goto error;
d50d200a 706 } else if (status == PARSE_NEXT_ITEM_STATUS_END) {
4624dad0
SM
707 break;
708 }
709
d50d200a 710 LTTNG_ASSERT(status == PARSE_NEXT_ITEM_STATUS_OK);
4624dad0 711
d50d200a
SM
712 if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_OPT) {
713 const struct argpar_opt_descr *descr =
714 argpar_item_opt_descr(argpar_item);
715 const char *arg = argpar_item_opt_arg(argpar_item);
4624dad0 716
d50d200a 717 switch (descr->id) {
24de704e 718 case OPT_TYPE:
d50d200a 719 if (!assign_event_rule_type(&event_rule_type, arg)) {
4624dad0
SM
720 goto error;
721 }
722
57739a6b 723 /* Save the string for later use. */
d50d200a
SM
724 if (!assign_string(&event_rule_type_str, arg,
725 "--type/-t")) {
57739a6b
JR
726 goto error;
727 }
728
4624dad0 729 break;
24de704e 730 case OPT_LOCATION:
d50d200a 731 if (!assign_string(&location, arg,
24de704e 732 "--location/-L")) {
50ad0862
SM
733 goto error;
734 }
735
736 break;
737 case OPT_EVENT_NAME:
d50d200a
SM
738 if (!assign_string(&event_name, arg,
739 "--event-name/-E")) {
50ad0862 740 goto error;
4624dad0
SM
741 }
742
4624dad0
SM
743 break;
744 case OPT_FILTER:
d50d200a
SM
745 if (!assign_string(&filter, arg,
746 "--filter/-f")) {
4624dad0
SM
747 goto error;
748 }
749
750 break;
50ad0862 751 case OPT_NAME:
d50d200a
SM
752 if (!assign_string(&name, arg,
753 "--name/-n")) {
50ad0862
SM
754 goto error;
755 }
756
757 break;
b03a81fb
SM
758 case OPT_EXCLUDE_NAME:
759 {
760 int ret;
761
762 ret = lttng_dynamic_pointer_array_add_pointer(
763 &exclude_names,
d50d200a 764 strdup(arg));
b03a81fb
SM
765 if (ret != 0) {
766 ERR("Failed to add pointer to dynamic pointer array.");
4624dad0
SM
767 goto error;
768 }
769
770 break;
b03a81fb 771 }
949f049b 772 case OPT_LOG_LEVEL:
d50d200a
SM
773 if (!assign_string(&log_level_str, arg,
774 "--log-level/-l")) {
4624dad0
SM
775 goto error;
776 }
777
4624dad0 778 break;
20a01d15
SM
779 case OPT_CAPTURE:
780 {
781 int ret;
20a01d15
SM
782
783 ret = filter_parser_ctx_create_from_filter_expression(
d50d200a 784 arg, &parser_ctx);
20a01d15 785 if (ret) {
d50d200a 786 ERR("Failed to parse capture expression `%s`.", arg);
20a01d15
SM
787 goto error;
788 }
789
790 event_expr = ir_op_root_to_event_expr(
d50d200a 791 parser_ctx->ir_root, arg);
20a01d15
SM
792 if (!event_expr) {
793 /*
794 * ir_op_root_to_event_expr has printed
795 * an error message.
796 */
797 goto error;
798 }
799
800 ret = lttng_dynamic_pointer_array_add_pointer(
801 &res.capture_descriptors,
802 event_expr);
803 if (ret) {
804 goto error;
805 }
806
807 /*
808 * The ownership of event expression was
809 * transferred to the dynamic array.
810 */
811 event_expr = NULL;
812
813 break;
814 }
4624dad0
SM
815 default:
816 abort();
817 }
818 } else {
d50d200a 819 const char *arg = argpar_item_non_opt_arg(argpar_item);
4624dad0 820
50ad0862 821 /* Don't accept non-option arguments. */
d50d200a 822 ERR("Unexpected argument '%s'", arg);
50ad0862 823 goto error;
4624dad0
SM
824 }
825 }
826
827 if (event_rule_type == LTTNG_EVENT_RULE_TYPE_UNKNOWN) {
0b621ec1
JG
828 ERR("Event rule requires a --type.");
829 goto error;
4624dad0
SM
830 }
831
832 /*
695f7044
JR
833 * Option --name is applicable to event rules of type kernel, user, jul,
834 * log4j,python and syscall. If --name is omitted, it is implicitly
835 * "*".
4624dad0 836 */
50ad0862 837 switch (event_rule_type) {
695f7044
JR
838 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
839 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT:
840 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
841 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
842 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
4f7da553 843 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL:
50ad0862
SM
844 if (!name) {
845 name = strdup("*");
4624dad0 846 }
50ad0862 847 break;
4624dad0 848
50ad0862
SM
849 default:
850 if (name) {
851 ERR("Can't use --name with %s event rules.",
852 lttng_event_rule_type_str(
853 event_rule_type));
4624dad0
SM
854 goto error;
855 }
856
b03a81fb
SM
857 if (lttng_dynamic_pointer_array_get_count(&exclude_names) > 0) {
858 ERR("Can't use --exclude-name/-x with %s event rules.",
50ad0862
SM
859 lttng_event_rule_type_str(
860 event_rule_type));
861 goto error;
862 }
4624dad0
SM
863 }
864
865 /*
24de704e
SM
866 * Option --location is only applicable to (and mandatory for) event
867 * rules of type {k,u}probe and function.
868 *
50ad0862 869 * Option --event-name is only applicable to event rules of type probe.
24de704e 870 * If omitted, it defaults to the location.
4624dad0 871 */
50ad0862 872 switch (event_rule_type) {
85522de5 873 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE:
46fd07ac 874 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE:
24de704e
SM
875 if (!location) {
876 ERR("Event rule of type %s requires a --location.",
877 lttng_event_rule_type_str(event_rule_type));
878 goto error;
879 }
880
881 if (!event_name) {
882 event_name = strdup(location);
50ad0862 883 }
4624dad0 884
50ad0862
SM
885 break;
886
887 default:
24de704e
SM
888 if (location) {
889 ERR("Can't use --location with %s event rules.",
890 lttng_event_rule_type_str(event_rule_type));
891 goto error;
892 }
893
894 if (event_name) {
50ad0862
SM
895 ERR("Can't use --event-name with %s event rules.",
896 lttng_event_rule_type_str(
897 event_rule_type));
898 goto error;
899 }
4624dad0
SM
900 }
901
902 /*
903 * Update *argc and *argv so our caller can keep parsing what follows.
904 */
d50d200a 905 consumed_args = argpar_iter_ingested_orig_args(argpar_iter);
a0377dfe 906 LTTNG_ASSERT(consumed_args >= 0);
4624dad0
SM
907 *argc -= consumed_args;
908 *argv += consumed_args;
909
4624dad0
SM
910 /*
911 * Adding a filter to a probe, function or userspace-probe would be
912 * denied by the kernel tracer as it's not supported at the moment. We
913 * do an early check here to warn the user.
914 */
695f7044 915 if (filter) {
4624dad0 916 switch (event_rule_type) {
695f7044
JR
917 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
918 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT:
919 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
920 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
921 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
4f7da553 922 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL:
4624dad0
SM
923 break;
924 default:
925 ERR("Filter expressions are not supported for %s event rules.",
926 lttng_event_rule_type_str(event_rule_type));
927 goto error;
928 }
929 }
930
695f7044
JR
931 /*
932 * If --exclude-name/-x was passed, split it into an exclusion list.
933 * Exclusions are only supported by
934 * LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT for now.
935 */
b03a81fb 936 if (lttng_dynamic_pointer_array_get_count(&exclude_names) > 0) {
695f7044 937 if (event_rule_type != LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT) {
4624dad0 938 ERR("Event name exclusions are not yet implemented for %s event rules.",
695f7044 939 lttng_event_rule_type_str(event_rule_type));
4624dad0
SM
940 goto error;
941 }
942
e358ddd5 943 if (validate_exclusion_list(name, &exclude_names) != 0) {
b03a81fb
SM
944 /*
945 * Assume validate_exclusion_list already prints an
946 * error message.
947 */
4624dad0
SM
948 goto error;
949 }
950 }
951
949f049b 952 if (log_level_str) {
695f7044
JR
953 switch (event_rule_type) {
954 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
955 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
956 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
957 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
958 {
959 int log_level;
960 bool log_level_only;
949f049b 961
695f7044
JR
962 if (strcmp(log_level_str, "..") == 0) {
963 /*
964 * ".." is the same as passing no log level
965 * option and correspond to the "ANY" case.
966 */
967 break;
968 }
969
970 if (!parse_log_level_string(log_level_str, event_rule_type,
971 &log_level, &log_level_only)) {
972 ERR("Failed to parse log level string `%s`.",
973 log_level_str);
974 goto error;
975 }
976
977 if (log_level_only) {
978 log_level_rule = lttng_log_level_rule_exactly_create(log_level);
979 } else {
980 log_level_rule = lttng_log_level_rule_at_least_as_severe_as_create(log_level);
981 }
982
983 if (log_level_rule == NULL) {
984 ERR("Failed to create log level rule object.");
985 goto error;
986 }
987 break;
988 }
989 default:
990 ERR("Log levels are not supported for %s event rules.",
991 lttng_event_rule_type_str(event_rule_type));
949f049b
SM
992 goto error;
993 }
4624dad0
SM
994 }
995
996 /* Finally, create the event rule object. */
997 switch (event_rule_type) {
695f7044 998 case LTTNG_EVENT_RULE_TYPE_USER_TRACEPOINT:
4624dad0
SM
999 {
1000 enum lttng_event_rule_status event_rule_status;
1001
695f7044 1002 res.er = lttng_event_rule_user_tracepoint_create();
20a01d15 1003 if (!res.er) {
695f7044 1004 ERR("Failed to create user_tracepoint event rule.");
4624dad0
SM
1005 goto error;
1006 }
1007
1008 /* Set pattern. */
695f7044 1009 event_rule_status = lttng_event_rule_user_tracepoint_set_name_pattern(
50ad0862 1010 res.er, name);
4624dad0 1011 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
695f7044 1012 ERR("Failed to set user_tracepoint event rule's pattern to '%s'.",
50ad0862 1013 name);
4624dad0
SM
1014 goto error;
1015 }
1016
1017 /* Set filter. */
1018 if (filter) {
695f7044 1019 event_rule_status = lttng_event_rule_user_tracepoint_set_filter(
20a01d15 1020 res.er, filter);
4624dad0 1021 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
695f7044 1022 ERR("Failed to set user_tracepoint event rule's filter to '%s'.",
4624dad0
SM
1023 filter);
1024 goto error;
1025 }
1026 }
1027
1028 /* Set exclusion list. */
b03a81fb 1029 if (lttng_dynamic_pointer_array_get_count(&exclude_names) > 0) {
4624dad0 1030 int n;
b03a81fb
SM
1031 int count = lttng_dynamic_pointer_array_get_count(
1032 &exclude_names);
1033
1034 for (n = 0; n < count; n++) {
1035 const char *exclude_name =
48a40005 1036 (const char *) lttng_dynamic_pointer_array_get_pointer(
b03a81fb
SM
1037 &exclude_names,
1038 n);
1039
1040 event_rule_status =
695f7044 1041 lttng_event_rule_user_tracepoint_add_name_pattern_exclusion(
b03a81fb
SM
1042 res.er,
1043 exclude_name);
4624dad0
SM
1044 if (event_rule_status !=
1045 LTTNG_EVENT_RULE_STATUS_OK) {
695f7044 1046 ERR("Failed to set user_tracepoint exclusion list element '%s'",
b03a81fb 1047 exclude_name);
4624dad0
SM
1048 goto error;
1049 }
1050 }
1051 }
1052
695f7044
JR
1053 if (log_level_rule) {
1054 event_rule_status =
1055 lttng_event_rule_user_tracepoint_set_log_level_rule(
1056 res.er, log_level_rule);
949f049b 1057
695f7044
JR
1058 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1059 ERR("Failed to set log level on event fule.");
4624dad0
SM
1060 goto error;
1061 }
695f7044 1062 }
4624dad0 1063
695f7044
JR
1064 break;
1065 }
1066 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT:
1067 {
1068 enum lttng_event_rule_status event_rule_status;
1069
1070 res.er = lttng_event_rule_kernel_tracepoint_create();
1071 if (!res.er) {
1072 ERR("Failed to create kernel_tracepoint event rule.");
1073 goto error;
1074 }
1075
1076 /* Set pattern. */
1077 event_rule_status = lttng_event_rule_kernel_tracepoint_set_name_pattern(
1078 res.er, name);
1079 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1080 ERR("Failed to set kernel_tracepoint event rule's pattern to '%s'.",
1081 name);
1082 goto error;
1083 }
1084
1085 /* Set filter. */
1086 if (filter) {
1087 event_rule_status = lttng_event_rule_kernel_tracepoint_set_filter(
1088 res.er, filter);
1089 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1090 ERR("Failed to set kernel_tracepoint event rule's filter to '%s'.",
1091 filter);
1092 goto error;
85b05318 1093 }
695f7044
JR
1094 }
1095 break;
1096 }
1097 case LTTNG_EVENT_RULE_TYPE_JUL_LOGGING:
1098 {
1099 enum lttng_event_rule_status event_rule_status;
85b05318 1100
695f7044
JR
1101 res.er = lttng_event_rule_jul_logging_create();
1102 if (!res.er) {
1103 ERR("Failed to create jul_logging event rule.");
1104 goto error;
1105 }
1106
1107 /* Set pattern. */
1108 event_rule_status = lttng_event_rule_jul_logging_set_name_pattern(
1109 res.er, name);
1110 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1111 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1112 name);
1113 goto error;
1114 }
1115
1116 /* Set filter. */
1117 if (filter) {
1118 event_rule_status = lttng_event_rule_jul_logging_set_filter(
1119 res.er, filter);
1120 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1121 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1122 filter);
85b05318 1123 goto error;
4624dad0 1124 }
695f7044 1125 }
4624dad0 1126
695f7044 1127 if (log_level_rule) {
85b05318 1128 event_rule_status =
695f7044 1129 lttng_event_rule_jul_logging_set_log_level_rule(
85b05318
JR
1130 res.er, log_level_rule);
1131
4624dad0
SM
1132 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1133 ERR("Failed to set log level on event fule.");
1134 goto error;
1135 }
1136 }
695f7044
JR
1137 break;
1138 }
1139 case LTTNG_EVENT_RULE_TYPE_LOG4J_LOGGING:
1140 {
1141 enum lttng_event_rule_status event_rule_status;
4624dad0 1142
695f7044
JR
1143 res.er = lttng_event_rule_log4j_logging_create();
1144 if (!res.er) {
1145 ERR("Failed to create jul_logging event rule.");
1146 goto error;
1147 }
1148
1149 /* Set pattern. */
1150 event_rule_status = lttng_event_rule_log4j_logging_set_name_pattern(
1151 res.er, name);
1152 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1153 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1154 name);
1155 goto error;
1156 }
1157
1158 /* Set filter. */
1159 if (filter) {
1160 event_rule_status = lttng_event_rule_log4j_logging_set_filter(
1161 res.er, filter);
1162 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1163 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1164 filter);
1165 goto error;
1166 }
1167 }
1168
1169 if (log_level_rule) {
1170 event_rule_status =
1171 lttng_event_rule_log4j_logging_set_log_level_rule(
1172 res.er, log_level_rule);
1173
1174 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1175 ERR("Failed to set log level on event fule.");
1176 goto error;
1177 }
1178 }
1179 break;
1180 }
1181 case LTTNG_EVENT_RULE_TYPE_PYTHON_LOGGING:
1182 {
1183 enum lttng_event_rule_status event_rule_status;
1184
1185 res.er = lttng_event_rule_python_logging_create();
1186 if (!res.er) {
1187 ERR("Failed to create jul_logging event rule.");
1188 goto error;
1189 }
1190
1191 /* Set pattern. */
1192 event_rule_status = lttng_event_rule_python_logging_set_name_pattern(
1193 res.er, name);
1194 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1195 ERR("Failed to set jul_logging event rule's pattern to '%s'.",
1196 name);
1197 goto error;
1198 }
1199
1200 /* Set filter. */
1201 if (filter) {
1202 event_rule_status = lttng_event_rule_python_logging_set_filter(
1203 res.er, filter);
1204 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1205 ERR("Failed to set jul_logging event rule's filter to '%s'.",
1206 filter);
1207 goto error;
1208 }
1209 }
1210
1211 if (log_level_rule) {
1212 event_rule_status =
1213 lttng_event_rule_python_logging_set_log_level_rule(
1214 res.er, log_level_rule);
1215
1216 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1217 ERR("Failed to set log level on event fule.");
1218 goto error;
1219 }
1220 }
4624dad0
SM
1221 break;
1222 }
85522de5 1223 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE:
4624dad0
SM
1224 {
1225 int ret;
1226 enum lttng_event_rule_status event_rule_status;
1227
50ad0862 1228 ret = parse_kernel_probe_opts(
24de704e 1229 location, &kernel_probe_location);
4624dad0
SM
1230 if (ret) {
1231 ERR("Failed to parse kernel probe location.");
1232 goto error;
1233 }
1234
a0377dfe 1235 LTTNG_ASSERT(kernel_probe_location);
85522de5 1236 res.er = lttng_event_rule_kernel_kprobe_create(kernel_probe_location);
602a6d40
JR
1237 if (!res.er) {
1238 ERR("Failed to create kprobe event rule.");
4624dad0
SM
1239 goto error;
1240 }
1241
50ad0862 1242 event_rule_status =
85522de5 1243 lttng_event_rule_kernel_kprobe_set_event_name(
24de704e 1244 res.er, event_name);
4624dad0 1245 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
50ad0862 1246 ERR("Failed to set kprobe event rule's name to '%s'.",
24de704e 1247 event_name);
4624dad0
SM
1248 goto error;
1249 }
1250
1251 break;
1252 }
46fd07ac 1253 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE:
4624dad0
SM
1254 {
1255 int ret;
1256 enum lttng_event_rule_status event_rule_status;
1257
1258 ret = parse_userspace_probe_opts(
24de704e 1259 location, &userspace_probe_location);
4624dad0
SM
1260 if (ret) {
1261 ERR("Failed to parse user space probe location.");
1262 goto error;
1263 }
1264
46fd07ac 1265 res.er = lttng_event_rule_kernel_uprobe_create(userspace_probe_location);
20a01d15
SM
1266 if (!res.er) {
1267 ERR("Failed to create userspace probe event rule.");
4624dad0
SM
1268 goto error;
1269 }
1270
50ad0862 1271 event_rule_status =
46fd07ac 1272 lttng_event_rule_kernel_uprobe_set_event_name(
24de704e 1273 res.er, event_name);
4624dad0
SM
1274 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1275 ERR("Failed to set user space probe event rule's name to '%s'.",
24de704e 1276 event_name);
4624dad0
SM
1277 goto error;
1278 }
1279
1280 break;
1281 }
4f7da553 1282 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL:
4624dad0
SM
1283 {
1284 enum lttng_event_rule_status event_rule_status;
4f7da553 1285 enum lttng_event_rule_kernel_syscall_emission_site emission_site;
57739a6b
JR
1286
1287 if (!parse_syscall_emission_site_from_type(
f6a5af19 1288 event_rule_type_str, &emission_site)) {
57739a6b
JR
1289 ERR("Failed to parse syscall type '%s'.", event_rule_type_str);
1290 goto error;
1291 }
4624dad0 1292
4f7da553 1293 res.er = lttng_event_rule_kernel_syscall_create(emission_site);
20a01d15 1294 if (!res.er) {
4624dad0
SM
1295 ERR("Failed to create syscall event rule.");
1296 goto error;
1297 }
1298
4f7da553 1299 event_rule_status = lttng_event_rule_kernel_syscall_set_name_pattern(
50ad0862 1300 res.er, name);
4624dad0
SM
1301 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1302 ERR("Failed to set syscall event rule's pattern to '%s'.",
50ad0862 1303 name);
4624dad0
SM
1304 goto error;
1305 }
1306
1307 if (filter) {
4f7da553 1308 event_rule_status = lttng_event_rule_kernel_syscall_set_filter(
20a01d15 1309 res.er, filter);
4624dad0
SM
1310 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
1311 ERR("Failed to set syscall event rule's filter to '%s'.",
1312 filter);
1313 goto error;
1314 }
1315 }
1316
1317 break;
1318 }
1319 default:
1320 abort();
1321 goto error;
1322 }
1323
1324 goto end;
1325
1326error:
20a01d15
SM
1327 lttng_event_rule_destroy(res.er);
1328 res.er = NULL;
1329 lttng_dynamic_pointer_array_reset(&res.capture_descriptors);
4624dad0
SM
1330
1331end:
20a01d15
SM
1332 if (parser_ctx) {
1333 filter_parser_ctx_free(parser_ctx);
1334 }
1335
1336 lttng_event_expr_destroy(event_expr);
d50d200a
SM
1337 argpar_item_destroy(argpar_item);
1338 argpar_iter_destroy(argpar_iter);
4624dad0 1339 free(filter);
50ad0862 1340 free(name);
b03a81fb 1341 lttng_dynamic_pointer_array_reset(&exclude_names);
949f049b 1342 free(log_level_str);
24de704e
SM
1343 free(location);
1344 free(event_name);
57739a6b 1345 free(event_rule_type_str);
50ad0862 1346
4624dad0
SM
1347 lttng_kernel_probe_location_destroy(kernel_probe_location);
1348 lttng_userspace_probe_location_destroy(userspace_probe_location);
85b05318 1349 lttng_log_level_rule_destroy(log_level_rule);
20a01d15 1350 return res;
4624dad0
SM
1351}
1352
1353static
35c4b2b3
SM
1354struct lttng_condition *handle_condition_event(int *argc, const char ***argv,
1355 int argc_offset)
4624dad0 1356{
20a01d15 1357 struct parse_event_rule_res res;
4624dad0 1358 struct lttng_condition *c;
20a01d15 1359 size_t i;
4624dad0 1360
35c4b2b3 1361 res = parse_event_rule(argc, argv, argc_offset);
20a01d15 1362 if (!res.er) {
4624dad0 1363 c = NULL;
20a01d15 1364 goto error;
4624dad0
SM
1365 }
1366
8dbb86b8 1367 c = lttng_condition_event_rule_matches_create(res.er);
20a01d15
SM
1368 lttng_event_rule_destroy(res.er);
1369 res.er = NULL;
4624dad0 1370 if (!c) {
20a01d15
SM
1371 goto error;
1372 }
1373
1374 for (i = 0; i < lttng_dynamic_pointer_array_get_count(&res.capture_descriptors);
1375 i++) {
1376 enum lttng_condition_status status;
1377 struct lttng_event_expr **expr =
48a40005 1378 (lttng_event_expr **) lttng_dynamic_array_get_element(
20a01d15
SM
1379 &res.capture_descriptors.array, i);
1380
a0377dfe
FD
1381 LTTNG_ASSERT(expr);
1382 LTTNG_ASSERT(*expr);
8dbb86b8 1383 status = lttng_condition_event_rule_matches_append_capture_descriptor(
20a01d15
SM
1384 c, *expr);
1385 if (status != LTTNG_CONDITION_STATUS_OK) {
81d566c9
JR
1386 if (status == LTTNG_CONDITION_STATUS_UNSUPPORTED) {
1387 ERR("The capture feature is unsupported by the event-rule condition type");
1388 }
1389
20a01d15
SM
1390 goto error;
1391 }
1392
1393 /* Ownership of event expression moved to `c` */
1394 *expr = NULL;
4624dad0
SM
1395 }
1396
20a01d15
SM
1397 goto end;
1398
1399error:
1400 lttng_condition_destroy(c);
1401 c = NULL;
1402
4624dad0 1403end:
20a01d15
SM
1404 lttng_dynamic_pointer_array_reset(&res.capture_descriptors);
1405 lttng_event_rule_destroy(res.er);
4624dad0
SM
1406 return c;
1407}
1408
f1494934 1409namespace {
4624dad0
SM
1410struct condition_descr {
1411 const char *name;
35c4b2b3
SM
1412 struct lttng_condition *(*handler) (int *argc, const char ***argv,
1413 int argc_offset);
4624dad0 1414};
f1494934 1415} /* namespace */
4624dad0
SM
1416
1417static const
1418struct condition_descr condition_descrs[] = {
665db063 1419 { "event-rule-matches", handle_condition_event },
4624dad0
SM
1420};
1421
ef9ff9cb
SM
1422static
1423void print_valid_condition_names(void)
1424{
1425 unsigned int i;
1426
1427 ERR("Valid condition names are:");
1428
1429 for (i = 0; i < ARRAY_SIZE(condition_descrs); ++i) {
1430 ERR(" %s", condition_descrs[i].name);
1431 }
1432}
1433
4624dad0 1434static
29bcf415 1435struct lttng_condition *parse_condition(const char *condition_name, int *argc,
e776cf47
SM
1436 const char ***argv, int argc_offset, int orig_arg_index,
1437 const char *orig_arg)
4624dad0
SM
1438{
1439 int i;
1440 struct lttng_condition *cond;
4624dad0
SM
1441 const struct condition_descr *descr = NULL;
1442
4624dad0
SM
1443 for (i = 0; i < ARRAY_SIZE(condition_descrs); i++) {
1444 if (strcmp(condition_name, condition_descrs[i].name) == 0) {
1445 descr = &condition_descrs[i];
1446 break;
1447 }
1448 }
1449
1450 if (!descr) {
e776cf47
SM
1451 ERR(WHILE_PARSING_ARG_N_ARG_FMT "Unknown condition name '%s'",
1452 orig_arg_index + 1, orig_arg, condition_name);
ef9ff9cb 1453 print_valid_condition_names();
4624dad0
SM
1454 goto error;
1455 }
1456
35c4b2b3 1457 cond = descr->handler(argc, argv, argc_offset);
4624dad0
SM
1458 if (!cond) {
1459 /* The handler has already printed an error message. */
1460 goto error;
1461 }
1462
1463 goto end;
1464error:
1465 cond = NULL;
1466end:
1467 return cond;
1468}
bbadb5e0
JR
1469
1470static struct lttng_rate_policy *parse_rate_policy(const char *policy_str)
1471{
e358ddd5
JG
1472 int ret;
1473 size_t num_token = 0;
1474 struct lttng_dynamic_pointer_array tokens;
bbadb5e0
JR
1475 struct lttng_rate_policy *policy = NULL;
1476 enum lttng_rate_policy_type policy_type;
1477 unsigned long long value;
1478 char *policy_type_str;
1479 char *policy_value_str;
1480
a0377dfe 1481 LTTNG_ASSERT(policy_str);
e358ddd5 1482 lttng_dynamic_pointer_array_init(&tokens, NULL);
bbadb5e0 1483
e358ddd5
JG
1484 /* Rate policy fields are separated by ':'. */
1485 ret = strutils_split(policy_str, ':', 1, &tokens);
1486 if (ret == 0) {
1487 num_token = lttng_dynamic_pointer_array_get_count(&tokens);
1488 }
bbadb5e0
JR
1489
1490 /*
1491 * Early sanity check that the number of parameter is exactly 2.
1492 * i.e : type:value
1493 */
1494 if (num_token != 2) {
1495 ERR("Rate policy format is invalid.");
1496 goto end;
1497 }
1498
48a40005
SM
1499 policy_type_str = (char *) lttng_dynamic_pointer_array_get_pointer(&tokens, 0);
1500 policy_value_str = (char *) lttng_dynamic_pointer_array_get_pointer(&tokens, 1);
bbadb5e0
JR
1501
1502 /* Parse the type. */
1503 if (strcmp(policy_type_str, "once-after") == 0) {
1504 policy_type = LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N;
1505 } else if (strcmp(policy_type_str, "every") == 0) {
1506 policy_type = LTTNG_RATE_POLICY_TYPE_EVERY_N;
1507 } else {
1508 ERR("Rate policy type `%s` unknown.", policy_type_str);
1509 goto end;
1510 }
1511
1512 /* Parse the value. */
1513 if (utils_parse_unsigned_long_long(policy_value_str, &value) != 0) {
1514 ERR("Failed to parse rate policy value `%s` as an integer.",
1515 policy_value_str);
1516 goto end;
1517 }
1518
1519 if (value == 0) {
1520 ERR("Rate policy value `%s` must be > 0.", policy_value_str);
1521 goto end;
1522 }
1523
1524 switch (policy_type) {
1525 case LTTNG_RATE_POLICY_TYPE_EVERY_N:
1526 policy = lttng_rate_policy_every_n_create(value);
1527 break;
1528 case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N:
1529 policy = lttng_rate_policy_once_after_n_create(value);
1530 break;
1531 default:
1532 abort();
1533 }
1534
1535 if (policy == NULL) {
1536 ERR("Failed to create rate policy `%s`.", policy_str);
1537 }
1538
1539end:
e358ddd5 1540 lttng_dynamic_pointer_array_reset(&tokens);
bbadb5e0
JR
1541 return policy;
1542}
1543
e45dd625 1544static const struct argpar_opt_descr notify_action_opt_descrs[] = {
bbadb5e0 1545 { OPT_RATE_POLICY, '\0', "rate-policy", true },
e45dd625
JR
1546 ARGPAR_OPT_DESCR_SENTINEL
1547};
4624dad0 1548
4624dad0 1549static
35c4b2b3
SM
1550struct lttng_action *handle_action_notify(int *argc, const char ***argv,
1551 int argc_offset)
4624dad0 1552{
e45dd625 1553 struct lttng_action *action = NULL;
d50d200a
SM
1554 struct argpar_iter *argpar_iter = NULL;
1555 const struct argpar_item *argpar_item = NULL;
7f4d5b07 1556 struct lttng_rate_policy *policy = NULL;
4624dad0 1557
d50d200a
SM
1558 argpar_iter = argpar_iter_create(*argc, *argv, notify_action_opt_descrs);
1559 if (!argpar_iter) {
1560 ERR("Failed to allocate an argpar iter.");
e45dd625
JR
1561 goto error;
1562 }
1563
1564 while (true) {
d50d200a 1565 enum parse_next_item_status status;
e45dd625 1566
35c4b2b3 1567 status = parse_next_item(argpar_iter, &argpar_item,
ef9ff9cb 1568 argc_offset, *argv, false, NULL,
35c4b2b3 1569 "While parsing `notify` action:");
ef9ff9cb
SM
1570 if (status == PARSE_NEXT_ITEM_STATUS_ERROR ||
1571 status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) {
e45dd625 1572 goto error;
d50d200a 1573 } else if (status == PARSE_NEXT_ITEM_STATUS_END) {
e45dd625
JR
1574 break;
1575 }
1576
d50d200a 1577 LTTNG_ASSERT(status == PARSE_NEXT_ITEM_STATUS_OK);
e45dd625 1578
d50d200a
SM
1579 if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_OPT) {
1580 const struct argpar_opt_descr *descr =
1581 argpar_item_opt_descr(argpar_item);
1582 const char *arg = argpar_item_opt_arg(argpar_item);
e45dd625 1583
d50d200a 1584 switch (descr->id) {
bbadb5e0 1585 case OPT_RATE_POLICY:
e45dd625 1586 {
d50d200a 1587 policy = parse_rate_policy(arg);
bbadb5e0 1588 if (!policy) {
e45dd625
JR
1589 goto error;
1590 }
e45dd625
JR
1591 break;
1592 }
e45dd625
JR
1593 default:
1594 abort();
1595 }
1596 } else {
d50d200a 1597 const char *arg = argpar_item_non_opt_arg(argpar_item);
e45dd625 1598
d50d200a
SM
1599 ERR("Unexpected argument `%s`.", arg);
1600 goto error;
e45dd625
JR
1601 }
1602 }
1603
d50d200a
SM
1604 *argc -= argpar_iter_ingested_orig_args(argpar_iter);
1605 *argv += argpar_iter_ingested_orig_args(argpar_iter);
e45dd625 1606
e45dd625
JR
1607 action = lttng_action_notify_create();
1608 if (!action) {
1609 ERR("Failed to create notify action");
1610 goto error;
1611 }
1612
1613 if (policy) {
1614 enum lttng_action_status status;
7f4d5b07 1615 status = lttng_action_notify_set_rate_policy(action, policy);
e45dd625 1616 if (status != LTTNG_ACTION_STATUS_OK) {
7f4d5b07 1617 ERR("Failed to set rate policy");
e45dd625
JR
1618 goto error;
1619 }
1620 }
1621
1622 goto end;
1623
1624error:
1625 lttng_action_destroy(action);
1626 action = NULL;
e45dd625 1627end:
7f4d5b07 1628 lttng_rate_policy_destroy(policy);
d50d200a
SM
1629 argpar_item_destroy(argpar_item);
1630 argpar_iter_destroy(argpar_iter);
e45dd625
JR
1631 return action;
1632}
4624dad0
SM
1633
1634/*
e45dd625 1635 * Generic handler for a kind of action that takes a session name and an
7f4d5b07 1636 * optional rate policy.
4624dad0
SM
1637 */
1638
e45dd625
JR
1639static struct lttng_action *handle_action_simple_session_with_policy(int *argc,
1640 const char ***argv,
35c4b2b3 1641 int argc_offset,
4624dad0 1642 struct lttng_action *(*create_action_cb)(void),
e45dd625
JR
1643 enum lttng_action_status (*set_session_name_cb)(
1644 struct lttng_action *, const char *),
7f4d5b07 1645 enum lttng_action_status (*set_rate_policy_cb)(
e45dd625 1646 struct lttng_action *,
7f4d5b07 1647 const struct lttng_rate_policy *),
4624dad0
SM
1648 const char *action_name)
1649{
1650 struct lttng_action *action = NULL;
d50d200a
SM
1651 struct argpar_iter *argpar_iter = NULL;
1652 const struct argpar_item *argpar_item = NULL;
4624dad0 1653 const char *session_name_arg = NULL;
4624dad0 1654 enum lttng_action_status action_status;
7f4d5b07 1655 struct lttng_rate_policy *policy = NULL;
e45dd625 1656
a0377dfe
FD
1657 LTTNG_ASSERT(set_session_name_cb);
1658 LTTNG_ASSERT(set_rate_policy_cb);
e45dd625 1659
7f4d5b07 1660 const struct argpar_opt_descr rate_policy_opt_descrs[] = {
bbadb5e0 1661 { OPT_RATE_POLICY, '\0', "rate-policy", true },
e45dd625
JR
1662 ARGPAR_OPT_DESCR_SENTINEL
1663 };
b03a81fb 1664
d50d200a
SM
1665 argpar_iter = argpar_iter_create(*argc, *argv, rate_policy_opt_descrs);
1666 if (!argpar_iter) {
1667 ERR("Failed to allocate an argpar iter.");
4624dad0
SM
1668 goto error;
1669 }
1670
1671 while (true) {
d50d200a 1672 enum parse_next_item_status status;
4624dad0 1673
35c4b2b3 1674 status = parse_next_item(argpar_iter, &argpar_item, argc_offset,
ef9ff9cb 1675 *argv, false, NULL,
35c4b2b3 1676 "While parsing `%s` action:", action_name);
ef9ff9cb
SM
1677 if (status == PARSE_NEXT_ITEM_STATUS_ERROR ||
1678 status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) {
4624dad0 1679 goto error;
d50d200a 1680 } else if (status == PARSE_NEXT_ITEM_STATUS_END) {
4624dad0
SM
1681 break;
1682 }
1683
d50d200a
SM
1684 LTTNG_ASSERT(status == PARSE_NEXT_ITEM_STATUS_OK);
1685
1686 if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_OPT) {
1687 const struct argpar_opt_descr *descr =
1688 argpar_item_opt_descr(argpar_item);
1689 const char *arg = argpar_item_opt_arg(argpar_item);
4624dad0 1690
d50d200a 1691 switch (descr->id) {
bbadb5e0 1692 case OPT_RATE_POLICY:
e45dd625 1693 {
d50d200a 1694 policy = parse_rate_policy(arg);
bbadb5e0 1695 if (!policy) {
e45dd625
JR
1696 goto error;
1697 }
e45dd625
JR
1698 break;
1699 }
e45dd625
JR
1700 default:
1701 abort();
1702 }
1703 } else {
d50d200a
SM
1704 const char *arg = argpar_item_non_opt_arg(argpar_item);
1705 unsigned int idx = argpar_item_non_opt_non_opt_index(argpar_item);
e45dd625 1706
d50d200a 1707 switch (idx) {
e45dd625 1708 case 0:
d50d200a 1709 session_name_arg = arg;
e45dd625
JR
1710 break;
1711 default:
d50d200a 1712 ERR("Unexpected argument `%s`.", arg);
e45dd625
JR
1713 goto error;
1714 }
4624dad0
SM
1715 }
1716 }
1717
d50d200a
SM
1718 *argc -= argpar_iter_ingested_orig_args(argpar_iter);
1719 *argv += argpar_iter_ingested_orig_args(argpar_iter);
4624dad0
SM
1720
1721 if (!session_name_arg) {
1722 ERR("Missing session name.");
1723 goto error;
1724 }
1725
1726 action = create_action_cb();
1727 if (!action) {
1728 ERR("Failed to allocate %s session action.", action_name);
1729 goto error;
1730 }
1731
1732 action_status = set_session_name_cb(action, session_name_arg);
1733 if (action_status != LTTNG_ACTION_STATUS_OK) {
1734 ERR("Failed to set action %s session's session name to '%s'.",
1735 action_name, session_name_arg);
1736 goto error;
1737 }
1738
e45dd625 1739 if (policy) {
7f4d5b07 1740 action_status = set_rate_policy_cb(action, policy);
e45dd625 1741 if (action_status != LTTNG_ACTION_STATUS_OK) {
7f4d5b07 1742 ERR("Failed to set rate policy");
e45dd625
JR
1743 goto error;
1744 }
1745 }
1746
4624dad0
SM
1747 goto end;
1748
1749error:
1750 lttng_action_destroy(action);
1751 action = NULL;
d50d200a 1752
4624dad0 1753end:
7f4d5b07 1754 lttng_rate_policy_destroy(policy);
d50d200a
SM
1755 argpar_item_destroy(argpar_item);
1756 argpar_iter_destroy(argpar_iter);
4624dad0
SM
1757 return action;
1758}
1759
1760static
1761struct lttng_action *handle_action_start_session(int *argc,
35c4b2b3 1762 const char ***argv, int argc_offset)
4624dad0 1763{
e45dd625 1764 return handle_action_simple_session_with_policy(argc, argv,
35c4b2b3 1765 argc_offset,
e45dd625
JR
1766 lttng_action_start_session_create,
1767 lttng_action_start_session_set_session_name,
7f4d5b07 1768 lttng_action_start_session_set_rate_policy, "start");
4624dad0
SM
1769}
1770
1771static
1772struct lttng_action *handle_action_stop_session(int *argc,
35c4b2b3 1773 const char ***argv, int argc_offset)
4624dad0 1774{
e45dd625 1775 return handle_action_simple_session_with_policy(argc, argv,
35c4b2b3 1776 argc_offset,
e45dd625
JR
1777 lttng_action_stop_session_create,
1778 lttng_action_stop_session_set_session_name,
7f4d5b07 1779 lttng_action_stop_session_set_rate_policy, "stop");
4624dad0
SM
1780}
1781
1782static
1783struct lttng_action *handle_action_rotate_session(int *argc,
35c4b2b3 1784 const char ***argv, int argc_offset)
4624dad0 1785{
e45dd625 1786 return handle_action_simple_session_with_policy(argc, argv,
35c4b2b3 1787 argc_offset,
4624dad0
SM
1788 lttng_action_rotate_session_create,
1789 lttng_action_rotate_session_set_session_name,
7f4d5b07 1790 lttng_action_rotate_session_set_rate_policy,
4624dad0
SM
1791 "rotate");
1792}
1793
1794static const struct argpar_opt_descr snapshot_action_opt_descrs[] = {
1795 { OPT_NAME, 'n', "name", true },
1796 { OPT_MAX_SIZE, 'm', "max-size", true },
1797 { OPT_CTRL_URL, '\0', "ctrl-url", true },
1798 { OPT_DATA_URL, '\0', "data-url", true },
1799 { OPT_URL, '\0', "url", true },
1800 { OPT_PATH, '\0', "path", true },
bbadb5e0 1801 { OPT_RATE_POLICY, '\0', "rate-policy", true },
4624dad0
SM
1802 ARGPAR_OPT_DESCR_SENTINEL
1803};
1804
1805static
1806struct lttng_action *handle_action_snapshot_session(int *argc,
35c4b2b3 1807 const char ***argv, int argc_offset)
4624dad0
SM
1808{
1809 struct lttng_action *action = NULL;
d50d200a
SM
1810 struct argpar_iter *argpar_iter = NULL;
1811 const struct argpar_item *argpar_item = NULL;
4624dad0
SM
1812 const char *session_name_arg = NULL;
1813 char *snapshot_name_arg = NULL;
1814 char *ctrl_url_arg = NULL;
1815 char *data_url_arg = NULL;
1816 char *max_size_arg = NULL;
1817 char *url_arg = NULL;
1818 char *path_arg = NULL;
1819 char *error = NULL;
1820 enum lttng_action_status action_status;
1821 struct lttng_snapshot_output *snapshot_output = NULL;
7f4d5b07 1822 struct lttng_rate_policy *policy = NULL;
4624dad0
SM
1823 int ret;
1824 unsigned int locations_specified = 0;
1825
d50d200a
SM
1826 argpar_iter = argpar_iter_create(*argc, *argv, snapshot_action_opt_descrs);
1827 if (!argpar_iter) {
1828 ERR("Failed to allocate an argpar iter.");
4624dad0
SM
1829 goto error;
1830 }
1831
1832 while (true) {
d50d200a 1833 enum parse_next_item_status status;
4624dad0 1834
35c4b2b3 1835 status = parse_next_item(argpar_iter, &argpar_item, argc_offset,
ef9ff9cb
SM
1836 *argv, false, NULL, "While parsing `snapshot` action:");
1837 if (status == PARSE_NEXT_ITEM_STATUS_ERROR ||
1838 status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) {
4624dad0 1839 goto error;
d50d200a 1840 } else if (status == PARSE_NEXT_ITEM_STATUS_END) {
4624dad0
SM
1841 break;
1842 }
1843
d50d200a 1844 LTTNG_ASSERT(status == PARSE_NEXT_ITEM_STATUS_OK);
4624dad0 1845
d50d200a
SM
1846 if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_OPT) {
1847 const struct argpar_opt_descr *descr =
1848 argpar_item_opt_descr(argpar_item);
1849 const char *arg = argpar_item_opt_arg(argpar_item);
4624dad0 1850
d50d200a 1851 switch (descr->id) {
4624dad0 1852 case OPT_NAME:
d50d200a 1853 if (!assign_string(&snapshot_name_arg, arg, "--name/-n")) {
4624dad0
SM
1854 goto error;
1855 }
1856
1857 break;
1858 case OPT_MAX_SIZE:
d50d200a 1859 if (!assign_string(&max_size_arg, arg, "--max-size/-m")) {
4624dad0
SM
1860 goto error;
1861 }
1862
1863 break;
1864 case OPT_CTRL_URL:
d50d200a 1865 if (!assign_string(&ctrl_url_arg, arg, "--ctrl-url")) {
4624dad0
SM
1866 goto error;
1867 }
1868
1869 break;
1870 case OPT_DATA_URL:
d50d200a 1871 if (!assign_string(&data_url_arg, arg, "--data-url")) {
4624dad0
SM
1872 goto error;
1873 }
1874
1875 break;
1876 case OPT_URL:
d50d200a 1877 if (!assign_string(&url_arg, arg, "--url")) {
4624dad0
SM
1878 goto error;
1879 }
1880
1881 break;
1882 case OPT_PATH:
d50d200a 1883 if (!assign_string(&path_arg, arg, "--path")) {
4624dad0
SM
1884 goto error;
1885 }
1886
1887 break;
bbadb5e0 1888 case OPT_RATE_POLICY:
e45dd625 1889 {
d50d200a 1890 policy = parse_rate_policy(arg);
bbadb5e0 1891 if (!policy) {
e45dd625
JR
1892 goto error;
1893 }
e45dd625
JR
1894 break;
1895 }
4624dad0
SM
1896 default:
1897 abort();
1898 }
1899 } else {
d50d200a
SM
1900 const char *arg = argpar_item_non_opt_arg(argpar_item);
1901 const unsigned int idx = argpar_item_non_opt_non_opt_index(argpar_item);
4624dad0 1902
d50d200a 1903 switch (idx) {
4624dad0 1904 case 0:
d50d200a 1905 session_name_arg = arg;
4624dad0
SM
1906 break;
1907 default:
d50d200a 1908 ERR("Unexpected argument `%s`.", arg);
4624dad0
SM
1909 goto error;
1910 }
1911 }
1912 }
1913
d50d200a
SM
1914 *argc -= argpar_iter_ingested_orig_args(argpar_iter);
1915 *argv += argpar_iter_ingested_orig_args(argpar_iter);
4624dad0
SM
1916
1917 if (!session_name_arg) {
1918 ERR("Missing session name.");
1919 goto error;
1920 }
1921
1922 /* --ctrl-url and --data-url must come in pair. */
1923 if (ctrl_url_arg && !data_url_arg) {
1924 ERR("--ctrl-url is specified, but --data-url is missing.");
1925 goto error;
1926 }
1927
1928 if (!ctrl_url_arg && data_url_arg) {
1929 ERR("--data-url is specified, but --ctrl-url is missing.");
1930 goto error;
1931 }
1932
1933 locations_specified += !!(ctrl_url_arg || data_url_arg);
1934 locations_specified += !!url_arg;
1935 locations_specified += !!path_arg;
1936
1937 /* --ctrl-url/--data-url, --url and --path are mutually exclusive. */
1938 if (locations_specified > 1) {
1939 ERR("The --ctrl-url/--data-url, --url, and --path options can't be used together.");
1940 goto error;
1941 }
1942
1943 /*
1944 * Did the user specify an option that implies using a
1945 * custom/unregistered output?
1946 */
1947 if (url_arg || ctrl_url_arg || path_arg) {
1948 snapshot_output = lttng_snapshot_output_create();
1949 if (!snapshot_output) {
1950 ERR("Failed to allocate a snapshot output.");
1951 goto error;
1952 }
1953 }
1954
1955 action = lttng_action_snapshot_session_create();
1956 if (!action) {
1957 ERR("Failed to allocate snapshot session action.");
1958 goto error;
1959 }
1960
1961 action_status = lttng_action_snapshot_session_set_session_name(
1962 action, session_name_arg);
1963 if (action_status != LTTNG_ACTION_STATUS_OK) {
1964 ERR("Failed to set action snapshot session's session name to '%s'.",
1965 session_name_arg);
1966 goto error;
1967 }
1968
1969 if (snapshot_name_arg) {
1970 if (!snapshot_output) {
1971 ERR("Can't provide a snapshot output name without a snapshot output destination.");
1972 goto error;
1973 }
1974
1975 ret = lttng_snapshot_output_set_name(
1976 snapshot_name_arg, snapshot_output);
1977 if (ret != 0) {
1978 ERR("Failed to set name of snapshot output.");
1979 goto error;
1980 }
1981 }
1982
1983 if (max_size_arg) {
1984 uint64_t max_size;
1985
1986 if (!snapshot_output) {
1987 ERR("Can't provide a snapshot output max size without a snapshot output destination.");
1988 goto error;
1989 }
1990
1991 ret = utils_parse_size_suffix(max_size_arg, &max_size);
1992 if (ret != 0) {
1993 ERR("Failed to parse `%s` as a size.", max_size_arg);
1994 goto error;
1995 }
1996
1997 ret = lttng_snapshot_output_set_size(max_size, snapshot_output);
1998 if (ret != 0) {
1999 ERR("Failed to set snapshot output's max size to %" PRIu64 " bytes.",
2000 max_size);
2001 goto error;
2002 }
2003 }
2004
2005 if (url_arg) {
2006 int num_uris;
2007 struct lttng_uri *uris;
2008
2009 if (!strstr(url_arg, "://")) {
2010 ERR("Failed to parse '%s' as an URL.", url_arg);
2011 goto error;
2012 }
2013
2014 num_uris = uri_parse_str_urls(url_arg, NULL, &uris);
2015 if (num_uris < 1) {
2016 ERR("Failed to parse '%s' as an URL.", url_arg);
2017 goto error;
2018 }
2019
2020 if (uris[0].dtype == LTTNG_DST_PATH) {
2021 ret = lttng_snapshot_output_set_local_path(
2022 uris[0].dst.path, snapshot_output);
2023 free(uris);
2024 if (ret != 0) {
2025 ERR("Failed to assign '%s' as a local destination.",
2026 url_arg);
2027 goto error;
2028 }
2029 } else {
2030 ret = lttng_snapshot_output_set_network_url(
2031 url_arg, snapshot_output);
2032 free(uris);
2033 if (ret != 0) {
2034 ERR("Failed to assign '%s' as a network URL.",
2035 url_arg);
2036 goto error;
2037 }
2038 }
2039 }
2040
2041 if (path_arg) {
2042 ret = lttng_snapshot_output_set_local_path(
2043 path_arg, snapshot_output);
2044 if (ret != 0) {
2045 ERR("Failed to parse '%s' as a local path.", path_arg);
2046 goto error;
2047 }
2048 }
2049
2050 if (ctrl_url_arg) {
2051 /*
2052 * Two argument form, network output with separate control and
2053 * data URLs.
2054 */
2055 ret = lttng_snapshot_output_set_network_urls(
2056 ctrl_url_arg, data_url_arg, snapshot_output);
2057 if (ret != 0) {
2058 ERR("Failed to parse `%s` and `%s` as control and data URLs.",
2059 ctrl_url_arg, data_url_arg);
2060 goto error;
2061 }
2062 }
2063
2064 if (snapshot_output) {
2065 action_status = lttng_action_snapshot_session_set_output(
2066 action, snapshot_output);
2067 if (action_status != LTTNG_ACTION_STATUS_OK) {
2068 ERR("Failed to set snapshot session action's output.");
2069 goto error;
2070 }
2071
2072 /* Ownership of `snapshot_output` has been transferred to the action. */
2073 snapshot_output = NULL;
2074 }
2075
e45dd625
JR
2076 if (policy) {
2077 enum lttng_action_status status;
7f4d5b07 2078 status = lttng_action_snapshot_session_set_rate_policy(
e45dd625
JR
2079 action, policy);
2080 if (status != LTTNG_ACTION_STATUS_OK) {
7f4d5b07 2081 ERR("Failed to set rate policy");
e45dd625
JR
2082 goto error;
2083 }
2084 }
2085
4624dad0
SM
2086 goto end;
2087
2088error:
2089 lttng_action_destroy(action);
2090 action = NULL;
2091 free(error);
2092end:
2093 free(snapshot_name_arg);
2094 free(path_arg);
2095 free(url_arg);
2096 free(ctrl_url_arg);
2097 free(data_url_arg);
2098 free(snapshot_output);
f6b73530 2099 free(max_size_arg);
7f4d5b07 2100 lttng_rate_policy_destroy(policy);
d50d200a
SM
2101 argpar_item_destroy(argpar_item);
2102 argpar_iter_destroy(argpar_iter);
4624dad0
SM
2103 return action;
2104}
2105
f1494934 2106namespace {
4624dad0
SM
2107struct action_descr {
2108 const char *name;
35c4b2b3
SM
2109 struct lttng_action *(*handler) (int *argc, const char ***argv,
2110 int argc_offset);
4624dad0 2111};
f1494934 2112} /* namespace */
4624dad0
SM
2113
2114static const
2115struct action_descr action_descrs[] = {
2116 { "notify", handle_action_notify },
2117 { "start-session", handle_action_start_session },
2118 { "stop-session", handle_action_stop_session },
2119 { "rotate-session", handle_action_rotate_session },
2120 { "snapshot-session", handle_action_snapshot_session },
2121};
2122
ef9ff9cb
SM
2123static
2124void print_valid_action_names(void)
2125{
2126 unsigned int i;
2127
2128 ERR("Valid action names are:");
2129
2130 for (i = 0; i < ARRAY_SIZE(condition_descrs); ++i) {
2131 ERR(" %s", action_descrs[i].name);
2132 }
2133}
2134
4624dad0 2135static
35c4b2b3 2136struct lttng_action *parse_action(const char *action_name, int *argc,
e776cf47
SM
2137 const char ***argv, int argc_offset, int orig_arg_index,
2138 const char *orig_arg)
4624dad0
SM
2139{
2140 int i;
2141 struct lttng_action *action;
4624dad0
SM
2142 const struct action_descr *descr = NULL;
2143
4624dad0
SM
2144 for (i = 0; i < ARRAY_SIZE(action_descrs); i++) {
2145 if (strcmp(action_name, action_descrs[i].name) == 0) {
2146 descr = &action_descrs[i];
2147 break;
2148 }
2149 }
2150
2151 if (!descr) {
ef9ff9cb 2152 ERR(WHILE_PARSING_ARG_N_ARG_FMT "Unknown action name '%s'",
e776cf47 2153 orig_arg_index + 1, orig_arg, action_name);
ef9ff9cb 2154 print_valid_action_names();
4624dad0
SM
2155 goto error;
2156 }
2157
35c4b2b3 2158 action = descr->handler(argc, argv, argc_offset);
4624dad0
SM
2159 if (!action) {
2160 /* The handler has already printed an error message. */
2161 goto error;
2162 }
2163
2164 goto end;
2165error:
2166 action = NULL;
2167end:
2168 return action;
2169}
2170
2171static const
2172struct argpar_opt_descr add_trigger_options[] = {
2173 { OPT_HELP, 'h', "help", false },
2174 { OPT_LIST_OPTIONS, '\0', "list-options", false },
29bcf415
SM
2175 { OPT_CONDITION, '\0', "condition", true },
2176 { OPT_ACTION, '\0', "action", true },
1d4b59f2 2177 { OPT_NAME, '\0', "name", true },
481c5310 2178 { OPT_OWNER_UID, '\0', "owner-uid", true },
4624dad0
SM
2179 ARGPAR_OPT_DESCR_SENTINEL,
2180};
2181
2182static
2183void lttng_actions_destructor(void *p)
2184{
48a40005 2185 struct lttng_action *action = (lttng_action *) p;
4624dad0
SM
2186
2187 lttng_action_destroy(action);
2188}
2189
2190int cmd_add_trigger(int argc, const char **argv)
2191{
2192 int ret;
2193 int my_argc = argc - 1;
2194 const char **my_argv = argv + 1;
2195 struct lttng_condition *condition = NULL;
2196 struct lttng_dynamic_pointer_array actions;
d50d200a
SM
2197 struct argpar_iter *argpar_iter = NULL;
2198 const struct argpar_item *argpar_item = NULL;
ef9ff9cb 2199 const struct argpar_error *argpar_error = NULL;
702f26c8 2200 struct lttng_action *action_list = NULL;
4624dad0
SM
2201 struct lttng_action *action = NULL;
2202 struct lttng_trigger *trigger = NULL;
1d4b59f2 2203 char *name = NULL;
4624dad0 2204 int i;
481c5310 2205 char *owner_uid = NULL;
a5c2d2a7 2206 enum lttng_error_code ret_code;
523c4f8c 2207 struct mi_writer *mi_writer = NULL;
4624dad0
SM
2208
2209 lttng_dynamic_pointer_array_init(&actions, lttng_actions_destructor);
2210
523c4f8c
JR
2211 if (lttng_opt_mi) {
2212 mi_writer = mi_lttng_writer_create(
2213 fileno(stdout), lttng_opt_mi);
2214 if (!mi_writer) {
2215 ret = CMD_ERROR;
2216 goto error;
2217 }
2218
2219 /* Open command element. */
2220 ret = mi_lttng_writer_command_open(mi_writer,
2221 mi_lttng_element_command_add_trigger);
2222 if (ret) {
2223 ret = CMD_ERROR;
2224 goto error;
2225 }
2226
2227 /* Open output element. */
2228 ret = mi_lttng_writer_open_element(
2229 mi_writer, mi_lttng_element_command_output);
2230 if (ret) {
2231 ret = CMD_ERROR;
2232 goto error;
2233 }
2234 }
2235
4624dad0 2236 while (true) {
d50d200a 2237 enum parse_next_item_status status;
4624dad0 2238 int ingested_args;
d50d200a
SM
2239 const struct argpar_opt_descr *descr;
2240 const char *arg;
4624dad0 2241
d50d200a
SM
2242 argpar_iter_destroy(argpar_iter);
2243 argpar_iter = argpar_iter_create(my_argc, my_argv,
4624dad0 2244 add_trigger_options);
d50d200a
SM
2245 if (!argpar_iter) {
2246 ERR("Failed to create argpar iter.");
4624dad0
SM
2247 goto error;
2248 }
2249
35c4b2b3 2250 status = parse_next_item(argpar_iter, &argpar_item,
ef9ff9cb 2251 argc - my_argc, my_argv, true, &argpar_error, NULL);
d50d200a 2252 if (status == PARSE_NEXT_ITEM_STATUS_ERROR) {
ef9ff9cb
SM
2253
2254 if (argpar_error_type(argpar_error) ==
2255 ARGPAR_ERROR_TYPE_MISSING_OPT_ARG) {
2256 int opt_id = argpar_error_opt_descr(argpar_error, NULL)->id;
2257
2258 if (opt_id == OPT_CONDITION) {
2259 print_valid_condition_names();
2260 } else if (opt_id == OPT_ACTION) {
2261 print_valid_action_names();
2262 }
2263 }
2264
2265 goto error;
2266 } else if (status == PARSE_NEXT_ITEM_STATUS_ERROR_MEMORY) {
4624dad0 2267 goto error;
d50d200a 2268 } else if (status == PARSE_NEXT_ITEM_STATUS_END) {
4624dad0
SM
2269 break;
2270 }
2271
d50d200a 2272 LTTNG_ASSERT(status == PARSE_NEXT_ITEM_STATUS_OK);
4624dad0 2273
d50d200a
SM
2274 if (argpar_item_type(argpar_item) == ARGPAR_ITEM_TYPE_NON_OPT) {
2275 ERR("Unexpected argument `%s`.",
2276 argpar_item_non_opt_arg(argpar_item));
4624dad0
SM
2277 goto error;
2278 }
2279
d50d200a 2280 ingested_args = argpar_iter_ingested_orig_args(argpar_iter);
4624dad0
SM
2281
2282 my_argc -= ingested_args;
2283 my_argv += ingested_args;
2284
d50d200a
SM
2285 descr = argpar_item_opt_descr(argpar_item);
2286 arg = argpar_item_opt_arg(argpar_item);
2287
2288 switch (descr->id) {
4624dad0
SM
2289 case OPT_HELP:
2290 SHOW_HELP();
2291 ret = 0;
2292 goto end;
2293 case OPT_LIST_OPTIONS:
2294 list_cmd_options_argpar(stdout, add_trigger_options);
2295 ret = 0;
2296 goto end;
2297 case OPT_CONDITION:
2298 {
2299 if (condition) {
2300 ERR("A --condition was already given.");
2301 goto error;
2302 }
2303
35c4b2b3 2304 condition = parse_condition(arg, &my_argc, &my_argv,
e776cf47
SM
2305 argc - my_argc, argc - my_argc - ingested_args,
2306 my_argv[-ingested_args]);
4624dad0
SM
2307 if (!condition) {
2308 /*
2309 * An error message was already printed by
2310 * parse_condition.
2311 */
2312 goto error;
2313 }
2314
2315 break;
2316 }
2317 case OPT_ACTION:
2318 {
35c4b2b3 2319 action = parse_action(arg, &my_argc, &my_argv,
e776cf47
SM
2320 argc - my_argc, argc - my_argc - ingested_args,
2321 my_argv[-ingested_args]);
4624dad0
SM
2322 if (!action) {
2323 /*
2324 * An error message was already printed by
2325 * parse_condition.
2326 */
2327 goto error;
2328 }
2329
2330 ret = lttng_dynamic_pointer_array_add_pointer(
2331 &actions, action);
2332 if (ret) {
2333 ERR("Failed to add pointer to pointer array.");
2334 goto error;
2335 }
2336
a8940c5e 2337 /* Ownership of the action was transferred to the list. */
4624dad0
SM
2338 action = NULL;
2339
2340 break;
2341 }
1d4b59f2 2342 case OPT_NAME:
4624dad0 2343 {
d50d200a 2344 if (!assign_string(&name, arg, "--name")) {
4624dad0
SM
2345 goto error;
2346 }
2347
2348 break;
2349 }
481c5310 2350 case OPT_OWNER_UID:
4624dad0 2351 {
d50d200a 2352 if (!assign_string(&owner_uid, arg,
481c5310 2353 "--owner-uid")) {
4624dad0
SM
2354 goto error;
2355 }
2356
2357 break;
2358 }
2359 default:
2360 abort();
2361 }
2362 }
2363
2364 if (!condition) {
2365 ERR("Missing --condition.");
2366 goto error;
2367 }
2368
2369 if (lttng_dynamic_pointer_array_get_count(&actions) == 0) {
2370 ERR("Need at least one --action.");
2371 goto error;
2372 }
2373
702f26c8
JR
2374 action_list = lttng_action_list_create();
2375 if (!action_list) {
4624dad0
SM
2376 goto error;
2377 }
2378
2379 for (i = 0; i < lttng_dynamic_pointer_array_get_count(&actions); i++) {
2380 enum lttng_action_status status;
2381
48a40005 2382 action = (lttng_action *) lttng_dynamic_pointer_array_steal_pointer(&actions, i);
4624dad0 2383
702f26c8 2384 status = lttng_action_list_add_action(action_list, action);
4624dad0
SM
2385 if (status != LTTNG_ACTION_STATUS_OK) {
2386 goto error;
2387 }
2388
2389 /*
702f26c8 2390 * The `lttng_action_list_add_action()` takes a reference to
4624dad0
SM
2391 * the action. We can destroy ours.
2392 */
2393 lttng_action_destroy(action);
2394 action = NULL;
2395 }
2396
702f26c8 2397 trigger = lttng_trigger_create(condition, action_list);
4624dad0
SM
2398 if (!trigger) {
2399 goto error;
2400 }
2401
481c5310 2402 if (owner_uid) {
4624dad0
SM
2403 enum lttng_trigger_status trigger_status;
2404 char *end;
2405 long long uid;
2406
2407 errno = 0;
481c5310
SM
2408 uid = strtol(owner_uid, &end, 10);
2409 if (end == owner_uid || *end != '\0' || errno != 0) {
2410 ERR("Failed to parse `%s` as a user id.", owner_uid);
c6c76a9d 2411 goto error;
4624dad0
SM
2412 }
2413
2414 trigger_status = lttng_trigger_set_owner_uid(trigger, uid);
2415 if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
2416 ERR("Failed to set trigger's user identity.");
2417 goto error;
2418 }
2419 }
2420
a5c2d2a7
JG
2421 if (name) {
2422 ret_code = lttng_register_trigger_with_name(trigger, name);
2423 } else {
2424 ret_code = lttng_register_trigger_with_automatic_name(trigger);
2425 }
2426
2427 if (ret_code != LTTNG_OK) {
2428 ERR("Failed to register trigger: %s.",
2429 lttng_strerror(-ret_code));
4624dad0
SM
2430 goto error;
2431 }
2432
523c4f8c
JR
2433 if (lttng_opt_mi) {
2434 ret_code = lttng_trigger_mi_serialize(trigger, mi_writer, NULL);
2435 if (ret_code != LTTNG_OK) {
2436 goto error;
2437 }
f674361c
JG
2438 } else {
2439 const char *returned_trigger_name;
2440 const enum lttng_trigger_status trigger_status =
2441 lttng_trigger_get_name(trigger,
2442 &returned_trigger_name);
2443
2444 if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
2445 WARN("Failed to retrieve the added trigger's name.");
2446 } else {
2447 MSG("Added trigger `%s`.", returned_trigger_name);
2448 }
523c4f8c
JR
2449 }
2450
a5c2d2a7 2451 ret = 0;
4624dad0
SM
2452
2453 goto end;
2454
2455error:
2456 ret = 1;
2457
2458end:
523c4f8c 2459 /* Mi closing. */
4627fa54 2460 if (lttng_opt_mi && mi_writer) {
523c4f8c
JR
2461 int mi_ret;
2462
2463 /* Close output element. */
2464 mi_ret = mi_lttng_writer_close_element(mi_writer);
2465 if (mi_ret) {
2466 ret = 1;
2467 goto cleanup;
2468 }
2469
2470 mi_ret = mi_lttng_writer_write_element_bool(mi_writer,
2471 mi_lttng_element_command_success, ret ? 0 : 1);
2472 if (mi_ret) {
2473 ret = 1;
2474 goto cleanup;
2475 }
2476
2477 /* Command element close. */
2478 mi_ret = mi_lttng_writer_command_close(mi_writer);
2479 if (mi_ret) {
2480 ret = 1;
2481 goto cleanup;
2482 }
2483 }
2484
2485cleanup:
ef9ff9cb 2486 argpar_error_destroy(argpar_error);
d50d200a 2487 argpar_iter_destroy(argpar_iter);
4624dad0
SM
2488 argpar_item_destroy(argpar_item);
2489 lttng_dynamic_pointer_array_reset(&actions);
2490 lttng_condition_destroy(condition);
702f26c8 2491 lttng_action_destroy(action_list);
4624dad0
SM
2492 lttng_action_destroy(action);
2493 lttng_trigger_destroy(trigger);
1d4b59f2 2494 free(name);
481c5310 2495 free(owner_uid);
523c4f8c
JR
2496 if (mi_writer && mi_lttng_writer_destroy(mi_writer)) {
2497 /* Preserve original error code. */
2498 ret = ret ? ret : CMD_ERROR;
2499 }
2500
4624dad0
SM
2501 return ret;
2502}
This page took 0.166266 seconds and 4 git commands to generate.