Add the lttng view command
[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},
0c95f5b2 79 { "view", cmd_view},
f3ed775e
DG
80 { NULL, NULL} /* Array closure */
81};
82
83static void usage(FILE *ofp)
8c0faa1d 84{
f3ed775e 85 fprintf(ofp, "LTTng Trace Control " VERSION"\n\n");
852fdd0c 86 fprintf(ofp, "usage: lttng [OPTIONS] <COMMAND>\n");
f3ed775e
DG
87 fprintf(ofp, "\n");
88 fprintf(ofp, "Options:\n");
99bab54f
TD
89 fprintf(ofp, " -h, --help Show this help\n");
90 fprintf(ofp, " --list-options Simple listing of lttng options\n");
91 fprintf(ofp, " --list-commands Simple listing of lttng commands\n");
92 fprintf(ofp, " -v, --verbose Increase verbosity\n");
93 fprintf(ofp, " -q, --quiet Quiet mode\n");
94 fprintf(ofp, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
95 fprintf(ofp, " -n, --no-sessiond Don't spawn a session daemon\n");
96 fprintf(ofp, " --sessiond-path PATH Session daemon full path\n");
f3ed775e
DG
97 fprintf(ofp, "\n");
98 fprintf(ofp, "Commands:\n");
852fdd0c 99 fprintf(ofp, " add-context Add context to event and/or channel\n");
d0254c7c 100 fprintf(ofp, " calibrate Quantify LTTng overhead\n");
f3ed775e 101 fprintf(ofp, " create Create tracing session\n");
852fdd0c 102 fprintf(ofp, " destroy Tear down tracing session\n");
d36b8583 103 fprintf(ofp, " enable-channel Enable tracing channel\n");
26cc6b4e
DG
104 fprintf(ofp, " enable-event Enable tracing event\n");
105 fprintf(ofp, " disable-channel Disable tracing channel\n");
f3ed775e
DG
106 fprintf(ofp, " disable-event Disable tracing event\n");
107 fprintf(ofp, " list List possible tracing options\n");
3087ef18 108 fprintf(ofp, " set-session Set current session name\n");
f3ed775e
DG
109 fprintf(ofp, " start Start tracing\n");
110 fprintf(ofp, " stop Stop tracing\n");
111 fprintf(ofp, " version Show version information\n");
0c95f5b2 112 fprintf(ofp, " view Start trace viewer\n");
f3ed775e 113 fprintf(ofp, "\n");
99bab54f
TD
114 fprintf(ofp, "Each command also has its own -h, --help option.\n");
115 fprintf(ofp, "\n");
f3ed775e
DG
116 fprintf(ofp, "Please see the lttng(1) man page for full documentation.\n");
117 fprintf(ofp, "See http://lttng.org for updates, bug reports and news.\n");
8c0faa1d
DG
118}
119
865abf65
SM
120/*
121 * list_options
122 *
123 * List options line by line. This is mostly for bash auto completion and to
124 * avoid difficult parsing.
125 */
126static void list_options(FILE *ofp)
127{
128 int i = 0;
129 struct option *option = NULL;
130
131 option = &long_options[i];
132 while (option->name != NULL) {
133 fprintf(ofp, "--%s\n", option->name);
134
135 if (isprint(option->val)) {
136 fprintf(ofp, "-%c\n", option->val);
137 }
138
139 i++;
140 option = &long_options[i];
141 }
142}
143
144/*
145 * list_commands
146 *
147 * List commands line by line. This is mostly for bash auto completion and to
148 * avoid difficult parsing.
149 */
150static void list_commands(FILE *ofp)
151{
152 int i = 0;
153 struct cmd_struct *cmd = NULL;
154
155 cmd = &commands[i];
156 while (cmd->name != NULL) {
157 fprintf(ofp, "%s\n", cmd->name);
158 i++;
159 cmd = &commands[i];
160 }
161}
162
7442b2ba 163/*
f3ed775e 164 * clean_exit
96243366 165 */
f3ed775e 166static void clean_exit(int code)
96243366 167{
f3ed775e
DG
168 DBG("Clean exit");
169 exit(code);
894be886 170}
96243366 171
894be886 172/*
f3ed775e 173 * sighandler
2ef84c95 174 *
f3ed775e 175 * Signal handler for the daemon
2ef84c95 176 */
f3ed775e 177static void sighandler(int sig)
2ef84c95 178{
8db8d1dc
DG
179 int status;
180
f3ed775e
DG
181 switch (sig) {
182 case SIGTERM:
99bab54f 183 DBG("SIGTERM caught");
f3ed775e
DG
184 clean_exit(EXIT_FAILURE);
185 break;
186 case SIGCHLD:
99bab54f 187 DBG("SIGCHLD caught");
8db8d1dc 188 waitpid(sessiond_pid, &status, 0);
5a532b68 189 recv_child_signal = 1;
8db8d1dc
DG
190 /* Indicate that the session daemon died */
191 sessiond_pid = 0;
192 ERR("Session daemon died (exit status %d)", WEXITSTATUS(status));
193 break;
194 case SIGUSR1:
195 /* Notify is done */
5a532b68 196 recv_child_signal = 1;
99bab54f 197 DBG("SIGUSR1 caught");
f3ed775e
DG
198 break;
199 default:
99bab54f 200 DBG("Unknown signal %d caught", sig);
f3ed775e 201 break;
2ef84c95
DG
202 }
203
f3ed775e 204 return;
2ef84c95
DG
205}
206
207/*
f3ed775e 208 * set_signal_handler
894be886 209 *
f3ed775e 210 * Setup signal handler for SIGCHLD and SIGTERM.
894be886 211 */
f3ed775e 212static int set_signal_handler(void)
894be886 213{
f3ed775e
DG
214 int ret = 0;
215 struct sigaction sa;
216 sigset_t sigset;
33a2b854 217
f3ed775e
DG
218 if ((ret = sigemptyset(&sigset)) < 0) {
219 perror("sigemptyset");
33a2b854
DG
220 goto end;
221 }
222
f3ed775e
DG
223 sa.sa_handler = sighandler;
224 sa.sa_mask = sigset;
225 sa.sa_flags = 0;
8db8d1dc 226 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
f3ed775e 227 perror("sigaction");
96243366
DG
228 goto end;
229 }
230
f3ed775e
DG
231 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
232 perror("sigaction");
233 goto end;
894be886
DG
234 }
235
8db8d1dc
DG
236 if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) {
237 perror("sigaction");
238 goto end;
239 }
240
96243366 241end:
57167058
DG
242 return ret;
243}
244
fac6795d 245/*
f3ed775e 246 * handle_command
fac6795d 247 *
f3ed775e
DG
248 * Handle the full argv list of a first level command. Will find the command
249 * in the global commands array and call the function callback associated.
1c9f7941 250 *
f3ed775e
DG
251 * If command not found, return -1
252 * else, return function command error code.
1c9f7941 253 */
f3ed775e 254static int handle_command(int argc, char **argv)
1c9f7941 255{
f3ed775e
DG
256 int i = 0, ret;
257 struct cmd_struct *cmd;
1c9f7941 258
f3ed775e
DG
259 if (*argv == NULL) {
260 ret = CMD_SUCCESS;
47b74d63 261 goto end;
1c9f7941
DG
262 }
263
f3ed775e
DG
264 cmd = &commands[i];
265 while (cmd->func != NULL) {
266 /* Find command */
267 if (strcmp(argv[0], cmd->name) == 0) {
268 ret = cmd->func(argc, (const char**) argv);
f3ed775e 269 goto end;
894be886 270 }
f3ed775e
DG
271 i++;
272 cmd = &commands[i];
894be886
DG
273 }
274
f3ed775e
DG
275 /* Command not found */
276 ret = -1;
894be886
DG
277
278end:
f3ed775e 279 return ret;
8548ff30
DG
280}
281
5b8719f5
DG
282/*
283 * spawn_sessiond
284 *
285 * Spawn a session daemon by forking and execv.
286 */
287static int spawn_sessiond(char *pathname)
288{
289 int ret = 0;
290 pid_t pid;
291
f3ed775e 292 MSG("Spawning a session daemon");
5a532b68 293 recv_child_signal = 0;
5b8719f5
DG
294 pid = fork();
295 if (pid == 0) {
5e16da05
MD
296 /*
297 * Spawn session daemon and tell
5b8719f5
DG
298 * it to signal us when ready.
299 */
32258573 300 execlp(pathname, "lttng-sessiond", "--sig-parent", "--quiet", NULL);
5e16da05
MD
301 /* execlp only returns if error happened */
302 if (errno == ENOENT) {
303 ERR("No session daemon found. Use --sessiond-path.");
304 } else {
305 perror("execlp");
5b8719f5 306 }
5a532b68 307 kill(getppid(), SIGTERM); /* wake parent */
5e16da05 308 exit(EXIT_FAILURE);
5b8719f5 309 } else if (pid > 0) {
8db8d1dc 310 sessiond_pid = pid;
5a532b68 311 /*
af87c45a
DG
312 * Wait for lttng-sessiond to start. We need to use a flag to check if
313 * the signal has been sent to us, because the child can be scheduled
314 * before the parent, and thus send the signal before this check. In
315 * the signal handler, we set the recv_child_signal flag, so anytime we
316 * check it after the fork is fine. Note that sleep() is interrupted
317 * before the 1 second delay as soon as the signal is received, so it
318 * will not cause visible delay for the user.
5a532b68
MD
319 */
320 while (!recv_child_signal) {
321 sleep(1);
322 }
af87c45a
DG
323 /*
324 * The signal handler will nullify sessiond_pid on SIGCHLD
325 */
8db8d1dc
DG
326 if (!sessiond_pid) {
327 exit(EXIT_FAILURE);
328 }
5b8719f5
DG
329 goto end;
330 } else {
331 perror("fork");
332 ret = -1;
333 goto end;
334 }
335
336end:
337 return ret;
338}
339
fac6795d 340/*
f3ed775e 341 * check_sessiond
fac6795d
DG
342 *
343 * Check if the session daemon is available using
5b8719f5
DG
344 * the liblttngctl API for the check. If not, try to
345 * spawn a daemon.
fac6795d 346 */
f3ed775e 347static int check_sessiond(void)
fac6795d
DG
348{
349 int ret;
5e16da05 350 char *pathname = NULL, *alloc_pathname = NULL;
fac6795d 351
947308c4
DG
352 ret = lttng_session_daemon_alive();
353 if (ret == 0) { /* not alive */
5b8719f5
DG
354 /* Try command line option path */
355 if (opt_sessiond_path != NULL) {
356 ret = access(opt_sessiond_path, F_OK | X_OK);
357 if (ret < 0) {
3183dbb0 358 ERR("No such file or access denied: %s", opt_sessiond_path);
5b8719f5
DG
359 goto end;
360 }
361 pathname = opt_sessiond_path;
362 } else {
363 /* Try LTTNG_SESSIOND_PATH env variable */
bbccc3d2 364 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
5b8719f5
DG
365 }
366
367 /* Let's rock and roll */
368 if (pathname == NULL) {
32258573 369 ret = asprintf(&alloc_pathname, INSTALL_BIN_PATH "/lttng-sessiond");
5b8719f5 370 if (ret < 0) {
3f5fa9ed 371 perror("asprintf spawn sessiond");
5b8719f5
DG
372 goto end;
373 }
5e16da05 374 pathname = alloc_pathname;
5b8719f5
DG
375 }
376
377 ret = spawn_sessiond(pathname);
5e16da05 378 free(alloc_pathname);
5b8719f5 379 if (ret < 0) {
3183dbb0 380 ERR("Problem occurred when starting %s", pathname);
5b8719f5
DG
381 goto end;
382 }
fac6795d
DG
383 }
384
5b8719f5 385end:
0c95f5b2 386 printf("HIT essiond %d\n", ret);
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.059731 seconds and 5 git commands to generate.