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