031b015efba4dd05da900b25a8d0c8ead4dfa7b7
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.
25 #include <sys/types.h>
30 #include <lttng/lttng.h>
31 #include <common/error.h>
36 static char *progname
;
40 static int opt_no_sessiond
;
41 static char *opt_sessiond_path
;
42 static pid_t sessiond_pid
;
50 /* Getopt options. No first level command. */
51 static struct option long_options
[] = {
52 {"help", 0, NULL
, 'h'},
53 {"group", 1, NULL
, 'g'},
54 {"verbose", 0, NULL
, 'v'},
55 {"quiet", 0, NULL
, 'q'},
56 {"no-sessiond", 0, NULL
, 'n'},
57 {"sessiond-path", 1, NULL
, OPT_SESSION_PATH
},
58 {"list-options", 0, NULL
, OPT_DUMP_OPTIONS
},
59 {"list-commands", 0, NULL
, OPT_DUMP_COMMANDS
},
63 /* First level command */
64 static struct cmd_struct commands
[] = {
66 { "create", cmd_create
},
67 { "destroy", cmd_destroy
},
68 { "start", cmd_start
},
70 { "enable-event", cmd_enable_events
},
71 { "disable-event", cmd_disable_events
},
72 { "enable-channel", cmd_enable_channels
},
73 { "disable-channel", cmd_disable_channels
},
74 { "add-context", cmd_add_context
},
75 { "set-session", cmd_set_session
},
76 { "version", cmd_version
},
77 { "calibrate", cmd_calibrate
},
78 { NULL
, NULL
} /* Array closure */
81 static void usage(FILE *ofp
)
83 fprintf(ofp
, "LTTng Trace Control " VERSION
"\n\n");
84 fprintf(ofp
, "usage: lttng [options] <command>\n");
86 fprintf(ofp
, "Options:\n");
87 fprintf(ofp
, " -h, --help Show this help\n");
88 fprintf(ofp
, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
89 fprintf(ofp
, " -v, --verbose Verbose mode\n");
90 fprintf(ofp
, " -q, --quiet Quiet mode\n");
91 fprintf(ofp
, " -n, --no-sessiond Don't spawn a session daemon\n");
92 fprintf(ofp
, " --sessiond-path Session daemon full path\n");
93 fprintf(ofp
, " --list-options Simple listing of lttng options\n");
94 fprintf(ofp
, " --list-commands Simple listing of lttng commands\n");
96 fprintf(ofp
, "Commands:\n");
97 fprintf(ofp
, " add-context Add context to event or/and channel\n");
98 fprintf(ofp
, " calibrate Quantify LTTng overhead\n");
99 fprintf(ofp
, " create Create tracing session\n");
100 fprintf(ofp
, " destroy Teardown tracing session\n");
101 fprintf(ofp
, " enable-channel Enable tracing channel\n");
102 fprintf(ofp
, " enable-event Enable tracing event\n");
103 fprintf(ofp
, " disable-channel Disable tracing channel\n");
104 fprintf(ofp
, " disable-event Disable tracing event\n");
105 fprintf(ofp
, " list List possible tracing options\n");
106 fprintf(ofp
, " set-session Set current session name\n");
107 fprintf(ofp
, " start Start tracing\n");
108 fprintf(ofp
, " stop Stop tracing\n");
109 fprintf(ofp
, " version Show version information\n");
111 fprintf(ofp
, "Please see the lttng(1) man page for full documentation.\n");
112 fprintf(ofp
, "See http://lttng.org for updates, bug reports and news.\n");
118 * List options line by line. This is mostly for bash auto completion and to
119 * avoid difficult parsing.
121 static void list_options(FILE *ofp
)
124 struct option
*option
= NULL
;
126 option
= &long_options
[i
];
127 while (option
->name
!= NULL
) {
128 fprintf(ofp
, "--%s\n", option
->name
);
130 if (isprint(option
->val
)) {
131 fprintf(ofp
, "-%c\n", option
->val
);
135 option
= &long_options
[i
];
142 * List commands line by line. This is mostly for bash auto completion and to
143 * avoid difficult parsing.
145 static void list_commands(FILE *ofp
)
148 struct cmd_struct
*cmd
= NULL
;
151 while (cmd
->name
!= NULL
) {
152 fprintf(ofp
, "%s\n", cmd
->name
);
161 static void clean_exit(int code
)
170 * Signal handler for the daemon
172 static void sighandler(int sig
)
178 DBG("SIGTERM caugth");
179 clean_exit(EXIT_FAILURE
);
182 DBG("SIGCHLD caugth");
183 waitpid(sessiond_pid
, &status
, 0);
184 /* Indicate that the session daemon died */
186 ERR("Session daemon died (exit status %d)", WEXITSTATUS(status
));
190 DBG("SIGUSR1 caugth");
193 DBG("Unknown signal %d caugth", sig
);
203 * Setup signal handler for SIGCHLD and SIGTERM.
205 static int set_signal_handler(void)
211 if ((ret
= sigemptyset(&sigset
)) < 0) {
212 perror("sigemptyset");
216 sa
.sa_handler
= sighandler
;
219 if ((ret
= sigaction(SIGUSR1
, &sa
, NULL
)) < 0) {
224 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
229 if ((ret
= sigaction(SIGCHLD
, &sa
, NULL
)) < 0) {
241 * Handle the full argv list of a first level command. Will find the command
242 * in the global commands array and call the function callback associated.
244 * If command not found, return -1
245 * else, return function command error code.
247 static int handle_command(int argc
, char **argv
)
250 struct cmd_struct
*cmd
;
258 while (cmd
->func
!= NULL
) {
260 if (strcmp(argv
[0], cmd
->name
) == 0) {
261 ret
= cmd
->func(argc
, (const char**) argv
);
264 ERR("Command error");
267 ERR("Undefined command");
279 /* Command not found */
289 * Spawn a session daemon by forking and execv.
291 static int spawn_sessiond(char *pathname
)
296 MSG("Spawning a session daemon");
300 * Spawn session daemon and tell
301 * it to signal us when ready.
303 execlp(pathname
, "lttng-sessiond", "--sig-parent", "--quiet", NULL
);
304 /* execlp only returns if error happened */
305 if (errno
== ENOENT
) {
306 ERR("No session daemon found. Use --sessiond-path.");
310 kill(getppid(), SIGTERM
); /* unpause parent */
312 } else if (pid
> 0) {
314 /* Wait for lttng-sessiond to start */
333 * Check if the session daemon is available using
334 * the liblttngctl API for the check. If not, try to
337 static int check_sessiond(void)
340 char *pathname
= NULL
, *alloc_pathname
= NULL
;
342 ret
= lttng_session_daemon_alive();
343 if (ret
== 0) { /* not alive */
344 /* Try command line option path */
345 if (opt_sessiond_path
!= NULL
) {
346 ret
= access(opt_sessiond_path
, F_OK
| X_OK
);
348 ERR("No such file: %s", opt_sessiond_path
);
351 pathname
= opt_sessiond_path
;
353 /* Try LTTNG_SESSIOND_PATH env variable */
354 pathname
= getenv(LTTNG_SESSIOND_PATH_ENV
);
357 /* Let's rock and roll */
358 if (pathname
== NULL
) {
359 ret
= asprintf(&alloc_pathname
, INSTALL_BIN_PATH
"/lttng-sessiond");
361 perror("asprintf spawn sessiond");
364 pathname
= alloc_pathname
;
367 ret
= spawn_sessiond(pathname
);
368 free(alloc_pathname
);
370 ERR("Problem occurs when starting %s", pathname
);
380 * Check for the "help" option in the argv. If found, return 1 else return 0.
382 static int check_help_command(int argc
, char **argv
)
386 for (i
= 0; i
< argc
; i
++) {
387 if ((strncmp(argv
[i
], "-h", 2) == 0) ||
388 strncmp(argv
[i
], "--h", 3) == 0) {
399 * Parse command line arguments.
400 * Return 0 if OK, else -1
402 static int parse_args(int argc
, char **argv
)
408 clean_exit(EXIT_FAILURE
);
411 while ((opt
= getopt_long(argc
, argv
, "+hnvqg:", long_options
, NULL
)) != -1) {
423 lttng_set_tracing_group(optarg
);
428 case OPT_SESSION_PATH
:
429 opt_sessiond_path
= strdup(optarg
);
431 case OPT_DUMP_OPTIONS
:
432 list_options(stdout
);
435 case OPT_DUMP_COMMANDS
:
436 list_commands(stdout
);
445 /* If both options are specified, quiet wins */
446 if (opt_verbose
&& opt_quiet
) {
450 /* Spawn session daemon if needed */
451 if (opt_no_sessiond
== 0 && check_help_command(argc
, argv
) == 0 &&
452 (check_sessiond() < 0)) {
456 /* No leftovers, print usage and quit */
457 if ((argc
- optind
) == 0) {
463 * Handle leftovers which is a first level command with the trailing
466 ret
= handle_command(argc
- optind
, argv
+ optind
);
471 ERR("%s", lttng_strerror(ret
));
486 int main(int argc
, char *argv
[])
490 progname
= argv
[0] ? argv
[0] : "lttng";
492 /* For Mathieu Desnoyers aka Dr Tracing */
493 if (strncmp(progname
, "drtrace", 7) == 0) {
494 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
497 ret
= set_signal_handler();
502 ret
= parse_args(argc
, argv
);
504 clean_exit(EXIT_FAILURE
);
This page took 0.058346 seconds and 4 git commands to generate.