| 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 | #define _LGPL_SOURCE |
| 20 | #include <getopt.h> |
| 21 | #include <signal.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/wait.h> |
| 27 | #include <unistd.h> |
| 28 | #include <config.h> |
| 29 | #include <ctype.h> |
| 30 | |
| 31 | #include <lttng/lttng.h> |
| 32 | #include <common/error.h> |
| 33 | #include <common/compat/getenv.h> |
| 34 | |
| 35 | #include "command.h" |
| 36 | |
| 37 | /* Variables */ |
| 38 | static char *progname; |
| 39 | int opt_no_sessiond; |
| 40 | char *opt_sessiond_path; |
| 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 | { "track", cmd_track}, |
| 87 | { "untrack", cmd_untrack}, |
| 88 | { NULL, NULL} /* Array closure */ |
| 89 | }; |
| 90 | |
| 91 | static void usage(FILE *ofp) |
| 92 | { |
| 93 | fprintf(ofp, "LTTng Trace Control " VERSION " - " VERSION_NAME "%s\n\n", |
| 94 | GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION); |
| 95 | fprintf(ofp, "usage: lttng [OPTIONS] <COMMAND> [<ARGS>]\n"); |
| 96 | fprintf(ofp, "\n"); |
| 97 | fprintf(ofp, "Options:\n"); |
| 98 | fprintf(ofp, " -V, --version Show version\n"); |
| 99 | fprintf(ofp, " -h, --help Show this help\n"); |
| 100 | fprintf(ofp, " --list-options Simple listing of lttng options\n"); |
| 101 | fprintf(ofp, " --list-commands Simple listing of lttng commands\n"); |
| 102 | fprintf(ofp, " -v, --verbose Increase verbosity\n"); |
| 103 | fprintf(ofp, " -q, --quiet Quiet mode\n"); |
| 104 | fprintf(ofp, " -m, --mi TYPE Machine Interface mode.\n"); |
| 105 | fprintf(ofp, " Type: xml\n"); |
| 106 | fprintf(ofp, " -g, --group NAME Unix tracing group name. (default: tracing)\n"); |
| 107 | fprintf(ofp, " -n, --no-sessiond Don't spawn a session daemon\n"); |
| 108 | fprintf(ofp, " --sessiond-path PATH Session daemon full path\n"); |
| 109 | fprintf(ofp, " --relayd-path PATH Relayd daemon full path\n"); |
| 110 | fprintf(ofp, "\n"); |
| 111 | fprintf(ofp, "Commands:\n"); |
| 112 | fprintf(ofp, " add-context Add context to event and/or channel\n"); |
| 113 | fprintf(ofp, " calibrate Quantify LTTng overhead\n"); |
| 114 | fprintf(ofp, " create Create tracing session\n"); |
| 115 | fprintf(ofp, " destroy Tear down tracing session\n"); |
| 116 | fprintf(ofp, " enable-channel Enable tracing channel\n"); |
| 117 | fprintf(ofp, " enable-event Enable tracing event\n"); |
| 118 | fprintf(ofp, " disable-channel Disable tracing channel\n"); |
| 119 | fprintf(ofp, " disable-event Disable tracing event\n"); |
| 120 | fprintf(ofp, " list List possible tracing options\n"); |
| 121 | fprintf(ofp, " set-session Set current session name\n"); |
| 122 | fprintf(ofp, " snapshot Snapshot buffers of current session name\n"); |
| 123 | fprintf(ofp, " start Start tracing\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"); |
| 131 | fprintf(ofp, "\n"); |
| 132 | fprintf(ofp, "Each command also has its own -h, --help option.\n"); |
| 133 | fprintf(ofp, "\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"); |
| 136 | } |
| 137 | |
| 138 | static void version(FILE *ofp) |
| 139 | { |
| 140 | fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME "%s\n", |
| 141 | progname, |
| 142 | GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION); |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Find the MI output type enum from a string. This function is for the support |
| 147 | * of machine interface output. |
| 148 | */ |
| 149 | static int mi_output_type(const char *output_type) |
| 150 | { |
| 151 | int ret = 0; |
| 152 | |
| 153 | if (!strncasecmp("xml", output_type, 3)) { |
| 154 | ret = LTTNG_MI_XML; |
| 155 | } else { |
| 156 | /* Invalid output format */ |
| 157 | ERR("MI output format not supported"); |
| 158 | ret = -LTTNG_ERR_MI_OUTPUT_TYPE; |
| 159 | } |
| 160 | |
| 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | /* |
| 165 | * list_options |
| 166 | * |
| 167 | * List options line by line. This is mostly for bash auto completion and to |
| 168 | * avoid difficult parsing. |
| 169 | */ |
| 170 | static void list_options(FILE *ofp) |
| 171 | { |
| 172 | int i = 0; |
| 173 | struct option *option = NULL; |
| 174 | |
| 175 | option = &long_options[i]; |
| 176 | while (option->name != NULL) { |
| 177 | fprintf(ofp, "--%s\n", option->name); |
| 178 | |
| 179 | if (isprint(option->val)) { |
| 180 | fprintf(ofp, "-%c\n", option->val); |
| 181 | } |
| 182 | |
| 183 | i++; |
| 184 | option = &long_options[i]; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * clean_exit |
| 190 | */ |
| 191 | static void clean_exit(int code) |
| 192 | { |
| 193 | DBG("Clean exit"); |
| 194 | exit(code); |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * sighandler |
| 199 | * |
| 200 | * Signal handler for the daemon |
| 201 | */ |
| 202 | static void sighandler(int sig) |
| 203 | { |
| 204 | switch (sig) { |
| 205 | case SIGTERM: |
| 206 | DBG("SIGTERM caught"); |
| 207 | clean_exit(EXIT_FAILURE); |
| 208 | break; |
| 209 | default: |
| 210 | DBG("Unknown signal %d caught", sig); |
| 211 | break; |
| 212 | } |
| 213 | |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * set_signal_handler |
| 219 | * |
| 220 | * Setup signal handler for SIGCHLD and SIGTERM. |
| 221 | */ |
| 222 | static int set_signal_handler(void) |
| 223 | { |
| 224 | int ret = 0; |
| 225 | struct sigaction sa; |
| 226 | sigset_t sigset; |
| 227 | |
| 228 | if ((ret = sigemptyset(&sigset)) < 0) { |
| 229 | PERROR("sigemptyset"); |
| 230 | goto end; |
| 231 | } |
| 232 | |
| 233 | sa.sa_handler = sighandler; |
| 234 | sa.sa_mask = sigset; |
| 235 | sa.sa_flags = 0; |
| 236 | |
| 237 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
| 238 | PERROR("sigaction"); |
| 239 | goto end; |
| 240 | } |
| 241 | |
| 242 | end: |
| 243 | return ret; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * handle_command |
| 248 | * |
| 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. |
| 251 | * |
| 252 | * If command not found, return -1 |
| 253 | * else, return function command error code. |
| 254 | */ |
| 255 | static int handle_command(int argc, char **argv) |
| 256 | { |
| 257 | int i = 0, ret; |
| 258 | struct cmd_struct *cmd; |
| 259 | |
| 260 | if (*argv == NULL) { |
| 261 | ret = CMD_SUCCESS; |
| 262 | goto end; |
| 263 | } |
| 264 | |
| 265 | cmd = &commands[i]; |
| 266 | while (cmd->func != NULL) { |
| 267 | /* Find command */ |
| 268 | if (strcmp(argv[0], cmd->name) == 0) { |
| 269 | ret = cmd->func(argc, (const char**) argv); |
| 270 | goto end; |
| 271 | } |
| 272 | i++; |
| 273 | cmd = &commands[i]; |
| 274 | } |
| 275 | |
| 276 | /* Command not found */ |
| 277 | ret = CMD_UNDEFINED; |
| 278 | |
| 279 | end: |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Parse command line arguments. |
| 285 | * |
| 286 | * Return 0 if OK, else -1 |
| 287 | */ |
| 288 | static int parse_args(int argc, char **argv) |
| 289 | { |
| 290 | int opt, ret; |
| 291 | char *user; |
| 292 | |
| 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); |
| 296 | } |
| 297 | |
| 298 | if (argc < 2) { |
| 299 | usage(stderr); |
| 300 | clean_exit(EXIT_FAILURE); |
| 301 | } |
| 302 | |
| 303 | while ((opt = getopt_long(argc, argv, "+Vhnvqg:m:", long_options, NULL)) != -1) { |
| 304 | switch (opt) { |
| 305 | case 'V': |
| 306 | version(stdout); |
| 307 | ret = 0; |
| 308 | goto end; |
| 309 | case 'h': |
| 310 | usage(stdout); |
| 311 | ret = 0; |
| 312 | goto end; |
| 313 | case 'v': |
| 314 | /* There is only 3 possible level of verbosity. (-vvv) */ |
| 315 | if (lttng_opt_verbose < 3) { |
| 316 | lttng_opt_verbose += 1; |
| 317 | } |
| 318 | break; |
| 319 | case 'q': |
| 320 | lttng_opt_quiet = 1; |
| 321 | break; |
| 322 | case 'm': |
| 323 | lttng_opt_mi = mi_output_type(optarg); |
| 324 | if (lttng_opt_mi < 0) { |
| 325 | ret = lttng_opt_mi; |
| 326 | goto error; |
| 327 | } |
| 328 | break; |
| 329 | case 'g': |
| 330 | lttng_set_tracing_group(optarg); |
| 331 | break; |
| 332 | case 'n': |
| 333 | opt_no_sessiond = 1; |
| 334 | break; |
| 335 | case OPT_SESSION_PATH: |
| 336 | free(opt_sessiond_path); |
| 337 | opt_sessiond_path = strdup(optarg); |
| 338 | if (!opt_sessiond_path) { |
| 339 | ret = -1; |
| 340 | goto error; |
| 341 | } |
| 342 | break; |
| 343 | case OPT_RELAYD_PATH: |
| 344 | free(opt_relayd_path); |
| 345 | opt_relayd_path = strdup(optarg); |
| 346 | if (!opt_relayd_path) { |
| 347 | ret = -1; |
| 348 | goto error; |
| 349 | } |
| 350 | break; |
| 351 | case OPT_DUMP_OPTIONS: |
| 352 | list_options(stdout); |
| 353 | ret = 0; |
| 354 | goto end; |
| 355 | case OPT_DUMP_COMMANDS: |
| 356 | list_commands(commands, stdout); |
| 357 | ret = 0; |
| 358 | goto end; |
| 359 | default: |
| 360 | usage(stderr); |
| 361 | ret = 1; |
| 362 | goto error; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | /* If both options are specified, quiet wins */ |
| 367 | if (lttng_opt_verbose && lttng_opt_quiet) { |
| 368 | lttng_opt_verbose = 0; |
| 369 | } |
| 370 | |
| 371 | /* No leftovers, print usage and quit */ |
| 372 | if ((argc - optind) == 0) { |
| 373 | usage(stderr); |
| 374 | ret = 1; |
| 375 | goto error; |
| 376 | } |
| 377 | |
| 378 | /* For Mathieu Desnoyers a.k.a. Dr. Tracing */ |
| 379 | user = getenv("USER"); |
| 380 | if (user != NULL && ((strncmp(progname, "drtrace", 7) == 0 || |
| 381 | strncmp("compudj", user, 7) == 0))) { |
| 382 | MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n", 27,1,33,27,0); |
| 383 | } |
| 384 | /* Thanks Mathieu */ |
| 385 | |
| 386 | /* |
| 387 | * Handle leftovers which is a first level command with the trailing |
| 388 | * options. |
| 389 | */ |
| 390 | ret = handle_command(argc - optind, argv + optind); |
| 391 | switch (ret) { |
| 392 | case CMD_WARNING: |
| 393 | WARN("Some command(s) went wrong"); |
| 394 | break; |
| 395 | case CMD_ERROR: |
| 396 | ERR("Command error"); |
| 397 | break; |
| 398 | case CMD_UNDEFINED: |
| 399 | ERR("Undefined command"); |
| 400 | break; |
| 401 | case CMD_FATAL: |
| 402 | ERR("Fatal error"); |
| 403 | break; |
| 404 | case CMD_UNSUPPORTED: |
| 405 | ERR("Unsupported command"); |
| 406 | break; |
| 407 | case -1: |
| 408 | usage(stderr); |
| 409 | ret = 1; |
| 410 | break; |
| 411 | case 0: |
| 412 | break; |
| 413 | default: |
| 414 | if (ret < 0) { |
| 415 | ret = -ret; |
| 416 | } |
| 417 | break; |
| 418 | } |
| 419 | |
| 420 | end: |
| 421 | error: |
| 422 | return ret; |
| 423 | } |
| 424 | |
| 425 | |
| 426 | /* |
| 427 | * main |
| 428 | */ |
| 429 | int main(int argc, char *argv[]) |
| 430 | { |
| 431 | int ret; |
| 432 | |
| 433 | progname = argv[0] ? argv[0] : "lttng"; |
| 434 | |
| 435 | ret = set_signal_handler(); |
| 436 | if (ret < 0) { |
| 437 | clean_exit(ret); |
| 438 | } |
| 439 | |
| 440 | ret = parse_args(argc, argv); |
| 441 | if (ret != 0) { |
| 442 | clean_exit(ret); |
| 443 | } |
| 444 | |
| 445 | return 0; |
| 446 | } |