Update lttng-ust-abi.h for event field listing
[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 #if 0
33 /* Not implemented yet */
34 static char *opt_cmd_name;
35 static pid_t opt_pid;
36 #endif
37
38 const char *indent4 = " ";
39 const char *indent6 = " ";
40 const char *indent8 = " ";
41
42 enum {
43 OPT_HELP = 1,
44 OPT_USERSPACE,
45 OPT_LIST_OPTIONS,
46 };
47
48 static struct lttng_handle *handle;
49
50 static struct poptOption long_options[] = {
51 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
52 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
53 {"kernel", 'k', POPT_ARG_VAL, &opt_kernel, 1, 0, 0},
54 #if 0
55 /* Not implemented yet */
56 {"userspace", 'u', POPT_ARG_STRING | POPT_ARGFLAG_OPTIONAL, &opt_cmd_name, OPT_USERSPACE, 0, 0},
57 {"pid", 'p', POPT_ARG_INT, &opt_pid, 0, 0, 0},
58 #else
59 {"userspace", 'u', POPT_ARG_NONE, 0, OPT_USERSPACE, 0, 0},
60 #endif
61 {"channel", 'c', POPT_ARG_STRING, &opt_channel, 0, 0, 0},
62 {"domain", 'd', POPT_ARG_VAL, &opt_domain, 1, 0, 0},
63 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
64 {0, 0, 0, 0, 0, 0, 0}
65 };
66
67 /*
68 * usage
69 */
70 static void usage(FILE *ofp)
71 {
72 fprintf(ofp, "usage: lttng list [OPTIONS] [SESSION [<OPTIONS>]]\n");
73 fprintf(ofp, "\n");
74 fprintf(ofp, "With no arguments, list available tracing session(s)\n");
75 fprintf(ofp, "\n");
76 fprintf(ofp, "Without a session, -k lists available kernel events\n");
77 fprintf(ofp, "Without a session, -u lists available userspace events\n");
78 fprintf(ofp, "\n");
79 fprintf(ofp, " -h, --help Show this help\n");
80 fprintf(ofp, " --list-options Simple listing of options\n");
81 fprintf(ofp, " -k, --kernel Select kernel domain\n");
82 fprintf(ofp, " -u, --userspace Select user-space domain.\n");
83 #if 0
84 fprintf(ofp, " -p, --pid PID List user-space events by PID\n");
85 #endif
86 fprintf(ofp, "\n");
87 fprintf(ofp, "Session Options:\n");
88 fprintf(ofp, " -c, --channel NAME List details of a channel\n");
89 fprintf(ofp, " -d, --domain List available domain(s)\n");
90 fprintf(ofp, "\n");
91 }
92
93 /*
94 * Get command line from /proc for a specific pid.
95 *
96 * On success, return an allocated string pointer to the proc cmdline.
97 * On error, return NULL.
98 */
99 static char *get_cmdline_by_pid(pid_t pid)
100 {
101 int ret;
102 FILE *fp;
103 char *cmdline = NULL;
104 char path[24]; /* Can't go bigger than /proc/65535/cmdline */
105
106 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
107 fp = fopen(path, "r");
108 if (fp == NULL) {
109 goto end;
110 }
111
112 /* Caller must free() *cmdline */
113 cmdline = malloc(PATH_MAX);
114 ret = fread(cmdline, 1, PATH_MAX, fp);
115 if (ret < 0) {
116 perror("fread proc list");
117 }
118 fclose(fp);
119
120 end:
121 return cmdline;
122 }
123
124 static
125 const char *active_string(int value)
126 {
127 switch (value) {
128 case 0: return " [inactive]";
129 case 1: return " [active]";
130 case -1: return "";
131 default: return NULL;
132 }
133 }
134
135 static
136 const char *enabled_string(int value)
137 {
138 switch (value) {
139 case 0: return " [disabled]";
140 case 1: return " [enabled]";
141 case -1: return "";
142 default: return NULL;
143 }
144 }
145
146 static const char *loglevel_string(int value)
147 {
148 switch (value) {
149 case -1:
150 return "";
151 case LTTNG_LOGLEVEL_EMERG:
152 return "TRACE_EMERG";
153 case LTTNG_LOGLEVEL_ALERT:
154 return "TRACE_ALERT";
155 case LTTNG_LOGLEVEL_CRIT:
156 return "TRACE_CRIT";
157 case LTTNG_LOGLEVEL_ERR:
158 return "TRACE_ERR";
159 case LTTNG_LOGLEVEL_WARNING:
160 return "TRACE_WARNING";
161 case LTTNG_LOGLEVEL_NOTICE:
162 return "TRACE_NOTICE";
163 case LTTNG_LOGLEVEL_INFO:
164 return "TRACE_INFO";
165 case LTTNG_LOGLEVEL_DEBUG_SYSTEM:
166 return "TRACE_DEBUG_SYSTEM";
167 case LTTNG_LOGLEVEL_DEBUG_PROGRAM:
168 return "TRACE_DEBUG_PROGRAM";
169 case LTTNG_LOGLEVEL_DEBUG_PROCESS:
170 return "TRACE_DEBUG_PROCESS";
171 case LTTNG_LOGLEVEL_DEBUG_MODULE:
172 return "TRACE_DEBUG_MODULE";
173 case LTTNG_LOGLEVEL_DEBUG_UNIT:
174 return "TRACE_DEBUG_UNIT";
175 case LTTNG_LOGLEVEL_DEBUG_FUNCTION:
176 return "TRACE_DEBUG_FUNCTION";
177 case LTTNG_LOGLEVEL_DEBUG_LINE:
178 return "TRACE_DEBUG_LINE";
179 case LTTNG_LOGLEVEL_DEBUG:
180 return "TRACE_DEBUG";
181 default:
182 return "<<UNKNOWN>>";
183 }
184 }
185
186 /*
187 * Pretty print single event.
188 */
189 static void print_events(struct lttng_event *event)
190 {
191 switch (event->type) {
192 case LTTNG_EVENT_TRACEPOINT:
193 {
194 if (event->loglevel != -1) {
195 MSG("%s%s (loglevel: %s (%d)) (type: tracepoint)%s",
196 indent6,
197 event->name,
198 loglevel_string(event->loglevel),
199 event->loglevel,
200 enabled_string(event->enabled));
201 } else {
202 MSG("%s%s (type: tracepoint)%s",
203 indent6,
204 event->name,
205 enabled_string(event->enabled));
206 }
207 break;
208 }
209 case LTTNG_EVENT_PROBE:
210 MSG("%s%s (type: probe)%s", indent6,
211 event->name, enabled_string(event->enabled));
212 if (event->attr.probe.addr != 0) {
213 MSG("%saddr: 0x%" PRIx64, indent8, event->attr.probe.addr);
214 } else {
215 MSG("%soffset: 0x%" PRIx64, indent8, event->attr.probe.offset);
216 MSG("%ssymbol: %s", indent8, event->attr.probe.symbol_name);
217 }
218 break;
219 case LTTNG_EVENT_FUNCTION:
220 case LTTNG_EVENT_FUNCTION_ENTRY:
221 MSG("%s%s (type: function)%s", indent6,
222 event->name, enabled_string(event->enabled));
223 MSG("%ssymbol: \"%s\"", indent8, event->attr.ftrace.symbol_name);
224 break;
225 case LTTNG_EVENT_SYSCALL:
226 MSG("%ssyscalls (type: syscall)%s", indent6,
227 enabled_string(event->enabled));
228 break;
229 case LTTNG_EVENT_NOOP:
230 MSG("%s (type: noop)%s", indent6,
231 enabled_string(event->enabled));
232 break;
233 case LTTNG_EVENT_ALL:
234 /* We should never have "all" events in list. */
235 assert(0);
236 break;
237 }
238 }
239
240 /*
241 * Ask session daemon for all user space tracepoints available.
242 */
243 static int list_ust_events(void)
244 {
245 int i, size;
246 struct lttng_domain domain;
247 struct lttng_handle *handle;
248 struct lttng_event *event_list;
249 pid_t cur_pid = 0;
250
251 memset(&domain, 0, sizeof(domain));
252
253 DBG("Getting UST tracing events");
254
255 domain.type = LTTNG_DOMAIN_UST;
256
257 handle = lttng_create_handle(NULL, &domain);
258 if (handle == NULL) {
259 goto error;
260 }
261
262 size = lttng_list_tracepoints(handle, &event_list);
263 if (size < 0) {
264 ERR("Unable to list UST events");
265 lttng_destroy_handle(handle);
266 return size;
267 }
268
269 MSG("UST events:\n-------------");
270
271 if (size == 0) {
272 MSG("None");
273 }
274
275 for (i = 0; i < size; i++) {
276 if (cur_pid != event_list[i].pid) {
277 cur_pid = event_list[i].pid;
278 MSG("\nPID: %d - Name: %s", cur_pid, get_cmdline_by_pid(cur_pid));
279 }
280 print_events(&event_list[i]);
281 }
282
283 MSG("");
284
285 free(event_list);
286 lttng_destroy_handle(handle);
287
288 return CMD_SUCCESS;
289
290 error:
291 lttng_destroy_handle(handle);
292 return -1;
293 }
294
295 /*
296 * Ask for all trace events in the kernel and pretty print them.
297 */
298 static int list_kernel_events(void)
299 {
300 int i, size;
301 struct lttng_domain domain;
302 struct lttng_handle *handle;
303 struct lttng_event *event_list;
304
305 memset(&domain, 0, sizeof(domain));
306
307 DBG("Getting kernel tracing events");
308
309 domain.type = LTTNG_DOMAIN_KERNEL;
310
311 handle = lttng_create_handle(NULL, &domain);
312 if (handle == NULL) {
313 goto error;
314 }
315
316 size = lttng_list_tracepoints(handle, &event_list);
317 if (size < 0) {
318 ERR("Unable to list kernel events");
319 lttng_destroy_handle(handle);
320 return size;
321 }
322
323 MSG("Kernel events:\n-------------");
324
325 for (i = 0; i < size; i++) {
326 print_events(&event_list[i]);
327 }
328
329 MSG("");
330
331 free(event_list);
332
333 lttng_destroy_handle(handle);
334 return CMD_SUCCESS;
335
336 error:
337 lttng_destroy_handle(handle);
338 return -1;
339 }
340
341 /*
342 * List events of channel of session and domain.
343 */
344 static int list_events(const char *channel_name)
345 {
346 int ret, count, i;
347 struct lttng_event *events = NULL;
348
349 count = lttng_list_events(handle, channel_name, &events);
350 if (count < 0) {
351 ret = count;
352 goto error;
353 }
354
355 MSG("\n%sEvents:", indent4);
356 if (count == 0) {
357 MSG("%sNone\n", indent6);
358 goto end;
359 }
360
361 for (i = 0; i < count; i++) {
362 print_events(&events[i]);
363 }
364
365 MSG("");
366
367 end:
368 if (events) {
369 free(events);
370 }
371 ret = CMD_SUCCESS;
372
373 error:
374 return ret;
375 }
376
377 /*
378 * Pretty print channel
379 */
380 static void print_channel(struct lttng_channel *channel)
381 {
382 MSG("- %s:%s\n", channel->name, enabled_string(channel->enabled));
383
384 MSG("%sAttributes:", indent4);
385 MSG("%soverwrite mode: %d", indent6, channel->attr.overwrite);
386 MSG("%ssubbufers size: %" PRIu64, indent6, channel->attr.subbuf_size);
387 MSG("%snumber of subbufers: %" PRIu64, indent6, channel->attr.num_subbuf);
388 MSG("%sswitch timer interval: %u", indent6, channel->attr.switch_timer_interval);
389 MSG("%sread timer interval: %u", indent6, channel->attr.read_timer_interval);
390 switch (channel->attr.output) {
391 case LTTNG_EVENT_SPLICE:
392 MSG("%soutput: splice()", indent6);
393 break;
394 case LTTNG_EVENT_MMAP:
395 MSG("%soutput: mmap()", indent6);
396 break;
397 }
398 }
399
400 /*
401 * List channel(s) of session and domain.
402 *
403 * If channel_name is NULL, all channels are listed.
404 */
405 static int list_channels(const char *channel_name)
406 {
407 int count, i, ret = CMD_SUCCESS;
408 unsigned int chan_found = 0;
409 struct lttng_channel *channels = NULL;
410
411 DBG("Listing channel(s) (%s)", channel_name ? : "<all>");
412
413 count = lttng_list_channels(handle, &channels);
414 if (count < 0) {
415 ret = count;
416 goto error_channels;
417 } else if (count == 0) {
418 ERR("Channel %s not found", channel_name);
419 goto error;
420 }
421
422 if (channel_name == NULL) {
423 MSG("Channels:\n-------------");
424 }
425
426 for (i = 0; i < count; i++) {
427 if (channel_name != NULL) {
428 if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) {
429 chan_found = 1;
430 } else {
431 continue;
432 }
433 }
434 print_channel(&channels[i]);
435
436 /* Listing events per channel */
437 ret = list_events(channels[i].name);
438 if (ret < 0) {
439 MSG("%s", lttng_strerror(ret));
440 }
441
442 if (chan_found) {
443 break;
444 }
445 }
446
447 if (!chan_found && channel_name != NULL) {
448 ERR("Channel %s not found", channel_name);
449 goto error;
450 }
451
452 ret = CMD_SUCCESS;
453
454 error:
455 free(channels);
456
457 error_channels:
458 return ret;
459 }
460
461 /*
462 * List available tracing session. List only basic information.
463 *
464 * If session_name is NULL, all sessions are listed.
465 */
466 static int list_sessions(const char *session_name)
467 {
468 int ret, count, i;
469 unsigned int session_found = 0;
470 struct lttng_session *sessions;
471
472 count = lttng_list_sessions(&sessions);
473 DBG("Session count %d", count);
474 if (count < 0) {
475 ret = count;
476 goto error;
477 } else if (count == 0) {
478 MSG("Currently no available tracing session");
479 goto end;
480 }
481
482 if (session_name == NULL) {
483 MSG("Available tracing sessions:");
484 }
485
486 for (i = 0; i < count; i++) {
487 if (session_name != NULL) {
488 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
489 session_found = 1;
490 MSG("Tracing session %s:%s", session_name, active_string(sessions[i].enabled));
491 MSG("%sTrace path: %s\n", indent4, sessions[i].path);
492 break;
493 }
494 continue;
495 }
496
497 MSG(" %d) %s (%s)%s", i + 1, sessions[i].name, sessions[i].path,
498 active_string(sessions[i].enabled));
499
500 if (session_found) {
501 break;
502 }
503 }
504
505 free(sessions);
506
507 if (!session_found && session_name != NULL) {
508 ERR("Session '%s' not found", session_name);
509 ret = CMD_ERROR;
510 goto error;
511 }
512
513 if (session_name == NULL) {
514 MSG("\nUse lttng list <session_name> for more details");
515 }
516
517 end:
518 return CMD_SUCCESS;
519
520 error:
521 return ret;
522 }
523
524 /*
525 * List available domain(s) for a session.
526 */
527 static int list_domains(const char *session_name)
528 {
529 int i, count, ret = CMD_SUCCESS;
530 struct lttng_domain *domains = NULL;
531
532 MSG("Domains:\n-------------");
533
534 count = lttng_list_domains(session_name, &domains);
535 if (count < 0) {
536 ret = count;
537 goto error;
538 } else if (count == 0) {
539 MSG(" None");
540 goto end;
541 }
542
543 for (i = 0; i < count; i++) {
544 switch (domains[i].type) {
545 case LTTNG_DOMAIN_KERNEL:
546 MSG(" - Kernel");
547 break;
548 case LTTNG_DOMAIN_UST:
549 MSG(" - UST global");
550 break;
551 default:
552 break;
553 }
554 }
555
556 end:
557 free(domains);
558
559 error:
560 return ret;
561 }
562
563 /*
564 * The 'list <options>' first level command
565 */
566 int cmd_list(int argc, const char **argv)
567 {
568 int opt, i, ret = CMD_SUCCESS;
569 int nb_domain;
570 const char *session_name;
571 static poptContext pc;
572 struct lttng_domain domain;
573 struct lttng_domain *domains = NULL;
574
575 memset(&domain, 0, sizeof(domain));
576
577 if (argc < 1) {
578 usage(stderr);
579 ret = CMD_ERROR;
580 goto end;
581 }
582
583 pc = poptGetContext(NULL, argc, argv, long_options, 0);
584 poptReadDefaultConfig(pc, 0);
585
586 while ((opt = poptGetNextOpt(pc)) != -1) {
587 switch (opt) {
588 case OPT_HELP:
589 usage(stdout);
590 goto end;
591 case OPT_USERSPACE:
592 opt_userspace = 1;
593 break;
594 case OPT_LIST_OPTIONS:
595 list_cmd_options(stdout, long_options);
596 goto end;
597 default:
598 usage(stderr);
599 ret = CMD_UNDEFINED;
600 goto end;
601 }
602 }
603
604 /* Get session name (trailing argument) */
605 session_name = poptGetArg(pc);
606 DBG2("Session name: %s", session_name);
607
608 if (opt_kernel) {
609 domain.type = LTTNG_DOMAIN_KERNEL;
610 } else if (opt_userspace) {
611 DBG2("Listing userspace global domain");
612 domain.type = LTTNG_DOMAIN_UST;
613 }
614
615 if (opt_kernel || opt_userspace) {
616 handle = lttng_create_handle(session_name, &domain);
617 if (handle == NULL) {
618 ret = CMD_FATAL;
619 goto end;
620 }
621 }
622
623 if (session_name == NULL) {
624 if (!opt_kernel && !opt_userspace) {
625 ret = list_sessions(NULL);
626 if (ret != 0) {
627 goto end;
628 }
629 }
630 if (opt_kernel) {
631 ret = list_kernel_events();
632 if (ret < 0) {
633 goto end;
634 }
635 }
636 if (opt_userspace) {
637 ret = list_ust_events();
638 if (ret < 0) {
639 goto end;
640 }
641 }
642 } else {
643 /* List session attributes */
644 ret = list_sessions(session_name);
645 if (ret != 0) {
646 goto end;
647 }
648
649 /* Domain listing */
650 if (opt_domain) {
651 ret = list_domains(session_name);
652 goto end;
653 }
654
655 if (opt_kernel) {
656 /* Channel listing */
657 ret = list_channels(opt_channel);
658 if (ret < 0) {
659 goto end;
660 }
661 } else {
662 /* We want all domain(s) */
663 nb_domain = lttng_list_domains(session_name, &domains);
664 if (nb_domain < 0) {
665 ret = nb_domain;
666 goto end;
667 }
668
669 for (i = 0; i < nb_domain; i++) {
670 switch (domains[i].type) {
671 case LTTNG_DOMAIN_KERNEL:
672 MSG("=== Domain: Kernel ===\n");
673 break;
674 case LTTNG_DOMAIN_UST:
675 MSG("=== Domain: UST global ===\n");
676 break;
677 default:
678 MSG("=== Domain: Unimplemented ===\n");
679 break;
680 }
681
682 /* Clean handle before creating a new one */
683 if (handle) {
684 lttng_destroy_handle(handle);
685 }
686
687 handle = lttng_create_handle(session_name, &domains[i]);
688 if (handle == NULL) {
689 ret = CMD_FATAL;
690 goto end;
691 }
692
693 ret = list_channels(opt_channel);
694 if (ret < 0) {
695 goto end;
696 }
697 }
698 }
699 }
700
701 end:
702 if (domains) {
703 free(domains);
704 }
705 if (handle) {
706 lttng_destroy_handle(handle);
707 }
708
709 poptFreeContext(pc);
710 return ret;
711 }
This page took 0.04381 seconds and 5 git commands to generate.