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> |
| 6 | * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation |
| 7 | * Linux for s390 port by D.J. Barrow |
| 8 | * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com> |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 9 | * All rights reserved. |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without |
| 12 | * modification, are permitted provided that the following conditions |
| 13 | * are met: |
| 14 | * 1. Redistributions of source code must retain the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer. |
| 16 | * 2. Redistributions in binary form must reproduce the above copyright |
| 17 | * notice, this list of conditions and the following disclaimer in the |
| 18 | * documentation and/or other materials provided with the distribution. |
| 19 | * 3. The name of the author may not be used to endorse or promote products |
| 20 | * derived from this software without specific prior written permission. |
| 21 | * |
| 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 24 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 25 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 26 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 27 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 31 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | * |
| 33 | * $Id$ |
| 34 | */ |
| 35 | |
| 36 | #include "defs.h" |
| 37 | |
| 38 | #include <signal.h> |
| 39 | #include <time.h> |
| 40 | #include <errno.h> |
| 41 | #include <sys/user.h> |
| 42 | #include <sys/syscall.h> |
| 43 | #include <sys/param.h> |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 44 | |
Wichert Akkerman | 15dea97 | 1999-10-06 13:06:34 +0000 | [diff] [blame] | 45 | #if HAVE_ASM_REG_H |
Wichert Akkerman | 00a82ee | 2001-03-28 20:29:17 +0000 | [diff] [blame] | 46 | #ifdef SPARC |
| 47 | # define fpq kernel_fpq |
| 48 | # define fq kernel_fq |
| 49 | # define fpu kernel_fpu |
| 50 | #endif |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 51 | #include <asm/reg.h> |
Wichert Akkerman | 00a82ee | 2001-03-28 20:29:17 +0000 | [diff] [blame] | 52 | #ifdef SPARC |
| 53 | # undef fpq |
| 54 | # undef fq |
| 55 | # undef fpu |
| 56 | #endif |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 57 | #endif |
| 58 | |
Wichert Akkerman | 15dea97 | 1999-10-06 13:06:34 +0000 | [diff] [blame] | 59 | #ifdef HAVE_SYS_REG_H |
| 60 | #include <sys/reg.h> |
| 61 | #ifndef PTRACE_PEEKUSR |
| 62 | # define PTRACE_PEEKUSR PTRACE_PEEKUSER |
| 63 | #endif |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 64 | #elif defined(HAVE_LINUX_PTRACE_H) |
| 65 | #undef PTRACE_SYSCALL |
| 66 | #include <linux/ptrace.h> |
Wichert Akkerman | 15dea97 | 1999-10-06 13:06:34 +0000 | [diff] [blame] | 67 | #endif |
| 68 | |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 69 | #if defined(LINUX) && defined(IA64) |
| 70 | # include <asm/ptrace_offsets.h> |
| 71 | # include <asm/rse.h> |
| 72 | #endif |
| 73 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 74 | #ifndef SYS_ERRLIST_DECLARED |
| 75 | extern int sys_nerr; |
| 76 | extern char *sys_errlist[]; |
| 77 | #endif /* SYS_ERRLIST_DECLARED */ |
| 78 | |
Pavel Machek | d8ae7e3 | 2000-02-01 17:17:25 +0000 | [diff] [blame] | 79 | #define NR_SYSCALL_BASE 0 |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 80 | #ifdef LINUX |
| 81 | #ifndef ERESTARTSYS |
| 82 | #define ERESTARTSYS 512 |
| 83 | #endif |
| 84 | #ifndef ERESTARTNOINTR |
| 85 | #define ERESTARTNOINTR 513 |
| 86 | #endif |
| 87 | #ifndef ERESTARTNOHAND |
| 88 | #define ERESTARTNOHAND 514 /* restart if no handler.. */ |
| 89 | #endif |
| 90 | #ifndef ENOIOCTLCMD |
| 91 | #define ENOIOCTLCMD 515 /* No ioctl command */ |
| 92 | #endif |
| 93 | #ifndef NSIG |
| 94 | #define NSIG 32 |
| 95 | #endif |
| 96 | #ifdef ARM |
| 97 | #undef NSIG |
| 98 | #define NSIG 32 |
Pavel Machek | d8ae7e3 | 2000-02-01 17:17:25 +0000 | [diff] [blame] | 99 | #undef NR_SYSCALL_BASE |
| 100 | #define NR_SYSCALL_BASE __NR_SYSCALL_BASE |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 101 | #endif |
| 102 | #endif /* LINUX */ |
| 103 | |
| 104 | #include "syscall.h" |
| 105 | |
| 106 | /* Define these shorthand notations to simplify the syscallent files. */ |
| 107 | #define TF TRACE_FILE |
| 108 | #define TI TRACE_IPC |
| 109 | #define TN TRACE_NETWORK |
| 110 | #define TP TRACE_PROCESS |
| 111 | #define TS TRACE_SIGNAL |
| 112 | |
| 113 | struct sysent sysent0[] = { |
| 114 | #include "syscallent.h" |
| 115 | }; |
| 116 | int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0]; |
| 117 | |
| 118 | #if SUPPORTED_PERSONALITIES >= 2 |
| 119 | struct sysent sysent1[] = { |
| 120 | #include "syscallent1.h" |
| 121 | }; |
| 122 | int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0]; |
| 123 | #endif /* SUPPORTED_PERSONALITIES >= 2 */ |
| 124 | |
| 125 | #if SUPPORTED_PERSONALITIES >= 3 |
| 126 | struct sysent sysent2[] = { |
| 127 | #include "syscallent2.h" |
| 128 | }; |
| 129 | int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0]; |
| 130 | #endif /* SUPPORTED_PERSONALITIES >= 3 */ |
| 131 | |
| 132 | struct sysent *sysent; |
| 133 | int nsyscalls; |
| 134 | |
| 135 | /* Now undef them since short defines cause wicked namespace pollution. */ |
| 136 | #undef TF |
| 137 | #undef TI |
| 138 | #undef TN |
| 139 | #undef TP |
| 140 | #undef TS |
| 141 | |
| 142 | char *errnoent0[] = { |
| 143 | #include "errnoent.h" |
| 144 | }; |
| 145 | int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0]; |
| 146 | |
| 147 | #if SUPPORTED_PERSONALITIES >= 2 |
| 148 | char *errnoent1[] = { |
| 149 | #include "errnoent1.h" |
| 150 | }; |
| 151 | int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0]; |
| 152 | #endif /* SUPPORTED_PERSONALITIES >= 2 */ |
| 153 | |
| 154 | #if SUPPORTED_PERSONALITIES >= 3 |
| 155 | char *errnoent2[] = { |
| 156 | #include "errnoent2.h" |
| 157 | }; |
| 158 | int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0]; |
| 159 | #endif /* SUPPORTED_PERSONALITIES >= 3 */ |
| 160 | |
| 161 | char **errnoent; |
| 162 | int nerrnos; |
| 163 | |
| 164 | int current_personality; |
| 165 | |
| 166 | int |
Wichert Akkerman | e6f876c | 1999-06-22 15:28:30 +0000 | [diff] [blame] | 167 | set_personality(personality) |
| 168 | int personality; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 169 | { |
| 170 | switch (personality) { |
| 171 | case 0: |
| 172 | errnoent = errnoent0; |
| 173 | nerrnos = nerrnos0; |
| 174 | sysent = sysent0; |
| 175 | nsyscalls = nsyscalls0; |
| 176 | ioctlent = ioctlent0; |
| 177 | nioctlents = nioctlents0; |
| 178 | signalent = signalent0; |
| 179 | nsignals = nsignals0; |
| 180 | break; |
| 181 | |
| 182 | #if SUPPORTED_PERSONALITIES >= 2 |
| 183 | case 1: |
| 184 | errnoent = errnoent1; |
| 185 | nerrnos = nerrnos1; |
| 186 | sysent = sysent1; |
| 187 | nsyscalls = nsyscalls1; |
| 188 | ioctlent = ioctlent1; |
| 189 | nioctlents = nioctlents1; |
| 190 | signalent = signalent1; |
| 191 | nsignals = nsignals1; |
| 192 | break; |
| 193 | #endif /* SUPPORTED_PERSONALITIES >= 2 */ |
| 194 | |
| 195 | #if SUPPORTED_PERSONALITIES >= 3 |
| 196 | case 2: |
| 197 | errnoent = errnoent2; |
| 198 | nerrnos = nerrnos2; |
| 199 | sysent = sysent2; |
| 200 | nsyscalls = nsyscalls2; |
| 201 | ioctlent = ioctlent2; |
| 202 | nioctlents = nioctlents2; |
| 203 | signalent = signalent2; |
| 204 | nsignals = nsignals2; |
| 205 | break; |
| 206 | #endif /* SUPPORTED_PERSONALITIES >= 3 */ |
| 207 | |
| 208 | default: |
| 209 | return -1; |
| 210 | } |
| 211 | |
| 212 | current_personality = personality; |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | int qual_flags[MAX_QUALS]; |
| 217 | |
| 218 | static int call_count[MAX_QUALS]; |
| 219 | static int error_count[MAX_QUALS]; |
| 220 | static struct timeval tv_count[MAX_QUALS]; |
| 221 | static int sorted_count[MAX_QUALS]; |
| 222 | |
| 223 | static struct timeval shortest = { 1000000, 0 }; |
| 224 | |
| 225 | static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc(); |
| 226 | |
| 227 | static struct qual_options { |
| 228 | int bitflag; |
| 229 | char *option_name; |
| 230 | int (*lookup)(); |
| 231 | char *argument_name; |
| 232 | } qual_options[] = { |
| 233 | { QUAL_TRACE, "trace", lookup_syscall, "system call" }, |
| 234 | { QUAL_TRACE, "t", lookup_syscall, "system call" }, |
| 235 | { QUAL_ABBREV, "abbrev", lookup_syscall, "system call" }, |
| 236 | { QUAL_ABBREV, "a", lookup_syscall, "system call" }, |
| 237 | { QUAL_VERBOSE, "verbose", lookup_syscall, "system call" }, |
| 238 | { QUAL_VERBOSE, "v", lookup_syscall, "system call" }, |
| 239 | { QUAL_RAW, "raw", lookup_syscall, "system call" }, |
| 240 | { QUAL_RAW, "x", lookup_syscall, "system call" }, |
| 241 | { QUAL_SIGNAL, "signal", lookup_signal, "signal" }, |
| 242 | { QUAL_SIGNAL, "signals", lookup_signal, "signal" }, |
| 243 | { QUAL_SIGNAL, "s", lookup_signal, "signal" }, |
| 244 | { QUAL_FAULT, "fault", lookup_fault, "fault" }, |
| 245 | { QUAL_FAULT, "faults", lookup_fault, "fault" }, |
| 246 | { QUAL_FAULT, "m", lookup_fault, "fault" }, |
| 247 | { QUAL_READ, "read", lookup_desc, "descriptor" }, |
| 248 | { QUAL_READ, "reads", lookup_desc, "descriptor" }, |
| 249 | { QUAL_READ, "r", lookup_desc, "descriptor" }, |
| 250 | { QUAL_WRITE, "write", lookup_desc, "descriptor" }, |
| 251 | { QUAL_WRITE, "writes", lookup_desc, "descriptor" }, |
| 252 | { QUAL_WRITE, "w", lookup_desc, "descriptor" }, |
| 253 | { 0, NULL, NULL, NULL }, |
| 254 | }; |
| 255 | |
| 256 | static int |
| 257 | lookup_syscall(s) |
| 258 | char *s; |
| 259 | { |
| 260 | int i; |
| 261 | |
| 262 | for (i = 0; i < nsyscalls; i++) { |
| 263 | if (strcmp(s, sysent[i].sys_name) == 0) |
| 264 | return i; |
| 265 | } |
| 266 | return -1; |
| 267 | } |
| 268 | |
| 269 | static int |
| 270 | lookup_signal(s) |
| 271 | char *s; |
| 272 | { |
| 273 | int i; |
| 274 | char buf[32]; |
| 275 | |
Wichert Akkerman | 2ee6e45 | 2000-02-18 15:36:12 +0000 | [diff] [blame] | 276 | if (s && *s && isdigit((unsigned char)*s)) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 277 | return atoi(s); |
| 278 | strcpy(buf, s); |
| 279 | s = buf; |
| 280 | for (i = 0; s[i]; i++) |
Wichert Akkerman | 2ee6e45 | 2000-02-18 15:36:12 +0000 | [diff] [blame] | 281 | s[i] = toupper((unsigned char)(s[i])); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 282 | if (strncmp(s, "SIG", 3) == 0) |
| 283 | s += 3; |
| 284 | for (i = 0; i <= NSIG; i++) { |
Nate Sammons | ce780fc | 1999-03-29 23:23:13 +0000 | [diff] [blame] | 285 | if (strcmp(s, signame(i) + 3) == 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 286 | return i; |
| 287 | } |
| 288 | return -1; |
| 289 | } |
| 290 | |
| 291 | static int |
| 292 | lookup_fault(s) |
| 293 | char *s; |
| 294 | { |
| 295 | return -1; |
| 296 | } |
| 297 | |
| 298 | static int |
| 299 | lookup_desc(s) |
| 300 | char *s; |
| 301 | { |
Wichert Akkerman | 2ee6e45 | 2000-02-18 15:36:12 +0000 | [diff] [blame] | 302 | if (s && *s && isdigit((unsigned char)*s)) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 303 | return atoi(s); |
| 304 | return -1; |
| 305 | } |
| 306 | |
| 307 | static int |
| 308 | lookup_class(s) |
| 309 | char *s; |
| 310 | { |
| 311 | if (strcmp(s, "file") == 0) |
| 312 | return TRACE_FILE; |
| 313 | if (strcmp(s, "ipc") == 0) |
| 314 | return TRACE_IPC; |
| 315 | if (strcmp(s, "network") == 0) |
| 316 | return TRACE_NETWORK; |
| 317 | if (strcmp(s, "process") == 0) |
| 318 | return TRACE_PROCESS; |
| 319 | if (strcmp(s, "signal") == 0) |
| 320 | return TRACE_SIGNAL; |
| 321 | return -1; |
| 322 | } |
| 323 | |
| 324 | void |
| 325 | qualify(s) |
| 326 | char *s; |
| 327 | { |
| 328 | struct qual_options *opt; |
| 329 | int not; |
| 330 | char *p; |
| 331 | int i, n; |
| 332 | |
| 333 | opt = &qual_options[0]; |
| 334 | for (i = 0; (p = qual_options[i].option_name); i++) { |
| 335 | n = strlen(p); |
| 336 | if (strncmp(s, p, n) == 0 && s[n] == '=') { |
| 337 | opt = &qual_options[i]; |
| 338 | s += n + 1; |
| 339 | break; |
| 340 | } |
| 341 | } |
| 342 | not = 0; |
| 343 | if (*s == '!') { |
| 344 | not = 1; |
| 345 | s++; |
| 346 | } |
| 347 | if (strcmp(s, "none") == 0) { |
| 348 | not = 1 - not; |
| 349 | s = "all"; |
| 350 | } |
| 351 | if (strcmp(s, "all") == 0) { |
| 352 | for (i = 0; i < MAX_QUALS; i++) { |
| 353 | if (not) |
| 354 | qual_flags[i] &= ~opt->bitflag; |
| 355 | else |
| 356 | qual_flags[i] |= opt->bitflag; |
| 357 | } |
| 358 | return; |
| 359 | } |
| 360 | for (i = 0; i < MAX_QUALS; i++) { |
| 361 | if (not) |
| 362 | qual_flags[i] |= opt->bitflag; |
| 363 | else |
| 364 | qual_flags[i] &= ~opt->bitflag; |
| 365 | } |
| 366 | for (p = strtok(s, ","); p; p = strtok(NULL, ",")) { |
| 367 | if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) { |
| 368 | for (i = 0; i < MAX_QUALS; i++) { |
| 369 | if (sysent[i].sys_flags & n) { |
| 370 | if (not) |
| 371 | qual_flags[i] &= ~opt->bitflag; |
| 372 | else |
| 373 | qual_flags[i] |= opt->bitflag; |
| 374 | } |
| 375 | } |
| 376 | continue; |
| 377 | } |
| 378 | if ((n = (*opt->lookup)(p)) < 0) { |
| 379 | fprintf(stderr, "strace: invalid %s `%s'\n", |
| 380 | opt->argument_name, p); |
| 381 | exit(1); |
| 382 | } |
| 383 | if (not) |
| 384 | qual_flags[n] &= ~opt->bitflag; |
| 385 | else |
| 386 | qual_flags[n] |= opt->bitflag; |
| 387 | } |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | static void |
| 392 | dumpio(tcp) |
| 393 | struct tcb *tcp; |
| 394 | { |
| 395 | if (syserror(tcp)) |
| 396 | return; |
| 397 | if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS) |
| 398 | return; |
Pavel Machek | d8ae7e3 | 2000-02-01 17:17:25 +0000 | [diff] [blame] | 399 | switch (tcp->scno + NR_SYSCALL_BASE) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 400 | case SYS_read: |
| 401 | #ifdef SYS_recv |
| 402 | case SYS_recv: |
| 403 | #endif |
| 404 | #ifdef SYS_recvfrom |
| 405 | case SYS_recvfrom: |
| 406 | #endif |
| 407 | if (qual_flags[tcp->u_arg[0]] & QUAL_READ) |
| 408 | dumpstr(tcp, tcp->u_arg[1], tcp->u_rval); |
| 409 | break; |
| 410 | case SYS_write: |
| 411 | #ifdef SYS_send |
| 412 | case SYS_send: |
| 413 | #endif |
| 414 | #ifdef SYS_sendto |
| 415 | case SYS_sendto: |
| 416 | #endif |
| 417 | if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) |
| 418 | dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]); |
| 419 | break; |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame^] | 420 | #ifdef SYS_readv |
| 421 | case SYS_readv: |
| 422 | if (qual_flags[tcp->u_arg[0]] & QUAL_READ) |
| 423 | dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]); |
| 424 | break; |
| 425 | #endif |
| 426 | #ifdef SYS_writev |
| 427 | case SYS_writev: |
| 428 | |
| 429 | if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) |
| 430 | dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]); |
| 431 | break; |
| 432 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 436 | #ifndef FREEBSD |
Wichert Akkerman | 8829a55 | 1999-06-11 13:18:40 +0000 | [diff] [blame] | 437 | enum subcall_style { shift_style, deref_style, mask_style, door_style }; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 438 | #else /* FREEBSD */ |
| 439 | enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style }; |
| 440 | |
| 441 | struct subcall { |
| 442 | int call; |
| 443 | int nsubcalls; |
| 444 | int subcalls[5]; |
| 445 | }; |
| 446 | |
| 447 | const struct subcall subcalls_table[] = { |
| 448 | { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } }, |
John Hughes | 61eeb55 | 2001-03-06 15:51:53 +0000 | [diff] [blame] | 449 | #ifdef SYS_semconfig |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 450 | { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } }, |
John Hughes | 61eeb55 | 2001-03-06 15:51:53 +0000 | [diff] [blame] | 451 | #else |
| 452 | { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } }, |
| 453 | #endif |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 454 | { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } }, |
| 455 | }; |
| 456 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 457 | |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 458 | #if !(defined(LINUX) && ( defined(ALPHA) || defined(IA64) || defined(MIPS) )) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 459 | |
| 460 | const int socket_map [] = { |
| 461 | /* SYS_SOCKET */ 97, |
| 462 | /* SYS_BIND */ 104, |
| 463 | /* SYS_CONNECT */ 98, |
| 464 | /* SYS_LISTEN */ 106, |
| 465 | /* SYS_ACCEPT */ 99, |
| 466 | /* SYS_GETSOCKNAME */ 150, |
| 467 | /* SYS_GETPEERNAME */ 141, |
| 468 | /* SYS_SOCKETPAIR */ 135, |
| 469 | /* SYS_SEND */ 101, |
| 470 | /* SYS_RECV */ 102, |
| 471 | /* SYS_SENDTO */ 133, |
| 472 | /* SYS_RECVFROM */ 125, |
| 473 | /* SYS_SHUTDOWN */ 134, |
| 474 | /* SYS_SETSOCKOPT */ 105, |
| 475 | /* SYS_GETSOCKOPT */ 118, |
| 476 | /* SYS_SENDMSG */ 114, |
| 477 | /* SYS_RECVMSG */ 113 |
| 478 | }; |
| 479 | |
| 480 | void |
Wichert Akkerman | e6f876c | 1999-06-22 15:28:30 +0000 | [diff] [blame] | 481 | sparc_socket_decode (tcp) |
| 482 | struct tcb *tcp; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 483 | { |
| 484 | volatile long addr; |
| 485 | volatile int i, n; |
| 486 | |
| 487 | if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){ |
| 488 | return; |
| 489 | } |
| 490 | tcp->scno = socket_map [tcp->u_arg [0]-1]; |
| 491 | n = tcp->u_nargs = sysent [tcp->scno].nargs; |
| 492 | addr = tcp->u_arg [1]; |
| 493 | for (i = 0; i < n; i++){ |
| 494 | int arg; |
| 495 | if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0) |
| 496 | arg = 0; |
| 497 | tcp->u_arg [i] = arg; |
| 498 | addr += sizeof (arg); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | static void |
| 503 | decode_subcall(tcp, subcall, nsubcalls, style) |
| 504 | struct tcb *tcp; |
| 505 | int subcall; |
| 506 | int nsubcalls; |
| 507 | enum subcall_style style; |
| 508 | { |
| 509 | int i, addr, mask, arg; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 510 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 511 | switch (style) { |
| 512 | case shift_style: |
Wichert Akkerman | 16a03d2 | 2000-08-10 02:14:04 +0000 | [diff] [blame] | 513 | if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls) |
| 514 | return; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 515 | tcp->scno = subcall + tcp->u_arg[0]; |
| 516 | if (sysent[tcp->scno].nargs != -1) |
| 517 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 518 | else |
| 519 | tcp->u_nargs--; |
| 520 | for (i = 0; i < tcp->u_nargs; i++) |
| 521 | tcp->u_arg[i] = tcp->u_arg[i + 1]; |
| 522 | break; |
| 523 | case deref_style: |
Wichert Akkerman | 16a03d2 | 2000-08-10 02:14:04 +0000 | [diff] [blame] | 524 | if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls) |
| 525 | return; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 526 | tcp->scno = subcall + tcp->u_arg[0]; |
| 527 | addr = tcp->u_arg[1]; |
| 528 | for (i = 0; i < sysent[tcp->scno].nargs; i++) { |
| 529 | if (umove(tcp, addr, &arg) < 0) |
| 530 | arg = 0; |
| 531 | tcp->u_arg[i] = arg; |
| 532 | addr += sizeof(arg); |
| 533 | } |
| 534 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 535 | break; |
| 536 | case mask_style: |
| 537 | mask = (tcp->u_arg[0] >> 8) & 0xff; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 538 | for (i = 0; mask; i++) |
| 539 | mask >>= 1; |
Wichert Akkerman | 16a03d2 | 2000-08-10 02:14:04 +0000 | [diff] [blame] | 540 | if (i >= nsubcalls) |
| 541 | return; |
| 542 | tcp->u_arg[0] &= 0xff; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 543 | tcp->scno = subcall + i; |
| 544 | if (sysent[tcp->scno].nargs != -1) |
| 545 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 546 | break; |
Wichert Akkerman | 8829a55 | 1999-06-11 13:18:40 +0000 | [diff] [blame] | 547 | case door_style: |
| 548 | /* |
| 549 | * Oh, yuck. The call code is the *sixth* argument. |
Wichert Akkerman | 16a03d2 | 2000-08-10 02:14:04 +0000 | [diff] [blame] | 550 | * (don't you mean the *last* argument? - JH) |
Wichert Akkerman | 8829a55 | 1999-06-11 13:18:40 +0000 | [diff] [blame] | 551 | */ |
Wichert Akkerman | 16a03d2 | 2000-08-10 02:14:04 +0000 | [diff] [blame] | 552 | if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls) |
| 553 | return; |
Wichert Akkerman | 8829a55 | 1999-06-11 13:18:40 +0000 | [diff] [blame] | 554 | tcp->scno = subcall + tcp->u_arg[5]; |
| 555 | if (sysent[tcp->scno].nargs != -1) |
| 556 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 557 | else |
| 558 | tcp->u_nargs--; |
| 559 | break; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 560 | #ifdef FREEBSD |
| 561 | case table_style: |
| 562 | for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++) |
| 563 | if (subcalls_table[i].call == tcp->scno) break; |
| 564 | if (i < sizeof(subcalls_table) / sizeof(struct subcall) && |
| 565 | tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) { |
| 566 | tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]]; |
| 567 | for (i = 0; i < tcp->u_nargs; i++) |
| 568 | tcp->u_arg[i] = tcp->u_arg[i + 1]; |
| 569 | } |
| 570 | break; |
| 571 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | #endif |
| 575 | |
| 576 | struct tcb *tcp_last = NULL; |
| 577 | |
| 578 | static int |
| 579 | internal_syscall(tcp) |
| 580 | struct tcb *tcp; |
| 581 | { |
| 582 | /* |
| 583 | * We must always trace a few critical system calls in order to |
| 584 | * correctly support following forks in the presence of tracing |
| 585 | * qualifiers. |
| 586 | */ |
Pavel Machek | d8ae7e3 | 2000-02-01 17:17:25 +0000 | [diff] [blame] | 587 | switch (tcp->scno + NR_SYSCALL_BASE) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 588 | #ifdef SYS_fork |
| 589 | case SYS_fork: |
| 590 | #endif |
| 591 | #ifdef SYS_vfork |
| 592 | case SYS_vfork: |
| 593 | #endif |
John Hughes | 4e36a81 | 2001-04-18 15:11:51 +0000 | [diff] [blame] | 594 | #ifdef SYS_fork1 |
| 595 | case SYS_fork1: |
| 596 | #endif |
| 597 | #ifdef SYS_forkall |
| 598 | case SYS_forkall: |
| 599 | #endif |
| 600 | #ifdef SYS_rfork1 |
| 601 | case SYS_rfork1: |
| 602 | #endif |
| 603 | #ifdef SYS_rforkall |
| 604 | case SYS_rforkall: |
| 605 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 606 | internal_fork(tcp); |
| 607 | break; |
Wichert Akkerman | 7a0b649 | 1999-12-23 15:08:17 +0000 | [diff] [blame] | 608 | #ifdef SYS_clone |
| 609 | case SYS_clone: |
| 610 | internal_clone(tcp); |
| 611 | break; |
| 612 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 613 | #ifdef SYS_execv |
| 614 | case SYS_execv: |
| 615 | #endif |
| 616 | #ifdef SYS_execve |
| 617 | case SYS_execve: |
| 618 | #endif |
John Hughes | 4e36a81 | 2001-04-18 15:11:51 +0000 | [diff] [blame] | 619 | #ifdef SYS_rexecve |
| 620 | case SYS_rexecve: |
| 621 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 622 | internal_exec(tcp); |
| 623 | break; |
| 624 | |
| 625 | #ifdef SYS_wait |
| 626 | case SYS_wait: |
| 627 | #endif |
| 628 | #ifdef SYS_wait4 |
| 629 | case SYS_wait4: |
| 630 | #endif |
| 631 | #ifdef SYS_waitpid |
| 632 | case SYS_waitpid: |
| 633 | #endif |
| 634 | #ifdef SYS_waitsys |
| 635 | case SYS_waitsys: |
| 636 | #endif |
| 637 | internal_wait(tcp); |
| 638 | break; |
| 639 | |
| 640 | #ifdef SYS_exit |
| 641 | case SYS_exit: |
| 642 | #endif |
| 643 | internal_exit(tcp); |
| 644 | break; |
| 645 | } |
| 646 | return 0; |
| 647 | } |
| 648 | |
Wichert Akkerman | c792698 | 2000-04-10 22:22:31 +0000 | [diff] [blame] | 649 | |
| 650 | #ifdef LINUX |
| 651 | #if defined (I386) |
| 652 | static long eax; |
| 653 | #elif defined (IA64) |
| 654 | long r8, r10, psr; |
| 655 | long ia32 = 0; |
| 656 | #elif defined (POWERPC) |
| 657 | static long result,flags; |
| 658 | #elif defined (M68K) |
| 659 | static int d0; |
| 660 | #elif defined (ARM) |
| 661 | static int r0; |
| 662 | #elif defined (ALPHA) |
| 663 | static long r0; |
| 664 | static long a3; |
| 665 | #elif defined (SPARC) |
Wichert Akkerman | 00a82ee | 2001-03-28 20:29:17 +0000 | [diff] [blame] | 666 | static struct regs regs; |
Wichert Akkerman | c792698 | 2000-04-10 22:22:31 +0000 | [diff] [blame] | 667 | static unsigned long trap; |
| 668 | #elif defined(MIPS) |
| 669 | static long a3; |
| 670 | static long r2; |
| 671 | #elif defined(S390) |
| 672 | static long gpr2; |
| 673 | static long pc; |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 674 | #elif defined(HPPA) |
| 675 | static long r28; |
Wichert Akkerman | c792698 | 2000-04-10 22:22:31 +0000 | [diff] [blame] | 676 | #endif |
| 677 | #endif /* LINUX */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 678 | #ifdef FREEBSD |
| 679 | struct reg regs; |
| 680 | #endif /* FREEBSD */ |
Wichert Akkerman | c792698 | 2000-04-10 22:22:31 +0000 | [diff] [blame] | 681 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 682 | int |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 683 | get_scno(tcp) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 684 | struct tcb *tcp; |
| 685 | { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 686 | long scno = 0; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 687 | #ifndef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 688 | int pid = tcp->pid; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 689 | #endif /* !PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 690 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 691 | #ifdef LINUX |
Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 692 | #if defined(S390) |
| 693 | if (upeek(tcp->pid,PT_PSWADDR,&pc) < 0) |
| 694 | return -1; |
| 695 | scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-4),0); |
| 696 | if (errno) |
| 697 | return -1; |
| 698 | scno&=0xFF; |
| 699 | #elif defined (POWERPC) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 700 | if (upeek(pid, 4*PT_R0, &scno) < 0) |
| 701 | return -1; |
| 702 | if (!(tcp->flags & TCB_INSYSCALL)) { |
| 703 | /* Check if we return from execve. */ |
| 704 | if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) { |
| 705 | tcp->flags &= ~TCB_WAITEXECVE; |
| 706 | return 0; |
| 707 | } |
| 708 | } |
| 709 | #elif defined (I386) |
| 710 | if (upeek(pid, 4*ORIG_EAX, &scno) < 0) |
| 711 | return -1; |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 712 | #elif defined(IA64) |
| 713 | #define IA64_PSR_IS ((long)1 << 34) |
| 714 | if (upeek (pid, PT_CR_IPSR, &psr) >= 0) |
| 715 | ia32 = (psr & IA64_PSR_IS); |
| 716 | if (!(tcp->flags & TCB_INSYSCALL)) { |
| 717 | if (ia32) { |
| 718 | if (upeek(pid, PT_R8, &scno) < 0) |
| 719 | return -1; |
| 720 | } else { |
| 721 | if (upeek (pid, PT_R15, &scno) < 0) |
| 722 | return -1; |
| 723 | } |
| 724 | } else { |
| 725 | /* syscall in progress */ |
| 726 | if (upeek (pid, PT_R8, &r8) < 0) |
| 727 | return -1; |
| 728 | if (upeek (pid, PT_R10, &r10) < 0) |
| 729 | return -1; |
| 730 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 731 | #elif defined (ARM) |
| 732 | { |
| 733 | long pc; |
| 734 | upeek(pid, 4*15, &pc); |
| 735 | umoven(tcp, pc-4, 4, (char *)&scno); |
| 736 | scno &= 0x000fffff; |
| 737 | } |
| 738 | #elif defined (M68K) |
| 739 | if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0) |
| 740 | return -1; |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 741 | #elif defined (MIPS) |
| 742 | if (upeek(pid, REG_A3, &a3) < 0) |
| 743 | return -1; |
| 744 | |
| 745 | if(!(tcp->flags & TCB_INSYSCALL)) { |
| 746 | if (upeek(pid, REG_V0, &scno) < 0) |
| 747 | return -1; |
| 748 | |
| 749 | if (scno < 0 || scno > nsyscalls) { |
| 750 | if(a3 == 0 || a3 == -1) { |
| 751 | if(debug) |
| 752 | fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno); |
| 753 | return 0; |
| 754 | } |
| 755 | } |
| 756 | } else { |
| 757 | if (upeek(pid, REG_V0, &r2) < 0) |
| 758 | return -1; |
| 759 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 760 | #elif defined (ALPHA) |
| 761 | if (upeek(pid, REG_A3, &a3) < 0) |
| 762 | return -1; |
| 763 | |
| 764 | if (!(tcp->flags & TCB_INSYSCALL)) { |
| 765 | if (upeek(pid, REG_R0, &scno) < 0) |
| 766 | return -1; |
| 767 | |
| 768 | /* Check if we return from execve. */ |
| 769 | if (scno == 0 && tcp->flags & TCB_WAITEXECVE) { |
| 770 | tcp->flags &= ~TCB_WAITEXECVE; |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | /* |
| 775 | * Do some sanity checks to figure out if it's |
| 776 | * really a syscall entry |
| 777 | */ |
| 778 | if (scno < 0 || scno > nsyscalls) { |
| 779 | if (a3 == 0 || a3 == -1) { |
| 780 | if (debug) |
| 781 | fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno); |
| 782 | return 0; |
| 783 | } |
| 784 | } |
| 785 | } |
| 786 | else { |
| 787 | if (upeek(pid, REG_R0, &r0) < 0) |
| 788 | return -1; |
| 789 | } |
| 790 | #elif defined (SPARC) |
| 791 | /* Everything we need is in the current register set. */ |
| 792 | if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0) |
| 793 | return -1; |
| 794 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 795 | /* If we are entering, then disassemble the syscall trap. */ |
| 796 | if (!(tcp->flags & TCB_INSYSCALL)) { |
| 797 | /* Retrieve the syscall trap instruction. */ |
| 798 | errno = 0; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 799 | trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 800 | if (errno) |
| 801 | return -1; |
| 802 | |
| 803 | /* Disassemble the trap to see what personality to use. */ |
| 804 | switch (trap) { |
| 805 | case 0x91d02010: |
| 806 | /* Linux/SPARC syscall trap. */ |
| 807 | set_personality(0); |
| 808 | break; |
Wichert Akkerman | dacfb6e | 1999-06-03 14:21:07 +0000 | [diff] [blame] | 809 | case 0x91d0206d: |
| 810 | /* Linux/SPARC64 syscall trap. */ |
| 811 | fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n"); |
| 812 | return -1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 813 | case 0x91d02000: |
| 814 | /* SunOS syscall trap. (pers 1) */ |
| 815 | fprintf(stderr,"syscall: SunOS no support\n"); |
| 816 | return -1; |
| 817 | case 0x91d02008: |
| 818 | /* Solaris 2.x syscall trap. (per 2) */ |
| 819 | set_personality(1); |
| 820 | break; |
| 821 | case 0x91d02009: |
| 822 | /* NetBSD/FreeBSD syscall trap. */ |
| 823 | fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n"); |
| 824 | return -1; |
| 825 | case 0x91d02027: |
| 826 | /* Solaris 2.x gettimeofday */ |
| 827 | set_personality(1); |
| 828 | break; |
| 829 | default: |
| 830 | /* Unknown syscall trap. */ |
| 831 | if(tcp->flags & TCB_WAITEXECVE) { |
| 832 | tcp->flags &= ~TCB_WAITEXECVE; |
| 833 | return 0; |
| 834 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 835 | fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 836 | return -1; |
| 837 | } |
| 838 | |
| 839 | /* Extract the system call number from the registers. */ |
| 840 | if (trap == 0x91d02027) |
| 841 | scno = 156; |
| 842 | else |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 843 | scno = regs.r_g1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 844 | if (scno == 0) { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 845 | scno = regs.r_o0; |
| 846 | memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0)); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 847 | } |
| 848 | } |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 849 | #elif defined(HPPA) |
| 850 | if (upeek(pid, PT_GR20, &scno) < 0) |
| 851 | return -1; |
| 852 | if (!(tcp->flags & TCB_INSYSCALL)) { |
| 853 | /* Check if we return from execve. */ |
| 854 | if ((tcp->flags & TCB_WAITEXECVE)) { |
| 855 | tcp->flags &= ~TCB_WAITEXECVE; |
| 856 | return 0; |
| 857 | } |
| 858 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 859 | #endif |
| 860 | #endif /* LINUX */ |
| 861 | #ifdef SUNOS4 |
| 862 | if (upeek(pid, uoff(u_arg[7]), &scno) < 0) |
| 863 | return -1; |
| 864 | #endif |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 865 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 866 | #ifdef HAVE_PR_SYSCALL |
John Hughes | 2529971 | 2001-03-06 10:10:06 +0000 | [diff] [blame] | 867 | scno = tcp->status.PR_SYSCALL; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 868 | #else /* !HAVE_PR_SYSCALL */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 869 | #ifndef FREEBSD |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 870 | scno = tcp->status.PR_WHAT; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 871 | #else /* FREEBSD */ |
| 872 | if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) { |
| 873 | perror("pread"); |
| 874 | return -1; |
| 875 | } |
| 876 | switch (regs.r_eax) { |
| 877 | case SYS_syscall: |
| 878 | case SYS___syscall: |
| 879 | pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int)); |
| 880 | break; |
| 881 | default: |
| 882 | scno = regs.r_eax; |
| 883 | break; |
| 884 | } |
| 885 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 886 | #endif /* !HAVE_PR_SYSCALL */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 887 | #endif /* USE_PROCFS */ |
Wichert Akkerman | 5ae21ea | 2000-05-01 01:53:59 +0000 | [diff] [blame] | 888 | if (!(tcp->flags & TCB_INSYSCALL)) |
| 889 | tcp->scno = scno; |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 890 | return 1; |
| 891 | } |
| 892 | |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 893 | |
| 894 | int |
| 895 | syscall_fixup(tcp) |
| 896 | struct tcb *tcp; |
| 897 | { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 898 | #ifndef USE_PROCFS |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 899 | int pid = tcp->pid; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 900 | #else /* USE_PROCFS */ |
| 901 | int scno = tcp->scno; |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 902 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 903 | if (!(tcp->flags & TCB_INSYSCALL)) { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 904 | if (tcp->status.PR_WHY != PR_SYSENTRY) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 905 | if ( |
| 906 | scno == SYS_fork |
| 907 | #ifdef SYS_vfork |
| 908 | || scno == SYS_vfork |
| 909 | #endif /* SYS_vfork */ |
John Hughes | 4e36a81 | 2001-04-18 15:11:51 +0000 | [diff] [blame] | 910 | #ifdef SYS_fork1 |
| 911 | || scno == SYS_fork1 |
| 912 | #endif /* SYS_fork1 */ |
| 913 | #ifdef SYS_forkall |
| 914 | || scno == SYS_forkall |
| 915 | #endif /* SYS_forkall */ |
| 916 | #ifdef SYS_rfork1 |
| 917 | || scno == SYS_rfork1 |
| 918 | #endif /* SYS_fork1 */ |
| 919 | #ifdef SYS_rforkall |
| 920 | || scno == SYS_rforkall |
| 921 | #endif /* SYS_rforkall */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 922 | ) { |
| 923 | /* We are returning in the child, fake it. */ |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 924 | tcp->status.PR_WHY = PR_SYSENTRY; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 925 | trace_syscall(tcp); |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 926 | tcp->status.PR_WHY = PR_SYSEXIT; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 927 | } |
| 928 | else { |
| 929 | fprintf(stderr, "syscall: missing entry\n"); |
| 930 | tcp->flags |= TCB_INSYSCALL; |
| 931 | } |
| 932 | } |
| 933 | } |
| 934 | else { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 935 | if (tcp->status.PR_WHY != PR_SYSEXIT) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 936 | fprintf(stderr, "syscall: missing exit\n"); |
| 937 | tcp->flags &= ~TCB_INSYSCALL; |
| 938 | } |
| 939 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 940 | #endif /* USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 941 | #ifdef SUNOS4 |
| 942 | if (!(tcp->flags & TCB_INSYSCALL)) { |
| 943 | if (scno == 0) { |
| 944 | fprintf(stderr, "syscall: missing entry\n"); |
| 945 | tcp->flags |= TCB_INSYSCALL; |
| 946 | } |
| 947 | } |
| 948 | else { |
| 949 | if (scno != 0) { |
| 950 | if (debug) { |
| 951 | /* |
| 952 | * This happens when a signal handler |
| 953 | * for a signal which interrupted a |
| 954 | * a system call makes another system call. |
| 955 | */ |
| 956 | fprintf(stderr, "syscall: missing exit\n"); |
| 957 | } |
| 958 | tcp->flags &= ~TCB_INSYSCALL; |
| 959 | } |
| 960 | } |
| 961 | #endif /* SUNOS4 */ |
| 962 | #ifdef LINUX |
| 963 | #if defined (I386) |
| 964 | if (upeek(pid, 4*EAX, &eax) < 0) |
| 965 | return -1; |
| 966 | if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) { |
| 967 | if (debug) |
| 968 | fprintf(stderr, "stray syscall exit: eax = %ld\n", eax); |
| 969 | return 0; |
| 970 | } |
Wichert Akkerman | 12f75d1 | 2000-02-14 16:23:40 +0000 | [diff] [blame] | 971 | #elif defined (S390) |
| 972 | if (upeek(pid, PT_GPR2, &gpr2) < 0) |
| 973 | return -1; |
| 974 | if (gpr2 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) { |
| 975 | if (debug) |
| 976 | fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2); |
| 977 | return 0; |
| 978 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 979 | #elif defined (POWERPC) |
| 980 | # define SO_MASK 0x10000000 |
| 981 | if (upeek(pid, 4*PT_CCR, &flags) < 0) |
| 982 | return -1; |
| 983 | if (upeek(pid, 4*PT_R3, &result) < 0) |
| 984 | return -1; |
| 985 | if (flags & SO_MASK) |
| 986 | result = -result; |
| 987 | #elif defined (M68K) |
| 988 | if (upeek(pid, 4*PT_D0, &d0) < 0) |
| 989 | return -1; |
| 990 | if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) { |
| 991 | if (debug) |
| 992 | fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0); |
| 993 | return 0; |
| 994 | } |
| 995 | #elif defined (ARM) |
| 996 | if (upeek(pid, 4*0, (long *)&r0) < 0) |
| 997 | return -1; |
| 998 | if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) { |
| 999 | if (debug) |
| 1000 | fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0); |
| 1001 | return 0; |
| 1002 | } |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1003 | #elif defined (HPPA) |
| 1004 | if (upeek(pid, PT_GR28, &r28) < 0) |
| 1005 | return -1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1006 | #endif |
| 1007 | #endif /* LINUX */ |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1008 | return 1; |
| 1009 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1010 | |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1011 | int |
| 1012 | get_error(tcp) |
| 1013 | struct tcb *tcp; |
| 1014 | { |
| 1015 | int u_error = 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1016 | #ifdef LINUX |
Wichert Akkerman | 12f75d1 | 2000-02-14 16:23:40 +0000 | [diff] [blame] | 1017 | #ifdef S390 |
| 1018 | if (gpr2 && (unsigned) -gpr2 < nerrnos) { |
| 1019 | tcp->u_rval = -1; |
| 1020 | u_error = -gpr2; |
| 1021 | } |
| 1022 | else { |
| 1023 | tcp->u_rval = gpr2; |
| 1024 | u_error = 0; |
| 1025 | } |
| 1026 | #else /* !S390 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1027 | #ifdef I386 |
| 1028 | if (eax < 0 && -eax < nerrnos) { |
| 1029 | tcp->u_rval = -1; |
| 1030 | u_error = -eax; |
| 1031 | } |
| 1032 | else { |
| 1033 | tcp->u_rval = eax; |
| 1034 | u_error = 0; |
| 1035 | } |
| 1036 | #else /* !I386 */ |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1037 | #ifdef IA64 |
| 1038 | if (ia32) { |
| 1039 | int err; |
| 1040 | |
| 1041 | err = (int)r8; |
| 1042 | if (err < 0 && -err < nerrnos) { |
| 1043 | tcp->u_rval = -1; |
| 1044 | u_error = -err; |
| 1045 | } |
| 1046 | else { |
| 1047 | tcp->u_rval = err; |
| 1048 | u_error = 0; |
| 1049 | } |
| 1050 | } else { |
| 1051 | if (r10) { |
| 1052 | tcp->u_rval = -1; |
| 1053 | u_error = r8; |
| 1054 | } else { |
| 1055 | tcp->u_rval = r8; |
| 1056 | u_error = 0; |
| 1057 | } |
| 1058 | } |
| 1059 | #else /* !IA64 */ |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 1060 | #ifdef MIPS |
| 1061 | if (a3) { |
| 1062 | tcp->u_rval = -1; |
| 1063 | u_error = r2; |
| 1064 | } else { |
| 1065 | tcp->u_rval = r2; |
| 1066 | u_error = 0; |
| 1067 | } |
| 1068 | #else |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1069 | #ifdef POWERPC |
| 1070 | if (result && (unsigned) -result < nerrnos) { |
| 1071 | tcp->u_rval = -1; |
| 1072 | u_error = -result; |
| 1073 | } |
| 1074 | else { |
| 1075 | tcp->u_rval = result; |
| 1076 | u_error = 0; |
| 1077 | } |
| 1078 | #else /* !POWERPC */ |
| 1079 | #ifdef M68K |
| 1080 | if (d0 && (unsigned) -d0 < nerrnos) { |
| 1081 | tcp->u_rval = -1; |
| 1082 | u_error = -d0; |
| 1083 | } |
| 1084 | else { |
| 1085 | tcp->u_rval = d0; |
| 1086 | u_error = 0; |
| 1087 | } |
| 1088 | #else /* !M68K */ |
| 1089 | #ifdef ARM |
| 1090 | if (r0 && (unsigned) -r0 < nerrnos) { |
| 1091 | tcp->u_rval = -1; |
| 1092 | u_error = -r0; |
| 1093 | } |
| 1094 | else { |
| 1095 | tcp->u_rval = r0; |
| 1096 | u_error = 0; |
| 1097 | } |
| 1098 | #else /* !ARM */ |
| 1099 | #ifdef ALPHA |
| 1100 | if (a3) { |
| 1101 | tcp->u_rval = -1; |
| 1102 | u_error = r0; |
| 1103 | } |
| 1104 | else { |
| 1105 | tcp->u_rval = r0; |
| 1106 | u_error = 0; |
| 1107 | } |
| 1108 | #else /* !ALPHA */ |
| 1109 | #ifdef SPARC |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1110 | if (regs.r_psr & PSR_C) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1111 | tcp->u_rval = -1; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1112 | u_error = regs.r_o0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1113 | } |
| 1114 | else { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1115 | tcp->u_rval = regs.r_o0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1116 | u_error = 0; |
| 1117 | } |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1118 | #else /* !SPARC */ |
| 1119 | #ifdef HPPA |
| 1120 | if (r28 && (unsigned) -r28 < nerrnos) { |
| 1121 | tcp->u_rval = -1; |
| 1122 | u_error = -r28; |
| 1123 | } |
| 1124 | else { |
| 1125 | tcp->u_rval = r28; |
| 1126 | u_error = 0; |
| 1127 | } |
| 1128 | #endif /* HPPA */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1129 | #endif /* SPARC */ |
| 1130 | #endif /* ALPHA */ |
| 1131 | #endif /* ARM */ |
| 1132 | #endif /* M68K */ |
| 1133 | #endif /* POWERPC */ |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 1134 | #endif /* MIPS */ |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1135 | #endif /* IA64 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1136 | #endif /* I386 */ |
Wichert Akkerman | 12f75d1 | 2000-02-14 16:23:40 +0000 | [diff] [blame] | 1137 | #endif /* S390 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1138 | #endif /* LINUX */ |
| 1139 | #ifdef SUNOS4 |
| 1140 | /* get error code from user struct */ |
| 1141 | if (upeek(pid, uoff(u_error), &u_error) < 0) |
| 1142 | return -1; |
| 1143 | u_error >>= 24; /* u_error is a char */ |
| 1144 | |
| 1145 | /* get system call return value */ |
| 1146 | if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0) |
| 1147 | return -1; |
| 1148 | #endif /* SUNOS4 */ |
| 1149 | #ifdef SVR4 |
| 1150 | #ifdef SPARC |
| 1151 | /* Judicious guessing goes a long way. */ |
| 1152 | if (tcp->status.pr_reg[R_PSR] & 0x100000) { |
| 1153 | tcp->u_rval = -1; |
| 1154 | u_error = tcp->status.pr_reg[R_O0]; |
| 1155 | } |
| 1156 | else { |
| 1157 | tcp->u_rval = tcp->status.pr_reg[R_O0]; |
| 1158 | u_error = 0; |
| 1159 | } |
| 1160 | #endif /* SPARC */ |
| 1161 | #ifdef I386 |
| 1162 | /* Wanna know how to kill an hour single-stepping? */ |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1163 | if (tcp->status.PR_REG[EFL] & 0x1) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1164 | tcp->u_rval = -1; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1165 | u_error = tcp->status.PR_REG[EAX]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1166 | } |
| 1167 | else { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1168 | tcp->u_rval = tcp->status.PR_REG[EAX]; |
Wichert Akkerman | 16a03d2 | 2000-08-10 02:14:04 +0000 | [diff] [blame] | 1169 | #ifdef HAVE_LONG_LONG |
| 1170 | tcp->u_lrval = |
| 1171 | ((unsigned long long) tcp->status.PR_REG[EDX] << 32) + |
| 1172 | tcp->status.PR_REG[EAX]; |
| 1173 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1174 | u_error = 0; |
| 1175 | } |
| 1176 | #endif /* I386 */ |
| 1177 | #ifdef MIPS |
| 1178 | if (tcp->status.pr_reg[CTX_A3]) { |
| 1179 | tcp->u_rval = -1; |
| 1180 | u_error = tcp->status.pr_reg[CTX_V0]; |
| 1181 | } |
| 1182 | else { |
| 1183 | tcp->u_rval = tcp->status.pr_reg[CTX_V0]; |
| 1184 | u_error = 0; |
| 1185 | } |
| 1186 | #endif /* MIPS */ |
| 1187 | #endif /* SVR4 */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1188 | #ifdef FREEBSD |
| 1189 | if (regs.r_eflags & PSL_C) { |
| 1190 | tcp->u_rval = -1; |
| 1191 | u_error = regs.r_eax; |
| 1192 | } else { |
| 1193 | tcp->u_rval = regs.r_eax; |
| 1194 | tcp->u_lrval = |
| 1195 | ((unsigned long long) regs.r_edx << 32) + regs.r_eax; |
| 1196 | u_error = 0; |
| 1197 | } |
| 1198 | #endif /* FREEBSD */ |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1199 | tcp->u_error = u_error; |
| 1200 | return 1; |
| 1201 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1202 | |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1203 | int syscall_enter(tcp) |
| 1204 | struct tcb *tcp; |
| 1205 | { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1206 | #ifndef USE_PROCFS |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1207 | int pid = tcp->pid; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1208 | #endif /* !USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1209 | #ifdef LINUX |
Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 1210 | #if defined(S390) |
| 1211 | { |
| 1212 | int i; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1213 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
| 1214 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1215 | else |
| 1216 | tcp->u_nargs = MAX_ARGS; |
Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 1217 | for (i = 0; i < tcp->u_nargs; i++) { |
| 1218 | if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+(i<<2), &tcp->u_arg[i]) < 0) |
| 1219 | return -1; |
| 1220 | } |
| 1221 | } |
| 1222 | #elif defined (ALPHA) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1223 | { |
| 1224 | int i; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1225 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
| 1226 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1227 | else |
| 1228 | tcp->u_nargs = MAX_ARGS; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1229 | for (i = 0; i < tcp->u_nargs; i++) { |
Wichert Akkerman | b859bea | 1999-04-18 22:50:50 +0000 | [diff] [blame] | 1230 | /* WTA: if scno is out-of-bounds this will bomb. Add range-check |
| 1231 | * for scno somewhere above here! |
| 1232 | */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1233 | if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0) |
| 1234 | return -1; |
| 1235 | } |
| 1236 | } |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1237 | #elif defined (IA64) |
| 1238 | { |
Wichert Akkerman | 5ae21ea | 2000-05-01 01:53:59 +0000 | [diff] [blame] | 1239 | unsigned long *bsp, cfm, i; |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1240 | |
| 1241 | if (upeek(pid, PT_AR_BSP, (long *) &bsp) < 0) |
| 1242 | return -1; |
Wichert Akkerman | 5ae21ea | 2000-05-01 01:53:59 +0000 | [diff] [blame] | 1243 | if (upeek(pid, PT_CFM, (long *) &cfm) < 0) |
| 1244 | return -1; |
| 1245 | |
| 1246 | bsp = ia64_rse_skip_regs(bsp, -(cfm & 0x7f)); |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1247 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1248 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
| 1249 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1250 | else |
| 1251 | tcp->u_nargs = MAX_ARGS; |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1252 | for (i = 0; i < tcp->u_nargs; ++i) { |
| 1253 | if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(bsp, i), sizeof(long), |
| 1254 | (char *) &tcp->u_arg[i]) |
| 1255 | < 0) |
| 1256 | return -1; |
| 1257 | } |
| 1258 | } |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 1259 | #elif defined (MIPS) |
| 1260 | { |
| 1261 | long sp; |
| 1262 | int i, nargs; |
| 1263 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1264 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
| 1265 | nargs = tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1266 | else |
| 1267 | nargs = tcp->u_nargs = MAX_ARGS; |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 1268 | if(nargs > 4) { |
| 1269 | if(upeek(pid, REG_SP, &sp) < 0) |
| 1270 | return -1; |
| 1271 | for(i = 0; i < 4; i++) { |
| 1272 | if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0) |
| 1273 | return -1; |
| 1274 | } |
| 1275 | umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]), |
| 1276 | (char *)(tcp->u_arg + 4)); |
| 1277 | } else { |
| 1278 | for(i = 0; i < nargs; i++) { |
| 1279 | if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0) |
| 1280 | return -1; |
| 1281 | } |
| 1282 | } |
| 1283 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1284 | #elif defined (POWERPC) |
| 1285 | { |
| 1286 | int i; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1287 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
| 1288 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1289 | else |
| 1290 | tcp->u_nargs = MAX_ARGS; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1291 | for (i = 0; i < tcp->u_nargs; i++) { |
| 1292 | if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0) |
| 1293 | return -1; |
| 1294 | } |
| 1295 | } |
| 1296 | #elif defined (SPARC) |
| 1297 | { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1298 | int i; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1299 | |
| 1300 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
| 1301 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1302 | else |
| 1303 | tcp->u_nargs = MAX_ARGS; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1304 | for (i = 0; i < tcp->u_nargs; i++) |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1305 | tcp->u_arg[i] = *((®s.r_o0) + i); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1306 | } |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1307 | #elif defined (HPPA) |
| 1308 | { |
| 1309 | int i; |
| 1310 | |
| 1311 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
| 1312 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1313 | else |
| 1314 | tcp->u_nargs = MAX_ARGS; |
| 1315 | for (i = 0; i < tcp->u_nargs; i++) { |
| 1316 | if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0) |
| 1317 | return -1; |
| 1318 | } |
| 1319 | } |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1320 | #else /* Other architecture (like i386) (32bits specific) */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1321 | { |
| 1322 | int i; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1323 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
| 1324 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1325 | else |
| 1326 | tcp->u_nargs = MAX_ARGS; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1327 | for (i = 0; i < tcp->u_nargs; i++) { |
| 1328 | if (upeek(pid, i*4, &tcp->u_arg[i]) < 0) |
| 1329 | return -1; |
| 1330 | } |
| 1331 | } |
| 1332 | #endif |
| 1333 | #endif /* LINUX */ |
| 1334 | #ifdef SUNOS4 |
| 1335 | { |
| 1336 | int i; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1337 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
| 1338 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1339 | else |
| 1340 | tcp->u_nargs = MAX_ARGS; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1341 | for (i = 0; i < tcp->u_nargs; i++) { |
| 1342 | struct user *u; |
| 1343 | |
| 1344 | if (upeek(pid, uoff(u_arg[0]) + |
| 1345 | (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0) |
| 1346 | return -1; |
| 1347 | } |
| 1348 | } |
| 1349 | #endif /* SUNOS4 */ |
| 1350 | #ifdef SVR4 |
| 1351 | #ifdef MIPS |
| 1352 | /* |
| 1353 | * SGI is broken: even though it has pr_sysarg, it doesn't |
| 1354 | * set them on system call entry. Get a clue. |
| 1355 | */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1356 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1357 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1358 | else |
| 1359 | tcp->u_nargs = tcp->status.pr_nsysarg; |
| 1360 | if (tcp->u_nargs > 4) { |
| 1361 | memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0], |
| 1362 | 4*sizeof(tcp->u_arg[0])); |
| 1363 | umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16, |
| 1364 | (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4)); |
| 1365 | } |
| 1366 | else { |
| 1367 | memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0], |
| 1368 | tcp->u_nargs*sizeof(tcp->u_arg[0])); |
| 1369 | } |
John Hughes | 2529971 | 2001-03-06 10:10:06 +0000 | [diff] [blame] | 1370 | #elif UNIXWARE >= 2 |
| 1371 | /* |
| 1372 | * Like SGI, UnixWare doesn't set pr_sysarg until system call exit |
| 1373 | */ |
| 1374 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
| 1375 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1376 | else |
| 1377 | tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg; |
| 1378 | umoven(tcp, tcp->status.PR_REG[UESP] + 4, |
| 1379 | tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg); |
| 1380 | #elif defined (HAVE_PR_SYSCALL) |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1381 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1382 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1383 | else |
| 1384 | tcp->u_nargs = tcp->status.pr_nsysarg; |
| 1385 | { |
| 1386 | int i; |
| 1387 | for (i = 0; i < tcp->u_nargs; i++) |
| 1388 | tcp->u_arg[i] = tcp->status.pr_sysarg[i]; |
| 1389 | } |
John Hughes | 2529971 | 2001-03-06 10:10:06 +0000 | [diff] [blame] | 1390 | #elif defined (I386) |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1391 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1392 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1393 | else |
| 1394 | tcp->u_nargs = 5; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1395 | umoven(tcp, tcp->status.PR_REG[UESP] + 4, |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1396 | tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg); |
John Hughes | 2529971 | 2001-03-06 10:10:06 +0000 | [diff] [blame] | 1397 | #else |
| 1398 | I DONT KNOW WHAT TO DO |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1399 | #endif /* !HAVE_PR_SYSCALL */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1400 | #endif /* SVR4 */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1401 | #ifdef FREEBSD |
| 1402 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && |
| 1403 | sysent[tcp->scno].nargs > tcp->status.val) |
| 1404 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1405 | else |
| 1406 | tcp->u_nargs = tcp->status.val; |
| 1407 | if (tcp->u_nargs < 0) |
| 1408 | tcp->u_nargs = 0; |
| 1409 | if (tcp->u_nargs > MAX_ARGS) |
| 1410 | tcp->u_nargs = MAX_ARGS; |
| 1411 | switch(regs.r_eax) { |
| 1412 | case SYS___syscall: |
| 1413 | pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long), |
| 1414 | regs.r_esp + sizeof(int) + sizeof(quad_t)); |
| 1415 | break; |
| 1416 | case SYS_syscall: |
| 1417 | pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long), |
| 1418 | regs.r_esp + 2 * sizeof(int)); |
| 1419 | break; |
| 1420 | default: |
| 1421 | pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long), |
| 1422 | regs.r_esp + sizeof(int)); |
| 1423 | break; |
| 1424 | } |
| 1425 | #endif /* FREEBSD */ |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1426 | return 1; |
| 1427 | } |
| 1428 | |
| 1429 | int |
| 1430 | trace_syscall(tcp) |
| 1431 | struct tcb *tcp; |
| 1432 | { |
| 1433 | int sys_res; |
| 1434 | struct timeval tv; |
| 1435 | int res; |
| 1436 | |
| 1437 | /* Measure the exit time as early as possible to avoid errors. */ |
| 1438 | if (dtime && (tcp->flags & TCB_INSYSCALL)) |
| 1439 | gettimeofday(&tv, NULL); |
| 1440 | |
| 1441 | res = get_scno(tcp); |
| 1442 | if (res != 1) |
| 1443 | return res; |
| 1444 | |
| 1445 | res = syscall_fixup(tcp); |
| 1446 | if (res != 1) |
| 1447 | return res; |
| 1448 | |
| 1449 | if (tcp->flags & TCB_INSYSCALL) { |
| 1450 | long u_error; |
| 1451 | res = get_error(tcp); |
| 1452 | if (res != 1) |
| 1453 | return res; |
| 1454 | u_error = tcp->u_error; |
| 1455 | |
| 1456 | |
| 1457 | internal_syscall(tcp); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1458 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && |
| 1459 | !(qual_flags[tcp->scno] & QUAL_TRACE)) { |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1460 | tcp->flags &= ~TCB_INSYSCALL; |
| 1461 | return 0; |
| 1462 | } |
| 1463 | |
| 1464 | if (tcp->flags & TCB_REPRINT) { |
| 1465 | printleader(tcp); |
| 1466 | tprintf("<... "); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1467 | if (tcp->scno >= nsyscalls || tcp->scno < 0) |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1468 | tprintf("syscall_%lu", tcp->scno); |
| 1469 | else |
| 1470 | tprintf("%s", sysent[tcp->scno].sys_name); |
| 1471 | tprintf(" resumed> "); |
| 1472 | } |
| 1473 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1474 | if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) { |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1475 | call_count[tcp->scno]++; |
| 1476 | if (tcp->u_error) |
| 1477 | error_count[tcp->scno]++; |
| 1478 | tv_sub(&tv, &tv, &tcp->etime); |
| 1479 | #ifdef LINUX |
| 1480 | if (tv_cmp(&tv, &tcp->dtime) > 0) { |
| 1481 | static struct timeval one_tick = |
| 1482 | { 0, 1000000 / HZ }; |
| 1483 | |
| 1484 | if (tv_nz(&tcp->dtime)) |
| 1485 | tv = tcp->dtime; |
| 1486 | else if (tv_cmp(&tv, &one_tick) > 0) { |
| 1487 | if (tv_cmp(&shortest, &one_tick) < 0) |
| 1488 | tv = shortest; |
| 1489 | else |
| 1490 | tv = one_tick; |
| 1491 | } |
| 1492 | } |
| 1493 | #endif /* LINUX */ |
| 1494 | if (tv_cmp(&tv, &shortest) < 0) |
| 1495 | shortest = tv; |
| 1496 | tv_add(&tv_count[tcp->scno], |
| 1497 | &tv_count[tcp->scno], &tv); |
| 1498 | tcp->flags &= ~TCB_INSYSCALL; |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1502 | if (tcp->scno >= nsyscalls || tcp->scno < 0 |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1503 | || (qual_flags[tcp->scno] & QUAL_RAW)) |
| 1504 | sys_res = printargs(tcp); |
| 1505 | else |
| 1506 | sys_res = (*sysent[tcp->scno].sys_func)(tcp); |
| 1507 | u_error = tcp->u_error; |
| 1508 | tprintf(") "); |
| 1509 | tabto(acolumn); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1510 | if (tcp->scno >= nsyscalls || tcp->scno < 0 || |
| 1511 | qual_flags[tcp->scno] & QUAL_RAW) { |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1512 | if (u_error) |
| 1513 | tprintf("= -1 (errno %ld)", u_error); |
| 1514 | else |
| 1515 | tprintf("= %#lx", tcp->u_rval); |
| 1516 | } |
| 1517 | else if (!(sys_res & RVAL_NONE) && u_error) { |
| 1518 | switch (u_error) { |
| 1519 | #ifdef LINUX |
| 1520 | case ERESTARTSYS: |
| 1521 | tprintf("= ? ERESTARTSYS (To be restarted)"); |
| 1522 | break; |
| 1523 | case ERESTARTNOINTR: |
| 1524 | tprintf("= ? ERESTARTNOINTR (To be restarted)"); |
| 1525 | break; |
| 1526 | case ERESTARTNOHAND: |
| 1527 | tprintf("= ? ERESTARTNOHAND (To be restarted)"); |
| 1528 | break; |
| 1529 | #endif /* LINUX */ |
| 1530 | default: |
| 1531 | tprintf("= -1 "); |
| 1532 | if (u_error < nerrnos && u_error < sys_nerr) |
| 1533 | tprintf("%s (%s)", errnoent[u_error], |
| 1534 | sys_errlist[u_error]); |
| 1535 | else if (u_error < nerrnos) |
| 1536 | tprintf("%s (errno %ld)", |
| 1537 | errnoent[u_error], u_error); |
| 1538 | else if (u_error < sys_nerr) |
| 1539 | tprintf("ERRNO_%ld (%s)", u_error, |
| 1540 | sys_errlist[u_error]); |
| 1541 | else |
| 1542 | tprintf("E??? (errno %ld)", u_error); |
| 1543 | break; |
| 1544 | } |
| 1545 | } |
| 1546 | else { |
| 1547 | if (sys_res & RVAL_NONE) |
| 1548 | tprintf("= ?"); |
| 1549 | else { |
| 1550 | switch (sys_res & RVAL_MASK) { |
| 1551 | case RVAL_HEX: |
| 1552 | tprintf("= %#lx", tcp->u_rval); |
| 1553 | break; |
| 1554 | case RVAL_OCTAL: |
| 1555 | tprintf("= %#lo", tcp->u_rval); |
| 1556 | break; |
| 1557 | case RVAL_UDECIMAL: |
| 1558 | tprintf("= %lu", tcp->u_rval); |
| 1559 | break; |
| 1560 | case RVAL_DECIMAL: |
| 1561 | tprintf("= %ld", tcp->u_rval); |
| 1562 | break; |
Wichert Akkerman | 16a03d2 | 2000-08-10 02:14:04 +0000 | [diff] [blame] | 1563 | #ifdef HAVE_LONG_LONG |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1564 | case RVAL_LHEX: |
| 1565 | tprintf("= %#llx", tcp->u_lrval); |
Wichert Akkerman | 16a03d2 | 2000-08-10 02:14:04 +0000 | [diff] [blame] | 1566 | break; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1567 | case RVAL_LOCTAL: |
| 1568 | tprintf("= %#llo", tcp->u_lrval); |
| 1569 | break; |
| 1570 | case RVAL_LUDECIMAL: |
| 1571 | tprintf("= %llu", tcp->u_lrval); |
| 1572 | break; |
| 1573 | case RVAL_LDECIMAL: |
| 1574 | tprintf("= %lld", tcp->u_lrval); |
| 1575 | break; |
Wichert Akkerman | 16a03d2 | 2000-08-10 02:14:04 +0000 | [diff] [blame] | 1576 | #endif |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1577 | default: |
| 1578 | fprintf(stderr, |
| 1579 | "invalid rval format\n"); |
| 1580 | break; |
| 1581 | } |
| 1582 | } |
| 1583 | if ((sys_res & RVAL_STR) && tcp->auxstr) |
| 1584 | tprintf(" (%s)", tcp->auxstr); |
| 1585 | } |
| 1586 | if (dtime) { |
| 1587 | tv_sub(&tv, &tv, &tcp->etime); |
| 1588 | tprintf(" <%ld.%06ld>", |
| 1589 | (long) tv.tv_sec, (long) tv.tv_usec); |
| 1590 | } |
| 1591 | printtrailer(tcp); |
| 1592 | |
| 1593 | dumpio(tcp); |
| 1594 | if (fflush(tcp->outf) == EOF) |
| 1595 | return -1; |
| 1596 | tcp->flags &= ~TCB_INSYSCALL; |
| 1597 | return 0; |
| 1598 | } |
| 1599 | |
| 1600 | /* Entering system call */ |
| 1601 | res = syscall_enter(tcp); |
| 1602 | if (res != 1) |
| 1603 | return res; |
| 1604 | |
Pavel Machek | d8ae7e3 | 2000-02-01 17:17:25 +0000 | [diff] [blame] | 1605 | switch (tcp->scno + NR_SYSCALL_BASE) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1606 | #ifdef LINUX |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1607 | #if !defined (ALPHA) && !defined(IA64) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1608 | case SYS_socketcall: |
| 1609 | decode_subcall(tcp, SYS_socket_subcall, |
| 1610 | SYS_socket_nsubcalls, deref_style); |
| 1611 | break; |
| 1612 | case SYS_ipc: |
| 1613 | decode_subcall(tcp, SYS_ipc_subcall, |
| 1614 | SYS_ipc_nsubcalls, shift_style); |
| 1615 | break; |
Wichert Akkerman | 12f75d1 | 2000-02-14 16:23:40 +0000 | [diff] [blame] | 1616 | #endif /* !ALPHA && !IA64 && !MIPS && !SPARC */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1617 | #ifdef SPARC |
| 1618 | case SYS_socketcall: |
| 1619 | sparc_socket_decode (tcp); |
| 1620 | break; |
| 1621 | #endif |
| 1622 | #endif /* LINUX */ |
| 1623 | #ifdef SVR4 |
| 1624 | #ifdef SYS_pgrpsys_subcall |
| 1625 | case SYS_pgrpsys: |
| 1626 | decode_subcall(tcp, SYS_pgrpsys_subcall, |
| 1627 | SYS_pgrpsys_nsubcalls, shift_style); |
| 1628 | break; |
| 1629 | #endif /* SYS_pgrpsys_subcall */ |
| 1630 | #ifdef SYS_sigcall_subcall |
| 1631 | case SYS_sigcall: |
| 1632 | decode_subcall(tcp, SYS_sigcall_subcall, |
| 1633 | SYS_sigcall_nsubcalls, mask_style); |
| 1634 | break; |
| 1635 | #endif /* SYS_sigcall_subcall */ |
| 1636 | case SYS_msgsys: |
| 1637 | decode_subcall(tcp, SYS_msgsys_subcall, |
| 1638 | SYS_msgsys_nsubcalls, shift_style); |
| 1639 | break; |
| 1640 | case SYS_shmsys: |
| 1641 | decode_subcall(tcp, SYS_shmsys_subcall, |
| 1642 | SYS_shmsys_nsubcalls, shift_style); |
| 1643 | break; |
| 1644 | case SYS_semsys: |
| 1645 | decode_subcall(tcp, SYS_semsys_subcall, |
| 1646 | SYS_semsys_nsubcalls, shift_style); |
| 1647 | break; |
| 1648 | #if 0 /* broken */ |
| 1649 | case SYS_utssys: |
| 1650 | decode_subcall(tcp, SYS_utssys_subcall, |
| 1651 | SYS_utssys_nsubcalls, shift_style); |
| 1652 | break; |
| 1653 | #endif |
| 1654 | case SYS_sysfs: |
| 1655 | decode_subcall(tcp, SYS_sysfs_subcall, |
| 1656 | SYS_sysfs_nsubcalls, shift_style); |
| 1657 | break; |
| 1658 | case SYS_spcall: |
| 1659 | decode_subcall(tcp, SYS_spcall_subcall, |
| 1660 | SYS_spcall_nsubcalls, shift_style); |
| 1661 | break; |
| 1662 | #ifdef SYS_context_subcall |
| 1663 | case SYS_context: |
| 1664 | decode_subcall(tcp, SYS_context_subcall, |
| 1665 | SYS_context_nsubcalls, shift_style); |
| 1666 | break; |
| 1667 | #endif /* SYS_context_subcall */ |
Wichert Akkerman | 8829a55 | 1999-06-11 13:18:40 +0000 | [diff] [blame] | 1668 | #ifdef SYS_door_subcall |
| 1669 | case SYS_door: |
| 1670 | decode_subcall(tcp, SYS_door_subcall, |
| 1671 | SYS_door_nsubcalls, door_style); |
| 1672 | break; |
| 1673 | #endif /* SYS_door_subcall */ |
John Hughes | bdf48f5 | 2001-03-06 15:08:09 +0000 | [diff] [blame] | 1674 | #ifdef SYS_kaio_subcall |
| 1675 | case SYS_kaio: |
| 1676 | decode_subcall(tcp, SYS_kaio_subcall, |
| 1677 | SYS_kaio_nsubcalls, shift_style); |
| 1678 | break; |
| 1679 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1680 | #endif /* SVR4 */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1681 | #ifdef FREEBSD |
| 1682 | case SYS_msgsys: |
| 1683 | case SYS_shmsys: |
| 1684 | case SYS_semsys: |
| 1685 | decode_subcall(tcp, 0, 0, table_style); |
| 1686 | break; |
| 1687 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1688 | #ifdef SUNOS4 |
| 1689 | case SYS_semsys: |
| 1690 | decode_subcall(tcp, SYS_semsys_subcall, |
| 1691 | SYS_semsys_nsubcalls, shift_style); |
| 1692 | break; |
| 1693 | case SYS_msgsys: |
| 1694 | decode_subcall(tcp, SYS_msgsys_subcall, |
| 1695 | SYS_msgsys_nsubcalls, shift_style); |
| 1696 | break; |
| 1697 | case SYS_shmsys: |
| 1698 | decode_subcall(tcp, SYS_shmsys_subcall, |
| 1699 | SYS_shmsys_nsubcalls, shift_style); |
| 1700 | break; |
| 1701 | #endif |
| 1702 | } |
| 1703 | |
| 1704 | internal_syscall(tcp); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1705 | if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1706 | tcp->flags |= TCB_INSYSCALL; |
| 1707 | return 0; |
| 1708 | } |
| 1709 | |
| 1710 | if (cflag) { |
| 1711 | gettimeofday(&tcp->etime, NULL); |
| 1712 | tcp->flags |= TCB_INSYSCALL; |
| 1713 | return 0; |
| 1714 | } |
| 1715 | |
| 1716 | printleader(tcp); |
| 1717 | tcp->flags &= ~TCB_REPRINT; |
| 1718 | tcp_last = tcp; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1719 | if (tcp->scno >= nsyscalls || tcp->scno < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1720 | tprintf("syscall_%lu(", tcp->scno); |
| 1721 | else |
| 1722 | tprintf("%s(", sysent[tcp->scno].sys_name); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1723 | if (tcp->scno >= nsyscalls || tcp->scno < 0 || |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1724 | ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit)) |
| 1725 | sys_res = printargs(tcp); |
| 1726 | else |
| 1727 | sys_res = (*sysent[tcp->scno].sys_func)(tcp); |
| 1728 | if (fflush(tcp->outf) == EOF) |
| 1729 | return -1; |
| 1730 | tcp->flags |= TCB_INSYSCALL; |
| 1731 | /* Measure the entrance time as late as possible to avoid errors. */ |
| 1732 | if (dtime) |
| 1733 | gettimeofday(&tcp->etime, NULL); |
| 1734 | return sys_res; |
| 1735 | } |
| 1736 | |
| 1737 | int |
| 1738 | printargs(tcp) |
| 1739 | struct tcb *tcp; |
| 1740 | { |
| 1741 | if (entering(tcp)) { |
| 1742 | int i; |
| 1743 | |
| 1744 | for (i = 0; i < tcp->u_nargs; i++) |
| 1745 | tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]); |
| 1746 | } |
| 1747 | return 0; |
| 1748 | } |
| 1749 | |
| 1750 | long |
| 1751 | getrval2(tcp) |
| 1752 | struct tcb *tcp; |
| 1753 | { |
| 1754 | long val = -1; |
| 1755 | |
| 1756 | #ifdef LINUX |
| 1757 | #ifdef SPARC |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1758 | struct regs regs; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1759 | if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0) |
| 1760 | return -1; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1761 | val = regs.r_o1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1762 | #endif /* SPARC */ |
| 1763 | #endif /* LINUX */ |
| 1764 | |
| 1765 | #ifdef SUNOS4 |
| 1766 | if (upeek(tcp->pid, uoff(u_rval2), &val) < 0) |
| 1767 | return -1; |
| 1768 | #endif /* SUNOS4 */ |
| 1769 | |
| 1770 | #ifdef SVR4 |
| 1771 | #ifdef SPARC |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1772 | val = tcp->status.PR_REG[R_O1]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1773 | #endif /* SPARC */ |
| 1774 | #ifdef I386 |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1775 | val = tcp->status.PR_REG[EDX]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1776 | #endif /* I386 */ |
| 1777 | #ifdef MIPS |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1778 | val = tcp->status.PR_REG[CTX_V1]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1779 | #endif /* MIPS */ |
| 1780 | #endif /* SVR4 */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1781 | #ifdef FREEBSD |
| 1782 | struct reg regs; |
| 1783 | pread(tcp->pfd_reg, ®s, sizeof(regs), 0); |
| 1784 | val = regs.r_edx; |
| 1785 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1786 | return val; |
| 1787 | } |
| 1788 | |
| 1789 | /* |
| 1790 | * Apparently, indirect system calls have already be converted by ptrace(2), |
| 1791 | * so if you see "indir" this program has gone astray. |
| 1792 | */ |
| 1793 | int |
| 1794 | sys_indir(tcp) |
| 1795 | struct tcb *tcp; |
| 1796 | { |
| 1797 | int i, scno, nargs; |
| 1798 | |
| 1799 | if (entering(tcp)) { |
| 1800 | if ((scno = tcp->u_arg[0]) > nsyscalls) { |
| 1801 | fprintf(stderr, "Bogus syscall: %u\n", scno); |
| 1802 | return 0; |
| 1803 | } |
| 1804 | nargs = sysent[scno].nargs; |
| 1805 | tprintf("%s", sysent[scno].sys_name); |
| 1806 | for (i = 0; i < nargs; i++) |
| 1807 | tprintf(", %#lx", tcp->u_arg[i+1]); |
| 1808 | } |
| 1809 | return 0; |
| 1810 | } |
| 1811 | |
| 1812 | static int |
| 1813 | time_cmp(a, b) |
| 1814 | void *a; |
| 1815 | void *b; |
| 1816 | { |
| 1817 | return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]); |
| 1818 | } |
| 1819 | |
| 1820 | static int |
| 1821 | syscall_cmp(a, b) |
| 1822 | void *a; |
| 1823 | void *b; |
| 1824 | { |
| 1825 | return strcmp(sysent[*((int *) a)].sys_name, |
| 1826 | sysent[*((int *) b)].sys_name); |
| 1827 | } |
| 1828 | |
| 1829 | static int |
| 1830 | count_cmp(a, b) |
| 1831 | void *a; |
| 1832 | void *b; |
| 1833 | { |
| 1834 | int m = call_count[*((int *) a)], n = call_count[*((int *) b)]; |
| 1835 | |
| 1836 | return (m < n) ? 1 : (m > n) ? -1 : 0; |
| 1837 | } |
| 1838 | |
| 1839 | static int (*sortfun)(); |
| 1840 | static struct timeval overhead = { -1, -1 }; |
| 1841 | |
| 1842 | void |
| 1843 | set_sortby(sortby) |
| 1844 | char *sortby; |
| 1845 | { |
| 1846 | if (strcmp(sortby, "time") == 0) |
| 1847 | sortfun = time_cmp; |
| 1848 | else if (strcmp(sortby, "calls") == 0) |
| 1849 | sortfun = count_cmp; |
| 1850 | else if (strcmp(sortby, "name") == 0) |
| 1851 | sortfun = syscall_cmp; |
| 1852 | else if (strcmp(sortby, "nothing") == 0) |
| 1853 | sortfun = NULL; |
| 1854 | else { |
| 1855 | fprintf(stderr, "invalid sortby: `%s'\n", sortby); |
| 1856 | exit(1); |
| 1857 | } |
| 1858 | } |
| 1859 | |
| 1860 | void set_overhead(n) |
| 1861 | int n; |
| 1862 | { |
| 1863 | overhead.tv_sec = n / 1000000; |
| 1864 | overhead.tv_usec = n % 1000000; |
| 1865 | } |
| 1866 | |
| 1867 | void |
| 1868 | call_summary(outf) |
| 1869 | FILE *outf; |
| 1870 | { |
| 1871 | int i, j; |
| 1872 | int call_cum, error_cum; |
| 1873 | struct timeval tv_cum, dtv; |
| 1874 | double percent; |
| 1875 | char *dashes = "-------------------------"; |
| 1876 | char error_str[16]; |
| 1877 | |
| 1878 | call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0; |
| 1879 | if (overhead.tv_sec == -1) { |
| 1880 | tv_mul(&overhead, &shortest, 8); |
| 1881 | tv_div(&overhead, &overhead, 10); |
| 1882 | } |
| 1883 | for (i = 0; i < nsyscalls; i++) { |
| 1884 | sorted_count[i] = i; |
| 1885 | if (call_count[i] == 0) |
| 1886 | continue; |
| 1887 | tv_mul(&dtv, &overhead, call_count[i]); |
| 1888 | tv_sub(&tv_count[i], &tv_count[i], &dtv); |
| 1889 | call_cum += call_count[i]; |
| 1890 | error_cum += error_count[i]; |
| 1891 | tv_add(&tv_cum, &tv_cum, &tv_count[i]); |
| 1892 | } |
| 1893 | if (sortfun) |
| 1894 | qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun); |
| 1895 | fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n", |
| 1896 | "% time", "seconds", "usecs/call", |
| 1897 | "calls", "errors", "syscall"); |
| 1898 | fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n", |
| 1899 | dashes, dashes, dashes, dashes, dashes, dashes); |
| 1900 | for (i = 0; i < nsyscalls; i++) { |
| 1901 | j = sorted_count[i]; |
| 1902 | if (call_count[j] == 0) |
| 1903 | continue; |
| 1904 | tv_div(&dtv, &tv_count[j], call_count[j]); |
| 1905 | if (error_count[j]) |
| 1906 | sprintf(error_str, "%d", error_count[j]); |
| 1907 | else |
| 1908 | error_str[0] = '\0'; |
| 1909 | percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum); |
| 1910 | fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n", |
| 1911 | percent, (long) tv_count[j].tv_sec, |
| 1912 | (long) tv_count[j].tv_usec, |
| 1913 | (long) 1000000 * dtv.tv_sec + dtv.tv_usec, |
| 1914 | call_count[j], error_str, sysent[j].sys_name); |
| 1915 | } |
| 1916 | fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n", |
| 1917 | dashes, dashes, dashes, dashes, dashes, dashes); |
| 1918 | if (error_cum) |
| 1919 | sprintf(error_str, "%d", error_cum); |
| 1920 | else |
| 1921 | error_str[0] = '\0'; |
| 1922 | fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n", |
| 1923 | "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "", |
| 1924 | call_cum, error_str, "total"); |
| 1925 | } |