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