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