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 { NULL
, NULL
} /* Array closure */
90 static void usage(FILE *ofp
)
92 fprintf(ofp
, "LTTng Trace Control " VERSION
" - " VERSION_NAME
"%s\n\n",
93 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
);
94 fprintf(ofp
, "usage: lttng [OPTIONS] <COMMAND> [<ARGS>]\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");
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
, " status Show current session's details\n");
124 fprintf(ofp
, " stop Stop tracing\n");
125 fprintf(ofp
, " version Show version information\n");
126 fprintf(ofp
, " view Start trace viewer\n");
127 fprintf(ofp
, " save Save session configuration\n");
128 fprintf(ofp
, " load Load session configuration\n");
129 fprintf(ofp
, " track Track specific system resources\n");
130 fprintf(ofp
, " untrack Untrack specific system resources\n");
132 fprintf(ofp
, "Each command also has its own -h, --help option.\n");
134 fprintf(ofp
, "Please see the lttng(1) man page for full documentation.\n");
135 fprintf(ofp
, "See http://lttng.org for updates, bug reports and news.\n");
138 static void version(FILE *ofp
)
140 fprintf(ofp
, "%s (LTTng Trace Control) " VERSION
" - " VERSION_NAME
"%s\n",
142 GIT_VERSION
[0] == '\0' ? "" : " - " GIT_VERSION
);
146 * Find the MI output type enum from a string. This function is for the support
147 * of machine interface output.
149 static int mi_output_type(const char *output_type
)
153 if (!strncasecmp("xml", output_type
, 3)) {
156 /* Invalid output format */
157 ERR("MI output format not supported");
158 ret
= -LTTNG_ERR_MI_OUTPUT_TYPE
;
167 * List options line by line. This is mostly for bash auto completion and to
168 * avoid difficult parsing.
170 static void list_options(FILE *ofp
)
173 struct option
*option
= NULL
;
175 option
= &long_options
[i
];
176 while (option
->name
!= NULL
) {
177 fprintf(ofp
, "--%s\n", option
->name
);
179 if (isprint(option
->val
)) {
180 fprintf(ofp
, "-%c\n", option
->val
);
184 option
= &long_options
[i
];
191 static void clean_exit(int code
)
200 * Signal handler for the daemon
202 static void sighandler(int sig
)
206 DBG("SIGTERM caught");
207 clean_exit(EXIT_FAILURE
);
210 DBG("Unknown signal %d caught", sig
);
220 * Setup signal handler for SIGCHLD and SIGTERM.
222 static int set_signal_handler(void)
228 if ((ret
= sigemptyset(&sigset
)) < 0) {
229 PERROR("sigemptyset");
233 sa
.sa_handler
= sighandler
;
237 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
249 * Handle the full argv list of a first level command. Will find the command
250 * in the global commands array and call the function callback associated.
252 * If command not found, return -1
253 * else, return function command error code.
255 static int handle_command(int argc
, char **argv
)
258 struct cmd_struct
*cmd
;
266 while (cmd
->func
!= NULL
) {
268 if (strcmp(argv
[0], cmd
->name
) == 0) {
269 ret
= cmd
->func(argc
, (const char**) argv
);
276 /* Command not found */
284 * Parse command line arguments.
286 * Return 0 if OK, else -1
288 static int parse_args(int argc
, char **argv
)
293 if (lttng_is_setuid_setgid()) {
294 ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv
[0]);
295 clean_exit(EXIT_FAILURE
);
300 clean_exit(EXIT_FAILURE
);
303 while ((opt
= getopt_long(argc
, argv
, "+Vhnvqg:m:", long_options
, NULL
)) != -1) {
314 /* There is only 3 possible level of verbosity. (-vvv) */
315 if (lttng_opt_verbose
< 3) {
316 lttng_opt_verbose
+= 1;
323 lttng_opt_mi
= mi_output_type(optarg
);
324 if (lttng_opt_mi
< 0) {
330 lttng_set_tracing_group(optarg
);
335 case OPT_SESSION_PATH
:
336 opt_sessiond_path
= strdup(optarg
);
337 if (!opt_sessiond_path
) {
342 case OPT_RELAYD_PATH
:
343 opt_relayd_path
= strdup(optarg
);
344 if (!opt_relayd_path
) {
349 case OPT_DUMP_OPTIONS
:
350 list_options(stdout
);
353 case OPT_DUMP_COMMANDS
:
354 list_commands(commands
, stdout
);
364 /* If both options are specified, quiet wins */
365 if (lttng_opt_verbose
&& lttng_opt_quiet
) {
366 lttng_opt_verbose
= 0;
369 /* No leftovers, print usage and quit */
370 if ((argc
- optind
) == 0) {
376 /* For Mathieu Desnoyers a.k.a. Dr. Tracing */
377 user
= getenv("USER");
378 if (user
!= NULL
&& ((strncmp(progname
, "drtrace", 7) == 0 ||
379 strncmp("compudj", user
, 7) == 0))) {
380 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0);
385 * Handle leftovers which is a first level command with the trailing
388 ret
= handle_command(argc
- optind
, argv
+ optind
);
391 WARN("Some command(s) went wrong");
394 ERR("Command error");
397 ERR("Undefined command");
402 case CMD_UNSUPPORTED
:
403 ERR("Unsupported command");
427 int main(int argc
, char *argv
[])
431 progname
= argv
[0] ? argv
[0] : "lttng";
433 ret
= set_signal_handler();
438 ret
= parse_args(argc
, argv
);
This page took 0.039201 seconds and 5 git commands to generate.