lttng: Add list-triggers command
[lttng-tools.git] / src / bin / lttng / commands / list_triggers.c
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 <stdio.h>
9
10 #include "../command.h"
11
12 #include "common/argpar/argpar.h"
13 #include "common/dynamic-array.h"
14 #include "common/mi-lttng.h"
15 /* For lttng_condition_type_str(). */
16 #include "lttng/condition/condition-internal.h"
17 /* For lttng_domain_type_str(). */
18 #include "lttng/domain-internal.h"
19
20 #ifdef LTTNG_EMBED_HELP
21 static const char help_msg[] =
22 #include <lttng-list-trigger.1.h>
23 ;
24 #endif
25
26 enum {
27 OPT_HELP,
28 OPT_LIST_OPTIONS,
29 };
30
31 static const
32 struct argpar_opt_descr list_trigger_options[] = {
33 { OPT_HELP, 'h', "help", false },
34 { OPT_LIST_OPTIONS, '\0', "list-options", false },
35 ARGPAR_OPT_DESCR_SENTINEL,
36 };
37
38 static
39 void print_event_rule_tracepoint(const struct lttng_event_rule *event_rule)
40 {
41 enum lttng_event_rule_status event_rule_status;
42 enum lttng_domain_type domain_type;
43 const char *pattern;
44 const char *filter;
45 int log_level;
46 unsigned int exclusions_count;
47 int i;
48
49 event_rule_status = lttng_event_rule_tracepoint_get_pattern(
50 event_rule, &pattern);
51 assert(event_rule_status == LTTNG_EVENT_RULE_STATUS_OK);
52
53 event_rule_status = lttng_event_rule_tracepoint_get_domain_type(
54 event_rule, &domain_type);
55 assert(event_rule_status == LTTNG_EVENT_RULE_STATUS_OK);
56
57 _MSG(" rule: %s (type: tracepoint, domain: %s", pattern,
58 lttng_domain_type_str(domain_type));
59
60 event_rule_status = lttng_event_rule_tracepoint_get_filter(
61 event_rule, &filter);
62 if (event_rule_status == LTTNG_EVENT_RULE_STATUS_OK) {
63 _MSG(", filter: %s", filter);
64 } else {
65 assert(event_rule_status == LTTNG_EVENT_RULE_STATUS_UNSET);
66 }
67
68 event_rule_status = lttng_event_rule_tracepoint_get_log_level(
69 event_rule, &log_level);
70 if (event_rule_status == LTTNG_EVENT_RULE_STATUS_OK) {
71 enum lttng_loglevel_type log_level_type;
72 const char *log_level_op;
73
74 event_rule_status = lttng_event_rule_tracepoint_get_log_level_type(
75 event_rule, &log_level_type);
76 assert(event_rule_status == LTTNG_EVENT_RULE_STATUS_OK);
77 assert(log_level_type == LTTNG_EVENT_LOGLEVEL_RANGE ||
78 log_level_type == LTTNG_EVENT_LOGLEVEL_SINGLE);
79
80 log_level_op = (log_level_type == LTTNG_EVENT_LOGLEVEL_RANGE ? "<=" : "==");
81
82 _MSG(", log level %s %s", log_level_op,
83 mi_lttng_loglevel_string(
84 log_level, domain_type));
85 } else {
86 assert(event_rule_status == LTTNG_EVENT_RULE_STATUS_UNSET);
87 }
88
89 event_rule_status = lttng_event_rule_tracepoint_get_exclusions_count(
90 event_rule, &exclusions_count);
91 assert(event_rule_status == LTTNG_EVENT_RULE_STATUS_OK);
92 if (exclusions_count > 0) {
93 _MSG(", exclusions: ");
94 for (i = 0; i < exclusions_count; i++) {
95 const char *exclusion;
96
97 event_rule_status = lttng_event_rule_tracepoint_get_exclusion_at_index(
98 event_rule, i, &exclusion);
99 assert(event_rule_status == LTTNG_EVENT_RULE_STATUS_OK);
100
101 _MSG("%s%s", i > 0 ? "," : "", exclusion);
102 }
103 }
104
105 MSG(")");
106 }
107
108 static void print_kernel_probe_location(
109 const struct lttng_kernel_probe_location *location)
110 {
111 enum lttng_kernel_probe_location_status status;
112 switch (lttng_kernel_probe_location_get_type(location)) {
113 case LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS:
114 {
115 uint64_t address;
116
117 status = lttng_kernel_probe_location_address_get_address(
118 location, &address);
119 if (status != LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK) {
120 ERR("Getting kernel probe location address failed.");
121 goto end;
122 }
123
124 _MSG("0x%" PRIx64, address);
125
126 break;
127 }
128 case LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET:
129 {
130 uint64_t offset;
131 const char *symbol_name;
132
133 symbol_name = lttng_kernel_probe_location_symbol_get_name(
134 location);
135 if (!symbol_name) {
136 ERR("Getting kernel probe location symbol name failed.");
137 goto end;
138 }
139
140 status = lttng_kernel_probe_location_symbol_get_offset(
141 location, &offset);
142 if (status != LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK) {
143 ERR("Getting kernel probe location address failed.");
144 goto end;
145 }
146
147 if (offset == 0) {
148 _MSG("%s", symbol_name);
149 } else {
150 _MSG("%s+0x%" PRIx64, symbol_name, offset);
151 }
152
153 break;
154 }
155 default:
156 abort();
157 };
158 end:
159 return;
160 }
161
162 static
163 void print_event_rule_kprobe(const struct lttng_event_rule *event_rule)
164 {
165 enum lttng_event_rule_status event_rule_status;
166 const char *name;
167 const struct lttng_kernel_probe_location *location;
168
169 assert(lttng_event_rule_get_type(event_rule) == LTTNG_EVENT_RULE_TYPE_KPROBE);
170
171 event_rule_status = lttng_event_rule_kprobe_get_name(event_rule, &name);
172 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
173 ERR("Failed to get kprobe event rule's name.");
174 goto end;
175 }
176
177 event_rule_status = lttng_event_rule_kprobe_get_location(
178 event_rule, &location);
179 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
180 ERR("Failed to get kprobe event rule's location.");
181 goto end;
182 }
183
184 _MSG(" rule: %s (type: probe, location: ", name);
185
186 print_kernel_probe_location(location);
187
188 MSG(")");
189
190 end:
191 return;
192 }
193
194 static
195 void print_event_rule_uprobe(const struct lttng_event_rule *event_rule)
196 {
197 enum lttng_event_rule_status event_rule_status;
198 const char *name;
199 const struct lttng_userspace_probe_location *location;
200 enum lttng_userspace_probe_location_type userspace_probe_location_type;
201
202 assert(lttng_event_rule_get_type(event_rule) == LTTNG_EVENT_RULE_TYPE_UPROBE);
203
204 event_rule_status = lttng_event_rule_uprobe_get_name(event_rule, &name);
205 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
206 ERR("Failed to get uprobe event rule's name.");
207 goto end;
208 }
209
210 event_rule_status = lttng_event_rule_uprobe_get_location(
211 event_rule, &location);
212 if (event_rule_status != LTTNG_EVENT_RULE_STATUS_OK) {
213 ERR("Failed to get uprobe event rule's location.");
214 goto end;
215 }
216
217 _MSG(" rule: %s (type: userspace probe, location: ", name);
218
219 userspace_probe_location_type =
220 lttng_userspace_probe_location_get_type(location);
221
222 switch (userspace_probe_location_type) {
223 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION:
224 {
225 const char *binary_path, *function_name;
226
227 binary_path = lttng_userspace_probe_location_function_get_binary_path(
228 location);
229 function_name = lttng_userspace_probe_location_function_get_function_name(
230 location);
231
232 _MSG("%s:%s", binary_path, function_name);
233 break;
234 }
235 case LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT:
236 _MSG("SDT not implemented yet");
237 break;
238 default:
239 abort();
240 }
241
242 MSG(")");
243
244 end:
245 return;
246 }
247
248 static
249 void print_event_rule_syscall(const struct lttng_event_rule *event_rule)
250 {
251 const char *pattern, *filter;
252 enum lttng_event_rule_status event_rule_status;
253
254 assert(lttng_event_rule_get_type(event_rule) == LTTNG_EVENT_RULE_TYPE_SYSCALL);
255
256 event_rule_status = lttng_event_rule_syscall_get_pattern(
257 event_rule, &pattern);
258 assert(event_rule_status == LTTNG_EVENT_RULE_STATUS_OK);
259
260 _MSG(" rule: %s (type: syscall", pattern);
261
262 event_rule_status = lttng_event_rule_syscall_get_filter(
263 event_rule, &filter);
264 if (event_rule_status == LTTNG_EVENT_RULE_STATUS_OK) {
265 _MSG(", filter: %s", filter);
266 } else {
267 assert(event_rule_status == LTTNG_EVENT_RULE_STATUS_UNSET);
268 }
269
270 MSG(")");
271 }
272
273 static
274 void print_event_rule(const struct lttng_event_rule *event_rule)
275 {
276 const enum lttng_event_rule_type event_rule_type =
277 lttng_event_rule_get_type(event_rule);
278
279 switch (event_rule_type) {
280 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT:
281 print_event_rule_tracepoint(event_rule);
282 break;
283 case LTTNG_EVENT_RULE_TYPE_KPROBE:
284 print_event_rule_kprobe(event_rule);
285 break;
286 case LTTNG_EVENT_RULE_TYPE_UPROBE:
287 print_event_rule_uprobe(event_rule);
288 break;
289 case LTTNG_EVENT_RULE_TYPE_SYSCALL:
290 print_event_rule_syscall(event_rule);
291 break;
292 default:
293 abort();
294 }
295 }
296
297 static
298 void print_condition_event_rule_hit(const struct lttng_condition *condition)
299 {
300 const struct lttng_event_rule *event_rule;
301 enum lttng_condition_status condition_status;
302
303 condition_status =
304 lttng_condition_event_rule_get_rule(condition, &event_rule);
305 assert(condition_status == LTTNG_CONDITION_STATUS_OK);
306
307 print_event_rule(event_rule);
308 }
309
310 static
311 void print_one_action(const struct lttng_action *action)
312 {
313 enum lttng_action_type action_type;
314 enum lttng_action_status action_status;
315 const char *value;
316
317 action_type = lttng_action_get_type(action);
318 assert(action_type != LTTNG_ACTION_TYPE_GROUP);
319
320 switch (action_type) {
321 case LTTNG_ACTION_TYPE_NOTIFY:
322 MSG("notify");
323 break;
324 case LTTNG_ACTION_TYPE_START_SESSION:
325 action_status = lttng_action_start_session_get_session_name(
326 action, &value);
327 assert(action_status == LTTNG_ACTION_STATUS_OK);
328 MSG("start session `%s`", value);
329 break;
330 case LTTNG_ACTION_TYPE_STOP_SESSION:
331 action_status = lttng_action_stop_session_get_session_name(
332 action, &value);
333 assert(action_status == LTTNG_ACTION_STATUS_OK);
334 MSG("stop session `%s`", value);
335 break;
336 case LTTNG_ACTION_TYPE_ROTATE_SESSION:
337 action_status = lttng_action_rotate_session_get_session_name(
338 action, &value);
339 assert(action_status == LTTNG_ACTION_STATUS_OK);
340 MSG("rotate session `%s`", value);
341 break;
342 case LTTNG_ACTION_TYPE_SNAPSHOT_SESSION:
343 {
344 const struct lttng_snapshot_output *output;
345
346 action_status = lttng_action_snapshot_session_get_session_name(
347 action, &value);
348 assert(action_status == LTTNG_ACTION_STATUS_OK);
349 _MSG("snapshot session `%s`", value);
350
351 action_status = lttng_action_snapshot_session_get_output(
352 action, &output);
353 if (action_status == LTTNG_ACTION_STATUS_OK) {
354 const char *name;
355 uint64_t max_size;
356 const char *ctrl_url, *data_url;
357 bool starts_with_file, starts_with_net, starts_with_net6;
358
359 ctrl_url = lttng_snapshot_output_get_ctrl_url(output);
360 assert(ctrl_url && strlen(ctrl_url) > 0);
361
362 data_url = lttng_snapshot_output_get_data_url(output);
363 assert(data_url);
364
365 starts_with_file = strncmp(ctrl_url, "file://", strlen("file://")) == 0;
366 starts_with_net = strncmp(ctrl_url, "net://", strlen("net://")) == 0;
367 starts_with_net6 = strncmp(ctrl_url, "net6://", strlen("net6://")) == 0;
368
369 if (ctrl_url[0] == '/' || starts_with_file) {
370 if (starts_with_file) {
371 ctrl_url += strlen("file://");
372 }
373
374 _MSG(", path: %s", ctrl_url);
375 } else if (starts_with_net || starts_with_net6) {
376 _MSG(", url: %s", ctrl_url);
377 } else {
378 assert(strlen(data_url) > 0);
379
380 _MSG(", control url: %s, data url: %s", ctrl_url, data_url);
381 }
382
383 name = lttng_snapshot_output_get_name(output);
384 assert(name);
385 if (strlen(name) > 0) {
386 _MSG(", name: %s", name);
387 }
388
389 max_size = lttng_snapshot_output_get_maxsize(output);
390 if (max_size != -1ULL) {
391 _MSG(", max size: %" PRIu64, max_size);
392 }
393 }
394
395 MSG("");
396 break;
397 }
398
399 default:
400 abort();
401 }
402 }
403
404 static
405 void print_one_trigger(const struct lttng_trigger *trigger)
406 {
407 const struct lttng_condition *condition;
408 enum lttng_condition_type condition_type;
409 const struct lttng_action *action;
410 enum lttng_action_type action_type;
411 enum lttng_trigger_status trigger_status;
412 const char *name;
413 enum lttng_trigger_firing_policy firing_policy_type;
414 uint64_t threshold;
415 uid_t trigger_uid;
416
417 trigger_status = lttng_trigger_get_name(trigger, &name);
418 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
419
420 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_uid);
421 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
422
423 MSG("- id: %s", name);
424 MSG(" user id: %d", trigger_uid);
425
426 trigger_status = lttng_trigger_get_firing_policy(
427 trigger, &firing_policy_type, &threshold);
428 if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
429 ERR("Failed to get trigger's policy.");
430 goto end;
431 }
432
433 switch (firing_policy_type) {
434 case LTTNG_TRIGGER_FIRING_POLICY_EVERY_N:
435 if (threshold > 1) {
436 MSG(" firing policy: after every %" PRIu64 " occurences", threshold);
437 }
438 break;
439 case LTTNG_TRIGGER_FIRING_POLICY_ONCE_AFTER_N:
440 MSG(" firing policy: once after %" PRIu64 " occurences", threshold);
441 break;
442 default:
443 abort();
444 }
445
446 condition = lttng_trigger_get_const_condition(trigger);
447 condition_type = lttng_condition_get_type(condition);
448 MSG(" condition: %s", lttng_condition_type_str(condition_type));
449 switch (condition_type) {
450 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT:
451 print_condition_event_rule_hit(condition);
452 break;
453 default:
454 MSG(" (condition type not handled in %s)", __func__);
455 break;
456 }
457
458 action = lttng_trigger_get_const_action(trigger);
459 action_type = lttng_action_get_type(action);
460 if (action_type == LTTNG_ACTION_TYPE_GROUP) {
461 unsigned int count, i;
462 enum lttng_action_status action_status;
463
464 MSG(" actions:");
465
466 action_status = lttng_action_group_get_count(action, &count);
467 assert(action_status == LTTNG_ACTION_STATUS_OK);
468
469 for (i = 0; i < count; i++) {
470 const struct lttng_action *subaction =
471 lttng_action_group_get_at_index(
472 action, i);
473
474 _MSG(" ");
475 print_one_action(subaction);
476 }
477 } else {
478 _MSG(" action:");
479 print_one_action(action);
480 }
481
482 end:
483 return;
484 }
485
486 static
487 int compare_triggers_by_name(const void *a, const void *b)
488 {
489 const struct lttng_trigger *trigger_a = *((const struct lttng_trigger **) a);
490 const struct lttng_trigger *trigger_b = *((const struct lttng_trigger **) b);
491 const char *name_a, *name_b;
492 enum lttng_trigger_status trigger_status;
493
494 trigger_status = lttng_trigger_get_name(trigger_a, &name_a);
495 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
496
497 trigger_status = lttng_trigger_get_name(trigger_b, &name_b);
498 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
499
500 return strcmp(name_a, name_b);
501 }
502
503 int cmd_list_triggers(int argc, const char **argv)
504 {
505 int ret;
506 struct argpar_parse_ret argpar_parse_ret = {};
507 struct lttng_triggers *triggers = NULL;
508 int i;
509 struct lttng_dynamic_pointer_array sorted_triggers;
510 enum lttng_trigger_status trigger_status;
511 unsigned int num_triggers;
512
513 lttng_dynamic_pointer_array_init(&sorted_triggers, NULL);
514
515 argpar_parse_ret = argpar_parse(
516 argc - 1, argv + 1, list_trigger_options, true);
517 if (!argpar_parse_ret.items) {
518 ERR("%s", argpar_parse_ret.error);
519 goto error;
520 }
521
522 for (i = 0; i < argpar_parse_ret.items->n_items; i++) {
523 const struct argpar_item *item =
524 argpar_parse_ret.items->items[i];
525
526 if (item->type == ARGPAR_ITEM_TYPE_OPT) {
527 const struct argpar_item_opt *item_opt =
528 (const struct argpar_item_opt *) item;
529
530 switch (item_opt->descr->id) {
531 case OPT_HELP:
532 SHOW_HELP();
533 ret = 0;
534 goto end;
535
536 case OPT_LIST_OPTIONS:
537 list_cmd_options_argpar(stdout,
538 list_trigger_options);
539 ret = 0;
540 goto end;
541
542 default:
543 abort();
544 }
545
546 } else {
547 const struct argpar_item_non_opt *item_non_opt =
548 (const struct argpar_item_non_opt *) item;
549
550 ERR("Unexpected argument: %s", item_non_opt->arg);
551 }
552 }
553
554 ret = lttng_list_triggers(&triggers);
555 if (ret != LTTNG_OK) {
556 ERR("Error listing triggers: %s.", lttng_strerror(-ret));
557 goto error;
558 }
559
560 trigger_status = lttng_triggers_get_count(triggers, &num_triggers);
561 if (trigger_status != LTTNG_TRIGGER_STATUS_OK) {
562 ERR("Failed to get trigger count.");
563 goto error;
564 }
565
566 for (i = 0; i < num_triggers; i++) {
567 const int add_ret = lttng_dynamic_pointer_array_add_pointer(
568 &sorted_triggers,
569 (void *) lttng_triggers_get_at_index(triggers, i));
570
571 if (add_ret) {
572 ERR("Failed to allocate array of struct lttng_trigger *.");
573 goto error;
574 }
575 }
576
577 qsort(sorted_triggers.array.buffer.data, num_triggers,
578 sizeof(struct lttng_trigger *),
579 compare_triggers_by_name);
580
581 for (i = 0; i < num_triggers; i++) {
582 const struct lttng_trigger *trigger_to_print =
583 (const struct lttng_trigger *)
584 lttng_dynamic_pointer_array_get_pointer(
585 &sorted_triggers, i);
586
587 print_one_trigger(trigger_to_print);
588 }
589
590 ret = 0;
591 goto end;
592
593 error:
594 ret = 1;
595
596 end:
597 argpar_parse_ret_fini(&argpar_parse_ret);
598 lttng_triggers_destroy(triggers);
599 lttng_dynamic_pointer_array_reset(&sorted_triggers);
600
601 return ret;
602 }
This page took 0.071488 seconds and 4 git commands to generate.