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