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