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