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 |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 55 | # undef fpu |
Wichert Akkerman | 00a82ee | 2001-03-28 20:29:17 +0000 | [diff] [blame] | 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 | |
Pavel Machek | d8ae7e3 | 2000-02-01 17:17:25 +0000 | [diff] [blame] | 74 | #define NR_SYSCALL_BASE 0 |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 75 | #ifdef LINUX |
| 76 | #ifndef ERESTARTSYS |
| 77 | #define ERESTARTSYS 512 |
| 78 | #endif |
| 79 | #ifndef ERESTARTNOINTR |
| 80 | #define ERESTARTNOINTR 513 |
| 81 | #endif |
| 82 | #ifndef ERESTARTNOHAND |
| 83 | #define ERESTARTNOHAND 514 /* restart if no handler.. */ |
| 84 | #endif |
| 85 | #ifndef ENOIOCTLCMD |
| 86 | #define ENOIOCTLCMD 515 /* No ioctl command */ |
| 87 | #endif |
| 88 | #ifndef NSIG |
| 89 | #define NSIG 32 |
| 90 | #endif |
| 91 | #ifdef ARM |
| 92 | #undef NSIG |
| 93 | #define NSIG 32 |
Pavel Machek | d8ae7e3 | 2000-02-01 17:17:25 +0000 | [diff] [blame] | 94 | #undef NR_SYSCALL_BASE |
| 95 | #define NR_SYSCALL_BASE __NR_SYSCALL_BASE |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 96 | #endif |
| 97 | #endif /* LINUX */ |
| 98 | |
| 99 | #include "syscall.h" |
| 100 | |
| 101 | /* Define these shorthand notations to simplify the syscallent files. */ |
| 102 | #define TF TRACE_FILE |
| 103 | #define TI TRACE_IPC |
| 104 | #define TN TRACE_NETWORK |
| 105 | #define TP TRACE_PROCESS |
| 106 | #define TS TRACE_SIGNAL |
| 107 | |
| 108 | struct sysent sysent0[] = { |
| 109 | #include "syscallent.h" |
| 110 | }; |
| 111 | int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0]; |
| 112 | |
| 113 | #if SUPPORTED_PERSONALITIES >= 2 |
| 114 | struct sysent sysent1[] = { |
| 115 | #include "syscallent1.h" |
| 116 | }; |
| 117 | int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0]; |
| 118 | #endif /* SUPPORTED_PERSONALITIES >= 2 */ |
| 119 | |
| 120 | #if SUPPORTED_PERSONALITIES >= 3 |
| 121 | struct sysent sysent2[] = { |
| 122 | #include "syscallent2.h" |
| 123 | }; |
| 124 | int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0]; |
| 125 | #endif /* SUPPORTED_PERSONALITIES >= 3 */ |
| 126 | |
| 127 | struct sysent *sysent; |
| 128 | int nsyscalls; |
| 129 | |
| 130 | /* Now undef them since short defines cause wicked namespace pollution. */ |
| 131 | #undef TF |
| 132 | #undef TI |
| 133 | #undef TN |
| 134 | #undef TP |
| 135 | #undef TS |
| 136 | |
| 137 | char *errnoent0[] = { |
| 138 | #include "errnoent.h" |
| 139 | }; |
| 140 | int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0]; |
| 141 | |
| 142 | #if SUPPORTED_PERSONALITIES >= 2 |
| 143 | char *errnoent1[] = { |
| 144 | #include "errnoent1.h" |
| 145 | }; |
| 146 | int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0]; |
| 147 | #endif /* SUPPORTED_PERSONALITIES >= 2 */ |
| 148 | |
| 149 | #if SUPPORTED_PERSONALITIES >= 3 |
| 150 | char *errnoent2[] = { |
| 151 | #include "errnoent2.h" |
| 152 | }; |
| 153 | int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0]; |
| 154 | #endif /* SUPPORTED_PERSONALITIES >= 3 */ |
| 155 | |
| 156 | char **errnoent; |
| 157 | int nerrnos; |
| 158 | |
| 159 | int current_personality; |
| 160 | |
| 161 | int |
Wichert Akkerman | e6f876c | 1999-06-22 15:28:30 +0000 | [diff] [blame] | 162 | set_personality(personality) |
| 163 | int personality; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 164 | { |
| 165 | switch (personality) { |
| 166 | case 0: |
| 167 | errnoent = errnoent0; |
| 168 | nerrnos = nerrnos0; |
| 169 | sysent = sysent0; |
| 170 | nsyscalls = nsyscalls0; |
| 171 | ioctlent = ioctlent0; |
| 172 | nioctlents = nioctlents0; |
| 173 | signalent = signalent0; |
| 174 | nsignals = nsignals0; |
| 175 | break; |
| 176 | |
| 177 | #if SUPPORTED_PERSONALITIES >= 2 |
| 178 | case 1: |
| 179 | errnoent = errnoent1; |
| 180 | nerrnos = nerrnos1; |
| 181 | sysent = sysent1; |
| 182 | nsyscalls = nsyscalls1; |
| 183 | ioctlent = ioctlent1; |
| 184 | nioctlents = nioctlents1; |
| 185 | signalent = signalent1; |
| 186 | nsignals = nsignals1; |
| 187 | break; |
| 188 | #endif /* SUPPORTED_PERSONALITIES >= 2 */ |
| 189 | |
| 190 | #if SUPPORTED_PERSONALITIES >= 3 |
| 191 | case 2: |
| 192 | errnoent = errnoent2; |
| 193 | nerrnos = nerrnos2; |
| 194 | sysent = sysent2; |
| 195 | nsyscalls = nsyscalls2; |
| 196 | ioctlent = ioctlent2; |
| 197 | nioctlents = nioctlents2; |
| 198 | signalent = signalent2; |
| 199 | nsignals = nsignals2; |
| 200 | break; |
| 201 | #endif /* SUPPORTED_PERSONALITIES >= 3 */ |
| 202 | |
| 203 | default: |
| 204 | return -1; |
| 205 | } |
| 206 | |
| 207 | current_personality = personality; |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | int qual_flags[MAX_QUALS]; |
| 212 | |
| 213 | static int call_count[MAX_QUALS]; |
| 214 | static int error_count[MAX_QUALS]; |
| 215 | static struct timeval tv_count[MAX_QUALS]; |
| 216 | static int sorted_count[MAX_QUALS]; |
| 217 | |
| 218 | static struct timeval shortest = { 1000000, 0 }; |
| 219 | |
| 220 | static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc(); |
| 221 | |
| 222 | static struct qual_options { |
| 223 | int bitflag; |
| 224 | char *option_name; |
| 225 | int (*lookup)(); |
| 226 | char *argument_name; |
| 227 | } qual_options[] = { |
| 228 | { QUAL_TRACE, "trace", lookup_syscall, "system call" }, |
| 229 | { QUAL_TRACE, "t", lookup_syscall, "system call" }, |
| 230 | { QUAL_ABBREV, "abbrev", lookup_syscall, "system call" }, |
| 231 | { QUAL_ABBREV, "a", lookup_syscall, "system call" }, |
| 232 | { QUAL_VERBOSE, "verbose", lookup_syscall, "system call" }, |
| 233 | { QUAL_VERBOSE, "v", lookup_syscall, "system call" }, |
| 234 | { QUAL_RAW, "raw", lookup_syscall, "system call" }, |
| 235 | { QUAL_RAW, "x", lookup_syscall, "system call" }, |
| 236 | { QUAL_SIGNAL, "signal", lookup_signal, "signal" }, |
| 237 | { QUAL_SIGNAL, "signals", lookup_signal, "signal" }, |
| 238 | { QUAL_SIGNAL, "s", lookup_signal, "signal" }, |
| 239 | { QUAL_FAULT, "fault", lookup_fault, "fault" }, |
| 240 | { QUAL_FAULT, "faults", lookup_fault, "fault" }, |
| 241 | { QUAL_FAULT, "m", lookup_fault, "fault" }, |
| 242 | { QUAL_READ, "read", lookup_desc, "descriptor" }, |
| 243 | { QUAL_READ, "reads", lookup_desc, "descriptor" }, |
| 244 | { QUAL_READ, "r", lookup_desc, "descriptor" }, |
| 245 | { QUAL_WRITE, "write", lookup_desc, "descriptor" }, |
| 246 | { QUAL_WRITE, "writes", lookup_desc, "descriptor" }, |
| 247 | { QUAL_WRITE, "w", lookup_desc, "descriptor" }, |
| 248 | { 0, NULL, NULL, NULL }, |
| 249 | }; |
| 250 | |
| 251 | static int |
| 252 | lookup_syscall(s) |
| 253 | char *s; |
| 254 | { |
| 255 | int i; |
| 256 | |
| 257 | for (i = 0; i < nsyscalls; i++) { |
| 258 | if (strcmp(s, sysent[i].sys_name) == 0) |
| 259 | return i; |
| 260 | } |
| 261 | return -1; |
| 262 | } |
| 263 | |
| 264 | static int |
| 265 | lookup_signal(s) |
| 266 | char *s; |
| 267 | { |
| 268 | int i; |
| 269 | char buf[32]; |
| 270 | |
Wichert Akkerman | 2ee6e45 | 2000-02-18 15:36:12 +0000 | [diff] [blame] | 271 | if (s && *s && isdigit((unsigned char)*s)) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 272 | return atoi(s); |
| 273 | strcpy(buf, s); |
| 274 | s = buf; |
| 275 | for (i = 0; s[i]; i++) |
Wichert Akkerman | 2ee6e45 | 2000-02-18 15:36:12 +0000 | [diff] [blame] | 276 | s[i] = toupper((unsigned char)(s[i])); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 277 | if (strncmp(s, "SIG", 3) == 0) |
| 278 | s += 3; |
| 279 | for (i = 0; i <= NSIG; i++) { |
Nate Sammons | ce780fc | 1999-03-29 23:23:13 +0000 | [diff] [blame] | 280 | if (strcmp(s, signame(i) + 3) == 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 281 | return i; |
| 282 | } |
| 283 | return -1; |
| 284 | } |
| 285 | |
| 286 | static int |
| 287 | lookup_fault(s) |
| 288 | char *s; |
| 289 | { |
| 290 | return -1; |
| 291 | } |
| 292 | |
| 293 | static int |
| 294 | lookup_desc(s) |
| 295 | char *s; |
| 296 | { |
Wichert Akkerman | 2ee6e45 | 2000-02-18 15:36:12 +0000 | [diff] [blame] | 297 | if (s && *s && isdigit((unsigned char)*s)) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 298 | return atoi(s); |
| 299 | return -1; |
| 300 | } |
| 301 | |
| 302 | static int |
| 303 | lookup_class(s) |
| 304 | char *s; |
| 305 | { |
| 306 | if (strcmp(s, "file") == 0) |
| 307 | return TRACE_FILE; |
| 308 | if (strcmp(s, "ipc") == 0) |
| 309 | return TRACE_IPC; |
| 310 | if (strcmp(s, "network") == 0) |
| 311 | return TRACE_NETWORK; |
| 312 | if (strcmp(s, "process") == 0) |
| 313 | return TRACE_PROCESS; |
| 314 | if (strcmp(s, "signal") == 0) |
| 315 | return TRACE_SIGNAL; |
| 316 | return -1; |
| 317 | } |
| 318 | |
| 319 | void |
| 320 | qualify(s) |
| 321 | char *s; |
| 322 | { |
| 323 | struct qual_options *opt; |
| 324 | int not; |
| 325 | char *p; |
| 326 | int i, n; |
| 327 | |
| 328 | opt = &qual_options[0]; |
| 329 | for (i = 0; (p = qual_options[i].option_name); i++) { |
| 330 | n = strlen(p); |
| 331 | if (strncmp(s, p, n) == 0 && s[n] == '=') { |
| 332 | opt = &qual_options[i]; |
| 333 | s += n + 1; |
| 334 | break; |
| 335 | } |
| 336 | } |
| 337 | not = 0; |
| 338 | if (*s == '!') { |
| 339 | not = 1; |
| 340 | s++; |
| 341 | } |
| 342 | if (strcmp(s, "none") == 0) { |
| 343 | not = 1 - not; |
| 344 | s = "all"; |
| 345 | } |
| 346 | if (strcmp(s, "all") == 0) { |
| 347 | for (i = 0; i < MAX_QUALS; i++) { |
| 348 | if (not) |
| 349 | qual_flags[i] &= ~opt->bitflag; |
| 350 | else |
| 351 | qual_flags[i] |= opt->bitflag; |
| 352 | } |
| 353 | return; |
| 354 | } |
| 355 | for (i = 0; i < MAX_QUALS; i++) { |
| 356 | if (not) |
| 357 | qual_flags[i] |= opt->bitflag; |
| 358 | else |
| 359 | qual_flags[i] &= ~opt->bitflag; |
| 360 | } |
| 361 | for (p = strtok(s, ","); p; p = strtok(NULL, ",")) { |
| 362 | if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) { |
| 363 | for (i = 0; i < MAX_QUALS; i++) { |
| 364 | if (sysent[i].sys_flags & n) { |
| 365 | if (not) |
| 366 | qual_flags[i] &= ~opt->bitflag; |
| 367 | else |
| 368 | qual_flags[i] |= opt->bitflag; |
| 369 | } |
| 370 | } |
| 371 | continue; |
| 372 | } |
| 373 | if ((n = (*opt->lookup)(p)) < 0) { |
| 374 | fprintf(stderr, "strace: invalid %s `%s'\n", |
| 375 | opt->argument_name, p); |
| 376 | exit(1); |
| 377 | } |
| 378 | if (not) |
| 379 | qual_flags[n] &= ~opt->bitflag; |
| 380 | else |
| 381 | qual_flags[n] |= opt->bitflag; |
| 382 | } |
| 383 | return; |
| 384 | } |
| 385 | |
| 386 | static void |
| 387 | dumpio(tcp) |
| 388 | struct tcb *tcp; |
| 389 | { |
| 390 | if (syserror(tcp)) |
| 391 | return; |
| 392 | if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS) |
| 393 | return; |
Pavel Machek | d8ae7e3 | 2000-02-01 17:17:25 +0000 | [diff] [blame] | 394 | switch (tcp->scno + NR_SYSCALL_BASE) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 395 | case SYS_read: |
| 396 | #ifdef SYS_recv |
| 397 | case SYS_recv: |
| 398 | #endif |
| 399 | #ifdef SYS_recvfrom |
| 400 | case SYS_recvfrom: |
| 401 | #endif |
| 402 | if (qual_flags[tcp->u_arg[0]] & QUAL_READ) |
| 403 | dumpstr(tcp, tcp->u_arg[1], tcp->u_rval); |
| 404 | break; |
| 405 | case SYS_write: |
| 406 | #ifdef SYS_send |
| 407 | case SYS_send: |
| 408 | #endif |
| 409 | #ifdef SYS_sendto |
| 410 | case SYS_sendto: |
| 411 | #endif |
| 412 | if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) |
| 413 | dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]); |
| 414 | break; |
John Hughes | 1d08dcf | 2001-07-10 13:48:44 +0000 | [diff] [blame] | 415 | #ifdef SYS_readv |
| 416 | case SYS_readv: |
| 417 | if (qual_flags[tcp->u_arg[0]] & QUAL_READ) |
| 418 | dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]); |
| 419 | break; |
| 420 | #endif |
| 421 | #ifdef SYS_writev |
| 422 | case SYS_writev: |
| 423 | |
| 424 | if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) |
| 425 | dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]); |
| 426 | break; |
| 427 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 431 | #ifndef FREEBSD |
Wichert Akkerman | 8829a55 | 1999-06-11 13:18:40 +0000 | [diff] [blame] | 432 | enum subcall_style { shift_style, deref_style, mask_style, door_style }; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 433 | #else /* FREEBSD */ |
| 434 | enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style }; |
| 435 | |
| 436 | struct subcall { |
| 437 | int call; |
| 438 | int nsubcalls; |
| 439 | int subcalls[5]; |
| 440 | }; |
| 441 | |
| 442 | const struct subcall subcalls_table[] = { |
| 443 | { 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] | 444 | #ifdef SYS_semconfig |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 445 | { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } }, |
John Hughes | 61eeb55 | 2001-03-06 15:51:53 +0000 | [diff] [blame] | 446 | #else |
| 447 | { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } }, |
| 448 | #endif |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 449 | { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } }, |
| 450 | }; |
| 451 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 452 | |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 453 | #if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) )) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 454 | |
| 455 | const int socket_map [] = { |
| 456 | /* SYS_SOCKET */ 97, |
| 457 | /* SYS_BIND */ 104, |
| 458 | /* SYS_CONNECT */ 98, |
| 459 | /* SYS_LISTEN */ 106, |
| 460 | /* SYS_ACCEPT */ 99, |
| 461 | /* SYS_GETSOCKNAME */ 150, |
| 462 | /* SYS_GETPEERNAME */ 141, |
| 463 | /* SYS_SOCKETPAIR */ 135, |
| 464 | /* SYS_SEND */ 101, |
| 465 | /* SYS_RECV */ 102, |
| 466 | /* SYS_SENDTO */ 133, |
| 467 | /* SYS_RECVFROM */ 125, |
| 468 | /* SYS_SHUTDOWN */ 134, |
| 469 | /* SYS_SETSOCKOPT */ 105, |
| 470 | /* SYS_GETSOCKOPT */ 118, |
| 471 | /* SYS_SENDMSG */ 114, |
| 472 | /* SYS_RECVMSG */ 113 |
| 473 | }; |
| 474 | |
| 475 | void |
Wichert Akkerman | e6f876c | 1999-06-22 15:28:30 +0000 | [diff] [blame] | 476 | sparc_socket_decode (tcp) |
| 477 | struct tcb *tcp; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 478 | { |
| 479 | volatile long addr; |
| 480 | volatile int i, n; |
| 481 | |
| 482 | if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){ |
| 483 | return; |
| 484 | } |
| 485 | tcp->scno = socket_map [tcp->u_arg [0]-1]; |
| 486 | n = tcp->u_nargs = sysent [tcp->scno].nargs; |
| 487 | addr = tcp->u_arg [1]; |
| 488 | for (i = 0; i < n; i++){ |
| 489 | int arg; |
| 490 | if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0) |
| 491 | arg = 0; |
| 492 | tcp->u_arg [i] = arg; |
| 493 | addr += sizeof (arg); |
| 494 | } |
| 495 | } |
| 496 | |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 497 | void |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 498 | decode_subcall(tcp, subcall, nsubcalls, style) |
| 499 | struct tcb *tcp; |
| 500 | int subcall; |
| 501 | int nsubcalls; |
| 502 | enum subcall_style style; |
| 503 | { |
Michal Ludvig | 10a88d0 | 2002-10-07 14:31:00 +0000 | [diff] [blame] | 504 | long addr, mask, arg; |
| 505 | int i; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 506 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 507 | switch (style) { |
| 508 | case shift_style: |
Wichert Akkerman | 16a03d2 | 2000-08-10 02:14:04 +0000 | [diff] [blame] | 509 | if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls) |
| 510 | return; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 511 | tcp->scno = subcall + tcp->u_arg[0]; |
| 512 | if (sysent[tcp->scno].nargs != -1) |
| 513 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 514 | else |
| 515 | tcp->u_nargs--; |
| 516 | for (i = 0; i < tcp->u_nargs; i++) |
| 517 | tcp->u_arg[i] = tcp->u_arg[i + 1]; |
| 518 | break; |
| 519 | case deref_style: |
Wichert Akkerman | 16a03d2 | 2000-08-10 02:14:04 +0000 | [diff] [blame] | 520 | if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls) |
| 521 | return; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 522 | tcp->scno = subcall + tcp->u_arg[0]; |
| 523 | addr = tcp->u_arg[1]; |
| 524 | for (i = 0; i < sysent[tcp->scno].nargs; i++) { |
| 525 | if (umove(tcp, addr, &arg) < 0) |
| 526 | arg = 0; |
| 527 | tcp->u_arg[i] = arg; |
| 528 | addr += sizeof(arg); |
| 529 | } |
| 530 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 531 | break; |
| 532 | case mask_style: |
| 533 | mask = (tcp->u_arg[0] >> 8) & 0xff; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 534 | for (i = 0; mask; i++) |
| 535 | mask >>= 1; |
Wichert Akkerman | 16a03d2 | 2000-08-10 02:14:04 +0000 | [diff] [blame] | 536 | if (i >= nsubcalls) |
| 537 | return; |
| 538 | tcp->u_arg[0] &= 0xff; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 539 | tcp->scno = subcall + i; |
| 540 | if (sysent[tcp->scno].nargs != -1) |
| 541 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 542 | break; |
Wichert Akkerman | 8829a55 | 1999-06-11 13:18:40 +0000 | [diff] [blame] | 543 | case door_style: |
| 544 | /* |
| 545 | * Oh, yuck. The call code is the *sixth* argument. |
Wichert Akkerman | 16a03d2 | 2000-08-10 02:14:04 +0000 | [diff] [blame] | 546 | * (don't you mean the *last* argument? - JH) |
Wichert Akkerman | 8829a55 | 1999-06-11 13:18:40 +0000 | [diff] [blame] | 547 | */ |
Wichert Akkerman | 16a03d2 | 2000-08-10 02:14:04 +0000 | [diff] [blame] | 548 | if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls) |
| 549 | return; |
Wichert Akkerman | 8829a55 | 1999-06-11 13:18:40 +0000 | [diff] [blame] | 550 | tcp->scno = subcall + tcp->u_arg[5]; |
| 551 | if (sysent[tcp->scno].nargs != -1) |
| 552 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 553 | else |
| 554 | tcp->u_nargs--; |
| 555 | break; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 556 | #ifdef FREEBSD |
| 557 | case table_style: |
| 558 | for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++) |
| 559 | if (subcalls_table[i].call == tcp->scno) break; |
| 560 | if (i < sizeof(subcalls_table) / sizeof(struct subcall) && |
| 561 | tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) { |
| 562 | tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]]; |
| 563 | for (i = 0; i < tcp->u_nargs; i++) |
| 564 | tcp->u_arg[i] = tcp->u_arg[i + 1]; |
| 565 | } |
| 566 | break; |
| 567 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | #endif |
| 571 | |
| 572 | struct tcb *tcp_last = NULL; |
| 573 | |
| 574 | static int |
| 575 | internal_syscall(tcp) |
| 576 | struct tcb *tcp; |
| 577 | { |
| 578 | /* |
| 579 | * We must always trace a few critical system calls in order to |
| 580 | * correctly support following forks in the presence of tracing |
| 581 | * qualifiers. |
| 582 | */ |
Pavel Machek | d8ae7e3 | 2000-02-01 17:17:25 +0000 | [diff] [blame] | 583 | switch (tcp->scno + NR_SYSCALL_BASE) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 584 | #ifdef SYS_fork |
| 585 | case SYS_fork: |
| 586 | #endif |
| 587 | #ifdef SYS_vfork |
| 588 | case SYS_vfork: |
| 589 | #endif |
John Hughes | 4e36a81 | 2001-04-18 15:11:51 +0000 | [diff] [blame] | 590 | #ifdef SYS_fork1 |
| 591 | case SYS_fork1: |
| 592 | #endif |
| 593 | #ifdef SYS_forkall |
| 594 | case SYS_forkall: |
| 595 | #endif |
| 596 | #ifdef SYS_rfork1 |
| 597 | case SYS_rfork1: |
| 598 | #endif |
| 599 | #ifdef SYS_rforkall |
| 600 | case SYS_rforkall: |
| 601 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 602 | internal_fork(tcp); |
| 603 | break; |
Wichert Akkerman | 7a0b649 | 1999-12-23 15:08:17 +0000 | [diff] [blame] | 604 | #ifdef SYS_clone |
| 605 | case SYS_clone: |
| 606 | internal_clone(tcp); |
| 607 | break; |
| 608 | #endif |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 609 | #ifdef SYS_clone2 |
| 610 | case SYS_clone2: |
| 611 | internal_clone(tcp); |
| 612 | break; |
| 613 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 614 | #ifdef SYS_execv |
| 615 | case SYS_execv: |
| 616 | #endif |
| 617 | #ifdef SYS_execve |
| 618 | case SYS_execve: |
| 619 | #endif |
John Hughes | 4e36a81 | 2001-04-18 15:11:51 +0000 | [diff] [blame] | 620 | #ifdef SYS_rexecve |
| 621 | case SYS_rexecve: |
| 622 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 623 | internal_exec(tcp); |
| 624 | break; |
| 625 | |
| 626 | #ifdef SYS_wait |
| 627 | case SYS_wait: |
| 628 | #endif |
| 629 | #ifdef SYS_wait4 |
| 630 | case SYS_wait4: |
| 631 | #endif |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 632 | #ifdef SYS32_wait4 |
| 633 | case SYS32_wait4: |
| 634 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 635 | #ifdef SYS_waitpid |
| 636 | case SYS_waitpid: |
| 637 | #endif |
| 638 | #ifdef SYS_waitsys |
| 639 | case SYS_waitsys: |
| 640 | #endif |
| 641 | internal_wait(tcp); |
| 642 | break; |
| 643 | |
| 644 | #ifdef SYS_exit |
| 645 | case SYS_exit: |
| 646 | #endif |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 647 | #ifdef SYS32_exit |
| 648 | case SYS32_exit: |
| 649 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 650 | internal_exit(tcp); |
| 651 | break; |
| 652 | } |
| 653 | return 0; |
| 654 | } |
| 655 | |
Wichert Akkerman | c792698 | 2000-04-10 22:22:31 +0000 | [diff] [blame] | 656 | |
| 657 | #ifdef LINUX |
| 658 | #if defined (I386) |
| 659 | static long eax; |
| 660 | #elif defined (IA64) |
| 661 | long r8, r10, psr; |
| 662 | long ia32 = 0; |
| 663 | #elif defined (POWERPC) |
| 664 | static long result,flags; |
| 665 | #elif defined (M68K) |
| 666 | static int d0; |
| 667 | #elif defined (ARM) |
| 668 | static int r0; |
| 669 | #elif defined (ALPHA) |
| 670 | static long r0; |
| 671 | static long a3; |
| 672 | #elif defined (SPARC) |
Wichert Akkerman | 00a82ee | 2001-03-28 20:29:17 +0000 | [diff] [blame] | 673 | static struct regs regs; |
Wichert Akkerman | c792698 | 2000-04-10 22:22:31 +0000 | [diff] [blame] | 674 | static unsigned long trap; |
| 675 | #elif defined(MIPS) |
| 676 | static long a3; |
| 677 | static long r2; |
Michal Ludvig | 10a88d0 | 2002-10-07 14:31:00 +0000 | [diff] [blame] | 678 | #elif defined(S390) || defined(S390X) |
Wichert Akkerman | c792698 | 2000-04-10 22:22:31 +0000 | [diff] [blame] | 679 | static long gpr2; |
| 680 | static long pc; |
Michal Ludvig | 882eda8 | 2002-11-11 12:50:47 +0000 | [diff] [blame] | 681 | static long syscall_mode; |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 682 | #elif defined(HPPA) |
| 683 | static long r28; |
Wichert Akkerman | ccef637 | 2002-05-01 16:39:22 +0000 | [diff] [blame] | 684 | #elif defined(SH) |
| 685 | static long r0; |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 686 | #elif defined(X86_64) |
| 687 | static long rax; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 688 | #endif |
Wichert Akkerman | c792698 | 2000-04-10 22:22:31 +0000 | [diff] [blame] | 689 | #endif /* LINUX */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 690 | #ifdef FREEBSD |
| 691 | struct reg regs; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 692 | #endif /* FREEBSD */ |
Wichert Akkerman | c792698 | 2000-04-10 22:22:31 +0000 | [diff] [blame] | 693 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 694 | int |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 695 | get_scno(tcp) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 696 | struct tcb *tcp; |
| 697 | { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 698 | long scno = 0; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 699 | #ifndef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 700 | int pid = tcp->pid; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 701 | #endif /* !PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 702 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 703 | #ifdef LINUX |
Michal Ludvig | 10a88d0 | 2002-10-07 14:31:00 +0000 | [diff] [blame] | 704 | #if defined(S390) || defined(S390X) |
Michal Ludvig | 882eda8 | 2002-11-11 12:50:47 +0000 | [diff] [blame] | 705 | if (upeek(pid, PT_GPR2, &syscall_mode) < 0) |
Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 706 | return -1; |
Michal Ludvig | 882eda8 | 2002-11-11 12:50:47 +0000 | [diff] [blame] | 707 | if (syscall_mode != -ENOSYS){ |
| 708 | /* |
| 709 | * Since kernel version 2.5.44 the scno gets passed in gpr2. |
| 710 | */ |
| 711 | scno = syscall_mode; |
| 712 | } |
| 713 | else { |
| 714 | /* |
| 715 | * Old style of "passing" the scno via the SVC instruction. |
| 716 | */ |
| 717 | |
| 718 | long opcode, offset_reg, tmp; |
| 719 | void * svc_addr; |
| 720 | int gpr_offset[16] = {PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3, |
| 721 | PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7, |
| 722 | PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11, |
| 723 | PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15}; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 724 | |
Michal Ludvig | 882eda8 | 2002-11-11 12:50:47 +0000 | [diff] [blame] | 725 | if (upeek(pid, PT_PSWADDR, &pc) < 0) |
| 726 | return -1; |
| 727 | opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0); |
| 728 | if (errno) |
| 729 | return -1; |
| 730 | |
| 731 | /* |
| 732 | * We have to check if the SVC got executed directly or via an |
| 733 | * EXECUTE instruction. In case of EXECUTE it is necessary to do |
| 734 | * instruction decoding to derive the system call number. |
| 735 | * Unfortunately the opcode sizes of EXECUTE and SVC are differently, |
| 736 | * so that this doesn't work if a SVC opcode is part of an EXECUTE |
| 737 | * opcode. Since there is no way to find out the opcode size this |
| 738 | * is the best we can do... |
| 739 | */ |
| 740 | |
| 741 | if ((opcode & 0xff00) == 0x0a00) { |
| 742 | /* SVC opcode */ |
| 743 | scno = opcode & 0xff; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 744 | } |
Michal Ludvig | 882eda8 | 2002-11-11 12:50:47 +0000 | [diff] [blame] | 745 | else { |
| 746 | /* SVC got executed by EXECUTE instruction */ |
| 747 | |
| 748 | /* |
| 749 | * Do instruction decoding of EXECUTE. If you really want to |
| 750 | * understand this, read the Principles of Operations. |
| 751 | */ |
| 752 | svc_addr = (void *) (opcode & 0xfff); |
| 753 | |
| 754 | tmp = 0; |
| 755 | offset_reg = (opcode & 0x000f0000) >> 16; |
| 756 | if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0)) |
| 757 | return -1; |
| 758 | svc_addr += tmp; |
| 759 | |
| 760 | tmp = 0; |
| 761 | offset_reg = (opcode & 0x0000f000) >> 12; |
| 762 | if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0)) |
| 763 | return -1; |
| 764 | svc_addr += tmp; |
| 765 | |
| 766 | scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0); |
| 767 | if (errno) |
| 768 | return -1; |
| 769 | #if defined(S390X) |
| 770 | scno >>= 48; |
| 771 | #else |
| 772 | scno >>= 16; |
| 773 | #endif |
| 774 | tmp = 0; |
| 775 | offset_reg = (opcode & 0x00f00000) >> 20; |
| 776 | if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0)) |
| 777 | return -1; |
| 778 | |
| 779 | scno = (scno | tmp) & 0xff; |
| 780 | } |
| 781 | } |
Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 782 | #elif defined (POWERPC) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 783 | if (upeek(pid, 4*PT_R0, &scno) < 0) |
| 784 | return -1; |
| 785 | if (!(tcp->flags & TCB_INSYSCALL)) { |
| 786 | /* Check if we return from execve. */ |
| 787 | if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) { |
| 788 | tcp->flags &= ~TCB_WAITEXECVE; |
| 789 | return 0; |
| 790 | } |
| 791 | } |
| 792 | #elif defined (I386) |
| 793 | if (upeek(pid, 4*ORIG_EAX, &scno) < 0) |
| 794 | return -1; |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 795 | #elif defined (X86_64) |
| 796 | if (upeek(pid, 8*ORIG_RAX, &scno) < 0) |
| 797 | return -1; |
| 798 | |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 799 | if (!(tcp->flags & TCB_INSYSCALL)) { |
| 800 | static int currpers=-1; |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 801 | long val; |
| 802 | |
| 803 | /* Check CS register value. On x86-64 linux it is: |
| 804 | * 0x33 for long mode (64 bit) |
| 805 | * 0x23 for compatibility mode (32 bit) |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 806 | * It takes only one ptrace and thus doesn't need |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 807 | * to be cached. |
| 808 | */ |
| 809 | if (upeek(pid, 8*CS, &val) < 0) |
| 810 | return -1; |
| 811 | switch(val) |
| 812 | { |
| 813 | case 0x23: currpers = 1; break; |
| 814 | case 0x33: currpers = 0; break; |
| 815 | default: |
| 816 | fprintf(stderr, "Unknown value CS=0x%02X while " |
| 817 | "detecting personality of process " |
| 818 | "PID=%d\n", (int)val, pid); |
| 819 | currpers = current_personality; |
| 820 | break; |
| 821 | } |
| 822 | #if 0 |
| 823 | /* This version analyzes the opcode of a syscall instruction. |
| 824 | * (int 0x80 on i386 vs. syscall on x86-64) |
| 825 | * It works, but is too complicated. |
| 826 | */ |
| 827 | unsigned long val, rip, i; |
| 828 | |
| 829 | if(upeek(pid, 8*RIP, &rip)<0) |
| 830 | perror("upeek(RIP)"); |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 831 | |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 832 | /* sizeof(syscall) == sizeof(int 0x80) == 2 */ |
| 833 | rip-=2; |
| 834 | errno = 0; |
| 835 | |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 836 | call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0); |
| 837 | if (errno) |
| 838 | printf("ptrace_peektext failed: %s\n", |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 839 | strerror(errno)); |
| 840 | switch (call & 0xffff) |
| 841 | { |
| 842 | /* x86-64: syscall = 0x0f 0x05 */ |
| 843 | case 0x050f: currpers = 0; break; |
| 844 | /* i386: int 0x80 = 0xcd 0x80 */ |
| 845 | case 0x80cd: currpers = 1; break; |
| 846 | default: |
| 847 | currpers = current_personality; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 848 | fprintf(stderr, |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 849 | "Unknown syscall opcode (0x%04X) while " |
| 850 | "detecting personality of process " |
| 851 | "PID=%d\n", (int)call, pid); |
| 852 | break; |
| 853 | } |
| 854 | #endif |
| 855 | if(currpers != current_personality) |
| 856 | { |
| 857 | char *names[]={"64 bit", "32 bit"}; |
| 858 | set_personality(currpers); |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 859 | printf("[ Process PID=%d runs in %s mode. ]\n", |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 860 | pid, names[current_personality]); |
| 861 | } |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 862 | } |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 863 | #elif defined(IA64) |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 864 | # define IA64_PSR_IS ((long)1 << 34) |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 865 | if (upeek (pid, PT_CR_IPSR, &psr) >= 0) |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 866 | ia32 = (psr & IA64_PSR_IS) != 0; |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 867 | if (!(tcp->flags & TCB_INSYSCALL)) { |
| 868 | if (ia32) { |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 869 | if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */ |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 870 | return -1; |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 871 | /* Check if we return from execve. */ |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 872 | } else { |
| 873 | if (upeek (pid, PT_R15, &scno) < 0) |
| 874 | return -1; |
| 875 | } |
| 876 | } else { |
| 877 | /* syscall in progress */ |
| 878 | if (upeek (pid, PT_R8, &r8) < 0) |
| 879 | return -1; |
| 880 | if (upeek (pid, PT_R10, &r10) < 0) |
| 881 | return -1; |
| 882 | } |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 883 | if (tcp->flags & TCB_WAITEXECVE) { |
| 884 | tcp->flags &= ~TCB_WAITEXECVE; |
| 885 | return 0; |
| 886 | } |
| 887 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 888 | #elif defined (ARM) |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 889 | { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 890 | long pc; |
| 891 | upeek(pid, 4*15, &pc); |
| 892 | umoven(tcp, pc-4, 4, (char *)&scno); |
| 893 | scno &= 0x000fffff; |
| 894 | } |
| 895 | #elif defined (M68K) |
| 896 | if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0) |
| 897 | return -1; |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 898 | #elif defined (MIPS) |
| 899 | if (upeek(pid, REG_A3, &a3) < 0) |
| 900 | return -1; |
| 901 | |
| 902 | if(!(tcp->flags & TCB_INSYSCALL)) { |
| 903 | if (upeek(pid, REG_V0, &scno) < 0) |
| 904 | return -1; |
| 905 | |
| 906 | if (scno < 0 || scno > nsyscalls) { |
| 907 | if(a3 == 0 || a3 == -1) { |
| 908 | if(debug) |
| 909 | fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno); |
| 910 | return 0; |
| 911 | } |
| 912 | } |
| 913 | } else { |
| 914 | if (upeek(pid, REG_V0, &r2) < 0) |
| 915 | return -1; |
| 916 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 917 | #elif defined (ALPHA) |
| 918 | if (upeek(pid, REG_A3, &a3) < 0) |
| 919 | return -1; |
| 920 | |
| 921 | if (!(tcp->flags & TCB_INSYSCALL)) { |
| 922 | if (upeek(pid, REG_R0, &scno) < 0) |
| 923 | return -1; |
| 924 | |
| 925 | /* Check if we return from execve. */ |
| 926 | if (scno == 0 && tcp->flags & TCB_WAITEXECVE) { |
| 927 | tcp->flags &= ~TCB_WAITEXECVE; |
| 928 | return 0; |
| 929 | } |
| 930 | |
| 931 | /* |
| 932 | * Do some sanity checks to figure out if it's |
| 933 | * really a syscall entry |
| 934 | */ |
| 935 | if (scno < 0 || scno > nsyscalls) { |
| 936 | if (a3 == 0 || a3 == -1) { |
| 937 | if (debug) |
| 938 | fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno); |
| 939 | return 0; |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | else { |
| 944 | if (upeek(pid, REG_R0, &r0) < 0) |
| 945 | return -1; |
| 946 | } |
| 947 | #elif defined (SPARC) |
| 948 | /* Everything we need is in the current register set. */ |
| 949 | if (ptrace(PTRACE_GETREGS,pid,(char *)®s,0) < 0) |
| 950 | return -1; |
| 951 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 952 | /* If we are entering, then disassemble the syscall trap. */ |
| 953 | if (!(tcp->flags & TCB_INSYSCALL)) { |
| 954 | /* Retrieve the syscall trap instruction. */ |
| 955 | errno = 0; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 956 | trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 957 | if (errno) |
| 958 | return -1; |
| 959 | |
| 960 | /* Disassemble the trap to see what personality to use. */ |
| 961 | switch (trap) { |
| 962 | case 0x91d02010: |
| 963 | /* Linux/SPARC syscall trap. */ |
| 964 | set_personality(0); |
| 965 | break; |
Wichert Akkerman | dacfb6e | 1999-06-03 14:21:07 +0000 | [diff] [blame] | 966 | case 0x91d0206d: |
| 967 | /* Linux/SPARC64 syscall trap. */ |
| 968 | fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n"); |
| 969 | return -1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 970 | case 0x91d02000: |
| 971 | /* SunOS syscall trap. (pers 1) */ |
| 972 | fprintf(stderr,"syscall: SunOS no support\n"); |
| 973 | return -1; |
| 974 | case 0x91d02008: |
| 975 | /* Solaris 2.x syscall trap. (per 2) */ |
| 976 | set_personality(1); |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 977 | break; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 978 | case 0x91d02009: |
| 979 | /* NetBSD/FreeBSD syscall trap. */ |
| 980 | fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n"); |
| 981 | return -1; |
| 982 | case 0x91d02027: |
| 983 | /* Solaris 2.x gettimeofday */ |
| 984 | set_personality(1); |
| 985 | break; |
| 986 | default: |
| 987 | /* Unknown syscall trap. */ |
| 988 | if(tcp->flags & TCB_WAITEXECVE) { |
| 989 | tcp->flags &= ~TCB_WAITEXECVE; |
| 990 | return 0; |
| 991 | } |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 992 | 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] | 993 | return -1; |
| 994 | } |
| 995 | |
| 996 | /* Extract the system call number from the registers. */ |
| 997 | if (trap == 0x91d02027) |
| 998 | scno = 156; |
| 999 | else |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1000 | scno = regs.r_g1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1001 | if (scno == 0) { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1002 | scno = regs.r_o0; |
| 1003 | memmove (®s.r_o0, ®s.r_o1, 7*sizeof(regs.r_o0)); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1004 | } |
| 1005 | } |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1006 | #elif defined(HPPA) |
| 1007 | if (upeek(pid, PT_GR20, &scno) < 0) |
| 1008 | return -1; |
| 1009 | if (!(tcp->flags & TCB_INSYSCALL)) { |
| 1010 | /* Check if we return from execve. */ |
| 1011 | if ((tcp->flags & TCB_WAITEXECVE)) { |
| 1012 | tcp->flags &= ~TCB_WAITEXECVE; |
| 1013 | return 0; |
| 1014 | } |
| 1015 | } |
Wichert Akkerman | ccef637 | 2002-05-01 16:39:22 +0000 | [diff] [blame] | 1016 | #elif defined(SH) |
| 1017 | /* |
| 1018 | * In the new syscall ABI, the system call number is in R3. |
| 1019 | */ |
| 1020 | if (upeek(pid, 4*(REG_REG0+3), &scno) < 0) |
| 1021 | return -1; |
| 1022 | |
| 1023 | if (scno < 0) { |
| 1024 | /* Odd as it may seem, a glibc bug has been known to cause |
| 1025 | glibc to issue bogus negative syscall numbers. So for |
| 1026 | our purposes, make strace print what it *should* have been */ |
| 1027 | long correct_scno = (scno & 0xff); |
| 1028 | if (debug) |
| 1029 | fprintf(stderr, |
Michal Ludvig | 53b320f | 2002-09-23 13:30:09 +0000 | [diff] [blame] | 1030 | "Detected glibc bug: bogus system call number = %ld, " |
| 1031 | "correcting to %ld\n", |
Wichert Akkerman | ccef637 | 2002-05-01 16:39:22 +0000 | [diff] [blame] | 1032 | scno, |
| 1033 | correct_scno); |
| 1034 | scno = correct_scno; |
| 1035 | } |
| 1036 | |
| 1037 | |
| 1038 | if (!(tcp->flags & TCB_INSYSCALL)) { |
| 1039 | /* Check if we return from execve. */ |
| 1040 | if (scno == 0 && tcp->flags & TCB_WAITEXECVE) { |
| 1041 | tcp->flags &= ~TCB_WAITEXECVE; |
| 1042 | return 0; |
| 1043 | } |
| 1044 | } |
| 1045 | #endif /* SH */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1046 | #endif /* LINUX */ |
| 1047 | #ifdef SUNOS4 |
| 1048 | if (upeek(pid, uoff(u_arg[7]), &scno) < 0) |
| 1049 | return -1; |
Wichert Akkerman | ccef637 | 2002-05-01 16:39:22 +0000 | [diff] [blame] | 1050 | #elif defined(SH) |
| 1051 | /* new syscall ABI returns result in R0 */ |
| 1052 | if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0) |
| 1053 | return -1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1054 | #endif |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1055 | #ifdef USE_PROCFS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1056 | #ifdef HAVE_PR_SYSCALL |
John Hughes | 2529971 | 2001-03-06 10:10:06 +0000 | [diff] [blame] | 1057 | scno = tcp->status.PR_SYSCALL; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1058 | #else /* !HAVE_PR_SYSCALL */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1059 | #ifndef FREEBSD |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1060 | scno = tcp->status.PR_WHAT; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1061 | #else /* FREEBSD */ |
| 1062 | if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) { |
| 1063 | perror("pread"); |
| 1064 | return -1; |
| 1065 | } |
| 1066 | switch (regs.r_eax) { |
| 1067 | case SYS_syscall: |
| 1068 | case SYS___syscall: |
| 1069 | pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int)); |
| 1070 | break; |
| 1071 | default: |
| 1072 | scno = regs.r_eax; |
| 1073 | break; |
| 1074 | } |
| 1075 | #endif /* FREEBSD */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1076 | #endif /* !HAVE_PR_SYSCALL */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1077 | #endif /* USE_PROCFS */ |
Wichert Akkerman | 5ae21ea | 2000-05-01 01:53:59 +0000 | [diff] [blame] | 1078 | if (!(tcp->flags & TCB_INSYSCALL)) |
| 1079 | tcp->scno = scno; |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1080 | return 1; |
| 1081 | } |
| 1082 | |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1083 | |
| 1084 | int |
| 1085 | syscall_fixup(tcp) |
| 1086 | struct tcb *tcp; |
| 1087 | { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1088 | #ifndef USE_PROCFS |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1089 | int pid = tcp->pid; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 1090 | #else /* USE_PROCFS */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1091 | int scno = tcp->scno; |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1092 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1093 | if (!(tcp->flags & TCB_INSYSCALL)) { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1094 | if (tcp->status.PR_WHY != PR_SYSENTRY) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1095 | if ( |
| 1096 | scno == SYS_fork |
| 1097 | #ifdef SYS_vfork |
| 1098 | || scno == SYS_vfork |
| 1099 | #endif /* SYS_vfork */ |
John Hughes | 4e36a81 | 2001-04-18 15:11:51 +0000 | [diff] [blame] | 1100 | #ifdef SYS_fork1 |
| 1101 | || scno == SYS_fork1 |
| 1102 | #endif /* SYS_fork1 */ |
| 1103 | #ifdef SYS_forkall |
| 1104 | || scno == SYS_forkall |
| 1105 | #endif /* SYS_forkall */ |
| 1106 | #ifdef SYS_rfork1 |
| 1107 | || scno == SYS_rfork1 |
| 1108 | #endif /* SYS_fork1 */ |
| 1109 | #ifdef SYS_rforkall |
| 1110 | || scno == SYS_rforkall |
| 1111 | #endif /* SYS_rforkall */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1112 | ) { |
| 1113 | /* We are returning in the child, fake it. */ |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1114 | tcp->status.PR_WHY = PR_SYSENTRY; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1115 | trace_syscall(tcp); |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1116 | tcp->status.PR_WHY = PR_SYSEXIT; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1117 | } |
| 1118 | else { |
| 1119 | fprintf(stderr, "syscall: missing entry\n"); |
| 1120 | tcp->flags |= TCB_INSYSCALL; |
| 1121 | } |
| 1122 | } |
| 1123 | } |
| 1124 | else { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1125 | if (tcp->status.PR_WHY != PR_SYSEXIT) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1126 | fprintf(stderr, "syscall: missing exit\n"); |
| 1127 | tcp->flags &= ~TCB_INSYSCALL; |
| 1128 | } |
| 1129 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1130 | #endif /* USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1131 | #ifdef SUNOS4 |
| 1132 | if (!(tcp->flags & TCB_INSYSCALL)) { |
| 1133 | if (scno == 0) { |
| 1134 | fprintf(stderr, "syscall: missing entry\n"); |
| 1135 | tcp->flags |= TCB_INSYSCALL; |
| 1136 | } |
| 1137 | } |
| 1138 | else { |
| 1139 | if (scno != 0) { |
| 1140 | if (debug) { |
| 1141 | /* |
| 1142 | * This happens when a signal handler |
| 1143 | * for a signal which interrupted a |
| 1144 | * a system call makes another system call. |
| 1145 | */ |
| 1146 | fprintf(stderr, "syscall: missing exit\n"); |
| 1147 | } |
| 1148 | tcp->flags &= ~TCB_INSYSCALL; |
| 1149 | } |
| 1150 | } |
| 1151 | #endif /* SUNOS4 */ |
| 1152 | #ifdef LINUX |
| 1153 | #if defined (I386) |
| 1154 | if (upeek(pid, 4*EAX, &eax) < 0) |
| 1155 | return -1; |
| 1156 | if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) { |
| 1157 | if (debug) |
| 1158 | fprintf(stderr, "stray syscall exit: eax = %ld\n", eax); |
| 1159 | return 0; |
| 1160 | } |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1161 | #elif defined (X86_64) |
| 1162 | if (upeek(pid, 8*RAX, &rax) < 0) |
| 1163 | return -1; |
| 1164 | if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) { |
| 1165 | if (debug) |
| 1166 | fprintf(stderr, "stray syscall exit: rax = %ld\n", rax); |
| 1167 | return 0; |
| 1168 | } |
Michal Ludvig | 10a88d0 | 2002-10-07 14:31:00 +0000 | [diff] [blame] | 1169 | #elif defined (S390) || defined (S390X) |
Wichert Akkerman | 12f75d1 | 2000-02-14 16:23:40 +0000 | [diff] [blame] | 1170 | if (upeek(pid, PT_GPR2, &gpr2) < 0) |
| 1171 | return -1; |
Michal Ludvig | 882eda8 | 2002-11-11 12:50:47 +0000 | [diff] [blame] | 1172 | if (syscall_mode != -ENOSYS) |
| 1173 | syscall_mode = tcp->scno; |
| 1174 | if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) { |
Wichert Akkerman | 12f75d1 | 2000-02-14 16:23:40 +0000 | [diff] [blame] | 1175 | if (debug) |
| 1176 | fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2); |
| 1177 | return 0; |
| 1178 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1179 | #elif defined (POWERPC) |
| 1180 | # define SO_MASK 0x10000000 |
| 1181 | if (upeek(pid, 4*PT_CCR, &flags) < 0) |
| 1182 | return -1; |
| 1183 | if (upeek(pid, 4*PT_R3, &result) < 0) |
| 1184 | return -1; |
| 1185 | if (flags & SO_MASK) |
| 1186 | result = -result; |
| 1187 | #elif defined (M68K) |
| 1188 | if (upeek(pid, 4*PT_D0, &d0) < 0) |
| 1189 | return -1; |
| 1190 | if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) { |
| 1191 | if (debug) |
| 1192 | fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0); |
| 1193 | return 0; |
| 1194 | } |
| 1195 | #elif defined (ARM) |
| 1196 | if (upeek(pid, 4*0, (long *)&r0) < 0) |
| 1197 | return -1; |
| 1198 | if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) { |
| 1199 | if (debug) |
| 1200 | fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0); |
| 1201 | return 0; |
| 1202 | } |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1203 | #elif defined (HPPA) |
| 1204 | if (upeek(pid, PT_GR28, &r28) < 0) |
| 1205 | return -1; |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 1206 | #elif defined(IA64) |
| 1207 | if (upeek(pid, PT_R10, &r10) < 0) |
| 1208 | return -1; |
| 1209 | if (upeek(pid, PT_R8, &r8) < 0) |
| 1210 | return -1; |
| 1211 | if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) { |
| 1212 | if (debug) |
| 1213 | fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8); |
| 1214 | return 0; |
| 1215 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1216 | #endif |
| 1217 | #endif /* LINUX */ |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1218 | return 1; |
| 1219 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1220 | |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1221 | int |
| 1222 | get_error(tcp) |
| 1223 | struct tcb *tcp; |
| 1224 | { |
| 1225 | int u_error = 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1226 | #ifdef LINUX |
Michal Ludvig | 10a88d0 | 2002-10-07 14:31:00 +0000 | [diff] [blame] | 1227 | #if defined(S390) || defined(S390X) |
Wichert Akkerman | 12f75d1 | 2000-02-14 16:23:40 +0000 | [diff] [blame] | 1228 | if (gpr2 && (unsigned) -gpr2 < nerrnos) { |
| 1229 | tcp->u_rval = -1; |
| 1230 | u_error = -gpr2; |
| 1231 | } |
| 1232 | else { |
| 1233 | tcp->u_rval = gpr2; |
| 1234 | u_error = 0; |
| 1235 | } |
Michal Ludvig | 10a88d0 | 2002-10-07 14:31:00 +0000 | [diff] [blame] | 1236 | #else /* !S390 && !S390X */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1237 | #ifdef I386 |
| 1238 | if (eax < 0 && -eax < nerrnos) { |
| 1239 | tcp->u_rval = -1; |
| 1240 | u_error = -eax; |
| 1241 | } |
| 1242 | else { |
| 1243 | tcp->u_rval = eax; |
| 1244 | u_error = 0; |
| 1245 | } |
| 1246 | #else /* !I386 */ |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1247 | #ifdef X86_64 |
| 1248 | if (rax < 0 && -rax < nerrnos) { |
| 1249 | tcp->u_rval = -1; |
| 1250 | u_error = -rax; |
| 1251 | } |
| 1252 | else { |
| 1253 | tcp->u_rval = rax; |
| 1254 | u_error = 0; |
| 1255 | } |
| 1256 | #else |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1257 | #ifdef IA64 |
| 1258 | if (ia32) { |
| 1259 | int err; |
| 1260 | |
| 1261 | err = (int)r8; |
| 1262 | if (err < 0 && -err < nerrnos) { |
| 1263 | tcp->u_rval = -1; |
| 1264 | u_error = -err; |
| 1265 | } |
| 1266 | else { |
| 1267 | tcp->u_rval = err; |
| 1268 | u_error = 0; |
| 1269 | } |
| 1270 | } else { |
| 1271 | if (r10) { |
| 1272 | tcp->u_rval = -1; |
| 1273 | u_error = r8; |
| 1274 | } else { |
| 1275 | tcp->u_rval = r8; |
| 1276 | u_error = 0; |
| 1277 | } |
| 1278 | } |
| 1279 | #else /* !IA64 */ |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 1280 | #ifdef MIPS |
| 1281 | if (a3) { |
| 1282 | tcp->u_rval = -1; |
| 1283 | u_error = r2; |
| 1284 | } else { |
| 1285 | tcp->u_rval = r2; |
| 1286 | u_error = 0; |
| 1287 | } |
| 1288 | #else |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1289 | #ifdef POWERPC |
| 1290 | if (result && (unsigned) -result < nerrnos) { |
| 1291 | tcp->u_rval = -1; |
| 1292 | u_error = -result; |
| 1293 | } |
| 1294 | else { |
| 1295 | tcp->u_rval = result; |
| 1296 | u_error = 0; |
| 1297 | } |
| 1298 | #else /* !POWERPC */ |
| 1299 | #ifdef M68K |
| 1300 | if (d0 && (unsigned) -d0 < nerrnos) { |
| 1301 | tcp->u_rval = -1; |
| 1302 | u_error = -d0; |
| 1303 | } |
| 1304 | else { |
| 1305 | tcp->u_rval = d0; |
| 1306 | u_error = 0; |
| 1307 | } |
| 1308 | #else /* !M68K */ |
| 1309 | #ifdef ARM |
| 1310 | if (r0 && (unsigned) -r0 < nerrnos) { |
| 1311 | tcp->u_rval = -1; |
| 1312 | u_error = -r0; |
| 1313 | } |
| 1314 | else { |
| 1315 | tcp->u_rval = r0; |
| 1316 | u_error = 0; |
| 1317 | } |
| 1318 | #else /* !ARM */ |
| 1319 | #ifdef ALPHA |
| 1320 | if (a3) { |
| 1321 | tcp->u_rval = -1; |
| 1322 | u_error = r0; |
| 1323 | } |
| 1324 | else { |
| 1325 | tcp->u_rval = r0; |
| 1326 | u_error = 0; |
| 1327 | } |
| 1328 | #else /* !ALPHA */ |
| 1329 | #ifdef SPARC |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1330 | if (regs.r_psr & PSR_C) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1331 | tcp->u_rval = -1; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1332 | u_error = regs.r_o0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1333 | } |
| 1334 | else { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1335 | tcp->u_rval = regs.r_o0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1336 | u_error = 0; |
| 1337 | } |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1338 | #else /* !SPARC */ |
| 1339 | #ifdef HPPA |
| 1340 | if (r28 && (unsigned) -r28 < nerrnos) { |
| 1341 | tcp->u_rval = -1; |
| 1342 | u_error = -r28; |
| 1343 | } |
| 1344 | else { |
| 1345 | tcp->u_rval = r28; |
| 1346 | u_error = 0; |
| 1347 | } |
Wichert Akkerman | ccef637 | 2002-05-01 16:39:22 +0000 | [diff] [blame] | 1348 | #else |
| 1349 | #ifdef SH |
| 1350 | /* interpret R0 as return value or error number */ |
| 1351 | if (r0 && (unsigned) -r0 < nerrnos) { |
| 1352 | tcp->u_rval = -1; |
| 1353 | u_error = -r0; |
| 1354 | } |
| 1355 | else { |
| 1356 | tcp->u_rval = r0; |
| 1357 | u_error = 0; |
| 1358 | } |
| 1359 | #endif /* SH */ |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1360 | #endif /* HPPA */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1361 | #endif /* SPARC */ |
| 1362 | #endif /* ALPHA */ |
| 1363 | #endif /* ARM */ |
| 1364 | #endif /* M68K */ |
| 1365 | #endif /* POWERPC */ |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 1366 | #endif /* MIPS */ |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1367 | #endif /* IA64 */ |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1368 | #endif /* X86_64 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1369 | #endif /* I386 */ |
Michal Ludvig | 10a88d0 | 2002-10-07 14:31:00 +0000 | [diff] [blame] | 1370 | #endif /* S390 || S390X */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1371 | #endif /* LINUX */ |
| 1372 | #ifdef SUNOS4 |
| 1373 | /* get error code from user struct */ |
| 1374 | if (upeek(pid, uoff(u_error), &u_error) < 0) |
| 1375 | return -1; |
| 1376 | u_error >>= 24; /* u_error is a char */ |
| 1377 | |
| 1378 | /* get system call return value */ |
| 1379 | if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0) |
| 1380 | return -1; |
| 1381 | #endif /* SUNOS4 */ |
| 1382 | #ifdef SVR4 |
| 1383 | #ifdef SPARC |
| 1384 | /* Judicious guessing goes a long way. */ |
| 1385 | if (tcp->status.pr_reg[R_PSR] & 0x100000) { |
| 1386 | tcp->u_rval = -1; |
| 1387 | u_error = tcp->status.pr_reg[R_O0]; |
| 1388 | } |
| 1389 | else { |
| 1390 | tcp->u_rval = tcp->status.pr_reg[R_O0]; |
| 1391 | u_error = 0; |
| 1392 | } |
| 1393 | #endif /* SPARC */ |
| 1394 | #ifdef I386 |
| 1395 | /* Wanna know how to kill an hour single-stepping? */ |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1396 | if (tcp->status.PR_REG[EFL] & 0x1) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1397 | tcp->u_rval = -1; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1398 | u_error = tcp->status.PR_REG[EAX]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1399 | } |
| 1400 | else { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1401 | tcp->u_rval = tcp->status.PR_REG[EAX]; |
Wichert Akkerman | 16a03d2 | 2000-08-10 02:14:04 +0000 | [diff] [blame] | 1402 | #ifdef HAVE_LONG_LONG |
| 1403 | tcp->u_lrval = |
| 1404 | ((unsigned long long) tcp->status.PR_REG[EDX] << 32) + |
| 1405 | tcp->status.PR_REG[EAX]; |
| 1406 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1407 | u_error = 0; |
| 1408 | } |
| 1409 | #endif /* I386 */ |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1410 | #ifdef X86_64 |
| 1411 | /* Wanna know how to kill an hour single-stepping? */ |
| 1412 | if (tcp->status.PR_REG[EFLAGS] & 0x1) { |
| 1413 | tcp->u_rval = -1; |
| 1414 | u_error = tcp->status.PR_REG[RAX]; |
| 1415 | } |
| 1416 | else { |
| 1417 | tcp->u_rval = tcp->status.PR_REG[RAX]; |
| 1418 | u_error = 0; |
| 1419 | } |
| 1420 | #endif /* X86_64 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1421 | #ifdef MIPS |
| 1422 | if (tcp->status.pr_reg[CTX_A3]) { |
| 1423 | tcp->u_rval = -1; |
| 1424 | u_error = tcp->status.pr_reg[CTX_V0]; |
| 1425 | } |
| 1426 | else { |
| 1427 | tcp->u_rval = tcp->status.pr_reg[CTX_V0]; |
| 1428 | u_error = 0; |
| 1429 | } |
| 1430 | #endif /* MIPS */ |
| 1431 | #endif /* SVR4 */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1432 | #ifdef FREEBSD |
| 1433 | if (regs.r_eflags & PSL_C) { |
| 1434 | tcp->u_rval = -1; |
| 1435 | u_error = regs.r_eax; |
| 1436 | } else { |
| 1437 | tcp->u_rval = regs.r_eax; |
| 1438 | tcp->u_lrval = |
| 1439 | ((unsigned long long) regs.r_edx << 32) + regs.r_eax; |
| 1440 | u_error = 0; |
| 1441 | } |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 1442 | #endif /* FREEBSD */ |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1443 | tcp->u_error = u_error; |
| 1444 | return 1; |
| 1445 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1446 | |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1447 | int syscall_enter(tcp) |
| 1448 | struct tcb *tcp; |
| 1449 | { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1450 | #ifndef USE_PROCFS |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1451 | int pid = tcp->pid; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 1452 | #endif /* !USE_PROCFS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1453 | #ifdef LINUX |
Michal Ludvig | 10a88d0 | 2002-10-07 14:31:00 +0000 | [diff] [blame] | 1454 | #if defined(S390) || defined(S390X) |
Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 1455 | { |
| 1456 | int i; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1457 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
| 1458 | tcp->u_nargs = sysent[tcp->scno].nargs; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 1459 | else |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1460 | tcp->u_nargs = MAX_ARGS; |
Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 1461 | for (i = 0; i < tcp->u_nargs; i++) { |
Michal Ludvig | 10a88d0 | 2002-10-07 14:31:00 +0000 | [diff] [blame] | 1462 | if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0) |
Wichert Akkerman | 4dc8a2a | 1999-12-23 14:20:14 +0000 | [diff] [blame] | 1463 | return -1; |
| 1464 | } |
| 1465 | } |
| 1466 | #elif defined (ALPHA) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1467 | { |
| 1468 | int i; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1469 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
| 1470 | tcp->u_nargs = sysent[tcp->scno].nargs; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 1471 | else |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1472 | tcp->u_nargs = MAX_ARGS; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1473 | for (i = 0; i < tcp->u_nargs; i++) { |
Wichert Akkerman | b859bea | 1999-04-18 22:50:50 +0000 | [diff] [blame] | 1474 | /* WTA: if scno is out-of-bounds this will bomb. Add range-check |
| 1475 | * for scno somewhere above here! |
| 1476 | */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1477 | if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0) |
| 1478 | return -1; |
| 1479 | } |
| 1480 | } |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1481 | #elif defined (IA64) |
| 1482 | { |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 1483 | if (!ia32) { |
| 1484 | unsigned long *out0, *rbs_end, cfm, sof, sol, i; |
| 1485 | /* be backwards compatible with kernel < 2.4.4... */ |
| 1486 | # ifndef PT_RBS_END |
| 1487 | # define PT_RBS_END PT_AR_BSP |
| 1488 | # endif |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1489 | |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 1490 | if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0) |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1491 | return -1; |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 1492 | if (upeek(pid, PT_CFM, (long *) &cfm) < 0) |
| 1493 | return -1; |
| 1494 | |
| 1495 | sof = (cfm >> 0) & 0x7f; |
| 1496 | sol = (cfm >> 7) & 0x7f; |
| 1497 | out0 = ia64_rse_skip_regs(rbs_end, -sof + sol); |
| 1498 | |
| 1499 | if (tcp->scno >= 0 && tcp->scno < nsyscalls |
| 1500 | && sysent[tcp->scno].nargs != -1) |
| 1501 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1502 | else |
| 1503 | tcp->u_nargs = MAX_ARGS; |
| 1504 | for (i = 0; i < tcp->u_nargs; ++i) { |
| 1505 | if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i), |
| 1506 | sizeof(long), (char *) &tcp->u_arg[i]) < 0) |
| 1507 | return -1; |
| 1508 | } |
| 1509 | } else { |
| 1510 | int i; |
| 1511 | |
| 1512 | if (/* EBX = out0 */ |
| 1513 | upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0 |
| 1514 | /* ECX = out1 */ |
| 1515 | || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0 |
| 1516 | /* EDX = out2 */ |
| 1517 | || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0 |
| 1518 | /* ESI = out3 */ |
| 1519 | || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0 |
| 1520 | /* EDI = out4 */ |
| 1521 | || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0 |
| 1522 | /* EBP = out5 */ |
| 1523 | || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0) |
| 1524 | return -1; |
| 1525 | |
| 1526 | for (i = 0; i < 6; ++i) |
| 1527 | /* truncate away IVE sign-extension */ |
| 1528 | tcp->u_arg[i] &= 0xffffffff; |
| 1529 | |
| 1530 | if (tcp->scno >= 0 && tcp->scno < nsyscalls |
| 1531 | && sysent[tcp->scno].nargs != -1) |
| 1532 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1533 | else |
| 1534 | tcp->u_nargs = 5; |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1535 | } |
| 1536 | } |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 1537 | #elif defined (MIPS) |
| 1538 | { |
| 1539 | long sp; |
| 1540 | int i, nargs; |
| 1541 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1542 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
| 1543 | nargs = tcp->u_nargs = sysent[tcp->scno].nargs; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 1544 | else |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1545 | nargs = tcp->u_nargs = MAX_ARGS; |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 1546 | if(nargs > 4) { |
| 1547 | if(upeek(pid, REG_SP, &sp) < 0) |
| 1548 | return -1; |
| 1549 | for(i = 0; i < 4; i++) { |
| 1550 | if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0) |
| 1551 | return -1; |
| 1552 | } |
| 1553 | umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]), |
| 1554 | (char *)(tcp->u_arg + 4)); |
| 1555 | } else { |
| 1556 | for(i = 0; i < nargs; i++) { |
| 1557 | if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0) |
| 1558 | return -1; |
| 1559 | } |
| 1560 | } |
| 1561 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1562 | #elif defined (POWERPC) |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 1563 | #ifndef PT_ORIG_R3 |
| 1564 | #define PT_ORIG_R3 34 |
| 1565 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1566 | { |
| 1567 | int i; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1568 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
| 1569 | tcp->u_nargs = sysent[tcp->scno].nargs; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 1570 | else |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1571 | tcp->u_nargs = MAX_ARGS; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1572 | for (i = 0; i < tcp->u_nargs; i++) { |
| 1573 | if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0) |
| 1574 | return -1; |
| 1575 | } |
| 1576 | } |
| 1577 | #elif defined (SPARC) |
| 1578 | { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1579 | int i; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1580 | |
| 1581 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
| 1582 | tcp->u_nargs = sysent[tcp->scno].nargs; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 1583 | else |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1584 | tcp->u_nargs = MAX_ARGS; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1585 | for (i = 0; i < tcp->u_nargs; i++) |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1586 | tcp->u_arg[i] = *((®s.r_o0) + i); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1587 | } |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1588 | #elif defined (HPPA) |
| 1589 | { |
| 1590 | int i; |
| 1591 | |
| 1592 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
| 1593 | tcp->u_nargs = sysent[tcp->scno].nargs; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 1594 | else |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1595 | tcp->u_nargs = MAX_ARGS; |
| 1596 | for (i = 0; i < tcp->u_nargs; i++) { |
| 1597 | if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0) |
| 1598 | return -1; |
| 1599 | } |
| 1600 | } |
Wichert Akkerman | ccef637 | 2002-05-01 16:39:22 +0000 | [diff] [blame] | 1601 | #elif defined(SH) |
| 1602 | { |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 1603 | int i; |
Wichert Akkerman | ccef637 | 2002-05-01 16:39:22 +0000 | [diff] [blame] | 1604 | static int syscall_regs[] = { |
| 1605 | REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7, |
| 1606 | REG_REG0, REG_REG0+1, REG_REG0+2 |
| 1607 | }; |
| 1608 | |
| 1609 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1610 | for (i = 0; i < tcp->u_nargs; i++) { |
| 1611 | if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0) |
| 1612 | return -1; |
| 1613 | } |
| 1614 | } |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1615 | #elif defined(X86_64) |
| 1616 | { |
| 1617 | int i; |
| 1618 | static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = { |
| 1619 | {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */ |
| 1620 | {RBX,RCX,RDX,RDX,RSI,RDI,RBP} /* i386 ABI */ |
| 1621 | }; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 1622 | |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1623 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
| 1624 | tcp->u_nargs = sysent[tcp->scno].nargs; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 1625 | else |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1626 | tcp->u_nargs = MAX_ARGS; |
| 1627 | for (i = 0; i < tcp->u_nargs; i++) { |
| 1628 | if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0) |
| 1629 | return -1; |
| 1630 | } |
| 1631 | } |
Wichert Akkerman | faf7222 | 2000-02-19 23:59:03 +0000 | [diff] [blame] | 1632 | #else /* Other architecture (like i386) (32bits specific) */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1633 | { |
| 1634 | int i; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1635 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
| 1636 | tcp->u_nargs = sysent[tcp->scno].nargs; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 1637 | else |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1638 | tcp->u_nargs = MAX_ARGS; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1639 | for (i = 0; i < tcp->u_nargs; i++) { |
| 1640 | if (upeek(pid, i*4, &tcp->u_arg[i]) < 0) |
| 1641 | return -1; |
| 1642 | } |
| 1643 | } |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 1644 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1645 | #endif /* LINUX */ |
| 1646 | #ifdef SUNOS4 |
| 1647 | { |
| 1648 | int i; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1649 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
| 1650 | tcp->u_nargs = sysent[tcp->scno].nargs; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 1651 | else |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1652 | tcp->u_nargs = MAX_ARGS; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1653 | for (i = 0; i < tcp->u_nargs; i++) { |
| 1654 | struct user *u; |
| 1655 | |
| 1656 | if (upeek(pid, uoff(u_arg[0]) + |
| 1657 | (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0) |
| 1658 | return -1; |
| 1659 | } |
| 1660 | } |
| 1661 | #endif /* SUNOS4 */ |
| 1662 | #ifdef SVR4 |
| 1663 | #ifdef MIPS |
| 1664 | /* |
| 1665 | * SGI is broken: even though it has pr_sysarg, it doesn't |
| 1666 | * set them on system call entry. Get a clue. |
| 1667 | */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1668 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1669 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1670 | else |
| 1671 | tcp->u_nargs = tcp->status.pr_nsysarg; |
| 1672 | if (tcp->u_nargs > 4) { |
| 1673 | memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0], |
| 1674 | 4*sizeof(tcp->u_arg[0])); |
| 1675 | umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16, |
| 1676 | (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4)); |
| 1677 | } |
| 1678 | else { |
| 1679 | memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0], |
| 1680 | tcp->u_nargs*sizeof(tcp->u_arg[0])); |
| 1681 | } |
John Hughes | 2529971 | 2001-03-06 10:10:06 +0000 | [diff] [blame] | 1682 | #elif UNIXWARE >= 2 |
| 1683 | /* |
| 1684 | * Like SGI, UnixWare doesn't set pr_sysarg until system call exit |
| 1685 | */ |
| 1686 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
| 1687 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1688 | else |
| 1689 | tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg; |
| 1690 | umoven(tcp, tcp->status.PR_REG[UESP] + 4, |
| 1691 | tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg); |
| 1692 | #elif defined (HAVE_PR_SYSCALL) |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1693 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1694 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1695 | else |
| 1696 | tcp->u_nargs = tcp->status.pr_nsysarg; |
| 1697 | { |
| 1698 | int i; |
| 1699 | for (i = 0; i < tcp->u_nargs; i++) |
| 1700 | tcp->u_arg[i] = tcp->status.pr_sysarg[i]; |
| 1701 | } |
John Hughes | 2529971 | 2001-03-06 10:10:06 +0000 | [diff] [blame] | 1702 | #elif defined (I386) |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1703 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1704 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 1705 | else |
| 1706 | tcp->u_nargs = 5; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1707 | umoven(tcp, tcp->status.PR_REG[UESP] + 4, |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1708 | tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg); |
John Hughes | 2529971 | 2001-03-06 10:10:06 +0000 | [diff] [blame] | 1709 | #else |
| 1710 | I DONT KNOW WHAT TO DO |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1711 | #endif /* !HAVE_PR_SYSCALL */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1712 | #endif /* SVR4 */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1713 | #ifdef FREEBSD |
| 1714 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && |
| 1715 | sysent[tcp->scno].nargs > tcp->status.val) |
| 1716 | tcp->u_nargs = sysent[tcp->scno].nargs; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 1717 | else |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1718 | tcp->u_nargs = tcp->status.val; |
| 1719 | if (tcp->u_nargs < 0) |
| 1720 | tcp->u_nargs = 0; |
| 1721 | if (tcp->u_nargs > MAX_ARGS) |
| 1722 | tcp->u_nargs = MAX_ARGS; |
| 1723 | switch(regs.r_eax) { |
| 1724 | case SYS___syscall: |
| 1725 | pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long), |
| 1726 | regs.r_esp + sizeof(int) + sizeof(quad_t)); |
| 1727 | break; |
| 1728 | case SYS_syscall: |
| 1729 | pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long), |
| 1730 | regs.r_esp + 2 * sizeof(int)); |
| 1731 | break; |
| 1732 | default: |
| 1733 | pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long), |
| 1734 | regs.r_esp + sizeof(int)); |
| 1735 | break; |
| 1736 | } |
| 1737 | #endif /* FREEBSD */ |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1738 | return 1; |
| 1739 | } |
| 1740 | |
| 1741 | int |
| 1742 | trace_syscall(tcp) |
| 1743 | struct tcb *tcp; |
| 1744 | { |
| 1745 | int sys_res; |
| 1746 | struct timeval tv; |
| 1747 | int res; |
| 1748 | |
| 1749 | /* Measure the exit time as early as possible to avoid errors. */ |
| 1750 | if (dtime && (tcp->flags & TCB_INSYSCALL)) |
| 1751 | gettimeofday(&tv, NULL); |
| 1752 | |
| 1753 | res = get_scno(tcp); |
| 1754 | if (res != 1) |
| 1755 | return res; |
| 1756 | |
| 1757 | res = syscall_fixup(tcp); |
| 1758 | if (res != 1) |
| 1759 | return res; |
| 1760 | |
| 1761 | if (tcp->flags & TCB_INSYSCALL) { |
| 1762 | long u_error; |
| 1763 | res = get_error(tcp); |
| 1764 | if (res != 1) |
| 1765 | return res; |
| 1766 | u_error = tcp->u_error; |
| 1767 | |
| 1768 | |
| 1769 | internal_syscall(tcp); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1770 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && |
| 1771 | !(qual_flags[tcp->scno] & QUAL_TRACE)) { |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1772 | tcp->flags &= ~TCB_INSYSCALL; |
| 1773 | return 0; |
| 1774 | } |
| 1775 | |
| 1776 | if (tcp->flags & TCB_REPRINT) { |
| 1777 | printleader(tcp); |
| 1778 | tprintf("<... "); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1779 | if (tcp->scno >= nsyscalls || tcp->scno < 0) |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1780 | tprintf("syscall_%lu", tcp->scno); |
| 1781 | else |
| 1782 | tprintf("%s", sysent[tcp->scno].sys_name); |
| 1783 | tprintf(" resumed> "); |
| 1784 | } |
| 1785 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1786 | if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) { |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1787 | call_count[tcp->scno]++; |
| 1788 | if (tcp->u_error) |
| 1789 | error_count[tcp->scno]++; |
| 1790 | tv_sub(&tv, &tv, &tcp->etime); |
| 1791 | #ifdef LINUX |
| 1792 | if (tv_cmp(&tv, &tcp->dtime) > 0) { |
| 1793 | static struct timeval one_tick = |
| 1794 | { 0, 1000000 / HZ }; |
| 1795 | |
| 1796 | if (tv_nz(&tcp->dtime)) |
| 1797 | tv = tcp->dtime; |
| 1798 | else if (tv_cmp(&tv, &one_tick) > 0) { |
| 1799 | if (tv_cmp(&shortest, &one_tick) < 0) |
| 1800 | tv = shortest; |
| 1801 | else |
| 1802 | tv = one_tick; |
| 1803 | } |
| 1804 | } |
| 1805 | #endif /* LINUX */ |
| 1806 | if (tv_cmp(&tv, &shortest) < 0) |
| 1807 | shortest = tv; |
| 1808 | tv_add(&tv_count[tcp->scno], |
| 1809 | &tv_count[tcp->scno], &tv); |
| 1810 | tcp->flags &= ~TCB_INSYSCALL; |
| 1811 | return 0; |
| 1812 | } |
| 1813 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1814 | if (tcp->scno >= nsyscalls || tcp->scno < 0 |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1815 | || (qual_flags[tcp->scno] & QUAL_RAW)) |
| 1816 | sys_res = printargs(tcp); |
Michal Ludvig | 17f8fb3 | 2002-11-06 13:17:21 +0000 | [diff] [blame] | 1817 | else { |
| 1818 | if (not_failing_only && tcp->u_error) |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 1819 | return 0; /* ignore failed syscalls */ |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1820 | sys_res = (*sysent[tcp->scno].sys_func)(tcp); |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 1821 | } |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1822 | u_error = tcp->u_error; |
| 1823 | tprintf(") "); |
| 1824 | tabto(acolumn); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1825 | if (tcp->scno >= nsyscalls || tcp->scno < 0 || |
| 1826 | qual_flags[tcp->scno] & QUAL_RAW) { |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1827 | if (u_error) |
| 1828 | tprintf("= -1 (errno %ld)", u_error); |
| 1829 | else |
| 1830 | tprintf("= %#lx", tcp->u_rval); |
| 1831 | } |
| 1832 | else if (!(sys_res & RVAL_NONE) && u_error) { |
| 1833 | switch (u_error) { |
| 1834 | #ifdef LINUX |
| 1835 | case ERESTARTSYS: |
| 1836 | tprintf("= ? ERESTARTSYS (To be restarted)"); |
| 1837 | break; |
| 1838 | case ERESTARTNOINTR: |
| 1839 | tprintf("= ? ERESTARTNOINTR (To be restarted)"); |
| 1840 | break; |
| 1841 | case ERESTARTNOHAND: |
| 1842 | tprintf("= ? ERESTARTNOHAND (To be restarted)"); |
| 1843 | break; |
| 1844 | #endif /* LINUX */ |
| 1845 | default: |
| 1846 | tprintf("= -1 "); |
Wichert Akkerman | 4527dae | 2002-03-31 19:03:29 +0000 | [diff] [blame] | 1847 | if (u_error < 0) |
| 1848 | tprintf("E??? (errno %ld)", u_error); |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1849 | else if (u_error < nerrnos) |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 1850 | tprintf("%s (%s)", errnoent[u_error], |
| 1851 | strerror(u_error)); |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1852 | else |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 1853 | tprintf("ERRNO_%ld (%s)", u_error, |
| 1854 | strerror(u_error)); |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1855 | break; |
| 1856 | } |
| 1857 | } |
| 1858 | else { |
| 1859 | if (sys_res & RVAL_NONE) |
| 1860 | tprintf("= ?"); |
| 1861 | else { |
| 1862 | switch (sys_res & RVAL_MASK) { |
| 1863 | case RVAL_HEX: |
| 1864 | tprintf("= %#lx", tcp->u_rval); |
| 1865 | break; |
| 1866 | case RVAL_OCTAL: |
| 1867 | tprintf("= %#lo", tcp->u_rval); |
| 1868 | break; |
| 1869 | case RVAL_UDECIMAL: |
| 1870 | tprintf("= %lu", tcp->u_rval); |
| 1871 | break; |
| 1872 | case RVAL_DECIMAL: |
| 1873 | tprintf("= %ld", tcp->u_rval); |
| 1874 | break; |
Wichert Akkerman | 16a03d2 | 2000-08-10 02:14:04 +0000 | [diff] [blame] | 1875 | #ifdef HAVE_LONG_LONG |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1876 | case RVAL_LHEX: |
| 1877 | tprintf("= %#llx", tcp->u_lrval); |
Wichert Akkerman | 16a03d2 | 2000-08-10 02:14:04 +0000 | [diff] [blame] | 1878 | break; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1879 | case RVAL_LOCTAL: |
| 1880 | tprintf("= %#llo", tcp->u_lrval); |
| 1881 | break; |
| 1882 | case RVAL_LUDECIMAL: |
| 1883 | tprintf("= %llu", tcp->u_lrval); |
| 1884 | break; |
| 1885 | case RVAL_LDECIMAL: |
| 1886 | tprintf("= %lld", tcp->u_lrval); |
| 1887 | break; |
Wichert Akkerman | 16a03d2 | 2000-08-10 02:14:04 +0000 | [diff] [blame] | 1888 | #endif |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1889 | default: |
| 1890 | fprintf(stderr, |
| 1891 | "invalid rval format\n"); |
| 1892 | break; |
| 1893 | } |
| 1894 | } |
| 1895 | if ((sys_res & RVAL_STR) && tcp->auxstr) |
| 1896 | tprintf(" (%s)", tcp->auxstr); |
| 1897 | } |
| 1898 | if (dtime) { |
| 1899 | tv_sub(&tv, &tv, &tcp->etime); |
| 1900 | tprintf(" <%ld.%06ld>", |
| 1901 | (long) tv.tv_sec, (long) tv.tv_usec); |
| 1902 | } |
| 1903 | printtrailer(tcp); |
| 1904 | |
| 1905 | dumpio(tcp); |
| 1906 | if (fflush(tcp->outf) == EOF) |
| 1907 | return -1; |
| 1908 | tcp->flags &= ~TCB_INSYSCALL; |
| 1909 | return 0; |
| 1910 | } |
| 1911 | |
| 1912 | /* Entering system call */ |
| 1913 | res = syscall_enter(tcp); |
| 1914 | if (res != 1) |
| 1915 | return res; |
| 1916 | |
Pavel Machek | d8ae7e3 | 2000-02-01 17:17:25 +0000 | [diff] [blame] | 1917 | switch (tcp->scno + NR_SYSCALL_BASE) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1918 | #ifdef LINUX |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1919 | #if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1920 | case SYS_socketcall: |
| 1921 | decode_subcall(tcp, SYS_socket_subcall, |
| 1922 | SYS_socket_nsubcalls, deref_style); |
| 1923 | break; |
| 1924 | case SYS_ipc: |
| 1925 | decode_subcall(tcp, SYS_ipc_subcall, |
| 1926 | SYS_ipc_nsubcalls, shift_style); |
| 1927 | break; |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1928 | #endif /* !ALPHA && !MIPS && !SPARC && !HPPA && !X86_64 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1929 | #ifdef SPARC |
| 1930 | case SYS_socketcall: |
| 1931 | sparc_socket_decode (tcp); |
| 1932 | break; |
| 1933 | #endif |
| 1934 | #endif /* LINUX */ |
| 1935 | #ifdef SVR4 |
| 1936 | #ifdef SYS_pgrpsys_subcall |
| 1937 | case SYS_pgrpsys: |
| 1938 | decode_subcall(tcp, SYS_pgrpsys_subcall, |
| 1939 | SYS_pgrpsys_nsubcalls, shift_style); |
| 1940 | break; |
| 1941 | #endif /* SYS_pgrpsys_subcall */ |
| 1942 | #ifdef SYS_sigcall_subcall |
| 1943 | case SYS_sigcall: |
| 1944 | decode_subcall(tcp, SYS_sigcall_subcall, |
| 1945 | SYS_sigcall_nsubcalls, mask_style); |
| 1946 | break; |
| 1947 | #endif /* SYS_sigcall_subcall */ |
| 1948 | case SYS_msgsys: |
| 1949 | decode_subcall(tcp, SYS_msgsys_subcall, |
| 1950 | SYS_msgsys_nsubcalls, shift_style); |
| 1951 | break; |
| 1952 | case SYS_shmsys: |
| 1953 | decode_subcall(tcp, SYS_shmsys_subcall, |
| 1954 | SYS_shmsys_nsubcalls, shift_style); |
| 1955 | break; |
| 1956 | case SYS_semsys: |
| 1957 | decode_subcall(tcp, SYS_semsys_subcall, |
| 1958 | SYS_semsys_nsubcalls, shift_style); |
| 1959 | break; |
| 1960 | #if 0 /* broken */ |
| 1961 | case SYS_utssys: |
| 1962 | decode_subcall(tcp, SYS_utssys_subcall, |
| 1963 | SYS_utssys_nsubcalls, shift_style); |
| 1964 | break; |
| 1965 | #endif |
| 1966 | case SYS_sysfs: |
| 1967 | decode_subcall(tcp, SYS_sysfs_subcall, |
| 1968 | SYS_sysfs_nsubcalls, shift_style); |
| 1969 | break; |
| 1970 | case SYS_spcall: |
| 1971 | decode_subcall(tcp, SYS_spcall_subcall, |
| 1972 | SYS_spcall_nsubcalls, shift_style); |
| 1973 | break; |
| 1974 | #ifdef SYS_context_subcall |
| 1975 | case SYS_context: |
| 1976 | decode_subcall(tcp, SYS_context_subcall, |
| 1977 | SYS_context_nsubcalls, shift_style); |
| 1978 | break; |
| 1979 | #endif /* SYS_context_subcall */ |
Wichert Akkerman | 8829a55 | 1999-06-11 13:18:40 +0000 | [diff] [blame] | 1980 | #ifdef SYS_door_subcall |
| 1981 | case SYS_door: |
| 1982 | decode_subcall(tcp, SYS_door_subcall, |
| 1983 | SYS_door_nsubcalls, door_style); |
| 1984 | break; |
| 1985 | #endif /* SYS_door_subcall */ |
John Hughes | bdf48f5 | 2001-03-06 15:08:09 +0000 | [diff] [blame] | 1986 | #ifdef SYS_kaio_subcall |
| 1987 | case SYS_kaio: |
| 1988 | decode_subcall(tcp, SYS_kaio_subcall, |
| 1989 | SYS_kaio_nsubcalls, shift_style); |
| 1990 | break; |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 1991 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1992 | #endif /* SVR4 */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1993 | #ifdef FREEBSD |
| 1994 | case SYS_msgsys: |
| 1995 | case SYS_shmsys: |
| 1996 | case SYS_semsys: |
| 1997 | decode_subcall(tcp, 0, 0, table_style); |
| 1998 | break; |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 1999 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2000 | #ifdef SUNOS4 |
| 2001 | case SYS_semsys: |
| 2002 | decode_subcall(tcp, SYS_semsys_subcall, |
| 2003 | SYS_semsys_nsubcalls, shift_style); |
| 2004 | break; |
| 2005 | case SYS_msgsys: |
| 2006 | decode_subcall(tcp, SYS_msgsys_subcall, |
| 2007 | SYS_msgsys_nsubcalls, shift_style); |
| 2008 | break; |
| 2009 | case SYS_shmsys: |
| 2010 | decode_subcall(tcp, SYS_shmsys_subcall, |
| 2011 | SYS_shmsys_nsubcalls, shift_style); |
| 2012 | break; |
| 2013 | #endif |
| 2014 | } |
| 2015 | |
| 2016 | internal_syscall(tcp); |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2017 | 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] | 2018 | tcp->flags |= TCB_INSYSCALL; |
| 2019 | return 0; |
| 2020 | } |
| 2021 | |
| 2022 | if (cflag) { |
| 2023 | gettimeofday(&tcp->etime, NULL); |
| 2024 | tcp->flags |= TCB_INSYSCALL; |
| 2025 | return 0; |
| 2026 | } |
| 2027 | |
| 2028 | printleader(tcp); |
| 2029 | tcp->flags &= ~TCB_REPRINT; |
| 2030 | tcp_last = tcp; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2031 | if (tcp->scno >= nsyscalls || tcp->scno < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2032 | tprintf("syscall_%lu(", tcp->scno); |
| 2033 | else |
| 2034 | tprintf("%s(", sysent[tcp->scno].sys_name); |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 2035 | if (tcp->scno >= nsyscalls || tcp->scno < 0 || |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2036 | ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit)) |
| 2037 | sys_res = printargs(tcp); |
| 2038 | else |
| 2039 | sys_res = (*sysent[tcp->scno].sys_func)(tcp); |
| 2040 | if (fflush(tcp->outf) == EOF) |
| 2041 | return -1; |
| 2042 | tcp->flags |= TCB_INSYSCALL; |
| 2043 | /* Measure the entrance time as late as possible to avoid errors. */ |
| 2044 | if (dtime) |
| 2045 | gettimeofday(&tcp->etime, NULL); |
| 2046 | return sys_res; |
| 2047 | } |
| 2048 | |
| 2049 | int |
| 2050 | printargs(tcp) |
| 2051 | struct tcb *tcp; |
| 2052 | { |
| 2053 | if (entering(tcp)) { |
| 2054 | int i; |
| 2055 | |
| 2056 | for (i = 0; i < tcp->u_nargs; i++) |
| 2057 | tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]); |
| 2058 | } |
| 2059 | return 0; |
| 2060 | } |
| 2061 | |
| 2062 | long |
| 2063 | getrval2(tcp) |
| 2064 | struct tcb *tcp; |
| 2065 | { |
| 2066 | long val = -1; |
| 2067 | |
| 2068 | #ifdef LINUX |
| 2069 | #ifdef SPARC |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2070 | struct regs regs; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2071 | if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)®s,0) < 0) |
| 2072 | return -1; |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2073 | val = regs.r_o1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2074 | #endif /* SPARC */ |
| 2075 | #endif /* LINUX */ |
| 2076 | |
| 2077 | #ifdef SUNOS4 |
| 2078 | if (upeek(tcp->pid, uoff(u_rval2), &val) < 0) |
| 2079 | return -1; |
| 2080 | #endif /* SUNOS4 */ |
| 2081 | |
| 2082 | #ifdef SVR4 |
| 2083 | #ifdef SPARC |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2084 | val = tcp->status.PR_REG[R_O1]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2085 | #endif /* SPARC */ |
| 2086 | #ifdef I386 |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2087 | val = tcp->status.PR_REG[EDX]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2088 | #endif /* I386 */ |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 2089 | #ifdef X86_64 |
| 2090 | val = tcp->status.PR_REG[RDX]; |
| 2091 | #endif /* X86_64 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2092 | #ifdef MIPS |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2093 | val = tcp->status.PR_REG[CTX_V1]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2094 | #endif /* MIPS */ |
| 2095 | #endif /* SVR4 */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2096 | #ifdef FREEBSD |
| 2097 | struct reg regs; |
| 2098 | pread(tcp->pfd_reg, ®s, sizeof(regs), 0); |
| 2099 | val = regs.r_edx; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 2100 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2101 | return val; |
| 2102 | } |
| 2103 | |
| 2104 | /* |
| 2105 | * Apparently, indirect system calls have already be converted by ptrace(2), |
| 2106 | * so if you see "indir" this program has gone astray. |
| 2107 | */ |
| 2108 | int |
| 2109 | sys_indir(tcp) |
| 2110 | struct tcb *tcp; |
| 2111 | { |
| 2112 | int i, scno, nargs; |
| 2113 | |
| 2114 | if (entering(tcp)) { |
| 2115 | if ((scno = tcp->u_arg[0]) > nsyscalls) { |
| 2116 | fprintf(stderr, "Bogus syscall: %u\n", scno); |
| 2117 | return 0; |
| 2118 | } |
| 2119 | nargs = sysent[scno].nargs; |
| 2120 | tprintf("%s", sysent[scno].sys_name); |
| 2121 | for (i = 0; i < nargs; i++) |
| 2122 | tprintf(", %#lx", tcp->u_arg[i+1]); |
| 2123 | } |
| 2124 | return 0; |
| 2125 | } |
| 2126 | |
| 2127 | static int |
| 2128 | time_cmp(a, b) |
| 2129 | void *a; |
| 2130 | void *b; |
| 2131 | { |
| 2132 | return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]); |
| 2133 | } |
| 2134 | |
| 2135 | static int |
| 2136 | syscall_cmp(a, b) |
| 2137 | void *a; |
| 2138 | void *b; |
| 2139 | { |
| 2140 | return strcmp(sysent[*((int *) a)].sys_name, |
| 2141 | sysent[*((int *) b)].sys_name); |
| 2142 | } |
| 2143 | |
| 2144 | static int |
| 2145 | count_cmp(a, b) |
| 2146 | void *a; |
| 2147 | void *b; |
| 2148 | { |
| 2149 | int m = call_count[*((int *) a)], n = call_count[*((int *) b)]; |
| 2150 | |
| 2151 | return (m < n) ? 1 : (m > n) ? -1 : 0; |
| 2152 | } |
| 2153 | |
| 2154 | static int (*sortfun)(); |
| 2155 | static struct timeval overhead = { -1, -1 }; |
| 2156 | |
| 2157 | void |
| 2158 | set_sortby(sortby) |
| 2159 | char *sortby; |
| 2160 | { |
| 2161 | if (strcmp(sortby, "time") == 0) |
| 2162 | sortfun = time_cmp; |
| 2163 | else if (strcmp(sortby, "calls") == 0) |
| 2164 | sortfun = count_cmp; |
| 2165 | else if (strcmp(sortby, "name") == 0) |
| 2166 | sortfun = syscall_cmp; |
| 2167 | else if (strcmp(sortby, "nothing") == 0) |
| 2168 | sortfun = NULL; |
| 2169 | else { |
| 2170 | fprintf(stderr, "invalid sortby: `%s'\n", sortby); |
| 2171 | exit(1); |
| 2172 | } |
| 2173 | } |
| 2174 | |
| 2175 | void set_overhead(n) |
| 2176 | int n; |
| 2177 | { |
| 2178 | overhead.tv_sec = n / 1000000; |
| 2179 | overhead.tv_usec = n % 1000000; |
| 2180 | } |
| 2181 | |
| 2182 | void |
| 2183 | call_summary(outf) |
| 2184 | FILE *outf; |
| 2185 | { |
| 2186 | int i, j; |
| 2187 | int call_cum, error_cum; |
| 2188 | struct timeval tv_cum, dtv; |
| 2189 | double percent; |
| 2190 | char *dashes = "-------------------------"; |
| 2191 | char error_str[16]; |
| 2192 | |
| 2193 | call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0; |
| 2194 | if (overhead.tv_sec == -1) { |
| 2195 | tv_mul(&overhead, &shortest, 8); |
| 2196 | tv_div(&overhead, &overhead, 10); |
| 2197 | } |
| 2198 | for (i = 0; i < nsyscalls; i++) { |
| 2199 | sorted_count[i] = i; |
| 2200 | if (call_count[i] == 0) |
| 2201 | continue; |
| 2202 | tv_mul(&dtv, &overhead, call_count[i]); |
| 2203 | tv_sub(&tv_count[i], &tv_count[i], &dtv); |
| 2204 | call_cum += call_count[i]; |
| 2205 | error_cum += error_count[i]; |
| 2206 | tv_add(&tv_cum, &tv_cum, &tv_count[i]); |
| 2207 | } |
| 2208 | if (sortfun) |
| 2209 | qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun); |
| 2210 | fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n", |
| 2211 | "% time", "seconds", "usecs/call", |
| 2212 | "calls", "errors", "syscall"); |
| 2213 | fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n", |
| 2214 | dashes, dashes, dashes, dashes, dashes, dashes); |
| 2215 | for (i = 0; i < nsyscalls; i++) { |
| 2216 | j = sorted_count[i]; |
| 2217 | if (call_count[j] == 0) |
| 2218 | continue; |
| 2219 | tv_div(&dtv, &tv_count[j], call_count[j]); |
| 2220 | if (error_count[j]) |
| 2221 | sprintf(error_str, "%d", error_count[j]); |
| 2222 | else |
| 2223 | error_str[0] = '\0'; |
| 2224 | percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum); |
| 2225 | fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n", |
| 2226 | percent, (long) tv_count[j].tv_sec, |
| 2227 | (long) tv_count[j].tv_usec, |
| 2228 | (long) 1000000 * dtv.tv_sec + dtv.tv_usec, |
| 2229 | call_count[j], error_str, sysent[j].sys_name); |
| 2230 | } |
| 2231 | fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n", |
| 2232 | dashes, dashes, dashes, dashes, dashes, dashes); |
| 2233 | if (error_cum) |
| 2234 | sprintf(error_str, "%d", error_cum); |
| 2235 | else |
| 2236 | error_str[0] = '\0'; |
| 2237 | fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n", |
| 2238 | "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "", |
| 2239 | call_cum, error_str, "total"); |
| 2240 | } |