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