Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl> |
| 3 | * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl> |
| 4 | * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com> |
Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 5 | * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl> |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 6 | * All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * 2. Redistributions in binary form must reproduce the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer in the |
| 15 | * documentation and/or other materials provided with the distribution. |
| 16 | * 3. The name of the author may not be used to endorse or promote products |
| 17 | * derived from this software without specific prior written permission. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | * |
| 30 | * $Id$ |
| 31 | */ |
| 32 | |
Wichert Akkerman | 2ee6e45 | 2000-02-18 15:36:12 +0000 | [diff] [blame] | 33 | #include <sys/types.h> |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 34 | #include "defs.h" |
| 35 | |
| 36 | #include <signal.h> |
| 37 | #include <errno.h> |
| 38 | #include <sys/param.h> |
| 39 | #include <fcntl.h> |
| 40 | #include <sys/resource.h> |
| 41 | #include <sys/wait.h> |
| 42 | #include <sys/stat.h> |
| 43 | #include <pwd.h> |
| 44 | #include <grp.h> |
| 45 | #include <string.h> |
| 46 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 47 | #ifdef USE_PROCFS |
| 48 | #include <poll.h> |
| 49 | #endif |
| 50 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 51 | #ifdef SVR4 |
| 52 | #include <sys/stropts.h> |
Wichert Akkerman | ea78f0f | 1999-11-29 15:34:02 +0000 | [diff] [blame] | 53 | #ifdef HAVE_MP_PROCFS |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 54 | #include <sys/uio.h> |
| 55 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 56 | #endif |
| 57 | |
| 58 | int debug = 0, followfork = 0, followvfork = 0, interactive = 0; |
| 59 | int rflag = 0, tflag = 0, dtime = 0, cflag = 0; |
| 60 | int iflag = 0, xflag = 0, qflag = 0; |
| 61 | int pflag_seen = 0; |
| 62 | |
| 63 | char *username = NULL; |
| 64 | uid_t run_uid; |
| 65 | gid_t run_gid; |
| 66 | |
| 67 | int acolumn = DEFAULT_ACOLUMN; |
| 68 | int max_strlen = DEFAULT_STRLEN; |
| 69 | char *outfname = NULL; |
| 70 | FILE *outf; |
| 71 | struct tcb tcbtab[MAX_PROCS]; |
| 72 | int nprocs; |
| 73 | char *progname; |
| 74 | extern char version[]; |
| 75 | extern char **environ; |
| 76 | |
| 77 | static struct tcb *pid2tcb P((int pid)); |
| 78 | static int trace P((void)); |
| 79 | static void cleanup P((void)); |
| 80 | static void interrupt P((int sig)); |
| 81 | static sigset_t empty_set, blocked_set; |
| 82 | |
| 83 | #ifdef HAVE_SIG_ATOMIC_T |
| 84 | static volatile sig_atomic_t interrupted; |
| 85 | #else /* !HAVE_SIG_ATOMIC_T */ |
| 86 | #ifdef __STDC__ |
| 87 | static volatile int interrupted; |
| 88 | #else /* !__STDC__ */ |
| 89 | static int interrupted; |
| 90 | #endif /* !__STDC__ */ |
| 91 | #endif /* !HAVE_SIG_ATOMIC_T */ |
| 92 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 93 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 94 | |
| 95 | static struct tcb *pfd2tcb P((int pfd)); |
| 96 | static void reaper P((int sig)); |
| 97 | static void rebuild_pollv P((void)); |
Wichert Akkerman | e68d61c | 1999-06-28 13:17:16 +0000 | [diff] [blame] | 98 | struct pollfd pollv[MAX_PROCS]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 99 | |
| 100 | #ifndef HAVE_POLLABLE_PROCFS |
| 101 | |
| 102 | static void proc_poll_open P((void)); |
| 103 | static void proc_poller P((int pfd)); |
| 104 | |
| 105 | struct proc_pollfd { |
| 106 | int fd; |
| 107 | int revents; |
| 108 | int pid; |
| 109 | }; |
| 110 | |
| 111 | static int poller_pid; |
| 112 | static int proc_poll_pipe[2] = { -1, -1 }; |
| 113 | |
| 114 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 115 | |
Wichert Akkerman | ea78f0f | 1999-11-29 15:34:02 +0000 | [diff] [blame] | 116 | #ifdef HAVE_MP_PROCFS |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 117 | #define POLLWANT POLLWRNORM |
| 118 | #else |
| 119 | #define POLLWANT POLLPRI |
| 120 | #endif |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 121 | #endif /* USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 122 | |
| 123 | static void |
| 124 | usage(ofp, exitval) |
| 125 | FILE *ofp; |
| 126 | int exitval; |
| 127 | { |
| 128 | fprintf(ofp, "\ |
| 129 | usage: strace [-dffhiqrtttTvVxx] [-a column] [-e expr] ... [-o file]\n\ |
| 130 | [-p pid] ... [-s strsize] [-u username] [command [arg ...]]\n\ |
| 131 | or: strace -c [-e expr] ... [-O overhead] [-S sortby] [command [arg ...]]\n\ |
| 132 | -c -- count time, calls, and errors for each syscall and report summary\n\ |
| 133 | -f -- follow forks, -ff -- with output into separate files\n\ |
| 134 | -F -- attempt to follow vforks, -h -- print help message\n\ |
| 135 | -i -- print instruction pointer at time of syscall\n\ |
| 136 | -q -- suppress messages about attaching, detaching, etc.\n\ |
| 137 | -r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs\n\ |
| 138 | -T -- print time spent in each syscall, -V -- print version\n\ |
| 139 | -v -- verbose mode: print unabbreviated argv, stat, termio[s], etc. args\n\ |
| 140 | -x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\ |
| 141 | -a column -- alignment COLUMN for printing syscall results (default %d)\n\ |
| 142 | -e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...\n\ |
| 143 | options: trace, abbrev, verbose, raw, signal, read, or write\n\ |
| 144 | -o file -- send trace output to FILE instead of stderr\n\ |
| 145 | -O overhead -- set overhead for tracing syscalls to OVERHEAD usecs\n\ |
| 146 | -p pid -- trace process with process id PID, may be repeated\n\ |
| 147 | -s strsize -- limit length of print strings to STRSIZE chars (default %d)\n\ |
| 148 | -S sortby -- sort syscall counts by: time, calls, name, nothing (default %s)\n\ |
| 149 | -u username -- run command as username handling setuid and/or setgid\n\ |
| 150 | ", DEFAULT_ACOLUMN, DEFAULT_STRLEN, DEFAULT_SORTBY); |
| 151 | exit(exitval); |
| 152 | } |
| 153 | |
| 154 | #ifdef SVR4 |
| 155 | #ifdef MIPS |
| 156 | void |
| 157 | foobar() |
| 158 | { |
| 159 | } |
| 160 | #endif /* MIPS */ |
| 161 | #endif /* SVR4 */ |
| 162 | |
| 163 | int |
| 164 | main(argc, argv) |
| 165 | int argc; |
| 166 | char *argv[]; |
| 167 | { |
| 168 | extern int optind; |
| 169 | extern char *optarg; |
| 170 | struct tcb *tcp; |
| 171 | int c, pid = 0; |
| 172 | struct sigaction sa; |
| 173 | |
| 174 | static char buf[BUFSIZ]; |
| 175 | |
| 176 | progname = argv[0]; |
| 177 | outf = stderr; |
| 178 | interactive = 1; |
| 179 | qualify("trace=all"); |
| 180 | qualify("abbrev=all"); |
| 181 | qualify("verbose=all"); |
| 182 | qualify("signal=all"); |
| 183 | set_sortby(DEFAULT_SORTBY); |
| 184 | set_personality(DEFAULT_PERSONALITY); |
| 185 | while ((c = getopt(argc, argv, |
| 186 | "+cdfFhiqrtTvVxa:e:o:O:p:s:S:u:")) != EOF) { |
| 187 | switch (c) { |
| 188 | case 'c': |
| 189 | cflag++; |
| 190 | dtime++; |
| 191 | break; |
| 192 | case 'd': |
| 193 | debug++; |
| 194 | break; |
| 195 | case 'f': |
| 196 | followfork++; |
| 197 | break; |
| 198 | case 'F': |
| 199 | followvfork++; |
| 200 | break; |
| 201 | case 'h': |
| 202 | usage(stdout, 0); |
| 203 | break; |
| 204 | case 'i': |
| 205 | iflag++; |
| 206 | break; |
| 207 | case 'q': |
| 208 | qflag++; |
| 209 | break; |
| 210 | case 'r': |
| 211 | rflag++; |
| 212 | tflag++; |
| 213 | break; |
| 214 | case 't': |
| 215 | tflag++; |
| 216 | break; |
| 217 | case 'T': |
| 218 | dtime++; |
| 219 | break; |
| 220 | case 'x': |
| 221 | xflag++; |
| 222 | break; |
| 223 | case 'v': |
| 224 | qualify("abbrev=none"); |
| 225 | break; |
| 226 | case 'V': |
| 227 | printf("%s\n", version); |
| 228 | exit(0); |
| 229 | break; |
| 230 | case 'a': |
| 231 | acolumn = atoi(optarg); |
| 232 | break; |
| 233 | case 'e': |
| 234 | qualify(optarg); |
| 235 | break; |
| 236 | case 'o': |
| 237 | outfname = strdup(optarg); |
| 238 | break; |
| 239 | case 'O': |
| 240 | set_overhead(atoi(optarg)); |
| 241 | break; |
| 242 | case 'p': |
| 243 | if ((pid = atoi(optarg)) == 0) { |
| 244 | fprintf(stderr, "%s: Invalid process id: %s\n", |
| 245 | progname, optarg); |
| 246 | break; |
| 247 | } |
| 248 | if (pid == getpid()) { |
Wichert Akkerman | 54a4767 | 1999-10-17 00:57:34 +0000 | [diff] [blame] | 249 | fprintf(stderr, "%s: I'm sorry, I can't let you do that, Dave.\n", progname); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 250 | break; |
| 251 | } |
| 252 | if ((tcp = alloctcb(pid)) == NULL) { |
| 253 | fprintf(stderr, "%s: tcb table full, please recompile strace\n", |
| 254 | progname); |
| 255 | exit(1); |
| 256 | } |
| 257 | tcp->flags |= TCB_ATTACHED; |
| 258 | pflag_seen++; |
| 259 | break; |
| 260 | case 's': |
| 261 | max_strlen = atoi(optarg); |
| 262 | break; |
| 263 | case 'S': |
| 264 | set_sortby(optarg); |
| 265 | break; |
| 266 | case 'u': |
| 267 | username = strdup(optarg); |
| 268 | break; |
| 269 | default: |
| 270 | usage(stderr, 1); |
| 271 | break; |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /* See if they want to run as another user. */ |
| 276 | if (username != NULL) { |
| 277 | struct passwd *pent; |
| 278 | |
| 279 | if (getuid() != 0 || geteuid() != 0) { |
| 280 | fprintf(stderr, |
| 281 | "%s: you must be root to use the -u option\n", |
| 282 | progname); |
| 283 | exit(1); |
| 284 | } |
| 285 | if ((pent = getpwnam(username)) == NULL) { |
| 286 | fprintf(stderr, "%s: cannot find user `%s'\n", |
| 287 | progname, optarg); |
| 288 | exit(1); |
| 289 | } |
| 290 | run_uid = pent->pw_uid; |
| 291 | run_gid = pent->pw_gid; |
| 292 | } |
| 293 | else { |
| 294 | run_uid = getuid(); |
| 295 | run_gid = getgid(); |
| 296 | } |
| 297 | |
| 298 | #ifndef SVR4 |
| 299 | setreuid(geteuid(), getuid()); |
| 300 | #endif |
| 301 | |
| 302 | /* See if they want to pipe the output. */ |
| 303 | if (outfname && (outfname[0] == '|' || outfname[0] == '!')) { |
| 304 | if ((outf = popen(outfname + 1, "w")) == NULL) { |
| 305 | fprintf(stderr, "%s: can't popen '%s': %s\n", |
| 306 | progname, outfname + 1, strerror(errno)); |
| 307 | exit(1); |
| 308 | } |
| 309 | free(outfname); |
| 310 | outfname = NULL; |
| 311 | } |
| 312 | |
| 313 | /* Check if they want to redirect the output. */ |
| 314 | if (outfname) { |
| 315 | if ((outf = fopen(outfname, "w")) == NULL) { |
| 316 | fprintf(stderr, "%s: can't fopen '%s': %s\n", |
| 317 | progname, outfname, strerror(errno)); |
| 318 | exit(1); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | #ifndef SVR4 |
| 323 | setreuid(geteuid(), getuid()); |
| 324 | #endif |
| 325 | |
| 326 | if (!outfname) { |
| 327 | qflag = 1; |
| 328 | setvbuf(outf, buf, _IOLBF, BUFSIZ); |
| 329 | } |
| 330 | else if (optind < argc) |
| 331 | interactive = 0; |
| 332 | else |
| 333 | qflag = 1; |
| 334 | |
| 335 | for (c = 0, tcp = tcbtab; c < MAX_PROCS; c++, tcp++) { |
| 336 | /* Reinitialize the output since it may have changed. */ |
| 337 | tcp->outf = outf; |
| 338 | if (!(tcp->flags & TCB_INUSE) || !(tcp->flags & TCB_ATTACHED)) |
| 339 | continue; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 340 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 341 | if (proc_open(tcp, 1) < 0) { |
| 342 | fprintf(stderr, "trouble opening proc file\n"); |
| 343 | droptcb(tcp); |
| 344 | continue; |
| 345 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 346 | #else /* !USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 347 | if (ptrace(PTRACE_ATTACH, tcp->pid, (char *) 1, 0) < 0) { |
| 348 | perror("attach: ptrace(PTRACE_ATTACH, ...)"); |
| 349 | droptcb(tcp); |
| 350 | continue; |
| 351 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 352 | #endif /* !USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 353 | if (!qflag) |
| 354 | fprintf(stderr, |
| 355 | "Process %u attached - interrupt to quit\n", |
| 356 | pid); |
| 357 | } |
| 358 | |
| 359 | if (optind < argc) { |
| 360 | struct stat statbuf; |
| 361 | char *filename; |
| 362 | char pathname[MAXPATHLEN]; |
| 363 | |
| 364 | filename = argv[optind]; |
| 365 | if (strchr(filename, '/')) |
| 366 | strcpy(pathname, filename); |
| 367 | #ifdef USE_DEBUGGING_EXEC |
| 368 | /* |
| 369 | * Debuggers customarily check the current directory |
| 370 | * first regardless of the path but doing that gives |
| 371 | * security geeks a panic attack. |
| 372 | */ |
| 373 | else if (stat(filename, &statbuf) == 0) |
| 374 | strcpy(pathname, filename); |
| 375 | #endif /* USE_DEBUGGING_EXEC */ |
| 376 | else { |
| 377 | char *path; |
| 378 | int m, n, len; |
| 379 | |
| 380 | for (path = getenv("PATH"); path && *path; path += m) { |
| 381 | if (strchr(path, ':')) { |
| 382 | n = strchr(path, ':') - path; |
| 383 | m = n + 1; |
| 384 | } |
| 385 | else |
| 386 | m = n = strlen(path); |
| 387 | if (n == 0) { |
| 388 | getcwd(pathname, MAXPATHLEN); |
| 389 | len = strlen(pathname); |
| 390 | } |
| 391 | else { |
| 392 | strncpy(pathname, path, n); |
| 393 | len = n; |
| 394 | } |
| 395 | if (len && pathname[len - 1] != '/') |
| 396 | pathname[len++] = '/'; |
| 397 | strcpy(pathname + len, filename); |
| 398 | if (stat(pathname, &statbuf) == 0) |
| 399 | break; |
| 400 | } |
| 401 | } |
| 402 | if (stat(pathname, &statbuf) < 0) { |
| 403 | fprintf(stderr, "%s: %s: command not found\n", |
| 404 | progname, filename); |
| 405 | exit(1); |
| 406 | } |
| 407 | switch (pid = fork()) { |
| 408 | case -1: |
| 409 | perror("strace: fork"); |
| 410 | cleanup(); |
| 411 | exit(1); |
| 412 | break; |
| 413 | case 0: { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 414 | #ifdef USE_PROCFS |
| 415 | if (outf != stderr) close (fileno (outf)); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 416 | #ifdef MIPS |
| 417 | /* Kludge for SGI, see proc_open for details. */ |
| 418 | sa.sa_handler = foobar; |
| 419 | sa.sa_flags = 0; |
| 420 | sigemptyset(&sa.sa_mask); |
| 421 | sigaction(SIGINT, &sa, NULL); |
| 422 | #endif /* MIPS */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 423 | #ifndef FREEBSD |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 424 | pause(); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 425 | #else /* FREEBSD */ |
| 426 | kill(getpid(), SIGSTOP); /* stop HERE */ |
| 427 | #endif /* FREEBSD */ |
| 428 | #else /* !USE_PROCFS */ |
Wichert Akkerman | bd4125c | 2000-06-27 17:28:06 +0000 | [diff] [blame] | 429 | if (outf!=stderr) |
Wichert Akkerman | 7987cdf | 2000-07-05 16:05:39 +0000 | [diff] [blame] | 430 | close(fileno (outf)); |
Wichert Akkerman | bd4125c | 2000-06-27 17:28:06 +0000 | [diff] [blame] | 431 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 432 | if (ptrace(PTRACE_TRACEME, 0, (char *) 1, 0) < 0) { |
| 433 | perror("strace: ptrace(PTRACE_TRACEME, ...)"); |
| 434 | return -1; |
| 435 | } |
| 436 | if (debug) |
| 437 | kill(getpid(), SIGSTOP); |
| 438 | |
| 439 | if (username != NULL || geteuid() == 0) { |
| 440 | uid_t run_euid = run_uid; |
| 441 | gid_t run_egid = run_gid; |
| 442 | |
| 443 | if (statbuf.st_mode & S_ISUID) |
| 444 | run_euid = statbuf.st_uid; |
| 445 | if (statbuf.st_mode & S_ISGID) |
| 446 | run_egid = statbuf.st_gid; |
| 447 | |
| 448 | /* |
| 449 | * It is important to set groups before we |
| 450 | * lose privileges on setuid. |
| 451 | */ |
Wichert Akkerman | 5ae21ea | 2000-05-01 01:53:59 +0000 | [diff] [blame] | 452 | if (username != NULL) { |
| 453 | if (initgroups(username, run_gid) < 0) { |
| 454 | perror("initgroups"); |
| 455 | exit(1); |
| 456 | } |
| 457 | if (setregid(run_gid, run_egid) < 0) { |
| 458 | perror("setregid"); |
| 459 | exit(1); |
| 460 | } |
| 461 | if (setreuid(run_uid, run_euid) < 0) { |
| 462 | perror("setreuid"); |
| 463 | exit(1); |
| 464 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | else |
| 468 | setreuid(run_uid, run_uid); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 469 | #endif /* !USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 470 | |
| 471 | execv(pathname, &argv[optind]); |
| 472 | perror("strace: exec"); |
| 473 | _exit(1); |
| 474 | break; |
| 475 | } |
| 476 | default: |
| 477 | if ((tcp = alloctcb(pid)) == NULL) { |
| 478 | fprintf(stderr, "tcb table full\n"); |
| 479 | cleanup(); |
| 480 | exit(1); |
| 481 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 482 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 483 | if (proc_open(tcp, 0) < 0) { |
| 484 | fprintf(stderr, "trouble opening proc file\n"); |
| 485 | cleanup(); |
| 486 | exit(1); |
| 487 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 488 | #endif /* USE_PROCFS */ |
| 489 | #ifndef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 490 | fake_execve(tcp, pathname, &argv[optind], environ); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 491 | #endif /* !USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 492 | break; |
| 493 | } |
| 494 | } |
| 495 | else if (pflag_seen == 0) |
| 496 | usage(stderr, 1); |
| 497 | |
| 498 | sigemptyset(&empty_set); |
| 499 | sigemptyset(&blocked_set); |
| 500 | sa.sa_handler = SIG_IGN; |
| 501 | sigemptyset(&sa.sa_mask); |
| 502 | sa.sa_flags = 0; |
| 503 | sigaction(SIGTTOU, &sa, NULL); |
| 504 | sigaction(SIGTTIN, &sa, NULL); |
| 505 | if (interactive) { |
| 506 | sigaddset(&blocked_set, SIGHUP); |
| 507 | sigaddset(&blocked_set, SIGINT); |
| 508 | sigaddset(&blocked_set, SIGQUIT); |
| 509 | sigaddset(&blocked_set, SIGPIPE); |
| 510 | sigaddset(&blocked_set, SIGTERM); |
| 511 | sa.sa_handler = interrupt; |
| 512 | #ifdef SUNOS4 |
| 513 | /* POSIX signals on sunos4.1 are a little broken. */ |
| 514 | sa.sa_flags = SA_INTERRUPT; |
| 515 | #endif /* SUNOS4 */ |
| 516 | } |
| 517 | sigaction(SIGHUP, &sa, NULL); |
| 518 | sigaction(SIGINT, &sa, NULL); |
| 519 | sigaction(SIGQUIT, &sa, NULL); |
| 520 | sigaction(SIGPIPE, &sa, NULL); |
| 521 | sigaction(SIGTERM, &sa, NULL); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 522 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 523 | sa.sa_handler = reaper; |
| 524 | sigaction(SIGCHLD, &sa, NULL); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 525 | #endif /* USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 526 | |
| 527 | if (trace() < 0) |
| 528 | exit(1); |
| 529 | cleanup(); |
| 530 | exit(0); |
| 531 | } |
| 532 | |
| 533 | void |
| 534 | newoutf(tcp) |
| 535 | struct tcb *tcp; |
| 536 | { |
| 537 | char name[MAXPATHLEN]; |
| 538 | FILE *fp; |
| 539 | |
| 540 | if (outfname && followfork > 1) { |
| 541 | sprintf(name, "%s.%u", outfname, tcp->pid); |
| 542 | #ifndef SVR4 |
| 543 | setreuid(geteuid(), getuid()); |
| 544 | #endif |
| 545 | fp = fopen(name, "w"); |
| 546 | #ifndef SVR4 |
| 547 | setreuid(geteuid(), getuid()); |
| 548 | #endif |
| 549 | if (fp == NULL) { |
| 550 | perror("fopen"); |
| 551 | return; |
| 552 | } |
| 553 | tcp->outf = fp; |
| 554 | } |
| 555 | return; |
| 556 | } |
| 557 | |
| 558 | struct tcb * |
| 559 | alloctcb(pid) |
| 560 | int pid; |
| 561 | { |
| 562 | int i; |
| 563 | struct tcb *tcp; |
| 564 | |
| 565 | for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) { |
| 566 | if ((tcp->flags & TCB_INUSE) == 0) { |
| 567 | tcp->pid = pid; |
| 568 | tcp->parent = NULL; |
| 569 | tcp->nchildren = 0; |
| 570 | tcp->flags = TCB_INUSE | TCB_STARTUP; |
| 571 | tcp->outf = outf; /* Initialise to current out file */ |
| 572 | tcp->stime.tv_sec = 0; |
| 573 | tcp->stime.tv_usec = 0; |
| 574 | tcp->pfd = -1; |
| 575 | nprocs++; |
| 576 | return tcp; |
| 577 | } |
| 578 | } |
| 579 | return NULL; |
| 580 | } |
| 581 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 582 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 583 | int |
| 584 | proc_open(tcp, attaching) |
| 585 | struct tcb *tcp; |
| 586 | int attaching; |
| 587 | { |
| 588 | char proc[32]; |
| 589 | long arg; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 590 | #ifdef SVR4 |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 591 | sysset_t sc_enter, sc_exit; |
| 592 | sigset_t signals; |
| 593 | fltset_t faults; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 594 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 595 | #ifndef HAVE_POLLABLE_PROCFS |
| 596 | static int last_pfd; |
| 597 | #endif |
| 598 | |
Wichert Akkerman | ea78f0f | 1999-11-29 15:34:02 +0000 | [diff] [blame] | 599 | #ifdef HAVE_MP_PROCFS |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 600 | /* Open the process pseudo-files in /proc. */ |
| 601 | sprintf(proc, "/proc/%d/ctl", tcp->pid); |
| 602 | if ((tcp->pfd = open(proc, O_WRONLY|O_EXCL)) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 603 | perror("strace: open(\"/proc/...\", ...)"); |
| 604 | return -1; |
| 605 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 606 | if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) { |
| 607 | perror("F_GETFD"); |
| 608 | return -1; |
| 609 | } |
| 610 | if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) { |
| 611 | perror("F_SETFD"); |
| 612 | return -1; |
| 613 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 614 | sprintf(proc, "/proc/%d/status", tcp->pid); |
| 615 | if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) { |
| 616 | perror("strace: open(\"/proc/...\", ...)"); |
| 617 | return -1; |
| 618 | } |
| 619 | if ((arg = fcntl(tcp->pfd_stat, F_GETFD)) < 0) { |
| 620 | perror("F_GETFD"); |
| 621 | return -1; |
| 622 | } |
| 623 | if (fcntl(tcp->pfd_stat, F_SETFD, arg|FD_CLOEXEC) < 0) { |
| 624 | perror("F_SETFD"); |
| 625 | return -1; |
| 626 | } |
| 627 | sprintf(proc, "/proc/%d/as", tcp->pid); |
| 628 | if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) { |
| 629 | perror("strace: open(\"/proc/...\", ...)"); |
| 630 | return -1; |
| 631 | } |
| 632 | if ((arg = fcntl(tcp->pfd_as, F_GETFD)) < 0) { |
| 633 | perror("F_GETFD"); |
| 634 | return -1; |
| 635 | } |
| 636 | if (fcntl(tcp->pfd_as, F_SETFD, arg|FD_CLOEXEC) < 0) { |
| 637 | perror("F_SETFD"); |
| 638 | return -1; |
| 639 | } |
| 640 | #else |
| 641 | /* Open the process pseudo-file in /proc. */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 642 | #ifndef FREEBSD |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 643 | sprintf(proc, "/proc/%d", tcp->pid); |
| 644 | if ((tcp->pfd = open(proc, O_RDWR|O_EXCL)) < 0) { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 645 | #else /* FREEBSD */ |
| 646 | sprintf(proc, "/proc/%d/mem", tcp->pid); |
| 647 | if ((tcp->pfd = open(proc, O_RDWR)) < 0) { |
| 648 | #endif /* FREEBSD */ |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 649 | perror("strace: open(\"/proc/...\", ...)"); |
| 650 | return -1; |
| 651 | } |
| 652 | if ((arg = fcntl(tcp->pfd, F_GETFD)) < 0) { |
| 653 | perror("F_GETFD"); |
| 654 | return -1; |
| 655 | } |
| 656 | if (fcntl(tcp->pfd, F_SETFD, arg|FD_CLOEXEC) < 0) { |
| 657 | perror("F_SETFD"); |
| 658 | return -1; |
| 659 | } |
| 660 | #endif |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 661 | #ifdef FREEBSD |
| 662 | sprintf(proc, "/proc/%d/regs", tcp->pid); |
| 663 | if ((tcp->pfd_reg = open(proc, O_RDONLY)) < 0) { |
| 664 | perror("strace: open(\"/proc/.../regs\", ...)"); |
| 665 | return -1; |
| 666 | } |
| 667 | if (cflag) { |
| 668 | sprintf(proc, "/proc/%d/status", tcp->pid); |
| 669 | if ((tcp->pfd_status = open(proc, O_RDONLY)) < 0) { |
| 670 | perror("strace: open(\"/proc/.../status\", ...)"); |
| 671 | return -1; |
| 672 | } |
| 673 | } else |
| 674 | tcp->pfd_status = -1; |
| 675 | #endif /* FREEBSD */ |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 676 | rebuild_pollv(); |
| 677 | if (!attaching) { |
| 678 | /* |
| 679 | * Wait for the child to pause. Because of a race |
| 680 | * condition we have to poll for the event. |
| 681 | */ |
| 682 | for (;;) { |
| 683 | if (IOCTL_STATUS (tcp) < 0) { |
| 684 | perror("strace: PIOCSTATUS"); |
| 685 | return -1; |
| 686 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 687 | #ifndef FREEBSD |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 688 | if (tcp->status.PR_FLAGS & PR_ASLEEP) |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 689 | #else |
| 690 | if (tcp->status.state == 1) |
| 691 | #endif |
| 692 | break; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 693 | } |
| 694 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 695 | #ifndef FREEBSD |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 696 | /* Stop the process so that we own the stop. */ |
Wichert Akkerman | 16a03d2 | 2000-08-10 02:14:04 +0000 | [diff] [blame] | 697 | if (IOCTL(tcp->pfd, PIOCSTOP, (char *)NULL) < 0) { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 698 | perror("strace: PIOCSTOP"); |
| 699 | return -1; |
| 700 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 701 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 702 | #ifdef PIOCSET |
| 703 | /* Set Run-on-Last-Close. */ |
| 704 | arg = PR_RLC; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 705 | if (IOCTL(tcp->pfd, PIOCSET, &arg) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 706 | perror("PIOCSET PR_RLC"); |
| 707 | return -1; |
| 708 | } |
| 709 | /* Set or Reset Inherit-on-Fork. */ |
| 710 | arg = PR_FORK; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 711 | if (IOCTL(tcp->pfd, followfork ? PIOCSET : PIOCRESET, &arg) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 712 | perror("PIOC{SET,RESET} PR_FORK"); |
| 713 | return -1; |
| 714 | } |
| 715 | #else /* !PIOCSET */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 716 | #ifndef FREEBSD |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 717 | if (ioctl(tcp->pfd, PIOCSRLC) < 0) { |
| 718 | perror("PIOCSRLC"); |
| 719 | return -1; |
| 720 | } |
| 721 | if (ioctl(tcp->pfd, followfork ? PIOCSFORK : PIOCRFORK) < 0) { |
| 722 | perror("PIOC{S,R}FORK"); |
| 723 | return -1; |
| 724 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 725 | #else /* FREEBSD */ |
| 726 | /* just unset the PF_LINGER flag for the Run-on-Last-Close. */ |
| 727 | if (ioctl(tcp->pfd, PIOCGFL, &arg) < 0) { |
| 728 | perror("PIOCGFL"); |
| 729 | return -1; |
| 730 | } |
| 731 | arg &= ~PF_LINGER; |
| 732 | if (ioctl(tcp->pfd, PIOCSFL, arg) < 0) { |
| 733 | perror("PIOCSFL"); |
| 734 | return -1; |
| 735 | } |
| 736 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 737 | #endif /* !PIOCSET */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 738 | #ifndef FREEBSD |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 739 | /* Enable all syscall entries. */ |
| 740 | prfillset(&sc_enter); |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 741 | if (IOCTL(tcp->pfd, PIOCSENTRY, &sc_enter) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 742 | perror("PIOCSENTRY"); |
| 743 | return -1; |
| 744 | } |
| 745 | /* Enable all syscall exits. */ |
| 746 | prfillset(&sc_exit); |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 747 | if (IOCTL(tcp->pfd, PIOCSEXIT, &sc_exit) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 748 | perror("PIOSEXIT"); |
| 749 | return -1; |
| 750 | } |
| 751 | /* Enable all signals. */ |
| 752 | prfillset(&signals); |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 753 | if (IOCTL(tcp->pfd, PIOCSTRACE, &signals) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 754 | perror("PIOCSTRACE"); |
| 755 | return -1; |
| 756 | } |
| 757 | /* Enable all faults. */ |
| 758 | prfillset(&faults); |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 759 | if (IOCTL(tcp->pfd, PIOCSFAULT, &faults) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 760 | perror("PIOCSFAULT"); |
| 761 | return -1; |
| 762 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 763 | #else /* FREEBSD */ |
| 764 | /* set events flags. */ |
| 765 | arg = S_SIG | S_SCE | S_SCX ; |
| 766 | if(ioctl(tcp->pfd, PIOCBIS, arg) < 0) { |
| 767 | perror("PIOCBIS"); |
| 768 | return -1; |
| 769 | } |
| 770 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 771 | if (!attaching) { |
| 772 | #ifdef MIPS |
| 773 | /* |
| 774 | * The SGI PRSABORT doesn't work for pause() so |
| 775 | * we send it a caught signal to wake it up. |
| 776 | */ |
| 777 | kill(tcp->pid, SIGINT); |
| 778 | #else /* !MIPS */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 779 | #ifdef PRSABORT |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 780 | /* The child is in a pause(), abort it. */ |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 781 | arg = PRSABORT; |
| 782 | if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 783 | perror("PIOCRUN"); |
| 784 | return -1; |
| 785 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 786 | #endif |
| 787 | #endif /* !MIPS*/ |
| 788 | #ifdef FREEBSD |
| 789 | /* wake up the child if it received the SIGSTOP */ |
| 790 | kill(tcp->pid, SIGCONT); |
| 791 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 792 | for (;;) { |
| 793 | /* Wait for the child to do something. */ |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 794 | if (IOCTL_WSTOP (tcp) < 0) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 795 | perror("PIOCWSTOP"); |
| 796 | return -1; |
| 797 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 798 | if (tcp->status.PR_WHY == PR_SYSENTRY) { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 799 | tcp->flags &= ~TCB_INSYSCALL; |
| 800 | get_scno(tcp); |
| 801 | if (tcp->scno == SYS_execve) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 802 | break; |
| 803 | } |
| 804 | /* Set it running: maybe execve will be next. */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 805 | #ifndef FREEBSD |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 806 | arg = 0; |
| 807 | if (IOCTL(tcp->pfd, PIOCRUN, &arg) < 0) { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 808 | #else /* FREEBSD */ |
| 809 | if (IOCTL(tcp->pfd, PIOCRUN, 0) < 0) { |
| 810 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 811 | perror("PIOCRUN"); |
| 812 | return -1; |
| 813 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 814 | #ifdef FREEBSD |
| 815 | /* handle the case where we "opened" the child before |
| 816 | it did the kill -STOP */ |
| 817 | if (tcp->status.PR_WHY == PR_SIGNALLED && |
| 818 | tcp->status.PR_WHAT == SIGSTOP) |
| 819 | kill(tcp->pid, SIGCONT); |
| 820 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 821 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 822 | #ifndef FREEBSD |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 823 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 824 | #else /* FREEBSD */ |
| 825 | } else { |
| 826 | /* little hack to show the current syscall */ |
| 827 | IOCTL_STATUS(tcp); |
| 828 | tcp->flags &= ~TCB_INSYSCALL; |
| 829 | tcp->status.why = PR_SYSENTRY; |
| 830 | trace_syscall(tcp); |
| 831 | } |
| 832 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 833 | #ifndef HAVE_POLLABLE_PROCFS |
| 834 | if (proc_poll_pipe[0] != -1) |
| 835 | proc_poller(tcp->pfd); |
| 836 | else if (nprocs > 1) { |
| 837 | proc_poll_open(); |
| 838 | proc_poller(last_pfd); |
| 839 | proc_poller(tcp->pfd); |
| 840 | } |
| 841 | last_pfd = tcp->pfd; |
| 842 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 843 | return 0; |
| 844 | } |
| 845 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 846 | #endif /* USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 847 | |
| 848 | static struct tcb * |
| 849 | pid2tcb(pid) |
| 850 | int pid; |
| 851 | { |
| 852 | int i; |
| 853 | struct tcb *tcp; |
| 854 | |
| 855 | for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) { |
| 856 | if (pid && tcp->pid != pid) |
| 857 | continue; |
| 858 | if (tcp->flags & TCB_INUSE) |
| 859 | return tcp; |
| 860 | } |
| 861 | return NULL; |
| 862 | } |
| 863 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 864 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 865 | |
| 866 | static struct tcb * |
| 867 | pfd2tcb(pfd) |
| 868 | int pfd; |
| 869 | { |
| 870 | int i; |
| 871 | struct tcb *tcp; |
| 872 | |
| 873 | for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) { |
| 874 | if (tcp->pfd != pfd) |
| 875 | continue; |
| 876 | if (tcp->flags & TCB_INUSE) |
| 877 | return tcp; |
| 878 | } |
| 879 | return NULL; |
| 880 | } |
| 881 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 882 | #endif /* USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 883 | |
| 884 | void |
| 885 | droptcb(tcp) |
| 886 | struct tcb *tcp; |
| 887 | { |
| 888 | if (tcp->pid == 0) |
| 889 | return; |
| 890 | nprocs--; |
| 891 | tcp->pid = 0; |
| 892 | tcp->flags = 0; |
| 893 | if (tcp->pfd != -1) { |
| 894 | close(tcp->pfd); |
| 895 | tcp->pfd = -1; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 896 | #ifdef FREEBSD |
| 897 | if (tcp->pfd_reg != -1) { |
| 898 | close(tcp->pfd_reg); |
| 899 | tcp->pfd_reg = -1; |
| 900 | } |
| 901 | if (tcp->pfd_status != -1) { |
| 902 | close(tcp->pfd_status); |
| 903 | tcp->pfd_status = -1; |
| 904 | } |
| 905 | #endif /* !FREEBSD */ |
| 906 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 907 | rebuild_pollv(); |
| 908 | #endif |
| 909 | } |
| 910 | if (tcp->parent != NULL) { |
| 911 | tcp->parent->nchildren--; |
| 912 | tcp->parent = NULL; |
| 913 | } |
| 914 | #if 0 |
| 915 | if (tcp->outf != stderr) |
| 916 | fclose(tcp->outf); |
| 917 | #endif |
| 918 | tcp->outf = 0; |
| 919 | } |
| 920 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 921 | #ifndef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 922 | |
| 923 | static int |
| 924 | resume(tcp) |
| 925 | struct tcb *tcp; |
| 926 | { |
| 927 | if (tcp == NULL) |
| 928 | return -1; |
| 929 | |
| 930 | if (!(tcp->flags & TCB_SUSPENDED)) { |
| 931 | fprintf(stderr, "PANIC: pid %u not suspended\n", tcp->pid); |
| 932 | return -1; |
| 933 | } |
| 934 | tcp->flags &= ~TCB_SUSPENDED; |
| 935 | |
| 936 | if (ptrace(PTRACE_SYSCALL, tcp->pid, (char *) 1, 0) < 0) { |
| 937 | perror("resume: ptrace(PTRACE_SYSCALL, ...)"); |
| 938 | return -1; |
| 939 | } |
| 940 | |
| 941 | if (!qflag) |
| 942 | fprintf(stderr, "Process %u resumed\n", tcp->pid); |
| 943 | return 0; |
| 944 | } |
| 945 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 946 | #endif /* !USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 947 | |
| 948 | /* detach traced process; continue with sig */ |
| 949 | |
| 950 | static int |
| 951 | detach(tcp, sig) |
| 952 | struct tcb *tcp; |
| 953 | int sig; |
| 954 | { |
| 955 | int error = 0; |
| 956 | #ifdef LINUX |
| 957 | int status; |
| 958 | #endif |
| 959 | |
| 960 | if (tcp->flags & TCB_BPTSET) |
| 961 | sig = SIGKILL; |
| 962 | |
| 963 | #ifdef LINUX |
| 964 | /* |
| 965 | * Linux wrongly insists the child be stopped |
| 966 | * before detaching. Arghh. We go through hoops |
| 967 | * to make a clean break of things. |
| 968 | */ |
Wichert Akkerman | dacfb6e | 1999-06-03 14:21:07 +0000 | [diff] [blame] | 969 | #if defined(SPARC) |
| 970 | #undef PTRACE_DETACH |
| 971 | #define PTRACE_DETACH PTRACE_SUNDETACH |
| 972 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 973 | if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) { |
| 974 | /* On a clear day, you can see forever. */ |
| 975 | } |
| 976 | else if (errno != ESRCH) { |
| 977 | /* Shouldn't happen. */ |
| 978 | perror("detach: ptrace(PTRACE_DETACH, ...)"); |
| 979 | } |
| 980 | else if (kill(tcp->pid, 0) < 0) { |
| 981 | if (errno != ESRCH) |
| 982 | perror("detach: checking sanity"); |
| 983 | } |
| 984 | else if (kill(tcp->pid, SIGSTOP) < 0) { |
| 985 | if (errno != ESRCH) |
| 986 | perror("detach: stopping child"); |
| 987 | } |
| 988 | else { |
| 989 | for (;;) { |
| 990 | if (waitpid(tcp->pid, &status, 0) < 0) { |
| 991 | if (errno != ECHILD) |
| 992 | perror("detach: waiting"); |
| 993 | break; |
| 994 | } |
| 995 | if (!WIFSTOPPED(status)) { |
| 996 | /* Au revoir, mon ami. */ |
| 997 | break; |
| 998 | } |
| 999 | if (WSTOPSIG(status) == SIGSTOP) { |
| 1000 | if ((error = ptrace(PTRACE_DETACH, |
| 1001 | tcp->pid, (char *) 1, sig)) < 0) { |
| 1002 | if (errno != ESRCH) |
| 1003 | perror("detach: ptrace(PTRACE_DETACH, ...)"); |
| 1004 | /* I died trying. */ |
| 1005 | } |
| 1006 | break; |
| 1007 | } |
| 1008 | if ((error = ptrace(PTRACE_CONT, tcp->pid, (char *) 1, |
| 1009 | WSTOPSIG(status) == SIGTRAP ? |
| 1010 | 0 : WSTOPSIG(status))) < 0) { |
| 1011 | if (errno != ESRCH) |
| 1012 | perror("detach: ptrace(PTRACE_CONT, ...)"); |
| 1013 | break; |
| 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | #endif /* LINUX */ |
| 1018 | |
| 1019 | #if defined(SUNOS4) |
| 1020 | /* PTRACE_DETACH won't respect `sig' argument, so we post it here. */ |
| 1021 | if (sig && kill(tcp->pid, sig) < 0) |
| 1022 | perror("detach: kill"); |
| 1023 | sig = 0; |
| 1024 | if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) < 0) |
| 1025 | perror("detach: ptrace(PTRACE_DETACH, ...)"); |
| 1026 | #endif /* SUNOS4 */ |
| 1027 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 1028 | #ifndef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1029 | if (waiting_parent(tcp)) |
| 1030 | error = resume(tcp->parent); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 1031 | #endif /* !USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1032 | |
| 1033 | if (!qflag) |
| 1034 | fprintf(stderr, "Process %u detached\n", tcp->pid); |
| 1035 | |
| 1036 | droptcb(tcp); |
| 1037 | return error; |
| 1038 | } |
| 1039 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 1040 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1041 | |
| 1042 | static void |
| 1043 | reaper(sig) |
| 1044 | int sig; |
| 1045 | { |
| 1046 | int pid; |
| 1047 | int status; |
| 1048 | |
| 1049 | while ((pid = waitpid(-1, &status, WNOHANG)) > 0) { |
| 1050 | #if 0 |
| 1051 | struct tcb *tcp; |
| 1052 | |
| 1053 | tcp = pid2tcb(pid); |
| 1054 | if (tcp) |
| 1055 | droptcb(tcp); |
| 1056 | #endif |
| 1057 | } |
| 1058 | } |
| 1059 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 1060 | #endif /* USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1061 | |
| 1062 | static void |
| 1063 | cleanup() |
| 1064 | { |
| 1065 | int i; |
| 1066 | struct tcb *tcp; |
| 1067 | |
| 1068 | for (i = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) { |
| 1069 | if (!(tcp->flags & TCB_INUSE)) |
| 1070 | continue; |
| 1071 | if (debug) |
| 1072 | fprintf(stderr, |
| 1073 | "cleanup: looking at pid %u\n", tcp->pid); |
| 1074 | if (tcp_last && |
| 1075 | (!outfname || followfork < 2 || tcp_last == tcp)) { |
| 1076 | tprintf(" <unfinished ...>\n"); |
| 1077 | tcp_last = NULL; |
| 1078 | } |
| 1079 | if (tcp->flags & TCB_ATTACHED) |
| 1080 | detach(tcp, 0); |
| 1081 | else { |
| 1082 | kill(tcp->pid, SIGCONT); |
| 1083 | kill(tcp->pid, SIGTERM); |
| 1084 | } |
| 1085 | } |
| 1086 | if (cflag) |
| 1087 | call_summary(outf); |
| 1088 | } |
| 1089 | |
| 1090 | static void |
| 1091 | interrupt(sig) |
| 1092 | int sig; |
| 1093 | { |
| 1094 | interrupted = 1; |
| 1095 | } |
| 1096 | |
| 1097 | #ifndef HAVE_STRERROR |
| 1098 | |
| 1099 | #ifndef SYS_ERRLIST_DECLARED |
| 1100 | extern int sys_nerr; |
| 1101 | extern char *sys_errlist[]; |
| 1102 | #endif /* SYS_ERRLIST_DECLARED */ |
| 1103 | |
| 1104 | const char * |
| 1105 | strerror(errno) |
| 1106 | int errno; |
| 1107 | { |
| 1108 | static char buf[64]; |
| 1109 | |
| 1110 | if (errno < 1 || errno >= sys_nerr) { |
| 1111 | sprintf(buf, "Unknown error %d", errno); |
| 1112 | return buf; |
| 1113 | } |
| 1114 | return sys_errlist[errno]; |
| 1115 | } |
| 1116 | |
| 1117 | #endif /* HAVE_STERRROR */ |
| 1118 | |
| 1119 | #ifndef HAVE_STRSIGNAL |
| 1120 | |
| 1121 | #ifndef SYS_SIGLIST_DECLARED |
| 1122 | #ifdef HAVE__SYS_SIGLIST |
| 1123 | extern char *_sys_siglist[]; |
| 1124 | #else |
| 1125 | extern char *sys_siglist[]; |
| 1126 | #endif |
| 1127 | #endif /* SYS_SIGLIST_DECLARED */ |
| 1128 | |
| 1129 | const char * |
| 1130 | strsignal(sig) |
| 1131 | int sig; |
| 1132 | { |
| 1133 | static char buf[64]; |
| 1134 | |
| 1135 | if (sig < 1 || sig >= NSIG) { |
| 1136 | sprintf(buf, "Unknown signal %d", sig); |
| 1137 | return buf; |
| 1138 | } |
| 1139 | #ifdef HAVE__SYS_SIGLIST |
| 1140 | return _sys_siglist[sig]; |
| 1141 | #else |
| 1142 | return sys_siglist[sig]; |
| 1143 | #endif |
| 1144 | } |
| 1145 | |
| 1146 | #endif /* HAVE_STRSIGNAL */ |
| 1147 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 1148 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1149 | |
| 1150 | static void |
| 1151 | rebuild_pollv() |
| 1152 | { |
| 1153 | int i, j; |
| 1154 | struct tcb *tcp; |
| 1155 | |
| 1156 | for (i = j = 0, tcp = tcbtab; i < MAX_PROCS; i++, tcp++) { |
| 1157 | if (!(tcp->flags & TCB_INUSE)) |
| 1158 | continue; |
| 1159 | pollv[j].fd = tcp->pfd; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1160 | pollv[j].events = POLLWANT; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1161 | j++; |
| 1162 | } |
| 1163 | if (j != nprocs) { |
| 1164 | fprintf(stderr, "strace: proc miscount\n"); |
| 1165 | exit(1); |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | #ifndef HAVE_POLLABLE_PROCFS |
| 1170 | |
| 1171 | static void |
| 1172 | proc_poll_open() |
| 1173 | { |
| 1174 | int arg; |
| 1175 | int i; |
| 1176 | |
| 1177 | if (pipe(proc_poll_pipe) < 0) { |
| 1178 | perror("pipe"); |
| 1179 | exit(1); |
| 1180 | } |
| 1181 | for (i = 0; i < 2; i++) { |
| 1182 | if ((arg = fcntl(proc_poll_pipe[i], F_GETFD)) < 0) { |
| 1183 | perror("F_GETFD"); |
| 1184 | exit(1); |
| 1185 | } |
| 1186 | if (fcntl(proc_poll_pipe[i], F_SETFD, arg|FD_CLOEXEC) < 0) { |
| 1187 | perror("F_SETFD"); |
| 1188 | exit(1); |
| 1189 | } |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | static int |
| 1194 | proc_poll(pollv, nfds, timeout) |
| 1195 | struct pollfd *pollv; |
| 1196 | int nfds; |
| 1197 | int timeout; |
| 1198 | { |
| 1199 | int i; |
| 1200 | int n; |
| 1201 | struct proc_pollfd pollinfo; |
| 1202 | |
| 1203 | if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0) |
| 1204 | return n; |
| 1205 | if (n != sizeof(struct proc_pollfd)) { |
| 1206 | fprintf(stderr, "panic: short read: %d\n", n); |
| 1207 | exit(1); |
| 1208 | } |
| 1209 | for (i = 0; i < nprocs; i++) { |
| 1210 | if (pollv[i].fd == pollinfo.fd) |
| 1211 | pollv[i].revents = pollinfo.revents; |
| 1212 | else |
| 1213 | pollv[i].revents = 0; |
| 1214 | } |
| 1215 | poller_pid = pollinfo.pid; |
| 1216 | return 1; |
| 1217 | } |
| 1218 | |
| 1219 | static void |
| 1220 | wakeup_handler(sig) |
| 1221 | int sig; |
| 1222 | { |
| 1223 | } |
| 1224 | |
| 1225 | static void |
| 1226 | proc_poller(pfd) |
| 1227 | int pfd; |
| 1228 | { |
| 1229 | struct proc_pollfd pollinfo; |
| 1230 | struct sigaction sa; |
| 1231 | sigset_t blocked_set, empty_set; |
| 1232 | int i; |
| 1233 | int n; |
| 1234 | struct rlimit rl; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 1235 | #ifdef FREEBSD |
| 1236 | struct procfs_status pfs; |
| 1237 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1238 | |
| 1239 | switch (fork()) { |
| 1240 | case -1: |
| 1241 | perror("fork"); |
| 1242 | _exit(0); |
| 1243 | case 0: |
| 1244 | break; |
| 1245 | default: |
| 1246 | return; |
| 1247 | } |
| 1248 | |
| 1249 | sa.sa_handler = interactive ? SIG_DFL : SIG_IGN; |
| 1250 | sa.sa_flags = 0; |
| 1251 | sigemptyset(&sa.sa_mask); |
| 1252 | sigaction(SIGHUP, &sa, NULL); |
| 1253 | sigaction(SIGINT, &sa, NULL); |
| 1254 | sigaction(SIGQUIT, &sa, NULL); |
| 1255 | sigaction(SIGPIPE, &sa, NULL); |
| 1256 | sigaction(SIGTERM, &sa, NULL); |
| 1257 | sa.sa_handler = wakeup_handler; |
| 1258 | sigaction(SIGUSR1, &sa, NULL); |
| 1259 | sigemptyset(&blocked_set); |
| 1260 | sigaddset(&blocked_set, SIGUSR1); |
| 1261 | sigprocmask(SIG_BLOCK, &blocked_set, NULL); |
| 1262 | sigemptyset(&empty_set); |
| 1263 | |
| 1264 | if (getrlimit(RLIMIT_NOFILE, &rl) < 0) { |
| 1265 | perror("getrlimit(RLIMIT_NOFILE, ...)"); |
| 1266 | _exit(0); |
| 1267 | } |
| 1268 | n = rl.rlim_cur; |
| 1269 | for (i = 0; i < n; i++) { |
| 1270 | if (i != pfd && i != proc_poll_pipe[1]) |
| 1271 | close(i); |
| 1272 | } |
| 1273 | |
| 1274 | pollinfo.fd = pfd; |
| 1275 | pollinfo.pid = getpid(); |
| 1276 | for (;;) { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 1277 | #ifndef FREEBSD |
| 1278 | if (ioctl(pfd, PIOCWSTOP, NULL) < 0) |
| 1279 | #else /* FREEBSD */ |
| 1280 | if (ioctl(pfd, PIOCWSTOP, &pfs) < 0) |
| 1281 | #endif /* FREEBSD */ |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1282 | { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1283 | switch (errno) { |
| 1284 | case EINTR: |
| 1285 | continue; |
| 1286 | case EBADF: |
| 1287 | pollinfo.revents = POLLERR; |
| 1288 | break; |
| 1289 | case ENOENT: |
| 1290 | pollinfo.revents = POLLHUP; |
| 1291 | break; |
| 1292 | default: |
| 1293 | perror("proc_poller: PIOCWSTOP"); |
| 1294 | } |
| 1295 | write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo)); |
| 1296 | _exit(0); |
| 1297 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1298 | pollinfo.revents = POLLWANT; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1299 | write(proc_poll_pipe[1], &pollinfo, sizeof(pollinfo)); |
| 1300 | sigsuspend(&empty_set); |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 1305 | |
| 1306 | static int |
| 1307 | choose_pfd() |
| 1308 | { |
| 1309 | int i, j; |
| 1310 | struct tcb *tcp; |
| 1311 | |
| 1312 | static int last; |
| 1313 | |
| 1314 | if (followfork < 2 && |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1315 | last < nprocs && (pollv[last].revents & POLLWANT)) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1316 | /* |
| 1317 | * The previous process is ready to run again. We'll |
| 1318 | * let it do so if it is currently in a syscall. This |
| 1319 | * heuristic improves the readability of the trace. |
| 1320 | */ |
| 1321 | tcp = pfd2tcb(pollv[last].fd); |
| 1322 | if (tcp && (tcp->flags & TCB_INSYSCALL)) |
| 1323 | return pollv[last].fd; |
| 1324 | } |
| 1325 | |
| 1326 | for (i = 0; i < nprocs; i++) { |
| 1327 | /* Let competing children run round robin. */ |
| 1328 | j = (i + last + 1) % nprocs; |
| 1329 | if (pollv[j].revents & (POLLHUP | POLLERR)) { |
| 1330 | tcp = pfd2tcb(pollv[j].fd); |
| 1331 | if (!tcp) { |
| 1332 | fprintf(stderr, "strace: lost proc\n"); |
| 1333 | exit(1); |
| 1334 | } |
| 1335 | droptcb(tcp); |
| 1336 | return -1; |
| 1337 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1338 | if (pollv[j].revents & POLLWANT) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1339 | last = j; |
| 1340 | return pollv[j].fd; |
| 1341 | } |
| 1342 | } |
| 1343 | fprintf(stderr, "strace: nothing ready\n"); |
| 1344 | exit(1); |
| 1345 | } |
| 1346 | |
| 1347 | static int |
| 1348 | trace() |
| 1349 | { |
Wichert Akkerman | 9dbf154 | 1999-11-26 13:11:29 +0000 | [diff] [blame] | 1350 | #ifdef POLL_HACK |
| 1351 | struct tcb *in_syscall; |
| 1352 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1353 | struct tcb *tcp; |
| 1354 | int pfd; |
| 1355 | int what; |
| 1356 | int ioctl_result = 0, ioctl_errno = 0; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1357 | long arg; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1358 | |
| 1359 | for (;;) { |
| 1360 | if (interactive) |
| 1361 | sigprocmask(SIG_SETMASK, &empty_set, NULL); |
| 1362 | |
| 1363 | if (nprocs == 0) |
| 1364 | break; |
| 1365 | |
| 1366 | switch (nprocs) { |
| 1367 | case 1: |
| 1368 | #ifndef HAVE_POLLABLE_PROCFS |
| 1369 | if (proc_poll_pipe[0] == -1) { |
| 1370 | #endif |
| 1371 | tcp = pid2tcb(0); |
| 1372 | if (!tcp) |
| 1373 | continue; |
| 1374 | pfd = tcp->pfd; |
| 1375 | if (pfd == -1) |
| 1376 | continue; |
| 1377 | break; |
| 1378 | #ifndef HAVE_POLLABLE_PROCFS |
| 1379 | } |
| 1380 | /* fall through ... */ |
| 1381 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 1382 | default: |
| 1383 | #ifdef HAVE_POLLABLE_PROCFS |
Wichert Akkerman | 9dbf154 | 1999-11-26 13:11:29 +0000 | [diff] [blame] | 1384 | #ifdef POLL_HACK |
| 1385 | /* On some systems (e.g. UnixWare) we get too much ugly |
| 1386 | "unfinished..." stuff when multiple proceses are in |
| 1387 | syscalls. Here's a nasty hack */ |
| 1388 | |
| 1389 | if (in_syscall) { |
| 1390 | struct pollfd pv; |
| 1391 | tcp = in_syscall; |
| 1392 | in_syscall = NULL; |
| 1393 | pv.fd = tcp->pfd; |
| 1394 | pv.events = POLLWANT; |
| 1395 | if ((what = poll (&pv, 1, 1)) < 0) { |
| 1396 | if (interrupted) |
| 1397 | return 0; |
| 1398 | continue; |
| 1399 | } |
| 1400 | else if (what == 1 && pv.revents & POLLWANT) { |
| 1401 | goto FOUND; |
| 1402 | } |
| 1403 | } |
| 1404 | #endif |
| 1405 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1406 | if (poll(pollv, nprocs, INFTIM) < 0) { |
| 1407 | if (interrupted) |
| 1408 | return 0; |
| 1409 | continue; |
| 1410 | } |
| 1411 | #else /* !HAVE_POLLABLE_PROCFS */ |
| 1412 | if (proc_poll(pollv, nprocs, INFTIM) < 0) { |
| 1413 | if (interrupted) |
| 1414 | return 0; |
| 1415 | continue; |
| 1416 | } |
| 1417 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 1418 | pfd = choose_pfd(); |
| 1419 | if (pfd == -1) |
| 1420 | continue; |
| 1421 | break; |
| 1422 | } |
| 1423 | |
| 1424 | /* Look up `pfd' in our table. */ |
| 1425 | if ((tcp = pfd2tcb(pfd)) == NULL) { |
| 1426 | fprintf(stderr, "unknown pfd: %u\n", pfd); |
| 1427 | exit(1); |
| 1428 | } |
Wichert Akkerman | 9dbf154 | 1999-11-26 13:11:29 +0000 | [diff] [blame] | 1429 | FOUND: |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1430 | /* Get the status of the process. */ |
| 1431 | if (!interrupted) { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 1432 | #ifndef FREEBSD |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1433 | ioctl_result = IOCTL_WSTOP (tcp); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 1434 | #else /* FREEBSD */ |
| 1435 | /* Thanks to some scheduling mystery, the first poller |
| 1436 | sometimes waits for the already processed end of fork |
| 1437 | event. Doing a non blocking poll here solves the problem. */ |
| 1438 | if (proc_poll_pipe[0] != -1) |
| 1439 | ioctl_result = IOCTL_STATUS (tcp); |
| 1440 | else |
| 1441 | ioctl_result = IOCTL_WSTOP (tcp); |
| 1442 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1443 | ioctl_errno = errno; |
| 1444 | #ifndef HAVE_POLLABLE_PROCFS |
| 1445 | if (proc_poll_pipe[0] != -1) { |
| 1446 | if (ioctl_result < 0) |
| 1447 | kill(poller_pid, SIGKILL); |
| 1448 | else |
| 1449 | kill(poller_pid, SIGUSR1); |
| 1450 | } |
| 1451 | #endif /* !HAVE_POLLABLE_PROCFS */ |
| 1452 | } |
| 1453 | if (interrupted) |
| 1454 | return 0; |
| 1455 | |
| 1456 | if (interactive) |
| 1457 | sigprocmask(SIG_BLOCK, &blocked_set, NULL); |
| 1458 | |
| 1459 | if (ioctl_result < 0) { |
| 1460 | /* Find out what happened if it failed. */ |
| 1461 | switch (ioctl_errno) { |
| 1462 | case EINTR: |
| 1463 | case EBADF: |
| 1464 | continue; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 1465 | #ifdef FREEBSD |
| 1466 | case ENOTTY: |
| 1467 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1468 | case ENOENT: |
| 1469 | droptcb(tcp); |
| 1470 | continue; |
| 1471 | default: |
| 1472 | perror("PIOCWSTOP"); |
| 1473 | exit(1); |
| 1474 | } |
| 1475 | } |
| 1476 | |
| 1477 | /* clear the just started flag */ |
| 1478 | tcp->flags &= ~TCB_STARTUP; |
| 1479 | |
| 1480 | /* set current output file */ |
| 1481 | outf = tcp->outf; |
| 1482 | |
| 1483 | if (cflag) { |
| 1484 | struct timeval stime; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 1485 | #ifdef FREEBSD |
| 1486 | char buf[1024]; |
| 1487 | int len; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1488 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 1489 | if ((len = pread(tcp->pfd_status, buf, sizeof(buf) - 1, 0)) > 0) { |
| 1490 | buf[len] = '\0'; |
| 1491 | sscanf(buf, |
| 1492 | "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d %*d,%*d %ld,%ld", |
| 1493 | &stime.tv_sec, &stime.tv_usec); |
| 1494 | } else |
| 1495 | stime.tv_sec = stime.tv_usec = 0; |
| 1496 | #else /* !FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1497 | stime.tv_sec = tcp->status.pr_stime.tv_sec; |
| 1498 | stime.tv_usec = tcp->status.pr_stime.tv_nsec/1000; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 1499 | #endif /* !FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1500 | tv_sub(&tcp->dtime, &stime, &tcp->stime); |
| 1501 | tcp->stime = stime; |
| 1502 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1503 | what = tcp->status.PR_WHAT; |
| 1504 | switch (tcp->status.PR_WHY) { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 1505 | #ifndef FREEBSD |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1506 | case PR_REQUESTED: |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1507 | if (tcp->status.PR_FLAGS & PR_ASLEEP) { |
| 1508 | tcp->status.PR_WHY = PR_SYSENTRY; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1509 | if (trace_syscall(tcp) < 0) { |
| 1510 | fprintf(stderr, "syscall trouble\n"); |
| 1511 | exit(1); |
| 1512 | } |
| 1513 | } |
| 1514 | break; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 1515 | #endif /* !FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1516 | case PR_SYSENTRY: |
Wichert Akkerman | 9dbf154 | 1999-11-26 13:11:29 +0000 | [diff] [blame] | 1517 | #ifdef POLL_HACK |
| 1518 | in_syscall = tcp; |
| 1519 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1520 | case PR_SYSEXIT: |
| 1521 | if (trace_syscall(tcp) < 0) { |
| 1522 | fprintf(stderr, "syscall trouble\n"); |
| 1523 | exit(1); |
| 1524 | } |
| 1525 | break; |
| 1526 | case PR_SIGNALLED: |
| 1527 | if (!cflag && (qual_flags[what] & QUAL_SIGNAL)) { |
| 1528 | printleader(tcp); |
| 1529 | tprintf("--- %s (%s) ---", |
Nate Sammons | ce780fc | 1999-03-29 23:23:13 +0000 | [diff] [blame] | 1530 | signame(what), strsignal(what)); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1531 | printtrailer(tcp); |
| 1532 | } |
| 1533 | break; |
| 1534 | case PR_FAULTED: |
| 1535 | if (!cflag && (qual_flags[what] & QUAL_FAULT)) { |
| 1536 | printleader(tcp); |
| 1537 | tprintf("=== FAULT %d ===", what); |
| 1538 | printtrailer(tcp); |
| 1539 | } |
| 1540 | break; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 1541 | #ifdef FREEBSD |
| 1542 | case 0: /* handle case we polled for nothing */ |
| 1543 | continue; |
| 1544 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1545 | default: |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1546 | fprintf(stderr, "odd stop %d\n", tcp->status.PR_WHY); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1547 | exit(1); |
| 1548 | break; |
| 1549 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1550 | arg = 0; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 1551 | #ifndef FREEBSD |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1552 | if (IOCTL (tcp->pfd, PIOCRUN, &arg) < 0) { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 1553 | #else |
| 1554 | if (IOCTL (tcp->pfd, PIOCRUN, 0) < 0) { |
| 1555 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1556 | perror("PIOCRUN"); |
| 1557 | exit(1); |
| 1558 | } |
| 1559 | } |
| 1560 | return 0; |
| 1561 | } |
| 1562 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 1563 | #else /* !USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1564 | |
| 1565 | static int |
| 1566 | trace() |
| 1567 | { |
| 1568 | int pid; |
| 1569 | int wait_errno; |
| 1570 | int status; |
| 1571 | struct tcb *tcp; |
| 1572 | #ifdef LINUX |
| 1573 | struct rusage ru; |
| 1574 | #endif /* LINUX */ |
| 1575 | |
| 1576 | while (nprocs != 0) { |
| 1577 | if (interactive) |
| 1578 | sigprocmask(SIG_SETMASK, &empty_set, NULL); |
| 1579 | #ifdef LINUX |
| 1580 | pid = wait4(-1, &status, 0, cflag ? &ru : NULL); |
| 1581 | #endif /* LINUX */ |
| 1582 | #ifdef SUNOS4 |
| 1583 | pid = wait(&status); |
| 1584 | #endif /* SUNOS4 */ |
| 1585 | wait_errno = errno; |
| 1586 | if (interactive) |
| 1587 | sigprocmask(SIG_BLOCK, &blocked_set, NULL); |
| 1588 | |
| 1589 | if (interrupted) |
| 1590 | return 0; |
| 1591 | |
| 1592 | if (pid == -1) { |
| 1593 | switch (wait_errno) { |
| 1594 | case EINTR: |
| 1595 | continue; |
| 1596 | case ECHILD: |
| 1597 | /* |
| 1598 | * We would like to verify this case |
| 1599 | * but sometimes a race in Solbourne's |
| 1600 | * version of SunOS sometimes reports |
| 1601 | * ECHILD before sending us SIGCHILD. |
| 1602 | */ |
| 1603 | #if 0 |
| 1604 | if (nprocs == 0) |
| 1605 | return 0; |
| 1606 | fprintf(stderr, "strace: proc miscount\n"); |
| 1607 | exit(1); |
| 1608 | #endif |
| 1609 | return 0; |
| 1610 | default: |
| 1611 | errno = wait_errno; |
| 1612 | perror("strace: wait"); |
| 1613 | return -1; |
| 1614 | } |
| 1615 | } |
| 1616 | if (debug) |
| 1617 | fprintf(stderr, " [wait(%#x) = %u]\n", status, pid); |
| 1618 | |
| 1619 | /* Look up `pid' in our table. */ |
| 1620 | if ((tcp = pid2tcb(pid)) == NULL) { |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1621 | #if 0 /* XXX davidm */ /* WTA: disabled again */ |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1622 | struct tcb *tcpchild; |
| 1623 | |
| 1624 | if ((tcpchild = alloctcb(pid)) == NULL) { |
| 1625 | fprintf(stderr, " [tcb table full]\n"); |
| 1626 | kill(pid, SIGKILL); /* XXX */ |
| 1627 | return 0; |
| 1628 | } |
| 1629 | tcpchild->flags |= TCB_ATTACHED; |
| 1630 | newoutf(tcpchild); |
| 1631 | tcp->nchildren++; |
| 1632 | if (!qflag) |
| 1633 | fprintf(stderr, "Process %d attached\n", pid); |
| 1634 | #else |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1635 | fprintf(stderr, "unknown pid: %u\n", pid); |
| 1636 | if (WIFSTOPPED(status)) |
| 1637 | ptrace(PTRACE_CONT, pid, (char *) 1, 0); |
| 1638 | exit(1); |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1639 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1640 | } |
| 1641 | /* set current output file */ |
| 1642 | outf = tcp->outf; |
| 1643 | if (cflag) { |
| 1644 | #ifdef LINUX |
| 1645 | tv_sub(&tcp->dtime, &ru.ru_stime, &tcp->stime); |
| 1646 | tcp->stime = ru.ru_stime; |
| 1647 | #endif /* !LINUX */ |
| 1648 | } |
| 1649 | |
| 1650 | if (tcp->flags & TCB_SUSPENDED) { |
| 1651 | /* |
| 1652 | * Apparently, doing any ptrace() call on a stopped |
| 1653 | * process, provokes the kernel to report the process |
| 1654 | * status again on a subsequent wait(), even if the |
| 1655 | * process has not been actually restarted. |
| 1656 | * Since we have inspected the arguments of suspended |
| 1657 | * processes we end up here testing for this case. |
| 1658 | */ |
| 1659 | continue; |
| 1660 | } |
| 1661 | if (WIFSIGNALED(status)) { |
| 1662 | if (!cflag |
| 1663 | && (qual_flags[WTERMSIG(status)] & QUAL_SIGNAL)) { |
| 1664 | printleader(tcp); |
| 1665 | tprintf("+++ killed by %s +++", |
Nate Sammons | ce780fc | 1999-03-29 23:23:13 +0000 | [diff] [blame] | 1666 | signame(WTERMSIG(status))); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1667 | printtrailer(tcp); |
| 1668 | } |
| 1669 | droptcb(tcp); |
| 1670 | continue; |
| 1671 | } |
| 1672 | if (WIFEXITED(status)) { |
| 1673 | if (debug) |
| 1674 | fprintf(stderr, "pid %u exited\n", pid); |
| 1675 | if (tcp->flags & TCB_ATTACHED) |
| 1676 | fprintf(stderr, |
| 1677 | "PANIC: attached pid %u exited\n", |
| 1678 | pid); |
| 1679 | droptcb(tcp); |
| 1680 | continue; |
| 1681 | } |
| 1682 | if (!WIFSTOPPED(status)) { |
| 1683 | fprintf(stderr, "PANIC: pid %u not stopped\n", pid); |
| 1684 | droptcb(tcp); |
| 1685 | continue; |
| 1686 | } |
| 1687 | if (debug) |
| 1688 | fprintf(stderr, "pid %u stopped, [%s]\n", |
Nate Sammons | ce780fc | 1999-03-29 23:23:13 +0000 | [diff] [blame] | 1689 | pid, signame(WSTOPSIG(status))); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1690 | |
| 1691 | if (tcp->flags & TCB_STARTUP) { |
| 1692 | /* |
| 1693 | * This flag is there to keep us in sync. |
| 1694 | * Next time this process stops it should |
| 1695 | * really be entering a system call. |
| 1696 | */ |
| 1697 | tcp->flags &= ~TCB_STARTUP; |
| 1698 | if (tcp->flags & TCB_ATTACHED) { |
| 1699 | /* |
| 1700 | * Interestingly, the process may stop |
| 1701 | * with STOPSIG equal to some other signal |
| 1702 | * than SIGSTOP if we happend to attach |
| 1703 | * just before the process takes a signal. |
| 1704 | */ |
| 1705 | if (!WIFSTOPPED(status)) { |
| 1706 | fprintf(stderr, |
| 1707 | "pid %u not stopped\n", pid); |
| 1708 | detach(tcp, WSTOPSIG(status)); |
| 1709 | continue; |
| 1710 | } |
| 1711 | } |
| 1712 | else { |
| 1713 | #ifdef SUNOS4 |
| 1714 | /* A child of us stopped at exec */ |
| 1715 | if (WSTOPSIG(status) == SIGTRAP && followvfork) |
| 1716 | fixvfork(tcp); |
| 1717 | #endif /* SUNOS4 */ |
| 1718 | } |
| 1719 | if (tcp->flags & TCB_BPTSET) { |
| 1720 | if (clearbpt(tcp) < 0) /* Pretty fatal */ { |
| 1721 | droptcb(tcp); |
| 1722 | cleanup(); |
| 1723 | return -1; |
| 1724 | } |
| 1725 | } |
| 1726 | goto tracing; |
| 1727 | } |
| 1728 | |
| 1729 | if (WSTOPSIG(status) != SIGTRAP) { |
| 1730 | if (WSTOPSIG(status) == SIGSTOP && |
| 1731 | (tcp->flags & TCB_SIGTRAPPED)) { |
| 1732 | /* |
| 1733 | * Trapped attempt to block SIGTRAP |
| 1734 | * Hope we are back in control now. |
| 1735 | */ |
| 1736 | tcp->flags &= ~(TCB_INSYSCALL | TCB_SIGTRAPPED); |
| 1737 | if (ptrace(PTRACE_SYSCALL, |
| 1738 | pid, (char *) 1, 0) < 0) { |
| 1739 | perror("trace: ptrace(PTRACE_SYSCALL, ...)"); |
| 1740 | cleanup(); |
| 1741 | return -1; |
| 1742 | } |
| 1743 | continue; |
| 1744 | } |
| 1745 | if (!cflag |
| 1746 | && (qual_flags[WSTOPSIG(status)] & QUAL_SIGNAL)) { |
| 1747 | printleader(tcp); |
| 1748 | tprintf("--- %s (%s) ---", |
Nate Sammons | ce780fc | 1999-03-29 23:23:13 +0000 | [diff] [blame] | 1749 | signame(WSTOPSIG(status)), |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1750 | strsignal(WSTOPSIG(status))); |
| 1751 | printtrailer(tcp); |
| 1752 | } |
| 1753 | if ((tcp->flags & TCB_ATTACHED) && |
| 1754 | !sigishandled(tcp, WSTOPSIG(status))) { |
| 1755 | detach(tcp, WSTOPSIG(status)); |
| 1756 | continue; |
| 1757 | } |
| 1758 | if (ptrace(PTRACE_SYSCALL, pid, (char *) 1, |
| 1759 | WSTOPSIG(status)) < 0) { |
| 1760 | perror("trace: ptrace(PTRACE_SYSCALL, ...)"); |
| 1761 | cleanup(); |
| 1762 | return -1; |
| 1763 | } |
| 1764 | tcp->flags &= ~TCB_SUSPENDED; |
| 1765 | continue; |
| 1766 | } |
| 1767 | if (trace_syscall(tcp) < 0) { |
| 1768 | if (tcp->flags & TCB_ATTACHED) |
| 1769 | detach(tcp, 0); |
| 1770 | else { |
| 1771 | ptrace(PTRACE_KILL, |
| 1772 | tcp->pid, (char *) 1, SIGTERM); |
| 1773 | droptcb(tcp); |
| 1774 | } |
| 1775 | continue; |
| 1776 | } |
| 1777 | if (tcp->flags & TCB_EXITING) { |
| 1778 | if (tcp->flags & TCB_ATTACHED) |
| 1779 | detach(tcp, 0); |
| 1780 | else if (ptrace(PTRACE_CONT, pid, (char *) 1, 0) < 0) { |
| 1781 | perror("strace: ptrace(PTRACE_CONT, ...)"); |
| 1782 | cleanup(); |
| 1783 | return -1; |
| 1784 | } |
| 1785 | continue; |
| 1786 | } |
| 1787 | if (tcp->flags & TCB_SUSPENDED) { |
| 1788 | if (!qflag) |
| 1789 | fprintf(stderr, "Process %u suspended\n", pid); |
| 1790 | continue; |
| 1791 | } |
| 1792 | tracing: |
| 1793 | if (ptrace(PTRACE_SYSCALL, pid, (char *) 1, 0) < 0) { |
| 1794 | perror("trace: ptrace(PTRACE_SYSCALL, ...)"); |
| 1795 | cleanup(); |
| 1796 | return -1; |
| 1797 | } |
| 1798 | } |
| 1799 | return 0; |
| 1800 | } |
| 1801 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame^] | 1802 | #endif /* !USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1803 | |
| 1804 | static int curcol; |
| 1805 | |
| 1806 | #ifdef __STDC__ |
| 1807 | #include <stdarg.h> |
| 1808 | #define VA_START(a, b) va_start(a, b) |
| 1809 | #else |
| 1810 | #include <varargs.h> |
| 1811 | #define VA_START(a, b) va_start(a) |
| 1812 | #endif |
| 1813 | |
| 1814 | void |
| 1815 | #ifdef __STDC__ |
| 1816 | tprintf(const char *fmt, ...) |
| 1817 | #else |
| 1818 | tprintf(fmt, va_alist) |
| 1819 | char *fmt; |
| 1820 | va_dcl |
| 1821 | #endif |
| 1822 | { |
| 1823 | va_list args; |
| 1824 | |
| 1825 | VA_START(args, fmt); |
| 1826 | if (outf) |
| 1827 | curcol += vfprintf(outf, fmt, args); |
| 1828 | va_end(args); |
| 1829 | return; |
| 1830 | } |
| 1831 | |
| 1832 | void |
| 1833 | printleader(tcp) |
| 1834 | struct tcb *tcp; |
| 1835 | { |
| 1836 | if (tcp_last && (!outfname || followfork < 2 || tcp_last == tcp)) { |
| 1837 | tcp_last->flags |= TCB_REPRINT; |
| 1838 | tprintf(" <unfinished ...>\n"); |
| 1839 | } |
| 1840 | curcol = 0; |
| 1841 | if ((followfork == 1 || pflag_seen > 1) && outfname) |
| 1842 | tprintf("%-5d ", tcp->pid); |
| 1843 | else if (nprocs > 1 && !outfname) |
| 1844 | tprintf("[pid %5u] ", tcp->pid); |
| 1845 | if (tflag) { |
| 1846 | char str[sizeof("HH:MM:SS")]; |
| 1847 | struct timeval tv, dtv; |
| 1848 | static struct timeval otv; |
| 1849 | |
| 1850 | gettimeofday(&tv, NULL); |
| 1851 | if (rflag) { |
| 1852 | if (otv.tv_sec == 0) |
| 1853 | otv = tv; |
| 1854 | tv_sub(&dtv, &tv, &otv); |
| 1855 | tprintf("%6ld.%06ld ", |
| 1856 | (long) dtv.tv_sec, (long) dtv.tv_usec); |
| 1857 | otv = tv; |
| 1858 | } |
| 1859 | else if (tflag > 2) { |
| 1860 | tprintf("%ld.%06ld ", |
| 1861 | (long) tv.tv_sec, (long) tv.tv_usec); |
| 1862 | } |
| 1863 | else { |
| 1864 | time_t local = tv.tv_sec; |
| 1865 | strftime(str, sizeof(str), "%T", localtime(&local)); |
| 1866 | if (tflag > 1) |
| 1867 | tprintf("%s.%06ld ", str, (long) tv.tv_usec); |
| 1868 | else |
| 1869 | tprintf("%s ", str); |
| 1870 | } |
| 1871 | } |
| 1872 | if (iflag) |
| 1873 | printcall(tcp); |
| 1874 | } |
| 1875 | |
| 1876 | void |
| 1877 | tabto(col) |
| 1878 | int col; |
| 1879 | { |
| 1880 | if (curcol < col) |
| 1881 | tprintf("%*s", col - curcol, ""); |
| 1882 | } |
| 1883 | |
| 1884 | void |
| 1885 | printtrailer(tcp) |
| 1886 | struct tcb *tcp; |
| 1887 | { |
| 1888 | tprintf("\n"); |
| 1889 | tcp_last = NULL; |
| 1890 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1891 | |
Wichert Akkerman | ea78f0f | 1999-11-29 15:34:02 +0000 | [diff] [blame] | 1892 | #ifdef HAVE_MP_PROCFS |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1893 | |
| 1894 | int mp_ioctl (int fd, int cmd, void *arg, int size) { |
| 1895 | |
| 1896 | struct iovec iov[2]; |
| 1897 | int n = 1; |
| 1898 | |
| 1899 | iov[0].iov_base = &cmd; |
| 1900 | iov[0].iov_len = sizeof cmd; |
| 1901 | if (arg) { |
| 1902 | ++n; |
| 1903 | iov[1].iov_base = arg; |
| 1904 | iov[1].iov_len = size; |
| 1905 | } |
| 1906 | |
| 1907 | return writev (fd, iov, n); |
| 1908 | } |
| 1909 | |
| 1910 | #endif |