Fix: Don't report an error when listing a session with no channels
[lttng-tools.git] / src / bin / lttng / lttng.c
CommitLineData
826d496d
MD
1/*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
fac6795d
DG
3 *
4 * This program is free software; you can redistribute it and/or modify
d14d33bf
AM
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
fac6795d
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 *
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.
fac6795d
DG
16 */
17
18#define _GNU_SOURCE
fac6795d 19#include <getopt.h>
f3ed775e 20#include <signal.h>
fac6795d
DG
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
8db8d1dc
DG
24#include <sys/types.h>
25#include <sys/wait.h>
fac6795d 26#include <unistd.h>
3bd1e081 27#include <config.h>
730389d9 28#include <ctype.h>
fac6795d 29
5b97ec60 30#include <lttng/lttng.h>
db758600 31#include <common/error.h>
fac6795d 32
c399183f 33#include "command.h"
fac6795d
DG
34
35/* Variables */
36static char *progname;
f3ed775e
DG
37static int opt_no_sessiond;
38static char *opt_sessiond_path;
8db8d1dc 39static pid_t sessiond_pid;
5a532b68 40static volatile int recv_child_signal;
f3ed775e 41
8960e9cd
DG
42char *opt_relayd_path;
43
f3ed775e 44enum {
8960e9cd 45 OPT_RELAYD_PATH,
f3ed775e 46 OPT_SESSION_PATH,
865abf65
SM
47 OPT_DUMP_OPTIONS,
48 OPT_DUMP_COMMANDS,
f3ed775e
DG
49};
50
51/* Getopt options. No first level command. */
52static struct option long_options[] = {
70318c38 53 {"version", 0, NULL, 'V'},
f3ed775e
DG
54 {"help", 0, NULL, 'h'},
55 {"group", 1, NULL, 'g'},
56 {"verbose", 0, NULL, 'v'},
57 {"quiet", 0, NULL, 'q'},
8490ae7b 58 {"no-sessiond", 0, NULL, 'n'},
f3ed775e 59 {"sessiond-path", 1, NULL, OPT_SESSION_PATH},
8960e9cd 60 {"relayd-path", 1, NULL, OPT_RELAYD_PATH},
865abf65
SM
61 {"list-options", 0, NULL, OPT_DUMP_OPTIONS},
62 {"list-commands", 0, NULL, OPT_DUMP_COMMANDS},
f3ed775e
DG
63 {NULL, 0, NULL, 0}
64};
65
66/* First level command */
67static struct cmd_struct commands[] = {
68 { "list", cmd_list},
69 { "create", cmd_create},
70 { "destroy", cmd_destroy},
f3ed775e
DG
71 { "start", cmd_start},
72 { "stop", cmd_stop},
73 { "enable-event", cmd_enable_events},
e953ef25 74 { "disable-event", cmd_disable_events},
d36b8583 75 { "enable-channel", cmd_enable_channels},
26cc6b4e 76 { "disable-channel", cmd_disable_channels},
d65106b1 77 { "add-context", cmd_add_context},
3087ef18 78 { "set-session", cmd_set_session},
eb9cb8b7 79 { "version", cmd_version},
d0254c7c 80 { "calibrate", cmd_calibrate},
0c95f5b2 81 { "view", cmd_view},
57f272ed 82 { "snapshot", cmd_snapshot},
c864d6d7 83 { "save", cmd_save},
8c42d845 84 { "load", cmd_load},
9c6bda17
DG
85 { "enable-consumer", cmd_enable_consumer}, /* OBSOLETE */
86 { "disable-consumer", cmd_disable_consumer}, /* OBSOLETE */
f3ed775e
DG
87 { NULL, NULL} /* Array closure */
88};
89
90static void usage(FILE *ofp)
8c0faa1d 91{
88564da0 92 fprintf(ofp, "LTTng Trace Control " FULL_VERSION" - " VERSION_NAME"\n\n");
32a6298d 93 fprintf(ofp, "usage: lttng [OPTIONS] <COMMAND> [<ARGS>]\n");
f3ed775e
DG
94 fprintf(ofp, "\n");
95 fprintf(ofp, "Options:\n");
70318c38 96 fprintf(ofp, " -V, --version Show version\n");
99bab54f
TD
97 fprintf(ofp, " -h, --help Show this help\n");
98 fprintf(ofp, " --list-options Simple listing of lttng options\n");
99 fprintf(ofp, " --list-commands Simple listing of lttng commands\n");
100 fprintf(ofp, " -v, --verbose Increase verbosity\n");
101 fprintf(ofp, " -q, --quiet Quiet mode\n");
102 fprintf(ofp, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
103 fprintf(ofp, " -n, --no-sessiond Don't spawn a session daemon\n");
104 fprintf(ofp, " --sessiond-path PATH Session daemon full path\n");
8960e9cd 105 fprintf(ofp, " --relayd-path PATH Relayd daemon full path\n");
f3ed775e
DG
106 fprintf(ofp, "\n");
107 fprintf(ofp, "Commands:\n");
00e2e675
DG
108 fprintf(ofp, " add-context Add context to event and/or channel\n");
109 fprintf(ofp, " calibrate Quantify LTTng overhead\n");
110 fprintf(ofp, " create Create tracing session\n");
111 fprintf(ofp, " destroy Tear down tracing session\n");
112 fprintf(ofp, " enable-channel Enable tracing channel\n");
113 fprintf(ofp, " enable-event Enable tracing event\n");
114 fprintf(ofp, " disable-channel Disable tracing channel\n");
115 fprintf(ofp, " disable-event Disable tracing event\n");
00e2e675
DG
116 fprintf(ofp, " list List possible tracing options\n");
117 fprintf(ofp, " set-session Set current session name\n");
57f272ed 118 fprintf(ofp, " snapshot Snapshot buffers of current session name\n");
00e2e675
DG
119 fprintf(ofp, " start Start tracing\n");
120 fprintf(ofp, " stop Stop tracing\n");
121 fprintf(ofp, " version Show version information\n");
122 fprintf(ofp, " view Start trace viewer\n");
c864d6d7 123 fprintf(ofp, " save Save session configuration\n");
8c42d845 124 fprintf(ofp, " load Load session configuration\n");
f3ed775e 125 fprintf(ofp, "\n");
99bab54f
TD
126 fprintf(ofp, "Each command also has its own -h, --help option.\n");
127 fprintf(ofp, "\n");
f3ed775e
DG
128 fprintf(ofp, "Please see the lttng(1) man page for full documentation.\n");
129 fprintf(ofp, "See http://lttng.org for updates, bug reports and news.\n");
8c0faa1d
DG
130}
131
70318c38
SS
132static void version(FILE *ofp)
133{
88564da0 134 fprintf(ofp, "%s (LTTng Trace Control) " FULL_VERSION" - " VERSION_NAME"\n",
70318c38
SS
135 progname);
136}
137
865abf65
SM
138/*
139 * list_options
140 *
141 * List options line by line. This is mostly for bash auto completion and to
142 * avoid difficult parsing.
143 */
144static void list_options(FILE *ofp)
145{
146 int i = 0;
147 struct option *option = NULL;
148
149 option = &long_options[i];
150 while (option->name != NULL) {
151 fprintf(ofp, "--%s\n", option->name);
152
153 if (isprint(option->val)) {
154 fprintf(ofp, "-%c\n", option->val);
155 }
156
157 i++;
158 option = &long_options[i];
159 }
160}
161
7442b2ba 162/*
f3ed775e 163 * clean_exit
96243366 164 */
f3ed775e 165static void clean_exit(int code)
96243366 166{
f3ed775e
DG
167 DBG("Clean exit");
168 exit(code);
894be886 169}
96243366 170
894be886 171/*
f3ed775e 172 * sighandler
2ef84c95 173 *
f3ed775e 174 * Signal handler for the daemon
2ef84c95 175 */
f3ed775e 176static void sighandler(int sig)
2ef84c95 177{
8db8d1dc
DG
178 int status;
179
f3ed775e
DG
180 switch (sig) {
181 case SIGTERM:
99bab54f 182 DBG("SIGTERM caught");
f3ed775e
DG
183 clean_exit(EXIT_FAILURE);
184 break;
185 case SIGCHLD:
99bab54f 186 DBG("SIGCHLD caught");
8db8d1dc 187 waitpid(sessiond_pid, &status, 0);
5a532b68 188 recv_child_signal = 1;
8db8d1dc
DG
189 /* Indicate that the session daemon died */
190 sessiond_pid = 0;
191 ERR("Session daemon died (exit status %d)", WEXITSTATUS(status));
192 break;
193 case SIGUSR1:
194 /* Notify is done */
5a532b68 195 recv_child_signal = 1;
99bab54f 196 DBG("SIGUSR1 caught");
f3ed775e
DG
197 break;
198 default:
99bab54f 199 DBG("Unknown signal %d caught", sig);
f3ed775e 200 break;
2ef84c95
DG
201 }
202
f3ed775e 203 return;
2ef84c95
DG
204}
205
206/*
f3ed775e 207 * set_signal_handler
894be886 208 *
f3ed775e 209 * Setup signal handler for SIGCHLD and SIGTERM.
894be886 210 */
f3ed775e 211static int set_signal_handler(void)
894be886 212{
f3ed775e
DG
213 int ret = 0;
214 struct sigaction sa;
215 sigset_t sigset;
33a2b854 216
f3ed775e
DG
217 if ((ret = sigemptyset(&sigset)) < 0) {
218 perror("sigemptyset");
33a2b854
DG
219 goto end;
220 }
221
f3ed775e
DG
222 sa.sa_handler = sighandler;
223 sa.sa_mask = sigset;
224 sa.sa_flags = 0;
8db8d1dc 225 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
f3ed775e 226 perror("sigaction");
96243366
DG
227 goto end;
228 }
229
f3ed775e
DG
230 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
231 perror("sigaction");
232 goto end;
894be886
DG
233 }
234
8db8d1dc
DG
235 if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) {
236 perror("sigaction");
237 goto end;
238 }
239
96243366 240end:
57167058
DG
241 return ret;
242}
243
fac6795d 244/*
f3ed775e 245 * handle_command
fac6795d 246 *
f3ed775e
DG
247 * Handle the full argv list of a first level command. Will find the command
248 * in the global commands array and call the function callback associated.
1c9f7941 249 *
f3ed775e
DG
250 * If command not found, return -1
251 * else, return function command error code.
1c9f7941 252 */
f3ed775e 253static int handle_command(int argc, char **argv)
1c9f7941 254{
f3ed775e
DG
255 int i = 0, ret;
256 struct cmd_struct *cmd;
1c9f7941 257
f3ed775e
DG
258 if (*argv == NULL) {
259 ret = CMD_SUCCESS;
47b74d63 260 goto end;
1c9f7941
DG
261 }
262
f3ed775e
DG
263 cmd = &commands[i];
264 while (cmd->func != NULL) {
265 /* Find command */
266 if (strcmp(argv[0], cmd->name) == 0) {
267 ret = cmd->func(argc, (const char**) argv);
f3ed775e 268 goto end;
894be886 269 }
f3ed775e
DG
270 i++;
271 cmd = &commands[i];
894be886
DG
272 }
273
f3ed775e 274 /* Command not found */
4ce78777 275 ret = CMD_UNDEFINED;
894be886
DG
276
277end:
f3ed775e 278 return ret;
8548ff30
DG
279}
280
5b8719f5
DG
281/*
282 * spawn_sessiond
283 *
284 * Spawn a session daemon by forking and execv.
285 */
286static int spawn_sessiond(char *pathname)
287{
288 int ret = 0;
289 pid_t pid;
290
f3ed775e 291 MSG("Spawning a session daemon");
5a532b68 292 recv_child_signal = 0;
5b8719f5
DG
293 pid = fork();
294 if (pid == 0) {
5e16da05
MD
295 /*
296 * Spawn session daemon and tell
5b8719f5
DG
297 * it to signal us when ready.
298 */
32258573 299 execlp(pathname, "lttng-sessiond", "--sig-parent", "--quiet", NULL);
5e16da05
MD
300 /* execlp only returns if error happened */
301 if (errno == ENOENT) {
302 ERR("No session daemon found. Use --sessiond-path.");
303 } else {
304 perror("execlp");
5b8719f5 305 }
5a532b68 306 kill(getppid(), SIGTERM); /* wake parent */
5e16da05 307 exit(EXIT_FAILURE);
5b8719f5 308 } else if (pid > 0) {
8db8d1dc 309 sessiond_pid = pid;
5a532b68 310 /*
af87c45a
DG
311 * Wait for lttng-sessiond to start. We need to use a flag to check if
312 * the signal has been sent to us, because the child can be scheduled
313 * before the parent, and thus send the signal before this check. In
314 * the signal handler, we set the recv_child_signal flag, so anytime we
315 * check it after the fork is fine. Note that sleep() is interrupted
316 * before the 1 second delay as soon as the signal is received, so it
317 * will not cause visible delay for the user.
5a532b68
MD
318 */
319 while (!recv_child_signal) {
320 sleep(1);
321 }
af87c45a
DG
322 /*
323 * The signal handler will nullify sessiond_pid on SIGCHLD
324 */
8db8d1dc
DG
325 if (!sessiond_pid) {
326 exit(EXIT_FAILURE);
327 }
5b8719f5
DG
328 goto end;
329 } else {
330 perror("fork");
331 ret = -1;
332 goto end;
333 }
334
335end:
336 return ret;
337}
338
fac6795d 339/*
f3ed775e 340 * check_sessiond
fac6795d
DG
341 *
342 * Check if the session daemon is available using
5b8719f5
DG
343 * the liblttngctl API for the check. If not, try to
344 * spawn a daemon.
fac6795d 345 */
f3ed775e 346static int check_sessiond(void)
fac6795d
DG
347{
348 int ret;
b8ec3da8 349 char *pathname = NULL;
fac6795d 350
947308c4
DG
351 ret = lttng_session_daemon_alive();
352 if (ret == 0) { /* not alive */
5b8719f5 353 /* Try command line option path */
b8ec3da8
SM
354 pathname = opt_sessiond_path;
355
356 /* Try LTTNG_SESSIOND_PATH env variable */
357 if (pathname == NULL) {
bbccc3d2 358 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
5b8719f5
DG
359 }
360
b8ec3da8 361 /* Try with configured path */
5b8719f5 362 if (pathname == NULL) {
b8ec3da8
SM
363 if (CONFIG_SESSIOND_BIN[0] != '\0') {
364 pathname = CONFIG_SESSIOND_BIN;
5b8719f5 365 }
b8ec3da8
SM
366 }
367
368 /* Let's rock and roll while trying the default path */
369 if (pathname == NULL) {
370 pathname = INSTALL_BIN_PATH "/lttng-sessiond";
371 }
372
373 DBG("Session daemon at: %s", pathname);
374
375 /* Check existence and permissions */
376 ret = access(pathname, F_OK | X_OK);
377 if (ret < 0) {
378 ERR("No such file or access denied: %s", pathname);
379 goto end;
5b8719f5
DG
380 }
381
382 ret = spawn_sessiond(pathname);
5b8719f5 383 if (ret < 0) {
3183dbb0 384 ERR("Problem occurred when starting %s", pathname);
5b8719f5 385 }
c2a79a67 386 }
5b8719f5 387end:
fac6795d
DG
388 return ret;
389}
390
bcfa8a05 391/*
679b4943
SM
392 * Check args for specific options that *must* not trigger a session daemon
393 * execution.
394 *
395 * Return 1 if match else 0.
bcfa8a05 396 */
679b4943 397static int check_args_no_sessiond(int argc, char **argv)
bcfa8a05
DG
398{
399 int i;
400
401 for (i = 0; i < argc; i++) {
ba2926ef
MD
402 if ((strncmp(argv[i], "-h", sizeof("-h")) == 0) ||
403 strncmp(argv[i], "--h", sizeof("--h")) == 0 ||
3183dbb0 404 strncmp(argv[i], "--list-options", sizeof("--list-options")) == 0 ||
4747a49b 405 strncmp(argv[i], "--list-commands", sizeof("--list-commands")) == 0 ||
0c95f5b2
DG
406 strncmp(argv[i], "version", sizeof("version")) == 0 ||
407 strncmp(argv[i], "view", sizeof("view")) == 0) {
bcfa8a05
DG
408 return 1;
409 }
410 }
411
412 return 0;
413}
414
5b8719f5 415/*
3183dbb0 416 * Parse command line arguments.
5b8719f5 417 *
3183dbb0 418 * Return 0 if OK, else -1
5b8719f5 419 */
f3ed775e 420static int parse_args(int argc, char **argv)
5b8719f5 421{
f3ed775e 422 int opt, ret;
5b8719f5 423
f3ed775e
DG
424 if (argc < 2) {
425 usage(stderr);
426 clean_exit(EXIT_FAILURE);
5b8719f5
DG
427 }
428
70318c38 429 while ((opt = getopt_long(argc, argv, "+Vhnvqg:", long_options, NULL)) != -1) {
f3ed775e 430 switch (opt) {
70318c38
SS
431 case 'V':
432 version(stdout);
433 ret = 0;
434 goto end;
f3ed775e 435 case 'h':
3183dbb0 436 usage(stdout);
ae856491 437 ret = 0;
3183dbb0 438 goto end;
f3ed775e 439 case 'v':
849e5b7b
DG
440 /* There is only 3 possible level of verbosity. (-vvv) */
441 if (lttng_opt_verbose < 3) {
442 lttng_opt_verbose += 1;
443 }
f3ed775e
DG
444 break;
445 case 'q':
97e19046 446 lttng_opt_quiet = 1;
f3ed775e
DG
447 break;
448 case 'g':
449 lttng_set_tracing_group(optarg);
450 break;
8490ae7b 451 case 'n':
f3ed775e
DG
452 opt_no_sessiond = 1;
453 break;
454 case OPT_SESSION_PATH:
455 opt_sessiond_path = strdup(optarg);
456 break;
8960e9cd
DG
457 case OPT_RELAYD_PATH:
458 opt_relayd_path = strdup(optarg);
459 break;
865abf65
SM
460 case OPT_DUMP_OPTIONS:
461 list_options(stdout);
462 ret = 0;
3183dbb0 463 goto end;
865abf65 464 case OPT_DUMP_COMMANDS:
3c9bd23c 465 list_commands(commands, stdout);
865abf65 466 ret = 0;
3183dbb0 467 goto end;
f3ed775e
DG
468 default:
469 usage(stderr);
ae856491 470 ret = 1;
f3ed775e
DG
471 goto error;
472 }
5b8719f5 473 }
fac6795d 474
f3ed775e 475 /* If both options are specified, quiet wins */
97e19046
DG
476 if (lttng_opt_verbose && lttng_opt_quiet) {
477 lttng_opt_verbose = 0;
5b8719f5
DG
478 }
479
f3ed775e 480 /* Spawn session daemon if needed */
679b4943 481 if (opt_no_sessiond == 0 && check_args_no_sessiond(argc, argv) == 0 &&
bcfa8a05 482 (check_sessiond() < 0)) {
8005f29a 483 ret = 1;
f3ed775e 484 goto error;
5b8719f5
DG
485 }
486
f3ed775e
DG
487 /* No leftovers, print usage and quit */
488 if ((argc - optind) == 0) {
489 usage(stderr);
8005f29a 490 ret = 1;
f3ed775e
DG
491 goto error;
492 }
7442b2ba 493
f3ed775e
DG
494 /*
495 * Handle leftovers which is a first level command with the trailing
496 * options.
497 */
498 ret = handle_command(argc - optind, argv + optind);
ae856491
DG
499 switch (ret) {
500 case CMD_WARNING:
501 WARN("Some command(s) went wrong");
502 break;
503 case CMD_ERROR:
504 ERR("Command error");
505 break;
506 case CMD_UNDEFINED:
507 ERR("Undefined command");
508 break;
509 case CMD_FATAL:
510 ERR("Fatal error");
511 break;
4ce78777
DG
512 case CMD_UNSUPPORTED:
513 ERR("Unsupported command");
514 break;
ae856491
DG
515 case -1:
516 usage(stderr);
517 ret = 1;
518 break;
519 case 0:
520 break;
521 default:
42224349
DG
522 if (ret < 0) {
523 ret = -ret;
524 }
ae856491 525 break;
96243366
DG
526 }
527
3183dbb0 528end:
f3ed775e 529error:
ae856491 530 return ret;
fac6795d
DG
531}
532
f3ed775e 533
fac6795d 534/*
5b8719f5 535 * main
fac6795d
DG
536 */
537int main(int argc, char *argv[])
538{
539 int ret;
cbbb813a 540 char *user;
fac6795d
DG
541
542 progname = argv[0] ? argv[0] : "lttng";
543
99bab54f 544 /* For Mathieu Desnoyers a.k.a. Dr. Tracing */
cbbb813a
DG
545 user = getenv("USER");
546 if (user != NULL && ((strncmp(progname, "drtrace", 7) == 0 ||
547 strncmp("compudj", user, 7) == 0))) {
4666e71c 548 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
fac6795d 549 }
cbbb813a 550 /* Thanks Mathieu */
fac6795d 551
5b8719f5
DG
552 ret = set_signal_handler();
553 if (ret < 0) {
87378cf5 554 clean_exit(ret);
5b8719f5
DG
555 }
556
f3ed775e 557 ret = parse_args(argc, argv);
ae856491
DG
558 if (ret != 0) {
559 clean_exit(ret);
1fff1faa 560 }
87378cf5 561
fac6795d
DG
562 return 0;
563}
This page took 0.067906 seconds and 4 git commands to generate.