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 | |
5 | * it under the terms of the GNU General Public License as published by | |
82a3637f DG |
6 | * as published by the Free Software Foundation; only version 2 |
7 | * of the License. | |
fac6795d DG |
8 | * |
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. | |
13 | * | |
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. | |
fac6795d DG |
17 | */ |
18 | ||
19 | #define _GNU_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> | |
fac6795d | 25 | #include <unistd.h> |
3bd1e081 | 26 | #include <config.h> |
fac6795d | 27 | |
5b97ec60 | 28 | #include <lttng/lttng.h> |
db758600 | 29 | #include <common/error.h> |
fac6795d | 30 | |
c399183f | 31 | #include "command.h" |
fac6795d DG |
32 | |
33 | /* Variables */ | |
34 | static char *progname; | |
fac6795d | 35 | |
f3ed775e DG |
36 | int opt_quiet; |
37 | int opt_verbose; | |
38 | static int opt_no_sessiond; | |
39 | static char *opt_sessiond_path; | |
40 | ||
41 | enum { | |
f3ed775e | 42 | OPT_SESSION_PATH, |
865abf65 SM |
43 | OPT_DUMP_OPTIONS, |
44 | OPT_DUMP_COMMANDS, | |
f3ed775e DG |
45 | }; |
46 | ||
47 | /* Getopt options. No first level command. */ | |
48 | static struct option long_options[] = { | |
49 | {"help", 0, NULL, 'h'}, | |
50 | {"group", 1, NULL, 'g'}, | |
51 | {"verbose", 0, NULL, 'v'}, | |
52 | {"quiet", 0, NULL, 'q'}, | |
8490ae7b | 53 | {"no-sessiond", 0, NULL, 'n'}, |
f3ed775e | 54 | {"sessiond-path", 1, NULL, OPT_SESSION_PATH}, |
865abf65 SM |
55 | {"list-options", 0, NULL, OPT_DUMP_OPTIONS}, |
56 | {"list-commands", 0, NULL, OPT_DUMP_COMMANDS}, | |
f3ed775e DG |
57 | {NULL, 0, NULL, 0} |
58 | }; | |
59 | ||
60 | /* First level command */ | |
61 | static struct cmd_struct commands[] = { | |
62 | { "list", cmd_list}, | |
63 | { "create", cmd_create}, | |
64 | { "destroy", cmd_destroy}, | |
f3ed775e DG |
65 | { "start", cmd_start}, |
66 | { "stop", cmd_stop}, | |
67 | { "enable-event", cmd_enable_events}, | |
e953ef25 | 68 | { "disable-event", cmd_disable_events}, |
d36b8583 | 69 | { "enable-channel", cmd_enable_channels}, |
26cc6b4e | 70 | { "disable-channel", cmd_disable_channels}, |
d65106b1 | 71 | { "add-context", cmd_add_context}, |
3087ef18 | 72 | { "set-session", cmd_set_session}, |
eb9cb8b7 | 73 | { "version", cmd_version}, |
d0254c7c | 74 | { "calibrate", cmd_calibrate}, |
f3ed775e DG |
75 | { NULL, NULL} /* Array closure */ |
76 | }; | |
77 | ||
78 | static void usage(FILE *ofp) | |
8c0faa1d | 79 | { |
f3ed775e DG |
80 | fprintf(ofp, "LTTng Trace Control " VERSION"\n\n"); |
81 | fprintf(ofp, "usage: lttng [options] <command>\n"); | |
82 | fprintf(ofp, "\n"); | |
83 | fprintf(ofp, "Options:\n"); | |
84 | fprintf(ofp, " -h, --help Show this help\n"); | |
85 | fprintf(ofp, " -g, --group NAME Unix tracing group name. (default: tracing)\n"); | |
86 | fprintf(ofp, " -v, --verbose Verbose mode\n"); | |
87 | fprintf(ofp, " -q, --quiet Quiet mode\n"); | |
8490ae7b | 88 | fprintf(ofp, " -n, --no-sessiond Don't spawn a session daemon\n"); |
f3ed775e | 89 | fprintf(ofp, " --sessiond-path Session daemon full path\n"); |
865abf65 SM |
90 | fprintf(ofp, " --list-options Simple listing of lttng options\n"); |
91 | fprintf(ofp, " --list-commands Simple listing of lttng commands\n"); | |
f3ed775e DG |
92 | fprintf(ofp, "\n"); |
93 | fprintf(ofp, "Commands:\n"); | |
d65106b1 | 94 | fprintf(ofp, " add-context Add context to event or/and channel\n"); |
d0254c7c | 95 | fprintf(ofp, " calibrate Quantify LTTng overhead\n"); |
f3ed775e DG |
96 | fprintf(ofp, " create Create tracing session\n"); |
97 | fprintf(ofp, " destroy Teardown tracing session\n"); | |
d36b8583 | 98 | fprintf(ofp, " enable-channel Enable tracing channel\n"); |
26cc6b4e DG |
99 | fprintf(ofp, " enable-event Enable tracing event\n"); |
100 | fprintf(ofp, " disable-channel Disable tracing channel\n"); | |
f3ed775e DG |
101 | fprintf(ofp, " disable-event Disable tracing event\n"); |
102 | fprintf(ofp, " list List possible tracing options\n"); | |
3087ef18 | 103 | fprintf(ofp, " set-session Set current session name\n"); |
f3ed775e DG |
104 | fprintf(ofp, " start Start tracing\n"); |
105 | fprintf(ofp, " stop Stop tracing\n"); | |
106 | fprintf(ofp, " version Show version information\n"); | |
107 | fprintf(ofp, "\n"); | |
108 | fprintf(ofp, "Please see the lttng(1) man page for full documentation.\n"); | |
109 | fprintf(ofp, "See http://lttng.org for updates, bug reports and news.\n"); | |
8c0faa1d DG |
110 | } |
111 | ||
865abf65 SM |
112 | /* |
113 | * list_options | |
114 | * | |
115 | * List options line by line. This is mostly for bash auto completion and to | |
116 | * avoid difficult parsing. | |
117 | */ | |
118 | static void list_options(FILE *ofp) | |
119 | { | |
120 | int i = 0; | |
121 | struct option *option = NULL; | |
122 | ||
123 | option = &long_options[i]; | |
124 | while (option->name != NULL) { | |
125 | fprintf(ofp, "--%s\n", option->name); | |
126 | ||
127 | if (isprint(option->val)) { | |
128 | fprintf(ofp, "-%c\n", option->val); | |
129 | } | |
130 | ||
131 | i++; | |
132 | option = &long_options[i]; | |
133 | } | |
134 | } | |
135 | ||
136 | /* | |
137 | * list_commands | |
138 | * | |
139 | * List commands line by line. This is mostly for bash auto completion and to | |
140 | * avoid difficult parsing. | |
141 | */ | |
142 | static void list_commands(FILE *ofp) | |
143 | { | |
144 | int i = 0; | |
145 | struct cmd_struct *cmd = NULL; | |
146 | ||
147 | cmd = &commands[i]; | |
148 | while (cmd->name != NULL) { | |
149 | fprintf(ofp, "%s\n", cmd->name); | |
150 | i++; | |
151 | cmd = &commands[i]; | |
152 | } | |
153 | } | |
154 | ||
7442b2ba | 155 | /* |
f3ed775e | 156 | * clean_exit |
96243366 | 157 | */ |
f3ed775e | 158 | static void clean_exit(int code) |
96243366 | 159 | { |
f3ed775e DG |
160 | DBG("Clean exit"); |
161 | exit(code); | |
894be886 | 162 | } |
96243366 | 163 | |
894be886 | 164 | /* |
f3ed775e | 165 | * sighandler |
2ef84c95 | 166 | * |
f3ed775e | 167 | * Signal handler for the daemon |
2ef84c95 | 168 | */ |
f3ed775e | 169 | static void sighandler(int sig) |
2ef84c95 | 170 | { |
f3ed775e DG |
171 | switch (sig) { |
172 | case SIGTERM: | |
3cede4fe | 173 | DBG("SIGTERM caugth"); |
f3ed775e DG |
174 | clean_exit(EXIT_FAILURE); |
175 | break; | |
176 | case SIGCHLD: | |
177 | /* Notify is done */ | |
3cede4fe | 178 | DBG("SIGCHLD caugth"); |
f3ed775e DG |
179 | break; |
180 | default: | |
3cede4fe | 181 | DBG("Unknown signal %d caugth", sig); |
f3ed775e | 182 | break; |
2ef84c95 DG |
183 | } |
184 | ||
f3ed775e | 185 | return; |
2ef84c95 DG |
186 | } |
187 | ||
188 | /* | |
f3ed775e | 189 | * set_signal_handler |
894be886 | 190 | * |
f3ed775e | 191 | * Setup signal handler for SIGCHLD and SIGTERM. |
894be886 | 192 | */ |
f3ed775e | 193 | static int set_signal_handler(void) |
894be886 | 194 | { |
f3ed775e DG |
195 | int ret = 0; |
196 | struct sigaction sa; | |
197 | sigset_t sigset; | |
33a2b854 | 198 | |
f3ed775e DG |
199 | if ((ret = sigemptyset(&sigset)) < 0) { |
200 | perror("sigemptyset"); | |
33a2b854 DG |
201 | goto end; |
202 | } | |
203 | ||
f3ed775e DG |
204 | sa.sa_handler = sighandler; |
205 | sa.sa_mask = sigset; | |
206 | sa.sa_flags = 0; | |
207 | if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) { | |
208 | perror("sigaction"); | |
96243366 DG |
209 | goto end; |
210 | } | |
211 | ||
f3ed775e DG |
212 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
213 | perror("sigaction"); | |
214 | goto end; | |
894be886 DG |
215 | } |
216 | ||
96243366 | 217 | end: |
57167058 DG |
218 | return ret; |
219 | } | |
220 | ||
fac6795d | 221 | /* |
f3ed775e | 222 | * handle_command |
fac6795d | 223 | * |
f3ed775e DG |
224 | * Handle the full argv list of a first level command. Will find the command |
225 | * in the global commands array and call the function callback associated. | |
1c9f7941 | 226 | * |
f3ed775e DG |
227 | * If command not found, return -1 |
228 | * else, return function command error code. | |
1c9f7941 | 229 | */ |
f3ed775e | 230 | static int handle_command(int argc, char **argv) |
1c9f7941 | 231 | { |
f3ed775e DG |
232 | int i = 0, ret; |
233 | struct cmd_struct *cmd; | |
1c9f7941 | 234 | |
f3ed775e DG |
235 | if (*argv == NULL) { |
236 | ret = CMD_SUCCESS; | |
47b74d63 | 237 | goto end; |
1c9f7941 DG |
238 | } |
239 | ||
f3ed775e DG |
240 | cmd = &commands[i]; |
241 | while (cmd->func != NULL) { | |
242 | /* Find command */ | |
243 | if (strcmp(argv[0], cmd->name) == 0) { | |
244 | ret = cmd->func(argc, (const char**) argv); | |
245 | switch (ret) { | |
246 | case CMD_ERROR: | |
247 | ERR("Command error"); | |
248 | break; | |
249 | case CMD_NOT_IMPLEMENTED: | |
250 | ERR("Options not implemented"); | |
251 | break; | |
252 | case CMD_UNDEFINED: | |
253 | ERR("Undefined command"); | |
254 | break; | |
255 | case CMD_FATAL: | |
256 | ERR("Fatal error"); | |
257 | break; | |
258 | } | |
259 | goto end; | |
894be886 | 260 | } |
f3ed775e DG |
261 | i++; |
262 | cmd = &commands[i]; | |
894be886 DG |
263 | } |
264 | ||
f3ed775e DG |
265 | /* Command not found */ |
266 | ret = -1; | |
894be886 DG |
267 | |
268 | end: | |
f3ed775e | 269 | return ret; |
8548ff30 DG |
270 | } |
271 | ||
5b8719f5 DG |
272 | /* |
273 | * spawn_sessiond | |
274 | * | |
275 | * Spawn a session daemon by forking and execv. | |
276 | */ | |
277 | static int spawn_sessiond(char *pathname) | |
278 | { | |
279 | int ret = 0; | |
280 | pid_t pid; | |
281 | ||
f3ed775e | 282 | MSG("Spawning a session daemon"); |
5b8719f5 DG |
283 | pid = fork(); |
284 | if (pid == 0) { | |
5e16da05 MD |
285 | /* |
286 | * Spawn session daemon and tell | |
5b8719f5 DG |
287 | * it to signal us when ready. |
288 | */ | |
32258573 | 289 | execlp(pathname, "lttng-sessiond", "--sig-parent", "--quiet", NULL); |
5e16da05 MD |
290 | /* execlp only returns if error happened */ |
291 | if (errno == ENOENT) { | |
292 | ERR("No session daemon found. Use --sessiond-path."); | |
293 | } else { | |
294 | perror("execlp"); | |
5b8719f5 | 295 | } |
5e16da05 MD |
296 | kill(getppid(), SIGTERM); /* unpause parent */ |
297 | exit(EXIT_FAILURE); | |
5b8719f5 | 298 | } else if (pid > 0) { |
32258573 | 299 | /* Wait for lttng-sessiond to start */ |
5b8719f5 DG |
300 | pause(); |
301 | goto end; | |
302 | } else { | |
303 | perror("fork"); | |
304 | ret = -1; | |
305 | goto end; | |
306 | } | |
307 | ||
308 | end: | |
309 | return ret; | |
310 | } | |
311 | ||
fac6795d | 312 | /* |
f3ed775e | 313 | * check_sessiond |
fac6795d DG |
314 | * |
315 | * Check if the session daemon is available using | |
5b8719f5 DG |
316 | * the liblttngctl API for the check. If not, try to |
317 | * spawn a daemon. | |
fac6795d | 318 | */ |
f3ed775e | 319 | static int check_sessiond(void) |
fac6795d DG |
320 | { |
321 | int ret; | |
5e16da05 | 322 | char *pathname = NULL, *alloc_pathname = NULL; |
fac6795d | 323 | |
947308c4 DG |
324 | ret = lttng_session_daemon_alive(); |
325 | if (ret == 0) { /* not alive */ | |
5b8719f5 DG |
326 | /* Try command line option path */ |
327 | if (opt_sessiond_path != NULL) { | |
328 | ret = access(opt_sessiond_path, F_OK | X_OK); | |
329 | if (ret < 0) { | |
330 | ERR("No such file: %s", opt_sessiond_path); | |
331 | goto end; | |
332 | } | |
333 | pathname = opt_sessiond_path; | |
334 | } else { | |
335 | /* Try LTTNG_SESSIOND_PATH env variable */ | |
e8f07c63 | 336 | pathname = getenv(LTTNG_SESSIOND_PATH_ENV); |
5b8719f5 DG |
337 | } |
338 | ||
339 | /* Let's rock and roll */ | |
340 | if (pathname == NULL) { | |
32258573 | 341 | ret = asprintf(&alloc_pathname, INSTALL_BIN_PATH "/lttng-sessiond"); |
5b8719f5 | 342 | if (ret < 0) { |
3f5fa9ed | 343 | perror("asprintf spawn sessiond"); |
5b8719f5 DG |
344 | goto end; |
345 | } | |
5e16da05 | 346 | pathname = alloc_pathname; |
5b8719f5 DG |
347 | } |
348 | ||
349 | ret = spawn_sessiond(pathname); | |
5e16da05 | 350 | free(alloc_pathname); |
5b8719f5 DG |
351 | if (ret < 0) { |
352 | ERR("Problem occurs when starting %s", pathname); | |
353 | goto end; | |
354 | } | |
fac6795d DG |
355 | } |
356 | ||
5b8719f5 | 357 | end: |
fac6795d DG |
358 | return ret; |
359 | } | |
360 | ||
bcfa8a05 DG |
361 | /* |
362 | * Check for the "help" option in the argv. If found, return 1 else return 0. | |
363 | */ | |
364 | static int check_help_command(int argc, char **argv) | |
365 | { | |
366 | int i; | |
367 | ||
368 | for (i = 0; i < argc; i++) { | |
369 | if ((strncmp(argv[i], "-h", 2) == 0) || | |
76150f62 | 370 | strncmp(argv[i], "--h", 3) == 0) { |
bcfa8a05 DG |
371 | return 1; |
372 | } | |
373 | } | |
374 | ||
375 | return 0; | |
376 | } | |
377 | ||
5b8719f5 | 378 | /* |
f3ed775e | 379 | * parse_args |
5b8719f5 | 380 | * |
f3ed775e DG |
381 | * Parse command line arguments. |
382 | * Return 0 if OK, else -1 | |
5b8719f5 | 383 | */ |
f3ed775e | 384 | static int parse_args(int argc, char **argv) |
5b8719f5 | 385 | { |
f3ed775e | 386 | int opt, ret; |
5b8719f5 | 387 | |
f3ed775e DG |
388 | if (argc < 2) { |
389 | usage(stderr); | |
390 | clean_exit(EXIT_FAILURE); | |
5b8719f5 DG |
391 | } |
392 | ||
8490ae7b | 393 | while ((opt = getopt_long(argc, argv, "+hnvqg:", long_options, NULL)) != -1) { |
f3ed775e DG |
394 | switch (opt) { |
395 | case 'h': | |
396 | usage(stderr); | |
397 | goto error; | |
398 | case 'v': | |
b551a063 | 399 | opt_verbose += 1; |
f3ed775e DG |
400 | break; |
401 | case 'q': | |
402 | opt_quiet = 1; | |
403 | break; | |
404 | case 'g': | |
405 | lttng_set_tracing_group(optarg); | |
406 | break; | |
8490ae7b | 407 | case 'n': |
f3ed775e DG |
408 | opt_no_sessiond = 1; |
409 | break; | |
410 | case OPT_SESSION_PATH: | |
411 | opt_sessiond_path = strdup(optarg); | |
412 | break; | |
865abf65 SM |
413 | case OPT_DUMP_OPTIONS: |
414 | list_options(stdout); | |
415 | ret = 0; | |
416 | goto error; | |
417 | case OPT_DUMP_COMMANDS: | |
418 | list_commands(stdout); | |
419 | ret = 0; | |
420 | goto error; | |
f3ed775e DG |
421 | default: |
422 | usage(stderr); | |
423 | goto error; | |
424 | } | |
5b8719f5 | 425 | } |
fac6795d | 426 | |
f3ed775e DG |
427 | /* If both options are specified, quiet wins */ |
428 | if (opt_verbose && opt_quiet) { | |
429 | opt_verbose = 0; | |
5b8719f5 DG |
430 | } |
431 | ||
f3ed775e | 432 | /* Spawn session daemon if needed */ |
bcfa8a05 DG |
433 | if (opt_no_sessiond == 0 && check_help_command(argc, argv) == 0 && |
434 | (check_sessiond() < 0)) { | |
f3ed775e | 435 | goto error; |
5b8719f5 DG |
436 | } |
437 | ||
f3ed775e DG |
438 | /* No leftovers, print usage and quit */ |
439 | if ((argc - optind) == 0) { | |
440 | usage(stderr); | |
441 | goto error; | |
442 | } | |
7442b2ba | 443 | |
f3ed775e DG |
444 | /* |
445 | * Handle leftovers which is a first level command with the trailing | |
446 | * options. | |
447 | */ | |
448 | ret = handle_command(argc - optind, argv + optind); | |
449 | if (ret < 0) { | |
450 | if (ret == -1) { | |
451 | usage(stderr); | |
f3ed775e | 452 | } else { |
9a745bc7 | 453 | ERR("%s", lttng_strerror(ret)); |
f3ed775e | 454 | } |
1fff1faa | 455 | goto error; |
96243366 DG |
456 | } |
457 | ||
1fff1faa | 458 | return 0; |
f3ed775e DG |
459 | |
460 | error: | |
461 | return -1; | |
fac6795d DG |
462 | } |
463 | ||
f3ed775e | 464 | |
fac6795d | 465 | /* |
5b8719f5 | 466 | * main |
fac6795d DG |
467 | */ |
468 | int main(int argc, char *argv[]) | |
469 | { | |
470 | int ret; | |
471 | ||
472 | progname = argv[0] ? argv[0] : "lttng"; | |
473 | ||
474 | /* For Mathieu Desnoyers aka Dr Tracing */ | |
475 | if (strncmp(progname, "drtrace", 7) == 0) { | |
476 | MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0); | |
477 | } | |
478 | ||
5b8719f5 DG |
479 | ret = set_signal_handler(); |
480 | if (ret < 0) { | |
87378cf5 | 481 | clean_exit(ret); |
5b8719f5 DG |
482 | } |
483 | ||
f3ed775e | 484 | ret = parse_args(argc, argv); |
1fff1faa DG |
485 | if (ret < 0) { |
486 | clean_exit(EXIT_FAILURE); | |
487 | } | |
87378cf5 | 488 | |
fac6795d DG |
489 | return 0; |
490 | } |