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