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