| 1 | /* |
| 2 | * Copyright (c) 2015-2021 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 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 _LGPL_SOURCE |
| 19 | #include <limits.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <unistd.h> |
| 26 | #include <signal.h> |
| 27 | #include <sys/ptrace.h> |
| 28 | #include <sys/wait.h> |
| 29 | #include <sys/signal.h> |
| 30 | #include <stdbool.h> |
| 31 | #include <errno.h> |
| 32 | #include <time.h> |
| 33 | |
| 34 | #include <lttng/lttng.h> |
| 35 | #include <lttng/handle.h> |
| 36 | #include <lttng/session.h> |
| 37 | #include <lttng/tracker.h> |
| 38 | |
| 39 | #define MESSAGE_PREFIX "[lttng-ptrace] " |
| 40 | #define NR_HANDLES 2 |
| 41 | |
| 42 | #ifndef PTRACE_EVENT_STOP |
| 43 | #define PTRACE_EVENT_STOP 128 |
| 44 | #endif |
| 45 | |
| 46 | #define PERROR(msg) perror(msg "\n") |
| 47 | #define ERR(fmt, args...) fprintf(stderr, fmt "\n", ## args) |
| 48 | |
| 49 | #ifdef DEBUG |
| 50 | #define DBG(fmt, args...) printf(fmt "\n", ## args) |
| 51 | #else |
| 52 | #define DBG(fmt, args...) |
| 53 | #endif |
| 54 | |
| 55 | #define __unused __attribute__((unused)) |
| 56 | |
| 57 | static pid_t sigfwd_pid; |
| 58 | |
| 59 | static bool opt_help = false, |
| 60 | opt_no_context = false, |
| 61 | opt_no_pause = false, |
| 62 | opt_no_syscall = false, |
| 63 | opt_session = false, |
| 64 | opt_view = false, |
| 65 | opt_output = false; |
| 66 | |
| 67 | static const char *output_path; |
| 68 | static const char *session_name; |
| 69 | |
| 70 | struct lttng_trace_ctx { |
| 71 | char session_name[LTTNG_NAME_MAX]; |
| 72 | char path[PATH_MAX]; |
| 73 | time_t creation_time; |
| 74 | }; |
| 75 | |
| 76 | static |
| 77 | long ptrace_setup(pid_t pid) |
| 78 | { |
| 79 | long ptrace_ret; |
| 80 | unsigned long flags; |
| 81 | |
| 82 | flags = PTRACE_O_TRACECLONE | PTRACE_O_TRACEEXIT |
| 83 | | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK |
| 84 | | PTRACE_O_TRACEEXEC; |
| 85 | //ptrace_ret = ptrace(PTRACE_SETOPTIONS, pid, |
| 86 | ptrace_ret = ptrace(PTRACE_SEIZE, pid, |
| 87 | NULL, (void *) flags); |
| 88 | if (ptrace_ret) { |
| 89 | //PERROR("ptrace setoptions"); |
| 90 | PERROR("ptrace seize"); |
| 91 | return -1; |
| 92 | } |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static |
| 97 | int wait_on_children(pid_t top_pid, struct lttng_handle **handle, |
| 98 | size_t nr_handles) |
| 99 | { |
| 100 | pid_t pid; |
| 101 | long ptrace_ret; |
| 102 | int ret; |
| 103 | size_t i; |
| 104 | |
| 105 | pid = top_pid; |
| 106 | DBG("Setup ptrace options on top child pid %d", pid); |
| 107 | ret = ptrace_setup(pid); |
| 108 | if (ret) { |
| 109 | return ret; |
| 110 | } |
| 111 | for (i = 0; i < nr_handles; i++) { |
| 112 | ret = lttng_track_pid(handle[i], pid); |
| 113 | if (ret && ret != -LTTNG_ERR_INVALID) { |
| 114 | ERR("Error %d tracking pid %d", ret, pid); |
| 115 | } |
| 116 | } |
| 117 | top_pid = -1; |
| 118 | /* Restart initial raise(SIGSTOP) */ |
| 119 | //ptrace_ret = ptrace(PTRACE_CONT, pid, 0, restartsig); |
| 120 | //TODO wait for child to have stopped.... |
| 121 | ret = kill(pid, SIGCONT); |
| 122 | if (ret) { |
| 123 | //if (ptrace_ret) { |
| 124 | PERROR("kill"); |
| 125 | abort(); |
| 126 | } |
| 127 | |
| 128 | for (;;) { |
| 129 | int status; |
| 130 | |
| 131 | pid = waitpid(-1, &status, __WALL); |
| 132 | DBG("Activity on child pid %d", pid); |
| 133 | if (pid < 0) { |
| 134 | if (errno == ECHILD) { |
| 135 | /* No more children to possibly wait for. */ |
| 136 | return 0; |
| 137 | } else { |
| 138 | PERROR("waitpid"); |
| 139 | return -1; |
| 140 | } |
| 141 | } else if (pid == 0) { |
| 142 | ERR("Unexpected PID 0"); |
| 143 | abort(); |
| 144 | } else { |
| 145 | if (WIFSTOPPED(status)) { |
| 146 | int shiftstatus, restartsig; |
| 147 | |
| 148 | DBG("Child pid %d is stopped", pid); |
| 149 | shiftstatus = status >> 8; |
| 150 | if (shiftstatus == (SIGTRAP | (PTRACE_EVENT_EXIT << 8))) { |
| 151 | DBG("Child pid %d is exiting", pid); |
| 152 | #if 0 |
| 153 | for (i = 0; i < nr_handles; i++) { |
| 154 | ret = lttng_untrack_pid(handle[i], pid); |
| 155 | if (ret && ret != -LTTNG_ERR_INVALID) { |
| 156 | ERR("Error %d untracking pid %d", ret, pid); |
| 157 | } |
| 158 | } |
| 159 | #endif |
| 160 | } else if (shiftstatus == (SIGTRAP | (PTRACE_EVENT_FORK << 8))) { |
| 161 | long newpid; |
| 162 | |
| 163 | ptrace_ret = ptrace(PTRACE_GETEVENTMSG, pid, 0, &newpid); |
| 164 | if (ptrace_ret) { |
| 165 | PERROR("ptrace"); |
| 166 | abort(); |
| 167 | } |
| 168 | DBG("Child pid %d is forking, child pid %ld", pid, newpid); |
| 169 | for (i = 0; i < nr_handles; i++) { |
| 170 | ret = lttng_track_pid(handle[i], newpid); |
| 171 | if (ret && ret != -LTTNG_ERR_INVALID) { |
| 172 | ERR("Error %d tracking pid %ld", ret, newpid); |
| 173 | } |
| 174 | } |
| 175 | } else if (shiftstatus == (SIGTRAP | (PTRACE_EVENT_VFORK << 8))) { |
| 176 | long newpid; |
| 177 | |
| 178 | ptrace_ret = ptrace(PTRACE_GETEVENTMSG, pid, 0, &newpid); |
| 179 | if (ptrace_ret) { |
| 180 | PERROR("ptrace"); |
| 181 | abort(); |
| 182 | } |
| 183 | DBG("Child pid %d issuing vfork, child pid %ld", pid, newpid); |
| 184 | for (i = 0; i < nr_handles; i++) { |
| 185 | ret = lttng_track_pid(handle[i], newpid); |
| 186 | if (ret && ret != -LTTNG_ERR_INVALID) { |
| 187 | ERR("Error %d tracking pid %ld", ret, newpid); |
| 188 | } |
| 189 | } |
| 190 | } else if (shiftstatus == (SIGTRAP | PTRACE_EVENT_CLONE << 8)) { |
| 191 | long newpid; |
| 192 | |
| 193 | ptrace_ret = ptrace(PTRACE_GETEVENTMSG, pid, 0, &newpid); |
| 194 | if (ptrace_ret) { |
| 195 | PERROR("ptrace"); |
| 196 | abort(); |
| 197 | } |
| 198 | DBG("Child pid %d issuing clone, child pid %ld", pid, newpid); |
| 199 | for (i = 0; i < nr_handles; i++) { |
| 200 | ret = lttng_track_pid(handle[i], newpid); |
| 201 | if (ret && ret != -LTTNG_ERR_INVALID) { |
| 202 | ERR("Error %d tracking pid %ld", ret, newpid); |
| 203 | } |
| 204 | } |
| 205 | } else if (shiftstatus == (SIGTRAP | PTRACE_EVENT_EXEC << 8)) { |
| 206 | long oldpid; |
| 207 | |
| 208 | ptrace_ret = ptrace(PTRACE_GETEVENTMSG, pid, 0, &oldpid); |
| 209 | if (ptrace_ret) { |
| 210 | PERROR("ptrace"); |
| 211 | abort(); |
| 212 | } |
| 213 | DBG("Child pid (old: %ld, new: %d) is issuing exec", |
| 214 | oldpid, pid); |
| 215 | /* |
| 216 | * Needed for exec issued from |
| 217 | * multithreaded process. |
| 218 | */ |
| 219 | for (i = 0; i < nr_handles; i++) { |
| 220 | ret = lttng_untrack_pid(handle[i], oldpid); |
| 221 | if (ret && ret != -LTTNG_ERR_INVALID) { |
| 222 | ERR("Error %d untracking pid %ld", ret, oldpid); |
| 223 | } |
| 224 | ret = lttng_track_pid(handle[i], pid); |
| 225 | if (ret && ret != -LTTNG_ERR_INVALID) { |
| 226 | ERR("Error %d tracking pid %d", ret, pid); |
| 227 | } |
| 228 | } |
| 229 | } else if (shiftstatus == SIGTRAP) { |
| 230 | DBG("Received SIGTRAP from pid %d without event of interest", pid); |
| 231 | } else if (shiftstatus == SIGSTOP) { |
| 232 | DBG("Received SIGSTOP from pid %d without event of interest", pid); |
| 233 | } else if (shiftstatus == SIGSEGV) { |
| 234 | DBG("Received SIGSEGV from pid %d without event of interest", pid); |
| 235 | } else if (shiftstatus == SIGTTIN) { |
| 236 | DBG("Received SIGTTIN from pid %d without event of interest", pid); |
| 237 | } else if (shiftstatus == SIGTTOU) { |
| 238 | DBG("Received SIGTTOU from pid %d without event of interest", pid); |
| 239 | } else if (shiftstatus == SIGTSTP) { |
| 240 | DBG("Received SIGTSTP from pid %d without event of interest", pid); |
| 241 | } else { |
| 242 | DBG("Ignoring signal %d (status %d) from pid %d (eventcode = %u)", |
| 243 | WSTOPSIG(status), status, pid, |
| 244 | (shiftstatus & ~WSTOPSIG(status)) >> 8); |
| 245 | } |
| 246 | |
| 247 | restartsig = WSTOPSIG(status); |
| 248 | switch (restartsig) { |
| 249 | case SIGTSTP: |
| 250 | case SIGTTIN: |
| 251 | case SIGTTOU: |
| 252 | case SIGSTOP: |
| 253 | { |
| 254 | //siginfo_t siginfo; |
| 255 | |
| 256 | errno = 0; |
| 257 | //ptrace_ret = ptrace(PTRACE_GETSIGINFO, pid, NULL, &siginfo); |
| 258 | //if (ptrace_ret < 0 && errno == EINVAL) { |
| 259 | if (restartsig == SIGTTIN) { |
| 260 | ret = kill(pid, SIGTTIN); |
| 261 | if (ret) { |
| 262 | PERROR("kill"); |
| 263 | abort(); |
| 264 | } |
| 265 | } else if (status >> 16 == PTRACE_EVENT_STOP) { |
| 266 | DBG("ptrace stop"); |
| 267 | //ptrace_ret = ptrace(PTRACE_LISTEN, pid, 0, 0); |
| 268 | ptrace_ret = ptrace(PTRACE_CONT, pid, 0, 0); |
| 269 | if (ptrace_ret) { |
| 270 | PERROR("ptrace cont"); |
| 271 | abort(); |
| 272 | } |
| 273 | } else { |
| 274 | DBG("job control stop ret %ld errno %d", ptrace_ret, errno); |
| 275 | /* |
| 276 | * It's not a group-stop, so restart process, |
| 277 | * skipping the signal. |
| 278 | */ |
| 279 | ptrace_ret = ptrace(PTRACE_CONT, pid, 0, 0); |
| 280 | if (ptrace_ret) { |
| 281 | PERROR("ptrace cont"); |
| 282 | abort(); |
| 283 | } |
| 284 | } |
| 285 | break; |
| 286 | } |
| 287 | case SIGTRAP: |
| 288 | { |
| 289 | //unsigned long data; |
| 290 | |
| 291 | //if (ptrace(PTRACE_GETEVENTMSG, pid, NULL, &data) == 0) { |
| 292 | /* |
| 293 | * Restart process skipping the signal when |
| 294 | * receiving a message. |
| 295 | */ |
| 296 | ptrace_ret = ptrace(PTRACE_CONT, pid, 0, 0); |
| 297 | if (ptrace_ret) { |
| 298 | PERROR("ptrace"); |
| 299 | abort(); |
| 300 | } |
| 301 | break; |
| 302 | //} |
| 303 | } |
| 304 | /* Fall-through */ |
| 305 | default: |
| 306 | /* Restart with original signal. */ |
| 307 | ptrace_ret = ptrace(PTRACE_CONT, pid, 0, restartsig); |
| 308 | if (ptrace_ret) { |
| 309 | PERROR("ptrace"); |
| 310 | abort(); |
| 311 | } |
| 312 | } |
| 313 | } else if (WIFEXITED(status)) { |
| 314 | DBG("Child pid %d exited normally with status %d", |
| 315 | pid, WEXITSTATUS(status)); |
| 316 | for (i = 0; i < nr_handles; i++) { |
| 317 | ret = lttng_untrack_pid(handle[i], pid); |
| 318 | if (ret && ret != -LTTNG_ERR_INVALID) { |
| 319 | ERR("Error %d tracking pid %d", ret, pid); |
| 320 | } |
| 321 | } |
| 322 | } else if (WIFSIGNALED(status)) { |
| 323 | DBG("Child pid %d terminated by signal %d", pid, |
| 324 | WTERMSIG(status)); |
| 325 | for (i = 0; i < nr_handles; i++) { |
| 326 | ret = lttng_untrack_pid(handle[i], pid); |
| 327 | if (ret && ret != -LTTNG_ERR_INVALID) { |
| 328 | ERR("Error %d tracking pid %d", ret, pid); |
| 329 | } |
| 330 | } |
| 331 | } else { |
| 332 | DBG("Unhandled status %d from child %d", status, pid); |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | static |
| 339 | int run_child(int argc, char **argv) |
| 340 | { |
| 341 | pid_t pid; |
| 342 | int ret; |
| 343 | |
| 344 | if (argc < 1) { |
| 345 | ERR("Please provide executable name as first argument."); |
| 346 | return -1; |
| 347 | } |
| 348 | |
| 349 | pid = fork(); |
| 350 | if (pid > 0) { |
| 351 | /* In parent */ |
| 352 | DBG("Child process created (pid: %d)", pid); |
| 353 | } else if (pid == 0) { |
| 354 | /* In child */ |
| 355 | #if 0 |
| 356 | long ptraceret; |
| 357 | |
| 358 | ptraceret = ptrace(PTRACE_TRACEME, 0, NULL, NULL); |
| 359 | if (ptraceret) { |
| 360 | PERROR("ptrace"); |
| 361 | exit(EXIT_FAILURE); |
| 362 | } |
| 363 | #endif |
| 364 | ret = raise(SIGSTOP); |
| 365 | if (ret) { |
| 366 | PERROR("raise"); |
| 367 | exit(EXIT_FAILURE); |
| 368 | } |
| 369 | ret = execvp(argv[0], &argv[0]); |
| 370 | if (ret) { |
| 371 | PERROR("execvp"); |
| 372 | exit(EXIT_FAILURE); |
| 373 | } |
| 374 | } else { |
| 375 | PERROR("fork"); |
| 376 | return -1; |
| 377 | } |
| 378 | return pid; |
| 379 | } |
| 380 | |
| 381 | static |
| 382 | int create_session(struct lttng_trace_ctx *ctx) |
| 383 | { |
| 384 | return lttng_create_session(ctx->session_name, ctx->path); |
| 385 | } |
| 386 | |
| 387 | static |
| 388 | int destroy_session(struct lttng_trace_ctx *ctx) |
| 389 | { |
| 390 | return lttng_destroy_session(ctx->session_name); |
| 391 | } |
| 392 | |
| 393 | static |
| 394 | int start_session(struct lttng_trace_ctx *ctx) |
| 395 | { |
| 396 | return lttng_start_tracing(ctx->session_name); |
| 397 | } |
| 398 | |
| 399 | static |
| 400 | int enable_syscalls(struct lttng_trace_ctx *ctx) |
| 401 | { |
| 402 | struct lttng_domain domain; |
| 403 | struct lttng_event *ev; |
| 404 | struct lttng_handle *handle; |
| 405 | int ret; |
| 406 | |
| 407 | if (opt_no_syscall) |
| 408 | return 0; |
| 409 | memset(&domain, 0, sizeof(domain)); |
| 410 | ev = lttng_event_create(); |
| 411 | if (!ev) |
| 412 | abort(); |
| 413 | domain.type = LTTNG_DOMAIN_KERNEL; |
| 414 | domain.buf_type = LTTNG_BUFFER_GLOBAL; |
| 415 | |
| 416 | handle = lttng_create_handle(ctx->session_name, &domain); |
| 417 | if (!handle) |
| 418 | abort(); |
| 419 | ev->type = LTTNG_EVENT_SYSCALL; |
| 420 | strcpy(ev->name, "*"); |
| 421 | ev->loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; |
| 422 | ret = lttng_enable_event_with_exclusions(handle, |
| 423 | ev, NULL, NULL, 0, NULL); |
| 424 | if (ret) |
| 425 | abort(); |
| 426 | lttng_destroy_handle(handle); |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | static |
| 431 | int add_contexts(struct lttng_trace_ctx *ctx, enum lttng_domain_type domain_type) |
| 432 | { |
| 433 | struct lttng_domain domain; |
| 434 | struct lttng_event_context event_ctx; |
| 435 | struct lttng_handle *handle; |
| 436 | |
| 437 | if (opt_no_context) |
| 438 | return 0; |
| 439 | memset(&domain, 0, sizeof(domain)); |
| 440 | switch (domain_type) { |
| 441 | case LTTNG_DOMAIN_KERNEL: |
| 442 | domain.buf_type = LTTNG_BUFFER_GLOBAL; |
| 443 | break; |
| 444 | case LTTNG_DOMAIN_UST: |
| 445 | domain.buf_type = LTTNG_BUFFER_PER_UID; |
| 446 | break; |
| 447 | default: |
| 448 | return -1; |
| 449 | } |
| 450 | domain.type = domain_type; |
| 451 | |
| 452 | handle = lttng_create_handle(ctx->session_name, &domain); |
| 453 | if (!handle) |
| 454 | abort(); |
| 455 | |
| 456 | memset(&event_ctx, 0, sizeof(event_ctx)); |
| 457 | event_ctx.ctx = LTTNG_EVENT_CONTEXT_PROCNAME; |
| 458 | if (lttng_add_context(handle, &event_ctx, NULL, NULL) < 0) |
| 459 | abort(); |
| 460 | |
| 461 | memset(&event_ctx, 0, sizeof(event_ctx)); |
| 462 | event_ctx.ctx = LTTNG_EVENT_CONTEXT_VPID; |
| 463 | if (lttng_add_context(handle, &event_ctx, NULL, NULL) < 0) |
| 464 | abort(); |
| 465 | |
| 466 | memset(&event_ctx, 0, sizeof(event_ctx)); |
| 467 | event_ctx.ctx = LTTNG_EVENT_CONTEXT_VTID; |
| 468 | if (lttng_add_context(handle, &event_ctx, NULL, NULL) < 0) |
| 469 | abort(); |
| 470 | |
| 471 | lttng_destroy_handle(handle); |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | static |
| 476 | int create_channels(struct lttng_trace_ctx *ctx, enum lttng_domain_type domain_type) |
| 477 | { |
| 478 | struct lttng_domain domain; |
| 479 | struct lttng_channel *channel; |
| 480 | struct lttng_handle *handle; |
| 481 | |
| 482 | memset(&domain, 0, sizeof(domain)); |
| 483 | switch (domain_type) { |
| 484 | case LTTNG_DOMAIN_KERNEL: |
| 485 | domain.buf_type = LTTNG_BUFFER_GLOBAL; |
| 486 | break; |
| 487 | case LTTNG_DOMAIN_UST: |
| 488 | domain.buf_type = LTTNG_BUFFER_PER_UID; |
| 489 | break; |
| 490 | default: |
| 491 | return -1; |
| 492 | } |
| 493 | domain.type = domain_type; |
| 494 | channel = lttng_channel_create(&domain); |
| 495 | channel->enabled = 1; |
| 496 | |
| 497 | handle = lttng_create_handle(ctx->session_name, &domain); |
| 498 | if (!handle) |
| 499 | abort(); |
| 500 | if (lttng_enable_channel(handle, channel) < 0) |
| 501 | abort(); |
| 502 | lttng_destroy_handle(handle); |
| 503 | |
| 504 | lttng_channel_destroy(channel); |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | static |
| 509 | struct lttng_handle *create_kernel_handle(struct lttng_trace_ctx *ctx) |
| 510 | { |
| 511 | struct lttng_domain domain; |
| 512 | |
| 513 | memset(&domain, 0, sizeof(domain)); |
| 514 | domain.type = LTTNG_DOMAIN_KERNEL; |
| 515 | domain.buf_type = LTTNG_BUFFER_GLOBAL; |
| 516 | return lttng_create_handle(ctx->session_name, &domain); |
| 517 | } |
| 518 | |
| 519 | static |
| 520 | struct lttng_handle *create_ust_handle(struct lttng_trace_ctx *ctx) |
| 521 | { |
| 522 | struct lttng_domain domain; |
| 523 | |
| 524 | memset(&domain, 0, sizeof(domain)); |
| 525 | domain.type = LTTNG_DOMAIN_UST; |
| 526 | domain.buf_type = LTTNG_BUFFER_PER_UID; |
| 527 | return lttng_create_handle(ctx->session_name, &domain); |
| 528 | } |
| 529 | |
| 530 | static |
| 531 | void sighandler(int signo, siginfo_t *siginfo __unused, void *context __unused) |
| 532 | { |
| 533 | int ret; |
| 534 | |
| 535 | DBG("sighandler receives signal %d, forwarding to child %d", |
| 536 | signo, sigfwd_pid); |
| 537 | ret = kill(sigfwd_pid, signo); |
| 538 | if (ret) { |
| 539 | PERROR("kill"); |
| 540 | abort(); |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | static |
| 545 | int lttng_trace_ctx_init(struct lttng_trace_ctx *ctx) |
| 546 | { |
| 547 | pid_t pid; |
| 548 | char pid_str[12]; |
| 549 | int ret; |
| 550 | char datetime[16]; |
| 551 | struct tm *timeinfo; |
| 552 | |
| 553 | ctx->creation_time = time(NULL); |
| 554 | if (ctx->creation_time == (time_t) -1) |
| 555 | abort(); |
| 556 | timeinfo = localtime(&ctx->creation_time); |
| 557 | if (!timeinfo) |
| 558 | abort(); |
| 559 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); |
| 560 | |
| 561 | if (opt_session) { |
| 562 | if (strlen(session_name) > LTTNG_NAME_MAX - 1) { |
| 563 | abort(); |
| 564 | } |
| 565 | strcpy(ctx->session_name, session_name); |
| 566 | } else { |
| 567 | memset(ctx, 0, sizeof(*ctx)); |
| 568 | strcpy(ctx->session_name, "lttng-ptrace-"); |
| 569 | pid = getpid(); |
| 570 | ret = sprintf(pid_str, "%d", (int) pid); |
| 571 | if (ret < 0) |
| 572 | return -1; |
| 573 | strcat(ctx->session_name, pid_str); |
| 574 | strcat(ctx->session_name, "-"); |
| 575 | strcat(ctx->session_name, datetime); |
| 576 | } |
| 577 | |
| 578 | if (opt_output) { |
| 579 | if (strlen(output_path) > PATH_MAX - 1) { |
| 580 | abort(); |
| 581 | } |
| 582 | strcpy(ctx->path, output_path); |
| 583 | } else { |
| 584 | strcpy(ctx->path, "/tmp/lttng-ptrace/"); |
| 585 | strcat(ctx->path, ctx->session_name); |
| 586 | } |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | static |
| 591 | int lttng_trace_untrack_all(struct lttng_handle **handle, |
| 592 | size_t nr_handles) |
| 593 | { |
| 594 | size_t i; |
| 595 | int ret; |
| 596 | |
| 597 | for (i = 0; i < nr_handles; i++) { |
| 598 | ret = lttng_untrack_pid(handle[i], -1); |
| 599 | if (ret && ret != -LTTNG_ERR_INVALID) { |
| 600 | ERR("Error %d untracking pid %d", ret, -1); |
| 601 | abort(); |
| 602 | } |
| 603 | } |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | /* Return value: |
| 608 | * >= 0: number of arguments to skip before command. |
| 609 | * < 0: error. |
| 610 | */ |
| 611 | static |
| 612 | int parse_args(int argc, char **argv) |
| 613 | { |
| 614 | int i; |
| 615 | |
| 616 | for (i = 1; i < argc; i++) { |
| 617 | const char *str = argv[i]; |
| 618 | |
| 619 | if (!strcmp(str, "--")) { |
| 620 | i++; /* Next is command position. */ |
| 621 | goto end; |
| 622 | } |
| 623 | if (str[0] != '-') { |
| 624 | goto end; /* Cursor at command position. */ |
| 625 | } |
| 626 | if (!strcmp(str, "--help")) { |
| 627 | opt_help = true; |
| 628 | } |
| 629 | if (!strcmp(str, "--no-context")) { |
| 630 | opt_no_context = true; |
| 631 | } |
| 632 | if (!strcmp(str, "--no-pause")) { |
| 633 | opt_no_pause = true; |
| 634 | } |
| 635 | if (!strcmp(str, "--no-syscall")) { |
| 636 | opt_no_syscall = true; |
| 637 | } |
| 638 | if (!strcmp(str, "--output")) { |
| 639 | opt_output = true; |
| 640 | if (i == argc - 1) { |
| 641 | ERR("Expected path argument after --output"); |
| 642 | return -1; |
| 643 | } |
| 644 | output_path = argv[++i]; |
| 645 | } |
| 646 | if (!strcmp(str, "--session")) { |
| 647 | opt_session = true; |
| 648 | if (i == argc - 1) { |
| 649 | ERR("Expected path argument after --session"); |
| 650 | return -1; |
| 651 | } |
| 652 | session_name = argv[++i]; |
| 653 | } |
| 654 | if (!strcmp(str, "--view")) { |
| 655 | opt_view = true; |
| 656 | } |
| 657 | } |
| 658 | end: |
| 659 | if (i == argc && !opt_help) { |
| 660 | ERR("Expected COMMAND argument after options. See `%s --help` for details.", argv[0]); |
| 661 | return -1; |
| 662 | } |
| 663 | return i; |
| 664 | } |
| 665 | |
| 666 | static |
| 667 | int show_help(int argc __unused, char **argv) |
| 668 | { |
| 669 | printf("Usage of %s:\n", argv[0]); |
| 670 | printf("\n"); |
| 671 | printf(" %s [OPTION] [--] COMMAND [COMMAND OPTIONS]\n", argv[0]); |
| 672 | printf("\n"); |
| 673 | printf("Runs COMMAND while tracing the system calls of the children\n"); |
| 674 | printf("process hierarchy. See standard error output while executing\n"); |
| 675 | printf("this command for more information.\n"); |
| 676 | printf("\n"); |
| 677 | printf("Supported options:\n"); |
| 678 | printf(" --help: This help screen.\n"); |
| 679 | printf(" --no-context: Do not trace default contexts (vpid, vtid, procname).\n"); |
| 680 | printf(" --no-pause: Do not wait for user input before running COMMAND.\n"); |
| 681 | printf(" --no-syscall: Do not trace system calls.\n"); |
| 682 | printf(" --output PATH: Write trace into output PATH. (default: /tmp/lttng-ptrace/$SESSION_NAME)\n"); |
| 683 | printf(" --session NAME: Tracing session name. (default: lttng-ptrace-$PID-$DATETIME)\n"); |
| 684 | printf(" --view: View trace after end of COMMAND execution.\n"); |
| 685 | printf("\n"); |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | int main(int argc, char **argv) |
| 690 | { |
| 691 | int retval = 0, ret; |
| 692 | pid_t pid; |
| 693 | struct lttng_handle *handle[NR_HANDLES]; |
| 694 | struct sigaction act; |
| 695 | struct lttng_trace_ctx ptrace_ctx; |
| 696 | int skip_args = 0; |
| 697 | |
| 698 | skip_args = parse_args(argc, argv); |
| 699 | if (skip_args < 0) { |
| 700 | return EXIT_FAILURE; |
| 701 | } |
| 702 | if (opt_help) { |
| 703 | show_help(argc, argv); |
| 704 | return EXIT_SUCCESS; |
| 705 | } |
| 706 | |
| 707 | if (lttng_trace_ctx_init(&ptrace_ctx)) |
| 708 | abort(); |
| 709 | |
| 710 | act.sa_sigaction = sighandler; |
| 711 | act.sa_flags = SA_SIGINFO | SA_RESTART; |
| 712 | sigemptyset(&act.sa_mask); |
| 713 | ret = sigaction(SIGTERM, &act, NULL); |
| 714 | if (ret) |
| 715 | abort(); |
| 716 | ret = sigaction(SIGINT, &act, NULL); |
| 717 | if (ret) |
| 718 | abort(); |
| 719 | |
| 720 | if (create_session(&ptrace_ctx) < 0) { |
| 721 | fprintf(stderr, "%sError: Unable to create tracing session. Please ensure that lttng-sessiond is running as root and that your user belongs to the `tracing` group.\n", MESSAGE_PREFIX); |
| 722 | retval = -1; |
| 723 | goto end; |
| 724 | } |
| 725 | handle[0] = create_kernel_handle(&ptrace_ctx); |
| 726 | if (!handle[0]) { |
| 727 | retval = -1; |
| 728 | goto end_kernel_handle; |
| 729 | } |
| 730 | handle[1] = create_ust_handle(&ptrace_ctx); |
| 731 | if (!handle[1]) { |
| 732 | retval = -1; |
| 733 | goto end_ust_handle; |
| 734 | } |
| 735 | if (create_channels(&ptrace_ctx, LTTNG_DOMAIN_KERNEL) < 0) { |
| 736 | retval = -1; |
| 737 | goto end_wait_on_children; |
| 738 | } |
| 739 | if (create_channels(&ptrace_ctx, LTTNG_DOMAIN_UST) < 0) { |
| 740 | retval = -1; |
| 741 | goto end_wait_on_children; |
| 742 | } |
| 743 | if (enable_syscalls(&ptrace_ctx) < 0) { |
| 744 | retval = -1; |
| 745 | goto end_wait_on_children; |
| 746 | } |
| 747 | if (add_contexts(&ptrace_ctx, LTTNG_DOMAIN_KERNEL) < 0) { |
| 748 | retval = -1; |
| 749 | goto end_wait_on_children; |
| 750 | } |
| 751 | if (add_contexts(&ptrace_ctx, LTTNG_DOMAIN_UST) < 0) { |
| 752 | retval = -1; |
| 753 | goto end_wait_on_children; |
| 754 | } |
| 755 | if (lttng_trace_untrack_all(handle, NR_HANDLES) < 0) { |
| 756 | retval = -1; |
| 757 | goto end_wait_on_children; |
| 758 | } |
| 759 | fprintf(stderr, "%sTracing session `%s` created. It can be customized using the `lttng` command.\n", MESSAGE_PREFIX, ptrace_ctx.session_name); |
| 760 | if (!opt_no_pause) { |
| 761 | fprintf(stderr, "Press <ENTER> key when ready to run the child process.\n"); |
| 762 | getchar(); |
| 763 | } |
| 764 | |
| 765 | if (start_session(&ptrace_ctx) < 0) { |
| 766 | retval = -1; |
| 767 | goto end_wait_on_children; |
| 768 | } |
| 769 | |
| 770 | //TODO: signal off before we can forward it. |
| 771 | pid = run_child(argc - skip_args, argv + skip_args); |
| 772 | if (pid <= 0) { |
| 773 | retval = -1; |
| 774 | goto end; |
| 775 | } |
| 776 | |
| 777 | sigfwd_pid = pid; |
| 778 | //TODO signals on |
| 779 | |
| 780 | ret = wait_on_children(pid, handle, NR_HANDLES); |
| 781 | if (ret) { |
| 782 | retval = -1; |
| 783 | goto end_wait_on_children; |
| 784 | } |
| 785 | |
| 786 | |
| 787 | end_wait_on_children: |
| 788 | lttng_destroy_handle(handle[1]); |
| 789 | end_ust_handle: |
| 790 | lttng_destroy_handle(handle[0]); |
| 791 | end_kernel_handle: |
| 792 | if (destroy_session(&ptrace_ctx)) |
| 793 | abort(); |
| 794 | end: |
| 795 | if (retval) { |
| 796 | return EXIT_FAILURE; |
| 797 | } else { |
| 798 | fprintf(stderr, "%sSub-process hierarchy traced successfully. View trace with `babeltrace2 %s`.\n", MESSAGE_PREFIX, |
| 799 | ptrace_ctx.path); |
| 800 | if (opt_view) { |
| 801 | return execlp("babeltrace2", "babeltrace2", ptrace_ctx.path, NULL); |
| 802 | } |
| 803 | return EXIT_SUCCESS; |
| 804 | } |
| 805 | return 0; |
| 806 | } |