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