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