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