Fix: out of tree build by adding missing ini.c include
[lttng-tools.git] / src / bin / lttng / lttng.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
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, version 2 only,
6 * as published by the Free Software Foundation.
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.
16 */
17
18#define _GNU_SOURCE
19#include <getopt.h>
20#include <signal.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/types.h>
25#include <sys/wait.h>
26#include <unistd.h>
27#include <config.h>
28#include <ctype.h>
29
30#include <lttng/lttng.h>
31#include <common/error.h>
32
33#include "command.h"
34
35/* Variables */
36static char *progname;
37static int opt_no_sessiond;
38static char *opt_sessiond_path;
39static pid_t sessiond_pid;
40static volatile int recv_child_signal;
41
42char *opt_relayd_path;
43
44enum {
45 OPT_RELAYD_PATH,
46 OPT_SESSION_PATH,
47 OPT_DUMP_OPTIONS,
48 OPT_DUMP_COMMANDS,
49};
50
51/* Getopt options. No first level command. */
52static struct option long_options[] = {
53 {"version", 0, NULL, 'V'},
54 {"help", 0, NULL, 'h'},
55 {"group", 1, NULL, 'g'},
56 {"verbose", 0, NULL, 'v'},
57 {"quiet", 0, NULL, 'q'},
58 {"mi", 1, NULL, 'm'},
59 {"no-sessiond", 0, NULL, 'n'},
60 {"sessiond-path", 1, NULL, OPT_SESSION_PATH},
61 {"relayd-path", 1, NULL, OPT_RELAYD_PATH},
62 {"list-options", 0, NULL, OPT_DUMP_OPTIONS},
63 {"list-commands", 0, NULL, OPT_DUMP_COMMANDS},
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},
72 { "start", cmd_start},
73 { "stop", cmd_stop},
74 { "enable-event", cmd_enable_events},
75 { "disable-event", cmd_disable_events},
76 { "enable-channel", cmd_enable_channels},
77 { "disable-channel", cmd_disable_channels},
78 { "add-context", cmd_add_context},
79 { "set-session", cmd_set_session},
80 { "version", cmd_version},
81 { "calibrate", cmd_calibrate},
82 { "view", cmd_view},
83 { "snapshot", cmd_snapshot},
84 { "save", cmd_save},
85 { "load", cmd_load},
86 { "enable-consumer", cmd_enable_consumer}, /* OBSOLETE */
87 { "disable-consumer", cmd_disable_consumer}, /* OBSOLETE */
88 { NULL, NULL} /* Array closure */
89};
90
91static void usage(FILE *ofp)
92{
93 fprintf(ofp, "LTTng Trace Control " VERSION " - " VERSION_NAME" - " GIT_VERSION "\n\n");
94 fprintf(ofp, "usage: lttng [OPTIONS] <COMMAND> [<ARGS>]\n");
95 fprintf(ofp, "\n");
96 fprintf(ofp, "Options:\n");
97 fprintf(ofp, " -V, --version Show version\n");
98 fprintf(ofp, " -h, --help Show this help\n");
99 fprintf(ofp, " --list-options Simple listing of lttng options\n");
100 fprintf(ofp, " --list-commands Simple listing of lttng commands\n");
101 fprintf(ofp, " -v, --verbose Increase verbosity\n");
102 fprintf(ofp, " -q, --quiet Quiet mode\n");
103 fprintf(ofp, " -m, --mi TYPE Machine Interface mode.\n");
104 fprintf(ofp, " Type: xml\n");
105 fprintf(ofp, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
106 fprintf(ofp, " -n, --no-sessiond Don't spawn a session daemon\n");
107 fprintf(ofp, " --sessiond-path PATH Session daemon full path\n");
108 fprintf(ofp, " --relayd-path PATH Relayd daemon full path\n");
109 fprintf(ofp, "\n");
110 fprintf(ofp, "Commands:\n");
111 fprintf(ofp, " add-context Add context to event and/or channel\n");
112 fprintf(ofp, " calibrate Quantify LTTng overhead\n");
113 fprintf(ofp, " create Create tracing session\n");
114 fprintf(ofp, " destroy Tear down tracing session\n");
115 fprintf(ofp, " enable-channel Enable tracing channel\n");
116 fprintf(ofp, " enable-event Enable tracing event\n");
117 fprintf(ofp, " disable-channel Disable tracing channel\n");
118 fprintf(ofp, " disable-event Disable tracing event\n");
119 fprintf(ofp, " list List possible tracing options\n");
120 fprintf(ofp, " set-session Set current session name\n");
121 fprintf(ofp, " snapshot Snapshot buffers of current session name\n");
122 fprintf(ofp, " start Start tracing\n");
123 fprintf(ofp, " stop Stop tracing\n");
124 fprintf(ofp, " version Show version information\n");
125 fprintf(ofp, " view Start trace viewer\n");
126 fprintf(ofp, " save Save session configuration\n");
127 fprintf(ofp, " load Load session configuration\n");
128 fprintf(ofp, "\n");
129 fprintf(ofp, "Each command also has its own -h, --help option.\n");
130 fprintf(ofp, "\n");
131 fprintf(ofp, "Please see the lttng(1) man page for full documentation.\n");
132 fprintf(ofp, "See http://lttng.org for updates, bug reports and news.\n");
133}
134
135static void version(FILE *ofp)
136{
137 fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME" - " GIT_VERSION "\n",
138 progname);
139}
140
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
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
184/*
185 * clean_exit
186 */
187static void clean_exit(int code)
188{
189 DBG("Clean exit");
190 exit(code);
191}
192
193/*
194 * sighandler
195 *
196 * Signal handler for the daemon
197 */
198static void sighandler(int sig)
199{
200 int status;
201
202 switch (sig) {
203 case SIGTERM:
204 DBG("SIGTERM caught");
205 clean_exit(EXIT_FAILURE);
206 break;
207 case SIGCHLD:
208 DBG("SIGCHLD caught");
209 waitpid(sessiond_pid, &status, 0);
210 recv_child_signal = 1;
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 */
217 recv_child_signal = 1;
218 DBG("SIGUSR1 caught");
219 break;
220 default:
221 DBG("Unknown signal %d caught", sig);
222 break;
223 }
224
225 return;
226}
227
228/*
229 * set_signal_handler
230 *
231 * Setup signal handler for SIGCHLD and SIGTERM.
232 */
233static int set_signal_handler(void)
234{
235 int ret = 0;
236 struct sigaction sa;
237 sigset_t sigset;
238
239 if ((ret = sigemptyset(&sigset)) < 0) {
240 perror("sigemptyset");
241 goto end;
242 }
243
244 sa.sa_handler = sighandler;
245 sa.sa_mask = sigset;
246 sa.sa_flags = 0;
247 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
248 perror("sigaction");
249 goto end;
250 }
251
252 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
253 perror("sigaction");
254 goto end;
255 }
256
257 if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) {
258 perror("sigaction");
259 goto end;
260 }
261
262end:
263 return ret;
264}
265
266/*
267 * handle_command
268 *
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.
271 *
272 * If command not found, return -1
273 * else, return function command error code.
274 */
275static int handle_command(int argc, char **argv)
276{
277 int i = 0, ret;
278 struct cmd_struct *cmd;
279
280 if (*argv == NULL) {
281 ret = CMD_SUCCESS;
282 goto end;
283 }
284
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);
290 goto end;
291 }
292 i++;
293 cmd = &commands[i];
294 }
295
296 /* Command not found */
297 ret = CMD_UNDEFINED;
298
299end:
300 return ret;
301}
302
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
313 MSG("Spawning a session daemon");
314 recv_child_signal = 0;
315 pid = fork();
316 if (pid == 0) {
317 /*
318 * Spawn session daemon and tell
319 * it to signal us when ready.
320 */
321 execlp(pathname, "lttng-sessiond", "--sig-parent", "--quiet", NULL);
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");
327 }
328 kill(getppid(), SIGTERM); /* wake parent */
329 exit(EXIT_FAILURE);
330 } else if (pid > 0) {
331 sessiond_pid = pid;
332 /*
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.
340 */
341 while (!recv_child_signal) {
342 sleep(1);
343 }
344 /*
345 * The signal handler will nullify sessiond_pid on SIGCHLD
346 */
347 if (!sessiond_pid) {
348 exit(EXIT_FAILURE);
349 }
350 goto end;
351 } else {
352 perror("fork");
353 ret = -1;
354 goto end;
355 }
356
357end:
358 return ret;
359}
360
361/*
362 * check_sessiond
363 *
364 * Check if the session daemon is available using
365 * the liblttngctl API for the check. If not, try to
366 * spawn a daemon.
367 */
368static int check_sessiond(void)
369{
370 int ret;
371 char *pathname = NULL;
372
373 ret = lttng_session_daemon_alive();
374 if (ret == 0) { /* not alive */
375 /* Try command line option path */
376 pathname = opt_sessiond_path;
377
378 /* Try LTTNG_SESSIOND_PATH env variable */
379 if (pathname == NULL) {
380 pathname = getenv(DEFAULT_SESSIOND_PATH_ENV);
381 }
382
383 /* Try with configured path */
384 if (pathname == NULL) {
385 if (CONFIG_SESSIOND_BIN[0] != '\0') {
386 pathname = CONFIG_SESSIOND_BIN;
387 }
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;
402 }
403
404 ret = spawn_sessiond(pathname);
405 if (ret < 0) {
406 ERR("Problem occurred when starting %s", pathname);
407 }
408 }
409end:
410 return ret;
411}
412
413/*
414 * Check args for specific options that *must* not trigger a session daemon
415 * execution.
416 *
417 * Return 1 if match else 0.
418 */
419static int check_args_no_sessiond(int argc, char **argv)
420{
421 int i;
422
423 for (i = 0; i < argc; i++) {
424 if ((strncmp(argv[i], "-h", sizeof("-h")) == 0) ||
425 strncmp(argv[i], "--h", sizeof("--h")) == 0 ||
426 strncmp(argv[i], "--list-options", sizeof("--list-options")) == 0 ||
427 strncmp(argv[i], "--list-commands", sizeof("--list-commands")) == 0 ||
428 strncmp(argv[i], "version", sizeof("version")) == 0 ||
429 strncmp(argv[i], "view", sizeof("view")) == 0) {
430 return 1;
431 }
432 }
433
434 return 0;
435}
436
437/*
438 * Parse command line arguments.
439 *
440 * Return 0 if OK, else -1
441 */
442static int parse_args(int argc, char **argv)
443{
444 int opt, ret;
445
446 if (argc < 2) {
447 usage(stderr);
448 clean_exit(EXIT_FAILURE);
449 }
450
451 while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) {
452 switch (opt) {
453 case 'V':
454 version(stdout);
455 ret = 0;
456 goto end;
457 case 'h':
458 usage(stdout);
459 ret = 0;
460 goto end;
461 case 'v':
462 /* There is only 3 possible level of verbosity. (-vvv) */
463 if (lttng_opt_verbose < 3) {
464 lttng_opt_verbose += 1;
465 }
466 break;
467 case 'q':
468 lttng_opt_quiet = 1;
469 break;
470 case 'm':
471 lttng_opt_mi = mi_output_type(optarg);
472 if (lttng_opt_mi < 0) {
473 ret = lttng_opt_mi;
474 goto error;
475 }
476 break;
477 case 'g':
478 lttng_set_tracing_group(optarg);
479 break;
480 case 'n':
481 opt_no_sessiond = 1;
482 break;
483 case OPT_SESSION_PATH:
484 opt_sessiond_path = strdup(optarg);
485 break;
486 case OPT_RELAYD_PATH:
487 opt_relayd_path = strdup(optarg);
488 break;
489 case OPT_DUMP_OPTIONS:
490 list_options(stdout);
491 ret = 0;
492 goto end;
493 case OPT_DUMP_COMMANDS:
494 list_commands(commands, stdout);
495 ret = 0;
496 goto end;
497 default:
498 usage(stderr);
499 ret = 1;
500 goto error;
501 }
502 }
503
504 /* If both options are specified, quiet wins */
505 if (lttng_opt_verbose && lttng_opt_quiet) {
506 lttng_opt_verbose = 0;
507 }
508
509 /* Spawn session daemon if needed */
510 if (opt_no_sessiond == 0 && check_args_no_sessiond(argc, argv) == 0 &&
511 (check_sessiond() < 0)) {
512 ret = 1;
513 goto error;
514 }
515
516 /* No leftovers, print usage and quit */
517 if ((argc - optind) == 0) {
518 usage(stderr);
519 ret = 1;
520 goto error;
521 }
522
523 /*
524 * Handle leftovers which is a first level command with the trailing
525 * options.
526 */
527 ret = handle_command(argc - optind, argv + optind);
528 switch (ret) {
529 case CMD_WARNING:
530 WARN("Some command(s) went wrong");
531 break;
532 case CMD_ERROR:
533 ERR("Command error");
534 break;
535 case CMD_UNDEFINED:
536 ERR("Undefined command");
537 break;
538 case CMD_FATAL:
539 ERR("Fatal error");
540 break;
541 case CMD_UNSUPPORTED:
542 ERR("Unsupported command");
543 break;
544 case -1:
545 usage(stderr);
546 ret = 1;
547 break;
548 case 0:
549 break;
550 default:
551 if (ret < 0) {
552 ret = -ret;
553 }
554 break;
555 }
556
557end:
558error:
559 return ret;
560}
561
562
563/*
564 * main
565 */
566int main(int argc, char *argv[])
567{
568 int ret;
569 char *user;
570
571 progname = argv[0] ? argv[0] : "lttng";
572
573 /* For Mathieu Desnoyers a.k.a. Dr. Tracing */
574 user = getenv("USER");
575 if (user != NULL && ((strncmp(progname, "drtrace", 7) == 0 ||
576 strncmp("compudj", user, 7) == 0))) {
577 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
578 }
579 /* Thanks Mathieu */
580
581 ret = set_signal_handler();
582 if (ret < 0) {
583 clean_exit(ret);
584 }
585
586 ret = parse_args(argc, argv);
587 if (ret != 0) {
588 clean_exit(ret);
589 }
590
591 return 0;
592}
This page took 0.024948 seconds and 4 git commands to generate.