Commit | Line | Data |
---|---|---|
826d496d MD |
1 | /* |
2 | * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca> | |
fac6795d DG |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify | |
d14d33bf AM |
5 | * it under the terms of the GNU General Public License, version 2 only, |
6 | * as published by the Free Software Foundation. | |
fac6795d DG |
7 | * |
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. | |
12 | * | |
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. | |
fac6795d DG |
16 | */ |
17 | ||
6c1c0768 | 18 | #define _LGPL_SOURCE |
fac6795d | 19 | #include <getopt.h> |
f3ed775e | 20 | #include <signal.h> |
fac6795d DG |
21 | #include <stdio.h> |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
8db8d1dc DG |
24 | #include <sys/types.h> |
25 | #include <sys/wait.h> | |
fac6795d | 26 | #include <unistd.h> |
730389d9 | 27 | #include <ctype.h> |
fac6795d | 28 | |
5b97ec60 | 29 | #include <lttng/lttng.h> |
db758600 | 30 | #include <common/error.h> |
e8fa9fb0 | 31 | #include <common/compat/getenv.h> |
fac6795d | 32 | |
c399183f | 33 | #include "command.h" |
fac6795d DG |
34 | |
35 | /* Variables */ | |
36 | static char *progname; | |
92360082 JG |
37 | int opt_no_sessiond; |
38 | char *opt_sessiond_path; | |
f3ed775e | 39 | |
8960e9cd DG |
40 | char *opt_relayd_path; |
41 | ||
f3ed775e | 42 | enum { |
8960e9cd | 43 | OPT_RELAYD_PATH, |
f3ed775e | 44 | OPT_SESSION_PATH, |
865abf65 SM |
45 | OPT_DUMP_OPTIONS, |
46 | OPT_DUMP_COMMANDS, | |
f3ed775e DG |
47 | }; |
48 | ||
49 | /* Getopt options. No first level command. */ | |
50 | static struct option long_options[] = { | |
70318c38 | 51 | {"version", 0, NULL, 'V'}, |
f3ed775e DG |
52 | {"help", 0, NULL, 'h'}, |
53 | {"group", 1, NULL, 'g'}, | |
54 | {"verbose", 0, NULL, 'v'}, | |
55 | {"quiet", 0, NULL, 'q'}, | |
c7e35b03 | 56 | {"mi", 1, NULL, 'm'}, |
8490ae7b | 57 | {"no-sessiond", 0, NULL, 'n'}, |
f3ed775e | 58 | {"sessiond-path", 1, NULL, OPT_SESSION_PATH}, |
8960e9cd | 59 | {"relayd-path", 1, NULL, OPT_RELAYD_PATH}, |
865abf65 SM |
60 | {"list-options", 0, NULL, OPT_DUMP_OPTIONS}, |
61 | {"list-commands", 0, NULL, OPT_DUMP_COMMANDS}, | |
f3ed775e DG |
62 | {NULL, 0, NULL, 0} |
63 | }; | |
64 | ||
65 | /* First level command */ | |
66 | static struct cmd_struct commands[] = { | |
67 | { "list", cmd_list}, | |
68 | { "create", cmd_create}, | |
69 | { "destroy", cmd_destroy}, | |
f3ed775e DG |
70 | { "start", cmd_start}, |
71 | { "stop", cmd_stop}, | |
72 | { "enable-event", cmd_enable_events}, | |
e953ef25 | 73 | { "disable-event", cmd_disable_events}, |
d36b8583 | 74 | { "enable-channel", cmd_enable_channels}, |
26cc6b4e | 75 | { "disable-channel", cmd_disable_channels}, |
d65106b1 | 76 | { "add-context", cmd_add_context}, |
3087ef18 | 77 | { "set-session", cmd_set_session}, |
eb9cb8b7 | 78 | { "version", cmd_version}, |
d0254c7c | 79 | { "calibrate", cmd_calibrate}, |
0c95f5b2 | 80 | { "view", cmd_view}, |
57f272ed | 81 | { "snapshot", cmd_snapshot}, |
c864d6d7 | 82 | { "save", cmd_save}, |
8c42d845 | 83 | { "load", cmd_load}, |
ccf10263 MD |
84 | { "track", cmd_track}, |
85 | { "untrack", cmd_untrack}, | |
f3ed775e DG |
86 | { NULL, NULL} /* Array closure */ |
87 | }; | |
88 | ||
89 | static void usage(FILE *ofp) | |
8c0faa1d | 90 | { |
4c6ac053 MD |
91 | fprintf(ofp, "LTTng Trace Control " VERSION " - " VERSION_NAME "%s\n\n", |
92 | GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION); | |
32a6298d | 93 | fprintf(ofp, "usage: lttng [OPTIONS] <COMMAND> [<ARGS>]\n"); |
f3ed775e DG |
94 | fprintf(ofp, "\n"); |
95 | fprintf(ofp, "Options:\n"); | |
70318c38 | 96 | fprintf(ofp, " -V, --version Show version\n"); |
99bab54f TD |
97 | fprintf(ofp, " -h, --help Show this help\n"); |
98 | fprintf(ofp, " --list-options Simple listing of lttng options\n"); | |
99 | fprintf(ofp, " --list-commands Simple listing of lttng commands\n"); | |
100 | fprintf(ofp, " -v, --verbose Increase verbosity\n"); | |
101 | fprintf(ofp, " -q, --quiet Quiet mode\n"); | |
c7e35b03 JR |
102 | fprintf(ofp, " -m, --mi TYPE Machine Interface mode.\n"); |
103 | fprintf(ofp, " Type: xml\n"); | |
99bab54f TD |
104 | fprintf(ofp, " -g, --group NAME Unix tracing group name. (default: tracing)\n"); |
105 | fprintf(ofp, " -n, --no-sessiond Don't spawn a session daemon\n"); | |
106 | fprintf(ofp, " --sessiond-path PATH Session daemon full path\n"); | |
8960e9cd | 107 | fprintf(ofp, " --relayd-path PATH Relayd daemon full path\n"); |
f3ed775e DG |
108 | fprintf(ofp, "\n"); |
109 | fprintf(ofp, "Commands:\n"); | |
00e2e675 DG |
110 | fprintf(ofp, " add-context Add context to event and/or channel\n"); |
111 | fprintf(ofp, " calibrate Quantify LTTng overhead\n"); | |
112 | fprintf(ofp, " create Create tracing session\n"); | |
113 | fprintf(ofp, " destroy Tear down tracing session\n"); | |
114 | fprintf(ofp, " enable-channel Enable tracing channel\n"); | |
115 | fprintf(ofp, " enable-event Enable tracing event\n"); | |
116 | fprintf(ofp, " disable-channel Disable tracing channel\n"); | |
117 | fprintf(ofp, " disable-event Disable tracing event\n"); | |
00e2e675 DG |
118 | fprintf(ofp, " list List possible tracing options\n"); |
119 | fprintf(ofp, " set-session Set current session name\n"); | |
57f272ed | 120 | fprintf(ofp, " snapshot Snapshot buffers of current session name\n"); |
00e2e675 DG |
121 | fprintf(ofp, " start Start tracing\n"); |
122 | fprintf(ofp, " stop Stop tracing\n"); | |
123 | fprintf(ofp, " version Show version information\n"); | |
124 | fprintf(ofp, " view Start trace viewer\n"); | |
c864d6d7 | 125 | fprintf(ofp, " save Save session configuration\n"); |
8c42d845 | 126 | fprintf(ofp, " load Load session configuration\n"); |
ccf10263 MD |
127 | fprintf(ofp, " track Track specific system resources\n"); |
128 | fprintf(ofp, " untrack Untrack specific system resources\n"); | |
f3ed775e | 129 | fprintf(ofp, "\n"); |
99bab54f TD |
130 | fprintf(ofp, "Each command also has its own -h, --help option.\n"); |
131 | fprintf(ofp, "\n"); | |
f3ed775e DG |
132 | fprintf(ofp, "Please see the lttng(1) man page for full documentation.\n"); |
133 | fprintf(ofp, "See http://lttng.org for updates, bug reports and news.\n"); | |
8c0faa1d DG |
134 | } |
135 | ||
70318c38 SS |
136 | static void version(FILE *ofp) |
137 | { | |
4c6ac053 MD |
138 | fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME "%s\n", |
139 | progname, | |
140 | GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION); | |
70318c38 SS |
141 | } |
142 | ||
c7e35b03 JR |
143 | /* |
144 | * Find the MI output type enum from a string. This function is for the support | |
145 | * of machine interface output. | |
146 | */ | |
147 | static int mi_output_type(const char *output_type) | |
148 | { | |
149 | int ret = 0; | |
150 | ||
151 | if (!strncasecmp("xml", output_type, 3)) { | |
152 | ret = LTTNG_MI_XML; | |
153 | } else { | |
154 | /* Invalid output format */ | |
155 | ERR("MI output format not supported"); | |
156 | ret = -LTTNG_ERR_MI_OUTPUT_TYPE; | |
157 | } | |
158 | ||
159 | return ret; | |
160 | } | |
161 | ||
865abf65 SM |
162 | /* |
163 | * list_options | |
164 | * | |
165 | * List options line by line. This is mostly for bash auto completion and to | |
166 | * avoid difficult parsing. | |
167 | */ | |
168 | static void list_options(FILE *ofp) | |
169 | { | |
170 | int i = 0; | |
171 | struct option *option = NULL; | |
172 | ||
173 | option = &long_options[i]; | |
174 | while (option->name != NULL) { | |
175 | fprintf(ofp, "--%s\n", option->name); | |
176 | ||
177 | if (isprint(option->val)) { | |
178 | fprintf(ofp, "-%c\n", option->val); | |
179 | } | |
180 | ||
181 | i++; | |
182 | option = &long_options[i]; | |
183 | } | |
184 | } | |
185 | ||
7442b2ba | 186 | /* |
f3ed775e | 187 | * clean_exit |
96243366 | 188 | */ |
f3ed775e | 189 | static void clean_exit(int code) |
96243366 | 190 | { |
f3ed775e DG |
191 | DBG("Clean exit"); |
192 | exit(code); | |
894be886 | 193 | } |
96243366 | 194 | |
894be886 | 195 | /* |
f3ed775e | 196 | * sighandler |
2ef84c95 | 197 | * |
f3ed775e | 198 | * Signal handler for the daemon |
2ef84c95 | 199 | */ |
f3ed775e | 200 | static void sighandler(int sig) |
2ef84c95 | 201 | { |
f3ed775e DG |
202 | switch (sig) { |
203 | case SIGTERM: | |
99bab54f | 204 | DBG("SIGTERM caught"); |
f3ed775e DG |
205 | clean_exit(EXIT_FAILURE); |
206 | break; | |
f3ed775e | 207 | default: |
99bab54f | 208 | DBG("Unknown signal %d caught", sig); |
f3ed775e | 209 | break; |
2ef84c95 DG |
210 | } |
211 | ||
f3ed775e | 212 | return; |
2ef84c95 DG |
213 | } |
214 | ||
215 | /* | |
f3ed775e | 216 | * set_signal_handler |
894be886 | 217 | * |
f3ed775e | 218 | * Setup signal handler for SIGCHLD and SIGTERM. |
894be886 | 219 | */ |
f3ed775e | 220 | static int set_signal_handler(void) |
894be886 | 221 | { |
f3ed775e DG |
222 | int ret = 0; |
223 | struct sigaction sa; | |
224 | sigset_t sigset; | |
33a2b854 | 225 | |
f3ed775e | 226 | if ((ret = sigemptyset(&sigset)) < 0) { |
6f04ed72 | 227 | PERROR("sigemptyset"); |
33a2b854 DG |
228 | goto end; |
229 | } | |
230 | ||
f3ed775e DG |
231 | sa.sa_handler = sighandler; |
232 | sa.sa_mask = sigset; | |
233 | sa.sa_flags = 0; | |
96243366 | 234 | |
f3ed775e | 235 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
6f04ed72 | 236 | PERROR("sigaction"); |
f3ed775e | 237 | goto end; |
894be886 DG |
238 | } |
239 | ||
96243366 | 240 | end: |
57167058 DG |
241 | return ret; |
242 | } | |
243 | ||
fac6795d | 244 | /* |
f3ed775e | 245 | * handle_command |
fac6795d | 246 | * |
f3ed775e DG |
247 | * Handle the full argv list of a first level command. Will find the command |
248 | * in the global commands array and call the function callback associated. | |
1c9f7941 | 249 | * |
f3ed775e DG |
250 | * If command not found, return -1 |
251 | * else, return function command error code. | |
1c9f7941 | 252 | */ |
f3ed775e | 253 | static int handle_command(int argc, char **argv) |
1c9f7941 | 254 | { |
f3ed775e DG |
255 | int i = 0, ret; |
256 | struct cmd_struct *cmd; | |
1c9f7941 | 257 | |
f3ed775e DG |
258 | if (*argv == NULL) { |
259 | ret = CMD_SUCCESS; | |
47b74d63 | 260 | goto end; |
1c9f7941 DG |
261 | } |
262 | ||
f3ed775e DG |
263 | cmd = &commands[i]; |
264 | while (cmd->func != NULL) { | |
265 | /* Find command */ | |
266 | if (strcmp(argv[0], cmd->name) == 0) { | |
267 | ret = cmd->func(argc, (const char**) argv); | |
f3ed775e | 268 | goto end; |
894be886 | 269 | } |
f3ed775e DG |
270 | i++; |
271 | cmd = &commands[i]; | |
894be886 DG |
272 | } |
273 | ||
f3ed775e | 274 | /* Command not found */ |
4ce78777 | 275 | ret = CMD_UNDEFINED; |
894be886 DG |
276 | |
277 | end: | |
f3ed775e | 278 | return ret; |
8548ff30 DG |
279 | } |
280 | ||
5b8719f5 | 281 | /* |
3183dbb0 | 282 | * Parse command line arguments. |
5b8719f5 | 283 | * |
3183dbb0 | 284 | * Return 0 if OK, else -1 |
5b8719f5 | 285 | */ |
f3ed775e | 286 | static int parse_args(int argc, char **argv) |
5b8719f5 | 287 | { |
f3ed775e | 288 | int opt, ret; |
883d80f9 | 289 | char *user; |
5b8719f5 | 290 | |
e8fa9fb0 MD |
291 | if (lttng_is_setuid_setgid()) { |
292 | ERR("'%s' is not allowed to be executed as a setuid/setgid binary for security reasons. Aborting.", argv[0]); | |
293 | clean_exit(EXIT_FAILURE); | |
294 | } | |
295 | ||
f3ed775e DG |
296 | if (argc < 2) { |
297 | usage(stderr); | |
298 | clean_exit(EXIT_FAILURE); | |
5b8719f5 DG |
299 | } |
300 | ||
c7e35b03 | 301 | while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) { |
f3ed775e | 302 | switch (opt) { |
70318c38 SS |
303 | case 'V': |
304 | version(stdout); | |
305 | ret = 0; | |
306 | goto end; | |
f3ed775e | 307 | case 'h': |
3183dbb0 | 308 | usage(stdout); |
ae856491 | 309 | ret = 0; |
3183dbb0 | 310 | goto end; |
f3ed775e | 311 | case 'v': |
849e5b7b DG |
312 | /* There is only 3 possible level of verbosity. (-vvv) */ |
313 | if (lttng_opt_verbose < 3) { | |
314 | lttng_opt_verbose += 1; | |
315 | } | |
f3ed775e DG |
316 | break; |
317 | case 'q': | |
97e19046 | 318 | lttng_opt_quiet = 1; |
f3ed775e | 319 | break; |
c7e35b03 JR |
320 | case 'm': |
321 | lttng_opt_mi = mi_output_type(optarg); | |
322 | if (lttng_opt_mi < 0) { | |
323 | ret = lttng_opt_mi; | |
324 | goto error; | |
325 | } | |
326 | break; | |
f3ed775e DG |
327 | case 'g': |
328 | lttng_set_tracing_group(optarg); | |
329 | break; | |
8490ae7b | 330 | case 'n': |
f3ed775e DG |
331 | opt_no_sessiond = 1; |
332 | break; | |
333 | case OPT_SESSION_PATH: | |
334 | opt_sessiond_path = strdup(optarg); | |
d7c5d39f MD |
335 | if (!opt_sessiond_path) { |
336 | ret = -1; | |
337 | goto error; | |
338 | } | |
f3ed775e | 339 | break; |
8960e9cd DG |
340 | case OPT_RELAYD_PATH: |
341 | opt_relayd_path = strdup(optarg); | |
d7c5d39f MD |
342 | if (!opt_relayd_path) { |
343 | ret = -1; | |
344 | goto error; | |
345 | } | |
8960e9cd | 346 | break; |
865abf65 SM |
347 | case OPT_DUMP_OPTIONS: |
348 | list_options(stdout); | |
349 | ret = 0; | |
3183dbb0 | 350 | goto end; |
865abf65 | 351 | case OPT_DUMP_COMMANDS: |
3c9bd23c | 352 | list_commands(commands, stdout); |
865abf65 | 353 | ret = 0; |
3183dbb0 | 354 | goto end; |
f3ed775e DG |
355 | default: |
356 | usage(stderr); | |
ae856491 | 357 | ret = 1; |
f3ed775e DG |
358 | goto error; |
359 | } | |
5b8719f5 | 360 | } |
fac6795d | 361 | |
f3ed775e | 362 | /* If both options are specified, quiet wins */ |
97e19046 DG |
363 | if (lttng_opt_verbose && lttng_opt_quiet) { |
364 | lttng_opt_verbose = 0; | |
5b8719f5 DG |
365 | } |
366 | ||
f3ed775e DG |
367 | /* No leftovers, print usage and quit */ |
368 | if ((argc - optind) == 0) { | |
369 | usage(stderr); | |
8005f29a | 370 | ret = 1; |
f3ed775e DG |
371 | goto error; |
372 | } | |
7442b2ba | 373 | |
883d80f9 DG |
374 | /* For Mathieu Desnoyers a.k.a. Dr. Tracing */ |
375 | user = getenv("USER"); | |
376 | if (user != NULL && ((strncmp(progname, "drtrace", 7) == 0 || | |
377 | strncmp("compudj", user, 7) == 0))) { | |
378 | MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0); | |
379 | } | |
380 | /* Thanks Mathieu */ | |
381 | ||
92360082 | 382 | /* |
f3ed775e DG |
383 | * Handle leftovers which is a first level command with the trailing |
384 | * options. | |
385 | */ | |
386 | ret = handle_command(argc - optind, argv + optind); | |
ae856491 DG |
387 | switch (ret) { |
388 | case CMD_WARNING: | |
389 | WARN("Some command(s) went wrong"); | |
390 | break; | |
391 | case CMD_ERROR: | |
392 | ERR("Command error"); | |
393 | break; | |
394 | case CMD_UNDEFINED: | |
395 | ERR("Undefined command"); | |
396 | break; | |
397 | case CMD_FATAL: | |
398 | ERR("Fatal error"); | |
399 | break; | |
4ce78777 DG |
400 | case CMD_UNSUPPORTED: |
401 | ERR("Unsupported command"); | |
402 | break; | |
ae856491 DG |
403 | case -1: |
404 | usage(stderr); | |
405 | ret = 1; | |
406 | break; | |
407 | case 0: | |
408 | break; | |
409 | default: | |
42224349 DG |
410 | if (ret < 0) { |
411 | ret = -ret; | |
412 | } | |
ae856491 | 413 | break; |
96243366 DG |
414 | } |
415 | ||
3183dbb0 | 416 | end: |
f3ed775e | 417 | error: |
ae856491 | 418 | return ret; |
fac6795d DG |
419 | } |
420 | ||
f3ed775e | 421 | |
fac6795d | 422 | /* |
5b8719f5 | 423 | * main |
fac6795d DG |
424 | */ |
425 | int main(int argc, char *argv[]) | |
426 | { | |
427 | int ret; | |
428 | ||
429 | progname = argv[0] ? argv[0] : "lttng"; | |
430 | ||
5b8719f5 DG |
431 | ret = set_signal_handler(); |
432 | if (ret < 0) { | |
87378cf5 | 433 | clean_exit(ret); |
5b8719f5 DG |
434 | } |
435 | ||
f3ed775e | 436 | ret = parse_args(argc, argv); |
ae856491 DG |
437 | if (ret != 0) { |
438 | clean_exit(ret); | |
1fff1faa | 439 | } |
87378cf5 | 440 | |
fac6795d DG |
441 | return 0; |
442 | } |