65cea55a25e73a8698fd01c2846b4a405488916a
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 as published by
6 * as published by the Free Software Foundation; only version 2
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.
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.
28 #include <lttng/lttng.h>
29 #include <common/lttngerr.h>
35 static char *progname
;
39 static int opt_no_sessiond
;
40 static char *opt_sessiond_path
;
48 /* Getopt options. No first level command. */
49 static struct option long_options
[] = {
50 {"help", 0, NULL
, 'h'},
51 {"group", 1, NULL
, 'g'},
52 {"verbose", 0, NULL
, 'v'},
53 {"quiet", 0, NULL
, 'q'},
54 {"no-sessiond", 0, NULL
, 'n'},
55 {"sessiond-path", 1, NULL
, OPT_SESSION_PATH
},
56 {"list-options", 0, NULL
, OPT_DUMP_OPTIONS
},
57 {"list-commands", 0, NULL
, OPT_DUMP_COMMANDS
},
61 /* First level command */
62 static struct cmd_struct commands
[] = {
64 { "create", cmd_create
},
65 { "destroy", cmd_destroy
},
66 { "start", cmd_start
},
68 { "enable-event", cmd_enable_events
},
69 { "disable-event", cmd_disable_events
},
70 { "enable-channel", cmd_enable_channels
},
71 { "disable-channel", cmd_disable_channels
},
72 { "add-context", cmd_add_context
},
73 { "set-session", cmd_set_session
},
74 { "version", cmd_version
},
75 { "calibrate", cmd_calibrate
},
76 { NULL
, NULL
} /* Array closure */
79 static void usage(FILE *ofp
)
81 fprintf(ofp
, "LTTng Trace Control " VERSION
"\n\n");
82 fprintf(ofp
, "usage: lttng [options] <command>\n");
84 fprintf(ofp
, "Options:\n");
85 fprintf(ofp
, " -h, --help Show this help\n");
86 fprintf(ofp
, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
87 fprintf(ofp
, " -v, --verbose Verbose mode\n");
88 fprintf(ofp
, " -q, --quiet Quiet mode\n");
89 fprintf(ofp
, " -n, --no-sessiond Don't spawn a session daemon\n");
90 fprintf(ofp
, " --sessiond-path Session daemon full path\n");
91 fprintf(ofp
, " --list-options Simple listing of lttng options\n");
92 fprintf(ofp
, " --list-commands Simple listing of lttng commands\n");
94 fprintf(ofp
, "Commands:\n");
95 fprintf(ofp
, " add-context Add context to event or/and channel\n");
96 fprintf(ofp
, " calibrate Quantify LTTng overhead\n");
97 fprintf(ofp
, " create Create tracing session\n");
98 fprintf(ofp
, " destroy Teardown tracing session\n");
99 fprintf(ofp
, " enable-channel Enable tracing channel\n");
100 fprintf(ofp
, " enable-event Enable tracing event\n");
101 fprintf(ofp
, " disable-channel Disable tracing channel\n");
102 fprintf(ofp
, " disable-event Disable tracing event\n");
103 fprintf(ofp
, " list List possible tracing options\n");
104 fprintf(ofp
, " set-session Set current session name\n");
105 fprintf(ofp
, " start Start tracing\n");
106 fprintf(ofp
, " stop Stop tracing\n");
107 fprintf(ofp
, " version Show version information\n");
109 fprintf(ofp
, "Please see the lttng(1) man page for full documentation.\n");
110 fprintf(ofp
, "See http://lttng.org for updates, bug reports and news.\n");
116 * List options line by line. This is mostly for bash auto completion and to
117 * avoid difficult parsing.
119 static void list_options(FILE *ofp
)
122 struct option
*option
= NULL
;
124 option
= &long_options
[i
];
125 while (option
->name
!= NULL
) {
126 fprintf(ofp
, "--%s\n", option
->name
);
128 if (isprint(option
->val
)) {
129 fprintf(ofp
, "-%c\n", option
->val
);
133 option
= &long_options
[i
];
140 * List commands line by line. This is mostly for bash auto completion and to
141 * avoid difficult parsing.
143 static void list_commands(FILE *ofp
)
146 struct cmd_struct
*cmd
= NULL
;
149 while (cmd
->name
!= NULL
) {
150 fprintf(ofp
, "%s\n", cmd
->name
);
159 static void clean_exit(int code
)
168 * Signal handler for the daemon
170 static void sighandler(int sig
)
174 DBG("SIGTERM catched");
175 clean_exit(EXIT_FAILURE
);
179 DBG("SIGCHLD catched");
182 DBG("Unknown signal %d catched", sig
);
192 * Setup signal handler for SIGCHLD and SIGTERM.
194 static int set_signal_handler(void)
200 if ((ret
= sigemptyset(&sigset
)) < 0) {
201 perror("sigemptyset");
205 sa
.sa_handler
= sighandler
;
208 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
213 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
225 * Handle the full argv list of a first level command. Will find the command
226 * in the global commands array and call the function callback associated.
228 * If command not found, return -1
229 * else, return function command error code.
231 static int handle_command(int argc
, char **argv
)
234 struct cmd_struct
*cmd
;
242 while (cmd
->func
!= NULL
) {
244 if (strcmp(argv
[0], cmd
->name
) == 0) {
245 ret
= cmd
->func(argc
, (const char**) argv
);
248 ERR("Command error");
250 case CMD_NOT_IMPLEMENTED
:
251 ERR("Options not implemented");
254 ERR("Undefined command");
266 /* Command not found */
276 * Spawn a session daemon by forking and execv.
278 static int spawn_sessiond(char *pathname
)
283 MSG("Spawning a session daemon");
287 * Spawn session daemon and tell
288 * it to signal us when ready.
290 execlp(pathname
, "lttng-sessiond", "--sig-parent", "--quiet", NULL
);
291 /* execlp only returns if error happened */
292 if (errno
== ENOENT
) {
293 ERR("No session daemon found. Use --sessiond-path.");
297 kill(getppid(), SIGTERM
); /* unpause parent */
299 } else if (pid
> 0) {
300 /* Wait for lttng-sessiond to start */
316 * Check if the session daemon is available using
317 * the liblttngctl API for the check. If not, try to
320 static int check_sessiond(void)
323 char *pathname
= NULL
, *alloc_pathname
= NULL
;
325 ret
= lttng_session_daemon_alive();
326 if (ret
== 0) { /* not alive */
327 /* Try command line option path */
328 if (opt_sessiond_path
!= NULL
) {
329 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
331 ERR("No such file: %s", opt_sessiond_path
);
334 pathname
= opt_sessiond_path
;
336 /* Try LTTNG_SESSIOND_PATH env variable */
337 pathname
= getenv(LTTNG_SESSIOND_PATH_ENV
);
340 /* Let's rock and roll */
341 if (pathname
== NULL
) {
342 ret
= asprintf(&alloc_pathname
, INSTALL_BIN_PATH
"/lttng-sessiond");
344 perror("asprintf spawn sessiond");
347 pathname
= alloc_pathname
;
350 ret
= spawn_sessiond(pathname
);
351 free(alloc_pathname
);
353 ERR("Problem occurs when starting %s", pathname
);
363 * Check for the "help" option in the argv. If found, return 1 else return 0.
365 static int check_help_command(int argc
, char **argv
)
369 for (i
= 0; i
< argc
; i
++) {
370 if ((strncmp(argv
[i
], "-h", 2) == 0) ||
371 strncmp(argv
[i
], "--h", 3) == 0) {
382 * Parse command line arguments.
383 * Return 0 if OK, else -1
385 static int parse_args(int argc
, char **argv
)
391 clean_exit(EXIT_FAILURE
);
394 while ((opt
= getopt_long(argc
, argv
, "+hnvqg:", long_options
, NULL
)) != -1) {
406 lttng_set_tracing_group(optarg
);
411 case OPT_SESSION_PATH
:
412 opt_sessiond_path
= strdup(optarg
);
414 case OPT_DUMP_OPTIONS
:
415 list_options(stdout
);
418 case OPT_DUMP_COMMANDS
:
419 list_commands(stdout
);
428 /* If both options are specified, quiet wins */
429 if (opt_verbose
&& opt_quiet
) {
433 /* Spawn session daemon if needed */
434 if (opt_no_sessiond
== 0 && check_help_command(argc
, argv
) == 0 &&
435 (check_sessiond() < 0)) {
439 /* No leftovers, print usage and quit */
440 if ((argc
- optind
) == 0) {
446 * Handle leftovers which is a first level command with the trailing
449 ret
= handle_command(argc
- optind
, argv
+ optind
);
454 ERR("%s", lttng_strerror(ret
));
469 int main(int argc
, char *argv
[])
473 progname
= argv
[0] ? argv
[0] : "lttng";
475 /* For Mathieu Desnoyers aka Dr Tracing */
476 if (strncmp(progname
, "drtrace", 7) == 0) {
477 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
480 ret
= set_signal_handler();
485 ret
= parse_args(argc
, argv
);
487 clean_exit(EXIT_FAILURE
);
This page took 0.051475 seconds and 5 git commands to generate.