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