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