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.
24 #include <sys/types.h>
29 #include <lttng/lttng.h>
30 #include <common/error.h>
31 #include <common/compat/getenv.h>
36 static char *progname
;
38 char *opt_sessiond_path
;
40 char *opt_relayd_path
;
49 /* Getopt options. No first level command. */
50 static struct option long_options
[] = {
51 {"version", 0, NULL
, 'V'},
52 {"help", 0, NULL
, 'h'},
53 {"group", 1, NULL
, 'g'},
54 {"verbose", 0, NULL
, 'v'},
55 {"quiet", 0, NULL
, 'q'},
57 {"no-sessiond", 0, NULL
, 'n'},
58 {"sessiond-path", 1, NULL
, OPT_SESSION_PATH
},
59 {"relayd-path", 1, NULL
, OPT_RELAYD_PATH
},
60 {"list-options", 0, NULL
, OPT_DUMP_OPTIONS
},
61 {"list-commands", 0, NULL
, OPT_DUMP_COMMANDS
},
65 /* First level command */
66 static struct cmd_struct commands
[] = {
68 { "status", cmd_status
},
69 { "create", cmd_create
},
70 { "destroy", cmd_destroy
},
71 { "start", cmd_start
},
73 { "enable-event", cmd_enable_events
},
74 { "disable-event", cmd_disable_events
},
75 { "enable-channel", cmd_enable_channels
},
76 { "disable-channel", cmd_disable_channels
},
77 { "add-context", cmd_add_context
},
78 { "set-session", cmd_set_session
},
79 { "version", cmd_version
},
80 { "calibrate", cmd_calibrate
},
82 { "snapshot", cmd_snapshot
},
85 { "track", cmd_track
},
86 { "untrack", cmd_untrack
},
87 { "metadata", cmd_metadata
},
88 { NULL
, NULL
} /* Array closure */
91 static void usage(FILE *ofp
)
93 fprintf(ofp
, "LTTng Trace Control " VERSION
" - " VERSION_NAME
"%s\n\n",
94 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
);
95 fprintf(ofp
, "usage: lttng [OPTIONS] <COMMAND> [<ARGS>]\n");
97 fprintf(ofp
, "Options:\n");
98 fprintf(ofp
, " -V, --version Show version\n");
99 fprintf(ofp
, " -h, --help Show this help\n");
100 fprintf(ofp
, " --list-options Simple listing of lttng options\n");
101 fprintf(ofp
, " --list-commands Simple listing of lttng commands\n");
102 fprintf(ofp
, " -v, --verbose Increase verbosity\n");
103 fprintf(ofp
, " -q, --quiet Quiet mode\n");
104 fprintf(ofp
, " -m, --mi TYPE Machine Interface mode.\n");
105 fprintf(ofp
, " Type: xml\n");
106 fprintf(ofp
, " -g, --group NAME Unix tracing group name. (default: tracing)\n");
107 fprintf(ofp
, " -n, --no-sessiond Don't spawn a session daemon\n");
108 fprintf(ofp
, " --sessiond-path PATH Session daemon full path\n");
109 fprintf(ofp
, " --relayd-path PATH Relayd daemon full path\n");
111 fprintf(ofp
, "Commands:\n");
112 fprintf(ofp
, " add-context Add context to event and/or channel\n");
113 fprintf(ofp
, " calibrate Quantify LTTng overhead\n");
114 fprintf(ofp
, " create Create tracing session\n");
115 fprintf(ofp
, " destroy Tear down tracing session\n");
116 fprintf(ofp
, " enable-channel Enable tracing channel\n");
117 fprintf(ofp
, " enable-event Enable tracing event\n");
118 fprintf(ofp
, " disable-channel Disable tracing channel\n");
119 fprintf(ofp
, " disable-event Disable tracing event\n");
120 fprintf(ofp
, " list List possible tracing options\n");
121 fprintf(ofp
, " set-session Set current session name\n");
122 fprintf(ofp
, " snapshot Snapshot buffers of current session name\n");
123 fprintf(ofp
, " start Start tracing\n");
124 fprintf(ofp
, " status Show current session's details\n");
125 fprintf(ofp
, " stop Stop tracing\n");
126 fprintf(ofp
, " version Show version information\n");
127 fprintf(ofp
, " view Start trace viewer\n");
128 fprintf(ofp
, " save Save session configuration\n");
129 fprintf(ofp
, " load Load session configuration\n");
130 fprintf(ofp
, " track Track specific system resources\n");
131 fprintf(ofp
, " untrack Untrack specific system resources\n");
132 fprintf(ofp
, " metadata Regenerate the metadata of a session\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
)
208 DBG("SIGTERM caught");
209 clean_exit(EXIT_FAILURE
);
212 DBG("Unknown signal %d caught", sig
);
222 * Setup signal handler for SIGCHLD and SIGTERM.
224 static int set_signal_handler(void)
230 if ((ret
= sigemptyset(&sigset
)) < 0) {
231 PERROR("sigemptyset");
235 sa
.sa_handler
= sighandler
;
239 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
251 * Handle the full argv list of a first level command. Will find the command
252 * in the global commands array and call the function callback associated.
254 * If command not found, return -1
255 * else, return function command error code.
257 static int handle_command(int argc
, char **argv
)
260 struct cmd_struct
*cmd
;
268 while (cmd
->func
!= NULL
) {
270 if (strcmp(argv
[0], cmd
->name
) == 0) {
271 ret
= cmd
->func(argc
, (const char**) argv
);
278 /* Command not found */
286 * Parse command line arguments.
288 * Return 0 if OK, else -1
290 static int parse_args(int argc
, char **argv
)
295 if (lttng_is_setuid_setgid()) {
296 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv
[0]);
297 clean_exit(EXIT_FAILURE
);
302 clean_exit(EXIT_FAILURE
);
305 while ((opt
= getopt_long(argc
, argv
, "+Vhnvqg:m:", long_options
, NULL
)) != -1) {
316 /* There is only 3 possible level of verbosity. (-vvv) */
317 if (lttng_opt_verbose
< 3) {
318 lttng_opt_verbose
+= 1;
325 lttng_opt_mi
= mi_output_type(optarg
);
326 if (lttng_opt_mi
< 0) {
332 lttng_set_tracing_group(optarg
);
337 case OPT_SESSION_PATH
:
338 opt_sessiond_path
= strdup(optarg
);
339 if (!opt_sessiond_path
) {
344 case OPT_RELAYD_PATH
:
345 opt_relayd_path
= strdup(optarg
);
346 if (!opt_relayd_path
) {
351 case OPT_DUMP_OPTIONS
:
352 list_options(stdout
);
355 case OPT_DUMP_COMMANDS
:
356 list_commands(commands
, stdout
);
366 /* If both options are specified, quiet wins */
367 if (lttng_opt_verbose
&& lttng_opt_quiet
) {
368 lttng_opt_verbose
= 0;
371 /* No leftovers, print usage and quit */
372 if ((argc
- optind
) == 0) {
378 /* For Mathieu Desnoyers a.k.a. Dr. Tracing */
379 user
= getenv("USER");
380 if (user
!= NULL
&& ((strncmp(progname
, "drtrace", 7) == 0 ||
381 strncmp("compudj", user
, 7) == 0))) {
382 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
387 * Handle leftovers which is a first level command with the trailing
390 ret
= handle_command(argc
- optind
, argv
+ optind
);
393 WARN("Some command(s) went wrong");
396 ERR("Command error");
399 ERR("Undefined command");
404 case CMD_UNSUPPORTED
:
405 ERR("Unsupported command");
429 int main(int argc
, char *argv
[])
433 progname
= argv
[0] ? argv
[0] : "lttng";
435 ret
= set_signal_handler();
440 ret
= parse_args(argc
, argv
);
This page took 0.039481 seconds and 5 git commands to generate.