Add a '--list-options' option to each command.
[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 [[-k] [-u] [-p PID] [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, "With -k alone, list available kernel events\n");
78 fprintf(ofp, "With -u alone, list 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, "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
148 const char *loglevel_string_pre(const char *loglevel)
149 {
150 if (loglevel[0] == '\0') {
151 return "";
152 } else {
153 return " (loglevel: ";
154 }
155 }
156
157 static
158 const char *loglevel_string_post(const char *loglevel)
159 {
160 if (loglevel[0] == '\0') {
161 return "";
162 } else {
163 return ")";
164 }
165 }
166
167 /*
168 * Pretty print single event.
169 */
170 static void print_events(struct lttng_event *event)
171 {
172 switch (event->type) {
173 case LTTNG_EVENT_TRACEPOINT:
174 {
175 char ll_value[LTTNG_SYMBOL_NAME_LEN] = "";
176
177 if (event->loglevel[0] != '\0') {
178 int ret;
179
180 ret = snprintf(ll_value, LTTNG_SYMBOL_NAME_LEN,
181 " (%lld)", (long long) event->loglevel_value);
182 if (ret < 0)
183 ERR("snprintf error");
184 }
185 MSG("%s%s%s%s%s%s (type: tracepoint)%s", indent6,
186 event->name,
187 loglevel_string_pre(event->loglevel),
188 event->loglevel,
189 ll_value,
190 loglevel_string_post(event->loglevel),
191 enabled_string(event->enabled));
192 break;
193 }
194 case LTTNG_EVENT_PROBE:
195 MSG("%s%s (type: probe)%s", indent6,
196 event->name, enabled_string(event->enabled));
197 if (event->attr.probe.addr != 0) {
198 MSG("%saddr: 0x%" PRIx64, indent8, event->attr.probe.addr);
199 } else {
200 MSG("%soffset: 0x%" PRIx64, indent8, event->attr.probe.offset);
201 MSG("%ssymbol: %s", indent8, event->attr.probe.symbol_name);
202 }
203 break;
204 case LTTNG_EVENT_FUNCTION:
205 case LTTNG_EVENT_FUNCTION_ENTRY:
206 MSG("%s%s (type: function)%s", indent6,
207 event->name, enabled_string(event->enabled));
208 MSG("%ssymbol: \"%s\"", indent8, event->attr.ftrace.symbol_name);
209 break;
210 case LTTNG_EVENT_SYSCALL:
211 MSG("%s (type: syscall)%s", indent6,
212 enabled_string(event->enabled));
213 break;
214 case LTTNG_EVENT_NOOP:
215 MSG("%s (type: noop)%s", indent6,
216 enabled_string(event->enabled));
217 break;
218 case LTTNG_EVENT_TRACEPOINT_LOGLEVEL:
219 MSG("%s%s (type: tracepoint loglevel)%s", indent6,
220 event->name,
221 enabled_string(event->enabled));
222 break;
223 case LTTNG_EVENT_ALL:
224 /* We should never have "all" events in list. */
225 assert(0);
226 break;
227 }
228 }
229
230 /*
231 * Ask session daemon for all user space tracepoints available.
232 */
233 static int list_ust_events(void)
234 {
235 int i, size;
236 struct lttng_domain domain;
237 struct lttng_handle *handle;
238 struct lttng_event *event_list;
239 pid_t cur_pid = 0;
240
241 DBG("Getting UST tracing events");
242
243 domain.type = LTTNG_DOMAIN_UST;
244
245 handle = lttng_create_handle(NULL, &domain);
246 if (handle == NULL) {
247 goto error;
248 }
249
250 size = lttng_list_tracepoints(handle, &event_list);
251 if (size < 0) {
252 ERR("Unable to list UST events");
253 return size;
254 }
255
256 MSG("UST events:\n-------------");
257
258 if (size == 0) {
259 MSG("None");
260 }
261
262 for (i = 0; i < size; i++) {
263 if (cur_pid != event_list[i].pid) {
264 cur_pid = event_list[i].pid;
265 MSG("\nPID: %d - Name: %s", cur_pid, get_cmdline_by_pid(cur_pid));
266 }
267 print_events(&event_list[i]);
268 }
269
270 MSG("");
271
272 free(event_list);
273
274 return CMD_SUCCESS;
275
276 error:
277 return -1;
278 }
279
280 /*
281 * Ask for all trace events in the kernel and pretty print them.
282 */
283 static int list_kernel_events(void)
284 {
285 int i, size;
286 struct lttng_domain domain;
287 struct lttng_handle *handle;
288 struct lttng_event *event_list;
289
290 DBG("Getting kernel tracing events");
291
292 domain.type = LTTNG_DOMAIN_KERNEL;
293
294 handle = lttng_create_handle(NULL, &domain);
295 if (handle == NULL) {
296 goto error;
297 }
298
299 size = lttng_list_tracepoints(handle, &event_list);
300 if (size < 0) {
301 ERR("Unable to list kernel events");
302 return size;
303 }
304
305 MSG("Kernel events:\n-------------");
306
307 for (i = 0; i < size; i++) {
308 print_events(&event_list[i]);
309 }
310
311 MSG("");
312
313 free(event_list);
314
315 return CMD_SUCCESS;
316
317 error:
318 return -1;
319 }
320
321 /*
322 * List events of channel of session and domain.
323 */
324 static int list_events(const char *channel_name)
325 {
326 int ret, count, i;
327 struct lttng_event *events = NULL;
328
329 count = lttng_list_events(handle, channel_name, &events);
330 if (count < 0) {
331 ret = count;
332 goto error;
333 }
334
335 MSG("\n%sEvents:", indent4);
336 if (count == 0) {
337 MSG("%sNone\n", indent6);
338 goto end;
339 }
340
341 for (i = 0; i < count; i++) {
342 print_events(&events[i]);
343 }
344
345 MSG("");
346
347 end:
348 if (events) {
349 free(events);
350 }
351 ret = CMD_SUCCESS;
352
353 error:
354 return ret;
355 }
356
357 /*
358 * Pretty print channel
359 */
360 static void print_channel(struct lttng_channel *channel)
361 {
362 MSG("- %s:%s\n", channel->name, enabled_string(channel->enabled));
363
364 MSG("%sAttributes:", indent4);
365 MSG("%soverwrite mode: %d", indent6, channel->attr.overwrite);
366 MSG("%ssubbufers size: %" PRIu64, indent6, channel->attr.subbuf_size);
367 MSG("%snumber of subbufers: %" PRIu64, indent6, channel->attr.num_subbuf);
368 MSG("%sswitch timer interval: %u", indent6, channel->attr.switch_timer_interval);
369 MSG("%sread timer interval: %u", indent6, channel->attr.read_timer_interval);
370 switch (channel->attr.output) {
371 case LTTNG_EVENT_SPLICE:
372 MSG("%soutput: splice()", indent6);
373 break;
374 case LTTNG_EVENT_MMAP:
375 MSG("%soutput: mmap()", indent6);
376 break;
377 }
378 }
379
380 /*
381 * List channel(s) of session and domain.
382 *
383 * If channel_name is NULL, all channels are listed.
384 */
385 static int list_channels(const char *channel_name)
386 {
387 int count, i, ret = CMD_SUCCESS;
388 unsigned int chan_found = 0;
389 struct lttng_channel *channels = NULL;
390
391 DBG("Listing channel(s) (%s)", channel_name ? : "<all>");
392
393 count = lttng_list_channels(handle, &channels);
394 if (count < 0) {
395 ret = count;
396 goto error;
397 } else if (count == 0) {
398 MSG("No channel found");
399 goto end;
400 }
401
402 if (channel_name == NULL) {
403 MSG("Channels:\n-------------");
404 }
405
406 for (i = 0; i < count; i++) {
407 if (channel_name != NULL) {
408 if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) {
409 chan_found = 1;
410 } else {
411 continue;
412 }
413 }
414 print_channel(&channels[i]);
415
416 /* Listing events per channel */
417 ret = list_events(channels[i].name);
418 if (ret < 0) {
419 MSG("%s", lttng_strerror(ret));
420 }
421
422 if (chan_found) {
423 break;
424 }
425 }
426
427 if (!chan_found && channel_name != NULL) {
428 MSG("Channel %s not found", channel_name);
429 }
430
431 end:
432 free(channels);
433 ret = CMD_SUCCESS;
434
435 error:
436 return ret;
437 }
438
439 /*
440 * List available tracing session. List only basic information.
441 *
442 * If session_name is NULL, all sessions are listed.
443 */
444 static int list_sessions(const char *session_name)
445 {
446 int ret, count, i;
447 unsigned int session_found = 0;
448 struct lttng_session *sessions;
449
450 count = lttng_list_sessions(&sessions);
451 DBG("Session count %d", count);
452 if (count < 0) {
453 ret = count;
454 goto error;
455 }
456
457 if (session_name == NULL) {
458 MSG("Available tracing sessions:");
459 }
460
461 for (i = 0; i < count; i++) {
462 if (session_name != NULL) {
463 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
464 session_found = 1;
465 MSG("Tracing session %s:%s", session_name, active_string(sessions[i].enabled));
466 MSG("%sTrace path: %s\n", indent4, sessions[i].path);
467 break;
468 }
469 continue;
470 }
471
472 MSG(" %d) %s (%s)%s", i + 1, sessions[i].name, sessions[i].path, active_string(sessions[i].enabled));
473
474 if (session_found) {
475 break;
476 }
477 }
478
479 free(sessions);
480
481 if (!session_found && session_name != NULL) {
482 MSG("Session %s not found", session_name);
483 }
484
485 if (session_name == NULL) {
486 MSG("\nUse lttng list <session_name> for more details");
487 }
488
489 return CMD_SUCCESS;
490
491 error:
492 return ret;
493 }
494
495 /*
496 * List available domain(s) for a session.
497 */
498 static int list_domains(const char *session_name)
499 {
500 int i, count, ret = CMD_SUCCESS;
501 struct lttng_domain *domains = NULL;
502
503 MSG("Domains:\n-------------");
504
505 count = lttng_list_domains(session_name, &domains);
506 if (count < 0) {
507 ret = count;
508 goto error;
509 } else if (count == 0) {
510 MSG(" None");
511 goto end;
512 }
513
514 for (i = 0; i < count; i++) {
515 switch (domains[i].type) {
516 case LTTNG_DOMAIN_KERNEL:
517 MSG(" - Kernel");
518 break;
519 case LTTNG_DOMAIN_UST:
520 MSG(" - UST global");
521 break;
522 default:
523 break;
524 }
525 }
526
527 end:
528 free(domains);
529
530 error:
531 return ret;
532 }
533
534 /*
535 * The 'list <options>' first level command
536 */
537 int cmd_list(int argc, const char **argv)
538 {
539 int opt, i, ret = CMD_SUCCESS;
540 int nb_domain;
541 const char *session_name;
542 static poptContext pc;
543 struct lttng_domain domain;
544 struct lttng_domain *domains = NULL;
545
546 if (argc < 1) {
547 usage(stderr);
548 goto end;
549 }
550
551 pc = poptGetContext(NULL, argc, argv, long_options, 0);
552 poptReadDefaultConfig(pc, 0);
553
554 while ((opt = poptGetNextOpt(pc)) != -1) {
555 switch (opt) {
556 case OPT_HELP:
557 usage(stderr);
558 goto end;
559 case OPT_USERSPACE:
560 opt_userspace = 1;
561 break;
562 case OPT_LIST_OPTIONS:
563 list_cmd_options(stdout, long_options);
564 ret = CMD_SUCCESS;
565 goto end;
566 default:
567 usage(stderr);
568 ret = CMD_UNDEFINED;
569 goto end;
570 }
571 }
572
573 /* Get session name (trailing argument) */
574 session_name = poptGetArg(pc);
575 DBG2("Session name: %s", session_name);
576
577 if (opt_kernel) {
578 domain.type = LTTNG_DOMAIN_KERNEL;
579 } else if (opt_userspace) {
580 DBG2("Listing userspace global domain");
581 domain.type = LTTNG_DOMAIN_UST;
582 }
583
584 handle = lttng_create_handle(session_name, &domain);
585 if (handle == NULL) {
586 goto end;
587 }
588
589 if (session_name == NULL) {
590 if (!opt_kernel && !opt_userspace) {
591 ret = list_sessions(NULL);
592 if (ret < 0) {
593 goto end;
594 }
595 }
596 if (opt_kernel) {
597 ret = list_kernel_events();
598 if (ret < 0) {
599 goto end;
600 }
601 }
602 if (opt_userspace) {
603 ret = list_ust_events();
604 if (ret < 0) {
605 goto end;
606 }
607 }
608 } else {
609 /* List session attributes */
610 ret = list_sessions(session_name);
611 if (ret < 0) {
612 goto end;
613 }
614
615 /* Domain listing */
616 if (opt_domain) {
617 ret = list_domains(session_name);
618 goto end;
619 }
620
621 if (opt_kernel) {
622 /* Channel listing */
623 ret = list_channels(opt_channel);
624 if (ret < 0) {
625 goto end;
626 }
627 } else {
628 /* We want all domain(s) */
629 nb_domain = lttng_list_domains(session_name, &domains);
630 if (nb_domain < 0) {
631 ret = nb_domain;
632 goto end;
633 }
634
635 for (i = 0; i < nb_domain; i++) {
636 switch (domains[i].type) {
637 case LTTNG_DOMAIN_KERNEL:
638 MSG("=== Domain: Kernel ===\n");
639 break;
640 case LTTNG_DOMAIN_UST:
641 MSG("=== Domain: UST global ===\n");
642 break;
643 default:
644 MSG("=== Domain: Unimplemented ===\n");
645 break;
646 }
647
648 /* Clean handle before creating a new one */
649 lttng_destroy_handle(handle);
650
651 handle = lttng_create_handle(session_name, &domains[i]);
652 if (handle == NULL) {
653 goto end;
654 }
655
656 ret = list_channels(opt_channel);
657 if (ret < 0) {
658 goto end;
659 }
660 }
661 }
662 }
663
664 end:
665 if (domains) {
666 free(domains);
667 }
668 lttng_destroy_handle(handle);
669
670 return ret;
671 }
This page took 0.069322 seconds and 5 git commands to generate.