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