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