cppcheck: don't check NULL pointer before freeing them
[lttng-tools.git] / src / bin / lttng / commands / list.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <inttypes.h>
20 #include <popt.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
25
26 #include "../command.h"
27
28 static int opt_userspace;
29 static int opt_kernel;
30 static char *opt_channel;
31 static int opt_domain;
32 static int opt_fields;
33 #if 0
34 /* Not implemented yet */
35 static char *opt_cmd_name;
36 static pid_t opt_pid;
37 #endif
38
39 const char *indent4 = " ";
40 const char *indent6 = " ";
41 const char *indent8 = " ";
42
43 enum {
44 OPT_HELP = 1,
45 OPT_USERSPACE,
46 OPT_LIST_OPTIONS,
47 };
48
49 static struct lttng_handle *handle;
50
51 static struct poptOption long_options[] = {
52 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
53 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
54 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
55 #if 0
56 /* Not implemented yet */
57 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
58 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
59 #else
60 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
61 #endif
62 {"channel", 'c', POPT_ARG_STRING, &opt_channel, 0, 0, 0},
63 {"domain", 'd', POPT_ARG_VAL, &opt_domain, 1, 0, 0},
64 {"fields", 'f', POPT_ARG_VAL, &opt_fields, 1, 0, 0},
65 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
66 {0, 0, 0, 0, 0, 0, 0}
67 };
68
69 /*
70 * usage
71 */
72 static void usage(FILE *ofp)
73 {
74 fprintf(ofp, "usage: lttng list [OPTIONS] [SESSION [<OPTIONS>]]\n");
75 fprintf(ofp, "\n");
76 fprintf(ofp, "With no arguments, list available tracing session(s)\n");
77 fprintf(ofp, "\n");
78 fprintf(ofp, "Without a session, -k lists available kernel events\n");
79 fprintf(ofp, "Without a session, -u lists available userspace events\n");
80 fprintf(ofp, "\n");
81 fprintf(ofp, " -h, --help Show this help\n");
82 fprintf(ofp, " --list-options Simple listing of options\n");
83 fprintf(ofp, " -k, --kernel Select kernel domain\n");
84 fprintf(ofp, " -u, --userspace Select user-space domain.\n");
85 fprintf(ofp, " -f, --fields List event fields.\n");
86 #if 0
87 fprintf(ofp, " -p, --pid PID List user-space events by PID\n");
88 #endif
89 fprintf(ofp, "\n");
90 fprintf(ofp, "Session Options:\n");
91 fprintf(ofp, " -c, --channel NAME List details of a channel\n");
92 fprintf(ofp, " -d, --domain List available domain(s)\n");
93 fprintf(ofp, "\n");
94 }
95
96 /*
97 * Get command line from /proc for a specific pid.
98 *
99 * On success, return an allocated string pointer to the proc cmdline.
100 * On error, return NULL.
101 */
102 static char *get_cmdline_by_pid(pid_t pid)
103 {
104 int ret;
105 FILE *fp;
106 char *cmdline = NULL;
107 char path[24]; /* Can't go bigger than /proc/65535/cmdline */
108
109 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
110 fp = fopen(path, "r");
111 if (fp == NULL) {
112 goto end;
113 }
114
115 /* Caller must free() *cmdline */
116 cmdline = malloc(PATH_MAX);
117 ret = fread(cmdline, 1, PATH_MAX, fp);
118 if (ret < 0) {
119 perror("fread proc list");
120 }
121 fclose(fp);
122
123 end:
124 return cmdline;
125 }
126
127 static
128 const char *active_string(int value)
129 {
130 switch (value) {
131 case 0: return " [inactive]";
132 case 1: return " [active]";
133 case -1: return "";
134 default: return NULL;
135 }
136 }
137
138 static
139 const char *enabled_string(int value)
140 {
141 switch (value) {
142 case 0: return " [disabled]";
143 case 1: return " [enabled]";
144 case -1: return "";
145 default: return NULL;
146 }
147 }
148
149 static
150 const char *filter_string(int value)
151 {
152 switch (value) {
153 case 1: return " [with filter]";
154 default: return "";
155 }
156 }
157
158 static const char *loglevel_string(int value)
159 {
160 switch (value) {
161 case -1:
162 return "";
163 case LTTNG_LOGLEVEL_EMERG:
164 return "TRACE_EMERG";
165 case LTTNG_LOGLEVEL_ALERT:
166 return "TRACE_ALERT";
167 case LTTNG_LOGLEVEL_CRIT:
168 return "TRACE_CRIT";
169 case LTTNG_LOGLEVEL_ERR:
170 return "TRACE_ERR";
171 case LTTNG_LOGLEVEL_WARNING:
172 return "TRACE_WARNING";
173 case LTTNG_LOGLEVEL_NOTICE:
174 return "TRACE_NOTICE";
175 case LTTNG_LOGLEVEL_INFO:
176 return "TRACE_INFO";
177 case LTTNG_LOGLEVEL_DEBUG_SYSTEM:
178 return "TRACE_DEBUG_SYSTEM";
179 case LTTNG_LOGLEVEL_DEBUG_PROGRAM:
180 return "TRACE_DEBUG_PROGRAM";
181 case LTTNG_LOGLEVEL_DEBUG_PROCESS:
182 return "TRACE_DEBUG_PROCESS";
183 case LTTNG_LOGLEVEL_DEBUG_MODULE:
184 return "TRACE_DEBUG_MODULE";
185 case LTTNG_LOGLEVEL_DEBUG_UNIT:
186 return "TRACE_DEBUG_UNIT";
187 case LTTNG_LOGLEVEL_DEBUG_FUNCTION:
188 return "TRACE_DEBUG_FUNCTION";
189 case LTTNG_LOGLEVEL_DEBUG_LINE:
190 return "TRACE_DEBUG_LINE";
191 case LTTNG_LOGLEVEL_DEBUG:
192 return "TRACE_DEBUG";
193 default:
194 return "<<UNKNOWN>>";
195 }
196 }
197
198 /*
199 * Pretty print single event.
200 */
201 static void print_events(struct lttng_event *event)
202 {
203 switch (event->type) {
204 case LTTNG_EVENT_TRACEPOINT:
205 {
206 if (event->loglevel != -1) {
207 MSG("%s%s (loglevel: %s (%d)) (type: tracepoint)%s%s",
208 indent6,
209 event->name,
210 loglevel_string(event->loglevel),
211 event->loglevel,
212 enabled_string(event->enabled),
213 filter_string(event->filter));
214 } else {
215 MSG("%s%s (type: tracepoint)%s%s",
216 indent6,
217 event->name,
218 enabled_string(event->enabled),
219 filter_string(event->filter));
220 }
221 break;
222 }
223 case LTTNG_EVENT_PROBE:
224 MSG("%s%s (type: probe)%s%s", indent6,
225 event->name, enabled_string(event->enabled),
226 filter_string(event->filter));
227 if (event->attr.probe.addr != 0) {
228 MSG("%saddr: 0x%" PRIx64, indent8, event->attr.probe.addr);
229 } else {
230 MSG("%soffset: 0x%" PRIx64, indent8, event->attr.probe.offset);
231 MSG("%ssymbol: %s", indent8, event->attr.probe.symbol_name);
232 }
233 break;
234 case LTTNG_EVENT_FUNCTION:
235 case LTTNG_EVENT_FUNCTION_ENTRY:
236 MSG("%s%s (type: function)%s%s", indent6,
237 event->name, enabled_string(event->enabled),
238 filter_string(event->filter));
239 MSG("%ssymbol: \"%s\"", indent8, event->attr.ftrace.symbol_name);
240 break;
241 case LTTNG_EVENT_SYSCALL:
242 MSG("%ssyscalls (type: syscall)%s%s", indent6,
243 enabled_string(event->enabled),
244 filter_string(event->filter));
245 break;
246 case LTTNG_EVENT_NOOP:
247 MSG("%s (type: noop)%s%s", indent6,
248 enabled_string(event->enabled),
249 filter_string(event->filter));
250 break;
251 case LTTNG_EVENT_ALL:
252 /* We should never have "all" events in list. */
253 assert(0);
254 break;
255 }
256 }
257
258 static const char *field_type(struct lttng_event_field *field)
259 {
260 switch(field->type) {
261 case LTTNG_EVENT_FIELD_INTEGER:
262 return "integer";
263 case LTTNG_EVENT_FIELD_ENUM:
264 return "enum";
265 case LTTNG_EVENT_FIELD_FLOAT:
266 return "float";
267 case LTTNG_EVENT_FIELD_STRING:
268 return "string";
269 case LTTNG_EVENT_FIELD_OTHER:
270 default: /* fall-through */
271 return "unknown";
272 }
273 }
274
275 /*
276 * Pretty print single event fields.
277 */
278 static void print_event_field(struct lttng_event_field *field)
279 {
280 if (!field->field_name[0]) {
281 return;
282 }
283 MSG("%sfield: %s (%s)%s", indent8, field->field_name,
284 field_type(field), field->nowrite ? " [no write]" : "");
285 }
286
287 /*
288 * Ask session daemon for all user space tracepoints available.
289 */
290 static int list_ust_events(void)
291 {
292 int i, size;
293 struct lttng_domain domain;
294 struct lttng_handle *handle;
295 struct lttng_event *event_list;
296 pid_t cur_pid = 0;
297
298 memset(&domain, 0, sizeof(domain));
299
300 DBG("Getting UST tracing events");
301
302 domain.type = LTTNG_DOMAIN_UST;
303
304 handle = lttng_create_handle(NULL, &domain);
305 if (handle == NULL) {
306 goto error;
307 }
308
309 size = lttng_list_tracepoints(handle, &event_list);
310 if (size < 0) {
311 ERR("Unable to list UST events");
312 lttng_destroy_handle(handle);
313 return size;
314 }
315
316 MSG("UST events:\n-------------");
317
318 if (size == 0) {
319 MSG("None");
320 }
321
322 for (i = 0; i < size; i++) {
323 if (cur_pid != event_list[i].pid) {
324 cur_pid = event_list[i].pid;
325 MSG("\nPID: %d - Name: %s", cur_pid, get_cmdline_by_pid(cur_pid));
326 }
327 print_events(&event_list[i]);
328 }
329
330 MSG("");
331
332 free(event_list);
333 lttng_destroy_handle(handle);
334
335 return CMD_SUCCESS;
336
337 error:
338 lttng_destroy_handle(handle);
339 return -1;
340 }
341
342 /*
343 * Ask session daemon for all user space tracepoint fields available.
344 */
345 static int list_ust_event_fields(void)
346 {
347 int i, size;
348 struct lttng_domain domain;
349 struct lttng_handle *handle;
350 struct lttng_event_field *event_field_list;
351 pid_t cur_pid = 0;
352 struct lttng_event cur_event;
353
354 memset(&domain, 0, sizeof(domain));
355 memset(&cur_event, 0, sizeof(cur_event));
356
357 DBG("Getting UST tracing event fields");
358
359 domain.type = LTTNG_DOMAIN_UST;
360
361 handle = lttng_create_handle(NULL, &domain);
362 if (handle == NULL) {
363 goto error;
364 }
365
366 size = lttng_list_tracepoint_fields(handle, &event_field_list);
367 if (size < 0) {
368 ERR("Unable to list UST event fields");
369 lttng_destroy_handle(handle);
370 return size;
371 }
372
373 MSG("UST events:\n-------------");
374
375 if (size == 0) {
376 MSG("None");
377 }
378
379 for (i = 0; i < size; i++) {
380 if (cur_pid != event_field_list[i].event.pid) {
381 cur_pid = event_field_list[i].event.pid;
382 MSG("\nPID: %d - Name: %s", cur_pid, get_cmdline_by_pid(cur_pid));
383 }
384 if (strcmp(cur_event.name, event_field_list[i].event.name) != 0) {
385 print_events(&event_field_list[i].event);
386 memcpy(&cur_event, &event_field_list[i].event,
387 sizeof(cur_event));
388 }
389 print_event_field(&event_field_list[i]);
390 }
391
392 MSG("");
393
394 free(event_field_list);
395 lttng_destroy_handle(handle);
396
397 return CMD_SUCCESS;
398
399 error:
400 lttng_destroy_handle(handle);
401 return -1;
402 }
403
404 /*
405 * Ask for all trace events in the kernel and pretty print them.
406 */
407 static int list_kernel_events(void)
408 {
409 int i, size;
410 struct lttng_domain domain;
411 struct lttng_handle *handle;
412 struct lttng_event *event_list;
413
414 memset(&domain, 0, sizeof(domain));
415
416 DBG("Getting kernel tracing events");
417
418 domain.type = LTTNG_DOMAIN_KERNEL;
419
420 handle = lttng_create_handle(NULL, &domain);
421 if (handle == NULL) {
422 goto error;
423 }
424
425 size = lttng_list_tracepoints(handle, &event_list);
426 if (size < 0) {
427 ERR("Unable to list kernel events");
428 lttng_destroy_handle(handle);
429 return size;
430 }
431
432 MSG("Kernel events:\n-------------");
433
434 for (i = 0; i < size; i++) {
435 print_events(&event_list[i]);
436 }
437
438 MSG("");
439
440 free(event_list);
441
442 lttng_destroy_handle(handle);
443 return CMD_SUCCESS;
444
445 error:
446 lttng_destroy_handle(handle);
447 return -1;
448 }
449
450 /*
451 * List events of channel of session and domain.
452 */
453 static int list_events(const char *channel_name)
454 {
455 int ret, count, i;
456 struct lttng_event *events = NULL;
457
458 count = lttng_list_events(handle, channel_name, &events);
459 if (count < 0) {
460 ret = count;
461 goto error;
462 }
463
464 MSG("\n%sEvents:", indent4);
465 if (count == 0) {
466 MSG("%sNone\n", indent6);
467 goto end;
468 }
469
470 for (i = 0; i < count; i++) {
471 print_events(&events[i]);
472 }
473
474 MSG("");
475
476 end:
477 free(events);
478 ret = CMD_SUCCESS;
479
480 error:
481 return ret;
482 }
483
484 /*
485 * Pretty print channel
486 */
487 static void print_channel(struct lttng_channel *channel)
488 {
489 MSG("- %s:%s\n", channel->name, enabled_string(channel->enabled));
490
491 MSG("%sAttributes:", indent4);
492 MSG("%soverwrite mode: %d", indent6, channel->attr.overwrite);
493 MSG("%ssubbufers size: %" PRIu64, indent6, channel->attr.subbuf_size);
494 MSG("%snumber of subbufers: %" PRIu64, indent6, channel->attr.num_subbuf);
495 MSG("%sswitch timer interval: %u", indent6, channel->attr.switch_timer_interval);
496 MSG("%sread timer interval: %u", indent6, channel->attr.read_timer_interval);
497 switch (channel->attr.output) {
498 case LTTNG_EVENT_SPLICE:
499 MSG("%soutput: splice()", indent6);
500 break;
501 case LTTNG_EVENT_MMAP:
502 MSG("%soutput: mmap()", indent6);
503 break;
504 }
505 }
506
507 /*
508 * List channel(s) of session and domain.
509 *
510 * If channel_name is NULL, all channels are listed.
511 */
512 static int list_channels(const char *channel_name)
513 {
514 int count, i, ret = CMD_SUCCESS;
515 unsigned int chan_found = 0;
516 struct lttng_channel *channels = NULL;
517
518 DBG("Listing channel(s) (%s)", channel_name ? : "<all>");
519
520 count = lttng_list_channels(handle, &channels);
521 if (count < 0) {
522 switch (-count) {
523 case LTTNG_ERR_KERN_CHAN_NOT_FOUND:
524 ret = CMD_SUCCESS;
525 WARN("No kernel channel");
526 break;
527 default:
528 /* We had a real error */
529 ret = count;
530 ERR("%s", lttng_strerror(ret));
531 }
532 goto error_channels;
533 }
534
535 if (channel_name == NULL) {
536 MSG("Channels:\n-------------");
537 }
538
539 for (i = 0; i < count; i++) {
540 if (channel_name != NULL) {
541 if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) {
542 chan_found = 1;
543 } else {
544 continue;
545 }
546 }
547 print_channel(&channels[i]);
548
549 /* Listing events per channel */
550 ret = list_events(channels[i].name);
551 if (ret < 0) {
552 MSG("%s", lttng_strerror(ret));
553 }
554
555 if (chan_found) {
556 break;
557 }
558 }
559
560 if (!chan_found && channel_name != NULL) {
561 ERR("Channel %s not found", channel_name);
562 goto error;
563 }
564
565 ret = CMD_SUCCESS;
566
567 error:
568 free(channels);
569
570 error_channels:
571 return ret;
572 }
573
574 /*
575 * List available tracing session. List only basic information.
576 *
577 * If session_name is NULL, all sessions are listed.
578 */
579 static int list_sessions(const char *session_name)
580 {
581 int ret, count, i;
582 unsigned int session_found = 0;
583 struct lttng_session *sessions;
584
585 count = lttng_list_sessions(&sessions);
586 DBG("Session count %d", count);
587 if (count < 0) {
588 ret = count;
589 goto error;
590 } else if (count == 0) {
591 MSG("Currently no available tracing session");
592 goto end;
593 }
594
595 if (session_name == NULL) {
596 MSG("Available tracing sessions:");
597 }
598
599 for (i = 0; i < count; i++) {
600 if (session_name != NULL) {
601 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
602 session_found = 1;
603 MSG("Tracing session %s:%s", session_name, active_string(sessions[i].enabled));
604 MSG("%sTrace path: %s\n", indent4, sessions[i].path);
605 break;
606 }
607 continue;
608 }
609
610 MSG(" %d) %s (%s)%s", i + 1, sessions[i].name, sessions[i].path,
611 active_string(sessions[i].enabled));
612
613 if (session_found) {
614 break;
615 }
616 }
617
618 free(sessions);
619
620 if (!session_found && session_name != NULL) {
621 ERR("Session '%s' not found", session_name);
622 ret = CMD_ERROR;
623 goto error;
624 }
625
626 if (session_name == NULL) {
627 MSG("\nUse lttng list <session_name> for more details");
628 }
629
630 end:
631 return CMD_SUCCESS;
632
633 error:
634 return ret;
635 }
636
637 /*
638 * List available domain(s) for a session.
639 */
640 static int list_domains(const char *session_name)
641 {
642 int i, count, ret = CMD_SUCCESS;
643 struct lttng_domain *domains = NULL;
644
645 MSG("Domains:\n-------------");
646
647 count = lttng_list_domains(session_name, &domains);
648 if (count < 0) {
649 ret = count;
650 goto error;
651 } else if (count == 0) {
652 MSG(" None");
653 goto end;
654 }
655
656 for (i = 0; i < count; i++) {
657 switch (domains[i].type) {
658 case LTTNG_DOMAIN_KERNEL:
659 MSG(" - Kernel");
660 break;
661 case LTTNG_DOMAIN_UST:
662 MSG(" - UST global");
663 break;
664 default:
665 break;
666 }
667 }
668
669 end:
670 free(domains);
671
672 error:
673 return ret;
674 }
675
676 /*
677 * The 'list <options>' first level command
678 */
679 int cmd_list(int argc, const char **argv)
680 {
681 int opt, ret = CMD_SUCCESS;
682 const char *session_name;
683 static poptContext pc;
684 struct lttng_domain domain;
685 struct lttng_domain *domains = NULL;
686
687 memset(&domain, 0, sizeof(domain));
688
689 if (argc < 1) {
690 usage(stderr);
691 ret = CMD_ERROR;
692 goto end;
693 }
694
695 pc = poptGetContext(NULL, argc, argv, long_options, 0);
696 poptReadDefaultConfig(pc, 0);
697
698 while ((opt = poptGetNextOpt(pc)) != -1) {
699 switch (opt) {
700 case OPT_HELP:
701 usage(stdout);
702 goto end;
703 case OPT_USERSPACE:
704 opt_userspace = 1;
705 break;
706 case OPT_LIST_OPTIONS:
707 list_cmd_options(stdout, long_options);
708 goto end;
709 default:
710 usage(stderr);
711 ret = CMD_UNDEFINED;
712 goto end;
713 }
714 }
715
716 /* Get session name (trailing argument) */
717 session_name = poptGetArg(pc);
718 DBG2("Session name: %s", session_name);
719
720 if (opt_kernel) {
721 domain.type = LTTNG_DOMAIN_KERNEL;
722 } else if (opt_userspace) {
723 DBG2("Listing userspace global domain");
724 domain.type = LTTNG_DOMAIN_UST;
725 }
726
727 if (opt_kernel || opt_userspace) {
728 handle = lttng_create_handle(session_name, &domain);
729 if (handle == NULL) {
730 ret = CMD_FATAL;
731 goto end;
732 }
733 }
734
735 if (session_name == NULL) {
736 if (!opt_kernel && !opt_userspace) {
737 ret = list_sessions(NULL);
738 if (ret != 0) {
739 goto end;
740 }
741 }
742 if (opt_kernel) {
743 ret = list_kernel_events();
744 if (ret < 0) {
745 ret = CMD_ERROR;
746 goto end;
747 }
748 }
749 if (opt_userspace) {
750 if (opt_fields) {
751 ret = list_ust_event_fields();
752 } else {
753 ret = list_ust_events();
754 }
755 if (ret < 0) {
756 ret = CMD_ERROR;
757 goto end;
758 }
759 }
760 } else {
761 /* List session attributes */
762 ret = list_sessions(session_name);
763 if (ret != 0) {
764 goto end;
765 }
766
767 /* Domain listing */
768 if (opt_domain) {
769 ret = list_domains(session_name);
770 goto end;
771 }
772
773 if (opt_kernel) {
774 /* Channel listing */
775 ret = list_channels(opt_channel);
776 if (ret < 0) {
777 goto end;
778 }
779 } else {
780 int i, nb_domain;
781
782 /* We want all domain(s) */
783 nb_domain = lttng_list_domains(session_name, &domains);
784 if (nb_domain < 0) {
785 ret = nb_domain;
786 goto end;
787 }
788
789 for (i = 0; i < nb_domain; i++) {
790 switch (domains[i].type) {
791 case LTTNG_DOMAIN_KERNEL:
792 MSG("=== Domain: Kernel ===\n");
793 break;
794 case LTTNG_DOMAIN_UST:
795 MSG("=== Domain: UST global ===\n");
796 break;
797 default:
798 MSG("=== Domain: Unimplemented ===\n");
799 break;
800 }
801
802 /* Clean handle before creating a new one */
803 if (handle) {
804 lttng_destroy_handle(handle);
805 }
806
807 handle = lttng_create_handle(session_name, &domains[i]);
808 if (handle == NULL) {
809 ret = CMD_FATAL;
810 goto end;
811 }
812
813 ret = list_channels(opt_channel);
814 if (ret < 0) {
815 goto end;
816 }
817 }
818 }
819 }
820
821 end:
822 free(domains);
823 if (handle) {
824 lttng_destroy_handle(handle);
825 }
826
827 poptFreeContext(pc);
828 return ret;
829 }
This page took 0.045426 seconds and 5 git commands to generate.