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