blob: ceddc5d3691459763c92669bf7f6bfe652e0e0ee [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
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 Akkerman4dc8a2a1999-12-23 14:20:14 +00005 * 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 Akkerman76baf7c1999-02-19 00:21:36 +00009 * 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.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000032 */
33
34#include "defs.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000035#include <sys/user.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000036#include <sys/param.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000037
Wichert Akkerman15dea971999-10-06 13:06:34 +000038#ifdef HAVE_SYS_REG_H
Denys Vlasenko523635f2012-02-25 02:44:25 +010039# include <sys/reg.h>
40# ifndef PTRACE_PEEKUSR
41# define PTRACE_PEEKUSR PTRACE_PEEKUSER
42# endif
Wichert Akkermanfaf72222000-02-19 23:59:03 +000043#elif defined(HAVE_LINUX_PTRACE_H)
Denys Vlasenko523635f2012-02-25 02:44:25 +010044# undef PTRACE_SYSCALL
Roland McGrathce9f0742004-03-01 21:29:22 +000045# ifdef HAVE_STRUCT_IA64_FPREG
46# define ia64_fpreg XXX_ia64_fpreg
47# endif
48# ifdef HAVE_STRUCT_PT_ALL_USER_REGS
49# define pt_all_user_regs XXX_pt_all_user_regs
50# endif
Denys Vlasenko523635f2012-02-25 02:44:25 +010051# include <linux/ptrace.h>
Roland McGrathce9f0742004-03-01 21:29:22 +000052# undef ia64_fpreg
53# undef pt_all_user_regs
Wichert Akkerman15dea971999-10-06 13:06:34 +000054#endif
55
Denys Vlasenko84703742012-02-25 02:38:52 +010056#if defined(SPARC64)
Roland McGrath6d1a65c2004-07-12 07:44:08 +000057# undef PTRACE_GETREGS
58# define PTRACE_GETREGS PTRACE_GETREGS64
59# undef PTRACE_SETREGS
60# define PTRACE_SETREGS PTRACE_SETREGS64
Denys Vlasenko84703742012-02-25 02:38:52 +010061#endif
Roland McGrath6d1a65c2004-07-12 07:44:08 +000062
Denys Vlasenko84703742012-02-25 02:38:52 +010063#if defined(IA64)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000064# include <asm/ptrace_offsets.h>
65# include <asm/rse.h>
66#endif
67
Dmitry V. Levin32c049a2013-03-18 00:59:27 +000068/* for struct iovec */
69#include <sys/uio.h>
70/* for NT_PRSTATUS */
71#ifdef HAVE_ELF_H
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010072# include <elf.h>
73#endif
74
Steve McIntyred8d3bd32012-10-24 17:58:16 +010075#if defined(AARCH64)
76# include <asm/ptrace.h>
James Hogan5f999a82013-02-22 14:44:10 +000077#endif
78
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000079#ifndef ERESTARTSYS
Denys Vlasenko523635f2012-02-25 02:44:25 +010080# define ERESTARTSYS 512
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000081#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010082#ifndef ERESTARTNOINTR
Denys Vlasenko523635f2012-02-25 02:44:25 +010083# define ERESTARTNOINTR 513
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000084#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010085#ifndef ERESTARTNOHAND
86# define ERESTARTNOHAND 514 /* restart if no handler */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000087#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010088#ifndef ERESTART_RESTARTBLOCK
Denys Vlasenko523635f2012-02-25 02:44:25 +010089# define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
Roland McGrath9c555e72003-07-09 09:47:59 +000090#endif
Denys Vlasenko523635f2012-02-25 02:44:25 +010091
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000092#ifndef NSIG
Denys Vlasenko523635f2012-02-25 02:44:25 +010093# warning: NSIG is not defined, using 32
94# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000095#endif
96#ifdef ARM
Denys Vlasenko041b3ee2011-08-18 12:48:56 +020097/* Ugh. Is this really correct? ARM has no RT signals?! */
Denys Vlasenko523635f2012-02-25 02:44:25 +010098# undef NSIG
99# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000100#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000101
102#include "syscall.h"
103
104/* Define these shorthand notations to simplify the syscallent files. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000105#define TD TRACE_DESC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000106#define TF TRACE_FILE
107#define TI TRACE_IPC
108#define TN TRACE_NETWORK
109#define TP TRACE_PROCESS
110#define TS TRACE_SIGNAL
Namhyung Kim96792962012-10-24 11:41:57 +0900111#define TM TRACE_MEMORY
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000112#define NF SYSCALL_NEVER_FAILS
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200113#define MA MAX_ARGS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000114
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100115const struct_sysent sysent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000116#include "syscallent.h"
117};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000118
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100119#if SUPPORTED_PERSONALITIES > 1
120static const struct_sysent sysent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100121# include "syscallent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000122};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200123#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000124
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100125#if SUPPORTED_PERSONALITIES > 2
126static const struct_sysent sysent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100127# include "syscallent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000128};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200129#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000130
131/* Now undef them since short defines cause wicked namespace pollution. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000132#undef TD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000133#undef TF
134#undef TI
135#undef TN
136#undef TP
137#undef TS
Namhyung Kim96792962012-10-24 11:41:57 +0900138#undef TM
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000139#undef NF
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200140#undef MA
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000141
Denys Vlasenko39fca622011-08-20 02:12:33 +0200142/*
143 * `ioctlent.h' may be generated from `ioctlent.raw' by the auxiliary
144 * program `ioctlsort', such that the list is sorted by the `code' field.
145 * This has the side-effect of resolving the _IO.. macros into
146 * plain integers, eliminating the need to include here everything
147 * in "/usr/include".
148 */
149
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100150const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000151#include "errnoent.h"
152};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100153const char *const signalent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200154#include "signalent.h"
155};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100156const struct_ioctlent ioctlent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200157#include "ioctlent.h"
158};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000159
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100160#if SUPPORTED_PERSONALITIES > 1
Roland McGrathee36ce12004-09-04 03:53:10 +0000161static const char *const errnoent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100162# include "errnoent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000163};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200164static const char *const signalent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100165# include "signalent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200166};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100167static const struct_ioctlent ioctlent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100168# include "ioctlent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200169};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200170#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000171
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100172#if SUPPORTED_PERSONALITIES > 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000173static const char *const errnoent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100174# include "errnoent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000175};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200176static const char *const signalent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100177# include "signalent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200178};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100179static const struct_ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100180# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200181};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200182#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000183
Dmitry V. Levine6f55242013-02-26 22:02:30 +0000184enum {
185 nsyscalls0 = ARRAY_SIZE(sysent0)
186#if SUPPORTED_PERSONALITIES > 1
187 , nsyscalls1 = ARRAY_SIZE(sysent1)
188# if SUPPORTED_PERSONALITIES > 2
189 , nsyscalls2 = ARRAY_SIZE(sysent2)
190# endif
191#endif
192};
193
194enum {
195 nerrnos0 = ARRAY_SIZE(errnoent0)
196#if SUPPORTED_PERSONALITIES > 1
197 , nerrnos1 = ARRAY_SIZE(errnoent1)
198# if SUPPORTED_PERSONALITIES > 2
199 , nerrnos2 = ARRAY_SIZE(errnoent2)
200# endif
201#endif
202};
203
204enum {
205 nsignals0 = ARRAY_SIZE(signalent0)
206#if SUPPORTED_PERSONALITIES > 1
207 , nsignals1 = ARRAY_SIZE(signalent1)
208# if SUPPORTED_PERSONALITIES > 2
209 , nsignals2 = ARRAY_SIZE(signalent2)
210# endif
211#endif
212};
213
214enum {
215 nioctlents0 = ARRAY_SIZE(ioctlent0)
216#if SUPPORTED_PERSONALITIES > 1
217 , nioctlents1 = ARRAY_SIZE(ioctlent1)
218# if SUPPORTED_PERSONALITIES > 2
219 , nioctlents2 = ARRAY_SIZE(ioctlent2)
220# endif
221#endif
222};
223
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100224#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100225const struct_sysent *sysent = sysent0;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100226const char *const *errnoent = errnoent0;
227const char *const *signalent = signalent0;
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100228const struct_ioctlent *ioctlent = ioctlent0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100229#endif
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100230unsigned nsyscalls = nsyscalls0;
231unsigned nerrnos = nerrnos0;
232unsigned nsignals = nsignals0;
233unsigned nioctlents = nioctlents0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100234
235unsigned num_quals;
236qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
237
238static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
239 nsyscalls0,
240#if SUPPORTED_PERSONALITIES > 1
241 nsyscalls1,
242#endif
243#if SUPPORTED_PERSONALITIES > 2
244 nsyscalls2,
245#endif
246};
247static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
248 sysent0,
249#if SUPPORTED_PERSONALITIES > 1
250 sysent1,
251#endif
252#if SUPPORTED_PERSONALITIES > 2
253 sysent2,
254#endif
255};
256
257enum {
258 MAX_NSYSCALLS1 = (nsyscalls0
259#if SUPPORTED_PERSONALITIES > 1
260 > nsyscalls1 ? nsyscalls0 : nsyscalls1
261#endif
262 ),
263 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
264#if SUPPORTED_PERSONALITIES > 2
265 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
266#endif
267 ),
268 MAX_NSYSCALLS = MAX_NSYSCALLS2,
269 /* We are ready for arches with up to 255 signals,
270 * even though the largest known signo is on MIPS and it is 128.
271 * The number of existing syscalls on all arches is
272 * larger that 255 anyway, so it is just a pedantic matter.
273 */
274 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
275};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000276
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100277#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100278unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000279
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100280# ifndef current_wordsize
281unsigned current_wordsize;
282static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000283 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000284 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100285# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000286 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100287# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200288};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100289# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000290
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200291void
Dmitry V. Levin3abe8b22006-12-20 22:37:21 +0000292set_personality(int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000293{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100294 nsyscalls = nsyscall_vec[personality];
295 sysent = sysent_vec[personality];
296
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000297 switch (personality) {
298 case 0:
299 errnoent = errnoent0;
300 nerrnos = nerrnos0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000301 ioctlent = ioctlent0;
302 nioctlents = nioctlents0;
303 signalent = signalent0;
304 nsignals = nsignals0;
305 break;
306
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000307 case 1:
308 errnoent = errnoent1;
309 nerrnos = nerrnos1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000310 ioctlent = ioctlent1;
311 nioctlents = nioctlents1;
312 signalent = signalent1;
313 nsignals = nsignals1;
314 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000315
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100316# if SUPPORTED_PERSONALITIES > 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000317 case 2:
318 errnoent = errnoent2;
319 nerrnos = nerrnos2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000320 ioctlent = ioctlent2;
321 nioctlents = nioctlents2;
322 signalent = signalent2;
323 nsignals = nsignals2;
324 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100325# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000326 }
327
328 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100329# ifndef current_wordsize
330 current_wordsize = personality_wordsize[personality];
331# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000332}
333
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000334static void
335update_personality(struct tcb *tcp, int personality)
336{
337 if (personality == current_personality)
338 return;
339 set_personality(personality);
340
341 if (personality == tcp->currpers)
342 return;
343 tcp->currpers = personality;
344
H.J. Lu35be5812012-04-16 13:00:01 +0200345# if defined(POWERPC64)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000346 if (!qflag) {
347 static const char *const names[] = {"64 bit", "32 bit"};
348 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
349 tcp->pid, names[personality]);
350 }
H.J. Lu35be5812012-04-16 13:00:01 +0200351# elif defined(X86_64)
352 if (!qflag) {
353 static const char *const names[] = {"64 bit", "32 bit", "x32"};
354 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
355 tcp->pid, names[personality]);
356 }
H.J. Lu085e4282012-04-17 11:05:04 -0700357# elif defined(X32)
358 if (!qflag) {
359 static const char *const names[] = {"x32", "32 bit"};
360 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
361 tcp->pid, names[personality]);
362 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000363# elif defined(AARCH64)
364 if (!qflag) {
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100365 static const char *const names[] = {"32-bit", "AArch64"};
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000366 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
367 tcp->pid, names[personality]);
368 }
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100369# elif defined(TILE)
370 if (!qflag) {
371 static const char *const names[] = {"64-bit", "32-bit"};
372 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
373 tcp->pid, names[personality]);
374 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100375# endif
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000376}
377#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000378
Denys Vlasenkoc1540fe2013-02-21 16:17:08 +0100379static int qual_syscall(), qual_signal(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000380
Roland McGrathe10e62a2004-09-04 04:20:43 +0000381static const struct qual_options {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000382 int bitflag;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000383 const char *option_name;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000384 int (*qualify)(const char *, int, int);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000385 const char *argument_name;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000386} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000387 { QUAL_TRACE, "trace", qual_syscall, "system call" },
388 { QUAL_TRACE, "t", qual_syscall, "system call" },
389 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
390 { QUAL_ABBREV, "a", qual_syscall, "system call" },
391 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
392 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
393 { QUAL_RAW, "raw", qual_syscall, "system call" },
394 { QUAL_RAW, "x", qual_syscall, "system call" },
395 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
396 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
397 { QUAL_SIGNAL, "s", qual_signal, "signal" },
Roland McGrath9797ceb2002-12-30 10:23:00 +0000398 { QUAL_READ, "read", qual_desc, "descriptor" },
399 { QUAL_READ, "reads", qual_desc, "descriptor" },
400 { QUAL_READ, "r", qual_desc, "descriptor" },
401 { QUAL_WRITE, "write", qual_desc, "descriptor" },
402 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
403 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000404 { 0, NULL, NULL, NULL },
405};
406
Roland McGrath9797ceb2002-12-30 10:23:00 +0000407static void
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100408reallocate_qual(int n)
409{
410 unsigned p;
411 qualbits_t *qp;
412 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
413 qp = qual_vec[p] = realloc(qual_vec[p], n * sizeof(qualbits_t));
414 if (!qp)
415 die_out_of_memory();
416 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
417 }
418 num_quals = n;
419}
420
421static void
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000422qualify_one(int n, int bitflag, int not, int pers)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000423{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100424 unsigned p;
Roland McGrath138c6a32006-01-12 09:50:49 +0000425
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100426 if (num_quals <= n)
427 reallocate_qual(n + 1);
Roland McGrath138c6a32006-01-12 09:50:49 +0000428
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100429 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
430 if (pers == p || pers < 0) {
431 if (not)
432 qual_vec[p][n] &= ~bitflag;
433 else
434 qual_vec[p][n] |= bitflag;
435 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000436 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000437}
438
439static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000440qual_syscall(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000441{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100442 unsigned p;
443 unsigned i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000444 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000445
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100446 if (*s >= '0' && *s <= '9') {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100447 i = string_to_uint(s);
Denys Vlasenkob43dacd2013-02-23 18:19:28 +0100448 if (i >= MAX_NSYSCALLS)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000449 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000450 qualify_one(i, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000451 return 0;
Roland McGrath48a035f2006-01-12 09:45:56 +0000452 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000453
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100454 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
455 for (i = 0; i < nsyscall_vec[p]; i++) {
456 if (sysent_vec[p][i].sys_name
457 && strcmp(s, sysent_vec[p][i].sys_name) == 0
458 ) {
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000459 qualify_one(i, bitflag, not, p);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100460 rc = 0;
461 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000462 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100463 }
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000464
Roland McGrathfe6b3522005-02-02 04:40:11 +0000465 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000466}
467
468static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000469qual_signal(const char *s, int bitflag, int not)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000470{
471 int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000472
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100473 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000474 int signo = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100475 if (signo < 0 || signo > 255)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000476 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000477 qualify_one(signo, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000478 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000479 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000480 if (strncasecmp(s, "SIG", 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000481 s += 3;
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100482 for (i = 0; i <= NSIG; i++) {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000483 if (strcasecmp(s, signame(i) + 3) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000484 qualify_one(i, bitflag, not, -1);
Roland McGrath76421df2005-02-02 03:51:18 +0000485 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000486 }
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100487 }
Roland McGrath76421df2005-02-02 03:51:18 +0000488 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000489}
490
491static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000492qual_desc(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000493{
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100494 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000495 int desc = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100496 if (desc < 0 || desc > 0x7fff) /* paranoia */
Roland McGrathfe6b3522005-02-02 04:40:11 +0000497 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000498 qualify_one(desc, bitflag, not, -1);
Roland McGrath2b619022003-04-10 18:58:20 +0000499 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000500 }
501 return -1;
502}
503
504static int
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000505lookup_class(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000506{
507 if (strcmp(s, "file") == 0)
508 return TRACE_FILE;
509 if (strcmp(s, "ipc") == 0)
510 return TRACE_IPC;
511 if (strcmp(s, "network") == 0)
512 return TRACE_NETWORK;
513 if (strcmp(s, "process") == 0)
514 return TRACE_PROCESS;
515 if (strcmp(s, "signal") == 0)
516 return TRACE_SIGNAL;
Roland McGrath2fe7b132005-07-05 03:25:35 +0000517 if (strcmp(s, "desc") == 0)
518 return TRACE_DESC;
Namhyung Kim96792962012-10-24 11:41:57 +0900519 if (strcmp(s, "memory") == 0)
520 return TRACE_MEMORY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000521 return -1;
522}
523
524void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000525qualify(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000526{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000527 const struct qual_options *opt;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000528 int not;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000529 char *copy;
530 const char *p;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000531 int i, n;
532
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100533 if (num_quals == 0)
534 reallocate_qual(MIN_QUALS);
535
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000536 opt = &qual_options[0];
537 for (i = 0; (p = qual_options[i].option_name); i++) {
538 n = strlen(p);
539 if (strncmp(s, p, n) == 0 && s[n] == '=') {
540 opt = &qual_options[i];
541 s += n + 1;
542 break;
543 }
544 }
545 not = 0;
546 if (*s == '!') {
547 not = 1;
548 s++;
549 }
550 if (strcmp(s, "none") == 0) {
551 not = 1 - not;
552 s = "all";
553 }
554 if (strcmp(s, "all") == 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100555 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000556 qualify_one(i, opt->bitflag, not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000557 }
558 return;
559 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100560 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000561 qualify_one(i, opt->bitflag, !not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000562 }
Denys Vlasenko5d645812011-08-20 12:48:18 +0200563 copy = strdup(s);
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200564 if (!copy)
565 die_out_of_memory();
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000566 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000567 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100568 unsigned pers;
569 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
570 for (i = 0; i < nsyscall_vec[pers]; i++)
571 if (sysent_vec[pers][i].sys_flags & n)
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000572 qualify_one(i, opt->bitflag, not, pers);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100573 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000574 continue;
575 }
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000576 if (opt->qualify(p, opt->bitflag, not)) {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100577 error_msg_and_die("invalid %s '%s'",
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000578 opt->argument_name, p);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000579 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000580 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000581 free(copy);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000582 return;
583}
584
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000585#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000586static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000587decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000588{
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000589 unsigned long addr;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100590 unsigned int i, n, size;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000591
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000592 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
593 return;
594
595 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100596 tcp->qual_flg = qual_flags[tcp->scno];
597 tcp->s_ent = &sysent[tcp->scno];
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000598 addr = tcp->u_arg[1];
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100599 size = current_wordsize;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100600 n = tcp->s_ent->nargs;
601 for (i = 0; i < n; ++i) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000602 if (size == sizeof(int)) {
603 unsigned int arg;
604 if (umove(tcp, addr, &arg) < 0)
605 arg = 0;
606 tcp->u_arg[i] = arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000607 }
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000608 else {
609 unsigned long arg;
610 if (umove(tcp, addr, &arg) < 0)
611 arg = 0;
612 tcp->u_arg[i] = arg;
613 }
614 addr += size;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000615 }
616}
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000617#endif
Mike Frysinger3362e892012-03-15 01:09:19 -0400618
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000619#ifdef SYS_ipc_subcall
620static void
621decode_ipc_subcall(struct tcb *tcp)
622{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100623 unsigned int i, n;
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000624
625 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
626 return;
627
628 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100629 tcp->qual_flg = qual_flags[tcp->scno];
630 tcp->s_ent = &sysent[tcp->scno];
631 n = tcp->s_ent->nargs;
632 for (i = 0; i < n; i++)
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000633 tcp->u_arg[i] = tcp->u_arg[i + 1];
634}
635#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000636
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200637int
638printargs(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000639{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200640 if (entering(tcp)) {
641 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100642 int n = tcp->s_ent->nargs;
643 for (i = 0; i < n; i++)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200644 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
645 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000646 return 0;
647}
648
Denys Vlasenko72879c62012-02-27 14:18:02 +0100649int
650printargs_lu(struct tcb *tcp)
651{
652 if (entering(tcp)) {
653 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100654 int n = tcp->s_ent->nargs;
655 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100656 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
657 }
658 return 0;
659}
660
661int
662printargs_ld(struct tcb *tcp)
663{
664 if (entering(tcp)) {
665 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100666 int n = tcp->s_ent->nargs;
667 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100668 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
669 }
670 return 0;
671}
672
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100673#if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200674long
675getrval2(struct tcb *tcp)
676{
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100677 long val;
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200678
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100679# if defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100680 val = sparc_regs.u_regs[U_REG_O1];
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100681# elif defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200682 if (upeek(tcp, 4*(REG_REG0+1), &val) < 0)
683 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100684# elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200685 if (upeek(tcp, PT_R9, &val) < 0)
686 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100687# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200688
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200689 return val;
690}
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100691#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200692
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200693int
694is_restart_error(struct tcb *tcp)
695{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200696 switch (tcp->u_error) {
697 case ERESTARTSYS:
698 case ERESTARTNOINTR:
699 case ERESTARTNOHAND:
700 case ERESTART_RESTARTBLOCK:
701 return 1;
702 default:
703 break;
704 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200705 return 0;
706}
707
Denys Vlasenko523635f2012-02-25 02:44:25 +0100708#if defined(I386)
Denys Vlasenko2550d482013-02-15 21:04:28 +0100709struct user_regs_struct i386_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000710# define ARCH_REGS_FOR_GETREGSET i386_regs
H.J. Lu35be5812012-04-16 13:00:01 +0200711#elif defined(X86_64) || defined(X32)
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100712/*
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100713 * On i386, pt_regs and user_regs_struct are the same,
714 * but on 64 bit x86, user_regs_struct has six more fields:
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100715 * fs_base, gs_base, ds, es, fs, gs.
716 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
717 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100718struct i386_user_regs_struct {
719 uint32_t ebx;
720 uint32_t ecx;
721 uint32_t edx;
722 uint32_t esi;
723 uint32_t edi;
724 uint32_t ebp;
725 uint32_t eax;
726 uint32_t xds;
727 uint32_t xes;
728 uint32_t xfs;
729 uint32_t xgs;
730 uint32_t orig_eax;
731 uint32_t eip;
732 uint32_t xcs;
733 uint32_t eflags;
734 uint32_t esp;
735 uint32_t xss;
736};
737static union {
738 struct user_regs_struct x86_64_r;
739 struct i386_user_regs_struct i386_r;
740} x86_regs_union;
741# define x86_64_regs x86_regs_union.x86_64_r
742# define i386_regs x86_regs_union.i386_r
743static struct iovec x86_io = {
744 .iov_base = &x86_regs_union
745};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100746#elif defined(IA64)
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200747long ia32 = 0; /* not static */
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100748static long ia64_r8, ia64_r10;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100749#elif defined(POWERPC)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +0100750static long ppc_result;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100751#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100752static long m68k_d0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100753#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100754static long bfin_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100755#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100756struct pt_regs arm_regs; /* not static */
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000757# define ARCH_REGS_FOR_GETREGSET arm_regs
Steve McIntyred8d3bd32012-10-24 17:58:16 +0100758#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100759static union {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100760 struct user_pt_regs aarch64_r;
761 struct arm_pt_regs arm_r;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100762} arm_regs_union;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100763# define aarch64_regs arm_regs_union.aarch64_r
764# define arm_regs arm_regs_union.arm_r
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100765static struct iovec aarch64_io = {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100766 .iov_base = &arm_regs_union
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100767};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100768#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100769static long alpha_r0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100770static long alpha_a3;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100771#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100772static struct pt_regs avr32_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100773#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100774struct pt_regs sparc_regs; /* not static */
Denys Vlasenko523635f2012-02-25 02:44:25 +0100775#elif defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100776static long long mips_a3;
777static long long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100778#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100779static long mips_a3;
780static long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100781#elif defined(S390) || defined(S390X)
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200782static long gpr2;
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200783static long syscall_mode;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100784#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100785static long hppa_r28;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100786#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100787static long sh_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100788#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100789static long sh64_r9;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100790#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100791static long cris_r10;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100792#elif defined(TILE)
793struct pt_regs tile_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100794#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100795static long microblaze_r3;
Christian Svensson492f81f2013-02-14 13:26:27 +0100796#elif defined(OR1K)
797static struct user_regs_struct or1k_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000798# define ARCH_REGS_FOR_GETREGSET or1k_regs
James Hogan5f999a82013-02-22 14:44:10 +0000799#elif defined(METAG)
800static struct user_gp_regs metag_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000801# define ARCH_REGS_FOR_GETREGSET metag_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +0100802#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000803
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100804void
805printcall(struct tcb *tcp)
806{
807#define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
808 sizeof(long) == 8 ? "[????????????????] " : \
809 NULL /* crash */)
810 if (get_regs_error) {
811 PRINTBADPC;
812 return;
813 }
814#if defined(I386)
815 tprintf("[%08lx] ", i386_regs.eip);
816#elif defined(S390) || defined(S390X)
817 long psw;
818 if (upeek(tcp, PT_PSWADDR, &psw) < 0) {
819 PRINTBADPC;
820 return;
821 }
822# ifdef S390
823 tprintf("[%08lx] ", psw);
824# elif S390X
Dmitry V. Levinddba73e2013-02-05 19:01:58 +0000825 tprintf("[%016lx] ", psw);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100826# endif
827#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100828 if (x86_io.iov_len == sizeof(i386_regs)) {
829 tprintf("[%08x] ", (unsigned) i386_regs.eip);
830 } else {
831# if defined(X86_64)
832 tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
833# elif defined(X32)
834 /* Note: this truncates 64-bit rip to 32 bits */
835 tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip);
836# endif
837 }
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100838#elif defined(IA64)
839 long ip;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100840 if (upeek(tcp, PT_B0, &ip) < 0) {
841 PRINTBADPC;
842 return;
843 }
844 tprintf("[%08lx] ", ip);
845#elif defined(POWERPC)
846 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100847 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) {
848 PRINTBADPC;
849 return;
850 }
851# ifdef POWERPC64
852 tprintf("[%016lx] ", pc);
853# else
854 tprintf("[%08lx] ", pc);
855# endif
856#elif defined(M68K)
857 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100858 if (upeek(tcp, 4*PT_PC, &pc) < 0) {
859 tprints("[????????] ");
860 return;
861 }
862 tprintf("[%08lx] ", pc);
863#elif defined(ALPHA)
864 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100865 if (upeek(tcp, REG_PC, &pc) < 0) {
866 tprints("[????????????????] ");
867 return;
868 }
869 tprintf("[%08lx] ", pc);
870#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100871 tprintf("[%08lx] ", sparc_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100872#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100873 tprintf("[%08lx] ", sparc_regs.tpc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100874#elif defined(HPPA)
875 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100876 if (upeek(tcp, PT_IAOQ0, &pc) < 0) {
877 tprints("[????????] ");
878 return;
879 }
880 tprintf("[%08lx] ", pc);
881#elif defined(MIPS)
882 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100883 if (upeek(tcp, REG_EPC, &pc) < 0) {
884 tprints("[????????] ");
885 return;
886 }
887 tprintf("[%08lx] ", pc);
888#elif defined(SH)
889 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100890 if (upeek(tcp, 4*REG_PC, &pc) < 0) {
891 tprints("[????????] ");
892 return;
893 }
894 tprintf("[%08lx] ", pc);
895#elif defined(SH64)
896 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100897 if (upeek(tcp, REG_PC, &pc) < 0) {
898 tprints("[????????????????] ");
899 return;
900 }
901 tprintf("[%08lx] ", pc);
902#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100903 tprintf("[%08lx] ", arm_regs.ARM_pc);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100904#elif defined(AARCH64)
905 /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100906#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100907 tprintf("[%08lx] ", avr32_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100908#elif defined(BFIN)
909 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100910 if (upeek(tcp, PT_PC, &pc) < 0) {
911 PRINTBADPC;
912 return;
913 }
914 tprintf("[%08lx] ", pc);
915#elif defined(CRISV10)
916 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100917 if (upeek(tcp, 4*PT_IRP, &pc) < 0) {
918 PRINTBADPC;
919 return;
920 }
921 tprintf("[%08lx] ", pc);
922#elif defined(CRISV32)
923 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100924 if (upeek(tcp, 4*PT_ERP, &pc) < 0) {
925 PRINTBADPC;
926 return;
927 }
928 tprintf("[%08lx] ", pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100929#elif defined(TILE)
930# ifdef _LP64
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500931 tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100932# else
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500933 tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100934# endif
Christian Svensson492f81f2013-02-14 13:26:27 +0100935#elif defined(OR1K)
936 tprintf("[%08lx] ", or1k_regs.pc);
James Hogan5f999a82013-02-22 14:44:10 +0000937#elif defined(METAG)
938 tprintf("[%08lx] ", metag_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100939#endif /* architecture */
940}
941
Denys Vlasenko7270de52013-02-21 15:46:34 +0100942/* Shuffle syscall numbers so that we don't have huge gaps in syscall table.
943 * The shuffling should be reversible: shuffle_scno(shuffle_scno(n)) == n.
944 */
945#if defined(ARM) /* So far only ARM needs this */
946static long
947shuffle_scno(unsigned long scno)
948{
949 if (scno <= ARM_LAST_ORDINARY_SYSCALL)
950 return scno;
951
952 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
953 if (scno == 0x000ffff0)
954 return ARM_LAST_ORDINARY_SYSCALL+1;
955 if (scno == ARM_LAST_ORDINARY_SYSCALL+1)
956 return 0x000ffff0;
957
958 /* Is it ARM specific syscall?
959 * Swap with [LAST_ORDINARY+2, LAST_ORDINARY+2 + LAST_SPECIAL] range.
960 */
961 if (scno >= 0x000f0000
962 && scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL
963 ) {
964 return scno - 0x000f0000 + (ARM_LAST_ORDINARY_SYSCALL+2);
965 }
966 if (/* scno >= ARM_LAST_ORDINARY_SYSCALL+2 - always true */ 1
967 && scno <= (ARM_LAST_ORDINARY_SYSCALL+2) + ARM_LAST_SPECIAL_SYSCALL
968 ) {
969 return scno + 0x000f0000 - (ARM_LAST_ORDINARY_SYSCALL+2);
970 }
971
972 return scno;
973}
974#else
975# define shuffle_scno(scno) ((long)(scno))
976#endif
977
978static char*
979undefined_scno_name(struct tcb *tcp)
980{
981 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
982
983 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
984 return buf;
985}
986
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100987#ifndef get_regs
988long get_regs_error;
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +0000989
990#if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +0000991static void get_regset(pid_t pid)
992{
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000993/* constant iovec */
994# if defined(ARM) \
995 || defined(I386) \
996 || defined(METAG) \
997 || defined(OR1K)
998 static struct iovec io = {
999 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1000 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001001 };
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001002 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1003
1004/* variable iovec */
1005# elif defined(X86_64) || defined(X32)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001006 /* x86_io.iov_base = &x86_regs_union; - already is */
1007 x86_io.iov_len = sizeof(x86_regs_union);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001008 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &x86_io);
1009# elif defined(AARCH64)
1010 /* aarch64_io.iov_base = &arm_regs_union; - already is */
1011 aarch64_io.iov_len = sizeof(arm_regs_union);
1012 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &aarch64_io);
1013# else
1014# warning both PTRACE_GETREGSET and NT_PRSTATUS are available but not yet used
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001015# endif
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001016}
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001017#endif /* PTRACE_GETREGSET && NT_PRSTATUS */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001018
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001019void
1020get_regs(pid_t pid)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001021{
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001022/* PTRACE_GETREGSET only */
1023# if defined(METAG) || defined(OR1K) || defined(X32) || defined(AARCH64)
1024 get_regset(pid);
1025
1026/* PTRACE_GETREGS only */
1027# elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001028 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001029# elif defined(TILE)
1030 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &tile_regs);
1031# elif defined(SPARC) || defined(SPARC64)
1032 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
1033
1034/* try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS */
1035# else
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001036# if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
1037 static int getregset_support;
1038
1039 if (getregset_support >= 0) {
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001040 get_regset(pid);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001041 if (getregset_support > 0)
1042 return;
1043 if (get_regs_error >= 0) {
1044 getregset_support = 1;
1045 return;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001046 }
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001047 if (errno == EPERM || errno == ESRCH)
1048 return;
1049 getregset_support = -1;
1050 }
1051# endif /* PTRACE_GETREGSET && NT_PRSTATUS */
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001052# if defined(ARM)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001053 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &arm_regs);
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001054# elif defined(I386)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001055 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &i386_regs);
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001056# elif defined(X86_64)
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001057 /* Use old method, with unreliable heuristical detection of 32-bitness. */
1058 x86_io.iov_len = sizeof(x86_64_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001059 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &x86_64_regs);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001060 if (!get_regs_error && x86_64_regs.cs == 0x23) {
1061 x86_io.iov_len = sizeof(i386_regs);
1062 /*
1063 * The order is important: i386_regs and x86_64_regs
1064 * are overlaid in memory!
1065 */
1066 i386_regs.ebx = x86_64_regs.rbx;
1067 i386_regs.ecx = x86_64_regs.rcx;
1068 i386_regs.edx = x86_64_regs.rdx;
1069 i386_regs.esi = x86_64_regs.rsi;
1070 i386_regs.edi = x86_64_regs.rdi;
1071 i386_regs.ebp = x86_64_regs.rbp;
1072 i386_regs.eax = x86_64_regs.rax;
1073 /* i386_regs.xds = x86_64_regs.ds; unused by strace */
1074 /* i386_regs.xes = x86_64_regs.es; ditto... */
1075 /* i386_regs.xfs = x86_64_regs.fs; */
1076 /* i386_regs.xgs = x86_64_regs.gs; */
1077 i386_regs.orig_eax = x86_64_regs.orig_rax;
1078 i386_regs.eip = x86_64_regs.rip;
1079 /* i386_regs.xcs = x86_64_regs.cs; */
1080 /* i386_regs.eflags = x86_64_regs.eflags; */
1081 i386_regs.esp = x86_64_regs.rsp;
1082 /* i386_regs.xss = x86_64_regs.ss; */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001083 }
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001084# else
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001085# error unhandled architecture
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001086# endif /* ARM || I386 || X86_64 */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001087# endif
1088}
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001089#endif /* !get_regs */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001090
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001091/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001092 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1093 * 1: ok, continue in trace_syscall_entering().
1094 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001095 * ("????" etc) and bail out.
1096 */
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01001097static int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001098get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001099{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001100 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001101
Denys Vlasenko523635f2012-02-25 02:44:25 +01001102#if defined(S390) || defined(S390X)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001103 if (upeek(tcp, PT_GPR2, &syscall_mode) < 0)
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001104 return -1;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001105
1106 if (syscall_mode != -ENOSYS) {
1107 /*
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001108 * Since kernel version 2.5.44 the scno gets passed in gpr2.
Roland McGrath2f924ca2003-06-26 22:23:28 +00001109 */
1110 scno = syscall_mode;
1111 } else {
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001112 /*
Michal Ludvig882eda82002-11-11 12:50:47 +00001113 * Old style of "passing" the scno via the SVC instruction.
1114 */
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001115 long psw;
Michal Ludvig882eda82002-11-11 12:50:47 +00001116 long opcode, offset_reg, tmp;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001117 void *svc_addr;
Denys Vlasenko7c9ba8b2011-08-19 19:46:32 +02001118 static const int gpr_offset[16] = {
1119 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
1120 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
1121 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
1122 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
1123 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001124
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001125 if (upeek(tcp, PT_PSWADDR, &psw) < 0)
Michal Ludvig882eda82002-11-11 12:50:47 +00001126 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001127 errno = 0;
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001128 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +00001129 if (errno) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001130 perror_msg("peektext(psw-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +00001131 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001132 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001133
1134 /*
1135 * We have to check if the SVC got executed directly or via an
1136 * EXECUTE instruction. In case of EXECUTE it is necessary to do
1137 * instruction decoding to derive the system call number.
1138 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1139 * so that this doesn't work if a SVC opcode is part of an EXECUTE
1140 * opcode. Since there is no way to find out the opcode size this
1141 * is the best we can do...
1142 */
Michal Ludvig882eda82002-11-11 12:50:47 +00001143 if ((opcode & 0xff00) == 0x0a00) {
1144 /* SVC opcode */
1145 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +00001146 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001147 else {
1148 /* SVC got executed by EXECUTE instruction */
1149
1150 /*
1151 * Do instruction decoding of EXECUTE. If you really want to
1152 * understand this, read the Principles of Operations.
1153 */
1154 svc_addr = (void *) (opcode & 0xfff);
1155
1156 tmp = 0;
1157 offset_reg = (opcode & 0x000f0000) >> 16;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001158 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001159 return -1;
1160 svc_addr += tmp;
1161
1162 tmp = 0;
1163 offset_reg = (opcode & 0x0000f000) >> 12;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001164 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001165 return -1;
1166 svc_addr += tmp;
1167
Denys Vlasenkofb036672009-01-23 16:30:26 +00001168 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
Michal Ludvig882eda82002-11-11 12:50:47 +00001169 if (errno)
1170 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001171# if defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +00001172 scno >>= 48;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001173# else
Michal Ludvig882eda82002-11-11 12:50:47 +00001174 scno >>= 16;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001175# endif
Michal Ludvig882eda82002-11-11 12:50:47 +00001176 tmp = 0;
1177 offset_reg = (opcode & 0x00f00000) >> 20;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001178 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001179 return -1;
1180
1181 scno = (scno | tmp) & 0xff;
1182 }
1183 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001184#elif defined(POWERPC)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001185 if (upeek(tcp, sizeof(unsigned long)*PT_R0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001186 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001187# ifdef POWERPC64
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001188 /* TODO: speed up strace by not doing this at every syscall.
1189 * We only need to do it after execve.
1190 */
1191 int currpers;
1192 long val;
Andreas Schwabd69fa492010-07-12 21:39:57 +02001193
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001194 /* Check for 64/32 bit mode. */
1195 if (upeek(tcp, sizeof(unsigned long)*PT_MSR, &val) < 0)
1196 return -1;
1197 /* SF is bit 0 of MSR */
1198 if (val < 0)
1199 currpers = 0;
1200 else
1201 currpers = 1;
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001202 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001203# endif
1204#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001205 scno = avr32_regs.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001206#elif defined(BFIN)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001207 if (upeek(tcp, PT_ORIG_P0, &scno))
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001208 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001209#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001210 scno = i386_regs.orig_eax;
H.J. Lu35be5812012-04-16 13:00:01 +02001211#elif defined(X86_64) || defined(X32)
1212# ifndef __X32_SYSCALL_BIT
1213# define __X32_SYSCALL_BIT 0x40000000
1214# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001215 int currpers;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001216# if 1
1217 /* GETREGSET of NT_PRSTATUS tells us regset size,
1218 * which unambiguously detects i386.
1219 *
1220 * Linux kernel distinguishes x86-64 and x32 processes
1221 * solely by looking at __X32_SYSCALL_BIT:
1222 * arch/x86/include/asm/compat.h::is_x32_task():
1223 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1224 * return true;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001225 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001226 if (x86_io.iov_len == sizeof(i386_regs)) {
1227 scno = i386_regs.orig_eax;
1228 currpers = 1;
1229 } else {
1230 scno = x86_64_regs.orig_rax;
1231 currpers = 0;
1232 if (scno & __X32_SYSCALL_BIT) {
1233 scno -= __X32_SYSCALL_BIT;
1234 currpers = 2;
1235 }
1236 }
1237# elif 0
1238 /* cs = 0x33 for long mode (native 64 bit and x32)
1239 * cs = 0x23 for compatibility mode (32 bit)
1240 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1241 */
1242 scno = x86_64_regs.orig_rax;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001243 switch (x86_64_regs.cs) {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001244 case 0x23: currpers = 1; break;
H.J. Lu35be5812012-04-16 13:00:01 +02001245 case 0x33:
1246 if (x86_64_regs.ds == 0x2b) {
1247 currpers = 2;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001248 scno &= ~__X32_SYSCALL_BIT;
H.J. Lu35be5812012-04-16 13:00:01 +02001249 } else
1250 currpers = 0;
1251 break;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001252 default:
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001253 fprintf(stderr, "Unknown value CS=0x%08X while "
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001254 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001255 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001256 currpers = current_personality;
1257 break;
1258 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001259# elif 0
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001260 /* This version analyzes the opcode of a syscall instruction.
1261 * (int 0x80 on i386 vs. syscall on x86-64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001262 * It works, but is too complicated, and strictly speaking, unreliable.
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001263 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001264 unsigned long call, rip = x86_64_regs.rip;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001265 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1266 rip -= 2;
1267 errno = 0;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001268 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001269 if (errno)
1270 fprintf(stderr, "ptrace_peektext failed: %s\n",
1271 strerror(errno));
1272 switch (call & 0xffff) {
1273 /* x86-64: syscall = 0x0f 0x05 */
1274 case 0x050f: currpers = 0; break;
1275 /* i386: int 0x80 = 0xcd 0x80 */
1276 case 0x80cd: currpers = 1; break;
1277 default:
1278 currpers = current_personality;
1279 fprintf(stderr,
1280 "Unknown syscall opcode (0x%04X) while "
1281 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001282 "PID=%d\n", (int)call, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001283 break;
1284 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001285# endif
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001286
H.J. Lu35be5812012-04-16 13:00:01 +02001287# ifdef X32
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001288 /* If we are built for a x32 system, then personality 0 is x32
1289 * (not x86_64), and stracing of x86_64 apps is not supported.
1290 * Stracing of i386 apps is still supported.
H.J. Lu085e4282012-04-17 11:05:04 -07001291 */
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001292 if (currpers == 0) {
1293 fprintf(stderr, "syscall_%lu(...) in unsupported "
1294 "64-bit mode of process PID=%d\n",
1295 scno, tcp->pid);
1296 return 0;
H.J. Lu35be5812012-04-16 13:00:01 +02001297 }
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001298 currpers &= ~2; /* map 2,1 to 0,1 */
H.J. Lu35be5812012-04-16 13:00:01 +02001299# endif
H.J. Lu085e4282012-04-17 11:05:04 -07001300 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001301#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001302# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01001303 long psr;
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001304 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001305 ia32 = (psr & IA64_PSR_IS) != 0;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001306 if (ia32) {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001307 if (upeek(tcp, PT_R1, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001308 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001309 } else {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001310 if (upeek(tcp, PT_R15, &scno) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001311 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001312 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001313#elif defined(AARCH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001314 switch (aarch64_io.iov_len) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001315 case sizeof(aarch64_regs):
1316 /* We are in 64-bit mode */
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001317 scno = aarch64_regs.regs[8];
1318 update_personality(tcp, 1);
1319 break;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001320 case sizeof(arm_regs):
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001321 /* We are in 32-bit mode */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001322 scno = arm_regs.ARM_r7;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001323 update_personality(tcp, 0);
1324 break;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001325 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001326#elif defined(ARM)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001327 if (arm_regs.ARM_ip != 0) {
1328 /* It is not a syscall entry */
1329 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001330 tcp->flags |= TCB_INSYSCALL;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001331 return 0;
1332 }
1333 /* Note: we support only 32-bit CPUs, not 26-bit */
1334
1335 if (arm_regs.ARM_cpsr & 0x20) {
1336 /* Thumb mode */
1337 scno = arm_regs.ARM_r7;
1338 } else {
1339 /* ARM mode */
1340 errno = 0;
1341 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1342 if (errno)
1343 return -1;
1344
1345 /* EABI syscall convention? */
1346 if (scno == 0xef000000) {
1347 scno = arm_regs.ARM_r7; /* yes */
1348 } else {
1349 if ((scno & 0x0ff00000) != 0x0f900000) {
1350 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1351 tcp->pid, scno);
1352 return -1;
1353 }
1354 /* Fixup the syscall number */
1355 scno &= 0x000fffff;
1356 }
1357 }
Denys Vlasenko7270de52013-02-21 15:46:34 +01001358
1359 scno = shuffle_scno(scno);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001360#elif defined(M68K)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001361 if (upeek(tcp, 4*PT_ORIG_D0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001362 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001363#elif defined(LINUX_MIPSN32)
Roland McGrath542c2c62008-05-20 01:11:56 +00001364 unsigned long long regs[38];
1365
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001366 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001367 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001368 mips_a3 = regs[REG_A3];
1369 mips_r2 = regs[REG_V0];
Roland McGrath542c2c62008-05-20 01:11:56 +00001370
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001371 scno = mips_r2;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001372 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001373 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001374 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001375 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001376 return 0;
1377 }
Roland McGrath542c2c62008-05-20 01:11:56 +00001378 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001379#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001380 if (upeek(tcp, REG_A3, &mips_a3) < 0)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001381 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001382 if (upeek(tcp, REG_V0, &scno) < 0)
1383 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001384
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001385 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001386 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001387 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001388 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001389 return 0;
1390 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001391 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001392#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001393 if (upeek(tcp, REG_A3, &alpha_a3) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001394 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001395 if (upeek(tcp, REG_R0, &scno) < 0)
1396 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001397
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001398 /*
1399 * Do some sanity checks to figure out if it's
1400 * really a syscall entry
1401 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001402 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001403 if (alpha_a3 == 0 || alpha_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001404 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001405 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001406 return 0;
1407 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001408 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001409#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001410 /* Disassemble the syscall trap. */
1411 /* Retrieve the syscall trap instruction. */
Denys Vlasenko46455822013-02-05 17:02:59 +01001412 unsigned long trap;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001413 errno = 0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001414# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001415 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001416 trap >>= 32;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001417# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001418 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001419# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001420 if (errno)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001421 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001422
1423 /* Disassemble the trap to see what personality to use. */
1424 switch (trap) {
1425 case 0x91d02010:
1426 /* Linux/SPARC syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001427 update_personality(tcp, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001428 break;
1429 case 0x91d0206d:
1430 /* Linux/SPARC64 syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001431 update_personality(tcp, 2);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001432 break;
1433 case 0x91d02000:
1434 /* SunOS syscall trap. (pers 1) */
1435 fprintf(stderr, "syscall: SunOS no support\n");
1436 return -1;
1437 case 0x91d02008:
1438 /* Solaris 2.x syscall trap. (per 2) */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001439 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001440 break;
1441 case 0x91d02009:
1442 /* NetBSD/FreeBSD syscall trap. */
1443 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1444 return -1;
1445 case 0x91d02027:
1446 /* Solaris 2.x gettimeofday */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001447 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001448 break;
1449 default:
Denys Vlasenko523635f2012-02-25 02:44:25 +01001450# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001451 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001452# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001453 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001454# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001455 return -1;
1456 }
1457
1458 /* Extract the system call number from the registers. */
1459 if (trap == 0x91d02027)
1460 scno = 156;
1461 else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001462 scno = sparc_regs.u_regs[U_REG_G1];
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001463 if (scno == 0) {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001464 scno = sparc_regs.u_regs[U_REG_O0];
1465 memmove(&sparc_regs.u_regs[U_REG_O0], &sparc_regs.u_regs[U_REG_O1], 7*sizeof(sparc_regs.u_regs[0]));
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001466 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001467#elif defined(HPPA)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001468 if (upeek(tcp, PT_GR20, &scno) < 0)
1469 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001470#elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001471 /*
1472 * In the new syscall ABI, the system call number is in R3.
1473 */
1474 if (upeek(tcp, 4*(REG_REG0+3), &scno) < 0)
1475 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001476
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001477 if (scno < 0) {
1478 /* Odd as it may seem, a glibc bug has been known to cause
1479 glibc to issue bogus negative syscall numbers. So for
1480 our purposes, make strace print what it *should* have been */
1481 long correct_scno = (scno & 0xff);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001482 if (debug_flag)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001483 fprintf(stderr,
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001484 "Detected glibc bug: bogus system call"
1485 " number = %ld, correcting to %ld\n",
1486 scno,
1487 correct_scno);
1488 scno = correct_scno;
1489 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001490#elif defined(SH64)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001491 if (upeek(tcp, REG_SYSCALL, &scno) < 0)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001492 return -1;
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001493 scno &= 0xFFFF;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001494#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001495 if (upeek(tcp, 4*PT_R9, &scno) < 0)
1496 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001497#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001498 int currpers;
1499 scno = tile_regs.regs[10];
1500# ifdef __tilepro__
1501 currpers = 1;
1502# else
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001503# ifndef PT_FLAGS_COMPAT
1504# define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1505# endif
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001506 if (tile_regs.flags & PT_FLAGS_COMPAT)
1507 currpers = 1;
1508 else
1509 currpers = 0;
1510# endif
1511 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001512#elif defined(MICROBLAZE)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001513 if (upeek(tcp, 0, &scno) < 0)
1514 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001515#elif defined(OR1K)
1516 scno = or1k_regs.gpr[11];
James Hogan5f999a82013-02-22 14:44:10 +00001517#elif defined(METAG)
1518 scno = metag_regs.dx[0][1]; /* syscall number in D1Re0 (D1.0) */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001519#endif
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001520
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001521 tcp->scno = scno;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001522 if (SCNO_IS_VALID(tcp->scno)) {
1523 tcp->s_ent = &sysent[scno];
1524 tcp->qual_flg = qual_flags[scno];
1525 } else {
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01001526 static const struct_sysent unknown = {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001527 .nargs = MAX_ARGS,
1528 .sys_flags = 0,
1529 .sys_func = printargs,
1530 .sys_name = "unknown", /* not used */
1531 };
1532 tcp->s_ent = &unknown;
1533 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1534 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001535 return 1;
1536}
1537
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02001538/* Called at each syscall entry.
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001539 * Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001540 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1541 * 1: ok, continue in trace_syscall_entering().
1542 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001543 * ("????" etc) and bail out.
1544 */
Roland McGratha4d48532005-06-08 20:45:28 +00001545static int
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001546syscall_fixup_on_sysenter(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001547{
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001548 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001549#if defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001550 if (i386_regs.eax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001551 if (debug_flag)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001552 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1553 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001554 }
H.J. Lu35be5812012-04-16 13:00:01 +02001555#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001556 {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001557 long rax;
1558 if (x86_io.iov_len == sizeof(i386_regs)) {
1559 /* Sign extend from 32 bits */
1560 rax = (int32_t)i386_regs.eax;
1561 } else {
1562 /* Note: in X32 build, this truncates 64 to 32 bits */
1563 rax = x86_64_regs.rax;
1564 }
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001565 if (rax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001566 if (debug_flag)
Denys Vlasenko18beb982011-08-24 16:59:23 +02001567 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1568 return 0;
1569 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001570 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001571#elif defined(S390) || defined(S390X)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001572 /* TODO: we already fetched PT_GPR2 in get_scno
1573 * and stored it in syscall_mode, reuse it here
1574 * instead of re-fetching?
1575 */
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001576 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001577 return -1;
Michal Ludvig882eda82002-11-11 12:50:47 +00001578 if (syscall_mode != -ENOSYS)
1579 syscall_mode = tcp->scno;
Denys Vlasenkoece98792011-08-25 10:25:35 +02001580 if (gpr2 != syscall_mode) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001581 if (debug_flag)
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001582 fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2);
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001583 return 0;
1584 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001585#elif defined(M68K)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001586 /* TODO? Eliminate upeek's in arches below like we did in x86 */
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001587 if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001588 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001589 if (m68k_d0 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001590 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001591 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001592 return 0;
1593 }
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001594#elif defined(IA64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001595 if (upeek(tcp, PT_R10, &ia64_r10) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001596 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001597 if (upeek(tcp, PT_R8, &ia64_r8) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001598 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001599 if (ia32 && ia64_r8 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001600 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001601 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001602 return 0;
1603 }
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001604#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001605 if (upeek(tcp, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001606 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001607 if (cris_r10 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001608 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001609 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001610 return 0;
1611 }
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001612#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001613 if (upeek(tcp, 3 * 4, &microblaze_r3) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001614 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001615 if (microblaze_r3 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001616 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001617 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001618 return 0;
1619 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001620#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001621 return 1;
1622}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001623
Denys Vlasenko146b9442012-03-18 22:10:48 +01001624static void
1625internal_fork(struct tcb *tcp)
1626{
1627#if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1628# define ARG_FLAGS 1
1629#else
1630# define ARG_FLAGS 0
1631#endif
1632#ifndef CLONE_UNTRACED
1633# define CLONE_UNTRACED 0x00800000
1634#endif
1635 if ((ptrace_setoptions
1636 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1637 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1638 return;
1639
1640 if (!followfork)
1641 return;
1642
1643 if (entering(tcp)) {
1644 /*
1645 * We won't see the new child if clone is called with
1646 * CLONE_UNTRACED, so we keep the same logic with that option
1647 * and don't trace it.
1648 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001649 if ((tcp->s_ent->sys_func == sys_clone)
1650 && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
1651 )
Denys Vlasenko146b9442012-03-18 22:10:48 +01001652 return;
1653 setbpt(tcp);
1654 } else {
1655 if (tcp->flags & TCB_BPTSET)
1656 clearbpt(tcp);
1657 }
1658}
1659
1660#if defined(TCB_WAITEXECVE)
1661static void
1662internal_exec(struct tcb *tcp)
1663{
1664 /* Maybe we have post-execve SIGTRAP suppressed? */
1665 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1666 return; /* yes, no need to do anything */
1667
1668 if (exiting(tcp) && syserror(tcp))
1669 /* Error in execve, no post-execve SIGTRAP expected */
1670 tcp->flags &= ~TCB_WAITEXECVE;
1671 else
1672 tcp->flags |= TCB_WAITEXECVE;
1673}
1674#endif
1675
1676static void
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001677syscall_fixup_for_fork_exec(struct tcb *tcp)
Roland McGrathc1e45922008-05-27 23:18:29 +00001678{
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001679 /*
1680 * We must always trace a few critical system calls in order to
1681 * correctly support following forks in the presence of tracing
1682 * qualifiers.
1683 */
1684 int (*func)();
1685
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001686 func = tcp->s_ent->sys_func;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001687
1688 if ( sys_fork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001689 || sys_vfork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001690 || sys_clone == func
Denys Vlasenko146b9442012-03-18 22:10:48 +01001691 ) {
1692 internal_fork(tcp);
1693 return;
1694 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001695
Denys Vlasenko84703742012-02-25 02:38:52 +01001696#if defined(TCB_WAITEXECVE)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001697 if ( sys_execve == func
Denys Vlasenko84703742012-02-25 02:38:52 +01001698# if defined(SPARC) || defined(SPARC64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001699 || sys_execv == func
Denys Vlasenkoa7949742011-08-21 17:26:55 +02001700# endif
Denys Vlasenko146b9442012-03-18 22:10:48 +01001701 ) {
1702 internal_exec(tcp);
1703 return;
1704 }
Roland McGrathc1e45922008-05-27 23:18:29 +00001705#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001706}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001707
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001708/* Return -1 on error or 1 on success (never 0!) */
Roland McGratha4d48532005-06-08 20:45:28 +00001709static int
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001710get_syscall_args(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001711{
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001712 int i, nargs;
1713
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001714 nargs = tcp->s_ent->nargs;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001715
Denys Vlasenko523635f2012-02-25 02:44:25 +01001716#if defined(S390) || defined(S390X)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001717 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001718 if (upeek(tcp, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
1719 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001720#elif defined(ALPHA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001721 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001722 if (upeek(tcp, REG_A0+i, &tcp->u_arg[i]) < 0)
1723 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001724#elif defined(IA64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001725 if (!ia32) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001726 unsigned long *out0, cfm, sof, sol;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001727 long rbs_end;
1728 /* be backwards compatible with kernel < 2.4.4... */
1729# ifndef PT_RBS_END
1730# define PT_RBS_END PT_AR_BSP
1731# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001732
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001733 if (upeek(tcp, PT_RBS_END, &rbs_end) < 0)
1734 return -1;
1735 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001736 return -1;
1737
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001738 sof = (cfm >> 0) & 0x7f;
1739 sol = (cfm >> 7) & 0x7f;
1740 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1741
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001742 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001743 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1744 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1745 return -1;
1746 }
1747 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001748 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1749 PT_R9 /* ECX = out1 */,
1750 PT_R10 /* EDX = out2 */,
1751 PT_R14 /* ESI = out3 */,
1752 PT_R15 /* EDI = out4 */,
1753 PT_R13 /* EBP = out5 */};
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001754
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001755 for (i = 0; i < nargs; ++i) {
1756 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1757 return -1;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001758 /* truncate away IVE sign-extension */
1759 tcp->u_arg[i] &= 0xffffffff;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001760 }
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001761 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001762#elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001763 /* N32 and N64 both use up to six registers. */
1764 unsigned long long regs[38];
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001765
1766 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1767 return -1;
1768
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001769 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001770 tcp->u_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001771# if defined(LINUX_MIPSN32)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001772 tcp->ext_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001773# endif
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001774 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001775#elif defined(MIPS)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001776 if (nargs > 4) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001777 long sp;
1778
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001779 if (upeek(tcp, REG_SP, &sp) < 0)
1780 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001781 for (i = 0; i < 4; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001782 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1783 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001784 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001785 (char *)(tcp->u_arg + 4));
1786 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001787 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001788 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001789 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001790 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001791#elif defined(POWERPC)
1792# ifndef PT_ORIG_R3
1793# define PT_ORIG_R3 34
1794# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001795 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001796 if (upeek(tcp, (i==0) ?
1797 (sizeof(unsigned long) * PT_ORIG_R3) :
1798 ((i+PT_R3) * sizeof(unsigned long)),
1799 &tcp->u_arg[i]) < 0)
1800 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001801 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001802#elif defined(SPARC) || defined(SPARC64)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001803 for (i = 0; i < nargs; ++i)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001804 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001805#elif defined(HPPA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001806 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001807 if (upeek(tcp, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1808 return -1;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001809#elif defined(ARM) || defined(AARCH64)
1810# if defined(AARCH64)
1811 if (tcp->currpers == 1)
1812 for (i = 0; i < nargs; ++i)
1813 tcp->u_arg[i] = aarch64_regs.regs[i];
1814 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001815# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001816 for (i = 0; i < nargs; ++i)
Denys Vlasenko401374e2013-02-06 18:24:39 +01001817 tcp->u_arg[i] = arm_regs.uregs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001818#elif defined(AVR32)
Denys Vlasenkob5b25892011-08-30 19:04:54 +02001819 (void)i;
1820 (void)nargs;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001821 tcp->u_arg[0] = avr32_regs.r12;
1822 tcp->u_arg[1] = avr32_regs.r11;
1823 tcp->u_arg[2] = avr32_regs.r10;
1824 tcp->u_arg[3] = avr32_regs.r9;
1825 tcp->u_arg[4] = avr32_regs.r5;
1826 tcp->u_arg[5] = avr32_regs.r3;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001827#elif defined(BFIN)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001828 static const int argreg[MAX_ARGS] = { PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5 };
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001829
Denys Vlasenko4b887a52011-08-23 13:32:38 +02001830 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001831 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1832 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001833#elif defined(SH)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001834 static const int syscall_regs[MAX_ARGS] = {
1835 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1836 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001837 };
1838
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001839 for (i = 0; i < nargs; ++i)
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001840 if (upeek(tcp, syscall_regs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001841 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001842#elif defined(SH64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001843 int i;
1844 /* Registers used by SH5 Linux system calls for parameters */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001845 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001846
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001847 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001848 if (upeek(tcp, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1849 return -1;
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001850#elif defined(I386)
1851 (void)i;
1852 (void)nargs;
1853 tcp->u_arg[0] = i386_regs.ebx;
1854 tcp->u_arg[1] = i386_regs.ecx;
1855 tcp->u_arg[2] = i386_regs.edx;
1856 tcp->u_arg[3] = i386_regs.esi;
1857 tcp->u_arg[4] = i386_regs.edi;
1858 tcp->u_arg[5] = i386_regs.ebp;
H.J. Lu35be5812012-04-16 13:00:01 +02001859#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001860 (void)i;
1861 (void)nargs;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001862 if (x86_io.iov_len != sizeof(i386_regs)) {
1863 /* x86-64 or x32 ABI */
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001864 tcp->u_arg[0] = x86_64_regs.rdi;
1865 tcp->u_arg[1] = x86_64_regs.rsi;
1866 tcp->u_arg[2] = x86_64_regs.rdx;
1867 tcp->u_arg[3] = x86_64_regs.r10;
1868 tcp->u_arg[4] = x86_64_regs.r8;
1869 tcp->u_arg[5] = x86_64_regs.r9;
H.J. Lu35be5812012-04-16 13:00:01 +02001870# ifdef X32
1871 tcp->ext_arg[0] = x86_64_regs.rdi;
1872 tcp->ext_arg[1] = x86_64_regs.rsi;
1873 tcp->ext_arg[2] = x86_64_regs.rdx;
1874 tcp->ext_arg[3] = x86_64_regs.r10;
1875 tcp->ext_arg[4] = x86_64_regs.r8;
1876 tcp->ext_arg[5] = x86_64_regs.r9;
1877# endif
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001878 } else {
1879 /* i386 ABI */
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001880 /* Zero-extend from 32 bits */
1881 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1882 * if you need to use *sign-extended* parameter.
1883 */
1884 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1885 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1886 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1887 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1888 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1889 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001890 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001891#elif defined(MICROBLAZE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001892 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001893 if (upeek(tcp, (5 + i) * 4, &tcp->u_arg[i]) < 0)
1894 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001895#elif defined(CRISV10) || defined(CRISV32)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001896 static const int crisregs[MAX_ARGS] = {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001897 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001898 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001899 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001900
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001901 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001902 if (upeek(tcp, crisregs[i], &tcp->u_arg[i]) < 0)
1903 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001904#elif defined(TILE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001905 for (i = 0; i < nargs; ++i)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001906 tcp->u_arg[i] = tile_regs.regs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001907#elif defined(M68K)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001908 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001909 if (upeek(tcp, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
1910 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001911#elif defined(OR1K)
1912 (void)nargs;
1913 for (i = 0; i < 6; ++i)
1914 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
James Hogan5f999a82013-02-22 14:44:10 +00001915#elif defined(METAG)
1916 for (i = 0; i < nargs; i++)
1917 /* arguments go backwards from D1Ar1 (D1.3) */
1918 tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001919#else /* Other architecture (32bits specific) */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001920 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001921 if (upeek(tcp, i*4, &tcp->u_arg[i]) < 0)
1922 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001923#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001924 return 1;
1925}
1926
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00001927static int
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001928trace_syscall_entering(struct tcb *tcp)
1929{
1930 int res, scno_good;
1931
Denys Vlasenko2ce12ed2011-08-24 17:25:32 +02001932#if defined TCB_WAITEXECVE
1933 if (tcp->flags & TCB_WAITEXECVE) {
1934 /* This is the post-execve SIGTRAP. */
1935 tcp->flags &= ~TCB_WAITEXECVE;
1936 return 0;
1937 }
1938#endif
1939
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001940 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001941 if (res == 0)
1942 return res;
Denys Vlasenko907735a2012-03-21 00:23:16 +01001943 if (res == 1) {
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001944 res = syscall_fixup_on_sysenter(tcp);
Denys Vlasenko907735a2012-03-21 00:23:16 +01001945 if (res == 0)
1946 return res;
1947 if (res == 1)
1948 res = get_syscall_args(tcp);
1949 }
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001950
1951 if (res != 1) {
1952 printleader(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001953 if (scno_good != 1)
Denys Vlasenkob7a6dae2012-03-20 16:48:35 +01001954 tprints("????" /* anti-trigraph gap */ "(");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001955 else if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01001956 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001957 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001958 tprintf("%s(", tcp->s_ent->sys_name);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001959 /*
1960 * " <unavailable>" will be added later by the code which
1961 * detects ptrace errors.
1962 */
1963 goto ret;
1964 }
1965
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001966#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001967 while (1) {
Denys Vlasenko523635f2012-02-25 02:44:25 +01001968# ifdef SYS_socket_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001969 if (tcp->s_ent->sys_func == sys_socketcall) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00001970 decode_socket_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001971 break;
1972 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001973# endif
1974# ifdef SYS_ipc_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001975 if (tcp->s_ent->sys_func == sys_ipc) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00001976 decode_ipc_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001977 break;
1978 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001979# endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001980 break;
1981 }
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001982#endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001983
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001984 if (need_fork_exec_workarounds)
1985 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001986
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001987 if (!(tcp->qual_flg & QUAL_TRACE)
1988 || (tracing_paths && !pathtrace_match(tcp))
1989 ) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001990 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
1991 return 0;
1992 }
1993
1994 tcp->flags &= ~TCB_FILTERED;
1995
1996 if (cflag == CFLAG_ONLY_STATS) {
1997 res = 0;
1998 goto ret;
1999 }
2000
2001 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002002 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002003 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002004 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002005 tprintf("%s(", tcp->s_ent->sys_name);
2006 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002007 res = printargs(tcp);
2008 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002009 res = tcp->s_ent->sys_func(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002010
Dmitry V. Levinb742d8c2012-09-17 22:40:12 +00002011 fflush(tcp->outf);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002012 ret:
2013 tcp->flags |= TCB_INSYSCALL;
2014 /* Measure the entrance time as late as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002015 if (Tflag || cflag)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002016 gettimeofday(&tcp->etime, NULL);
2017 return res;
2018}
2019
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002020/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01002021 * 1: ok, continue in trace_syscall_exiting().
2022 * -1: error, trace_syscall_exiting() should print error indicator
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002023 * ("????" etc) and bail out.
2024 */
2025static int
2026get_syscall_result(struct tcb *tcp)
2027{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002028#if defined(S390) || defined(S390X)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002029 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
2030 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002031#elif defined(POWERPC)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002032# define SO_MASK 0x10000000
2033 {
2034 long flags;
2035 if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0)
2036 return -1;
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002037 if (upeek(tcp, sizeof(unsigned long)*PT_R3, &ppc_result) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002038 return -1;
2039 if (flags & SO_MASK)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002040 ppc_result = -ppc_result;
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002041 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002042#elif defined(AVR32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002043 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002044#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002045 if (upeek(tcp, PT_R0, &bfin_r0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002046 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002047#elif defined(I386)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002048 /* already done by get_regs */
H.J. Lu35be5812012-04-16 13:00:01 +02002049#elif defined(X86_64) || defined(X32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002050 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002051#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002052# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01002053 long psr;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002054 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
2055 ia32 = (psr & IA64_PSR_IS) != 0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002056 if (upeek(tcp, PT_R8, &ia64_r8) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002057 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002058 if (upeek(tcp, PT_R10, &ia64_r10) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002059 return -1;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002060#elif defined(ARM)
2061 /* already done by get_regs */
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002062#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002063 /* register reading already done by get_regs */
2064
2065 /* Used to do this, but we did it on syscall entry already: */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002066 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
2067 * else it's personality 0.
2068 */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002069 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
Denys Vlasenko523635f2012-02-25 02:44:25 +01002070#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002071 if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002072 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002073#elif defined(LINUX_MIPSN32)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002074 unsigned long long regs[38];
2075
2076 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
2077 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002078 mips_a3 = regs[REG_A3];
2079 mips_r2 = regs[REG_V0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01002080#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002081 if (upeek(tcp, REG_A3, &mips_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002082 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002083 if (upeek(tcp, REG_V0, &mips_r2) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002084 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002085#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002086 if (upeek(tcp, REG_A3, &alpha_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002087 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002088 if (upeek(tcp, REG_R0, &alpha_r0) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002089 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002090#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002091 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002092#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002093 if (upeek(tcp, PT_GR28, &hppa_r28) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002094 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002095#elif defined(SH)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002096 /* new syscall ABI returns result in R0 */
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002097 if (upeek(tcp, 4*REG_REG0, (long *)&sh_r0) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002098 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002099#elif defined(SH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002100 /* ABI defines result returned in r9 */
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002101 if (upeek(tcp, REG_GENERAL(9), (long *)&sh64_r9) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002102 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002103#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002104 if (upeek(tcp, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002105 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002106#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002107 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002108#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002109 if (upeek(tcp, 3 * 4, &microblaze_r3) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002110 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01002111#elif defined(OR1K)
2112 /* already done by get_regs */
James Hogan5f999a82013-02-22 14:44:10 +00002113#elif defined(METAG)
2114 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002115#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002116 return 1;
2117}
2118
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002119/* Called at each syscall exit */
2120static void
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002121syscall_fixup_on_sysexit(struct tcb *tcp)
2122{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002123#if defined(S390) || defined(S390X)
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002124 if (syscall_mode != -ENOSYS)
2125 syscall_mode = tcp->scno;
Denys Vlasenkoece98792011-08-25 10:25:35 +02002126 if ((tcp->flags & TCB_WAITEXECVE)
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002127 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
2128 /*
2129 * Return from execve.
2130 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2131 * flag set for the post-execve SIGTRAP to see and reset.
2132 */
2133 gpr2 = 0;
2134 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002135#endif
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002136}
2137
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002138/*
2139 * Check the syscall return value register value for whether it is
2140 * a negated errno code indicating an error, or a success return value.
2141 */
2142static inline int
2143is_negated_errno(unsigned long int val)
2144{
2145 unsigned long int max = -(long int) nerrnos;
Denys Vlasenko2544f982013-02-19 17:39:56 +01002146#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01002147 if (current_wordsize < sizeof(val)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002148 val = (unsigned int) val;
2149 max = (unsigned int) max;
2150 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002151#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002152 return val > max;
2153}
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002154
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002155#if defined(X32)
2156static inline int
2157is_negated_errno_x32(unsigned long long val)
2158{
2159 unsigned long long max = -(long long) nerrnos;
2160 /*
2161 * current_wordsize is 4 even in personality 0 (native X32)
2162 * but truncation _must not_ be done in it.
2163 * can't check current_wordsize here!
2164 */
2165 if (current_personality != 0) {
2166 val = (uint32_t) val;
2167 max = (uint32_t) max;
2168 }
2169 return val > max;
2170}
2171#endif
2172
Denys Vlasenko907735a2012-03-21 00:23:16 +01002173/* Returns:
2174 * 1: ok, continue in trace_syscall_exiting().
2175 * -1: error, trace_syscall_exiting() should print error indicator
2176 * ("????" etc) and bail out.
2177 */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002178static void
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002179get_error(struct tcb *tcp)
2180{
2181 int u_error = 0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002182 int check_errno = 1;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002183 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002184 check_errno = 0;
2185 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002186#if defined(S390) || defined(S390X)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002187 if (check_errno && is_negated_errno(gpr2)) {
2188 tcp->u_rval = -1;
2189 u_error = -gpr2;
2190 }
2191 else {
2192 tcp->u_rval = gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002193 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002194#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002195 if (check_errno && is_negated_errno(i386_regs.eax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002196 tcp->u_rval = -1;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002197 u_error = -i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002198 }
2199 else {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002200 tcp->u_rval = i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002201 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002202#elif defined(X86_64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002203 long rax;
2204 if (x86_io.iov_len == sizeof(i386_regs)) {
2205 /* Sign extend from 32 bits */
2206 rax = (int32_t)i386_regs.eax;
2207 } else {
2208 rax = x86_64_regs.rax;
2209 }
2210 if (check_errno && is_negated_errno(rax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002211 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002212 u_error = -rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002213 }
2214 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002215 tcp->u_rval = rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002216 }
2217#elif defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002218 /* In X32, return value is 64-bit (llseek uses one).
2219 * Using merely "long rax" would not work.
2220 */
2221 long long rax;
2222 if (x86_io.iov_len == sizeof(i386_regs)) {
2223 /* Sign extend from 32 bits */
2224 rax = (int32_t)i386_regs.eax;
2225 } else {
2226 rax = x86_64_regs.rax;
2227 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002228 /* Careful: is_negated_errno() works only on longs */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002229 if (check_errno && is_negated_errno_x32(rax)) {
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002230 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002231 u_error = -rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002232 }
2233 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002234 tcp->u_rval = rax; /* truncating */
2235 tcp->u_lrval = rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002236 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002237#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002238 if (ia32) {
2239 int err;
2240
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002241 err = (int)ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002242 if (check_errno && is_negated_errno(err)) {
2243 tcp->u_rval = -1;
2244 u_error = -err;
2245 }
2246 else {
2247 tcp->u_rval = err;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002248 }
2249 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002250 if (check_errno && ia64_r10) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002251 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002252 u_error = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002253 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002254 tcp->u_rval = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002255 }
2256 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002257#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002258 if (check_errno && mips_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002259 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002260 u_error = mips_r2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002261 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002262 tcp->u_rval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002263# if defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002264 tcp->u_lrval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002265# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002266 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002267#elif defined(POWERPC)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002268 if (check_errno && is_negated_errno(ppc_result)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002269 tcp->u_rval = -1;
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002270 u_error = -ppc_result;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002271 }
2272 else {
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002273 tcp->u_rval = ppc_result;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002274 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002275#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002276 if (check_errno && is_negated_errno(m68k_d0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002277 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002278 u_error = -m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002279 }
2280 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002281 tcp->u_rval = m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002282 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002283#elif defined(ARM) || defined(AARCH64)
2284# if defined(AARCH64)
2285 if (tcp->currpers == 1) {
2286 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2287 tcp->u_rval = -1;
2288 u_error = -aarch64_regs.regs[0];
2289 }
2290 else {
2291 tcp->u_rval = aarch64_regs.regs[0];
2292 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002293 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002294 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002295# endif
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002296 {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002297 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002298 tcp->u_rval = -1;
Denys Vlasenko401374e2013-02-06 18:24:39 +01002299 u_error = -arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002300 }
2301 else {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002302 tcp->u_rval = arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002303 }
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002304 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002305#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002306 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002307 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002308 u_error = -avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002309 }
2310 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002311 tcp->u_rval = avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002312 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002313#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002314 if (check_errno && is_negated_errno(bfin_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002315 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002316 u_error = -bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002317 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002318 tcp->u_rval = bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002319 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002320#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002321 if (check_errno && alpha_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002322 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002323 u_error = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002324 }
2325 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002326 tcp->u_rval = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002327 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002328#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002329 if (check_errno && sparc_regs.psr & PSR_C) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002330 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002331 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002332 }
2333 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002334 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002335 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002336#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002337 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002338 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002339 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002340 }
2341 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002342 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002343 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002344#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002345 if (check_errno && is_negated_errno(hppa_r28)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002346 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002347 u_error = -hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002348 }
2349 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002350 tcp->u_rval = hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002351 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002352#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002353 if (check_errno && is_negated_errno(sh_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002354 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002355 u_error = -sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002356 }
2357 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002358 tcp->u_rval = sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002359 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002360#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002361 if (check_errno && is_negated_errno(sh64_r9)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002362 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002363 u_error = -sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002364 }
2365 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002366 tcp->u_rval = sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002367 }
James Hogan5f999a82013-02-22 14:44:10 +00002368#elif defined(METAG)
2369 /* result pointer in D0Re0 (D0.0) */
2370 if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2371 tcp->u_rval = -1;
2372 u_error = -metag_regs.dx[0][0];
2373 }
2374 else {
2375 tcp->u_rval = metag_regs.dx[0][0];
2376 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002377#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002378 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002379 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002380 u_error = -cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002381 }
2382 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002383 tcp->u_rval = cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002384 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002385#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002386 /*
2387 * The standard tile calling convention returns the value (or negative
2388 * errno) in r0, and zero (or positive errno) in r1.
2389 * Until at least kernel 3.8, however, the r1 value is not reflected
2390 * in ptregs at this point, so we use r0 here.
2391 */
2392 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002393 tcp->u_rval = -1;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002394 u_error = -tile_regs.regs[0];
2395 } else {
2396 tcp->u_rval = tile_regs.regs[0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002397 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002398#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002399 if (check_errno && is_negated_errno(microblaze_r3)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002400 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002401 u_error = -microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002402 }
2403 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002404 tcp->u_rval = microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002405 }
Christian Svensson492f81f2013-02-14 13:26:27 +01002406#elif defined(OR1K)
2407 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2408 tcp->u_rval = -1;
2409 u_error = -or1k_regs.gpr[11];
2410 }
2411 else {
2412 tcp->u_rval = or1k_regs.gpr[11];
2413 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002414#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002415 tcp->u_error = u_error;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002416}
2417
2418static void
2419dumpio(struct tcb *tcp)
2420{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002421 int (*func)();
2422
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002423 if (syserror(tcp))
2424 return;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +01002425 if ((unsigned long) tcp->u_arg[0] >= num_quals)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002426 return;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002427 func = tcp->s_ent->sys_func;
2428 if (func == printargs)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002429 return;
2430 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002431 if (func == sys_read ||
2432 func == sys_pread ||
2433 func == sys_recv ||
2434 func == sys_recvfrom)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002435 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002436 else if (func == sys_readv)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002437 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2438 return;
2439 }
2440 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002441 if (func == sys_write ||
2442 func == sys_pwrite ||
2443 func == sys_send ||
2444 func == sys_sendto)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002445 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002446 else if (func == sys_writev)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002447 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2448 return;
2449 }
2450}
2451
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002452static int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002453trace_syscall_exiting(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002454{
2455 int sys_res;
2456 struct timeval tv;
Denys Vlasenko1a5b5a72011-08-25 00:29:56 +02002457 int res;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002458 long u_error;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002459
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002460 /* Measure the exit time as early as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002461 if (Tflag || cflag)
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002462 gettimeofday(&tv, NULL);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002463
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00002464#if SUPPORTED_PERSONALITIES > 1
2465 update_personality(tcp, tcp->currpers);
2466#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002467 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002468 if (res == 1) {
2469 syscall_fixup_on_sysexit(tcp); /* never fails */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002470 get_error(tcp); /* never fails */
2471 if (need_fork_exec_workarounds)
2472 syscall_fixup_for_fork_exec(tcp);
2473 if (filtered(tcp))
2474 goto ret;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002475 }
2476
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002477 if (cflag) {
2478 struct timeval t = tv;
Denys Vlasenkoc95a88f2011-08-21 17:47:40 +02002479 count_syscall(tcp, &t);
Denys Vlasenko7b609d52011-06-22 14:32:43 +02002480 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002481 goto ret;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002482 }
2483 }
2484
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002485 /* If not in -ff mode, and printing_tcp != tcp,
2486 * then the log currently does not end with output
2487 * of _our syscall entry_, but with something else.
2488 * We need to say which syscall's return is this.
2489 *
2490 * Forced reprinting via TCB_REPRINT is used only by
2491 * "strace -ff -oLOG test/threaded_execve" corner case.
2492 * It's the only case when -ff mode needs reprinting.
2493 */
2494 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2495 tcp->flags &= ~TCB_REPRINT;
2496 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002497 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002498 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002499 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002500 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002501 }
2502 printing_tcp = tcp;
2503
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002504 if (res != 1) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002505 /* There was error in one of prior ptrace ops */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002506 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002507 tabto();
Denys Vlasenko000b6012012-01-28 01:25:03 +01002508 tprints("= ? <unavailable>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002509 line_ended();
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002510 tcp->flags &= ~TCB_INSYSCALL;
2511 return res;
2512 }
2513
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002514 sys_res = 0;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002515 if (tcp->qual_flg & QUAL_RAW) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002516 /* sys_res = printargs(tcp); - but it's nop on sysexit */
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002517 } else {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002518 /* FIXME: not_failing_only (IOW, option -z) is broken:
2519 * failure of syscall is known only after syscall return.
2520 * Thus we end up with something like this on, say, ENOENT:
2521 * open("doesnt_exist", O_RDONLY <unfinished ...>
2522 * {next syscall decode}
2523 * whereas the intended result is that open(...) line
2524 * is not shown at all.
2525 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002526 if (not_failing_only && tcp->u_error)
Denys Vlasenko3b738812011-08-22 02:06:35 +02002527 goto ret; /* ignore failed syscalls */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002528 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002529 }
2530
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002531 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002532 tabto();
Denys Vlasenko3b738812011-08-22 02:06:35 +02002533 u_error = tcp->u_error;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002534 if (tcp->qual_flg & QUAL_RAW) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002535 if (u_error)
2536 tprintf("= -1 (errno %ld)", u_error);
2537 else
2538 tprintf("= %#lx", tcp->u_rval);
2539 }
2540 else if (!(sys_res & RVAL_NONE) && u_error) {
2541 switch (u_error) {
Denys Vlasenkofe585652012-01-12 11:26:34 +01002542 /* Blocked signals do not interrupt any syscalls.
2543 * In this case syscalls don't return ERESTARTfoo codes.
2544 *
2545 * Deadly signals set to SIG_DFL interrupt syscalls
2546 * and kill the process regardless of which of the codes below
2547 * is returned by the interrupted syscall.
2548 * In some cases, kernel forces a kernel-generated deadly
2549 * signal to be unblocked and set to SIG_DFL (and thus cause
2550 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2551 * or SIGILL. (The alternative is to leave process spinning
2552 * forever on the faulty instruction - not useful).
2553 *
2554 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2555 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2556 * but kernel will always restart them.
2557 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002558 case ERESTARTSYS:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002559 /* Most common type of signal-interrupted syscall exit code.
2560 * The system call will be restarted with the same arguments
2561 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2562 */
2563 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002564 break;
2565 case ERESTARTNOINTR:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002566 /* Rare. For example, fork() returns this if interrupted.
2567 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2568 */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002569 tprints("= ? ERESTARTNOINTR (To be restarted)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002570 break;
2571 case ERESTARTNOHAND:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002572 /* pause(), rt_sigsuspend() etc use this code.
2573 * SA_RESTART is ignored (assumed not set):
2574 * syscall won't restart (will return EINTR instead)
Denys Vlasenko30c03232013-02-19 16:59:26 +01002575 * even after signal with SA_RESTART set. However,
2576 * after SIG_IGN or SIG_DFL signal it will restart
2577 * (thus the name "restart only if has no handler").
Denys Vlasenkofe585652012-01-12 11:26:34 +01002578 */
Denys Vlasenkoaba62922013-03-05 16:56:35 +01002579 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002580 break;
2581 case ERESTART_RESTARTBLOCK:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002582 /* Syscalls like nanosleep(), poll() which can't be
2583 * restarted with their original arguments use this
2584 * code. Kernel will execute restart_syscall() instead,
2585 * which changes arguments before restarting syscall.
2586 * SA_RESTART is ignored (assumed not set) similarly
2587 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2588 * since restart data is saved in "restart block"
2589 * in task struct, and if signal handler uses a syscall
2590 * which in turn saves another such restart block,
2591 * old data is lost and restart becomes impossible)
2592 */
2593 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002594 break;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002595 default:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002596 if (u_error < 0)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002597 tprintf("= -1 E??? (errno %ld)", u_error);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002598 else if (u_error < nerrnos)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002599 tprintf("= -1 %s (%s)", errnoent[u_error],
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002600 strerror(u_error));
2601 else
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002602 tprintf("= -1 ERRNO_%ld (%s)", u_error,
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002603 strerror(u_error));
2604 break;
2605 }
2606 if ((sys_res & RVAL_STR) && tcp->auxstr)
2607 tprintf(" (%s)", tcp->auxstr);
2608 }
2609 else {
2610 if (sys_res & RVAL_NONE)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002611 tprints("= ?");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002612 else {
2613 switch (sys_res & RVAL_MASK) {
2614 case RVAL_HEX:
2615 tprintf("= %#lx", tcp->u_rval);
2616 break;
2617 case RVAL_OCTAL:
2618 tprintf("= %#lo", tcp->u_rval);
2619 break;
2620 case RVAL_UDECIMAL:
2621 tprintf("= %lu", tcp->u_rval);
2622 break;
2623 case RVAL_DECIMAL:
2624 tprintf("= %ld", tcp->u_rval);
2625 break;
H.J. Ludd0130b2012-04-16 12:16:45 +02002626#if defined(LINUX_MIPSN32) || defined(X32)
2627 /*
2628 case RVAL_LHEX:
2629 tprintf("= %#llx", tcp->u_lrval);
2630 break;
2631 case RVAL_LOCTAL:
2632 tprintf("= %#llo", tcp->u_lrval);
2633 break;
2634 */
2635 case RVAL_LUDECIMAL:
2636 tprintf("= %llu", tcp->u_lrval);
2637 break;
2638 /*
2639 case RVAL_LDECIMAL:
2640 tprintf("= %lld", tcp->u_lrval);
2641 break;
2642 */
2643#endif
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002644 default:
2645 fprintf(stderr,
2646 "invalid rval format\n");
2647 break;
2648 }
2649 }
2650 if ((sys_res & RVAL_STR) && tcp->auxstr)
2651 tprintf(" (%s)", tcp->auxstr);
2652 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002653 if (Tflag) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002654 tv_sub(&tv, &tv, &tcp->etime);
2655 tprintf(" <%ld.%06ld>",
2656 (long) tv.tv_sec, (long) tv.tv_usec);
2657 }
Denys Vlasenko000b6012012-01-28 01:25:03 +01002658 tprints("\n");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002659 dumpio(tcp);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002660 line_ended();
2661
Denys Vlasenko3b738812011-08-22 02:06:35 +02002662 ret:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002663 tcp->flags &= ~TCB_INSYSCALL;
2664 return 0;
2665}
2666
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002667int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002668trace_syscall(struct tcb *tcp)
2669{
2670 return exiting(tcp) ?
2671 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2672}