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 | */ |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 747 | #ifndef USE_PROCFS |
| 748 | static |
| 749 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 750 | int |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 751 | get_scno_on_sysenter(struct tcb *tcp) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 752 | { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 753 | long scno = 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 754 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 755 | #ifdef LINUX |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 756 | # if defined(S390) || defined(S390X) |
Roland McGrath | 96dc514 | 2003-01-20 10:23:04 +0000 | [diff] [blame] | 757 | if (tcp->flags & TCB_WAITEXECVE) { |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 758 | /* This is the post-execve SIGTRAP. */ |
Roland McGrath | 96dc514 | 2003-01-20 10:23:04 +0000 | [diff] [blame] | 759 | tcp->flags &= ~TCB_WAITEXECVE; |
| 760 | return 0; |
| 761 | } |
Roland McGrath | 2f924ca | 2003-06-26 22:23:28 +0000 | [diff] [blame] | 762 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 763 | if (upeek(tcp, PT_GPR2, &syscall_mode) < 0) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 764 | return -1; |
Roland McGrath | 2f924ca | 2003-06-26 22:23:28 +0000 | [diff] [blame] | 765 | |
| 766 | if (syscall_mode != -ENOSYS) { |
| 767 | /* |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 768 | * Since kernel version 2.5.44 the scno gets passed in gpr2. |
Roland McGrath | 2f924ca | 2003-06-26 22:23:28 +0000 | [diff] [blame] | 769 | */ |
| 770 | scno = syscall_mode; |
| 771 | } else { |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 772 | /* |
Michal Ludvig | 882eda8 | 2002-11-11 12:50:47 +0000 | [diff] [blame] | 773 | * Old style of "passing" the scno via the SVC instruction. |
| 774 | */ |
Michal Ludvig | 882eda8 | 2002-11-11 12:50:47 +0000 | [diff] [blame] | 775 | long opcode, offset_reg, tmp; |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 776 | void *svc_addr; |
Denys Vlasenko | 7c9ba8b | 2011-08-19 19:46:32 +0200 | [diff] [blame] | 777 | static const int gpr_offset[16] = { |
| 778 | PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3, |
| 779 | PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7, |
| 780 | PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11, |
| 781 | PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15 |
| 782 | }; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 783 | |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 784 | if (upeek(tcp, PT_PSWADDR, &pc) < 0) |
Michal Ludvig | 882eda8 | 2002-11-11 12:50:47 +0000 | [diff] [blame] | 785 | return -1; |
Roland McGrath | 96dc514 | 2003-01-20 10:23:04 +0000 | [diff] [blame] | 786 | errno = 0; |
Denys Vlasenko | fb03667 | 2009-01-23 16:30:26 +0000 | [diff] [blame] | 787 | opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-sizeof(long)), 0); |
Roland McGrath | 96dc514 | 2003-01-20 10:23:04 +0000 | [diff] [blame] | 788 | if (errno) { |
| 789 | perror("peektext(pc-oneword)"); |
Michal Ludvig | 882eda8 | 2002-11-11 12:50:47 +0000 | [diff] [blame] | 790 | return -1; |
Roland McGrath | 96dc514 | 2003-01-20 10:23:04 +0000 | [diff] [blame] | 791 | } |
Michal Ludvig | 882eda8 | 2002-11-11 12:50:47 +0000 | [diff] [blame] | 792 | |
| 793 | /* |
| 794 | * We have to check if the SVC got executed directly or via an |
| 795 | * EXECUTE instruction. In case of EXECUTE it is necessary to do |
| 796 | * instruction decoding to derive the system call number. |
| 797 | * Unfortunately the opcode sizes of EXECUTE and SVC are differently, |
| 798 | * so that this doesn't work if a SVC opcode is part of an EXECUTE |
| 799 | * opcode. Since there is no way to find out the opcode size this |
| 800 | * is the best we can do... |
| 801 | */ |
Michal Ludvig | 882eda8 | 2002-11-11 12:50:47 +0000 | [diff] [blame] | 802 | if ((opcode & 0xff00) == 0x0a00) { |
| 803 | /* SVC opcode */ |
| 804 | scno = opcode & 0xff; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 805 | } |
Michal Ludvig | 882eda8 | 2002-11-11 12:50:47 +0000 | [diff] [blame] | 806 | else { |
| 807 | /* SVC got executed by EXECUTE instruction */ |
| 808 | |
| 809 | /* |
| 810 | * Do instruction decoding of EXECUTE. If you really want to |
| 811 | * understand this, read the Principles of Operations. |
| 812 | */ |
| 813 | svc_addr = (void *) (opcode & 0xfff); |
| 814 | |
| 815 | tmp = 0; |
| 816 | offset_reg = (opcode & 0x000f0000) >> 16; |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 817 | if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0)) |
Michal Ludvig | 882eda8 | 2002-11-11 12:50:47 +0000 | [diff] [blame] | 818 | return -1; |
| 819 | svc_addr += tmp; |
| 820 | |
| 821 | tmp = 0; |
| 822 | offset_reg = (opcode & 0x0000f000) >> 12; |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 823 | if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0)) |
Michal Ludvig | 882eda8 | 2002-11-11 12:50:47 +0000 | [diff] [blame] | 824 | return -1; |
| 825 | svc_addr += tmp; |
| 826 | |
Denys Vlasenko | fb03667 | 2009-01-23 16:30:26 +0000 | [diff] [blame] | 827 | scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0); |
Michal Ludvig | 882eda8 | 2002-11-11 12:50:47 +0000 | [diff] [blame] | 828 | if (errno) |
| 829 | return -1; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 830 | # if defined(S390X) |
Michal Ludvig | 882eda8 | 2002-11-11 12:50:47 +0000 | [diff] [blame] | 831 | scno >>= 48; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 832 | # else |
Michal Ludvig | 882eda8 | 2002-11-11 12:50:47 +0000 | [diff] [blame] | 833 | scno >>= 16; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 834 | # endif |
Michal Ludvig | 882eda8 | 2002-11-11 12:50:47 +0000 | [diff] [blame] | 835 | tmp = 0; |
| 836 | offset_reg = (opcode & 0x00f00000) >> 20; |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 837 | if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0)) |
Michal Ludvig | 882eda8 | 2002-11-11 12:50:47 +0000 | [diff] [blame] | 838 | return -1; |
| 839 | |
| 840 | scno = (scno | tmp) & 0xff; |
| 841 | } |
| 842 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 843 | # elif defined (POWERPC) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 844 | if (upeek(tcp, sizeof(unsigned long)*PT_R0, &scno) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 845 | return -1; |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 846 | /* Check if this is the post-execve SIGTRAP. */ |
| 847 | if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) { |
| 848 | tcp->flags &= ~TCB_WAITEXECVE; |
| 849 | return 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 850 | } |
Andreas Schwab | d69fa49 | 2010-07-12 21:39:57 +0200 | [diff] [blame] | 851 | # ifdef POWERPC64 |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 852 | /* TODO: speed up strace by not doing this at every syscall. |
| 853 | * We only need to do it after execve. |
| 854 | */ |
| 855 | int currpers; |
| 856 | long val; |
| 857 | int pid = tcp->pid; |
Andreas Schwab | d69fa49 | 2010-07-12 21:39:57 +0200 | [diff] [blame] | 858 | |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 859 | /* Check for 64/32 bit mode. */ |
| 860 | if (upeek(tcp, sizeof(unsigned long)*PT_MSR, &val) < 0) |
| 861 | return -1; |
| 862 | /* SF is bit 0 of MSR */ |
| 863 | if (val < 0) |
| 864 | currpers = 0; |
| 865 | else |
| 866 | currpers = 1; |
| 867 | if (currpers != current_personality) { |
| 868 | static const char *const names[] = {"64 bit", "32 bit"}; |
| 869 | set_personality(currpers); |
| 870 | fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n", |
| 871 | pid, names[current_personality]); |
Andreas Schwab | d69fa49 | 2010-07-12 21:39:57 +0200 | [diff] [blame] | 872 | } |
| 873 | # endif |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 874 | # elif defined(AVR32) |
| 875 | /* |
| 876 | * Read complete register set in one go. |
| 877 | */ |
| 878 | if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, ®s) < 0) |
| 879 | return -1; |
| 880 | |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 881 | scno = regs.r8; |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 882 | |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 883 | /* Check if this is the post-execve SIGTRAP. */ |
| 884 | if (tcp->flags & TCB_WAITEXECVE) { |
| 885 | tcp->flags &= ~TCB_WAITEXECVE; |
| 886 | return 0; |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 887 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 888 | # elif defined(BFIN) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 889 | if (upeek(tcp, PT_ORIG_P0, &scno)) |
Dmitry V. Levin | 87ea1f4 | 2008-11-10 22:21:41 +0000 | [diff] [blame] | 890 | return -1; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 891 | # elif defined (I386) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 892 | if (upeek(tcp, 4*ORIG_EAX, &scno) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 893 | return -1; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 894 | # elif defined (X86_64) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 895 | if (upeek(tcp, 8*ORIG_RAX, &scno) < 0) |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 896 | return -1; |
| 897 | |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 898 | /* TODO: speed up strace by not doing this at every syscall. |
| 899 | * We only need to do it after execve. |
| 900 | */ |
| 901 | int currpers; |
| 902 | long val; |
| 903 | int pid = tcp->pid; |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 904 | |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 905 | /* Check CS register value. On x86-64 linux it is: |
| 906 | * 0x33 for long mode (64 bit) |
| 907 | * 0x23 for compatibility mode (32 bit) |
| 908 | * It takes only one ptrace and thus doesn't need |
| 909 | * to be cached. |
| 910 | */ |
| 911 | if (upeek(tcp, 8*CS, &val) < 0) |
| 912 | return -1; |
| 913 | switch (val) { |
| 914 | case 0x23: currpers = 1; break; |
| 915 | case 0x33: currpers = 0; break; |
| 916 | default: |
| 917 | fprintf(stderr, "Unknown value CS=0x%02X while " |
| 918 | "detecting personality of process " |
| 919 | "PID=%d\n", (int)val, pid); |
| 920 | currpers = current_personality; |
| 921 | break; |
| 922 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 923 | # if 0 |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 924 | /* This version analyzes the opcode of a syscall instruction. |
| 925 | * (int 0x80 on i386 vs. syscall on x86-64) |
| 926 | * It works, but is too complicated. |
| 927 | */ |
| 928 | unsigned long val, rip, i; |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 929 | |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 930 | if (upeek(tcp, 8*RIP, &rip) < 0) |
| 931 | perror("upeek(RIP)"); |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 932 | |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 933 | /* sizeof(syscall) == sizeof(int 0x80) == 2 */ |
| 934 | rip -= 2; |
| 935 | errno = 0; |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 936 | |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 937 | call = ptrace(PTRACE_PEEKTEXT, pid, (char *)rip, (char *)0); |
| 938 | if (errno) |
| 939 | fprintf(stderr, "ptrace_peektext failed: %s\n", |
| 940 | strerror(errno)); |
| 941 | switch (call & 0xffff) { |
| 942 | /* x86-64: syscall = 0x0f 0x05 */ |
| 943 | case 0x050f: currpers = 0; break; |
| 944 | /* i386: int 0x80 = 0xcd 0x80 */ |
| 945 | case 0x80cd: currpers = 1; break; |
| 946 | default: |
| 947 | currpers = current_personality; |
| 948 | fprintf(stderr, |
| 949 | "Unknown syscall opcode (0x%04X) while " |
| 950 | "detecting personality of process " |
| 951 | "PID=%d\n", (int)call, pid); |
| 952 | break; |
| 953 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 954 | # endif |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 955 | if (currpers != current_personality) { |
| 956 | static const char *const names[] = {"64 bit", "32 bit"}; |
| 957 | set_personality(currpers); |
| 958 | fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n", |
| 959 | pid, names[current_personality]); |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 960 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 961 | # elif defined(IA64) |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 962 | # define IA64_PSR_IS ((long)1 << 34) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 963 | if (upeek(tcp, PT_CR_IPSR, &psr) >= 0) |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 964 | ia32 = (psr & IA64_PSR_IS) != 0; |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 965 | if (ia32) { |
| 966 | if (upeek(tcp, PT_R1, &scno) < 0) /* orig eax */ |
| 967 | return -1; |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 968 | } else { |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 969 | if (upeek(tcp, PT_R15, &scno) < 0) |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 970 | return -1; |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 971 | } |
| 972 | /* Check if this is the post-execve SIGTRAP. */ |
| 973 | if (tcp->flags & TCB_WAITEXECVE) { |
| 974 | tcp->flags &= ~TCB_WAITEXECVE; |
| 975 | return 0; |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 976 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 977 | # elif defined (ARM) |
Roland McGrath | 0f87c49 | 2003-06-03 23:29:04 +0000 | [diff] [blame] | 978 | /* |
| 979 | * Read complete register set in one go. |
| 980 | */ |
Denys Vlasenko | fb03667 | 2009-01-23 16:30:26 +0000 | [diff] [blame] | 981 | if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (void *)®s) == -1) |
Roland McGrath | 0f87c49 | 2003-06-03 23:29:04 +0000 | [diff] [blame] | 982 | return -1; |
| 983 | |
| 984 | /* |
| 985 | * We only need to grab the syscall number on syscall entry. |
| 986 | */ |
| 987 | if (regs.ARM_ip == 0) { |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 988 | /* Check if this is the post-execve SIGTRAP. */ |
| 989 | if (tcp->flags & TCB_WAITEXECVE) { |
| 990 | tcp->flags &= ~TCB_WAITEXECVE; |
| 991 | return 0; |
Roland McGrath | 9bc6340 | 2007-11-01 21:42:18 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Roland McGrath | 0f87c49 | 2003-06-03 23:29:04 +0000 | [diff] [blame] | 994 | /* |
| 995 | * Note: we only deal with only 32-bit CPUs here. |
| 996 | */ |
| 997 | if (regs.ARM_cpsr & 0x20) { |
| 998 | /* |
| 999 | * Get the Thumb-mode system call number |
| 1000 | */ |
| 1001 | scno = regs.ARM_r7; |
| 1002 | } else { |
| 1003 | /* |
| 1004 | * Get the ARM-mode system call number |
| 1005 | */ |
| 1006 | errno = 0; |
Denys Vlasenko | fb03667 | 2009-01-23 16:30:26 +0000 | [diff] [blame] | 1007 | scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(regs.ARM_pc - 4), NULL); |
Roland McGrath | 0f87c49 | 2003-06-03 23:29:04 +0000 | [diff] [blame] | 1008 | if (errno) |
| 1009 | return -1; |
| 1010 | |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 1011 | /* FIXME: bogus check? it is already done before, |
Denys Vlasenko | b88f961 | 2011-08-21 18:03:23 +0200 | [diff] [blame] | 1012 | * so we never can see it here? |
| 1013 | */ |
Roland McGrath | 0f87c49 | 2003-06-03 23:29:04 +0000 | [diff] [blame] | 1014 | if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) { |
| 1015 | tcp->flags &= ~TCB_WAITEXECVE; |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
Roland McGrath | f691bd2 | 2006-04-25 07:34:41 +0000 | [diff] [blame] | 1019 | /* Handle the EABI syscall convention. We do not |
| 1020 | bother converting structures between the two |
| 1021 | ABIs, but basic functionality should work even |
| 1022 | if strace and the traced program have different |
| 1023 | ABIs. */ |
| 1024 | if (scno == 0xef000000) { |
| 1025 | scno = regs.ARM_r7; |
| 1026 | } else { |
| 1027 | if ((scno & 0x0ff00000) != 0x0f900000) { |
| 1028 | fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n", |
| 1029 | scno); |
| 1030 | return -1; |
| 1031 | } |
Roland McGrath | 0f87c49 | 2003-06-03 23:29:04 +0000 | [diff] [blame] | 1032 | |
Roland McGrath | f691bd2 | 2006-04-25 07:34:41 +0000 | [diff] [blame] | 1033 | /* |
| 1034 | * Fixup the syscall number |
| 1035 | */ |
| 1036 | scno &= 0x000fffff; |
| 1037 | } |
Roland McGrath | 0f87c49 | 2003-06-03 23:29:04 +0000 | [diff] [blame] | 1038 | } |
Roland McGrath | 5670331 | 2008-05-20 01:35:55 +0000 | [diff] [blame] | 1039 | if (scno & 0x0f0000) { |
| 1040 | /* |
| 1041 | * Handle ARM specific syscall |
| 1042 | */ |
| 1043 | set_personality(1); |
| 1044 | scno &= 0x0000ffff; |
| 1045 | } else |
| 1046 | set_personality(0); |
Roland McGrath | 0f87c49 | 2003-06-03 23:29:04 +0000 | [diff] [blame] | 1047 | |
Roland McGrath | 0f87c49 | 2003-06-03 23:29:04 +0000 | [diff] [blame] | 1048 | } else { |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 1049 | fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid); |
| 1050 | tcp->flags |= TCB_INSYSCALL; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1051 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1052 | # elif defined (M68K) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1053 | if (upeek(tcp, 4*PT_ORIG_D0, &scno) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1054 | return -1; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1055 | # elif defined (LINUX_MIPSN32) |
Roland McGrath | 542c2c6 | 2008-05-20 01:11:56 +0000 | [diff] [blame] | 1056 | unsigned long long regs[38]; |
| 1057 | |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1058 | if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0) |
Roland McGrath | 542c2c6 | 2008-05-20 01:11:56 +0000 | [diff] [blame] | 1059 | return -1; |
| 1060 | a3 = regs[REG_A3]; |
| 1061 | r2 = regs[REG_V0]; |
| 1062 | |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 1063 | scno = r2; |
Roland McGrath | 542c2c6 | 2008-05-20 01:11:56 +0000 | [diff] [blame] | 1064 | |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 1065 | /* Check if this is the post-execve SIGTRAP. */ |
| 1066 | if (scno == 0 && tcp->flags & TCB_WAITEXECVE) { |
| 1067 | tcp->flags &= ~TCB_WAITEXECVE; |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
| 1071 | if (scno < 0 || scno > nsyscalls) { |
| 1072 | if (a3 == 0 || a3 == -1) { |
| 1073 | if (debug) |
| 1074 | fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno); |
Roland McGrath | 542c2c6 | 2008-05-20 01:11:56 +0000 | [diff] [blame] | 1075 | return 0; |
| 1076 | } |
Roland McGrath | 542c2c6 | 2008-05-20 01:11:56 +0000 | [diff] [blame] | 1077 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1078 | # elif defined (MIPS) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1079 | if (upeek(tcp, REG_A3, &a3) < 0) |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 1080 | return -1; |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 1081 | if (upeek(tcp, REG_V0, &scno) < 0) |
| 1082 | return -1; |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 1083 | |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 1084 | /* Check if this is the post-execve SIGTRAP. */ |
| 1085 | if (scno == 0 && tcp->flags & TCB_WAITEXECVE) { |
| 1086 | tcp->flags &= ~TCB_WAITEXECVE; |
| 1087 | return 0; |
| 1088 | } |
| 1089 | |
| 1090 | if (scno < 0 || scno > nsyscalls) { |
| 1091 | if (a3 == 0 || a3 == -1) { |
| 1092 | if (debug) |
| 1093 | fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno); |
Roland McGrath | 542c2c6 | 2008-05-20 01:11:56 +0000 | [diff] [blame] | 1094 | return 0; |
| 1095 | } |
Wichert Akkerman | f90da01 | 1999-10-31 21:15:38 +0000 | [diff] [blame] | 1096 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1097 | # elif defined (ALPHA) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1098 | if (upeek(tcp, REG_A3, &a3) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1099 | return -1; |
| 1100 | |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 1101 | if (upeek(tcp, REG_R0, &scno) < 0) |
| 1102 | return -1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1103 | |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 1104 | /* Check if this is the post-execve SIGTRAP. */ |
| 1105 | if (scno == 0 && tcp->flags & TCB_WAITEXECVE) { |
| 1106 | tcp->flags &= ~TCB_WAITEXECVE; |
| 1107 | return 0; |
| 1108 | } |
| 1109 | |
| 1110 | /* |
| 1111 | * Do some sanity checks to figure out if it's |
| 1112 | * really a syscall entry |
| 1113 | */ |
| 1114 | if (scno < 0 || scno > nsyscalls) { |
| 1115 | if (a3 == 0 || a3 == -1) { |
| 1116 | if (debug) |
| 1117 | fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1118 | return 0; |
| 1119 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1120 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1121 | # elif defined (SPARC) || defined (SPARC64) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1122 | /* Everything we need is in the current register set. */ |
Denys Vlasenko | fb03667 | 2009-01-23 16:30:26 +0000 | [diff] [blame] | 1123 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1124 | return -1; |
| 1125 | |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 1126 | /* Disassemble the syscall trap. */ |
| 1127 | /* Retrieve the syscall trap instruction. */ |
| 1128 | errno = 0; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1129 | # if defined(SPARC64) |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 1130 | trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)regs.tpc, 0); |
| 1131 | trap >>= 32; |
Mike Frysinger | 8566c50 | 2009-10-12 11:05:14 -0400 | [diff] [blame] | 1132 | # else |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 1133 | trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)regs.pc, 0); |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1134 | # endif |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 1135 | if (errno) |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1136 | return -1; |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 1137 | |
| 1138 | /* Disassemble the trap to see what personality to use. */ |
| 1139 | switch (trap) { |
| 1140 | case 0x91d02010: |
| 1141 | /* Linux/SPARC syscall trap. */ |
| 1142 | set_personality(0); |
| 1143 | break; |
| 1144 | case 0x91d0206d: |
| 1145 | /* Linux/SPARC64 syscall trap. */ |
| 1146 | set_personality(2); |
| 1147 | break; |
| 1148 | case 0x91d02000: |
| 1149 | /* SunOS syscall trap. (pers 1) */ |
| 1150 | fprintf(stderr, "syscall: SunOS no support\n"); |
| 1151 | return -1; |
| 1152 | case 0x91d02008: |
| 1153 | /* Solaris 2.x syscall trap. (per 2) */ |
| 1154 | set_personality(1); |
| 1155 | break; |
| 1156 | case 0x91d02009: |
| 1157 | /* NetBSD/FreeBSD syscall trap. */ |
| 1158 | fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n"); |
| 1159 | return -1; |
| 1160 | case 0x91d02027: |
| 1161 | /* Solaris 2.x gettimeofday */ |
| 1162 | set_personality(1); |
| 1163 | break; |
| 1164 | default: |
Denys Vlasenko | b88f961 | 2011-08-21 18:03:23 +0200 | [diff] [blame] | 1165 | /* Check if this is the post-execve SIGTRAP. */ |
| 1166 | if (tcp->flags & TCB_WAITEXECVE) { |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1167 | tcp->flags &= ~TCB_WAITEXECVE; |
| 1168 | return 0; |
| 1169 | } |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 1170 | # if defined (SPARC64) |
| 1171 | fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, regs.tpc); |
| 1172 | # else |
| 1173 | fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, regs.pc); |
| 1174 | # endif |
| 1175 | return -1; |
| 1176 | } |
| 1177 | |
| 1178 | /* Extract the system call number from the registers. */ |
| 1179 | if (trap == 0x91d02027) |
| 1180 | scno = 156; |
| 1181 | else |
| 1182 | scno = regs.u_regs[U_REG_G1]; |
| 1183 | if (scno == 0) { |
| 1184 | scno = regs.u_regs[U_REG_O0]; |
| 1185 | memmove(®s.u_regs[U_REG_O0], ®s.u_regs[U_REG_O1], 7*sizeof(regs.u_regs[0])); |
| 1186 | } |
| 1187 | # elif defined(HPPA) |
| 1188 | if (upeek(tcp, PT_GR20, &scno) < 0) |
| 1189 | return -1; |
| 1190 | /* Check if this is the post-execve SIGTRAP. */ |
| 1191 | if (tcp->flags & TCB_WAITEXECVE) { |
| 1192 | tcp->flags &= ~TCB_WAITEXECVE; |
| 1193 | return 0; |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1194 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1195 | # elif defined(SH) |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1196 | /* |
| 1197 | * In the new syscall ABI, the system call number is in R3. |
| 1198 | */ |
| 1199 | if (upeek(tcp, 4*(REG_REG0+3), &scno) < 0) |
| 1200 | return -1; |
Wichert Akkerman | ccef637 | 2002-05-01 16:39:22 +0000 | [diff] [blame] | 1201 | |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1202 | if (scno < 0) { |
| 1203 | /* Odd as it may seem, a glibc bug has been known to cause |
| 1204 | glibc to issue bogus negative syscall numbers. So for |
| 1205 | our purposes, make strace print what it *should* have been */ |
| 1206 | long correct_scno = (scno & 0xff); |
| 1207 | if (debug) |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 1208 | fprintf(stderr, |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1209 | "Detected glibc bug: bogus system call" |
| 1210 | " number = %ld, correcting to %ld\n", |
| 1211 | scno, |
| 1212 | correct_scno); |
| 1213 | scno = correct_scno; |
| 1214 | } |
Wichert Akkerman | ccef637 | 2002-05-01 16:39:22 +0000 | [diff] [blame] | 1215 | |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 1216 | /* Check if this is the post-execve SIGTRAP. */ |
| 1217 | if (scno == 0 && tcp->flags & TCB_WAITEXECVE) { |
| 1218 | tcp->flags &= ~TCB_WAITEXECVE; |
| 1219 | return 0; |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1220 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1221 | # elif defined(SH64) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1222 | if (upeek(tcp, REG_SYSCALL, &scno) < 0) |
Roland McGrath | e1e584b | 2003-06-02 19:18:58 +0000 | [diff] [blame] | 1223 | return -1; |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1224 | scno &= 0xFFFF; |
Roland McGrath | e1e584b | 2003-06-02 19:18:58 +0000 | [diff] [blame] | 1225 | |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 1226 | /* Check if this is the post-execve SIGTRAP. */ |
| 1227 | if (tcp->flags & TCB_WAITEXECVE) { |
| 1228 | tcp->flags &= ~TCB_WAITEXECVE; |
| 1229 | return 0; |
Roland McGrath | e1e584b | 2003-06-02 19:18:58 +0000 | [diff] [blame] | 1230 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1231 | # elif defined(CRISV10) || defined(CRISV32) |
| 1232 | if (upeek(tcp, 4*PT_R9, &scno) < 0) |
| 1233 | return -1; |
Chris Metcalf | c8c6698 | 2009-12-28 10:00:15 -0500 | [diff] [blame] | 1234 | # elif defined(TILE) |
| 1235 | if (upeek(tcp, PTREGS_OFFSET_REG(10), &scno) < 0) |
| 1236 | return -1; |
| 1237 | |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 1238 | /* Check if this is the post-execve SIGTRAP. */ |
| 1239 | if (tcp->flags & TCB_WAITEXECVE) { |
| 1240 | tcp->flags &= ~TCB_WAITEXECVE; |
| 1241 | return 0; |
Chris Metcalf | c8c6698 | 2009-12-28 10:00:15 -0500 | [diff] [blame] | 1242 | } |
Edgar E. Iglesias | 939caba | 2010-07-06 14:21:07 +0200 | [diff] [blame] | 1243 | # elif defined(MICROBLAZE) |
| 1244 | if (upeek(tcp, 0, &scno) < 0) |
| 1245 | return -1; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1246 | # endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1247 | #endif /* LINUX */ |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1248 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1249 | #ifdef SUNOS4 |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1250 | if (upeek(tcp, uoff(u_arg[7]), &scno) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1251 | return -1; |
Wichert Akkerman | ccef637 | 2002-05-01 16:39:22 +0000 | [diff] [blame] | 1252 | #elif defined(SH) |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1253 | /* new syscall ABI returns result in R0 */ |
| 1254 | if (upeek(tcp, 4*REG_REG0, (long *)&r0) < 0) |
| 1255 | return -1; |
Roland McGrath | f5a4777 | 2003-06-26 22:40:42 +0000 | [diff] [blame] | 1256 | #elif defined(SH64) |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1257 | /* ABI defines result returned in r9 */ |
| 1258 | if (upeek(tcp, REG_GENERAL(9), (long *)&r9) < 0) |
| 1259 | return -1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1260 | #endif |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1261 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1262 | #ifdef USE_PROCFS |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1263 | # ifdef HAVE_PR_SYSCALL |
John Hughes | 2529971 | 2001-03-06 10:10:06 +0000 | [diff] [blame] | 1264 | scno = tcp->status.PR_SYSCALL; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1265 | # else |
| 1266 | # ifndef FREEBSD |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1267 | scno = tcp->status.PR_WHAT; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1268 | # else |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1269 | if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) { |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1270 | perror("pread"); |
| 1271 | return -1; |
| 1272 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1273 | switch (regs.r_eax) { |
| 1274 | case SYS_syscall: |
| 1275 | case SYS___syscall: |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1276 | pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int)); |
| 1277 | break; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1278 | default: |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1279 | scno = regs.r_eax; |
| 1280 | break; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1281 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1282 | # endif /* FREEBSD */ |
| 1283 | # endif /* !HAVE_PR_SYSCALL */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1284 | #endif /* USE_PROCFS */ |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1285 | |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 1286 | tcp->scno = scno; |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1287 | return 1; |
| 1288 | } |
| 1289 | |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1290 | /* Returns: |
| 1291 | * 0: "ignore this ptrace stop", bail out of trace_syscall() silently. |
| 1292 | * 1: ok, continue in trace_syscall(). |
| 1293 | * other: error, trace_syscall() should print error indicator |
| 1294 | * ("????" etc) and bail out. |
| 1295 | */ |
| 1296 | static int |
| 1297 | get_scno_on_sysexit(struct tcb *tcp) |
| 1298 | { |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1299 | #ifdef LINUX |
| 1300 | # if defined(S390) || defined(S390X) |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1301 | # elif defined (POWERPC) |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1302 | # elif defined(AVR32) |
| 1303 | /* |
| 1304 | * Read complete register set in one go. |
| 1305 | */ |
| 1306 | if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, ®s) < 0) |
| 1307 | return -1; |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1308 | # elif defined(BFIN) |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1309 | # elif defined (I386) |
Denys Vlasenko | 18beb98 | 2011-08-24 16:59:23 +0200 | [diff] [blame^] | 1310 | if (upeek(tcp, 4*EAX, &eax) < 0) |
| 1311 | return -1; |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1312 | # elif defined (X86_64) |
Denys Vlasenko | 18beb98 | 2011-08-24 16:59:23 +0200 | [diff] [blame^] | 1313 | if (upeek(tcp, 8*RAX, &rax) < 0) |
| 1314 | return -1; |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1315 | # elif defined(IA64) |
| 1316 | # define IA64_PSR_IS ((long)1 << 34) |
| 1317 | if (upeek(tcp, PT_CR_IPSR, &psr) >= 0) |
| 1318 | ia32 = (psr & IA64_PSR_IS) != 0; |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 1319 | if (upeek(tcp, PT_R8, &r8) < 0) |
| 1320 | return -1; |
| 1321 | if (upeek(tcp, PT_R10, &r10) < 0) |
| 1322 | return -1; |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1323 | # elif defined (ARM) |
| 1324 | /* |
| 1325 | * Read complete register set in one go. |
| 1326 | */ |
| 1327 | if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (void *)®s) == -1) |
| 1328 | return -1; |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1329 | # elif defined (M68K) |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1330 | # elif defined (LINUX_MIPSN32) |
| 1331 | unsigned long long regs[38]; |
| 1332 | |
| 1333 | if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0) |
| 1334 | return -1; |
| 1335 | a3 = regs[REG_A3]; |
| 1336 | r2 = regs[REG_V0]; |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1337 | # elif defined (MIPS) |
| 1338 | if (upeek(tcp, REG_A3, &a3) < 0) |
| 1339 | return -1; |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 1340 | if (upeek(tcp, REG_V0, &r2) < 0) |
| 1341 | return -1; |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1342 | # elif defined (ALPHA) |
| 1343 | if (upeek(tcp, REG_A3, &a3) < 0) |
| 1344 | return -1; |
Denys Vlasenko | 8cd1acd | 2011-08-24 16:52:57 +0200 | [diff] [blame] | 1345 | if (upeek(tcp, REG_R0, &r0) < 0) |
| 1346 | return -1; |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1347 | # elif defined (SPARC) || defined (SPARC64) |
| 1348 | /* Everything we need is in the current register set. */ |
| 1349 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) |
| 1350 | return -1; |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1351 | # elif defined(HPPA) |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1352 | # elif defined(SH) |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1353 | # elif defined(SH64) |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1354 | # elif defined(CRISV10) || defined(CRISV32) |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1355 | # elif defined(TILE) |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1356 | # elif defined(MICROBLAZE) |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1357 | # endif |
| 1358 | #endif /* LINUX */ |
| 1359 | |
| 1360 | #ifdef SUNOS4 |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1361 | #elif defined(SH) |
| 1362 | /* new syscall ABI returns result in R0 */ |
| 1363 | if (upeek(tcp, 4*REG_REG0, (long *)&r0) < 0) |
| 1364 | return -1; |
| 1365 | #elif defined(SH64) |
| 1366 | /* ABI defines result returned in r9 */ |
| 1367 | if (upeek(tcp, REG_GENERAL(9), (long *)&r9) < 0) |
| 1368 | return -1; |
| 1369 | #endif |
| 1370 | |
| 1371 | #ifdef USE_PROCFS |
Denys Vlasenko | 77a7459 | 2011-08-24 16:56:03 +0200 | [diff] [blame] | 1372 | # ifndef HAVE_PR_SYSCALL |
| 1373 | # ifdef FREEBSD |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1374 | if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) { |
| 1375 | perror("pread"); |
| 1376 | return -1; |
| 1377 | } |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1378 | # endif /* FREEBSD */ |
| 1379 | # endif /* !HAVE_PR_SYSCALL */ |
| 1380 | #endif /* USE_PROCFS */ |
| 1381 | |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 1382 | return 1; |
| 1383 | } |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1384 | |
Roland McGrath | 1735279 | 2005-06-07 23:21:26 +0000 | [diff] [blame] | 1385 | long |
Dmitry V. Levin | bd13645 | 2011-02-18 23:19:47 +0000 | [diff] [blame] | 1386 | known_scno(struct tcb *tcp) |
Roland McGrath | 1735279 | 2005-06-07 23:21:26 +0000 | [diff] [blame] | 1387 | { |
| 1388 | long scno = tcp->scno; |
Dmitry V. Levin | bd13645 | 2011-02-18 23:19:47 +0000 | [diff] [blame] | 1389 | #if SUPPORTED_PERSONALITIES > 1 |
Roland McGrath | 1735279 | 2005-06-07 23:21:26 +0000 | [diff] [blame] | 1390 | if (scno >= 0 && scno < nsyscalls && sysent[scno].native_scno != 0) |
| 1391 | scno = sysent[scno].native_scno; |
| 1392 | else |
Dmitry V. Levin | bd13645 | 2011-02-18 23:19:47 +0000 | [diff] [blame] | 1393 | #endif |
Roland McGrath | 1735279 | 2005-06-07 23:21:26 +0000 | [diff] [blame] | 1394 | scno += NR_SYSCALL_BASE; |
| 1395 | return scno; |
| 1396 | } |
| 1397 | |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1398 | /* Called in trace_syscall() at each syscall entry and exit. |
Denys Vlasenko | bc161ec | 2009-01-02 18:02:45 +0000 | [diff] [blame] | 1399 | * Returns: |
Denys Vlasenko | b88f961 | 2011-08-21 18:03:23 +0200 | [diff] [blame] | 1400 | * 0: "ignore this ptrace stop", bail out of trace_syscall() silently. |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 1401 | * 1: ok, continue in trace_syscall(). |
| 1402 | * other: error, trace_syscall() should print error indicator |
Denys Vlasenko | bc161ec | 2009-01-02 18:02:45 +0000 | [diff] [blame] | 1403 | * ("????" etc) and bail out. |
| 1404 | */ |
Roland McGrath | a4d4853 | 2005-06-08 20:45:28 +0000 | [diff] [blame] | 1405 | static int |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1406 | syscall_fixup(struct tcb *tcp) |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1407 | { |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1408 | #ifdef USE_PROCFS |
Roland McGrath | 1735279 | 2005-06-07 23:21:26 +0000 | [diff] [blame] | 1409 | int scno = known_scno(tcp); |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1410 | |
Denys Vlasenko | b88f961 | 2011-08-21 18:03:23 +0200 | [diff] [blame] | 1411 | if (entering(tcp)) { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1412 | if (tcp->status.PR_WHY != PR_SYSENTRY) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1413 | if ( |
| 1414 | scno == SYS_fork |
| 1415 | #ifdef SYS_vfork |
| 1416 | || scno == SYS_vfork |
| 1417 | #endif /* SYS_vfork */ |
John Hughes | 4e36a81 | 2001-04-18 15:11:51 +0000 | [diff] [blame] | 1418 | #ifdef SYS_fork1 |
| 1419 | || scno == SYS_fork1 |
| 1420 | #endif /* SYS_fork1 */ |
| 1421 | #ifdef SYS_forkall |
| 1422 | || scno == SYS_forkall |
| 1423 | #endif /* SYS_forkall */ |
| 1424 | #ifdef SYS_rfork1 |
| 1425 | || scno == SYS_rfork1 |
| 1426 | #endif /* SYS_fork1 */ |
| 1427 | #ifdef SYS_rforkall |
| 1428 | || scno == SYS_rforkall |
| 1429 | #endif /* SYS_rforkall */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1430 | ) { |
| 1431 | /* We are returning in the child, fake it. */ |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1432 | tcp->status.PR_WHY = PR_SYSENTRY; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1433 | trace_syscall(tcp); |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1434 | tcp->status.PR_WHY = PR_SYSEXIT; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1435 | } |
| 1436 | else { |
| 1437 | fprintf(stderr, "syscall: missing entry\n"); |
| 1438 | tcp->flags |= TCB_INSYSCALL; |
| 1439 | } |
| 1440 | } |
| 1441 | } |
| 1442 | else { |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 1443 | if (tcp->status.PR_WHY != PR_SYSEXIT) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1444 | fprintf(stderr, "syscall: missing exit\n"); |
| 1445 | tcp->flags &= ~TCB_INSYSCALL; |
| 1446 | } |
| 1447 | } |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1448 | #endif /* USE_PROCFS */ |
Denys Vlasenko | b88f961 | 2011-08-21 18:03:23 +0200 | [diff] [blame] | 1449 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1450 | #ifdef SUNOS4 |
Denys Vlasenko | b88f961 | 2011-08-21 18:03:23 +0200 | [diff] [blame] | 1451 | if (entering(tcp)) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1452 | if (scno == 0) { |
| 1453 | fprintf(stderr, "syscall: missing entry\n"); |
| 1454 | tcp->flags |= TCB_INSYSCALL; |
| 1455 | } |
| 1456 | } |
| 1457 | else { |
| 1458 | if (scno != 0) { |
| 1459 | if (debug) { |
| 1460 | /* |
| 1461 | * This happens when a signal handler |
| 1462 | * for a signal which interrupted a |
| 1463 | * a system call makes another system call. |
| 1464 | */ |
| 1465 | fprintf(stderr, "syscall: missing exit\n"); |
| 1466 | } |
| 1467 | tcp->flags &= ~TCB_INSYSCALL; |
| 1468 | } |
| 1469 | } |
| 1470 | #endif /* SUNOS4 */ |
Denys Vlasenko | b88f961 | 2011-08-21 18:03:23 +0200 | [diff] [blame] | 1471 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1472 | #ifdef LINUX |
Denys Vlasenko | b88f961 | 2011-08-21 18:03:23 +0200 | [diff] [blame] | 1473 | /* A common case of "not a syscall entry" is post-execve SIGTRAP */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1474 | #if defined (I386) |
Denys Vlasenko | 18beb98 | 2011-08-24 16:59:23 +0200 | [diff] [blame^] | 1475 | /* With PTRACE_O_TRACEEXEC, post-execve SIGTRAP is disabled. |
| 1476 | * Every extra ptrace call is expensive, so check EAX |
| 1477 | * on syscall entry only if PTRACE_O_TRACEEXEC is not enabled: |
| 1478 | */ |
| 1479 | if (entering(tcp) && !(ptrace_setoptions & PTRACE_O_TRACEEXEC)) { |
| 1480 | if (upeek(tcp, 4*EAX, &eax) < 0) |
| 1481 | return -1; |
| 1482 | if (eax != -ENOSYS) { |
| 1483 | if (debug) |
| 1484 | fprintf(stderr, "not a syscall entry (eax = %ld)\n", eax); |
| 1485 | return 0; |
| 1486 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1487 | } |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1488 | #elif defined (X86_64) |
Denys Vlasenko | 18beb98 | 2011-08-24 16:59:23 +0200 | [diff] [blame^] | 1489 | if (entering(tcp) && !(ptrace_setoptions & PTRACE_O_TRACEEXEC)) { |
| 1490 | if (upeek(tcp, 8*RAX, &rax) < 0) |
| 1491 | return -1; |
| 1492 | if (current_personality == 1) |
| 1493 | rax = (long int)(int)rax; /* sign extend from 32 bits */ |
| 1494 | if (rax != -ENOSYS && entering(tcp)) { |
| 1495 | if (debug) |
| 1496 | fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax); |
| 1497 | return 0; |
| 1498 | } |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 1499 | } |
Michal Ludvig | 10a88d0 | 2002-10-07 14:31:00 +0000 | [diff] [blame] | 1500 | #elif defined (S390) || defined (S390X) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1501 | if (upeek(tcp, PT_GPR2, &gpr2) < 0) |
Wichert Akkerman | 12f75d1 | 2000-02-14 16:23:40 +0000 | [diff] [blame] | 1502 | return -1; |
Michal Ludvig | 882eda8 | 2002-11-11 12:50:47 +0000 | [diff] [blame] | 1503 | if (syscall_mode != -ENOSYS) |
| 1504 | syscall_mode = tcp->scno; |
Denys Vlasenko | b88f961 | 2011-08-21 18:03:23 +0200 | [diff] [blame] | 1505 | if (gpr2 != syscall_mode && entering(tcp)) { |
Wichert Akkerman | 12f75d1 | 2000-02-14 16:23:40 +0000 | [diff] [blame] | 1506 | if (debug) |
Denys Vlasenko | b88f961 | 2011-08-21 18:03:23 +0200 | [diff] [blame] | 1507 | fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2); |
Wichert Akkerman | 12f75d1 | 2000-02-14 16:23:40 +0000 | [diff] [blame] | 1508 | return 0; |
| 1509 | } |
Roland McGrath | 96dc514 | 2003-01-20 10:23:04 +0000 | [diff] [blame] | 1510 | else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE)) |
| 1511 | == (TCB_INSYSCALL|TCB_WAITEXECVE)) |
| 1512 | && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) { |
| 1513 | /* |
| 1514 | * Fake a return value of zero. We leave the TCB_WAITEXECVE |
| 1515 | * flag set for the post-execve SIGTRAP to see and reset. |
| 1516 | */ |
| 1517 | gpr2 = 0; |
| 1518 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1519 | #elif defined (POWERPC) |
| 1520 | # define SO_MASK 0x10000000 |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1521 | if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1522 | return -1; |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1523 | if (upeek(tcp, sizeof(unsigned long)*PT_R3, &result) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1524 | return -1; |
| 1525 | if (flags & SO_MASK) |
| 1526 | result = -result; |
| 1527 | #elif defined (M68K) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1528 | if (upeek(tcp, 4*PT_D0, &d0) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1529 | return -1; |
Denys Vlasenko | b88f961 | 2011-08-21 18:03:23 +0200 | [diff] [blame] | 1530 | if (d0 != -ENOSYS && entering(tcp)) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1531 | if (debug) |
Denys Vlasenko | b88f961 | 2011-08-21 18:03:23 +0200 | [diff] [blame] | 1532 | fprintf(stderr, "not a syscall entry (d0 = %ld)\n", d0); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1533 | return 0; |
| 1534 | } |
| 1535 | #elif defined (ARM) |
Roland McGrath | 0f87c49 | 2003-06-03 23:29:04 +0000 | [diff] [blame] | 1536 | /* |
| 1537 | * Nothing required |
| 1538 | */ |
Dmitry V. Levin | 87ea1f4 | 2008-11-10 22:21:41 +0000 | [diff] [blame] | 1539 | #elif defined(BFIN) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1540 | if (upeek(tcp, PT_R0, &r0) < 0) |
Dmitry V. Levin | 87ea1f4 | 2008-11-10 22:21:41 +0000 | [diff] [blame] | 1541 | return -1; |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1542 | #elif defined (HPPA) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1543 | if (upeek(tcp, PT_GR28, &r28) < 0) |
Wichert Akkerman | c1652e2 | 2001-03-27 12:17:16 +0000 | [diff] [blame] | 1544 | return -1; |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 1545 | #elif defined(IA64) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1546 | if (upeek(tcp, PT_R10, &r10) < 0) |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 1547 | return -1; |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1548 | if (upeek(tcp, PT_R8, &r8) < 0) |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 1549 | return -1; |
Denys Vlasenko | b88f961 | 2011-08-21 18:03:23 +0200 | [diff] [blame] | 1550 | if (ia32 && r8 != -ENOSYS && entering(tcp)) { |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 1551 | if (debug) |
Denys Vlasenko | b88f961 | 2011-08-21 18:03:23 +0200 | [diff] [blame] | 1552 | fprintf(stderr, "not a syscall entry (r8 = %ld)\n", r8); |
Wichert Akkerman | 7b3346b | 2001-10-09 23:47:38 +0000 | [diff] [blame] | 1553 | return 0; |
| 1554 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1555 | #elif defined(CRISV10) || defined(CRISV32) |
Edgar E. Iglesias | eeb9ce3 | 2009-10-05 14:41:02 +0000 | [diff] [blame] | 1556 | if (upeek(tcp, 4*PT_R10, &r10) < 0) |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1557 | return -1; |
Denys Vlasenko | b88f961 | 2011-08-21 18:03:23 +0200 | [diff] [blame] | 1558 | if (r10 != -ENOSYS && entering(tcp)) { |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1559 | if (debug) |
Denys Vlasenko | b88f961 | 2011-08-21 18:03:23 +0200 | [diff] [blame] | 1560 | fprintf(stderr, "not a syscall entry (r10 = %ld)\n", r10); |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1561 | return 0; |
| 1562 | } |
Edgar E. Iglesias | 939caba | 2010-07-06 14:21:07 +0200 | [diff] [blame] | 1563 | #elif defined(MICROBLAZE) |
| 1564 | if (upeek(tcp, 3 * 4, &r3) < 0) |
| 1565 | return -1; |
Denys Vlasenko | b88f961 | 2011-08-21 18:03:23 +0200 | [diff] [blame] | 1566 | if (r3 != -ENOSYS && entering(tcp)) { |
Edgar E. Iglesias | 939caba | 2010-07-06 14:21:07 +0200 | [diff] [blame] | 1567 | if (debug) |
Denys Vlasenko | b88f961 | 2011-08-21 18:03:23 +0200 | [diff] [blame] | 1568 | fprintf(stderr, "not a syscall entry (r3 = %ld)\n", r3); |
Edgar E. Iglesias | 939caba | 2010-07-06 14:21:07 +0200 | [diff] [blame] | 1569 | return 0; |
| 1570 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1571 | #endif |
| 1572 | #endif /* LINUX */ |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1573 | return 1; |
| 1574 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1575 | |
Roland McGrath | c1e4592 | 2008-05-27 23:18:29 +0000 | [diff] [blame] | 1576 | #ifdef LINUX |
| 1577 | /* |
| 1578 | * Check the syscall return value register value for whether it is |
| 1579 | * a negated errno code indicating an error, or a success return value. |
| 1580 | */ |
| 1581 | static inline int |
| 1582 | is_negated_errno(unsigned long int val) |
| 1583 | { |
| 1584 | unsigned long int max = -(long int) nerrnos; |
Denys Vlasenko | a794974 | 2011-08-21 17:26:55 +0200 | [diff] [blame] | 1585 | # if SUPPORTED_PERSONALITIES > 1 |
Roland McGrath | c1e4592 | 2008-05-27 23:18:29 +0000 | [diff] [blame] | 1586 | if (personality_wordsize[current_personality] < sizeof(val)) { |
| 1587 | val = (unsigned int) val; |
| 1588 | max = (unsigned int) max; |
| 1589 | } |
Denys Vlasenko | a794974 | 2011-08-21 17:26:55 +0200 | [diff] [blame] | 1590 | # endif |
Roland McGrath | c1e4592 | 2008-05-27 23:18:29 +0000 | [diff] [blame] | 1591 | return val > max; |
| 1592 | } |
| 1593 | #endif |
| 1594 | |
Roland McGrath | a4d4853 | 2005-06-08 20:45:28 +0000 | [diff] [blame] | 1595 | static int |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1596 | get_error(struct tcb *tcp) |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1597 | { |
| 1598 | int u_error = 0; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1599 | #ifdef LINUX |
Dmitry V. Levin | 50a218d | 2011-01-18 17:36:20 +0000 | [diff] [blame] | 1600 | int check_errno = 1; |
| 1601 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && |
| 1602 | sysent[tcp->scno].sys_flags & SYSCALL_NEVER_FAILS) { |
| 1603 | check_errno = 0; |
| 1604 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1605 | # if defined(S390) || defined(S390X) |
Dmitry V. Levin | 50a218d | 2011-01-18 17:36:20 +0000 | [diff] [blame] | 1606 | if (check_errno && is_negated_errno(gpr2)) { |
Roland McGrath | c1e4592 | 2008-05-27 23:18:29 +0000 | [diff] [blame] | 1607 | tcp->u_rval = -1; |
| 1608 | u_error = -gpr2; |
| 1609 | } |
| 1610 | else { |
| 1611 | tcp->u_rval = gpr2; |
| 1612 | u_error = 0; |
| 1613 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1614 | # elif defined(I386) |
Dmitry V. Levin | 50a218d | 2011-01-18 17:36:20 +0000 | [diff] [blame] | 1615 | if (check_errno && is_negated_errno(eax)) { |
Roland McGrath | c1e4592 | 2008-05-27 23:18:29 +0000 | [diff] [blame] | 1616 | tcp->u_rval = -1; |
| 1617 | u_error = -eax; |
| 1618 | } |
| 1619 | else { |
| 1620 | tcp->u_rval = eax; |
| 1621 | u_error = 0; |
| 1622 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1623 | # elif defined(X86_64) |
Dmitry V. Levin | 50a218d | 2011-01-18 17:36:20 +0000 | [diff] [blame] | 1624 | if (check_errno && is_negated_errno(rax)) { |
Roland McGrath | c1e4592 | 2008-05-27 23:18:29 +0000 | [diff] [blame] | 1625 | tcp->u_rval = -1; |
| 1626 | u_error = -rax; |
| 1627 | } |
| 1628 | else { |
| 1629 | tcp->u_rval = rax; |
| 1630 | u_error = 0; |
| 1631 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1632 | # elif defined(IA64) |
Roland McGrath | c1e4592 | 2008-05-27 23:18:29 +0000 | [diff] [blame] | 1633 | if (ia32) { |
| 1634 | int err; |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1635 | |
Roland McGrath | c1e4592 | 2008-05-27 23:18:29 +0000 | [diff] [blame] | 1636 | err = (int)r8; |
Dmitry V. Levin | 50a218d | 2011-01-18 17:36:20 +0000 | [diff] [blame] | 1637 | if (check_errno && is_negated_errno(err)) { |
Roland McGrath | c1e4592 | 2008-05-27 23:18:29 +0000 | [diff] [blame] | 1638 | tcp->u_rval = -1; |
| 1639 | u_error = -err; |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 1640 | } |
Roland McGrath | c1e4592 | 2008-05-27 23:18:29 +0000 | [diff] [blame] | 1641 | else { |
| 1642 | tcp->u_rval = err; |
| 1643 | u_error = 0; |
| 1644 | } |
| 1645 | } else { |
Dmitry V. Levin | 50a218d | 2011-01-18 17:36:20 +0000 | [diff] [blame] | 1646 | if (check_errno && r10) { |
Roland McGrath | c1e4592 | 2008-05-27 23:18:29 +0000 | [diff] [blame] | 1647 | tcp->u_rval = -1; |
| 1648 | u_error = r8; |
| 1649 | } else { |
| 1650 | tcp->u_rval = r8; |
| 1651 | u_error = 0; |
| 1652 | } |
| 1653 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1654 | # elif defined(MIPS) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1655 | if (check_errno && a3) { |
| 1656 | tcp->u_rval = -1; |
| 1657 | u_error = r2; |
| 1658 | } else { |
| 1659 | tcp->u_rval = r2; |
| 1660 | u_error = 0; |
| 1661 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1662 | # elif defined(POWERPC) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1663 | if (check_errno && is_negated_errno(result)) { |
| 1664 | tcp->u_rval = -1; |
| 1665 | u_error = -result; |
| 1666 | } |
| 1667 | else { |
| 1668 | tcp->u_rval = result; |
| 1669 | u_error = 0; |
| 1670 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1671 | # elif defined(M68K) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1672 | if (check_errno && is_negated_errno(d0)) { |
| 1673 | tcp->u_rval = -1; |
| 1674 | u_error = -d0; |
| 1675 | } |
| 1676 | else { |
| 1677 | tcp->u_rval = d0; |
| 1678 | u_error = 0; |
| 1679 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1680 | # elif defined(ARM) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1681 | if (check_errno && is_negated_errno(regs.ARM_r0)) { |
| 1682 | tcp->u_rval = -1; |
| 1683 | u_error = -regs.ARM_r0; |
| 1684 | } |
| 1685 | else { |
| 1686 | tcp->u_rval = regs.ARM_r0; |
| 1687 | u_error = 0; |
| 1688 | } |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 1689 | # elif defined(AVR32) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1690 | if (check_errno && regs.r12 && (unsigned) -regs.r12 < nerrnos) { |
| 1691 | tcp->u_rval = -1; |
| 1692 | u_error = -regs.r12; |
| 1693 | } |
| 1694 | else { |
| 1695 | tcp->u_rval = regs.r12; |
| 1696 | u_error = 0; |
| 1697 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1698 | # elif defined(BFIN) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1699 | if (check_errno && is_negated_errno(r0)) { |
| 1700 | tcp->u_rval = -1; |
| 1701 | u_error = -r0; |
| 1702 | } else { |
| 1703 | tcp->u_rval = r0; |
| 1704 | u_error = 0; |
| 1705 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1706 | # elif defined(ALPHA) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1707 | if (check_errno && a3) { |
| 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(SPARC) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1716 | if (check_errno && regs.psr & PSR_C) { |
| 1717 | tcp->u_rval = -1; |
| 1718 | u_error = regs.u_regs[U_REG_O0]; |
| 1719 | } |
| 1720 | else { |
| 1721 | tcp->u_rval = regs.u_regs[U_REG_O0]; |
| 1722 | u_error = 0; |
| 1723 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1724 | # elif defined(SPARC64) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1725 | if (check_errno && regs.tstate & 0x1100000000UL) { |
| 1726 | tcp->u_rval = -1; |
| 1727 | u_error = regs.u_regs[U_REG_O0]; |
| 1728 | } |
| 1729 | else { |
| 1730 | tcp->u_rval = regs.u_regs[U_REG_O0]; |
| 1731 | u_error = 0; |
| 1732 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1733 | # elif defined(HPPA) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1734 | if (check_errno && is_negated_errno(r28)) { |
| 1735 | tcp->u_rval = -1; |
| 1736 | u_error = -r28; |
| 1737 | } |
| 1738 | else { |
| 1739 | tcp->u_rval = r28; |
| 1740 | u_error = 0; |
| 1741 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1742 | # elif defined(SH) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1743 | /* interpret R0 as return value or error number */ |
| 1744 | if (check_errno && is_negated_errno(r0)) { |
| 1745 | tcp->u_rval = -1; |
| 1746 | u_error = -r0; |
| 1747 | } |
| 1748 | else { |
| 1749 | tcp->u_rval = r0; |
| 1750 | u_error = 0; |
| 1751 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1752 | # elif defined(SH64) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1753 | /* interpret result as return value or error number */ |
| 1754 | if (check_errno && is_negated_errno(r9)) { |
| 1755 | tcp->u_rval = -1; |
| 1756 | u_error = -r9; |
| 1757 | } |
| 1758 | else { |
| 1759 | tcp->u_rval = r9; |
| 1760 | u_error = 0; |
| 1761 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1762 | # elif defined(CRISV10) || defined(CRISV32) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1763 | if (check_errno && r10 && (unsigned) -r10 < nerrnos) { |
| 1764 | tcp->u_rval = -1; |
| 1765 | u_error = -r10; |
| 1766 | } |
| 1767 | else { |
| 1768 | tcp->u_rval = r10; |
| 1769 | u_error = 0; |
| 1770 | } |
Chris Metcalf | c8c6698 | 2009-12-28 10:00:15 -0500 | [diff] [blame] | 1771 | # elif defined(TILE) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1772 | long rval; |
| 1773 | /* interpret result as return value or error number */ |
| 1774 | if (upeek(tcp, PTREGS_OFFSET_REG(0), &rval) < 0) |
| 1775 | return -1; |
| 1776 | if (check_errno && rval < 0 && rval > -nerrnos) { |
| 1777 | tcp->u_rval = -1; |
| 1778 | u_error = -rval; |
| 1779 | } |
| 1780 | else { |
| 1781 | tcp->u_rval = rval; |
| 1782 | u_error = 0; |
| 1783 | } |
Edgar E. Iglesias | 939caba | 2010-07-06 14:21:07 +0200 | [diff] [blame] | 1784 | # elif defined(MICROBLAZE) |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1785 | /* interpret result as return value or error number */ |
| 1786 | if (check_errno && is_negated_errno(r3)) { |
| 1787 | tcp->u_rval = -1; |
| 1788 | u_error = -r3; |
| 1789 | } |
| 1790 | else { |
| 1791 | tcp->u_rval = r3; |
| 1792 | u_error = 0; |
| 1793 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1794 | # endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1795 | #endif /* LINUX */ |
| 1796 | #ifdef SUNOS4 |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1797 | /* get error code from user struct */ |
| 1798 | if (upeek(tcp, uoff(u_error), &u_error) < 0) |
| 1799 | return -1; |
| 1800 | u_error >>= 24; /* u_error is a char */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1801 | |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1802 | /* get system call return value */ |
| 1803 | if (upeek(tcp, uoff(u_rval1), &tcp->u_rval) < 0) |
| 1804 | return -1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1805 | #endif /* SUNOS4 */ |
| 1806 | #ifdef SVR4 |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1807 | # ifdef SPARC |
| 1808 | /* Judicious guessing goes a long way. */ |
| 1809 | if (tcp->status.pr_reg[R_PSR] & 0x100000) { |
| 1810 | tcp->u_rval = -1; |
| 1811 | u_error = tcp->status.pr_reg[R_O0]; |
| 1812 | } |
| 1813 | else { |
| 1814 | tcp->u_rval = tcp->status.pr_reg[R_O0]; |
| 1815 | u_error = 0; |
| 1816 | } |
| 1817 | # endif /* SPARC */ |
| 1818 | # ifdef I386 |
| 1819 | /* Wanna know how to kill an hour single-stepping? */ |
| 1820 | if (tcp->status.PR_REG[EFL] & 0x1) { |
| 1821 | tcp->u_rval = -1; |
| 1822 | u_error = tcp->status.PR_REG[EAX]; |
| 1823 | } |
| 1824 | else { |
| 1825 | tcp->u_rval = tcp->status.PR_REG[EAX]; |
| 1826 | # ifdef HAVE_LONG_LONG |
| 1827 | tcp->u_lrval = |
| 1828 | ((unsigned long long) tcp->status.PR_REG[EDX] << 32) + |
| 1829 | tcp->status.PR_REG[EAX]; |
| 1830 | # endif |
| 1831 | u_error = 0; |
| 1832 | } |
| 1833 | # endif /* I386 */ |
| 1834 | # ifdef X86_64 |
| 1835 | /* Wanna know how to kill an hour single-stepping? */ |
| 1836 | if (tcp->status.PR_REG[EFLAGS] & 0x1) { |
| 1837 | tcp->u_rval = -1; |
| 1838 | u_error = tcp->status.PR_REG[RAX]; |
| 1839 | } |
| 1840 | else { |
| 1841 | tcp->u_rval = tcp->status.PR_REG[RAX]; |
| 1842 | u_error = 0; |
| 1843 | } |
| 1844 | # endif /* X86_64 */ |
| 1845 | # ifdef MIPS |
| 1846 | if (tcp->status.pr_reg[CTX_A3]) { |
| 1847 | tcp->u_rval = -1; |
| 1848 | u_error = tcp->status.pr_reg[CTX_V0]; |
| 1849 | } |
| 1850 | else { |
| 1851 | tcp->u_rval = tcp->status.pr_reg[CTX_V0]; |
| 1852 | u_error = 0; |
| 1853 | } |
| 1854 | # endif /* MIPS */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1855 | #endif /* SVR4 */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 1856 | #ifdef FREEBSD |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 1857 | if (regs.r_eflags & PSL_C) { |
| 1858 | tcp->u_rval = -1; |
| 1859 | u_error = regs.r_eax; |
| 1860 | } else { |
| 1861 | tcp->u_rval = regs.r_eax; |
| 1862 | tcp->u_lrval = |
| 1863 | ((unsigned long long) regs.r_edx << 32) + regs.r_eax; |
| 1864 | u_error = 0; |
| 1865 | } |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 1866 | #endif /* FREEBSD */ |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 1867 | tcp->u_error = u_error; |
| 1868 | return 1; |
| 1869 | } |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 1870 | |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 1871 | int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 1872 | force_result(struct tcb *tcp, int error, long rval) |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 1873 | { |
| 1874 | #ifdef LINUX |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1875 | # if defined(S390) || defined(S390X) |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 1876 | gpr2 = error ? -error : rval; |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 1877 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0) |
| 1878 | return -1; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1879 | # elif defined(I386) |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 1880 | eax = error ? -error : rval; |
| 1881 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0) |
| 1882 | return -1; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1883 | # elif defined(X86_64) |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 1884 | rax = error ? -error : rval; |
Roland McGrath | 998fac7 | 2004-06-23 01:40:45 +0000 | [diff] [blame] | 1885 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 8), rax) < 0) |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 1886 | return -1; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1887 | # elif defined(IA64) |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 1888 | if (ia32) { |
| 1889 | r8 = error ? -error : rval; |
| 1890 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0) |
| 1891 | return -1; |
| 1892 | } |
| 1893 | else { |
| 1894 | if (error) { |
| 1895 | r8 = error; |
| 1896 | r10 = -1; |
| 1897 | } |
| 1898 | else { |
| 1899 | r8 = rval; |
| 1900 | r10 = 0; |
| 1901 | } |
| 1902 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 || |
| 1903 | ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0) |
| 1904 | return -1; |
| 1905 | } |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1906 | # elif defined(BFIN) |
Dmitry V. Levin | 87ea1f4 | 2008-11-10 22:21:41 +0000 | [diff] [blame] | 1907 | r0 = error ? -error : rval; |
| 1908 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_R0, r0) < 0) |
| 1909 | return -1; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1910 | # elif defined(MIPS) |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 1911 | if (error) { |
| 1912 | r2 = error; |
| 1913 | a3 = -1; |
| 1914 | } |
| 1915 | else { |
| 1916 | r2 = rval; |
| 1917 | a3 = 0; |
| 1918 | } |
Roland McGrath | 542c2c6 | 2008-05-20 01:11:56 +0000 | [diff] [blame] | 1919 | /* 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] | 1920 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 || |
| 1921 | ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0) |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 1922 | return -1; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1923 | # elif defined(POWERPC) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 1924 | if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0) |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 1925 | return -1; |
| 1926 | if (error) { |
| 1927 | flags |= SO_MASK; |
| 1928 | result = error; |
| 1929 | } |
| 1930 | else { |
| 1931 | flags &= ~SO_MASK; |
| 1932 | result = rval; |
| 1933 | } |
Roland McGrath | eb28535 | 2003-01-14 09:59:00 +0000 | [diff] [blame] | 1934 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 || |
| 1935 | 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] | 1936 | return -1; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1937 | # elif defined(M68K) |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 1938 | d0 = error ? -error : rval; |
| 1939 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0) |
| 1940 | return -1; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1941 | # elif defined(ARM) |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1942 | regs.ARM_r0 = error ? -error : rval; |
| 1943 | 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] | 1944 | return -1; |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 1945 | # elif defined(AVR32) |
| 1946 | regs.r12 = error ? -error : rval; |
| 1947 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)REG_R12, regs.r12) < 0) |
| 1948 | return -1; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1949 | # elif defined(ALPHA) |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 1950 | if (error) { |
| 1951 | a3 = -1; |
| 1952 | r0 = error; |
| 1953 | } |
| 1954 | else { |
| 1955 | a3 = 0; |
| 1956 | r0 = rval; |
| 1957 | } |
| 1958 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 || |
| 1959 | ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0) |
| 1960 | return -1; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1961 | # elif defined(SPARC) |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 1962 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) |
| 1963 | return -1; |
| 1964 | if (error) { |
Mike Frysinger | 8566c50 | 2009-10-12 11:05:14 -0400 | [diff] [blame] | 1965 | regs.psr |= PSR_C; |
| 1966 | regs.u_regs[U_REG_O0] = error; |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 1967 | } |
| 1968 | else { |
Mike Frysinger | 8566c50 | 2009-10-12 11:05:14 -0400 | [diff] [blame] | 1969 | regs.psr &= ~PSR_C; |
| 1970 | regs.u_regs[U_REG_O0] = rval; |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 1971 | } |
| 1972 | if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0) |
| 1973 | return -1; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1974 | # elif defined(SPARC64) |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 1975 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) |
| 1976 | return -1; |
| 1977 | if (error) { |
Mike Frysinger | 8566c50 | 2009-10-12 11:05:14 -0400 | [diff] [blame] | 1978 | regs.tstate |= 0x1100000000UL; |
| 1979 | regs.u_regs[U_REG_O0] = error; |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 1980 | } |
| 1981 | else { |
Mike Frysinger | 8566c50 | 2009-10-12 11:05:14 -0400 | [diff] [blame] | 1982 | regs.tstate &= ~0x1100000000UL; |
| 1983 | regs.u_regs[U_REG_O0] = rval; |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 1984 | } |
| 1985 | if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)®s, 0) < 0) |
| 1986 | return -1; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1987 | # elif defined(HPPA) |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 1988 | r28 = error ? -error : rval; |
| 1989 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0) |
| 1990 | return -1; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1991 | # elif defined(SH) |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 1992 | r0 = error ? -error : rval; |
| 1993 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0) |
| 1994 | return -1; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1995 | # elif defined(SH64) |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 1996 | r9 = error ? -error : rval; |
Roland McGrath | e1e584b | 2003-06-02 19:18:58 +0000 | [diff] [blame] | 1997 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)REG_GENERAL(9), r9) < 0) |
| 1998 | return -1; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 1999 | # endif |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 2000 | #endif /* LINUX */ |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 2001 | |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 2002 | #ifdef SUNOS4 |
Roland McGrath | eb9e2e8 | 2009-06-02 16:49:22 -0700 | [diff] [blame] | 2003 | if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error), |
| 2004 | error << 24) < 0 || |
| 2005 | ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0) |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 2006 | return -1; |
| 2007 | #endif /* SUNOS4 */ |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 2008 | |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 2009 | #ifdef SVR4 |
| 2010 | /* XXX no clue */ |
| 2011 | return -1; |
| 2012 | #endif /* SVR4 */ |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 2013 | |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 2014 | #ifdef FREEBSD |
| 2015 | if (pread(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) { |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 2016 | perror("pread"); |
| 2017 | return -1; |
| 2018 | } |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 2019 | if (error) { |
| 2020 | regs.r_eflags |= PSL_C; |
| 2021 | regs.r_eax = error; |
| 2022 | } |
| 2023 | else { |
| 2024 | regs.r_eflags &= ~PSL_C; |
| 2025 | regs.r_eax = rval; |
| 2026 | } |
| 2027 | if (pwrite(tcp->pfd_reg, ®s, sizeof(regs), 0) < 0) { |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 2028 | perror("pwrite"); |
| 2029 | return -1; |
| 2030 | } |
Roland McGrath | b69f81b | 2002-12-21 23:25:18 +0000 | [diff] [blame] | 2031 | #endif /* FREEBSD */ |
| 2032 | |
| 2033 | /* All branches reach here on success (only). */ |
| 2034 | tcp->u_error = error; |
| 2035 | tcp->u_rval = rval; |
| 2036 | return 0; |
| 2037 | } |
| 2038 | |
Roland McGrath | a4d4853 | 2005-06-08 20:45:28 +0000 | [diff] [blame] | 2039 | static int |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 2040 | syscall_enter(struct tcb *tcp) |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 2041 | { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2042 | #ifdef LINUX |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2043 | int i, nargs; |
| 2044 | |
Denys Vlasenko | afc6403 | 2011-08-23 13:29:01 +0200 | [diff] [blame] | 2045 | if (tcp->scno >= 0 && tcp->scno < nsyscalls) |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2046 | nargs = tcp->u_nargs = sysent[tcp->scno].nargs; |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2047 | else |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2048 | nargs = tcp->u_nargs = MAX_ARGS; |
| 2049 | |
| 2050 | # if defined(S390) || defined(S390X) |
| 2051 | for (i = 0; i < nargs; ++i) |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2052 | if (upeek(tcp, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0) |
| 2053 | return -1; |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2054 | # elif defined(ALPHA) |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2055 | for (i = 0; i < nargs; ++i) |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2056 | if (upeek(tcp, REG_A0+i, &tcp->u_arg[i]) < 0) |
| 2057 | return -1; |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2058 | # elif defined(IA64) |
| 2059 | if (!ia32) { |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2060 | unsigned long *out0, cfm, sof, sol; |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2061 | long rbs_end; |
| 2062 | /* be backwards compatible with kernel < 2.4.4... */ |
| 2063 | # ifndef PT_RBS_END |
| 2064 | # define PT_RBS_END PT_AR_BSP |
| 2065 | # endif |
Wichert Akkerman | 8b1b40c | 2000-02-03 21:58:30 +0000 | [diff] [blame] | 2066 | |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2067 | if (upeek(tcp, PT_RBS_END, &rbs_end) < 0) |
| 2068 | return -1; |
| 2069 | if (upeek(tcp, PT_CFM, (long *) &cfm) < 0) |
Roland McGrath | 542c2c6 | 2008-05-20 01:11:56 +0000 | [diff] [blame] | 2070 | return -1; |
| 2071 | |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2072 | sof = (cfm >> 0) & 0x7f; |
| 2073 | sol = (cfm >> 7) & 0x7f; |
| 2074 | out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol); |
| 2075 | |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2076 | for (i = 0; i < nargs; ++i) { |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2077 | if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i), |
| 2078 | sizeof(long), (char *) &tcp->u_arg[i]) < 0) |
| 2079 | return -1; |
| 2080 | } |
| 2081 | } else { |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2082 | static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */, |
| 2083 | PT_R9 /* ECX = out1 */, |
| 2084 | PT_R10 /* EDX = out2 */, |
| 2085 | PT_R14 /* ESI = out3 */, |
| 2086 | PT_R15 /* EDI = out4 */, |
| 2087 | PT_R13 /* EBP = out5 */}; |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2088 | |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2089 | for (i = 0; i < nargs; ++i) { |
| 2090 | if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0) |
| 2091 | return -1; |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2092 | /* truncate away IVE sign-extension */ |
| 2093 | tcp->u_arg[i] &= 0xffffffff; |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2094 | } |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2095 | } |
| 2096 | # elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64) |
| 2097 | /* N32 and N64 both use up to six registers. */ |
| 2098 | unsigned long long regs[38]; |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2099 | |
| 2100 | if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) ®s) < 0) |
| 2101 | return -1; |
| 2102 | |
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 | tcp->u_arg[i] = regs[REG_A0 + i]; |
| 2105 | # if defined(LINUX_MIPSN32) |
| 2106 | tcp->ext_arg[i] = regs[REG_A0 + i]; |
| 2107 | # endif |
| 2108 | } |
| 2109 | # elif defined(MIPS) |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2110 | if (nargs > 4) { |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2111 | long sp; |
| 2112 | |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2113 | if (upeek(tcp, REG_SP, &sp) < 0) |
| 2114 | return -1; |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2115 | for (i = 0; i < 4; ++i) |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2116 | if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0) |
| 2117 | return -1; |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2118 | umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]), |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2119 | (char *)(tcp->u_arg + 4)); |
| 2120 | } else { |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [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, REG_A0 + i, &tcp->u_arg[i]) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2123 | return -1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2124 | } |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2125 | # elif defined(POWERPC) |
| 2126 | # ifndef PT_ORIG_R3 |
| 2127 | # define PT_ORIG_R3 34 |
| 2128 | # endif |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2129 | for (i = 0; i < nargs; ++i) { |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2130 | if (upeek(tcp, (i==0) ? |
| 2131 | (sizeof(unsigned long) * PT_ORIG_R3) : |
| 2132 | ((i+PT_R3) * sizeof(unsigned long)), |
| 2133 | &tcp->u_arg[i]) < 0) |
| 2134 | return -1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2135 | } |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2136 | # elif defined(SPARC) || defined(SPARC64) |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2137 | for (i = 0; i < nargs; ++i) |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2138 | tcp->u_arg[i] = regs.u_regs[U_REG_O0 + i]; |
| 2139 | # elif defined(HPPA) |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2140 | for (i = 0; i < nargs; ++i) |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2141 | if (upeek(tcp, PT_GR26-4*i, &tcp->u_arg[i]) < 0) |
| 2142 | return -1; |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2143 | # elif defined(ARM) |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2144 | for (i = 0; i < nargs; ++i) |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2145 | tcp->u_arg[i] = regs.uregs[i]; |
| 2146 | # elif defined(AVR32) |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2147 | static const unsigned long *argregp[MAX_ARGS] = { ®s.r12, |
| 2148 | ®s.r11, |
| 2149 | ®s.r10, |
| 2150 | ®s.r9, |
| 2151 | ®s.r5, |
| 2152 | ®s.r3 }; |
| 2153 | for (i = 0; i < nargs; ++i) |
| 2154 | tcp->u_arg[i] = *argregp[i]; |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2155 | # elif defined(BFIN) |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2156 | 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] | 2157 | |
Denys Vlasenko | 4b887a5 | 2011-08-23 13:32:38 +0200 | [diff] [blame] | 2158 | for (i = 0; i < nargs; ++i) |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2159 | if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0) |
| 2160 | return -1; |
| 2161 | # elif defined(SH) |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2162 | static const int syscall_regs[MAX_ARGS] = { |
| 2163 | 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6), |
| 2164 | 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1) |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2165 | }; |
| 2166 | |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2167 | for (i = 0; i < nargs; ++i) |
Denys Vlasenko | 0b6c73c | 2011-06-23 22:22:34 +0200 | [diff] [blame] | 2168 | if (upeek(tcp, syscall_regs[i], &tcp->u_arg[i]) < 0) |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2169 | return -1; |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2170 | # elif defined(SH64) |
| 2171 | int i; |
| 2172 | /* Registers used by SH5 Linux system calls for parameters */ |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2173 | 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] | 2174 | |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2175 | for (i = 0; i < nargs; ++i) |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2176 | if (upeek(tcp, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0) |
| 2177 | return -1; |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2178 | # elif defined(X86_64) |
Denys Vlasenko | 0b6c73c | 2011-06-23 22:22:34 +0200 | [diff] [blame] | 2179 | static const int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = { |
| 2180 | { 8 * RDI, 8 * RSI, 8 * RDX, 8 * R10, 8 * R8 , 8 * R9 }, /* x86-64 ABI */ |
| 2181 | { 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] | 2182 | }; |
| 2183 | |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2184 | for (i = 0; i < nargs; ++i) |
Denys Vlasenko | 0b6c73c | 2011-06-23 22:22:34 +0200 | [diff] [blame] | 2185 | if (upeek(tcp, argreg[current_personality][i], &tcp->u_arg[i]) < 0) |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2186 | return -1; |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2187 | # elif defined(MICROBLAZE) |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2188 | for (i = 0; i < nargs; ++i) |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2189 | if (upeek(tcp, (5 + i) * 4, &tcp->u_arg[i]) < 0) |
| 2190 | return -1; |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2191 | # elif defined(CRISV10) || defined(CRISV32) |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2192 | static const int crisregs[MAX_ARGS] = { |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2193 | 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12, |
Denys Vlasenko | 0b6c73c | 2011-06-23 22:22:34 +0200 | [diff] [blame] | 2194 | 4*PT_R13 , 4*PT_MOF, 4*PT_SRP |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2195 | }; |
Roland McGrath | e1e584b | 2003-06-02 19:18:58 +0000 | [diff] [blame] | 2196 | |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2197 | for (i = 0; i < nargs; ++i) |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2198 | if (upeek(tcp, crisregs[i], &tcp->u_arg[i]) < 0) |
| 2199 | return -1; |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2200 | # elif defined(TILE) |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2201 | for (i = 0; i < nargs; ++i) |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2202 | if (upeek(tcp, PTREGS_OFFSET_REG(i), &tcp->u_arg[i]) < 0) |
| 2203 | return -1; |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2204 | # elif defined(M68K) |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2205 | for (i = 0; i < nargs; ++i) |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2206 | if (upeek(tcp, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0) |
| 2207 | return -1; |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2208 | # else /* Other architecture (like i386) (32bits specific) */ |
Dmitry V. Levin | 5f731c4 | 2011-08-23 16:24:20 +0000 | [diff] [blame] | 2209 | for (i = 0; i < nargs; ++i) |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2210 | if (upeek(tcp, i*4, &tcp->u_arg[i]) < 0) |
| 2211 | return -1; |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2212 | # endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2213 | #endif /* LINUX */ |
| 2214 | #ifdef SUNOS4 |
Denys Vlasenko | 4b887a5 | 2011-08-23 13:32:38 +0200 | [diff] [blame] | 2215 | int i, nargs; |
Denys Vlasenko | afc6403 | 2011-08-23 13:29:01 +0200 | [diff] [blame] | 2216 | if (tcp->scno >= 0 && tcp->scno < nsyscalls) |
Denys Vlasenko | 4b887a5 | 2011-08-23 13:32:38 +0200 | [diff] [blame] | 2217 | nargs = tcp->u_nargs = sysent[tcp->scno].nargs; |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2218 | else |
Denys Vlasenko | 4b887a5 | 2011-08-23 13:32:38 +0200 | [diff] [blame] | 2219 | nargs = tcp->u_nargs = MAX_ARGS; |
| 2220 | for (i = 0; i < nargs; i++) { |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2221 | struct user *u; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2222 | |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2223 | if (upeek(tcp, uoff(u_arg[0]) + |
| 2224 | (i * sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0) |
| 2225 | return -1; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2226 | } |
| 2227 | #endif /* SUNOS4 */ |
| 2228 | #ifdef SVR4 |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2229 | # ifdef MIPS |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2230 | /* |
| 2231 | * SGI is broken: even though it has pr_sysarg, it doesn't |
| 2232 | * set them on system call entry. Get a clue. |
| 2233 | */ |
Denys Vlasenko | afc6403 | 2011-08-23 13:29:01 +0200 | [diff] [blame] | 2234 | if (tcp->scno >= 0 && tcp->scno < nsyscalls) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2235 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 2236 | else |
| 2237 | tcp->u_nargs = tcp->status.pr_nsysarg; |
| 2238 | if (tcp->u_nargs > 4) { |
| 2239 | memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0], |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2240 | 4 * sizeof(tcp->u_arg[0])); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2241 | umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16, |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2242 | (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] | 2243 | } |
| 2244 | else { |
| 2245 | memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0], |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2246 | tcp->u_nargs * sizeof(tcp->u_arg[0])); |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2247 | } |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2248 | # elif UNIXWARE >= 2 |
John Hughes | 2529971 | 2001-03-06 10:10:06 +0000 | [diff] [blame] | 2249 | /* |
| 2250 | * Like SGI, UnixWare doesn't set pr_sysarg until system call exit |
| 2251 | */ |
Denys Vlasenko | afc6403 | 2011-08-23 13:29:01 +0200 | [diff] [blame] | 2252 | if (tcp->scno >= 0 && tcp->scno < nsyscalls) |
John Hughes | 2529971 | 2001-03-06 10:10:06 +0000 | [diff] [blame] | 2253 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 2254 | else |
| 2255 | tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg; |
| 2256 | umoven(tcp, tcp->status.PR_REG[UESP] + 4, |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2257 | tcp->u_nargs * sizeof(tcp->u_arg[0]), (char *) tcp->u_arg); |
| 2258 | # elif defined(HAVE_PR_SYSCALL) |
| 2259 | int i; |
Denys Vlasenko | afc6403 | 2011-08-23 13:29:01 +0200 | [diff] [blame] | 2260 | if (tcp->scno >= 0 && tcp->scno < nsyscalls) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2261 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 2262 | else |
| 2263 | tcp->u_nargs = tcp->status.pr_nsysarg; |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2264 | for (i = 0; i < tcp->u_nargs; i++) |
| 2265 | tcp->u_arg[i] = tcp->status.pr_sysarg[i]; |
| 2266 | # elif defined(I386) |
Denys Vlasenko | afc6403 | 2011-08-23 13:29:01 +0200 | [diff] [blame] | 2267 | if (tcp->scno >= 0 && tcp->scno < nsyscalls) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2268 | tcp->u_nargs = sysent[tcp->scno].nargs; |
| 2269 | else |
| 2270 | tcp->u_nargs = 5; |
Denys Vlasenko | 4660fe6 | 2011-06-09 01:32:23 +0200 | [diff] [blame] | 2271 | if (tcp->u_nargs > 0) |
| 2272 | umoven(tcp, tcp->status.PR_REG[UESP] + 4, |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2273 | tcp->u_nargs * sizeof(tcp->u_arg[0]), (char *) tcp->u_arg); |
| 2274 | # else |
John Hughes | 2529971 | 2001-03-06 10:10:06 +0000 | [diff] [blame] | 2275 | I DONT KNOW WHAT TO DO |
Denys Vlasenko | f5d099c | 2011-06-23 22:10:54 +0200 | [diff] [blame] | 2276 | # endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2277 | #endif /* SVR4 */ |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2278 | #ifdef FREEBSD |
| 2279 | if (tcp->scno >= 0 && tcp->scno < nsyscalls && |
| 2280 | sysent[tcp->scno].nargs > tcp->status.val) |
| 2281 | tcp->u_nargs = sysent[tcp->scno].nargs; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 2282 | else |
Denys Vlasenko | 5ae2b7c | 2009-02-27 20:32:52 +0000 | [diff] [blame] | 2283 | tcp->u_nargs = tcp->status.val; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2284 | if (tcp->u_nargs < 0) |
| 2285 | tcp->u_nargs = 0; |
| 2286 | if (tcp->u_nargs > MAX_ARGS) |
| 2287 | tcp->u_nargs = MAX_ARGS; |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 2288 | switch (regs.r_eax) { |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2289 | case SYS___syscall: |
| 2290 | pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long), |
| 2291 | regs.r_esp + sizeof(int) + sizeof(quad_t)); |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 2292 | break; |
| 2293 | case SYS_syscall: |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2294 | pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long), |
| 2295 | regs.r_esp + 2 * sizeof(int)); |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 2296 | break; |
| 2297 | default: |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2298 | pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long), |
| 2299 | regs.r_esp + sizeof(int)); |
Denys Vlasenko | adedb51 | 2008-12-30 18:47:55 +0000 | [diff] [blame] | 2300 | break; |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2301 | } |
| 2302 | #endif /* FREEBSD */ |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 2303 | return 1; |
| 2304 | } |
| 2305 | |
Dmitry V. Levin | 7d7c963 | 2010-03-29 17:51:02 +0000 | [diff] [blame] | 2306 | static int |
Denys Vlasenko | ed4f4f0 | 2011-08-22 11:54:06 +0200 | [diff] [blame] | 2307 | trace_syscall_entering(struct tcb *tcp) |
| 2308 | { |
| 2309 | int res, scno_good; |
| 2310 | |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 2311 | scno_good = res = get_scno_on_sysenter(tcp); |
Denys Vlasenko | ed4f4f0 | 2011-08-22 11:54:06 +0200 | [diff] [blame] | 2312 | if (res == 0) |
| 2313 | return res; |
| 2314 | if (res == 1) |
| 2315 | res = syscall_fixup(tcp); |
| 2316 | if (res == 0) |
| 2317 | return res; |
| 2318 | if (res == 1) |
| 2319 | res = syscall_enter(tcp); |
| 2320 | if (res == 0) |
| 2321 | return res; |
| 2322 | |
| 2323 | if (res != 1) { |
| 2324 | printleader(tcp); |
| 2325 | tcp->flags &= ~TCB_REPRINT; |
| 2326 | tcp_last = tcp; |
| 2327 | if (scno_good != 1) |
| 2328 | tprintf("????" /* anti-trigraph gap */ "("); |
| 2329 | else if (tcp->scno >= nsyscalls || tcp->scno < 0) |
| 2330 | tprintf("syscall_%lu(", tcp->scno); |
| 2331 | else |
| 2332 | tprintf("%s(", sysent[tcp->scno].sys_name); |
| 2333 | /* |
| 2334 | * " <unavailable>" will be added later by the code which |
| 2335 | * detects ptrace errors. |
| 2336 | */ |
| 2337 | goto ret; |
| 2338 | } |
| 2339 | |
| 2340 | switch (known_scno(tcp)) { |
| 2341 | #ifdef SYS_socket_subcall |
| 2342 | case SYS_socketcall: |
| 2343 | decode_subcall(tcp, SYS_socket_subcall, |
| 2344 | SYS_socket_nsubcalls, deref_style); |
| 2345 | break; |
| 2346 | #endif |
| 2347 | #ifdef SYS_ipc_subcall |
| 2348 | case SYS_ipc: |
| 2349 | decode_subcall(tcp, SYS_ipc_subcall, |
| 2350 | SYS_ipc_nsubcalls, shift_style); |
| 2351 | break; |
| 2352 | #endif |
| 2353 | #ifdef SVR4 |
| 2354 | #ifdef SYS_pgrpsys_subcall |
| 2355 | case SYS_pgrpsys: |
| 2356 | decode_subcall(tcp, SYS_pgrpsys_subcall, |
| 2357 | SYS_pgrpsys_nsubcalls, shift_style); |
| 2358 | break; |
| 2359 | #endif /* SYS_pgrpsys_subcall */ |
| 2360 | #ifdef SYS_sigcall_subcall |
| 2361 | case SYS_sigcall: |
| 2362 | decode_subcall(tcp, SYS_sigcall_subcall, |
| 2363 | SYS_sigcall_nsubcalls, mask_style); |
| 2364 | break; |
| 2365 | #endif /* SYS_sigcall_subcall */ |
| 2366 | case SYS_msgsys: |
| 2367 | decode_subcall(tcp, SYS_msgsys_subcall, |
| 2368 | SYS_msgsys_nsubcalls, shift_style); |
| 2369 | break; |
| 2370 | case SYS_shmsys: |
| 2371 | decode_subcall(tcp, SYS_shmsys_subcall, |
| 2372 | SYS_shmsys_nsubcalls, shift_style); |
| 2373 | break; |
| 2374 | case SYS_semsys: |
| 2375 | decode_subcall(tcp, SYS_semsys_subcall, |
| 2376 | SYS_semsys_nsubcalls, shift_style); |
| 2377 | break; |
| 2378 | case SYS_sysfs: |
| 2379 | decode_subcall(tcp, SYS_sysfs_subcall, |
| 2380 | SYS_sysfs_nsubcalls, shift_style); |
| 2381 | break; |
| 2382 | case SYS_spcall: |
| 2383 | decode_subcall(tcp, SYS_spcall_subcall, |
| 2384 | SYS_spcall_nsubcalls, shift_style); |
| 2385 | break; |
| 2386 | #ifdef SYS_context_subcall |
| 2387 | case SYS_context: |
| 2388 | decode_subcall(tcp, SYS_context_subcall, |
| 2389 | SYS_context_nsubcalls, shift_style); |
| 2390 | break; |
| 2391 | #endif /* SYS_context_subcall */ |
| 2392 | #ifdef SYS_door_subcall |
| 2393 | case SYS_door: |
| 2394 | decode_subcall(tcp, SYS_door_subcall, |
| 2395 | SYS_door_nsubcalls, door_style); |
| 2396 | break; |
| 2397 | #endif /* SYS_door_subcall */ |
| 2398 | #ifdef SYS_kaio_subcall |
| 2399 | case SYS_kaio: |
| 2400 | decode_subcall(tcp, SYS_kaio_subcall, |
| 2401 | SYS_kaio_nsubcalls, shift_style); |
| 2402 | break; |
| 2403 | #endif |
| 2404 | #endif /* SVR4 */ |
| 2405 | #ifdef FREEBSD |
| 2406 | case SYS_msgsys: |
| 2407 | case SYS_shmsys: |
| 2408 | case SYS_semsys: |
| 2409 | decode_subcall(tcp, 0, 0, table_style); |
| 2410 | break; |
| 2411 | #endif |
| 2412 | #ifdef SUNOS4 |
| 2413 | case SYS_semsys: |
| 2414 | decode_subcall(tcp, SYS_semsys_subcall, |
| 2415 | SYS_semsys_nsubcalls, shift_style); |
| 2416 | break; |
| 2417 | case SYS_msgsys: |
| 2418 | decode_subcall(tcp, SYS_msgsys_subcall, |
| 2419 | SYS_msgsys_nsubcalls, shift_style); |
| 2420 | break; |
| 2421 | case SYS_shmsys: |
| 2422 | decode_subcall(tcp, SYS_shmsys_subcall, |
| 2423 | SYS_shmsys_nsubcalls, shift_style); |
| 2424 | break; |
| 2425 | #endif |
| 2426 | } |
| 2427 | |
| 2428 | internal_syscall(tcp); |
| 2429 | |
| 2430 | if ((tcp->scno >= 0 && tcp->scno < nsyscalls && |
| 2431 | !(qual_flags[tcp->scno] & QUAL_TRACE)) || |
| 2432 | (tracing_paths && !pathtrace_match(tcp))) { |
| 2433 | tcp->flags |= TCB_INSYSCALL | TCB_FILTERED; |
| 2434 | return 0; |
| 2435 | } |
| 2436 | |
| 2437 | tcp->flags &= ~TCB_FILTERED; |
| 2438 | |
| 2439 | if (cflag == CFLAG_ONLY_STATS) { |
| 2440 | res = 0; |
| 2441 | goto ret; |
| 2442 | } |
| 2443 | |
| 2444 | printleader(tcp); |
| 2445 | tcp->flags &= ~TCB_REPRINT; |
| 2446 | tcp_last = tcp; |
| 2447 | if (tcp->scno >= nsyscalls || tcp->scno < 0) |
| 2448 | tprintf("syscall_%lu(", tcp->scno); |
| 2449 | else |
| 2450 | tprintf("%s(", sysent[tcp->scno].sys_name); |
| 2451 | if (tcp->scno >= nsyscalls || tcp->scno < 0 || |
| 2452 | ((qual_flags[tcp->scno] & QUAL_RAW) && |
| 2453 | sysent[tcp->scno].sys_func != sys_exit)) |
| 2454 | res = printargs(tcp); |
| 2455 | else |
| 2456 | res = (*sysent[tcp->scno].sys_func)(tcp); |
| 2457 | |
| 2458 | if (fflush(tcp->outf) == EOF) |
| 2459 | return -1; |
| 2460 | ret: |
| 2461 | tcp->flags |= TCB_INSYSCALL; |
| 2462 | /* Measure the entrance time as late as possible to avoid errors. */ |
| 2463 | if (dtime || cflag) |
| 2464 | gettimeofday(&tcp->etime, NULL); |
| 2465 | return res; |
| 2466 | } |
| 2467 | |
| 2468 | static int |
Dmitry V. Levin | 7d7c963 | 2010-03-29 17:51:02 +0000 | [diff] [blame] | 2469 | trace_syscall_exiting(struct tcb *tcp) |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 2470 | { |
| 2471 | int sys_res; |
| 2472 | struct timeval tv; |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 2473 | int res, scno_good; |
Dmitry V. Levin | 7d7c963 | 2010-03-29 17:51:02 +0000 | [diff] [blame] | 2474 | long u_error; |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 2475 | |
Dmitry V. Levin | 7d7c963 | 2010-03-29 17:51:02 +0000 | [diff] [blame] | 2476 | /* Measure the exit time as early as possible to avoid errors. */ |
| 2477 | if (dtime || cflag) |
| 2478 | gettimeofday(&tv, NULL); |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 2479 | |
Dmitry V. Levin | 7d7c963 | 2010-03-29 17:51:02 +0000 | [diff] [blame] | 2480 | /* BTW, why we don't just memorize syscall no. on entry |
| 2481 | * in tcp->something? |
| 2482 | */ |
Denys Vlasenko | 9a36ae5 | 2011-08-24 16:47:32 +0200 | [diff] [blame] | 2483 | scno_good = res = get_scno_on_sysexit(tcp); |
Dmitry V. Levin | 7d7c963 | 2010-03-29 17:51:02 +0000 | [diff] [blame] | 2484 | if (res == 0) |
| 2485 | return res; |
| 2486 | if (res == 1) |
| 2487 | res = syscall_fixup(tcp); |
| 2488 | if (res == 0) |
| 2489 | return res; |
| 2490 | if (res == 1) |
| 2491 | res = get_error(tcp); |
| 2492 | if (res == 0) |
| 2493 | return res; |
| 2494 | if (res == 1) |
| 2495 | internal_syscall(tcp); |
Denys Vlasenko | 732d1bf | 2008-12-17 19:21:59 +0000 | [diff] [blame] | 2496 | |
Grant Edwards | 8a08277 | 2011-04-07 20:25:40 +0000 | [diff] [blame] | 2497 | if (res == 1 && filtered(tcp)) { |
Denys Vlasenko | 3b73881 | 2011-08-22 02:06:35 +0200 | [diff] [blame] | 2498 | goto ret; |
Pavel Machek | 4dc3b14 | 2000-02-01 17:58:41 +0000 | [diff] [blame] | 2499 | } |
| 2500 | |
Dmitry V. Levin | 7d7c963 | 2010-03-29 17:51:02 +0000 | [diff] [blame] | 2501 | if (tcp->flags & TCB_REPRINT) { |
| 2502 | printleader(tcp); |
| 2503 | tprintf("<... "); |
| 2504 | if (scno_good != 1) |
| 2505 | tprintf("????"); |
| 2506 | else if (tcp->scno >= nsyscalls || tcp->scno < 0) |
| 2507 | tprintf("syscall_%lu", tcp->scno); |
| 2508 | else |
| 2509 | tprintf("%s", sysent[tcp->scno].sys_name); |
| 2510 | tprintf(" resumed> "); |
| 2511 | } |
| 2512 | |
| 2513 | if (cflag) { |
| 2514 | struct timeval t = tv; |
Denys Vlasenko | c95a88f | 2011-08-21 17:47:40 +0200 | [diff] [blame] | 2515 | count_syscall(tcp, &t); |
Denys Vlasenko | 7b609d5 | 2011-06-22 14:32:43 +0200 | [diff] [blame] | 2516 | if (cflag == CFLAG_ONLY_STATS) { |
Denys Vlasenko | 3b73881 | 2011-08-22 02:06:35 +0200 | [diff] [blame] | 2517 | goto ret; |
Dmitry V. Levin | 7d7c963 | 2010-03-29 17:51:02 +0000 | [diff] [blame] | 2518 | } |
| 2519 | } |
| 2520 | |
| 2521 | if (res != 1) { |
| 2522 | tprintf(") "); |
| 2523 | tabto(acolumn); |
| 2524 | tprintf("= ? <unavailable>"); |
| 2525 | printtrailer(); |
| 2526 | tcp->flags &= ~TCB_INSYSCALL; |
| 2527 | return res; |
| 2528 | } |
| 2529 | |
| 2530 | if (tcp->scno >= nsyscalls || tcp->scno < 0 |
| 2531 | || (qual_flags[tcp->scno] & QUAL_RAW)) |
| 2532 | sys_res = printargs(tcp); |
| 2533 | else { |
Denys Vlasenko | 3b73881 | 2011-08-22 02:06:35 +0200 | [diff] [blame] | 2534 | /* FIXME: not_failing_only (IOW, option -z) is broken: |
| 2535 | * failure of syscall is known only after syscall return. |
| 2536 | * Thus we end up with something like this on, say, ENOENT: |
| 2537 | * open("doesnt_exist", O_RDONLY <unfinished ...> |
| 2538 | * {next syscall decode} |
| 2539 | * whereas the intended result is that open(...) line |
| 2540 | * is not shown at all. |
| 2541 | */ |
Dmitry V. Levin | 7d7c963 | 2010-03-29 17:51:02 +0000 | [diff] [blame] | 2542 | if (not_failing_only && tcp->u_error) |
Denys Vlasenko | 3b73881 | 2011-08-22 02:06:35 +0200 | [diff] [blame] | 2543 | goto ret; /* ignore failed syscalls */ |
Dmitry V. Levin | 7d7c963 | 2010-03-29 17:51:02 +0000 | [diff] [blame] | 2544 | sys_res = (*sysent[tcp->scno].sys_func)(tcp); |
| 2545 | } |
| 2546 | |
Dmitry V. Levin | 7d7c963 | 2010-03-29 17:51:02 +0000 | [diff] [blame] | 2547 | tprintf(") "); |
| 2548 | tabto(acolumn); |
Denys Vlasenko | 3b73881 | 2011-08-22 02:06:35 +0200 | [diff] [blame] | 2549 | u_error = tcp->u_error; |
Dmitry V. Levin | 7d7c963 | 2010-03-29 17:51:02 +0000 | [diff] [blame] | 2550 | if (tcp->scno >= nsyscalls || tcp->scno < 0 || |
| 2551 | qual_flags[tcp->scno] & QUAL_RAW) { |
| 2552 | if (u_error) |
| 2553 | tprintf("= -1 (errno %ld)", u_error); |
| 2554 | else |
| 2555 | tprintf("= %#lx", tcp->u_rval); |
| 2556 | } |
| 2557 | else if (!(sys_res & RVAL_NONE) && u_error) { |
| 2558 | switch (u_error) { |
| 2559 | #ifdef LINUX |
| 2560 | case ERESTARTSYS: |
| 2561 | tprintf("= ? ERESTARTSYS (To be restarted)"); |
| 2562 | break; |
| 2563 | case ERESTARTNOINTR: |
| 2564 | tprintf("= ? ERESTARTNOINTR (To be restarted)"); |
| 2565 | break; |
| 2566 | case ERESTARTNOHAND: |
| 2567 | tprintf("= ? ERESTARTNOHAND (To be restarted)"); |
| 2568 | break; |
| 2569 | case ERESTART_RESTARTBLOCK: |
| 2570 | tprintf("= ? ERESTART_RESTARTBLOCK (To be restarted)"); |
| 2571 | break; |
| 2572 | #endif /* LINUX */ |
| 2573 | default: |
Dmitry V. Levin | 7d7c963 | 2010-03-29 17:51:02 +0000 | [diff] [blame] | 2574 | if (u_error < 0) |
Denys Vlasenko | a794974 | 2011-08-21 17:26:55 +0200 | [diff] [blame] | 2575 | tprintf("= -1 E??? (errno %ld)", u_error); |
Dmitry V. Levin | 7d7c963 | 2010-03-29 17:51:02 +0000 | [diff] [blame] | 2576 | else if (u_error < nerrnos) |
Denys Vlasenko | a794974 | 2011-08-21 17:26:55 +0200 | [diff] [blame] | 2577 | tprintf("= -1 %s (%s)", errnoent[u_error], |
Dmitry V. Levin | 7d7c963 | 2010-03-29 17:51:02 +0000 | [diff] [blame] | 2578 | strerror(u_error)); |
| 2579 | else |
Denys Vlasenko | a794974 | 2011-08-21 17:26:55 +0200 | [diff] [blame] | 2580 | tprintf("= -1 ERRNO_%ld (%s)", u_error, |
Dmitry V. Levin | 7d7c963 | 2010-03-29 17:51:02 +0000 | [diff] [blame] | 2581 | strerror(u_error)); |
| 2582 | break; |
| 2583 | } |
| 2584 | if ((sys_res & RVAL_STR) && tcp->auxstr) |
| 2585 | tprintf(" (%s)", tcp->auxstr); |
| 2586 | } |
| 2587 | else { |
| 2588 | if (sys_res & RVAL_NONE) |
| 2589 | tprintf("= ?"); |
| 2590 | else { |
| 2591 | switch (sys_res & RVAL_MASK) { |
| 2592 | case RVAL_HEX: |
| 2593 | tprintf("= %#lx", tcp->u_rval); |
| 2594 | break; |
| 2595 | case RVAL_OCTAL: |
| 2596 | tprintf("= %#lo", tcp->u_rval); |
| 2597 | break; |
| 2598 | case RVAL_UDECIMAL: |
| 2599 | tprintf("= %lu", tcp->u_rval); |
| 2600 | break; |
| 2601 | case RVAL_DECIMAL: |
| 2602 | tprintf("= %ld", tcp->u_rval); |
| 2603 | break; |
| 2604 | #ifdef HAVE_LONG_LONG |
| 2605 | case RVAL_LHEX: |
| 2606 | tprintf("= %#llx", tcp->u_lrval); |
| 2607 | break; |
| 2608 | case RVAL_LOCTAL: |
| 2609 | tprintf("= %#llo", tcp->u_lrval); |
| 2610 | break; |
| 2611 | case RVAL_LUDECIMAL: |
| 2612 | tprintf("= %llu", tcp->u_lrval); |
| 2613 | break; |
| 2614 | case RVAL_LDECIMAL: |
| 2615 | tprintf("= %lld", tcp->u_lrval); |
| 2616 | break; |
| 2617 | #endif |
| 2618 | default: |
| 2619 | fprintf(stderr, |
| 2620 | "invalid rval format\n"); |
| 2621 | break; |
| 2622 | } |
| 2623 | } |
| 2624 | if ((sys_res & RVAL_STR) && tcp->auxstr) |
| 2625 | tprintf(" (%s)", tcp->auxstr); |
| 2626 | } |
| 2627 | if (dtime) { |
| 2628 | tv_sub(&tv, &tv, &tcp->etime); |
| 2629 | tprintf(" <%ld.%06ld>", |
| 2630 | (long) tv.tv_sec, (long) tv.tv_usec); |
| 2631 | } |
| 2632 | printtrailer(); |
| 2633 | |
| 2634 | dumpio(tcp); |
| 2635 | if (fflush(tcp->outf) == EOF) |
| 2636 | return -1; |
Denys Vlasenko | 3b73881 | 2011-08-22 02:06:35 +0200 | [diff] [blame] | 2637 | ret: |
Dmitry V. Levin | 7d7c963 | 2010-03-29 17:51:02 +0000 | [diff] [blame] | 2638 | tcp->flags &= ~TCB_INSYSCALL; |
| 2639 | return 0; |
| 2640 | } |
| 2641 | |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2642 | int |
Dmitry V. Levin | 7d7c963 | 2010-03-29 17:51:02 +0000 | [diff] [blame] | 2643 | trace_syscall(struct tcb *tcp) |
| 2644 | { |
| 2645 | return exiting(tcp) ? |
| 2646 | trace_syscall_exiting(tcp) : trace_syscall_entering(tcp); |
| 2647 | } |
| 2648 | |
| 2649 | int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 2650 | printargs(struct tcb *tcp) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2651 | { |
| 2652 | if (entering(tcp)) { |
| 2653 | int i; |
| 2654 | |
| 2655 | for (i = 0; i < tcp->u_nargs; i++) |
| 2656 | tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]); |
| 2657 | } |
| 2658 | return 0; |
| 2659 | } |
| 2660 | |
| 2661 | long |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 2662 | getrval2(struct tcb *tcp) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2663 | { |
| 2664 | long val = -1; |
| 2665 | |
| 2666 | #ifdef LINUX |
Roland McGrath | 6d1a65c | 2004-07-12 07:44:08 +0000 | [diff] [blame] | 2667 | #if defined (SPARC) || defined (SPARC64) |
Mike Frysinger | 8566c50 | 2009-10-12 11:05:14 -0400 | [diff] [blame] | 2668 | struct pt_regs regs; |
Denys Vlasenko | b63256e | 2011-06-07 12:13:24 +0200 | [diff] [blame] | 2669 | if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)®s, 0) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2670 | return -1; |
Mike Frysinger | 8566c50 | 2009-10-12 11:05:14 -0400 | [diff] [blame] | 2671 | val = regs.u_regs[U_REG_O1]; |
Roland McGrath | 6b1d43e | 2003-03-31 01:05:01 +0000 | [diff] [blame] | 2672 | #elif defined(SH) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 2673 | if (upeek(tcp, 4*(REG_REG0+1), &val) < 0) |
Roland McGrath | 6b1d43e | 2003-03-31 01:05:01 +0000 | [diff] [blame] | 2674 | return -1; |
Roland McGrath | b4ce176 | 2004-03-01 20:30:48 +0000 | [diff] [blame] | 2675 | #elif defined(IA64) |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 2676 | if (upeek(tcp, PT_R9, &val) < 0) |
Roland McGrath | b4ce176 | 2004-03-01 20:30:48 +0000 | [diff] [blame] | 2677 | return -1; |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 2678 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2679 | #endif /* LINUX */ |
| 2680 | |
| 2681 | #ifdef SUNOS4 |
Denys Vlasenko | 932fc7d | 2008-12-16 18:18:40 +0000 | [diff] [blame] | 2682 | if (upeek(tcp, uoff(u_rval2), &val) < 0) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2683 | return -1; |
| 2684 | #endif /* SUNOS4 */ |
| 2685 | |
| 2686 | #ifdef SVR4 |
| 2687 | #ifdef SPARC |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2688 | val = tcp->status.PR_REG[R_O1]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2689 | #endif /* SPARC */ |
| 2690 | #ifdef I386 |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2691 | val = tcp->status.PR_REG[EDX]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2692 | #endif /* I386 */ |
Michal Ludvig | 0e03550 | 2002-09-23 15:41:01 +0000 | [diff] [blame] | 2693 | #ifdef X86_64 |
| 2694 | val = tcp->status.PR_REG[RDX]; |
| 2695 | #endif /* X86_64 */ |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2696 | #ifdef MIPS |
Wichert Akkerman | 9ce1a63 | 1999-08-29 23:15:07 +0000 | [diff] [blame] | 2697 | val = tcp->status.PR_REG[CTX_V1]; |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2698 | #endif /* MIPS */ |
| 2699 | #endif /* SVR4 */ |
Denys Vlasenko | ea0e6e8 | 2009-02-25 17:08:40 +0000 | [diff] [blame] | 2700 | |
Wichert Akkerman | bf79f2e | 2000-09-01 21:03:06 +0000 | [diff] [blame] | 2701 | #ifdef FREEBSD |
| 2702 | struct reg regs; |
| 2703 | pread(tcp->pfd_reg, ®s, sizeof(regs), 0); |
| 2704 | val = regs.r_edx; |
Roland McGrath | 761b5d7 | 2002-12-15 23:58:31 +0000 | [diff] [blame] | 2705 | #endif |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2706 | return val; |
| 2707 | } |
| 2708 | |
Dmitry V. Levin | b9fe011 | 2006-12-13 16:59:44 +0000 | [diff] [blame] | 2709 | #ifdef SUNOS4 |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2710 | /* |
| 2711 | * Apparently, indirect system calls have already be converted by ptrace(2), |
| 2712 | * so if you see "indir" this program has gone astray. |
| 2713 | */ |
| 2714 | int |
Denys Vlasenko | 1201426 | 2011-05-30 14:00:14 +0200 | [diff] [blame] | 2715 | sys_indir(struct tcb *tcp) |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2716 | { |
| 2717 | int i, scno, nargs; |
| 2718 | |
| 2719 | if (entering(tcp)) { |
Denys Vlasenko | 5d64581 | 2011-08-20 12:48:18 +0200 | [diff] [blame] | 2720 | scno = tcp->u_arg[0]; |
| 2721 | if (scno > nsyscalls) { |
Wichert Akkerman | 76baf7c | 1999-02-19 00:21:36 +0000 | [diff] [blame] | 2722 | fprintf(stderr, "Bogus syscall: %u\n", scno); |
| 2723 | return 0; |
| 2724 | } |
| 2725 | nargs = sysent[scno].nargs; |
| 2726 | tprintf("%s", sysent[scno].sys_name); |
| 2727 | for (i = 0; i < nargs; i++) |
| 2728 | tprintf(", %#lx", tcp->u_arg[i+1]); |
| 2729 | } |
| 2730 | return 0; |
| 2731 | } |
Dmitry V. Levin | b9fe011 | 2006-12-13 16:59:44 +0000 | [diff] [blame] | 2732 | #endif /* SUNOS4 */ |
Dmitry V. Levin | 2e55ff4 | 2008-09-03 01:02:46 +0000 | [diff] [blame] | 2733 | |
| 2734 | int |
| 2735 | is_restart_error(struct tcb *tcp) |
| 2736 | { |
| 2737 | #ifdef LINUX |
| 2738 | if (!syserror(tcp)) |
| 2739 | return 0; |
| 2740 | switch (tcp->u_error) { |
| 2741 | case ERESTARTSYS: |
| 2742 | case ERESTARTNOINTR: |
| 2743 | case ERESTARTNOHAND: |
| 2744 | case ERESTART_RESTARTBLOCK: |
| 2745 | return 1; |
| 2746 | default: |
| 2747 | break; |
| 2748 | } |
| 2749 | #endif /* LINUX */ |
| 2750 | return 0; |
| 2751 | } |