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