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> |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * 3. The name of the author may not be used to endorse or promote products |
| 16 | * derived from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | * |
| 29 | * $Id$ |
| 30 | */ |
| 31 | |
| 32 | #include "defs.h" |
| 33 | |
| 34 | #include <signal.h> |
| 35 | #include <time.h> |
| 36 | #include <errno.h> |
| 37 | #include <sys/user.h> |
| 38 | #include <sys/syscall.h> |
| 39 | #include <sys/param.h> |
Wichert Akkerman | 9047076 | 1999-03-17 00:42:25 +0000 | [diff] [blame] | 40 | #if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 1 && (defined(I386) || defined(M68K)) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 41 | # include <sys/reg.h> |
| 42 | #endif |
| 43 | |
Ulrich Drepper | 8783c01 | 1999-05-29 04:13:58 +0000 | [diff] [blame] | 44 | #if defined LINUX && __GLIBC__ < 2 |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 45 | #include <linux/ptrace.h> |
| 46 | #endif /* LINUX */ |
| 47 | |
| 48 | #ifndef SYS_ERRLIST_DECLARED |
| 49 | extern int sys_nerr; |
| 50 | extern char *sys_errlist[]; |
| 51 | #endif /* SYS_ERRLIST_DECLARED */ |
| 52 | |
| 53 | #ifdef LINUX |
| 54 | #ifndef ERESTARTSYS |
| 55 | #define ERESTARTSYS 512 |
| 56 | #endif |
| 57 | #ifndef ERESTARTNOINTR |
| 58 | #define ERESTARTNOINTR 513 |
| 59 | #endif |
| 60 | #ifndef ERESTARTNOHAND |
| 61 | #define ERESTARTNOHAND 514 /* restart if no handler.. */ |
| 62 | #endif |
| 63 | #ifndef ENOIOCTLCMD |
| 64 | #define ENOIOCTLCMD 515 /* No ioctl command */ |
| 65 | #endif |
| 66 | #ifndef NSIG |
| 67 | #define NSIG 32 |
| 68 | #endif |
| 69 | #ifdef ARM |
| 70 | #undef NSIG |
| 71 | #define NSIG 32 |
| 72 | #endif |
| 73 | #endif /* LINUX */ |
| 74 | |
| 75 | #include "syscall.h" |
| 76 | |
| 77 | /* Define these shorthand notations to simplify the syscallent files. */ |
| 78 | #define TF TRACE_FILE |
| 79 | #define TI TRACE_IPC |
| 80 | #define TN TRACE_NETWORK |
| 81 | #define TP TRACE_PROCESS |
| 82 | #define TS TRACE_SIGNAL |
| 83 | |
| 84 | struct sysent sysent0[] = { |
| 85 | #include "syscallent.h" |
| 86 | }; |
| 87 | int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0]; |
| 88 | |
| 89 | #if SUPPORTED_PERSONALITIES >= 2 |
| 90 | struct sysent sysent1[] = { |
| 91 | #include "syscallent1.h" |
| 92 | }; |
| 93 | int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0]; |
| 94 | #endif /* SUPPORTED_PERSONALITIES >= 2 */ |
| 95 | |
| 96 | #if SUPPORTED_PERSONALITIES >= 3 |
| 97 | struct sysent sysent2[] = { |
| 98 | #include "syscallent2.h" |
| 99 | }; |
| 100 | int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0]; |
| 101 | #endif /* SUPPORTED_PERSONALITIES >= 3 */ |
| 102 | |
| 103 | struct sysent *sysent; |
| 104 | int nsyscalls; |
| 105 | |
| 106 | /* Now undef them since short defines cause wicked namespace pollution. */ |
| 107 | #undef TF |
| 108 | #undef TI |
| 109 | #undef TN |
| 110 | #undef TP |
| 111 | #undef TS |
| 112 | |
| 113 | char *errnoent0[] = { |
| 114 | #include "errnoent.h" |
| 115 | }; |
| 116 | int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0]; |
| 117 | |
| 118 | #if SUPPORTED_PERSONALITIES >= 2 |
| 119 | char *errnoent1[] = { |
| 120 | #include "errnoent1.h" |
| 121 | }; |
| 122 | int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0]; |
| 123 | #endif /* SUPPORTED_PERSONALITIES >= 2 */ |
| 124 | |
| 125 | #if SUPPORTED_PERSONALITIES >= 3 |
| 126 | char *errnoent2[] = { |
| 127 | #include "errnoent2.h" |
| 128 | }; |
| 129 | int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0]; |
| 130 | #endif /* SUPPORTED_PERSONALITIES >= 3 */ |
| 131 | |
| 132 | char **errnoent; |
| 133 | int nerrnos; |
| 134 | |
| 135 | int current_personality; |
| 136 | |
| 137 | int |
| 138 | set_personality(int personality) |
| 139 | { |
| 140 | switch (personality) { |
| 141 | case 0: |
| 142 | errnoent = errnoent0; |
| 143 | nerrnos = nerrnos0; |
| 144 | sysent = sysent0; |
| 145 | nsyscalls = nsyscalls0; |
| 146 | ioctlent = ioctlent0; |
| 147 | nioctlents = nioctlents0; |
| 148 | signalent = signalent0; |
| 149 | nsignals = nsignals0; |
| 150 | break; |
| 151 | |
| 152 | #if SUPPORTED_PERSONALITIES >= 2 |
| 153 | case 1: |
| 154 | errnoent = errnoent1; |
| 155 | nerrnos = nerrnos1; |
| 156 | sysent = sysent1; |
| 157 | nsyscalls = nsyscalls1; |
| 158 | ioctlent = ioctlent1; |
| 159 | nioctlents = nioctlents1; |
| 160 | signalent = signalent1; |
| 161 | nsignals = nsignals1; |
| 162 | break; |
| 163 | #endif /* SUPPORTED_PERSONALITIES >= 2 */ |
| 164 | |
| 165 | #if SUPPORTED_PERSONALITIES >= 3 |
| 166 | case 2: |
| 167 | errnoent = errnoent2; |
| 168 | nerrnos = nerrnos2; |
| 169 | sysent = sysent2; |
| 170 | nsyscalls = nsyscalls2; |
| 171 | ioctlent = ioctlent2; |
| 172 | nioctlents = nioctlents2; |
| 173 | signalent = signalent2; |
| 174 | nsignals = nsignals2; |
| 175 | break; |
| 176 | #endif /* SUPPORTED_PERSONALITIES >= 3 */ |
| 177 | |
| 178 | default: |
| 179 | return -1; |
| 180 | } |
| 181 | |
| 182 | current_personality = personality; |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | int qual_flags[MAX_QUALS]; |
| 187 | |
| 188 | static int call_count[MAX_QUALS]; |
| 189 | static int error_count[MAX_QUALS]; |
| 190 | static struct timeval tv_count[MAX_QUALS]; |
| 191 | static int sorted_count[MAX_QUALS]; |
| 192 | |
| 193 | static struct timeval shortest = { 1000000, 0 }; |
| 194 | |
| 195 | static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc(); |
| 196 | |
| 197 | static struct qual_options { |
| 198 | int bitflag; |
| 199 | char *option_name; |
| 200 | int (*lookup)(); |
| 201 | char *argument_name; |
| 202 | } qual_options[] = { |
| 203 | { QUAL_TRACE, "trace", lookup_syscall, "system call" }, |
| 204 | { QUAL_TRACE, "t", lookup_syscall, "system call" }, |
| 205 | { QUAL_ABBREV, "abbrev", lookup_syscall, "system call" }, |
| 206 | { QUAL_ABBREV, "a", lookup_syscall, "system call" }, |
| 207 | { QUAL_VERBOSE, "verbose", lookup_syscall, "system call" }, |
| 208 | { QUAL_VERBOSE, "v", lookup_syscall, "system call" }, |
| 209 | { QUAL_RAW, "raw", lookup_syscall, "system call" }, |
| 210 | { QUAL_RAW, "x", lookup_syscall, "system call" }, |
| 211 | { QUAL_SIGNAL, "signal", lookup_signal, "signal" }, |
| 212 | { QUAL_SIGNAL, "signals", lookup_signal, "signal" }, |
| 213 | { QUAL_SIGNAL, "s", lookup_signal, "signal" }, |
| 214 | { QUAL_FAULT, "fault", lookup_fault, "fault" }, |
| 215 | { QUAL_FAULT, "faults", lookup_fault, "fault" }, |
| 216 | { QUAL_FAULT, "m", lookup_fault, "fault" }, |
| 217 | { QUAL_READ, "read", lookup_desc, "descriptor" }, |
| 218 | { QUAL_READ, "reads", lookup_desc, "descriptor" }, |
| 219 | { QUAL_READ, "r", lookup_desc, "descriptor" }, |
| 220 | { QUAL_WRITE, "write", lookup_desc, "descriptor" }, |
| 221 | { QUAL_WRITE, "writes", lookup_desc, "descriptor" }, |
| 222 | { QUAL_WRITE, "w", lookup_desc, "descriptor" }, |
| 223 | { 0, NULL, NULL, NULL }, |
| 224 | }; |
| 225 | |
| 226 | static int |
| 227 | lookup_syscall(s) |
| 228 | char *s; |
| 229 | { |
| 230 | int i; |
| 231 | |
| 232 | for (i = 0; i < nsyscalls; i++) { |
| 233 | if (strcmp(s, sysent[i].sys_name) == 0) |
| 234 | return i; |
| 235 | } |
| 236 | return -1; |
| 237 | } |
| 238 | |
| 239 | static int |
| 240 | lookup_signal(s) |
| 241 | char *s; |
| 242 | { |
| 243 | int i; |
| 244 | char buf[32]; |
| 245 | |
| 246 | if (s && *s && isdigit(*s)) |
| 247 | return atoi(s); |
| 248 | strcpy(buf, s); |
| 249 | s = buf; |
| 250 | for (i = 0; s[i]; i++) |
| 251 | s[i] = toupper(s[i]); |
| 252 | if (strncmp(s, "SIG", 3) == 0) |
| 253 | s += 3; |
| 254 | for (i = 0; i <= NSIG; i++) { |
Nate Sammons | ce780fc | 1999-03-29 23:23:13 +0000 | [diff] [blame] | 255 | if (strcmp(s, signame(i) + 3) == 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 256 | return i; |
| 257 | } |
| 258 | return -1; |
| 259 | } |
| 260 | |
| 261 | static int |
| 262 | lookup_fault(s) |
| 263 | char *s; |
| 264 | { |
| 265 | return -1; |
| 266 | } |
| 267 | |
| 268 | static int |
| 269 | lookup_desc(s) |
| 270 | char *s; |
| 271 | { |
| 272 | if (s && *s && isdigit(*s)) |
| 273 | return atoi(s); |
| 274 | return -1; |
| 275 | } |
| 276 | |
| 277 | static int |
| 278 | lookup_class(s) |
| 279 | char *s; |
| 280 | { |
| 281 | if (strcmp(s, "file") == 0) |
| 282 | return TRACE_FILE; |
| 283 | if (strcmp(s, "ipc") == 0) |
| 284 | return TRACE_IPC; |
| 285 | if (strcmp(s, "network") == 0) |
| 286 | return TRACE_NETWORK; |
| 287 | if (strcmp(s, "process") == 0) |
| 288 | return TRACE_PROCESS; |
| 289 | if (strcmp(s, "signal") == 0) |
| 290 | return TRACE_SIGNAL; |
| 291 | return -1; |
| 292 | } |
| 293 | |
| 294 | void |
| 295 | qualify(s) |
| 296 | char *s; |
| 297 | { |
| 298 | struct qual_options *opt; |
| 299 | int not; |
| 300 | char *p; |
| 301 | int i, n; |
| 302 | |
| 303 | opt = &qual_options[0]; |
| 304 | for (i = 0; (p = qual_options[i].option_name); i++) { |
| 305 | n = strlen(p); |
| 306 | if (strncmp(s, p, n) == 0 && s[n] == '=') { |
| 307 | opt = &qual_options[i]; |
| 308 | s += n + 1; |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | not = 0; |
| 313 | if (*s == '!') { |
| 314 | not = 1; |
| 315 | s++; |
| 316 | } |
| 317 | if (strcmp(s, "none") == 0) { |
| 318 | not = 1 - not; |
| 319 | s = "all"; |
| 320 | } |
| 321 | if (strcmp(s, "all") == 0) { |
| 322 | for (i = 0; i < MAX_QUALS; i++) { |
| 323 | if (not) |
| 324 | qual_flags[i] &= ~opt->bitflag; |
| 325 | else |
| 326 | qual_flags[i] |= opt->bitflag; |
| 327 | } |
| 328 | return; |
| 329 | } |
| 330 | for (i = 0; i < MAX_QUALS; i++) { |
| 331 | if (not) |
| 332 | qual_flags[i] |= opt->bitflag; |
| 333 | else |
| 334 | qual_flags[i] &= ~opt->bitflag; |
| 335 | } |
| 336 | for (p = strtok(s, ","); p; p = strtok(NULL, ",")) { |
| 337 | if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) { |
| 338 | for (i = 0; i < MAX_QUALS; i++) { |
| 339 | if (sysent[i].sys_flags & n) { |
| 340 | if (not) |
| 341 | qual_flags[i] &= ~opt->bitflag; |
| 342 | else |
| 343 | qual_flags[i] |= opt->bitflag; |
| 344 | } |
| 345 | } |
| 346 | continue; |
| 347 | } |
| 348 | if ((n = (*opt->lookup)(p)) < 0) { |
| 349 | fprintf(stderr, "strace: invalid %s `%s'\n", |
| 350 | opt->argument_name, p); |
| 351 | exit(1); |
| 352 | } |
| 353 | if (not) |
| 354 | qual_flags[n] &= ~opt->bitflag; |
| 355 | else |
| 356 | qual_flags[n] |= opt->bitflag; |
| 357 | } |
| 358 | return; |
| 359 | } |
| 360 | |
| 361 | static void |
| 362 | dumpio(tcp) |
| 363 | struct tcb *tcp; |
| 364 | { |
| 365 | if (syserror(tcp)) |
| 366 | return; |
| 367 | if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS) |
| 368 | return; |
| 369 | #ifdef __arm__ |
| 370 | switch (tcp->scno + __NR_SYSCALL_BASE) { |
| 371 | #else |
| 372 | switch (tcp->scno) { |
| 373 | #endif |
| 374 | case SYS_read: |
| 375 | #ifdef SYS_recv |
| 376 | case SYS_recv: |
| 377 | #endif |
| 378 | #ifdef SYS_recvfrom |
| 379 | case SYS_recvfrom: |
| 380 | #endif |
| 381 | if (qual_flags[tcp->u_arg[0]] & QUAL_READ) |
| 382 | dumpstr(tcp, tcp->u_arg[1], tcp->u_rval); |
| 383 | break; |
| 384 | case SYS_write: |
| 385 | #ifdef SYS_send |
| 386 | case SYS_send: |
| 387 | #endif |
| 388 | #ifdef SYS_sendto |
| 389 | case SYS_sendto: |
| 390 | #endif |
| 391 | if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) |
| 392 | dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]); |
| 393 | break; |
| 394 | } |
| 395 | } |
| 396 | |
Wichert Akkerman | 8829a55 | 1999-06-11 13:18:40 +0000 | [diff] [blame^] | 397 | enum subcall_style { shift_style, deref_style, mask_style, door_style }; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 398 | |
| 399 | #if !(defined(LINUX) && defined(ALPHA)) |
| 400 | |
| 401 | const int socket_map [] = { |
| 402 | /* SYS_SOCKET */ 97, |
| 403 | /* SYS_BIND */ 104, |
| 404 | /* SYS_CONNECT */ 98, |
| 405 | /* SYS_LISTEN */ 106, |
| 406 | /* SYS_ACCEPT */ 99, |
| 407 | /* SYS_GETSOCKNAME */ 150, |
| 408 | /* SYS_GETPEERNAME */ 141, |
| 409 | /* SYS_SOCKETPAIR */ 135, |
| 410 | /* SYS_SEND */ 101, |
| 411 | /* SYS_RECV */ 102, |
| 412 | /* SYS_SENDTO */ 133, |
| 413 | /* SYS_RECVFROM */ 125, |
| 414 | /* SYS_SHUTDOWN */ 134, |
| 415 | /* SYS_SETSOCKOPT */ 105, |
| 416 | /* SYS_GETSOCKOPT */ 118, |
| 417 | /* SYS_SENDMSG */ 114, |
| 418 | /* SYS_RECVMSG */ 113 |
| 419 | }; |
| 420 | |
| 421 | void |
| 422 | sparc_socket_decode (struct tcb *tcp) |
| 423 | { |
| 424 | volatile long addr; |
| 425 | volatile int i, n; |
| 426 | |
| 427 | if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){ |
| 428 | return; |
| 429 | } |
| 430 | tcp->scno = socket_map [tcp->u_arg [0]-1]; |
| 431 | n = tcp->u_nargs = sysent [tcp->scno].nargs; |
| 432 | addr = tcp->u_arg [1]; |
| 433 | for (i = 0; i < n; i++){ |
| 434 | int arg; |
| 435 | if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0) |
| 436 | arg = 0; |
| 437 | tcp->u_arg [i] = arg; |
| 438 | addr += sizeof (arg); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | static void |
| 443 | decode_subcall(tcp, subcall, nsubcalls, style) |
| 444 | struct tcb *tcp; |
| 445 | int subcall; |
| 446 | int nsubcalls; |
| 447 | enum subcall_style style; |
| 448 | { |
| 449 | int i, addr, mask, arg; |
| 450 | |
| 451 | if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls) |
| 452 | return; |
| 453 | switch (style) { |
| 454 | case shift_style: |
| 455 | tcp->scno = subcall + tcp->u_arg[0]; |
| 456 | if (sysent[tcp->scno].nargs != -1) |
| 457 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 458 | else |
| 459 | tcp->u_nargs--; |
| 460 | for (i = 0; i < tcp->u_nargs; i++) |
| 461 | tcp->u_arg[i] = tcp->u_arg[i + 1]; |
| 462 | break; |
| 463 | case deref_style: |
| 464 | tcp->scno = subcall + tcp->u_arg[0]; |
| 465 | addr = tcp->u_arg[1]; |
| 466 | for (i = 0; i < sysent[tcp->scno].nargs; i++) { |
| 467 | if (umove(tcp, addr, &arg) < 0) |
| 468 | arg = 0; |
| 469 | tcp->u_arg[i] = arg; |
| 470 | addr += sizeof(arg); |
| 471 | } |
| 472 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 473 | break; |
| 474 | case mask_style: |
| 475 | mask = (tcp->u_arg[0] >> 8) & 0xff; |
| 476 | tcp->u_arg[0] &= 0xff; |
| 477 | for (i = 0; mask; i++) |
| 478 | mask >>= 1; |
| 479 | tcp->scno = subcall + i; |
| 480 | if (sysent[tcp->scno].nargs != -1) |
| 481 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 482 | break; |
Wichert Akkerman | 8829a55 | 1999-06-11 13:18:40 +0000 | [diff] [blame^] | 483 | case door_style: |
| 484 | /* |
| 485 | * Oh, yuck. The call code is the *sixth* argument. |
| 486 | */ |
| 487 | tcp->scno = subcall + tcp->u_arg[5]; |
| 488 | if (sysent[tcp->scno].nargs != -1) |
| 489 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 490 | else |
| 491 | tcp->u_nargs--; |
| 492 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | #endif |
| 496 | |
| 497 | struct tcb *tcp_last = NULL; |
| 498 | |
| 499 | static int |
| 500 | internal_syscall(tcp) |
| 501 | struct tcb *tcp; |
| 502 | { |
| 503 | /* |
| 504 | * We must always trace a few critical system calls in order to |
| 505 | * correctly support following forks in the presence of tracing |
| 506 | * qualifiers. |
| 507 | */ |
| 508 | #ifdef __arm__ |
| 509 | switch (tcp->scno + __NR_SYSCALL_BASE) { |
| 510 | #else |
| 511 | switch (tcp->scno) { |
| 512 | #endif |
| 513 | #ifdef SYS_fork |
| 514 | case SYS_fork: |
| 515 | #endif |
| 516 | #ifdef SYS_vfork |
| 517 | case SYS_vfork: |
| 518 | #endif |
| 519 | #ifdef SYS_clone |
| 520 | case SYS_clone: |
| 521 | #endif |
| 522 | internal_fork(tcp); |
| 523 | break; |
| 524 | |
| 525 | #ifdef SYS_execv |
| 526 | case SYS_execv: |
| 527 | #endif |
| 528 | #ifdef SYS_execve |
| 529 | case SYS_execve: |
| 530 | #endif |
| 531 | internal_exec(tcp); |
| 532 | break; |
| 533 | |
| 534 | #ifdef SYS_wait |
| 535 | case SYS_wait: |
| 536 | #endif |
| 537 | #ifdef SYS_wait4 |
| 538 | case SYS_wait4: |
| 539 | #endif |
| 540 | #ifdef SYS_waitpid |
| 541 | case SYS_waitpid: |
| 542 | #endif |
| 543 | #ifdef SYS_waitsys |
| 544 | case SYS_waitsys: |
| 545 | #endif |
| 546 | internal_wait(tcp); |
| 547 | break; |
| 548 | |
| 549 | #ifdef SYS_exit |
| 550 | case SYS_exit: |
| 551 | #endif |
| 552 | internal_exit(tcp); |
| 553 | break; |
| 554 | } |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | int |
| 559 | trace_syscall(tcp) |
| 560 | struct tcb *tcp; |
| 561 | { |
| 562 | int sys_res; |
| 563 | struct timeval tv; |
| 564 | long scno = 0; |
| 565 | #ifdef LINUX |
| 566 | #if defined (I386) |
| 567 | long eax; |
| 568 | #elif defined (POWERPC) |
| 569 | long result,flags; |
| 570 | #elif defined (M68K) |
| 571 | int d0; |
| 572 | #elif defined (ARM) |
| 573 | int r0; |
| 574 | #elif defined (ALPHA) |
| 575 | long r0; |
| 576 | long a3; |
| 577 | #elif defined (SPARC) |
| 578 | struct pt_regs regs; |
| 579 | unsigned long trap; |
| 580 | #endif |
| 581 | #endif /* LINUX */ |
| 582 | |
| 583 | #ifndef SVR4 |
| 584 | int pid = tcp->pid; |
| 585 | #endif /* !SVR4 */ |
| 586 | |
| 587 | /* Measure the exit time as early as possible to avoid errors. */ |
| 588 | if (dtime && (tcp->flags & TCB_INSYSCALL)) |
| 589 | gettimeofday(&tv, NULL); |
| 590 | #ifdef LINUX |
| 591 | #if defined (POWERPC) |
| 592 | if (upeek(pid, 4*PT_R0, &scno) < 0) |
| 593 | return -1; |
| 594 | if (!(tcp->flags & TCB_INSYSCALL)) { |
| 595 | /* Check if we return from execve. */ |
| 596 | if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) { |
| 597 | tcp->flags &= ~TCB_WAITEXECVE; |
| 598 | return 0; |
| 599 | } |
| 600 | } |
| 601 | #elif defined (I386) |
| 602 | if (upeek(pid, 4*ORIG_EAX, &scno) < 0) |
| 603 | return -1; |
| 604 | #elif defined (ARM) |
| 605 | { |
| 606 | long pc; |
| 607 | upeek(pid, 4*15, &pc); |
| 608 | umoven(tcp, pc-4, 4, (char *)&scno); |
| 609 | scno &= 0x000fffff; |
| 610 | } |
| 611 | #elif defined (M68K) |
| 612 | if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0) |
| 613 | return -1; |
| 614 | #elif defined (ALPHA) |
| 615 | if (upeek(pid, REG_A3, &a3) < 0) |
| 616 | return -1; |
| 617 | |
| 618 | if (!(tcp->flags & TCB_INSYSCALL)) { |
| 619 | if (upeek(pid, REG_R0, &scno) < 0) |
| 620 | return -1; |
| 621 | |
| 622 | /* Check if we return from execve. */ |
| 623 | if (scno == 0 && tcp->flags & TCB_WAITEXECVE) { |
| 624 | tcp->flags &= ~TCB_WAITEXECVE; |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | /* |
| 629 | * Do some sanity checks to figure out if it's |
| 630 | * really a syscall entry |
| 631 | */ |
| 632 | if (scno < 0 || scno > nsyscalls) { |
| 633 | if (a3 == 0 || a3 == -1) { |
| 634 | if (debug) |
| 635 | fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno); |
| 636 | return 0; |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | else { |
| 641 | if (upeek(pid, REG_R0, &r0) < 0) |
| 642 | return -1; |
| 643 | } |
| 644 | #elif defined (SPARC) |
| 645 | /* Everything we need is in the current register set. */ |
| 646 | if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0) |
| 647 | return -1; |
| 648 | |
| 649 | memmove (®s.u_regs [1], ®s.u_regs [0], |
| 650 | sizeof (regs.u_regs) - sizeof (regs.u_regs [0])); |
| 651 | |
| 652 | /* If we are entering, then disassemble the syscall trap. */ |
| 653 | if (!(tcp->flags & TCB_INSYSCALL)) { |
| 654 | /* Retrieve the syscall trap instruction. */ |
| 655 | errno = 0; |
| 656 | trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.pc,0); |
| 657 | if (errno) |
| 658 | return -1; |
| 659 | |
| 660 | /* Disassemble the trap to see what personality to use. */ |
| 661 | switch (trap) { |
| 662 | case 0x91d02010: |
| 663 | /* Linux/SPARC syscall trap. */ |
| 664 | set_personality(0); |
| 665 | break; |
Wichert Akkerman | dacfb6e | 1999-06-03 14:21:07 +0000 | [diff] [blame] | 666 | case 0x91d0206d: |
| 667 | /* Linux/SPARC64 syscall trap. */ |
| 668 | fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n"); |
| 669 | return -1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 670 | case 0x91d02000: |
| 671 | /* SunOS syscall trap. (pers 1) */ |
| 672 | fprintf(stderr,"syscall: SunOS no support\n"); |
| 673 | return -1; |
| 674 | case 0x91d02008: |
| 675 | /* Solaris 2.x syscall trap. (per 2) */ |
| 676 | set_personality(1); |
| 677 | break; |
| 678 | case 0x91d02009: |
| 679 | /* NetBSD/FreeBSD syscall trap. */ |
| 680 | fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n"); |
| 681 | return -1; |
| 682 | case 0x91d02027: |
| 683 | /* Solaris 2.x gettimeofday */ |
| 684 | set_personality(1); |
| 685 | break; |
| 686 | default: |
| 687 | /* Unknown syscall trap. */ |
| 688 | if(tcp->flags & TCB_WAITEXECVE) { |
| 689 | tcp->flags &= ~TCB_WAITEXECVE; |
| 690 | return 0; |
| 691 | } |
| 692 | fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.pc); |
| 693 | return -1; |
| 694 | } |
| 695 | |
| 696 | /* Extract the system call number from the registers. */ |
| 697 | if (trap == 0x91d02027) |
| 698 | scno = 156; |
| 699 | else |
| 700 | scno = regs.u_regs[UREG_G1]; |
| 701 | if (scno == 0) { |
| 702 | scno = regs.u_regs[UREG_I0]; |
| 703 | memmove (®s.u_regs[UREG_I0], ®s.u_regs[UREG_I1], 7*sizeof(regs.u_regs[0])); |
| 704 | } |
| 705 | } |
| 706 | #endif |
| 707 | #endif /* LINUX */ |
| 708 | #ifdef SUNOS4 |
| 709 | if (upeek(pid, uoff(u_arg[7]), &scno) < 0) |
| 710 | return -1; |
| 711 | #endif |
| 712 | #ifdef SVR4 |
| 713 | #ifdef HAVE_PR_SYSCALL |
| 714 | scno = tcp->status.pr_syscall; |
| 715 | #else /* !HAVE_PR_SYSCALL */ |
| 716 | scno = tcp->status.pr_what; |
| 717 | #endif /* !HAVE_PR_SYSCALL */ |
| 718 | if (!(tcp->flags & TCB_INSYSCALL)) { |
| 719 | if (tcp->status.pr_why != PR_SYSENTRY) { |
| 720 | if ( |
| 721 | scno == SYS_fork |
| 722 | #ifdef SYS_vfork |
| 723 | || scno == SYS_vfork |
| 724 | #endif /* SYS_vfork */ |
| 725 | ) { |
| 726 | /* We are returning in the child, fake it. */ |
| 727 | tcp->status.pr_why = PR_SYSENTRY; |
| 728 | trace_syscall(tcp); |
| 729 | tcp->status.pr_why = PR_SYSEXIT; |
| 730 | } |
| 731 | else { |
| 732 | fprintf(stderr, "syscall: missing entry\n"); |
| 733 | tcp->flags |= TCB_INSYSCALL; |
| 734 | } |
| 735 | } |
| 736 | } |
| 737 | else { |
| 738 | if (tcp->status.pr_why != PR_SYSEXIT) { |
| 739 | fprintf(stderr, "syscall: missing exit\n"); |
| 740 | tcp->flags &= ~TCB_INSYSCALL; |
| 741 | } |
| 742 | } |
| 743 | #endif /* SVR4 */ |
| 744 | #ifdef SUNOS4 |
| 745 | if (!(tcp->flags & TCB_INSYSCALL)) { |
| 746 | if (scno == 0) { |
| 747 | fprintf(stderr, "syscall: missing entry\n"); |
| 748 | tcp->flags |= TCB_INSYSCALL; |
| 749 | } |
| 750 | } |
| 751 | else { |
| 752 | if (scno != 0) { |
| 753 | if (debug) { |
| 754 | /* |
| 755 | * This happens when a signal handler |
| 756 | * for a signal which interrupted a |
| 757 | * a system call makes another system call. |
| 758 | */ |
| 759 | fprintf(stderr, "syscall: missing exit\n"); |
| 760 | } |
| 761 | tcp->flags &= ~TCB_INSYSCALL; |
| 762 | } |
| 763 | } |
| 764 | #endif /* SUNOS4 */ |
| 765 | #ifdef LINUX |
| 766 | #if defined (I386) |
| 767 | if (upeek(pid, 4*EAX, &eax) < 0) |
| 768 | return -1; |
| 769 | if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) { |
| 770 | if (debug) |
| 771 | fprintf(stderr, "stray syscall exit: eax = %ld\n", eax); |
| 772 | return 0; |
| 773 | } |
| 774 | #elif defined (POWERPC) |
| 775 | # define SO_MASK 0x10000000 |
| 776 | if (upeek(pid, 4*PT_CCR, &flags) < 0) |
| 777 | return -1; |
| 778 | if (upeek(pid, 4*PT_R3, &result) < 0) |
| 779 | return -1; |
| 780 | if (flags & SO_MASK) |
| 781 | result = -result; |
| 782 | #elif defined (M68K) |
| 783 | if (upeek(pid, 4*PT_D0, &d0) < 0) |
| 784 | return -1; |
| 785 | if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) { |
| 786 | if (debug) |
| 787 | fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0); |
| 788 | return 0; |
| 789 | } |
| 790 | #elif defined (ARM) |
| 791 | if (upeek(pid, 4*0, (long *)&r0) < 0) |
| 792 | return -1; |
| 793 | if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) { |
| 794 | if (debug) |
| 795 | fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0); |
| 796 | return 0; |
| 797 | } |
| 798 | #else |
| 799 | #endif |
| 800 | #endif /* LINUX */ |
| 801 | |
| 802 | if (tcp->flags & TCB_INSYSCALL) { |
| 803 | long u_error; |
| 804 | |
| 805 | #ifdef LINUX |
| 806 | #ifdef I386 |
| 807 | if (eax < 0 && -eax < nerrnos) { |
| 808 | tcp->u_rval = -1; |
| 809 | u_error = -eax; |
| 810 | } |
| 811 | else { |
| 812 | tcp->u_rval = eax; |
| 813 | u_error = 0; |
| 814 | } |
| 815 | #else /* !I386 */ |
| 816 | #ifdef POWERPC |
| 817 | if (result && (unsigned) -result < nerrnos) { |
| 818 | tcp->u_rval = -1; |
| 819 | u_error = -result; |
| 820 | } |
| 821 | else { |
| 822 | tcp->u_rval = result; |
| 823 | u_error = 0; |
| 824 | } |
| 825 | #else /* !POWERPC */ |
| 826 | #ifdef M68K |
| 827 | if (d0 && (unsigned) -d0 < nerrnos) { |
| 828 | tcp->u_rval = -1; |
| 829 | u_error = -d0; |
| 830 | } |
| 831 | else { |
| 832 | tcp->u_rval = d0; |
| 833 | u_error = 0; |
| 834 | } |
| 835 | #else /* !M68K */ |
| 836 | #ifdef ARM |
| 837 | if (r0 && (unsigned) -r0 < nerrnos) { |
| 838 | tcp->u_rval = -1; |
| 839 | u_error = -r0; |
| 840 | } |
| 841 | else { |
| 842 | tcp->u_rval = r0; |
| 843 | u_error = 0; |
| 844 | } |
| 845 | #else /* !ARM */ |
| 846 | #ifdef ALPHA |
| 847 | if (a3) { |
| 848 | tcp->u_rval = -1; |
| 849 | u_error = r0; |
| 850 | } |
| 851 | else { |
| 852 | tcp->u_rval = r0; |
| 853 | u_error = 0; |
| 854 | } |
| 855 | #else /* !ALPHA */ |
| 856 | #ifdef SPARC |
| 857 | if (regs.psr & PSR_C) { |
| 858 | tcp->u_rval = -1; |
| 859 | u_error = regs.u_regs[UREG_I0]; |
| 860 | } |
| 861 | else { |
| 862 | tcp->u_rval = regs.u_regs[UREG_I0]; |
| 863 | u_error = 0; |
| 864 | } |
| 865 | #endif /* SPARC */ |
| 866 | #endif /* ALPHA */ |
| 867 | #endif /* ARM */ |
| 868 | #endif /* M68K */ |
| 869 | #endif /* POWERPC */ |
| 870 | #endif /* I386 */ |
| 871 | #endif /* LINUX */ |
| 872 | #ifdef SUNOS4 |
| 873 | /* get error code from user struct */ |
| 874 | if (upeek(pid, uoff(u_error), &u_error) < 0) |
| 875 | return -1; |
| 876 | u_error >>= 24; /* u_error is a char */ |
| 877 | |
| 878 | /* get system call return value */ |
| 879 | if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0) |
| 880 | return -1; |
| 881 | #endif /* SUNOS4 */ |
| 882 | #ifdef SVR4 |
| 883 | #ifdef SPARC |
| 884 | /* Judicious guessing goes a long way. */ |
| 885 | if (tcp->status.pr_reg[R_PSR] & 0x100000) { |
| 886 | tcp->u_rval = -1; |
| 887 | u_error = tcp->status.pr_reg[R_O0]; |
| 888 | } |
| 889 | else { |
| 890 | tcp->u_rval = tcp->status.pr_reg[R_O0]; |
| 891 | u_error = 0; |
| 892 | } |
| 893 | #endif /* SPARC */ |
| 894 | #ifdef I386 |
| 895 | /* Wanna know how to kill an hour single-stepping? */ |
| 896 | if (tcp->status.pr_reg[EFL] & 0x1) { |
| 897 | tcp->u_rval = -1; |
| 898 | u_error = tcp->status.pr_reg[EAX]; |
| 899 | } |
| 900 | else { |
| 901 | tcp->u_rval = tcp->status.pr_reg[EAX]; |
| 902 | u_error = 0; |
| 903 | } |
| 904 | #endif /* I386 */ |
| 905 | #ifdef MIPS |
| 906 | if (tcp->status.pr_reg[CTX_A3]) { |
| 907 | tcp->u_rval = -1; |
| 908 | u_error = tcp->status.pr_reg[CTX_V0]; |
| 909 | } |
| 910 | else { |
| 911 | tcp->u_rval = tcp->status.pr_reg[CTX_V0]; |
| 912 | u_error = 0; |
| 913 | } |
| 914 | #endif /* MIPS */ |
| 915 | #endif /* SVR4 */ |
| 916 | tcp->u_error = u_error; |
| 917 | |
| 918 | internal_syscall(tcp); |
| 919 | if (!(qual_flags[tcp->scno] & QUAL_TRACE)) { |
| 920 | tcp->flags &= ~TCB_INSYSCALL; |
| 921 | return 0; |
| 922 | } |
| 923 | |
| 924 | if (tcp->flags & TCB_REPRINT) { |
| 925 | printleader(tcp); |
| 926 | tprintf("<... "); |
| 927 | if (tcp->scno >= nsyscalls) |
| 928 | tprintf("syscall_%lu", tcp->scno); |
| 929 | else |
| 930 | tprintf("%s", sysent[tcp->scno].sys_name); |
| 931 | tprintf(" resumed> "); |
| 932 | } |
| 933 | |
| 934 | if (cflag) { |
| 935 | call_count[tcp->scno]++; |
| 936 | if (u_error) |
| 937 | error_count[tcp->scno]++; |
| 938 | tv_sub(&tv, &tv, &tcp->etime); |
| 939 | #ifdef LINUX |
| 940 | if (tv_cmp(&tv, &tcp->dtime) > 0) { |
| 941 | static struct timeval one_tick = |
| 942 | { 0, 1000000 / HZ }; |
| 943 | |
| 944 | if (tv_nz(&tcp->dtime)) |
| 945 | tv = tcp->dtime; |
| 946 | else if (tv_cmp(&tv, &one_tick) > 0) { |
| 947 | if (tv_cmp(&shortest, &one_tick) < 0) |
| 948 | tv = shortest; |
| 949 | else |
| 950 | tv = one_tick; |
| 951 | } |
| 952 | } |
| 953 | #endif /* LINUX */ |
| 954 | if (tv_cmp(&tv, &shortest) < 0) |
| 955 | shortest = tv; |
| 956 | tv_add(&tv_count[tcp->scno], |
| 957 | &tv_count[tcp->scno], &tv); |
| 958 | tcp->flags &= ~TCB_INSYSCALL; |
| 959 | return 0; |
| 960 | } |
| 961 | |
| 962 | if (tcp->scno >= nsyscalls |
| 963 | || (qual_flags[tcp->scno] & QUAL_RAW)) |
| 964 | sys_res = printargs(tcp); |
| 965 | else |
| 966 | sys_res = (*sysent[tcp->scno].sys_func)(tcp); |
| 967 | u_error = tcp->u_error; |
| 968 | tprintf(") "); |
| 969 | tabto(acolumn); |
| 970 | if (qual_flags[tcp->scno] & QUAL_RAW) { |
| 971 | if (u_error) |
| 972 | tprintf("= -1 (errno %ld)", u_error); |
| 973 | else |
| 974 | tprintf("= %#lx", tcp->u_rval); |
| 975 | } |
| 976 | else if (!(sys_res & RVAL_NONE) && u_error) { |
| 977 | #ifdef LINUX |
| 978 | switch (u_error) { |
| 979 | case ERESTARTSYS: |
| 980 | tprintf("= ? ERESTARTSYS (To be restarted)"); |
| 981 | break; |
| 982 | case ERESTARTNOINTR: |
| 983 | tprintf("= ? ERESTARTNOINTR (To be restarted)"); |
| 984 | break; |
| 985 | case ERESTARTNOHAND: |
| 986 | tprintf("= ? ERESTARTNOHAND (To be restarted)"); |
| 987 | break; |
| 988 | default: |
| 989 | #endif /* LINUX */ |
| 990 | tprintf("= -1 "); |
| 991 | if (u_error < nerrnos && u_error < sys_nerr) |
| 992 | tprintf("%s (%s)", errnoent[u_error], |
| 993 | sys_errlist[u_error]); |
| 994 | else if (u_error < nerrnos) |
| 995 | tprintf("%s (errno %ld)", |
| 996 | errnoent[u_error], u_error); |
| 997 | else if (u_error < sys_nerr) |
| 998 | tprintf("ERRNO_%ld (%s)", u_error, |
| 999 | sys_errlist[u_error]); |
| 1000 | else |
| 1001 | tprintf("E??? (errno %ld)", u_error); |
| 1002 | #ifdef LINUX |
| 1003 | break; |
| 1004 | } |
| 1005 | #endif /* LINUX */ |
| 1006 | } |
| 1007 | else { |
| 1008 | if (sys_res & RVAL_NONE) |
| 1009 | tprintf("= ?"); |
| 1010 | else { |
| 1011 | switch (sys_res & RVAL_MASK) { |
| 1012 | case RVAL_HEX: |
| 1013 | tprintf("= %#lx", tcp->u_rval); |
| 1014 | break; |
| 1015 | case RVAL_OCTAL: |
| 1016 | tprintf("= %#lo", tcp->u_rval); |
| 1017 | break; |
| 1018 | case RVAL_UDECIMAL: |
| 1019 | tprintf("= %lu", tcp->u_rval); |
| 1020 | break; |
| 1021 | case RVAL_DECIMAL: |
| 1022 | tprintf("= %ld", tcp->u_rval); |
| 1023 | break; |
| 1024 | default: |
| 1025 | fprintf(stderr, |
| 1026 | "invalid rval format\n"); |
| 1027 | break; |
| 1028 | } |
| 1029 | } |
| 1030 | if ((sys_res & RVAL_STR) && tcp->auxstr) |
| 1031 | tprintf(" (%s)", tcp->auxstr); |
| 1032 | } |
| 1033 | if (dtime) { |
| 1034 | tv_sub(&tv, &tv, &tcp->etime); |
| 1035 | tprintf(" <%ld.%06ld>", |
| 1036 | (long) tv.tv_sec, (long) tv.tv_usec); |
| 1037 | } |
| 1038 | printtrailer(tcp); |
| 1039 | |
| 1040 | dumpio(tcp); |
| 1041 | if (fflush(tcp->outf) == EOF) |
| 1042 | return -1; |
| 1043 | tcp->flags &= ~TCB_INSYSCALL; |
| 1044 | return 0; |
| 1045 | } |
| 1046 | |
| 1047 | /* Entering system call */ |
| 1048 | tcp->scno = scno; |
| 1049 | #ifdef LINUX |
| 1050 | #if defined (ALPHA) |
| 1051 | { |
| 1052 | int i; |
| 1053 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1054 | for (i = 0; i < tcp->u_nargs; i++) { |
Wichert Akkerman | b859bea | 1999-04-18 22:50:50 +0000 | [diff] [blame] | 1055 | /* WTA: if scno is out-of-bounds this will bomb. Add range-check |
| 1056 | * for scno somewhere above here! |
| 1057 | */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1058 | if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0) |
| 1059 | return -1; |
| 1060 | } |
| 1061 | } |
| 1062 | #elif defined (POWERPC) |
| 1063 | { |
| 1064 | int i; |
| 1065 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1066 | for (i = 0; i < tcp->u_nargs; i++) { |
| 1067 | if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0) |
| 1068 | return -1; |
| 1069 | } |
| 1070 | } |
| 1071 | #elif defined (SPARC) |
| 1072 | { |
| 1073 | int i, offset; |
| 1074 | |
| 1075 | offset = UREG_I0; |
| 1076 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1077 | for (i = 0; i < tcp->u_nargs; i++) |
| 1078 | tcp->u_arg[i] = regs.u_regs[offset + i]; |
| 1079 | } |
| 1080 | #else |
| 1081 | { |
| 1082 | int i; |
| 1083 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1084 | for (i = 0; i < tcp->u_nargs; i++) { |
| 1085 | if (upeek(pid, i*4, &tcp->u_arg[i]) < 0) |
| 1086 | return -1; |
| 1087 | } |
| 1088 | } |
| 1089 | #endif |
| 1090 | #endif /* LINUX */ |
| 1091 | #ifdef SUNOS4 |
| 1092 | { |
| 1093 | int i; |
| 1094 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1095 | for (i = 0; i < tcp->u_nargs; i++) { |
| 1096 | struct user *u; |
| 1097 | |
| 1098 | if (upeek(pid, uoff(u_arg[0]) + |
| 1099 | (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0) |
| 1100 | return -1; |
| 1101 | } |
| 1102 | } |
| 1103 | #endif /* SUNOS4 */ |
| 1104 | #ifdef SVR4 |
| 1105 | #ifdef MIPS |
| 1106 | /* |
| 1107 | * SGI is broken: even though it has pr_sysarg, it doesn't |
| 1108 | * set them on system call entry. Get a clue. |
| 1109 | */ |
| 1110 | if (sysent[tcp->scno].nargs != -1) |
| 1111 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1112 | else |
| 1113 | tcp->u_nargs = tcp->status.pr_nsysarg; |
| 1114 | if (tcp->u_nargs > 4) { |
| 1115 | memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0], |
| 1116 | 4*sizeof(tcp->u_arg[0])); |
| 1117 | umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16, |
| 1118 | (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4)); |
| 1119 | } |
| 1120 | else { |
| 1121 | memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0], |
| 1122 | tcp->u_nargs*sizeof(tcp->u_arg[0])); |
| 1123 | } |
| 1124 | #else /* !MIPS */ |
| 1125 | #ifdef HAVE_PR_SYSCALL |
| 1126 | if (sysent[tcp->scno].nargs != -1) |
| 1127 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1128 | else |
| 1129 | tcp->u_nargs = tcp->status.pr_nsysarg; |
| 1130 | { |
| 1131 | int i; |
| 1132 | for (i = 0; i < tcp->u_nargs; i++) |
| 1133 | tcp->u_arg[i] = tcp->status.pr_sysarg[i]; |
| 1134 | } |
| 1135 | #else /* !HAVE_PR_SYSCALL */ |
| 1136 | #ifdef I386 |
| 1137 | if (sysent[tcp->scno].nargs != -1) |
| 1138 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1139 | else |
| 1140 | tcp->u_nargs = 5; |
| 1141 | umoven(tcp, tcp->status.pr_reg[UESP] + 4, |
| 1142 | tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg); |
| 1143 | #endif /* I386 */ |
| 1144 | #endif /* !HAVE_PR_SYSCALL */ |
| 1145 | #endif /* !MIPS */ |
| 1146 | #endif /* SVR4 */ |
| 1147 | #ifdef __arm__ |
| 1148 | switch (tcp->scno + __NR_SYSCALL_BASE) { |
| 1149 | #else |
| 1150 | switch (tcp->scno) { |
| 1151 | #endif |
| 1152 | #ifdef LINUX |
| 1153 | #if !defined (ALPHA) && !defined(SPARC) |
| 1154 | case SYS_socketcall: |
| 1155 | decode_subcall(tcp, SYS_socket_subcall, |
| 1156 | SYS_socket_nsubcalls, deref_style); |
| 1157 | break; |
| 1158 | case SYS_ipc: |
| 1159 | decode_subcall(tcp, SYS_ipc_subcall, |
| 1160 | SYS_ipc_nsubcalls, shift_style); |
| 1161 | break; |
| 1162 | #endif /* !ALPHA && !SPARC */ |
| 1163 | #ifdef SPARC |
| 1164 | case SYS_socketcall: |
| 1165 | sparc_socket_decode (tcp); |
| 1166 | break; |
| 1167 | #endif |
| 1168 | #endif /* LINUX */ |
| 1169 | #ifdef SVR4 |
| 1170 | #ifdef SYS_pgrpsys_subcall |
| 1171 | case SYS_pgrpsys: |
| 1172 | decode_subcall(tcp, SYS_pgrpsys_subcall, |
| 1173 | SYS_pgrpsys_nsubcalls, shift_style); |
| 1174 | break; |
| 1175 | #endif /* SYS_pgrpsys_subcall */ |
| 1176 | #ifdef SYS_sigcall_subcall |
| 1177 | case SYS_sigcall: |
| 1178 | decode_subcall(tcp, SYS_sigcall_subcall, |
| 1179 | SYS_sigcall_nsubcalls, mask_style); |
| 1180 | break; |
| 1181 | #endif /* SYS_sigcall_subcall */ |
| 1182 | case SYS_msgsys: |
| 1183 | decode_subcall(tcp, SYS_msgsys_subcall, |
| 1184 | SYS_msgsys_nsubcalls, shift_style); |
| 1185 | break; |
| 1186 | case SYS_shmsys: |
| 1187 | decode_subcall(tcp, SYS_shmsys_subcall, |
| 1188 | SYS_shmsys_nsubcalls, shift_style); |
| 1189 | break; |
| 1190 | case SYS_semsys: |
| 1191 | decode_subcall(tcp, SYS_semsys_subcall, |
| 1192 | SYS_semsys_nsubcalls, shift_style); |
| 1193 | break; |
| 1194 | #if 0 /* broken */ |
| 1195 | case SYS_utssys: |
| 1196 | decode_subcall(tcp, SYS_utssys_subcall, |
| 1197 | SYS_utssys_nsubcalls, shift_style); |
| 1198 | break; |
| 1199 | #endif |
| 1200 | case SYS_sysfs: |
| 1201 | decode_subcall(tcp, SYS_sysfs_subcall, |
| 1202 | SYS_sysfs_nsubcalls, shift_style); |
| 1203 | break; |
| 1204 | case SYS_spcall: |
| 1205 | decode_subcall(tcp, SYS_spcall_subcall, |
| 1206 | SYS_spcall_nsubcalls, shift_style); |
| 1207 | break; |
| 1208 | #ifdef SYS_context_subcall |
| 1209 | case SYS_context: |
| 1210 | decode_subcall(tcp, SYS_context_subcall, |
| 1211 | SYS_context_nsubcalls, shift_style); |
| 1212 | break; |
| 1213 | #endif /* SYS_context_subcall */ |
Wichert Akkerman | 8829a55 | 1999-06-11 13:18:40 +0000 | [diff] [blame^] | 1214 | #ifdef SYS_door_subcall |
| 1215 | case SYS_door: |
| 1216 | decode_subcall(tcp, SYS_door_subcall, |
| 1217 | SYS_door_nsubcalls, door_style); |
| 1218 | break; |
| 1219 | #endif /* SYS_door_subcall */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1220 | #endif /* SVR4 */ |
| 1221 | #ifdef SUNOS4 |
| 1222 | case SYS_semsys: |
| 1223 | decode_subcall(tcp, SYS_semsys_subcall, |
| 1224 | SYS_semsys_nsubcalls, shift_style); |
| 1225 | break; |
| 1226 | case SYS_msgsys: |
| 1227 | decode_subcall(tcp, SYS_msgsys_subcall, |
| 1228 | SYS_msgsys_nsubcalls, shift_style); |
| 1229 | break; |
| 1230 | case SYS_shmsys: |
| 1231 | decode_subcall(tcp, SYS_shmsys_subcall, |
| 1232 | SYS_shmsys_nsubcalls, shift_style); |
| 1233 | break; |
| 1234 | #endif |
| 1235 | } |
| 1236 | |
| 1237 | internal_syscall(tcp); |
| 1238 | if (!(qual_flags[tcp->scno] & QUAL_TRACE)) { |
| 1239 | tcp->flags |= TCB_INSYSCALL; |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
| 1243 | if (cflag) { |
| 1244 | gettimeofday(&tcp->etime, NULL); |
| 1245 | tcp->flags |= TCB_INSYSCALL; |
| 1246 | return 0; |
| 1247 | } |
| 1248 | |
| 1249 | printleader(tcp); |
| 1250 | tcp->flags &= ~TCB_REPRINT; |
| 1251 | tcp_last = tcp; |
| 1252 | if (tcp->scno >= nsyscalls) |
| 1253 | tprintf("syscall_%lu(", tcp->scno); |
| 1254 | else |
| 1255 | tprintf("%s(", sysent[tcp->scno].sys_name); |
| 1256 | if (tcp->scno >= nsyscalls || |
| 1257 | ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit)) |
| 1258 | sys_res = printargs(tcp); |
| 1259 | else |
| 1260 | sys_res = (*sysent[tcp->scno].sys_func)(tcp); |
| 1261 | if (fflush(tcp->outf) == EOF) |
| 1262 | return -1; |
| 1263 | tcp->flags |= TCB_INSYSCALL; |
| 1264 | /* Measure the entrance time as late as possible to avoid errors. */ |
| 1265 | if (dtime) |
| 1266 | gettimeofday(&tcp->etime, NULL); |
| 1267 | return sys_res; |
| 1268 | } |
| 1269 | |
| 1270 | int |
| 1271 | printargs(tcp) |
| 1272 | struct tcb *tcp; |
| 1273 | { |
| 1274 | if (entering(tcp)) { |
| 1275 | int i; |
| 1276 | |
| 1277 | for (i = 0; i < tcp->u_nargs; i++) |
| 1278 | tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]); |
| 1279 | } |
| 1280 | return 0; |
| 1281 | } |
| 1282 | |
| 1283 | long |
| 1284 | getrval2(tcp) |
| 1285 | struct tcb *tcp; |
| 1286 | { |
| 1287 | long val = -1; |
| 1288 | |
| 1289 | #ifdef LINUX |
| 1290 | #ifdef SPARC |
| 1291 | struct pt_regs regs; |
| 1292 | if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0) |
| 1293 | return -1; |
| 1294 | val = regs.u_regs[UREG_I1]; |
| 1295 | #endif /* SPARC */ |
| 1296 | #endif /* LINUX */ |
| 1297 | |
| 1298 | #ifdef SUNOS4 |
| 1299 | if (upeek(tcp->pid, uoff(u_rval2), &val) < 0) |
| 1300 | return -1; |
| 1301 | #endif /* SUNOS4 */ |
| 1302 | |
| 1303 | #ifdef SVR4 |
| 1304 | #ifdef SPARC |
| 1305 | val = tcp->status.pr_reg[R_O1]; |
| 1306 | #endif /* SPARC */ |
| 1307 | #ifdef I386 |
| 1308 | val = tcp->status.pr_reg[EDX]; |
| 1309 | #endif /* I386 */ |
| 1310 | #ifdef MIPS |
| 1311 | val = tcp->status.pr_reg[CTX_V1]; |
| 1312 | #endif /* MIPS */ |
| 1313 | #endif /* SVR4 */ |
| 1314 | |
| 1315 | return val; |
| 1316 | } |
| 1317 | |
| 1318 | /* |
| 1319 | * Apparently, indirect system calls have already be converted by ptrace(2), |
| 1320 | * so if you see "indir" this program has gone astray. |
| 1321 | */ |
| 1322 | int |
| 1323 | sys_indir(tcp) |
| 1324 | struct tcb *tcp; |
| 1325 | { |
| 1326 | int i, scno, nargs; |
| 1327 | |
| 1328 | if (entering(tcp)) { |
| 1329 | if ((scno = tcp->u_arg[0]) > nsyscalls) { |
| 1330 | fprintf(stderr, "Bogus syscall: %u\n", scno); |
| 1331 | return 0; |
| 1332 | } |
| 1333 | nargs = sysent[scno].nargs; |
| 1334 | tprintf("%s", sysent[scno].sys_name); |
| 1335 | for (i = 0; i < nargs; i++) |
| 1336 | tprintf(", %#lx", tcp->u_arg[i+1]); |
| 1337 | } |
| 1338 | return 0; |
| 1339 | } |
| 1340 | |
| 1341 | static int |
| 1342 | time_cmp(a, b) |
| 1343 | void *a; |
| 1344 | void *b; |
| 1345 | { |
| 1346 | return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]); |
| 1347 | } |
| 1348 | |
| 1349 | static int |
| 1350 | syscall_cmp(a, b) |
| 1351 | void *a; |
| 1352 | void *b; |
| 1353 | { |
| 1354 | return strcmp(sysent[*((int *) a)].sys_name, |
| 1355 | sysent[*((int *) b)].sys_name); |
| 1356 | } |
| 1357 | |
| 1358 | static int |
| 1359 | count_cmp(a, b) |
| 1360 | void *a; |
| 1361 | void *b; |
| 1362 | { |
| 1363 | int m = call_count[*((int *) a)], n = call_count[*((int *) b)]; |
| 1364 | |
| 1365 | return (m < n) ? 1 : (m > n) ? -1 : 0; |
| 1366 | } |
| 1367 | |
| 1368 | static int (*sortfun)(); |
| 1369 | static struct timeval overhead = { -1, -1 }; |
| 1370 | |
| 1371 | void |
| 1372 | set_sortby(sortby) |
| 1373 | char *sortby; |
| 1374 | { |
| 1375 | if (strcmp(sortby, "time") == 0) |
| 1376 | sortfun = time_cmp; |
| 1377 | else if (strcmp(sortby, "calls") == 0) |
| 1378 | sortfun = count_cmp; |
| 1379 | else if (strcmp(sortby, "name") == 0) |
| 1380 | sortfun = syscall_cmp; |
| 1381 | else if (strcmp(sortby, "nothing") == 0) |
| 1382 | sortfun = NULL; |
| 1383 | else { |
| 1384 | fprintf(stderr, "invalid sortby: `%s'\n", sortby); |
| 1385 | exit(1); |
| 1386 | } |
| 1387 | } |
| 1388 | |
| 1389 | void set_overhead(n) |
| 1390 | int n; |
| 1391 | { |
| 1392 | overhead.tv_sec = n / 1000000; |
| 1393 | overhead.tv_usec = n % 1000000; |
| 1394 | } |
| 1395 | |
| 1396 | void |
| 1397 | call_summary(outf) |
| 1398 | FILE *outf; |
| 1399 | { |
| 1400 | int i, j; |
| 1401 | int call_cum, error_cum; |
| 1402 | struct timeval tv_cum, dtv; |
| 1403 | double percent; |
| 1404 | char *dashes = "-------------------------"; |
| 1405 | char error_str[16]; |
| 1406 | |
| 1407 | call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0; |
| 1408 | if (overhead.tv_sec == -1) { |
| 1409 | tv_mul(&overhead, &shortest, 8); |
| 1410 | tv_div(&overhead, &overhead, 10); |
| 1411 | } |
| 1412 | for (i = 0; i < nsyscalls; i++) { |
| 1413 | sorted_count[i] = i; |
| 1414 | if (call_count[i] == 0) |
| 1415 | continue; |
| 1416 | tv_mul(&dtv, &overhead, call_count[i]); |
| 1417 | tv_sub(&tv_count[i], &tv_count[i], &dtv); |
| 1418 | call_cum += call_count[i]; |
| 1419 | error_cum += error_count[i]; |
| 1420 | tv_add(&tv_cum, &tv_cum, &tv_count[i]); |
| 1421 | } |
| 1422 | if (sortfun) |
| 1423 | qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun); |
| 1424 | fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n", |
| 1425 | "% time", "seconds", "usecs/call", |
| 1426 | "calls", "errors", "syscall"); |
| 1427 | fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n", |
| 1428 | dashes, dashes, dashes, dashes, dashes, dashes); |
| 1429 | for (i = 0; i < nsyscalls; i++) { |
| 1430 | j = sorted_count[i]; |
| 1431 | if (call_count[j] == 0) |
| 1432 | continue; |
| 1433 | tv_div(&dtv, &tv_count[j], call_count[j]); |
| 1434 | if (error_count[j]) |
| 1435 | sprintf(error_str, "%d", error_count[j]); |
| 1436 | else |
| 1437 | error_str[0] = '\0'; |
| 1438 | percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum); |
| 1439 | fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n", |
| 1440 | percent, (long) tv_count[j].tv_sec, |
| 1441 | (long) tv_count[j].tv_usec, |
| 1442 | (long) 1000000 * dtv.tv_sec + dtv.tv_usec, |
| 1443 | call_count[j], error_str, sysent[j].sys_name); |
| 1444 | } |
| 1445 | fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n", |
| 1446 | dashes, dashes, dashes, dashes, dashes, dashes); |
| 1447 | if (error_cum) |
| 1448 | sprintf(error_str, "%d", error_cum); |
| 1449 | else |
| 1450 | error_str[0] = '\0'; |
| 1451 | fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n", |
| 1452 | "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "", |
| 1453 | call_cum, error_str, "total"); |
| 1454 | } |