| 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 as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 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. |
| 17 | */ |
| 18 | |
| 19 | #define _GNU_SOURCE |
| 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <getopt.h> |
| 23 | #include <grp.h> |
| 24 | #include <limits.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/wait.h> |
| 31 | #include <unistd.h> |
| 32 | |
| 33 | #include <lttng/liblttngctl.h> |
| 34 | |
| 35 | #include "lttng.h" |
| 36 | #include "lttngerr.h" |
| 37 | |
| 38 | /* Variables */ |
| 39 | static char *progname; |
| 40 | |
| 41 | /* Prototypes */ |
| 42 | static int process_client_opt(void); |
| 43 | static int process_opt_list_apps(void); |
| 44 | static int process_opt_list_sessions(void); |
| 45 | static int process_opt_list_traces(void); |
| 46 | static int process_opt_create_session(void); |
| 47 | static void sighandler(int sig); |
| 48 | static int set_signal_handler(void); |
| 49 | static int validate_options(void); |
| 50 | static char *get_cmdline_by_pid(pid_t pid); |
| 51 | |
| 52 | /* |
| 53 | * start_client |
| 54 | * |
| 55 | * Process client request from the command line |
| 56 | * options. Every tracing action is done by the |
| 57 | * liblttngctl API. |
| 58 | */ |
| 59 | static int process_client_opt(void) |
| 60 | { |
| 61 | int ret; |
| 62 | uuid_t uuid; |
| 63 | |
| 64 | /* Connect to the session daemon */ |
| 65 | ret = lttng_connect_sessiond(); |
| 66 | if (ret < 0) { |
| 67 | goto end; |
| 68 | } |
| 69 | |
| 70 | if (opt_list_apps) { |
| 71 | ret = process_opt_list_apps(); |
| 72 | if (ret < 0) { |
| 73 | goto end; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if (opt_list_session) { |
| 78 | ret = process_opt_list_sessions(); |
| 79 | if (ret < 0) { |
| 80 | goto end; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if (opt_list_traces) { |
| 85 | ret = process_opt_list_traces(); |
| 86 | if (ret < 0) { |
| 87 | goto end; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if (opt_create_session != NULL) { |
| 92 | ret = process_opt_create_session(); |
| 93 | if (ret < 0) { |
| 94 | goto end; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | if (opt_destroy_session != NULL) { |
| 99 | uuid_parse(opt_destroy_session, uuid); |
| 100 | ret = lttng_destroy_session(&uuid); |
| 101 | if (ret < 0) { |
| 102 | goto end; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (opt_session_uuid != NULL) { |
| 107 | DBG("Set session uuid to %s", opt_session_uuid); |
| 108 | lttng_set_current_session_uuid(opt_session_uuid); |
| 109 | } |
| 110 | |
| 111 | if (opt_create_trace) { |
| 112 | DBG("Create trace for pid %d", opt_create_trace); |
| 113 | ret = lttng_ust_create_trace(opt_create_trace); |
| 114 | if (ret < 0) { |
| 115 | goto end; |
| 116 | } |
| 117 | MSG("Trace created successfully!\nUse --start PID to start tracing."); |
| 118 | } |
| 119 | |
| 120 | if (opt_start_trace) { |
| 121 | DBG("Start trace for pid %d", opt_start_trace); |
| 122 | ret = lttng_ust_start_trace(opt_start_trace); |
| 123 | if (ret < 0) { |
| 124 | goto end; |
| 125 | } |
| 126 | MSG("Trace started successfully!"); |
| 127 | } |
| 128 | |
| 129 | return 0; |
| 130 | |
| 131 | end: |
| 132 | ERR("%s", lttng_get_readable_code(ret)); |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * process_opt_list_traces |
| 138 | * |
| 139 | * Get list of all traces for a specific session uuid. |
| 140 | */ |
| 141 | static int process_opt_list_traces(void) |
| 142 | { |
| 143 | int ret, i; |
| 144 | uuid_t uuid; |
| 145 | struct lttng_trace *traces; |
| 146 | |
| 147 | uuid_parse(opt_session_uuid, uuid); |
| 148 | ret = lttng_list_traces(&uuid, &traces); |
| 149 | if (ret < 0) { |
| 150 | goto error; |
| 151 | } |
| 152 | |
| 153 | MSG("Userspace traces:"); |
| 154 | for (i = 0; i < ret; i++) { |
| 155 | if (traces[i].type == USERSPACE) { |
| 156 | MSG("\t%d) %s (pid: %d): %s", |
| 157 | i, traces[i].name, traces[i].pid, |
| 158 | get_cmdline_by_pid(traces[i].pid)); |
| 159 | } else { |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | MSG("Kernel traces:"); |
| 165 | for (;i < ret; i++) { |
| 166 | if (traces[i].type == KERNEL) { |
| 167 | MSG("\t%d) %s", i, traces[i].name); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | free(traces); |
| 172 | |
| 173 | error: |
| 174 | return ret; |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * process_opt_create_session |
| 179 | * |
| 180 | * Create a new session using the name pass |
| 181 | * to the command line. |
| 182 | */ |
| 183 | static int process_opt_create_session(void) |
| 184 | { |
| 185 | int ret; |
| 186 | uuid_t session_id; |
| 187 | char str_uuid[37]; |
| 188 | |
| 189 | ret = lttng_create_session(opt_create_session, &session_id); |
| 190 | if (ret < 0) { |
| 191 | goto error; |
| 192 | } |
| 193 | |
| 194 | uuid_unparse(session_id, str_uuid); |
| 195 | |
| 196 | MSG("Session created:"); |
| 197 | MSG(" %s (%s)", opt_create_session, str_uuid); |
| 198 | |
| 199 | error: |
| 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * process_opt_list_sessions |
| 205 | * |
| 206 | * Get the list of available sessions from |
| 207 | * the session daemon and print it to user. |
| 208 | */ |
| 209 | static int process_opt_list_sessions(void) |
| 210 | { |
| 211 | int ret, count, i; |
| 212 | struct lttng_session *sess; |
| 213 | |
| 214 | count = lttng_list_sessions(&sess); |
| 215 | if (count < 0) { |
| 216 | ret = count; |
| 217 | goto error; |
| 218 | } |
| 219 | |
| 220 | MSG("Available sessions [Name (uuid)]:"); |
| 221 | for (i = 0; i < count; i++) { |
| 222 | MSG("\tName: %s (uuid: %s)", sess[i].name, sess[i].uuid); |
| 223 | } |
| 224 | |
| 225 | free(sess); |
| 226 | MSG("\nTo select a session, use --session UUID."); |
| 227 | |
| 228 | return 0; |
| 229 | |
| 230 | error: |
| 231 | return ret; |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * process_opt_list_apps |
| 236 | * |
| 237 | * Get the UST traceable pid list and print |
| 238 | * them to the user. |
| 239 | */ |
| 240 | static int process_opt_list_apps(void) |
| 241 | { |
| 242 | int i, ret, count; |
| 243 | pid_t *pids; |
| 244 | char *cmdline; |
| 245 | |
| 246 | count = lttng_ust_list_apps(&pids); |
| 247 | if (count < 0) { |
| 248 | ret = count; |
| 249 | goto error; |
| 250 | } |
| 251 | |
| 252 | MSG("LTTng UST traceable application [name (pid)]:"); |
| 253 | for (i=0; i < count; i++) { |
| 254 | cmdline = get_cmdline_by_pid(pids[i]); |
| 255 | if (cmdline == NULL) { |
| 256 | MSG("\t(not running) (%d)", pids[i]); |
| 257 | continue; |
| 258 | } |
| 259 | MSG("\t%s (%d)", cmdline, pids[i]); |
| 260 | free(cmdline); |
| 261 | } |
| 262 | |
| 263 | /* Allocated by lttng_ust_list_apps() */ |
| 264 | free(pids); |
| 265 | |
| 266 | return 0; |
| 267 | |
| 268 | error: |
| 269 | return ret; |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * get_cmdline_by_pid |
| 274 | * |
| 275 | * Get command line from /proc for a specific pid. |
| 276 | * |
| 277 | * On success, return an allocated string pointer pointing to |
| 278 | * the proc cmdline. |
| 279 | * On error, return NULL. |
| 280 | */ |
| 281 | static char *get_cmdline_by_pid(pid_t pid) |
| 282 | { |
| 283 | int ret; |
| 284 | FILE *fp; |
| 285 | char *cmdline = NULL; |
| 286 | char path[24]; /* Can't go bigger than /proc/65535/cmdline */ |
| 287 | |
| 288 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); |
| 289 | fp = fopen(path, "r"); |
| 290 | if (fp == NULL) { |
| 291 | goto end; |
| 292 | } |
| 293 | |
| 294 | /* Caller must free() *cmdline */ |
| 295 | cmdline = malloc(PATH_MAX); |
| 296 | ret = fread(cmdline, 1, PATH_MAX, fp); |
| 297 | fclose(fp); |
| 298 | |
| 299 | end: |
| 300 | return cmdline; |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * validate_options |
| 305 | * |
| 306 | * Make sure that all options passed to the command line |
| 307 | * are compatible with each others. |
| 308 | * |
| 309 | * On error, return -1 |
| 310 | * On success, return 0 |
| 311 | */ |
| 312 | static int validate_options(void) |
| 313 | { |
| 314 | if ((opt_session_uuid == NULL) && |
| 315 | (opt_create_trace || opt_start_trace || opt_list_traces)) { |
| 316 | ERR("You need to specify a session UUID.\nPlease use --session UUID to do so."); |
| 317 | goto error; |
| 318 | } |
| 319 | |
| 320 | return 0; |
| 321 | |
| 322 | error: |
| 323 | return -1; |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * spawn_sessiond |
| 328 | * |
| 329 | * Spawn a session daemon by forking and execv. |
| 330 | */ |
| 331 | static int spawn_sessiond(char *pathname) |
| 332 | { |
| 333 | int ret = 0; |
| 334 | pid_t pid; |
| 335 | |
| 336 | MSG("Spawning session daemon"); |
| 337 | pid = fork(); |
| 338 | if (pid == 0) { |
| 339 | /* Spawn session daemon and tell |
| 340 | * it to signal us when ready. |
| 341 | */ |
| 342 | ret = execlp(pathname, "ltt-sessiond", "--sig-parent", "--quiet", NULL); |
| 343 | if (ret < 0) { |
| 344 | if (errno == ENOENT) { |
| 345 | ERR("No session daemon found. Use --sessiond-path."); |
| 346 | } else { |
| 347 | perror("execlp"); |
| 348 | } |
| 349 | kill(getppid(), SIGTERM); |
| 350 | exit(EXIT_FAILURE); |
| 351 | } |
| 352 | exit(EXIT_SUCCESS); |
| 353 | } else if (pid > 0) { |
| 354 | /* Wait for ltt-sessiond to start */ |
| 355 | pause(); |
| 356 | goto end; |
| 357 | } else { |
| 358 | perror("fork"); |
| 359 | ret = -1; |
| 360 | goto end; |
| 361 | } |
| 362 | |
| 363 | end: |
| 364 | return ret; |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * check_ltt_sessiond |
| 369 | * |
| 370 | * Check if the session daemon is available using |
| 371 | * the liblttngctl API for the check. If not, try to |
| 372 | * spawn a daemon. |
| 373 | */ |
| 374 | static int check_ltt_sessiond(void) |
| 375 | { |
| 376 | int ret; |
| 377 | char *pathname = NULL; |
| 378 | |
| 379 | ret = lttng_check_session_daemon(); |
| 380 | if (ret < 0) { |
| 381 | /* Try command line option path */ |
| 382 | if (opt_sessiond_path != NULL) { |
| 383 | ret = access(opt_sessiond_path, F_OK | X_OK); |
| 384 | if (ret < 0) { |
| 385 | ERR("No such file: %s", opt_sessiond_path); |
| 386 | goto end; |
| 387 | } |
| 388 | pathname = opt_sessiond_path; |
| 389 | } else { |
| 390 | /* Try LTTNG_SESSIOND_PATH env variable */ |
| 391 | pathname = getenv(LTTNG_SESSIOND_PATH_ENV); |
| 392 | if (pathname != NULL) { |
| 393 | /* strdup here in order to make the free() |
| 394 | * not fail later on. |
| 395 | */ |
| 396 | pathname = strdup(pathname); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /* Let's rock and roll */ |
| 401 | if (pathname == NULL) { |
| 402 | ret = asprintf(&pathname, "ltt-sessiond"); |
| 403 | if (ret < 0) { |
| 404 | goto end; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | ret = spawn_sessiond(pathname); |
| 409 | free(pathname); |
| 410 | if (ret < 0) { |
| 411 | ERR("Problem occurs when starting %s", pathname); |
| 412 | goto end; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | end: |
| 417 | return ret; |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * set_signal_handler |
| 422 | * |
| 423 | * Setup signal handler for SIGCHLD and SIGTERM. |
| 424 | */ |
| 425 | static int set_signal_handler(void) |
| 426 | { |
| 427 | int ret = 0; |
| 428 | struct sigaction sa; |
| 429 | sigset_t sigset; |
| 430 | |
| 431 | if ((ret = sigemptyset(&sigset)) < 0) { |
| 432 | perror("sigemptyset"); |
| 433 | goto end; |
| 434 | } |
| 435 | |
| 436 | sa.sa_handler = sighandler; |
| 437 | sa.sa_mask = sigset; |
| 438 | sa.sa_flags = 0; |
| 439 | if ((ret = sigaction(SIGCHLD, &sa, NULL)) < 0) { |
| 440 | perror("sigaction"); |
| 441 | goto end; |
| 442 | } |
| 443 | |
| 444 | if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { |
| 445 | perror("sigaction"); |
| 446 | goto end; |
| 447 | } |
| 448 | |
| 449 | end: |
| 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | /* |
| 454 | * sighandler |
| 455 | * |
| 456 | * Signal handler for the daemon |
| 457 | */ |
| 458 | static void sighandler(int sig) |
| 459 | { |
| 460 | switch (sig) { |
| 461 | case SIGTERM: |
| 462 | DBG("SIGTERM catched"); |
| 463 | clean_exit(EXIT_FAILURE); |
| 464 | break; |
| 465 | case SIGCHLD: |
| 466 | /* Notify is done */ |
| 467 | DBG("SIGCHLD catched"); |
| 468 | break; |
| 469 | default: |
| 470 | DBG("Unknown signal %d catched", sig); |
| 471 | break; |
| 472 | } |
| 473 | |
| 474 | return; |
| 475 | } |
| 476 | /* |
| 477 | * clean_exit |
| 478 | */ |
| 479 | void clean_exit(int code) |
| 480 | { |
| 481 | DBG("Clean exit"); |
| 482 | if (lttng_disconnect_sessiond() < 0) { |
| 483 | ERR("Session daemon disconnect failed."); |
| 484 | } |
| 485 | exit(code); |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * main |
| 490 | */ |
| 491 | int main(int argc, char *argv[]) |
| 492 | { |
| 493 | int ret; |
| 494 | |
| 495 | progname = argv[0] ? argv[0] : "lttng"; |
| 496 | |
| 497 | /* For Mathieu Desnoyers aka Dr Tracing */ |
| 498 | if (strncmp(progname, "drtrace", 7) == 0) { |
| 499 | MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0); |
| 500 | } |
| 501 | |
| 502 | ret = parse_args(argc, (const char **) argv); |
| 503 | if (ret < 0) { |
| 504 | clean_exit(EXIT_FAILURE); |
| 505 | } |
| 506 | |
| 507 | ret = validate_options(); |
| 508 | if (ret < 0) { |
| 509 | return EXIT_FAILURE; |
| 510 | } |
| 511 | |
| 512 | ret = set_signal_handler(); |
| 513 | if (ret < 0) { |
| 514 | clean_exit(ret); |
| 515 | } |
| 516 | |
| 517 | if (opt_tracing_group != NULL) { |
| 518 | DBG("Set tracing group to '%s'", opt_tracing_group); |
| 519 | lttng_set_tracing_group(opt_tracing_group); |
| 520 | } |
| 521 | |
| 522 | /* If ask for kernel tracing, need root perms */ |
| 523 | if (opt_trace_kernel) { |
| 524 | DBG("Kernel tracing activated"); |
| 525 | if (getuid() != 0) { |
| 526 | ERR("%s must be setuid root", progname); |
| 527 | clean_exit(-EPERM); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | /* Check if the lttng session daemon is running. |
| 532 | * If no, a daemon will be spawned. |
| 533 | */ |
| 534 | if (opt_no_sessiond == 0 && (check_ltt_sessiond() < 0)) { |
| 535 | clean_exit(EXIT_FAILURE); |
| 536 | } |
| 537 | |
| 538 | ret = process_client_opt(); |
| 539 | if (ret < 0) { |
| 540 | clean_exit(ret); |
| 541 | } |
| 542 | |
| 543 | clean_exit(0); |
| 544 | |
| 545 | return 0; |
| 546 | } |