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