2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
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.
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.
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.
25 #include <sys/types.h>
31 #include <lttng/lttng.h>
32 #include <common/error.h>
33 #include <common/compat/getenv.h>
38 static char *progname
;
39 static int opt_no_sessiond
;
40 static char *opt_sessiond_path
;
41 static pid_t sessiond_pid
;
42 static volatile int recv_child_signal
;
44 char *opt_relayd_path
;
53 /* Getopt options. No first level command. */
54 static struct option long_options
[] = {
55 {"version", 0, NULL
, 'V'},
56 {"help", 0, NULL
, 'h'},
57 {"group", 1, NULL
, 'g'},
58 {"verbose", 0, NULL
, 'v'},
59 {"quiet", 0, NULL
, 'q'},
61 {"no-sessiond", 0, NULL
, 'n'},
62 {"sessiond-path", 1, NULL
, OPT_SESSION_PATH
},
63 {"relayd-path", 1, NULL
, OPT_RELAYD_PATH
},
64 {"list-options", 0, NULL
, OPT_DUMP_OPTIONS
},
65 {"list-commands", 0, NULL
, OPT_DUMP_COMMANDS
},
69 /* First level command */
70 static struct cmd_struct commands
[] = {
72 { "create", cmd_create
},
73 { "destroy", cmd_destroy
},
74 { "start", cmd_start
},
76 { "enable-event", cmd_enable_events
},
77 { "disable-event", cmd_disable_events
},
78 { "enable-channel", cmd_enable_channels
},
79 { "disable-channel", cmd_disable_channels
},
80 { "add-context", cmd_add_context
},
81 { "set-session", cmd_set_session
},
82 { "version", cmd_version
},
83 { "calibrate", cmd_calibrate
},
85 { "snapshot", cmd_snapshot
},
88 { "track", cmd_track
},
89 { "untrack", cmd_untrack
},
90 { NULL
, NULL
} /* Array closure */
93 static void usage(FILE *ofp
)
95 fprintf(ofp
, "LTTng Trace Control " VERSION
" - " VERSION_NAME
"%s\n\n",
96 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
);
97 fprintf(ofp
, "usage: lttng [OPTIONS] <COMMAND> [<ARGS>]\n");
99 fprintf(ofp
, "Options:\n");
100 fprintf(ofp
, " -V, --version Show version\n");
101 fprintf(ofp
, " -h, --help Show this help\n");
102 fprintf(ofp
, " --list-options Simple listing of lttng options\n");
103 fprintf(ofp
, " --list-commands Simple listing of lttng commands\n");
104 fprintf(ofp
, " -v, --verbose Increase verbosity\n");
105 fprintf(ofp
, " -q, --quiet Quiet mode\n");
106 fprintf(ofp
, " -m, --mi TYPE Machine Interface mode.\n");
107 fprintf(ofp
, " Type: xml\n");
108 fprintf(ofp
, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
109 fprintf(ofp
, " -n, --no-sessiond Don't spawn a session daemon\n");
110 fprintf(ofp
, " --sessiond-path PATH Session daemon full path\n");
111 fprintf(ofp
, " --relayd-path PATH Relayd daemon full path\n");
113 fprintf(ofp
, "Commands:\n");
114 fprintf(ofp
, " add-context Add context to event and/or channel\n");
115 fprintf(ofp
, " calibrate Quantify LTTng overhead\n");
116 fprintf(ofp
, " create Create tracing session\n");
117 fprintf(ofp
, " destroy Tear down tracing session\n");
118 fprintf(ofp
, " enable-channel Enable tracing channel\n");
119 fprintf(ofp
, " enable-event Enable tracing event\n");
120 fprintf(ofp
, " disable-channel Disable tracing channel\n");
121 fprintf(ofp
, " disable-event Disable tracing event\n");
122 fprintf(ofp
, " list List possible tracing options\n");
123 fprintf(ofp
, " set-session Set current session name\n");
124 fprintf(ofp
, " snapshot Snapshot buffers of current session name\n");
125 fprintf(ofp
, " start Start tracing\n");
126 fprintf(ofp
, " stop Stop tracing\n");
127 fprintf(ofp
, " version Show version information\n");
128 fprintf(ofp
, " view Start trace viewer\n");
129 fprintf(ofp
, " save Save session configuration\n");
130 fprintf(ofp
, " load Load session configuration\n");
131 fprintf(ofp
, " track Track specific system resources\n");
132 fprintf(ofp
, " untrack Untrack specific system resources\n");
134 fprintf(ofp
, "Each command also has its own -h, --help option.\n");
136 fprintf(ofp
, "Please see the lttng(1) man page for full documentation.\n");
137 fprintf(ofp
, "See http://lttng.org for updates, bug reports and news.\n");
140 static void version(FILE *ofp
)
142 fprintf(ofp
, "%s (LTTng Trace Control) " VERSION
" - " VERSION_NAME
"%s\n",
144 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
);
148 * Find the MI output type enum from a string. This function is for the support
149 * of machine interface output.
151 static int mi_output_type(const char *output_type
)
155 if (!strncasecmp("xml", output_type
, 3)) {
158 /* Invalid output format */
159 ERR("MI output format not supported");
160 ret
= -LTTNG_ERR_MI_OUTPUT_TYPE
;
169 * List options line by line. This is mostly for bash auto completion and to
170 * avoid difficult parsing.
172 static void list_options(FILE *ofp
)
175 struct option
*option
= NULL
;
177 option
= &long_options
[i
];
178 while (option
->name
!= NULL
) {
179 fprintf(ofp
, "--%s\n", option
->name
);
181 if (isprint(option
->val
)) {
182 fprintf(ofp
, "-%c\n", option
->val
);
186 option
= &long_options
[i
];
193 static void clean_exit(int code
)
202 * Signal handler for the daemon
204 static void sighandler(int sig
)
210 DBG("SIGTERM caught");
211 clean_exit(EXIT_FAILURE
);
214 DBG("SIGCHLD caught");
215 waitpid(sessiond_pid
, &status
, 0);
216 recv_child_signal
= 1;
217 /* Indicate that the session daemon died */
219 ERR("Session daemon died (exit status %d)", WEXITSTATUS(status
));
223 recv_child_signal
= 1;
224 DBG("SIGUSR1 caught");
227 DBG("Unknown signal %d caught", sig
);
237 * Setup signal handler for SIGCHLD and SIGTERM.
239 static int set_signal_handler(void)
245 if ((ret
= sigemptyset(&sigset
)) < 0) {
246 PERROR("sigemptyset");
250 sa
.sa_handler
= sighandler
;
253 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
258 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
263 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
275 * Handle the full argv list of a first level command. Will find the command
276 * in the global commands array and call the function callback associated.
278 * If command not found, return -1
279 * else, return function command error code.
281 static int handle_command(int argc
, char **argv
)
284 struct cmd_struct
*cmd
;
292 while (cmd
->func
!= NULL
) {
294 if (strcmp(argv
[0], cmd
->name
) == 0) {
295 ret
= cmd
->func(argc
, (const char**) argv
);
302 /* Command not found */
312 * Spawn a session daemon by forking and execv.
314 static int spawn_sessiond(char *pathname
)
319 MSG("Spawning a session daemon");
320 recv_child_signal
= 0;
324 * Spawn session daemon and tell
325 * it to signal us when ready.
327 execlp(pathname
, "lttng-sessiond", "--sig-parent", "--quiet", NULL
);
328 /* execlp only returns if error happened */
329 if (errno
== ENOENT
) {
330 ERR("No session daemon found. Use --sessiond-path.");
334 kill(getppid(), SIGTERM
); /* wake parent */
336 } else if (pid
> 0) {
339 * Wait for lttng-sessiond to start. We need to use a flag to check if
340 * the signal has been sent to us, because the child can be scheduled
341 * before the parent, and thus send the signal before this check. In
342 * the signal handler, we set the recv_child_signal flag, so anytime we
343 * check it after the fork is fine. Note that sleep() is interrupted
344 * before the 1 second delay as soon as the signal is received, so it
345 * will not cause visible delay for the user.
347 while (!recv_child_signal
) {
351 * The signal handler will nullify sessiond_pid on SIGCHLD
370 * Check if the session daemon is available using
371 * the liblttngctl API for the check. If not, try to
374 static int check_sessiond(void)
377 char *pathname
= NULL
;
379 ret
= lttng_session_daemon_alive();
380 if (ret
== 0) { /* not alive */
381 /* Try command line option path */
382 pathname
= opt_sessiond_path
;
384 /* Try LTTNG_SESSIOND_PATH env variable */
385 if (pathname
== NULL
) {
386 pathname
= getenv(DEFAULT_SESSIOND_PATH_ENV
);
389 /* Try with configured path */
390 if (pathname
== NULL
) {
391 if (CONFIG_SESSIOND_BIN
[0] != '\0') {
392 pathname
= CONFIG_SESSIOND_BIN
;
396 /* Let's rock and roll while trying the default path */
397 if (pathname
== NULL
) {
398 pathname
= INSTALL_BIN_PATH
"/lttng-sessiond";
401 DBG("Session daemon at: %s", pathname
);
403 /* Check existence and permissions */
404 ret
= access(pathname
, F_OK
| X_OK
);
406 ERR("No such file or access denied: %s", pathname
);
410 ret
= spawn_sessiond(pathname
);
412 ERR("Problem occurred when starting %s", pathname
);
420 * Check args for specific options that *must* not trigger a session daemon
423 * Return 1 if match else 0.
425 static int check_args_no_sessiond(int argc
, char **argv
)
429 for (i
= 0; i
< argc
; i
++) {
430 if ((strncmp(argv
[i
], "-h", sizeof("-h")) == 0) ||
431 strncmp(argv
[i
], "--h", sizeof("--h")) == 0 ||
432 strncmp(argv
[i
], "--list-options", sizeof("--list-options")) == 0 ||
433 strncmp(argv
[i
], "--list-commands", sizeof("--list-commands")) == 0 ||
434 strncmp(argv
[i
], "version", sizeof("version")) == 0 ||
435 strncmp(argv
[i
], "view", sizeof("view")) == 0) {
444 * Parse command line arguments.
446 * Return 0 if OK, else -1
448 static int parse_args(int argc
, char **argv
)
453 if (lttng_is_setuid_setgid()) {
454 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv
[0]);
455 clean_exit(EXIT_FAILURE
);
460 clean_exit(EXIT_FAILURE
);
463 while ((opt
= getopt_long(argc
, argv
, "+Vhnvqg:m:", long_options
, NULL
)) != -1) {
474 /* There is only 3 possible level of verbosity. (-vvv) */
475 if (lttng_opt_verbose
< 3) {
476 lttng_opt_verbose
+= 1;
483 lttng_opt_mi
= mi_output_type(optarg
);
484 if (lttng_opt_mi
< 0) {
490 lttng_set_tracing_group(optarg
);
495 case OPT_SESSION_PATH
:
496 opt_sessiond_path
= strdup(optarg
);
497 if (!opt_sessiond_path
) {
502 case OPT_RELAYD_PATH
:
503 opt_relayd_path
= strdup(optarg
);
504 if (!opt_relayd_path
) {
509 case OPT_DUMP_OPTIONS
:
510 list_options(stdout
);
513 case OPT_DUMP_COMMANDS
:
514 list_commands(commands
, stdout
);
524 /* If both options are specified, quiet wins */
525 if (lttng_opt_verbose
&& lttng_opt_quiet
) {
526 lttng_opt_verbose
= 0;
529 /* Spawn session daemon if needed */
530 if (opt_no_sessiond
== 0 && check_args_no_sessiond(argc
, argv
) == 0 &&
531 (check_sessiond() < 0)) {
536 /* No leftovers, print usage and quit */
537 if ((argc
- optind
) == 0) {
543 /* For Mathieu Desnoyers a.k.a. Dr. Tracing */
544 user
= getenv("USER");
545 if (user
!= NULL
&& ((strncmp(progname
, "drtrace", 7) == 0 ||
546 strncmp("compudj", user
, 7) == 0))) {
547 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
552 * Handle leftovers which is a first level command with the trailing
555 ret
= handle_command(argc
- optind
, argv
+ optind
);
558 WARN("Some command(s) went wrong");
561 ERR("Command error");
564 ERR("Undefined command");
569 case CMD_UNSUPPORTED
:
570 ERR("Unsupported command");
594 int main(int argc
, char *argv
[])
598 progname
= argv
[0] ? argv
[0] : "lttng";
600 ret
= set_signal_handler();
605 ret
= parse_args(argc
, argv
);