Add jul-app ABI/API and handle registration
[lttng-tools.git] / src / bin / lttng / commands / list.c
CommitLineData
f3ed775e
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
f3ed775e
DG
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
d14d33bf
AM
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
f3ed775e
DG
16 */
17
18#define _GNU_SOURCE
9f19cc17 19#include <inttypes.h>
f3ed775e
DG
20#include <popt.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
7a3d1328 24#include <assert.h>
f3ed775e 25
c399183f 26#include "../command.h"
f3ed775e 27
9f19cc17
DG
28static int opt_userspace;
29static int opt_kernel;
30static char *opt_channel;
31static int opt_domain;
f37d259d 32static int opt_fields;
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},
f37d259d 64 {"fields", 'f', POPT_ARG_VAL, &opt_fields, 1, 0, 0},
679b4943 65 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
f3ed775e
DG
66 {0, 0, 0, 0, 0, 0, 0}
67};
68
69/*
70 * usage
71 */
72static void usage(FILE *ofp)
73{
747361fe 74 fprintf(ofp, "usage: lttng list [OPTIONS] [SESSION [SESSION OPTIONS]]\n");
9f19cc17
DG
75 fprintf(ofp, "\n");
76 fprintf(ofp, "With no arguments, list available tracing session(s)\n");
77 fprintf(ofp, "\n");
27221701
DG
78 fprintf(ofp, "Without a session, -k lists available kernel events\n");
79 fprintf(ofp, "Without a session, -u lists available userspace events\n");
9f19cc17
DG
80 fprintf(ofp, "\n");
81 fprintf(ofp, " -h, --help Show this help\n");
27221701 82 fprintf(ofp, " --list-options Simple listing of options\n");
9f19cc17
DG
83 fprintf(ofp, " -k, --kernel Select kernel domain\n");
84 fprintf(ofp, " -u, --userspace Select user-space domain.\n");
f37d259d 85 fprintf(ofp, " -f, --fields List event fields.\n");
d78d6610 86#if 0
9f19cc17 87 fprintf(ofp, " -p, --pid PID List user-space events by PID\n");
d78d6610 88#endif
f3ed775e 89 fprintf(ofp, "\n");
27221701 90 fprintf(ofp, "Session Options:\n");
9f19cc17
DG
91 fprintf(ofp, " -c, --channel NAME List details of a channel\n");
92 fprintf(ofp, " -d, --domain List available domain(s)\n");
f3ed775e
DG
93 fprintf(ofp, "\n");
94}
95
96/*
9f19cc17 97 * Get command line from /proc for a specific pid.
f3ed775e 98 *
9f19cc17
DG
99 * On success, return an allocated string pointer to the proc cmdline.
100 * On error, return NULL.
f3ed775e
DG
101 */
102static char *get_cmdline_by_pid(pid_t pid)
103{
104 int ret;
105 FILE *fp;
106 char *cmdline = NULL;
107 char path[24]; /* Can't go bigger than /proc/65535/cmdline */
108
109 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
110 fp = fopen(path, "r");
111 if (fp == NULL) {
112 goto end;
113 }
114
115 /* Caller must free() *cmdline */
116 cmdline = malloc(PATH_MAX);
117 ret = fread(cmdline, 1, PATH_MAX, fp);
f40799e8
DG
118 if (ret < 0) {
119 perror("fread proc list");
120 }
f3ed775e
DG
121 fclose(fp);
122
123end:
124 return cmdline;
125}
b551a063 126
464dd62d
MD
127static
128const char *active_string(int value)
129{
130 switch (value) {
2cbf8fed
DG
131 case 0: return "inactive";
132 case 1: return "active";
464dd62d
MD
133 case -1: return "";
134 default: return NULL;
135 }
136}
137
2cbf8fed
DG
138static const char *snapshot_string(int value)
139{
140 switch (value) {
141 case 1:
142 return " snapshot";
143 default:
144 return "";
145 }
146}
147
464dd62d
MD
148static
149const char *enabled_string(int value)
150{
151 switch (value) {
b09f0429
MD
152 case 0: return " [disabled]";
153 case 1: return " [enabled]";
464dd62d
MD
154 case -1: return "";
155 default: return NULL;
156 }
157}
158
fceb65df
MD
159static
160const char *filter_string(int value)
161{
162 switch (value) {
163 case 1: return " [with filter]";
164 default: return "";
165 }
166}
167
8005f29a
MD
168static const char *loglevel_string(int value)
169{
170 switch (value) {
255a5dab
MD
171 case -1:
172 return "";
173 case LTTNG_LOGLEVEL_EMERG:
174 return "TRACE_EMERG";
175 case LTTNG_LOGLEVEL_ALERT:
176 return "TRACE_ALERT";
177 case LTTNG_LOGLEVEL_CRIT:
178 return "TRACE_CRIT";
179 case LTTNG_LOGLEVEL_ERR:
180 return "TRACE_ERR";
181 case LTTNG_LOGLEVEL_WARNING:
182 return "TRACE_WARNING";
183 case LTTNG_LOGLEVEL_NOTICE:
184 return "TRACE_NOTICE";
185 case LTTNG_LOGLEVEL_INFO:
186 return "TRACE_INFO";
187 case LTTNG_LOGLEVEL_DEBUG_SYSTEM:
188 return "TRACE_DEBUG_SYSTEM";
189 case LTTNG_LOGLEVEL_DEBUG_PROGRAM:
190 return "TRACE_DEBUG_PROGRAM";
191 case LTTNG_LOGLEVEL_DEBUG_PROCESS:
192 return "TRACE_DEBUG_PROCESS";
193 case LTTNG_LOGLEVEL_DEBUG_MODULE:
194 return "TRACE_DEBUG_MODULE";
195 case LTTNG_LOGLEVEL_DEBUG_UNIT:
196 return "TRACE_DEBUG_UNIT";
197 case LTTNG_LOGLEVEL_DEBUG_FUNCTION:
198 return "TRACE_DEBUG_FUNCTION";
199 case LTTNG_LOGLEVEL_DEBUG_LINE:
200 return "TRACE_DEBUG_LINE";
201 case LTTNG_LOGLEVEL_DEBUG:
202 return "TRACE_DEBUG";
203 default:
204 return "<<UNKNOWN>>";
8005f29a
MD
205 }
206}
207
b551a063
DG
208/*
209 * Pretty print single event.
210 */
211static void print_events(struct lttng_event *event)
212{
213 switch (event->type) {
214 case LTTNG_EVENT_TRACEPOINT:
e4baff1e 215 {
8b703175 216 if (event->loglevel != -1) {
fceb65df 217 MSG("%s%s (loglevel: %s (%d)) (type: tracepoint)%s%s",
8b703175 218 indent6,
81afa345 219 event->name,
8005f29a 220 loglevel_string(event->loglevel),
8b703175 221 event->loglevel,
fceb65df
MD
222 enabled_string(event->enabled),
223 filter_string(event->filter));
8b703175 224 } else {
fceb65df 225 MSG("%s%s (type: tracepoint)%s%s",
8b703175
MD
226 indent6,
227 event->name,
fceb65df
MD
228 enabled_string(event->enabled),
229 filter_string(event->filter));
8b703175 230 }
b551a063 231 break;
e4baff1e 232 }
1896972b
DG
233 case LTTNG_EVENT_FUNCTION:
234 MSG("%s%s (type: function)%s%s", indent6,
235 event->name, enabled_string(event->enabled),
236 filter_string(event->filter));
237 if (event->attr.probe.addr != 0) {
238 MSG("%saddr: 0x%" PRIx64, indent8, event->attr.probe.addr);
239 } else {
240 MSG("%soffset: 0x%" PRIx64, indent8, event->attr.probe.offset);
241 MSG("%ssymbol: %s", indent8, event->attr.probe.symbol_name);
242 }
243 break;
b551a063 244 case LTTNG_EVENT_PROBE:
fceb65df
MD
245 MSG("%s%s (type: probe)%s%s", indent6,
246 event->name, enabled_string(event->enabled),
247 filter_string(event->filter));
b551a063
DG
248 if (event->attr.probe.addr != 0) {
249 MSG("%saddr: 0x%" PRIx64, indent8, event->attr.probe.addr);
250 } else {
251 MSG("%soffset: 0x%" PRIx64, indent8, event->attr.probe.offset);
252 MSG("%ssymbol: %s", indent8, event->attr.probe.symbol_name);
253 }
254 break;
b551a063 255 case LTTNG_EVENT_FUNCTION_ENTRY:
fceb65df
MD
256 MSG("%s%s (type: function)%s%s", indent6,
257 event->name, enabled_string(event->enabled),
258 filter_string(event->filter));
b551a063
DG
259 MSG("%ssymbol: \"%s\"", indent8, event->attr.ftrace.symbol_name);
260 break;
261 case LTTNG_EVENT_SYSCALL:
fceb65df
MD
262 MSG("%ssyscalls (type: syscall)%s%s", indent6,
263 enabled_string(event->enabled),
264 filter_string(event->filter));
b551a063
DG
265 break;
266 case LTTNG_EVENT_NOOP:
fceb65df
MD
267 MSG("%s (type: noop)%s%s", indent6,
268 enabled_string(event->enabled),
269 filter_string(event->filter));
b551a063
DG
270 break;
271 case LTTNG_EVENT_ALL:
272 /* We should never have "all" events in list. */
273 assert(0);
274 break;
275 }
276}
277
f37d259d
MD
278static const char *field_type(struct lttng_event_field *field)
279{
280 switch(field->type) {
281 case LTTNG_EVENT_FIELD_INTEGER:
282 return "integer";
283 case LTTNG_EVENT_FIELD_ENUM:
284 return "enum";
285 case LTTNG_EVENT_FIELD_FLOAT:
286 return "float";
287 case LTTNG_EVENT_FIELD_STRING:
288 return "string";
289 case LTTNG_EVENT_FIELD_OTHER:
290 default: /* fall-through */
291 return "unknown";
292 }
293}
294
295/*
296 * Pretty print single event fields.
297 */
298static void print_event_field(struct lttng_event_field *field)
299{
300 if (!field->field_name[0]) {
301 return;
302 }
d8fbe2a9 303 MSG("%sfield: %s (%s)%s", indent8, field->field_name,
45a32634 304 field_type(field), field->nowrite ? " [no write]" : "");
f37d259d
MD
305}
306
b551a063
DG
307/*
308 * Ask session daemon for all user space tracepoints available.
309 */
310static int list_ust_events(void)
311{
312 int i, size;
313 struct lttng_domain domain;
314 struct lttng_handle *handle;
315 struct lttng_event *event_list;
316 pid_t cur_pid = 0;
ea5cbc00 317 char *cmdline = NULL;
b551a063 318
441c16a7
MD
319 memset(&domain, 0, sizeof(domain));
320
b551a063
DG
321 DBG("Getting UST tracing events");
322
323 domain.type = LTTNG_DOMAIN_UST;
324
325 handle = lttng_create_handle(NULL, &domain);
326 if (handle == NULL) {
327 goto error;
328 }
329
330 size = lttng_list_tracepoints(handle, &event_list);
331 if (size < 0) {
60e835ca 332 ERR("Unable to list UST events: %s", lttng_strerror(size));
ef021732 333 lttng_destroy_handle(handle);
b551a063
DG
334 return size;
335 }
336
337 MSG("UST events:\n-------------");
338
339 if (size == 0) {
340 MSG("None");
341 }
342
343 for (i = 0; i < size; i++) {
344 if (cur_pid != event_list[i].pid) {
345 cur_pid = event_list[i].pid;
ea5cbc00
CB
346 cmdline = get_cmdline_by_pid(cur_pid);
347 MSG("\nPID: %d - Name: %s", cur_pid, cmdline);
348 free(cmdline);
b551a063
DG
349 }
350 print_events(&event_list[i]);
351 }
352
353 MSG("");
354
355 free(event_list);
ef021732 356 lttng_destroy_handle(handle);
b551a063
DG
357
358 return CMD_SUCCESS;
359
360error:
ef021732 361 lttng_destroy_handle(handle);
b551a063
DG
362 return -1;
363}
f3ed775e 364
f37d259d
MD
365/*
366 * Ask session daemon for all user space tracepoint fields available.
367 */
368static int list_ust_event_fields(void)
369{
370 int i, size;
371 struct lttng_domain domain;
372 struct lttng_handle *handle;
373 struct lttng_event_field *event_field_list;
374 pid_t cur_pid = 0;
ea5cbc00
CB
375 char *cmdline = NULL;
376
f37d259d
MD
377 struct lttng_event cur_event;
378
379 memset(&domain, 0, sizeof(domain));
380 memset(&cur_event, 0, sizeof(cur_event));
381
382 DBG("Getting UST tracing event fields");
383
384 domain.type = LTTNG_DOMAIN_UST;
385
386 handle = lttng_create_handle(NULL, &domain);
387 if (handle == NULL) {
388 goto error;
389 }
390
391 size = lttng_list_tracepoint_fields(handle, &event_field_list);
392 if (size < 0) {
60e835ca 393 ERR("Unable to list UST event fields: %s", lttng_strerror(size));
f37d259d
MD
394 lttng_destroy_handle(handle);
395 return size;
396 }
397
398 MSG("UST events:\n-------------");
399
400 if (size == 0) {
401 MSG("None");
402 }
403
404 for (i = 0; i < size; i++) {
405 if (cur_pid != event_field_list[i].event.pid) {
406 cur_pid = event_field_list[i].event.pid;
ea5cbc00
CB
407 cmdline = get_cmdline_by_pid(cur_pid);
408 MSG("\nPID: %d - Name: %s", cur_pid, cmdline);
409 free(cmdline);
f37d259d
MD
410 }
411 if (strcmp(cur_event.name, event_field_list[i].event.name) != 0) {
412 print_events(&event_field_list[i].event);
413 memcpy(&cur_event, &event_field_list[i].event,
414 sizeof(cur_event));
415 }
416 print_event_field(&event_field_list[i]);
417 }
418
419 MSG("");
420
421 free(event_field_list);
422 lttng_destroy_handle(handle);
423
424 return CMD_SUCCESS;
425
426error:
427 lttng_destroy_handle(handle);
428 return -1;
429}
430
f3ed775e 431/*
9f19cc17 432 * Ask for all trace events in the kernel and pretty print them.
f3ed775e 433 */
9f19cc17 434static int list_kernel_events(void)
f3ed775e 435{
9f19cc17 436 int i, size;
b551a063
DG
437 struct lttng_domain domain;
438 struct lttng_handle *handle;
9f19cc17 439 struct lttng_event *event_list;
f3ed775e 440
441c16a7
MD
441 memset(&domain, 0, sizeof(domain));
442
b551a063
DG
443 DBG("Getting kernel tracing events");
444
445 domain.type = LTTNG_DOMAIN_KERNEL;
446
447 handle = lttng_create_handle(NULL, &domain);
448 if (handle == NULL) {
449 goto error;
450 }
f3ed775e 451
cd80958d 452 size = lttng_list_tracepoints(handle, &event_list);
9f19cc17 453 if (size < 0) {
60e835ca 454 ERR("Unable to list kernel events: %s", lttng_strerror(size));
ef021732 455 lttng_destroy_handle(handle);
9f19cc17 456 return size;
f3ed775e
DG
457 }
458
9f19cc17 459 MSG("Kernel events:\n-------------");
f3ed775e 460
9f19cc17 461 for (i = 0; i < size; i++) {
b551a063 462 print_events(&event_list[i]);
f3ed775e
DG
463 }
464
b551a063
DG
465 MSG("");
466
f3ed775e
DG
467 free(event_list);
468
ef021732 469 lttng_destroy_handle(handle);
f3ed775e 470 return CMD_SUCCESS;
b551a063
DG
471
472error:
ef021732 473 lttng_destroy_handle(handle);
b551a063 474 return -1;
f3ed775e
DG
475}
476
477/*
9f19cc17 478 * List events of channel of session and domain.
f3ed775e 479 */
cd80958d 480static int list_events(const char *channel_name)
f3ed775e
DG
481{
482 int ret, count, i;
9f19cc17 483 struct lttng_event *events = NULL;
f3ed775e 484
cd80958d 485 count = lttng_list_events(handle, channel_name, &events);
f3ed775e
DG
486 if (count < 0) {
487 ret = count;
60e835ca 488 ERR("%s", lttng_strerror(ret));
f3ed775e
DG
489 goto error;
490 }
491
9f19cc17
DG
492 MSG("\n%sEvents:", indent4);
493 if (count == 0) {
5edd7e09 494 MSG("%sNone\n", indent6);
9f19cc17
DG
495 goto end;
496 }
497
f3ed775e 498 for (i = 0; i < count; i++) {
b551a063 499 print_events(&events[i]);
f3ed775e
DG
500 }
501
9f19cc17 502 MSG("");
f3ed775e 503
9f19cc17 504end:
0e428499 505 free(events);
9f19cc17 506 ret = CMD_SUCCESS;
f3ed775e
DG
507
508error:
509 return ret;
510}
511
512/*
9f19cc17
DG
513 * Pretty print channel
514 */
515static void print_channel(struct lttng_channel *channel)
516{
464dd62d 517 MSG("- %s:%s\n", channel->name, enabled_string(channel->enabled));
9f19cc17
DG
518
519 MSG("%sAttributes:", indent4);
a5bbe8f6
DG
520 MSG("%soverwrite mode: %d", indent6, channel->attr.overwrite);
521 MSG("%ssubbufers size: %" PRIu64, indent6, channel->attr.subbuf_size);
9f19cc17 522 MSG("%snumber of subbufers: %" PRIu64, indent6, channel->attr.num_subbuf);
a5bbe8f6
DG
523 MSG("%sswitch timer interval: %u", indent6, channel->attr.switch_timer_interval);
524 MSG("%sread timer interval: %u", indent6, channel->attr.read_timer_interval);
9f19cc17
DG
525 switch (channel->attr.output) {
526 case LTTNG_EVENT_SPLICE:
527 MSG("%soutput: splice()", indent6);
528 break;
529 case LTTNG_EVENT_MMAP:
530 MSG("%soutput: mmap()", indent6);
531 break;
532 }
533}
534
535/*
536 * List channel(s) of session and domain.
f3ed775e 537 *
9f19cc17 538 * If channel_name is NULL, all channels are listed.
f3ed775e 539 */
cd80958d 540static int list_channels(const char *channel_name)
f3ed775e 541{
9f19cc17
DG
542 int count, i, ret = CMD_SUCCESS;
543 unsigned int chan_found = 0;
544 struct lttng_channel *channels = NULL;
f3ed775e 545
3e25cb20 546 DBG("Listing channel(s) (%s)", channel_name ? : "<all>");
9f19cc17 547
cd80958d 548 count = lttng_list_channels(handle, &channels);
f3ed775e 549 if (count < 0) {
c7d620a2
DG
550 switch (-count) {
551 case LTTNG_ERR_KERN_CHAN_NOT_FOUND:
552 ret = CMD_SUCCESS;
553 WARN("No kernel channel");
554 break;
555 default:
556 /* We had a real error */
557 ret = count;
558 ERR("%s", lttng_strerror(ret));
60e835ca 559 break;
c7d620a2 560 }
ae856491 561 goto error_channels;
f3ed775e
DG
562 }
563
9f19cc17
DG
564 if (channel_name == NULL) {
565 MSG("Channels:\n-------------");
566 }
567
568 for (i = 0; i < count; i++) {
569 if (channel_name != NULL) {
570 if (strncmp(channels[i].name, channel_name, NAME_MAX) == 0) {
571 chan_found = 1;
572 } else {
573 continue;
574 }
575 }
576 print_channel(&channels[i]);
577
578 /* Listing events per channel */
cd80958d 579 ret = list_events(channels[i].name);
9f19cc17 580 if (ret < 0) {
60e835ca 581 ERR("%s", lttng_strerror(ret));
9f19cc17
DG
582 }
583
584 if (chan_found) {
585 break;
f3ed775e 586 }
f3ed775e
DG
587 }
588
9f19cc17 589 if (!chan_found && channel_name != NULL) {
ae856491
DG
590 ERR("Channel %s not found", channel_name);
591 goto error;
9f19cc17 592 }
f3ed775e 593
9f19cc17 594 ret = CMD_SUCCESS;
f3ed775e
DG
595
596error:
ae856491
DG
597 free(channels);
598
599error_channels:
f3ed775e
DG
600 return ret;
601}
602
603/*
9f19cc17 604 * List available tracing session. List only basic information.
f3ed775e 605 *
9f19cc17 606 * If session_name is NULL, all sessions are listed.
f3ed775e 607 */
9f19cc17 608static int list_sessions(const char *session_name)
f3ed775e 609{
9f19cc17
DG
610 int ret, count, i;
611 unsigned int session_found = 0;
612 struct lttng_session *sessions;
613
614 count = lttng_list_sessions(&sessions);
615 DBG("Session count %d", count);
616 if (count < 0) {
617 ret = count;
8f3f773f 618 ERR("%s", lttng_strerror(ret));
9f19cc17 619 goto error;
d32fb093
DG
620 } else if (count == 0) {
621 MSG("Currently no available tracing session");
622 goto end;
9f19cc17
DG
623 }
624
625 if (session_name == NULL) {
626 MSG("Available tracing sessions:");
627 }
628
629 for (i = 0; i < count; i++) {
630 if (session_name != NULL) {
631 if (strncmp(sessions[i].name, session_name, NAME_MAX) == 0) {
632 session_found = 1;
2cbf8fed
DG
633 MSG("Tracing session %s: [%s%s]", session_name,
634 active_string(sessions[i].enabled),
635 snapshot_string(sessions[i].snapshot_mode));
9f19cc17
DG
636 MSG("%sTrace path: %s\n", indent4, sessions[i].path);
637 break;
638 }
3842aa86 639 } else {
2cbf8fed
DG
640 MSG(" %d) %s (%s) [%s%s]", i + 1, sessions[i].name, sessions[i].path,
641 active_string(sessions[i].enabled),
642 snapshot_string(sessions[i].snapshot_mode));
9f19cc17
DG
643 }
644 }
645
646 free(sessions);
647
648 if (!session_found && session_name != NULL) {
b6913340
DG
649 ERR("Session '%s' not found", session_name);
650 ret = CMD_ERROR;
651 goto error;
9f19cc17
DG
652 }
653
654 if (session_name == NULL) {
6e0de39b 655 MSG("\nUse lttng list <session_name> for more details");
9f19cc17 656 }
f3ed775e 657
d32fb093 658end:
f3ed775e
DG
659 return CMD_SUCCESS;
660
661error:
662 return ret;
663}
f3ed775e
DG
664
665/*
9f19cc17 666 * List available domain(s) for a session.
f3ed775e 667 */
330be774 668static int list_domains(const char *session_name)
f3ed775e 669{
9f19cc17
DG
670 int i, count, ret = CMD_SUCCESS;
671 struct lttng_domain *domains = NULL;
672
673 MSG("Domains:\n-------------");
674
330be774 675 count = lttng_list_domains(session_name, &domains);
9f19cc17
DG
676 if (count < 0) {
677 ret = count;
60e835ca 678 ERR("%s", lttng_strerror(ret));
9f19cc17
DG
679 goto error;
680 } else if (count == 0) {
681 MSG(" None");
682 goto end;
683 }
684
685 for (i = 0; i < count; i++) {
686 switch (domains[i].type) {
687 case LTTNG_DOMAIN_KERNEL:
688 MSG(" - Kernel");
b551a063
DG
689 break;
690 case LTTNG_DOMAIN_UST:
691 MSG(" - UST global");
692 break;
9f19cc17
DG
693 default:
694 break;
695 }
696 }
697
698end:
699 free(domains);
700
701error:
702 return ret;
f3ed775e 703}
f3ed775e
DG
704
705/*
9f19cc17 706 * The 'list <options>' first level command
f3ed775e
DG
707 */
708int cmd_list(int argc, const char **argv)
709{
c617c0c6 710 int opt, ret = CMD_SUCCESS;
9f19cc17 711 const char *session_name;
f3ed775e 712 static poptContext pc;
9f19cc17
DG
713 struct lttng_domain domain;
714 struct lttng_domain *domains = NULL;
f3ed775e 715
441c16a7
MD
716 memset(&domain, 0, sizeof(domain));
717
9f19cc17 718 if (argc < 1) {
f3ed775e 719 usage(stderr);
ca1c3607 720 ret = CMD_ERROR;
f3ed775e
DG
721 goto end;
722 }
723
724 pc = poptGetContext(NULL, argc, argv, long_options, 0);
725 poptReadDefaultConfig(pc, 0);
726
727 while ((opt = poptGetNextOpt(pc)) != -1) {
728 switch (opt) {
729 case OPT_HELP:
ca1c3607 730 usage(stdout);
f3ed775e 731 goto end;
eeac7d46
MD
732 case OPT_USERSPACE:
733 opt_userspace = 1;
eeac7d46 734 break;
679b4943
SM
735 case OPT_LIST_OPTIONS:
736 list_cmd_options(stdout, long_options);
679b4943 737 goto end;
f3ed775e
DG
738 default:
739 usage(stderr);
740 ret = CMD_UNDEFINED;
741 goto end;
742 }
743 }
744
9f19cc17
DG
745 /* Get session name (trailing argument) */
746 session_name = poptGetArg(pc);
b551a063 747 DBG2("Session name: %s", session_name);
9f19cc17 748
cd80958d
DG
749 if (opt_kernel) {
750 domain.type = LTTNG_DOMAIN_KERNEL;
b551a063
DG
751 } else if (opt_userspace) {
752 DBG2("Listing userspace global domain");
753 domain.type = LTTNG_DOMAIN_UST;
cd80958d
DG
754 }
755
aa5de748
MD
756 if (opt_kernel || opt_userspace) {
757 handle = lttng_create_handle(session_name, &domain);
758 if (handle == NULL) {
759 ret = CMD_FATAL;
760 goto end;
761 }
cd80958d
DG
762 }
763
9f19cc17 764 if (session_name == NULL) {
b551a063
DG
765 if (!opt_kernel && !opt_userspace) {
766 ret = list_sessions(NULL);
b6913340 767 if (ret != 0) {
b551a063
DG
768 goto end;
769 }
770 }
9f19cc17
DG
771 if (opt_kernel) {
772 ret = list_kernel_events();
773 if (ret < 0) {
6191db77 774 ret = CMD_ERROR;
9f19cc17
DG
775 goto end;
776 }
b551a063
DG
777 }
778 if (opt_userspace) {
f37d259d
MD
779 if (opt_fields) {
780 ret = list_ust_event_fields();
781 } else {
782 ret = list_ust_events();
783 }
9f19cc17 784 if (ret < 0) {
6191db77 785 ret = CMD_ERROR;
9f19cc17
DG
786 goto end;
787 }
788 }
789 } else {
790 /* List session attributes */
791 ret = list_sessions(session_name);
b6913340 792 if (ret != 0) {
9f19cc17
DG
793 goto end;
794 }
795
796 /* Domain listing */
797 if (opt_domain) {
330be774 798 ret = list_domains(session_name);
9f19cc17
DG
799 goto end;
800 }
801
802 if (opt_kernel) {
9f19cc17 803 /* Channel listing */
cd80958d 804 ret = list_channels(opt_channel);
9f19cc17
DG
805 if (ret < 0) {
806 goto end;
807 }
9f19cc17 808 } else {
c617c0c6
MD
809 int i, nb_domain;
810
9f19cc17 811 /* We want all domain(s) */
330be774 812 nb_domain = lttng_list_domains(session_name, &domains);
b551a063 813 if (nb_domain < 0) {
67635185 814 ret = nb_domain;
60e835ca 815 ERR("%s", lttng_strerror(ret));
9f19cc17
DG
816 goto end;
817 }
818
b551a063 819 for (i = 0; i < nb_domain; i++) {
9f19cc17
DG
820 switch (domains[i].type) {
821 case LTTNG_DOMAIN_KERNEL:
822 MSG("=== Domain: Kernel ===\n");
823 break;
b551a063
DG
824 case LTTNG_DOMAIN_UST:
825 MSG("=== Domain: UST global ===\n");
88c5f0d8
DG
826 MSG("Buffer type: %s\n",
827 domains[i].buf_type ==
828 LTTNG_BUFFER_PER_PID ? "per PID" : "per UID");
b551a063 829 break;
9f19cc17
DG
830 default:
831 MSG("=== Domain: Unimplemented ===\n");
832 break;
833 }
834
cd80958d 835 /* Clean handle before creating a new one */
aa5de748
MD
836 if (handle) {
837 lttng_destroy_handle(handle);
838 }
cd80958d
DG
839
840 handle = lttng_create_handle(session_name, &domains[i]);
841 if (handle == NULL) {
ca1c3607 842 ret = CMD_FATAL;
cd80958d
DG
843 goto end;
844 }
845
846 ret = list_channels(opt_channel);
9f19cc17
DG
847 if (ret < 0) {
848 goto end;
849 }
850 }
851 }
f3ed775e
DG
852 }
853
854end:
0e428499 855 free(domains);
aa5de748
MD
856 if (handle) {
857 lttng_destroy_handle(handle);
858 }
cd80958d 859
ca1c3607 860 poptFreeContext(pc);
f3ed775e
DG
861 return ret;
862}
This page took 0.12416 seconds and 4 git commands to generate.