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