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