| 1 | /* |
| 2 | * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca> |
| 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, version 2 only, |
| 6 | * as published by the Free Software Foundation. |
| 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. |
| 16 | */ |
| 17 | |
| 18 | #define _GNU_SOURCE |
| 19 | #include <getopt.h> |
| 20 | #include <signal.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/wait.h> |
| 26 | #include <unistd.h> |
| 27 | #include <config.h> |
| 28 | #include <ctype.h> |
| 29 | |
| 30 | #include <lttng/lttng.h> |
| 31 | #include <common/error.h> |
| 32 | |
| 33 | #include "command.h" |
| 34 | |
| 35 | /* Variables */ |
| 36 | static char *progname; |
| 37 | static int opt_no_sessiond; |
| 38 | static char *opt_sessiond_path; |
| 39 | static pid_t sessiond_pid; |
| 40 | static volatile int recv_child_signal; |
| 41 | |
| 42 | char *opt_relayd_path; |
| 43 | |
| 44 | enum { |
| 45 | OPT_RELAYD_PATH, |
| 46 | OPT_SESSION_PATH, |
| 47 | OPT_DUMP_OPTIONS, |
| 48 | OPT_DUMP_COMMANDS, |
| 49 | }; |
| 50 | |
| 51 | /* Getopt options. No first level command. */ |
| 52 | static struct option long_options[] = { |
| 53 | {"version", 0, NULL, 'V'}, |
| 54 | {"help", 0, NULL, 'h'}, |
| 55 | {"group", 1, NULL, 'g'}, |
| 56 | {"verbose", 0, NULL, 'v'}, |
| 57 | {"quiet", 0, NULL, 'q'}, |
| 58 | {"mi", 1, NULL, 'm'}, |
| 59 | {"no-sessiond", 0, NULL, 'n'}, |
| 60 | {"sessiond-path", 1, NULL, OPT_SESSION_PATH}, |
| 61 | {"relayd-path", 1, NULL, OPT_RELAYD_PATH}, |
| 62 | {"list-options", 0, NULL, OPT_DUMP_OPTIONS}, |
| 63 | {"list-commands", 0, NULL, OPT_DUMP_COMMANDS}, |
| 64 | {NULL, 0, NULL, 0} |
| 65 | }; |
| 66 | |
| 67 | /* First level command */ |
| 68 | static struct cmd_struct commands[] = { |
| 69 | { "list", cmd_list}, |
| 70 | { "create", cmd_create}, |
| 71 | { "destroy", cmd_destroy}, |
| 72 | { "start", cmd_start}, |
| 73 | { "stop", cmd_stop}, |
| 74 | { "enable-event", cmd_enable_events}, |
| 75 | { "disable-event", cmd_disable_events}, |
| 76 | { "enable-channel", cmd_enable_channels}, |
| 77 | { "disable-channel", cmd_disable_channels}, |
| 78 | { "add-context", cmd_add_context}, |
| 79 | { "set-session", cmd_set_session}, |
| 80 | { "version", cmd_version}, |
| 81 | { "calibrate", cmd_calibrate}, |
| 82 | { "view", cmd_view}, |
| 83 | { "snapshot", cmd_snapshot}, |
| 84 | { "save", cmd_save}, |
| 85 | { "load", cmd_load}, |
| 86 | { NULL, NULL} /* Array closure */ |
| 87 | }; |
| 88 | |
| 89 | static void usage(FILE *ofp) |
| 90 | { |
| 91 | fprintf(ofp, "LTTng Trace Control " VERSION " - " VERSION_NAME "%s\n\n", |
| 92 | GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION); |
| 93 | fprintf(ofp, "usage: lttng [OPTIONS] <COMMAND> [<ARGS>]\n"); |
| 94 | fprintf(ofp, "\n"); |
| 95 | fprintf(ofp, "Options:\n"); |
| 96 | fprintf(ofp, " -V, --version Show version\n"); |
| 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"); |
| 102 | fprintf(ofp, " -m, --mi TYPE Machine Interface mode.\n"); |
| 103 | fprintf(ofp, " Type: xml\n"); |
| 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"); |
| 107 | fprintf(ofp, " --relayd-path PATH Relayd daemon full path\n"); |
| 108 | fprintf(ofp, "\n"); |
| 109 | fprintf(ofp, "Commands:\n"); |
| 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"); |
| 118 | fprintf(ofp, " list List possible tracing options\n"); |
| 119 | fprintf(ofp, " set-session Set current session name\n"); |
| 120 | fprintf(ofp, " snapshot Snapshot buffers of current session name\n"); |
| 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"); |
| 125 | fprintf(ofp, " save Save session configuration\n"); |
| 126 | fprintf(ofp, " load Load session configuration\n"); |
| 127 | fprintf(ofp, "\n"); |
| 128 | fprintf(ofp, "Each command also has its own -h, --help option.\n"); |
| 129 | fprintf(ofp, "\n"); |
| 130 | fprintf(ofp, "Please see the lttng(1) man page for full documentation.\n"); |
| 131 | fprintf(ofp, "See http://lttng.org for updates, bug reports and news.\n"); |
| 132 | } |
| 133 | |
| 134 | static void version(FILE *ofp) |
| 135 | { |
| 136 | fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME "%s\n", |
| 137 | progname, |
| 138 | GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION); |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Find the MI output type enum from a string. This function is for the support |
| 143 | * of machine interface output. |
| 144 | */ |
| 145 | static int mi_output_type(const char *output_type) |
| 146 | { |
| 147 | int ret = 0; |
| 148 | |
| 149 | if (!strncasecmp("xml", output_type, 3)) { |
| 150 | ret = LTTNG_MI_XML; |
| 151 | } else { |
| 152 | /* Invalid output format */ |
| 153 | ERR("MI output format not supported"); |
| 154 | ret = -LTTNG_ERR_MI_OUTPUT_TYPE; |
| 155 | } |
| 156 | |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * list_options |
| 162 | * |
| 163 | * List options line by line. This is mostly for bash auto completion and to |
| 164 | * avoid difficult parsing. |
| 165 | */ |
| 166 | static void list_options(FILE *ofp) |
| 167 | { |
| 168 | int i = 0; |
| 169 | struct option *option = NULL; |
| 170 | |
| 171 | option = &long_options[i]; |
| 172 | while (option->name != NULL) { |
| 173 | fprintf(ofp, "--%s\n", option->name); |
| 174 | |
| 175 | if (isprint(option->val)) { |
| 176 | fprintf(ofp, "-%c\n", option->val); |
| 177 | } |
| 178 | |
| 179 | i++; |
| 180 | option = &long_options[i]; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /* |
| 185 | * clean_exit |
| 186 | */ |
| 187 | static void clean_exit(int code) |
| 188 | { |
| 189 | DBG("Clean exit"); |
| 190 | exit(code); |
| 191 | } |
| 192 | |
| 193 | /* |
| 194 | * sighandler |
| 195 | * |
| 196 | * Signal handler for the daemon |
| 197 | */ |
| 198 | static void sighandler(int sig) |
| 199 | { |
| 200 | int status; |
| 201 | |
| 202 | switch (sig) { |
| 203 | case SIGTERM: |
| 204 | DBG("SIGTERM caught"); |
| 205 | clean_exit(EXIT_FAILURE); |
| 206 | break; |
| 207 | case SIGCHLD: |
| 208 | DBG("SIGCHLD caught"); |
| 209 | waitpid(sessiond_pid, &status, 0); |
| 210 | recv_child_signal = 1; |
| 211 | /* Indicate that the session daemon died */ |
| 212 | sessiond_pid = 0; |
| 213 | ERR("Session daemon died (exit status %d)", WEXITSTATUS(status)); |
| 214 | break; |
| 215 | case SIGUSR1: |
| 216 | /* Notify is done */ |
| 217 | recv_child_signal = 1; |
| 218 | DBG("SIGUSR1 caught"); |
| 219 | break; |
| 220 | default: |
| 221 | DBG("Unknown signal %d caught", sig); |
| 222 | break; |
| 223 | } |
| 224 | |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * set_signal_handler |
| 230 | * |
| 231 | * Setup signal handler for SIGCHLD and SIGTERM. |
| 232 | */ |
| 233 | static int set_signal_handler(void) |
| 234 | { |
| 235 | int ret = 0; |
| 236 | struct sigaction sa; |
| 237 | sigset_t sigset; |
| 238 | |
| 239 | if ((ret = sigemptyset(&sigset)) < 0) { |
| 240 | perror("sigemptyset"); |
| 241 | goto end; |
| 242 | } |
| 243 | |
| 244 | sa.sa_handler = sighandler; |
| 245 | sa.sa_mask = sigset; |
| 246 | sa.sa_flags = 0; |
| 247 | if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) { |
| 248 | perror("sigaction"); |
| 249 | goto end; |
| 250 | } |
| 251 | |
| 252 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
| 253 | perror("sigaction"); |
| 254 | goto end; |
| 255 | } |
| 256 | |
| 257 | if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) { |
| 258 | perror("sigaction"); |
| 259 | goto end; |
| 260 | } |
| 261 | |
| 262 | end: |
| 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * handle_command |
| 268 | * |
| 269 | * Handle the full argv list of a first level command. Will find the command |
| 270 | * in the global commands array and call the function callback associated. |
| 271 | * |
| 272 | * If command not found, return -1 |
| 273 | * else, return function command error code. |
| 274 | */ |
| 275 | static int handle_command(int argc, char **argv) |
| 276 | { |
| 277 | int i = 0, ret; |
| 278 | struct cmd_struct *cmd; |
| 279 | |
| 280 | if (*argv == NULL) { |
| 281 | ret = CMD_SUCCESS; |
| 282 | goto end; |
| 283 | } |
| 284 | |
| 285 | cmd = &commands[i]; |
| 286 | while (cmd->func != NULL) { |
| 287 | /* Find command */ |
| 288 | if (strcmp(argv[0], cmd->name) == 0) { |
| 289 | ret = cmd->func(argc, (const char**) argv); |
| 290 | goto end; |
| 291 | } |
| 292 | i++; |
| 293 | cmd = &commands[i]; |
| 294 | } |
| 295 | |
| 296 | /* Command not found */ |
| 297 | ret = CMD_UNDEFINED; |
| 298 | |
| 299 | end: |
| 300 | return ret; |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * spawn_sessiond |
| 305 | * |
| 306 | * Spawn a session daemon by forking and execv. |
| 307 | */ |
| 308 | static int spawn_sessiond(char *pathname) |
| 309 | { |
| 310 | int ret = 0; |
| 311 | pid_t pid; |
| 312 | |
| 313 | MSG("Spawning a session daemon"); |
| 314 | recv_child_signal = 0; |
| 315 | pid = fork(); |
| 316 | if (pid == 0) { |
| 317 | /* |
| 318 | * Spawn session daemon and tell |
| 319 | * it to signal us when ready. |
| 320 | */ |
| 321 | execlp(pathname, "lttng-sessiond", "--sig-parent", "--quiet", NULL); |
| 322 | /* execlp only returns if error happened */ |
| 323 | if (errno == ENOENT) { |
| 324 | ERR("No session daemon found. Use --sessiond-path."); |
| 325 | } else { |
| 326 | perror("execlp"); |
| 327 | } |
| 328 | kill(getppid(), SIGTERM); /* wake parent */ |
| 329 | exit(EXIT_FAILURE); |
| 330 | } else if (pid > 0) { |
| 331 | sessiond_pid = pid; |
| 332 | /* |
| 333 | * Wait for lttng-sessiond to start. We need to use a flag to check if |
| 334 | * the signal has been sent to us, because the child can be scheduled |
| 335 | * before the parent, and thus send the signal before this check. In |
| 336 | * the signal handler, we set the recv_child_signal flag, so anytime we |
| 337 | * check it after the fork is fine. Note that sleep() is interrupted |
| 338 | * before the 1 second delay as soon as the signal is received, so it |
| 339 | * will not cause visible delay for the user. |
| 340 | */ |
| 341 | while (!recv_child_signal) { |
| 342 | sleep(1); |
| 343 | } |
| 344 | /* |
| 345 | * The signal handler will nullify sessiond_pid on SIGCHLD |
| 346 | */ |
| 347 | if (!sessiond_pid) { |
| 348 | exit(EXIT_FAILURE); |
| 349 | } |
| 350 | goto end; |
| 351 | } else { |
| 352 | perror("fork"); |
| 353 | ret = -1; |
| 354 | goto end; |
| 355 | } |
| 356 | |
| 357 | end: |
| 358 | return ret; |
| 359 | } |
| 360 | |
| 361 | /* |
| 362 | * check_sessiond |
| 363 | * |
| 364 | * Check if the session daemon is available using |
| 365 | * the liblttngctl API for the check. If not, try to |
| 366 | * spawn a daemon. |
| 367 | */ |
| 368 | static int check_sessiond(void) |
| 369 | { |
| 370 | int ret; |
| 371 | char *pathname = NULL; |
| 372 | |
| 373 | ret = lttng_session_daemon_alive(); |
| 374 | if (ret == 0) { /* not alive */ |
| 375 | /* Try command line option path */ |
| 376 | pathname = opt_sessiond_path; |
| 377 | |
| 378 | /* Try LTTNG_SESSIOND_PATH env variable */ |
| 379 | if (pathname == NULL) { |
| 380 | pathname = getenv(DEFAULT_SESSIOND_PATH_ENV); |
| 381 | } |
| 382 | |
| 383 | /* Try with configured path */ |
| 384 | if (pathname == NULL) { |
| 385 | if (CONFIG_SESSIOND_BIN[0] != '\0') { |
| 386 | pathname = CONFIG_SESSIOND_BIN; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /* Let's rock and roll while trying the default path */ |
| 391 | if (pathname == NULL) { |
| 392 | pathname = INSTALL_BIN_PATH "/lttng-sessiond"; |
| 393 | } |
| 394 | |
| 395 | DBG("Session daemon at: %s", pathname); |
| 396 | |
| 397 | /* Check existence and permissions */ |
| 398 | ret = access(pathname, F_OK | X_OK); |
| 399 | if (ret < 0) { |
| 400 | ERR("No such file or access denied: %s", pathname); |
| 401 | goto end; |
| 402 | } |
| 403 | |
| 404 | ret = spawn_sessiond(pathname); |
| 405 | if (ret < 0) { |
| 406 | ERR("Problem occurred when starting %s", pathname); |
| 407 | } |
| 408 | } |
| 409 | end: |
| 410 | return ret; |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | * Check args for specific options that *must* not trigger a session daemon |
| 415 | * execution. |
| 416 | * |
| 417 | * Return 1 if match else 0. |
| 418 | */ |
| 419 | static int check_args_no_sessiond(int argc, char **argv) |
| 420 | { |
| 421 | int i; |
| 422 | |
| 423 | for (i = 0; i < argc; i++) { |
| 424 | if ((strncmp(argv[i], "-h", sizeof("-h")) == 0) || |
| 425 | strncmp(argv[i], "--h", sizeof("--h")) == 0 || |
| 426 | strncmp(argv[i], "--list-options", sizeof("--list-options")) == 0 || |
| 427 | strncmp(argv[i], "--list-commands", sizeof("--list-commands")) == 0 || |
| 428 | strncmp(argv[i], "version", sizeof("version")) == 0 || |
| 429 | strncmp(argv[i], "view", sizeof("view")) == 0) { |
| 430 | return 1; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * Parse command line arguments. |
| 439 | * |
| 440 | * Return 0 if OK, else -1 |
| 441 | */ |
| 442 | static int parse_args(int argc, char **argv) |
| 443 | { |
| 444 | int opt, ret; |
| 445 | char *user; |
| 446 | |
| 447 | if (argc < 2) { |
| 448 | usage(stderr); |
| 449 | clean_exit(EXIT_FAILURE); |
| 450 | } |
| 451 | |
| 452 | while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) { |
| 453 | switch (opt) { |
| 454 | case 'V': |
| 455 | version(stdout); |
| 456 | ret = 0; |
| 457 | goto end; |
| 458 | case 'h': |
| 459 | usage(stdout); |
| 460 | ret = 0; |
| 461 | goto end; |
| 462 | case 'v': |
| 463 | /* There is only 3 possible level of verbosity. (-vvv) */ |
| 464 | if (lttng_opt_verbose < 3) { |
| 465 | lttng_opt_verbose += 1; |
| 466 | } |
| 467 | break; |
| 468 | case 'q': |
| 469 | lttng_opt_quiet = 1; |
| 470 | break; |
| 471 | case 'm': |
| 472 | lttng_opt_mi = mi_output_type(optarg); |
| 473 | if (lttng_opt_mi < 0) { |
| 474 | ret = lttng_opt_mi; |
| 475 | goto error; |
| 476 | } |
| 477 | break; |
| 478 | case 'g': |
| 479 | lttng_set_tracing_group(optarg); |
| 480 | break; |
| 481 | case 'n': |
| 482 | opt_no_sessiond = 1; |
| 483 | break; |
| 484 | case OPT_SESSION_PATH: |
| 485 | opt_sessiond_path = strdup(optarg); |
| 486 | break; |
| 487 | case OPT_RELAYD_PATH: |
| 488 | opt_relayd_path = strdup(optarg); |
| 489 | break; |
| 490 | case OPT_DUMP_OPTIONS: |
| 491 | list_options(stdout); |
| 492 | ret = 0; |
| 493 | goto end; |
| 494 | case OPT_DUMP_COMMANDS: |
| 495 | list_commands(commands, stdout); |
| 496 | ret = 0; |
| 497 | goto end; |
| 498 | default: |
| 499 | usage(stderr); |
| 500 | ret = 1; |
| 501 | goto error; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | /* If both options are specified, quiet wins */ |
| 506 | if (lttng_opt_verbose && lttng_opt_quiet) { |
| 507 | lttng_opt_verbose = 0; |
| 508 | } |
| 509 | |
| 510 | /* Spawn session daemon if needed */ |
| 511 | if (opt_no_sessiond == 0 && check_args_no_sessiond(argc, argv) == 0 && |
| 512 | (check_sessiond() < 0)) { |
| 513 | ret = 1; |
| 514 | goto error; |
| 515 | } |
| 516 | |
| 517 | /* No leftovers, print usage and quit */ |
| 518 | if ((argc - optind) == 0) { |
| 519 | usage(stderr); |
| 520 | ret = 1; |
| 521 | goto error; |
| 522 | } |
| 523 | |
| 524 | /* For Mathieu Desnoyers a.k.a. Dr. Tracing */ |
| 525 | user = getenv("USER"); |
| 526 | if (user != NULL && ((strncmp(progname, "drtrace", 7) == 0 || |
| 527 | strncmp("compudj", user, 7) == 0))) { |
| 528 | MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0); |
| 529 | } |
| 530 | /* Thanks Mathieu */ |
| 531 | |
| 532 | /* |
| 533 | * Handle leftovers which is a first level command with the trailing |
| 534 | * options. |
| 535 | */ |
| 536 | ret = handle_command(argc - optind, argv + optind); |
| 537 | switch (ret) { |
| 538 | case CMD_WARNING: |
| 539 | WARN("Some command(s) went wrong"); |
| 540 | break; |
| 541 | case CMD_ERROR: |
| 542 | ERR("Command error"); |
| 543 | break; |
| 544 | case CMD_UNDEFINED: |
| 545 | ERR("Undefined command"); |
| 546 | break; |
| 547 | case CMD_FATAL: |
| 548 | ERR("Fatal error"); |
| 549 | break; |
| 550 | case CMD_UNSUPPORTED: |
| 551 | ERR("Unsupported command"); |
| 552 | break; |
| 553 | case -1: |
| 554 | usage(stderr); |
| 555 | ret = 1; |
| 556 | break; |
| 557 | case 0: |
| 558 | break; |
| 559 | default: |
| 560 | if (ret < 0) { |
| 561 | ret = -ret; |
| 562 | } |
| 563 | break; |
| 564 | } |
| 565 | |
| 566 | end: |
| 567 | error: |
| 568 | return ret; |
| 569 | } |
| 570 | |
| 571 | |
| 572 | /* |
| 573 | * main |
| 574 | */ |
| 575 | int main(int argc, char *argv[]) |
| 576 | { |
| 577 | int ret; |
| 578 | |
| 579 | progname = argv[0] ? argv[0] : "lttng"; |
| 580 | |
| 581 | ret = set_signal_handler(); |
| 582 | if (ret < 0) { |
| 583 | clean_exit(ret); |
| 584 | } |
| 585 | |
| 586 | ret = parse_args(argc, argv); |
| 587 | if (ret != 0) { |
| 588 | clean_exit(ret); |
| 589 | } |
| 590 | |
| 591 | return 0; |
| 592 | } |