blob: 7e2d0e848c6613a21df19780c92e3418bc10a899 [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
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010068#if defined(X86_64) || defined(X32)
69# include <linux/ptrace.h>
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010070# include <sys/uio.h>
71# include <elf.h>
72#endif
73
Steve McIntyred8d3bd32012-10-24 17:58:16 +010074#if defined(AARCH64)
75# include <asm/ptrace.h>
76# include <sys/uio.h>
77# include <elf.h>
78#endif
79
Christian Svensson492f81f2013-02-14 13:26:27 +010080#if defined(OR1K)
81# include <sys/uio.h>
82# include <elf.h>
83#endif
84
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000085#ifndef ERESTARTSYS
Denys Vlasenko523635f2012-02-25 02:44:25 +010086# define ERESTARTSYS 512
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000087#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010088#ifndef ERESTARTNOINTR
Denys Vlasenko523635f2012-02-25 02:44:25 +010089# define ERESTARTNOINTR 513
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000090#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010091#ifndef ERESTARTNOHAND
92# define ERESTARTNOHAND 514 /* restart if no handler */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000093#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010094#ifndef ERESTART_RESTARTBLOCK
Denys Vlasenko523635f2012-02-25 02:44:25 +010095# define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
Roland McGrath9c555e72003-07-09 09:47:59 +000096#endif
Denys Vlasenko523635f2012-02-25 02:44:25 +010097
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000098#ifndef NSIG
Denys Vlasenko523635f2012-02-25 02:44:25 +010099# warning: NSIG is not defined, using 32
100# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000101#endif
102#ifdef ARM
Denys Vlasenko041b3ee2011-08-18 12:48:56 +0200103/* Ugh. Is this really correct? ARM has no RT signals?! */
Denys Vlasenko523635f2012-02-25 02:44:25 +0100104# undef NSIG
105# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000106#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000107
108#include "syscall.h"
109
110/* Define these shorthand notations to simplify the syscallent files. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000111#define TD TRACE_DESC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000112#define TF TRACE_FILE
113#define TI TRACE_IPC
114#define TN TRACE_NETWORK
115#define TP TRACE_PROCESS
116#define TS TRACE_SIGNAL
Namhyung Kim96792962012-10-24 11:41:57 +0900117#define TM TRACE_MEMORY
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000118#define NF SYSCALL_NEVER_FAILS
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200119#define MA MAX_ARGS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000120
Roland McGrathee36ce12004-09-04 03:53:10 +0000121static const struct sysent sysent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000122#include "syscallent.h"
123};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000124
125#if SUPPORTED_PERSONALITIES >= 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000126static const struct sysent sysent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100127# include "syscallent1.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#if SUPPORTED_PERSONALITIES >= 3
Roland McGrathee36ce12004-09-04 03:53:10 +0000132static const struct sysent sysent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100133# include "syscallent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000134};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200135#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000136
137/* Now undef them since short defines cause wicked namespace pollution. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000138#undef TD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000139#undef TF
140#undef TI
141#undef TN
142#undef TP
143#undef TS
Namhyung Kim96792962012-10-24 11:41:57 +0900144#undef TM
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000145#undef NF
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200146#undef MA
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000147
Denys Vlasenko39fca622011-08-20 02:12:33 +0200148/*
149 * `ioctlent.h' may be generated from `ioctlent.raw' by the auxiliary
150 * program `ioctlsort', such that the list is sorted by the `code' field.
151 * This has the side-effect of resolving the _IO.. macros into
152 * plain integers, eliminating the need to include here everything
153 * in "/usr/include".
154 */
155
Roland McGrathee36ce12004-09-04 03:53:10 +0000156static const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000157#include "errnoent.h"
158};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200159static const char *const signalent0[] = {
160#include "signalent.h"
161};
162static const struct ioctlent ioctlent0[] = {
163#include "ioctlent.h"
164};
165enum { nsyscalls0 = ARRAY_SIZE(sysent0) };
166enum { nerrnos0 = ARRAY_SIZE(errnoent0) };
167enum { nsignals0 = ARRAY_SIZE(signalent0) };
168enum { nioctlents0 = ARRAY_SIZE(ioctlent0) };
169int qual_flags0[MAX_QUALS];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000170
171#if SUPPORTED_PERSONALITIES >= 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000172static const char *const errnoent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100173# include "errnoent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000174};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200175static const char *const signalent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100176# include "signalent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200177};
178static const struct ioctlent ioctlent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100179# include "ioctlent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200180};
181enum { nsyscalls1 = ARRAY_SIZE(sysent1) };
182enum { nerrnos1 = ARRAY_SIZE(errnoent1) };
183enum { nsignals1 = ARRAY_SIZE(signalent1) };
184enum { nioctlents1 = ARRAY_SIZE(ioctlent1) };
185int qual_flags1[MAX_QUALS];
186#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000187
188#if SUPPORTED_PERSONALITIES >= 3
Roland McGrathee36ce12004-09-04 03:53:10 +0000189static const char *const errnoent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100190# include "errnoent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000191};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200192static const char *const signalent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100193# include "signalent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200194};
195static const struct ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100196# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200197};
198enum { nsyscalls2 = ARRAY_SIZE(sysent2) };
199enum { nerrnos2 = ARRAY_SIZE(errnoent2) };
200enum { nsignals2 = ARRAY_SIZE(signalent2) };
201enum { nioctlents2 = ARRAY_SIZE(ioctlent2) };
202int qual_flags2[MAX_QUALS];
203#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000204
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100205const struct sysent *sysent = sysent0;
206const char *const *errnoent = errnoent0;
207const char *const *signalent = signalent0;
208const struct ioctlent *ioctlent = ioctlent0;
209unsigned nsyscalls = nsyscalls0;
210unsigned nerrnos = nerrnos0;
211unsigned nsignals = nsignals0;
212unsigned nioctlents = nioctlents0;
213int *qual_flags = qual_flags0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000214
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100215#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100216unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000217
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100218# ifndef current_wordsize
219unsigned current_wordsize;
220static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000221 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000222 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100223# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000224 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100225# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200226};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100227# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000228
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200229void
Dmitry V. Levin3abe8b22006-12-20 22:37:21 +0000230set_personality(int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000231{
232 switch (personality) {
233 case 0:
234 errnoent = errnoent0;
235 nerrnos = nerrnos0;
236 sysent = sysent0;
237 nsyscalls = nsyscalls0;
238 ioctlent = ioctlent0;
239 nioctlents = nioctlents0;
240 signalent = signalent0;
241 nsignals = nsignals0;
Roland McGrath138c6a32006-01-12 09:50:49 +0000242 qual_flags = qual_flags0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000243 break;
244
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000245 case 1:
246 errnoent = errnoent1;
247 nerrnos = nerrnos1;
248 sysent = sysent1;
249 nsyscalls = nsyscalls1;
250 ioctlent = ioctlent1;
251 nioctlents = nioctlents1;
252 signalent = signalent1;
253 nsignals = nsignals1;
Roland McGrath138c6a32006-01-12 09:50:49 +0000254 qual_flags = qual_flags1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000255 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000256
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100257# if SUPPORTED_PERSONALITIES >= 3
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000258 case 2:
259 errnoent = errnoent2;
260 nerrnos = nerrnos2;
261 sysent = sysent2;
262 nsyscalls = nsyscalls2;
263 ioctlent = ioctlent2;
264 nioctlents = nioctlents2;
265 signalent = signalent2;
266 nsignals = nsignals2;
Roland McGrath138c6a32006-01-12 09:50:49 +0000267 qual_flags = qual_flags2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000268 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100269# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000270 }
271
272 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100273# ifndef current_wordsize
274 current_wordsize = personality_wordsize[personality];
275# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000276}
277
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000278static void
279update_personality(struct tcb *tcp, int personality)
280{
281 if (personality == current_personality)
282 return;
283 set_personality(personality);
284
285 if (personality == tcp->currpers)
286 return;
287 tcp->currpers = personality;
288
H.J. Lu35be5812012-04-16 13:00:01 +0200289# if defined(POWERPC64)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000290 if (!qflag) {
291 static const char *const names[] = {"64 bit", "32 bit"};
292 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
293 tcp->pid, names[personality]);
294 }
H.J. Lu35be5812012-04-16 13:00:01 +0200295# elif defined(X86_64)
296 if (!qflag) {
297 static const char *const names[] = {"64 bit", "32 bit", "x32"};
298 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
299 tcp->pid, names[personality]);
300 }
H.J. Lu085e4282012-04-17 11:05:04 -0700301# elif defined(X32)
302 if (!qflag) {
303 static const char *const names[] = {"x32", "32 bit"};
304 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
305 tcp->pid, names[personality]);
306 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000307# elif defined(AARCH64)
308 if (!qflag) {
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100309 static const char *const names[] = {"32-bit", "AArch64"};
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000310 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
311 tcp->pid, names[personality]);
312 }
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100313# elif defined(TILE)
314 if (!qflag) {
315 static const char *const names[] = {"64-bit", "32-bit"};
316 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
317 tcp->pid, names[personality]);
318 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100319# endif
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000320}
321#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000322
Roland McGrath9797ceb2002-12-30 10:23:00 +0000323static int qual_syscall(), qual_signal(), qual_fault(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000324
Roland McGrathe10e62a2004-09-04 04:20:43 +0000325static const struct qual_options {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000326 int bitflag;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000327 const char *option_name;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000328 int (*qualify)(const char *, int, int);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000329 const char *argument_name;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000330} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000331 { QUAL_TRACE, "trace", qual_syscall, "system call" },
332 { QUAL_TRACE, "t", qual_syscall, "system call" },
333 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
334 { QUAL_ABBREV, "a", qual_syscall, "system call" },
335 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
336 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
337 { QUAL_RAW, "raw", qual_syscall, "system call" },
338 { QUAL_RAW, "x", qual_syscall, "system call" },
339 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
340 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
341 { QUAL_SIGNAL, "s", qual_signal, "signal" },
342 { QUAL_FAULT, "fault", qual_fault, "fault" },
343 { QUAL_FAULT, "faults", qual_fault, "fault" },
344 { QUAL_FAULT, "m", qual_fault, "fault" },
345 { QUAL_READ, "read", qual_desc, "descriptor" },
346 { QUAL_READ, "reads", qual_desc, "descriptor" },
347 { QUAL_READ, "r", qual_desc, "descriptor" },
348 { QUAL_WRITE, "write", qual_desc, "descriptor" },
349 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
350 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000351 { 0, NULL, NULL, NULL },
352};
353
Roland McGrath9797ceb2002-12-30 10:23:00 +0000354static void
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000355qualify_one(int n, int bitflag, int not, int pers)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000356{
Roland McGrath138c6a32006-01-12 09:50:49 +0000357 if (pers == 0 || pers < 0) {
358 if (not)
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000359 qual_flags0[n] &= ~bitflag;
Roland McGrath138c6a32006-01-12 09:50:49 +0000360 else
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000361 qual_flags0[n] |= bitflag;
Roland McGrath138c6a32006-01-12 09:50:49 +0000362 }
363
364#if SUPPORTED_PERSONALITIES >= 2
365 if (pers == 1 || pers < 0) {
366 if (not)
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000367 qual_flags1[n] &= ~bitflag;
Roland McGrath138c6a32006-01-12 09:50:49 +0000368 else
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000369 qual_flags1[n] |= bitflag;
Roland McGrath138c6a32006-01-12 09:50:49 +0000370 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100371#endif
Roland McGrath138c6a32006-01-12 09:50:49 +0000372
373#if SUPPORTED_PERSONALITIES >= 3
374 if (pers == 2 || pers < 0) {
375 if (not)
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000376 qual_flags2[n] &= ~bitflag;
Roland McGrath138c6a32006-01-12 09:50:49 +0000377 else
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000378 qual_flags2[n] |= bitflag;
Roland McGrath138c6a32006-01-12 09:50:49 +0000379 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100380#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000381}
382
383static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000384qual_syscall(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000385{
386 int i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000387 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000388
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100389 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000390 int i = string_to_uint(s);
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000391 if (i < 0 || i >= MAX_QUALS)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000392 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000393 qualify_one(i, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000394 return 0;
Roland McGrath48a035f2006-01-12 09:45:56 +0000395 }
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000396 for (i = 0; i < nsyscalls0; i++)
Dmitry V. Levin4372cc92012-03-26 14:14:50 +0000397 if (sysent0[i].sys_name &&
398 strcmp(s, sysent0[i].sys_name) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000399 qualify_one(i, bitflag, not, 0);
Roland McGrathfe6b3522005-02-02 04:40:11 +0000400 rc = 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000401 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000402
403#if SUPPORTED_PERSONALITIES >= 2
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000404 for (i = 0; i < nsyscalls1; i++)
Dmitry V. Levin4372cc92012-03-26 14:14:50 +0000405 if (sysent1[i].sys_name &&
406 strcmp(s, sysent1[i].sys_name) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000407 qualify_one(i, bitflag, not, 1);
Roland McGrath138c6a32006-01-12 09:50:49 +0000408 rc = 0;
409 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100410#endif
Roland McGrath138c6a32006-01-12 09:50:49 +0000411
412#if SUPPORTED_PERSONALITIES >= 3
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000413 for (i = 0; i < nsyscalls2; i++)
Dmitry V. Levin4372cc92012-03-26 14:14:50 +0000414 if (sysent2[i].sys_name &&
415 strcmp(s, sysent2[i].sys_name) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000416 qualify_one(i, bitflag, not, 2);
Roland McGrath138c6a32006-01-12 09:50:49 +0000417 rc = 0;
418 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100419#endif
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000420
Roland McGrathfe6b3522005-02-02 04:40:11 +0000421 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000422}
423
424static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000425qual_signal(const char *s, int bitflag, int not)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000426{
427 int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000428
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100429 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000430 int signo = string_to_uint(s);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000431 if (signo < 0 || signo >= MAX_QUALS)
432 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000433 qualify_one(signo, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000434 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000435 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000436 if (strncasecmp(s, "SIG", 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000437 s += 3;
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100438 for (i = 0; i <= NSIG; i++) {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000439 if (strcasecmp(s, signame(i) + 3) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000440 qualify_one(i, bitflag, not, -1);
Roland McGrath76421df2005-02-02 03:51:18 +0000441 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000442 }
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100443 }
Roland McGrath76421df2005-02-02 03:51:18 +0000444 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000445}
446
447static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000448qual_fault(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000449{
450 return -1;
451}
452
453static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000454qual_desc(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000455{
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100456 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000457 int desc = string_to_uint(s);
Roland McGrathfe6b3522005-02-02 04:40:11 +0000458 if (desc < 0 || desc >= MAX_QUALS)
459 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000460 qualify_one(desc, bitflag, not, -1);
Roland McGrath2b619022003-04-10 18:58:20 +0000461 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000462 }
463 return -1;
464}
465
466static int
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000467lookup_class(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000468{
469 if (strcmp(s, "file") == 0)
470 return TRACE_FILE;
471 if (strcmp(s, "ipc") == 0)
472 return TRACE_IPC;
473 if (strcmp(s, "network") == 0)
474 return TRACE_NETWORK;
475 if (strcmp(s, "process") == 0)
476 return TRACE_PROCESS;
477 if (strcmp(s, "signal") == 0)
478 return TRACE_SIGNAL;
Roland McGrath2fe7b132005-07-05 03:25:35 +0000479 if (strcmp(s, "desc") == 0)
480 return TRACE_DESC;
Namhyung Kim96792962012-10-24 11:41:57 +0900481 if (strcmp(s, "memory") == 0)
482 return TRACE_MEMORY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000483 return -1;
484}
485
486void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000487qualify(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000488{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000489 const struct qual_options *opt;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000490 int not;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000491 char *copy;
492 const char *p;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000493 int i, n;
494
495 opt = &qual_options[0];
496 for (i = 0; (p = qual_options[i].option_name); i++) {
497 n = strlen(p);
498 if (strncmp(s, p, n) == 0 && s[n] == '=') {
499 opt = &qual_options[i];
500 s += n + 1;
501 break;
502 }
503 }
504 not = 0;
505 if (*s == '!') {
506 not = 1;
507 s++;
508 }
509 if (strcmp(s, "none") == 0) {
510 not = 1 - not;
511 s = "all";
512 }
513 if (strcmp(s, "all") == 0) {
514 for (i = 0; i < MAX_QUALS; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000515 qualify_one(i, opt->bitflag, not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000516 }
517 return;
518 }
519 for (i = 0; i < MAX_QUALS; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000520 qualify_one(i, opt->bitflag, !not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000521 }
Denys Vlasenko5d645812011-08-20 12:48:18 +0200522 copy = strdup(s);
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200523 if (!copy)
524 die_out_of_memory();
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000525 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000526 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000527 for (i = 0; i < nsyscalls0; i++)
Roland McGrath138c6a32006-01-12 09:50:49 +0000528 if (sysent0[i].sys_flags & n)
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000529 qualify_one(i, opt->bitflag, not, 0);
Roland McGrath138c6a32006-01-12 09:50:49 +0000530
531#if SUPPORTED_PERSONALITIES >= 2
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000532 for (i = 0; i < nsyscalls1; i++)
Roland McGrath138c6a32006-01-12 09:50:49 +0000533 if (sysent1[i].sys_flags & n)
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000534 qualify_one(i, opt->bitflag, not, 1);
Denys Vlasenko523635f2012-02-25 02:44:25 +0100535#endif
Roland McGrath138c6a32006-01-12 09:50:49 +0000536
537#if SUPPORTED_PERSONALITIES >= 3
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000538 for (i = 0; i < nsyscalls2; i++)
Roland McGrath138c6a32006-01-12 09:50:49 +0000539 if (sysent2[i].sys_flags & n)
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000540 qualify_one(i, opt->bitflag, not, 2);
Denys Vlasenko523635f2012-02-25 02:44:25 +0100541#endif
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000542
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000543 continue;
544 }
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000545 if (opt->qualify(p, opt->bitflag, not)) {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100546 error_msg_and_die("invalid %s '%s'",
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000547 opt->argument_name, p);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000548 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000549 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000550 free(copy);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000551 return;
552}
553
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000554#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000555static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000556decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000557{
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000558 unsigned long addr;
559 unsigned int i, size;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000560
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000561 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
562 return;
563
564 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
565 addr = tcp->u_arg[1];
566 tcp->u_nargs = sysent[tcp->scno].nargs;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100567 size = current_wordsize;
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000568 for (i = 0; i < tcp->u_nargs; ++i) {
569 if (size == sizeof(int)) {
570 unsigned int arg;
571 if (umove(tcp, addr, &arg) < 0)
572 arg = 0;
573 tcp->u_arg[i] = arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000574 }
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000575 else {
576 unsigned long arg;
577 if (umove(tcp, addr, &arg) < 0)
578 arg = 0;
579 tcp->u_arg[i] = arg;
580 }
581 addr += size;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000582 }
583}
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000584#endif
Mike Frysinger3362e892012-03-15 01:09:19 -0400585
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000586#ifdef SYS_ipc_subcall
587static void
588decode_ipc_subcall(struct tcb *tcp)
589{
590 unsigned int i;
591
592 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
593 return;
594
595 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
596 tcp->u_nargs = sysent[tcp->scno].nargs;
597 for (i = 0; i < tcp->u_nargs; i++)
598 tcp->u_arg[i] = tcp->u_arg[i + 1];
599}
600#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000601
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200602int
603printargs(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000604{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200605 if (entering(tcp)) {
606 int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000607
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200608 for (i = 0; i < tcp->u_nargs; i++)
609 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
610 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000611 return 0;
612}
613
Denys Vlasenko72879c62012-02-27 14:18:02 +0100614int
615printargs_lu(struct tcb *tcp)
616{
617 if (entering(tcp)) {
618 int i;
619
620 for (i = 0; i < tcp->u_nargs; i++)
621 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
622 }
623 return 0;
624}
625
626int
627printargs_ld(struct tcb *tcp)
628{
629 if (entering(tcp)) {
630 int i;
631
632 for (i = 0; i < tcp->u_nargs; i++)
633 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
634 }
635 return 0;
636}
637
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100638#if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200639long
640getrval2(struct tcb *tcp)
641{
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100642 long val;
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200643
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100644# if defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100645 val = sparc_regs.u_regs[U_REG_O1];
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100646# elif defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200647 if (upeek(tcp, 4*(REG_REG0+1), &val) < 0)
648 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100649# elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200650 if (upeek(tcp, PT_R9, &val) < 0)
651 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100652# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200653
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200654 return val;
655}
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100656#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200657
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200658int
659is_restart_error(struct tcb *tcp)
660{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200661 switch (tcp->u_error) {
662 case ERESTARTSYS:
663 case ERESTARTNOINTR:
664 case ERESTARTNOHAND:
665 case ERESTART_RESTARTBLOCK:
666 return 1;
667 default:
668 break;
669 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200670 return 0;
671}
672
Denys Vlasenko523635f2012-02-25 02:44:25 +0100673#if defined(I386)
Denys Vlasenko2550d482013-02-15 21:04:28 +0100674struct user_regs_struct i386_regs;
H.J. Lu35be5812012-04-16 13:00:01 +0200675#elif defined(X86_64) || defined(X32)
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100676/*
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100677 * On i386, pt_regs and user_regs_struct are the same,
678 * but on 64 bit x86, user_regs_struct has six more fields:
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100679 * fs_base, gs_base, ds, es, fs, gs.
680 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
681 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100682struct i386_user_regs_struct {
683 uint32_t ebx;
684 uint32_t ecx;
685 uint32_t edx;
686 uint32_t esi;
687 uint32_t edi;
688 uint32_t ebp;
689 uint32_t eax;
690 uint32_t xds;
691 uint32_t xes;
692 uint32_t xfs;
693 uint32_t xgs;
694 uint32_t orig_eax;
695 uint32_t eip;
696 uint32_t xcs;
697 uint32_t eflags;
698 uint32_t esp;
699 uint32_t xss;
700};
701static union {
702 struct user_regs_struct x86_64_r;
703 struct i386_user_regs_struct i386_r;
704} x86_regs_union;
705# define x86_64_regs x86_regs_union.x86_64_r
706# define i386_regs x86_regs_union.i386_r
707static struct iovec x86_io = {
708 .iov_base = &x86_regs_union
709};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100710#elif defined(IA64)
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200711long ia32 = 0; /* not static */
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100712static long ia64_r8, ia64_r10;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100713#elif defined(POWERPC)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +0100714static long ppc_result;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100715#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100716static long m68k_d0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100717#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100718static long bfin_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100719#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100720struct pt_regs arm_regs; /* not static */
Steve McIntyred8d3bd32012-10-24 17:58:16 +0100721#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100722static union {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100723 struct user_pt_regs aarch64_r;
724 struct arm_pt_regs arm_r;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100725} arm_regs_union;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100726# define aarch64_regs arm_regs_union.aarch64_r
727# define arm_regs arm_regs_union.arm_r
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100728static struct iovec aarch64_io = {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100729 .iov_base = &arm_regs_union
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100730};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100731#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100732static long alpha_r0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100733static long alpha_a3;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100734#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100735static struct pt_regs avr32_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100736#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100737struct pt_regs sparc_regs; /* not static */
Denys Vlasenko523635f2012-02-25 02:44:25 +0100738#elif defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100739static long long mips_a3;
740static long long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100741#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100742static long mips_a3;
743static long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100744#elif defined(S390) || defined(S390X)
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200745static long gpr2;
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200746static long syscall_mode;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100747#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100748static long hppa_r28;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100749#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100750static long sh_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100751#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100752static long sh64_r9;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100753#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100754static long cris_r10;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100755#elif defined(TILE)
756struct pt_regs tile_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100757#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100758static long microblaze_r3;
Christian Svensson492f81f2013-02-14 13:26:27 +0100759#elif defined(OR1K)
760static struct user_regs_struct or1k_regs;
761static struct iovec or1k_io = {
762 .iov_base = &or1k_regs
763};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100764#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000765
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100766void
767printcall(struct tcb *tcp)
768{
769#define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
770 sizeof(long) == 8 ? "[????????????????] " : \
771 NULL /* crash */)
772 if (get_regs_error) {
773 PRINTBADPC;
774 return;
775 }
776#if defined(I386)
777 tprintf("[%08lx] ", i386_regs.eip);
778#elif defined(S390) || defined(S390X)
779 long psw;
780 if (upeek(tcp, PT_PSWADDR, &psw) < 0) {
781 PRINTBADPC;
782 return;
783 }
784# ifdef S390
785 tprintf("[%08lx] ", psw);
786# elif S390X
Dmitry V. Levinddba73e2013-02-05 19:01:58 +0000787 tprintf("[%016lx] ", psw);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100788# endif
789#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100790 if (x86_io.iov_len == sizeof(i386_regs)) {
791 tprintf("[%08x] ", (unsigned) i386_regs.eip);
792 } else {
793# if defined(X86_64)
794 tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
795# elif defined(X32)
796 /* Note: this truncates 64-bit rip to 32 bits */
797 tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip);
798# endif
799 }
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100800#elif defined(IA64)
801 long ip;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100802 if (upeek(tcp, PT_B0, &ip) < 0) {
803 PRINTBADPC;
804 return;
805 }
806 tprintf("[%08lx] ", ip);
807#elif defined(POWERPC)
808 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100809 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) {
810 PRINTBADPC;
811 return;
812 }
813# ifdef POWERPC64
814 tprintf("[%016lx] ", pc);
815# else
816 tprintf("[%08lx] ", pc);
817# endif
818#elif defined(M68K)
819 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100820 if (upeek(tcp, 4*PT_PC, &pc) < 0) {
821 tprints("[????????] ");
822 return;
823 }
824 tprintf("[%08lx] ", pc);
825#elif defined(ALPHA)
826 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100827 if (upeek(tcp, REG_PC, &pc) < 0) {
828 tprints("[????????????????] ");
829 return;
830 }
831 tprintf("[%08lx] ", pc);
832#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100833 tprintf("[%08lx] ", sparc_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100834#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100835 tprintf("[%08lx] ", sparc_regs.tpc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100836#elif defined(HPPA)
837 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100838 if (upeek(tcp, PT_IAOQ0, &pc) < 0) {
839 tprints("[????????] ");
840 return;
841 }
842 tprintf("[%08lx] ", pc);
843#elif defined(MIPS)
844 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100845 if (upeek(tcp, REG_EPC, &pc) < 0) {
846 tprints("[????????] ");
847 return;
848 }
849 tprintf("[%08lx] ", pc);
850#elif defined(SH)
851 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100852 if (upeek(tcp, 4*REG_PC, &pc) < 0) {
853 tprints("[????????] ");
854 return;
855 }
856 tprintf("[%08lx] ", pc);
857#elif defined(SH64)
858 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100859 if (upeek(tcp, REG_PC, &pc) < 0) {
860 tprints("[????????????????] ");
861 return;
862 }
863 tprintf("[%08lx] ", pc);
864#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100865 tprintf("[%08lx] ", arm_regs.ARM_pc);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100866#elif defined(AARCH64)
867 /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100868#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100869 tprintf("[%08lx] ", avr32_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100870#elif defined(BFIN)
871 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100872 if (upeek(tcp, PT_PC, &pc) < 0) {
873 PRINTBADPC;
874 return;
875 }
876 tprintf("[%08lx] ", pc);
877#elif defined(CRISV10)
878 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100879 if (upeek(tcp, 4*PT_IRP, &pc) < 0) {
880 PRINTBADPC;
881 return;
882 }
883 tprintf("[%08lx] ", pc);
884#elif defined(CRISV32)
885 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100886 if (upeek(tcp, 4*PT_ERP, &pc) < 0) {
887 PRINTBADPC;
888 return;
889 }
890 tprintf("[%08lx] ", pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100891#elif defined(TILE)
892# ifdef _LP64
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500893 tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100894# else
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500895 tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100896# endif
Christian Svensson492f81f2013-02-14 13:26:27 +0100897#elif defined(OR1K)
898 tprintf("[%08lx] ", or1k_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100899#endif /* architecture */
900}
901
902#ifndef get_regs
903long get_regs_error;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100904void
905get_regs(pid_t pid)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100906{
907# if defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100908 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100909# elif defined(I386)
910 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &i386_regs);
911# elif defined(X86_64) || defined(X32)
Denys Vlasenkoe3b248d2013-02-15 00:24:19 +0100912 /*
913 * PTRACE_GETREGSET was introduced in 2.6.33.
914 * Let's be paranoid and require a bit later kernel.
915 */
916 if (os_release >= KERNEL_VERSION(2,6,35)) {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100917 /*x86_io.iov_base = &x86_regs_union; - already is */
918 x86_io.iov_len = sizeof(x86_regs_union);
919 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (long) &x86_io);
920 } else {
921 /* Use old method, with heuristical detection of 32-bitness */
922 x86_io.iov_len = sizeof(x86_64_regs);
923 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &x86_64_regs);
924 if (!get_regs_error && x86_64_regs.cs == 0x23) {
925 x86_io.iov_len = sizeof(i386_regs);
926 /*
927 * The order is important: i386_regs and x86_64_regs
928 * are overlaid in memory!
929 */
930 i386_regs.ebx = x86_64_regs.rbx;
931 i386_regs.ecx = x86_64_regs.rcx;
932 i386_regs.edx = x86_64_regs.rdx;
933 i386_regs.esi = x86_64_regs.rsi;
934 i386_regs.edi = x86_64_regs.rdi;
935 i386_regs.ebp = x86_64_regs.rbp;
936 i386_regs.eax = x86_64_regs.rax;
937 /*i386_regs.xds = x86_64_regs.ds; unused by strace */
938 /*i386_regs.xes = x86_64_regs.es; ditto... */
939 /*i386_regs.xfs = x86_64_regs.fs;*/
940 /*i386_regs.xgs = x86_64_regs.gs;*/
941 i386_regs.orig_eax = x86_64_regs.orig_rax;
942 i386_regs.eip = x86_64_regs.rip;
943 /*i386_regs.xcs = x86_64_regs.cs;*/
944 /*i386_regs.eflags = x86_64_regs.eflags;*/
945 i386_regs.esp = x86_64_regs.rsp;
946 /*i386_regs.xss = x86_64_regs.ss;*/
947 }
948 }
Denys Vlasenko401374e2013-02-06 18:24:39 +0100949# elif defined(ARM)
950 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (void *)&arm_regs);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100951# elif defined(AARCH64)
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100952 /*aarch64_io.iov_base = &arm_regs_union; - already is */
953 aarch64_io.iov_len = sizeof(arm_regs_union);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100954 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (void *)&aarch64_io);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100955# if 0
956 /* Paranoia checks */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100957 if (get_regs_error)
958 return;
959 switch (aarch64_io.iov_len) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100960 case sizeof(aarch64_regs):
961 /* We are in 64-bit mode */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100962 break;
963 case sizeof(arm_regs):
964 /* We are in 32-bit mode */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100965 break;
966 default:
967 get_regs_error = -1;
968 break;
969 }
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100970# endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100971# elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100972 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100973# elif defined(TILE)
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500974 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &tile_regs);
Christian Svensson492f81f2013-02-14 13:26:27 +0100975# elif defined(OR1K)
976 or1k_io.iov_len = sizeof(or1k_regs);
977 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &or1k_io);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100978# endif
979}
980#endif
981
Denys Vlasenkob88f9612011-08-21 18:03:23 +0200982/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +0100983 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
984 * 1: ok, continue in trace_syscall_entering().
985 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +0200986 * ("????" etc) and bail out.
987 */
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100988static int
Denys Vlasenko06602d92011-08-24 17:53:52 +0200989get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000990{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000991 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000992
Denys Vlasenko523635f2012-02-25 02:44:25 +0100993#if defined(S390) || defined(S390X)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +0000994 if (upeek(tcp, PT_GPR2, &syscall_mode) < 0)
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200995 return -1;
Roland McGrath2f924ca2003-06-26 22:23:28 +0000996
997 if (syscall_mode != -ENOSYS) {
998 /*
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000999 * Since kernel version 2.5.44 the scno gets passed in gpr2.
Roland McGrath2f924ca2003-06-26 22:23:28 +00001000 */
1001 scno = syscall_mode;
1002 } else {
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001003 /*
Michal Ludvig882eda82002-11-11 12:50:47 +00001004 * Old style of "passing" the scno via the SVC instruction.
1005 */
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001006 long psw;
Michal Ludvig882eda82002-11-11 12:50:47 +00001007 long opcode, offset_reg, tmp;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001008 void *svc_addr;
Denys Vlasenko7c9ba8b2011-08-19 19:46:32 +02001009 static const int gpr_offset[16] = {
1010 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
1011 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
1012 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
1013 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
1014 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001015
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001016 if (upeek(tcp, PT_PSWADDR, &psw) < 0)
Michal Ludvig882eda82002-11-11 12:50:47 +00001017 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001018 errno = 0;
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001019 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +00001020 if (errno) {
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001021 perror_msg("%s", "peektext(psw-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +00001022 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001023 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001024
1025 /*
1026 * We have to check if the SVC got executed directly or via an
1027 * EXECUTE instruction. In case of EXECUTE it is necessary to do
1028 * instruction decoding to derive the system call number.
1029 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1030 * so that this doesn't work if a SVC opcode is part of an EXECUTE
1031 * opcode. Since there is no way to find out the opcode size this
1032 * is the best we can do...
1033 */
Michal Ludvig882eda82002-11-11 12:50:47 +00001034 if ((opcode & 0xff00) == 0x0a00) {
1035 /* SVC opcode */
1036 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +00001037 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001038 else {
1039 /* SVC got executed by EXECUTE instruction */
1040
1041 /*
1042 * Do instruction decoding of EXECUTE. If you really want to
1043 * understand this, read the Principles of Operations.
1044 */
1045 svc_addr = (void *) (opcode & 0xfff);
1046
1047 tmp = 0;
1048 offset_reg = (opcode & 0x000f0000) >> 16;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001049 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001050 return -1;
1051 svc_addr += tmp;
1052
1053 tmp = 0;
1054 offset_reg = (opcode & 0x0000f000) >> 12;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001055 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001056 return -1;
1057 svc_addr += tmp;
1058
Denys Vlasenkofb036672009-01-23 16:30:26 +00001059 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
Michal Ludvig882eda82002-11-11 12:50:47 +00001060 if (errno)
1061 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001062# if defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +00001063 scno >>= 48;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001064# else
Michal Ludvig882eda82002-11-11 12:50:47 +00001065 scno >>= 16;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001066# endif
Michal Ludvig882eda82002-11-11 12:50:47 +00001067 tmp = 0;
1068 offset_reg = (opcode & 0x00f00000) >> 20;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001069 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001070 return -1;
1071
1072 scno = (scno | tmp) & 0xff;
1073 }
1074 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001075#elif defined(POWERPC)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001076 if (upeek(tcp, sizeof(unsigned long)*PT_R0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001077 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001078# ifdef POWERPC64
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001079 /* TODO: speed up strace by not doing this at every syscall.
1080 * We only need to do it after execve.
1081 */
1082 int currpers;
1083 long val;
Andreas Schwabd69fa492010-07-12 21:39:57 +02001084
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001085 /* Check for 64/32 bit mode. */
1086 if (upeek(tcp, sizeof(unsigned long)*PT_MSR, &val) < 0)
1087 return -1;
1088 /* SF is bit 0 of MSR */
1089 if (val < 0)
1090 currpers = 0;
1091 else
1092 currpers = 1;
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001093 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001094# endif
1095#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001096 scno = avr32_regs.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001097#elif defined(BFIN)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001098 if (upeek(tcp, PT_ORIG_P0, &scno))
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001099 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001100#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001101 scno = i386_regs.orig_eax;
H.J. Lu35be5812012-04-16 13:00:01 +02001102#elif defined(X86_64) || defined(X32)
1103# ifndef __X32_SYSCALL_BIT
1104# define __X32_SYSCALL_BIT 0x40000000
1105# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001106 int currpers;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001107# if 1
1108 /* GETREGSET of NT_PRSTATUS tells us regset size,
1109 * which unambiguously detects i386.
1110 *
1111 * Linux kernel distinguishes x86-64 and x32 processes
1112 * solely by looking at __X32_SYSCALL_BIT:
1113 * arch/x86/include/asm/compat.h::is_x32_task():
1114 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1115 * return true;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001116 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001117 if (x86_io.iov_len == sizeof(i386_regs)) {
1118 scno = i386_regs.orig_eax;
1119 currpers = 1;
1120 } else {
1121 scno = x86_64_regs.orig_rax;
1122 currpers = 0;
1123 if (scno & __X32_SYSCALL_BIT) {
1124 scno -= __X32_SYSCALL_BIT;
1125 currpers = 2;
1126 }
1127 }
1128# elif 0
1129 /* cs = 0x33 for long mode (native 64 bit and x32)
1130 * cs = 0x23 for compatibility mode (32 bit)
1131 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1132 */
1133 scno = x86_64_regs.orig_rax;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001134 switch (x86_64_regs.cs) {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001135 case 0x23: currpers = 1; break;
H.J. Lu35be5812012-04-16 13:00:01 +02001136 case 0x33:
1137 if (x86_64_regs.ds == 0x2b) {
1138 currpers = 2;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001139 scno &= ~__X32_SYSCALL_BIT;
H.J. Lu35be5812012-04-16 13:00:01 +02001140 } else
1141 currpers = 0;
1142 break;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001143 default:
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001144 fprintf(stderr, "Unknown value CS=0x%08X while "
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001145 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001146 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001147 currpers = current_personality;
1148 break;
1149 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001150# elif 0
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001151 /* This version analyzes the opcode of a syscall instruction.
1152 * (int 0x80 on i386 vs. syscall on x86-64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001153 * It works, but is too complicated, and strictly speaking, unreliable.
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001154 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001155 unsigned long call, rip = x86_64_regs.rip;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001156 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1157 rip -= 2;
1158 errno = 0;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001159 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001160 if (errno)
1161 fprintf(stderr, "ptrace_peektext failed: %s\n",
1162 strerror(errno));
1163 switch (call & 0xffff) {
1164 /* x86-64: syscall = 0x0f 0x05 */
1165 case 0x050f: currpers = 0; break;
1166 /* i386: int 0x80 = 0xcd 0x80 */
1167 case 0x80cd: currpers = 1; break;
1168 default:
1169 currpers = current_personality;
1170 fprintf(stderr,
1171 "Unknown syscall opcode (0x%04X) while "
1172 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001173 "PID=%d\n", (int)call, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001174 break;
1175 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001176# endif
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001177
H.J. Lu35be5812012-04-16 13:00:01 +02001178# ifdef X32
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001179 /* If we are built for a x32 system, then personality 0 is x32
1180 * (not x86_64), and stracing of x86_64 apps is not supported.
1181 * Stracing of i386 apps is still supported.
H.J. Lu085e4282012-04-17 11:05:04 -07001182 */
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001183 if (currpers == 0) {
1184 fprintf(stderr, "syscall_%lu(...) in unsupported "
1185 "64-bit mode of process PID=%d\n",
1186 scno, tcp->pid);
1187 return 0;
H.J. Lu35be5812012-04-16 13:00:01 +02001188 }
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001189 currpers &= ~2; /* map 2,1 to 0,1 */
H.J. Lu35be5812012-04-16 13:00:01 +02001190# endif
H.J. Lu085e4282012-04-17 11:05:04 -07001191 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001192#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001193# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01001194 long psr;
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001195 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001196 ia32 = (psr & IA64_PSR_IS) != 0;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001197 if (ia32) {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001198 if (upeek(tcp, PT_R1, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001199 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001200 } else {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001201 if (upeek(tcp, PT_R15, &scno) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001202 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001203 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001204#elif defined(AARCH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001205 switch (aarch64_io.iov_len) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001206 case sizeof(aarch64_regs):
1207 /* We are in 64-bit mode */
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001208 scno = aarch64_regs.regs[8];
1209 update_personality(tcp, 1);
1210 break;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001211 case sizeof(arm_regs):
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001212 /* We are in 32-bit mode */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001213 scno = arm_regs.ARM_r7;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001214 update_personality(tcp, 0);
1215 break;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001216 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001217#elif defined(ARM)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001218 if (arm_regs.ARM_ip != 0) {
1219 /* It is not a syscall entry */
1220 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001221 tcp->flags |= TCB_INSYSCALL;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001222 return 0;
1223 }
1224 /* Note: we support only 32-bit CPUs, not 26-bit */
1225
1226 if (arm_regs.ARM_cpsr & 0x20) {
1227 /* Thumb mode */
1228 scno = arm_regs.ARM_r7;
1229 } else {
1230 /* ARM mode */
1231 errno = 0;
1232 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1233 if (errno)
1234 return -1;
1235
1236 /* EABI syscall convention? */
1237 if (scno == 0xef000000) {
1238 scno = arm_regs.ARM_r7; /* yes */
1239 } else {
1240 if ((scno & 0x0ff00000) != 0x0f900000) {
1241 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1242 tcp->pid, scno);
1243 return -1;
1244 }
1245 /* Fixup the syscall number */
1246 scno &= 0x000fffff;
1247 }
1248 }
1249 if (scno & 0x000f0000) {
1250 /* ARM specific syscall. We handle it as a separate "personality" */
1251 update_personality(tcp, 1);
1252 scno &= 0x0000ffff;
1253 } else {
1254 update_personality(tcp, 0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001255 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001256#elif defined(M68K)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001257 if (upeek(tcp, 4*PT_ORIG_D0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001258 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001259#elif defined(LINUX_MIPSN32)
Roland McGrath542c2c62008-05-20 01:11:56 +00001260 unsigned long long regs[38];
1261
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001262 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001263 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001264 mips_a3 = regs[REG_A3];
1265 mips_r2 = regs[REG_V0];
Roland McGrath542c2c62008-05-20 01:11:56 +00001266
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001267 scno = mips_r2;
Denys Vlasenko5721cdb2013-02-16 13:22:38 +01001268 if (!SCNO_IS_VALID(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001269 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001270 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001271 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001272 return 0;
1273 }
Roland McGrath542c2c62008-05-20 01:11:56 +00001274 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001275#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001276 if (upeek(tcp, REG_A3, &mips_a3) < 0)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001277 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001278 if (upeek(tcp, REG_V0, &scno) < 0)
1279 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001280
Denys Vlasenko5721cdb2013-02-16 13:22:38 +01001281 if (!SCNO_IS_VALID(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001282 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001283 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001284 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001285 return 0;
1286 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001287 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001288#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001289 if (upeek(tcp, REG_A3, &alpha_a3) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001290 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001291 if (upeek(tcp, REG_R0, &scno) < 0)
1292 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001293
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001294 /*
1295 * Do some sanity checks to figure out if it's
1296 * really a syscall entry
1297 */
Denys Vlasenko5721cdb2013-02-16 13:22:38 +01001298 if (!SCNO_IS_VALID(scno)) {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001299 if (alpha_a3 == 0 || alpha_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001300 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001301 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001302 return 0;
1303 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001304 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001305#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001306 /* Disassemble the syscall trap. */
1307 /* Retrieve the syscall trap instruction. */
Denys Vlasenko46455822013-02-05 17:02:59 +01001308 unsigned long trap;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001309 errno = 0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001310# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001311 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001312 trap >>= 32;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001313# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001314 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001315# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001316 if (errno)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001317 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001318
1319 /* Disassemble the trap to see what personality to use. */
1320 switch (trap) {
1321 case 0x91d02010:
1322 /* Linux/SPARC syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001323 update_personality(tcp, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001324 break;
1325 case 0x91d0206d:
1326 /* Linux/SPARC64 syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001327 update_personality(tcp, 2);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001328 break;
1329 case 0x91d02000:
1330 /* SunOS syscall trap. (pers 1) */
1331 fprintf(stderr, "syscall: SunOS no support\n");
1332 return -1;
1333 case 0x91d02008:
1334 /* Solaris 2.x syscall trap. (per 2) */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001335 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001336 break;
1337 case 0x91d02009:
1338 /* NetBSD/FreeBSD syscall trap. */
1339 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1340 return -1;
1341 case 0x91d02027:
1342 /* Solaris 2.x gettimeofday */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001343 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001344 break;
1345 default:
Denys Vlasenko523635f2012-02-25 02:44:25 +01001346# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001347 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001348# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001349 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001350# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001351 return -1;
1352 }
1353
1354 /* Extract the system call number from the registers. */
1355 if (trap == 0x91d02027)
1356 scno = 156;
1357 else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001358 scno = sparc_regs.u_regs[U_REG_G1];
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001359 if (scno == 0) {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001360 scno = sparc_regs.u_regs[U_REG_O0];
1361 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 +02001362 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001363#elif defined(HPPA)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001364 if (upeek(tcp, PT_GR20, &scno) < 0)
1365 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001366#elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001367 /*
1368 * In the new syscall ABI, the system call number is in R3.
1369 */
1370 if (upeek(tcp, 4*(REG_REG0+3), &scno) < 0)
1371 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001372
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001373 if (scno < 0) {
1374 /* Odd as it may seem, a glibc bug has been known to cause
1375 glibc to issue bogus negative syscall numbers. So for
1376 our purposes, make strace print what it *should* have been */
1377 long correct_scno = (scno & 0xff);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001378 if (debug_flag)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001379 fprintf(stderr,
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001380 "Detected glibc bug: bogus system call"
1381 " number = %ld, correcting to %ld\n",
1382 scno,
1383 correct_scno);
1384 scno = correct_scno;
1385 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001386#elif defined(SH64)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001387 if (upeek(tcp, REG_SYSCALL, &scno) < 0)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001388 return -1;
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001389 scno &= 0xFFFF;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001390#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001391 if (upeek(tcp, 4*PT_R9, &scno) < 0)
1392 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001393#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001394 int currpers;
1395 scno = tile_regs.regs[10];
1396# ifdef __tilepro__
1397 currpers = 1;
1398# else
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001399# ifndef PT_FLAGS_COMPAT
1400# define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1401# endif
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001402 if (tile_regs.flags & PT_FLAGS_COMPAT)
1403 currpers = 1;
1404 else
1405 currpers = 0;
1406# endif
1407 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001408#elif defined(MICROBLAZE)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001409 if (upeek(tcp, 0, &scno) < 0)
1410 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001411#elif defined(OR1K)
1412 scno = or1k_regs.gpr[11];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001413#endif
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001414
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001415 tcp->scno = scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +00001416 return 1;
1417}
1418
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02001419/* Called at each syscall entry.
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001420 * Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001421 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1422 * 1: ok, continue in trace_syscall_entering().
1423 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001424 * ("????" etc) and bail out.
1425 */
Roland McGratha4d48532005-06-08 20:45:28 +00001426static int
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001427syscall_fixup_on_sysenter(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001428{
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001429 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001430#if defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001431 if (i386_regs.eax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001432 if (debug_flag)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001433 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1434 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001435 }
H.J. Lu35be5812012-04-16 13:00:01 +02001436#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001437 {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001438 long rax;
1439 if (x86_io.iov_len == sizeof(i386_regs)) {
1440 /* Sign extend from 32 bits */
1441 rax = (int32_t)i386_regs.eax;
1442 } else {
1443 /* Note: in X32 build, this truncates 64 to 32 bits */
1444 rax = x86_64_regs.rax;
1445 }
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001446 if (rax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001447 if (debug_flag)
Denys Vlasenko18beb982011-08-24 16:59:23 +02001448 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1449 return 0;
1450 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001451 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001452#elif defined(S390) || defined(S390X)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001453 /* TODO: we already fetched PT_GPR2 in get_scno
1454 * and stored it in syscall_mode, reuse it here
1455 * instead of re-fetching?
1456 */
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001457 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001458 return -1;
Michal Ludvig882eda82002-11-11 12:50:47 +00001459 if (syscall_mode != -ENOSYS)
1460 syscall_mode = tcp->scno;
Denys Vlasenkoece98792011-08-25 10:25:35 +02001461 if (gpr2 != syscall_mode) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001462 if (debug_flag)
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001463 fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2);
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001464 return 0;
1465 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001466#elif defined(M68K)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001467 /* TODO? Eliminate upeek's in arches below like we did in x86 */
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001468 if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001469 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001470 if (m68k_d0 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001471 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001472 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001473 return 0;
1474 }
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001475#elif defined(IA64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001476 if (upeek(tcp, PT_R10, &ia64_r10) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001477 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001478 if (upeek(tcp, PT_R8, &ia64_r8) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001479 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001480 if (ia32 && ia64_r8 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001481 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001482 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001483 return 0;
1484 }
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001485#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001486 if (upeek(tcp, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001487 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001488 if (cris_r10 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001489 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001490 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001491 return 0;
1492 }
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001493#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001494 if (upeek(tcp, 3 * 4, &microblaze_r3) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001495 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001496 if (microblaze_r3 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001497 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001498 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001499 return 0;
1500 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001501#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001502 return 1;
1503}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001504
Denys Vlasenko146b9442012-03-18 22:10:48 +01001505static void
1506internal_fork(struct tcb *tcp)
1507{
1508#if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1509# define ARG_FLAGS 1
1510#else
1511# define ARG_FLAGS 0
1512#endif
1513#ifndef CLONE_UNTRACED
1514# define CLONE_UNTRACED 0x00800000
1515#endif
1516 if ((ptrace_setoptions
1517 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1518 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1519 return;
1520
1521 if (!followfork)
1522 return;
1523
1524 if (entering(tcp)) {
1525 /*
1526 * We won't see the new child if clone is called with
1527 * CLONE_UNTRACED, so we keep the same logic with that option
1528 * and don't trace it.
1529 */
1530 if ((sysent[tcp->scno].sys_func == sys_clone) &&
1531 (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED))
1532 return;
1533 setbpt(tcp);
1534 } else {
1535 if (tcp->flags & TCB_BPTSET)
1536 clearbpt(tcp);
1537 }
1538}
1539
1540#if defined(TCB_WAITEXECVE)
1541static void
1542internal_exec(struct tcb *tcp)
1543{
1544 /* Maybe we have post-execve SIGTRAP suppressed? */
1545 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1546 return; /* yes, no need to do anything */
1547
1548 if (exiting(tcp) && syserror(tcp))
1549 /* Error in execve, no post-execve SIGTRAP expected */
1550 tcp->flags &= ~TCB_WAITEXECVE;
1551 else
1552 tcp->flags |= TCB_WAITEXECVE;
1553}
1554#endif
1555
1556static void
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001557syscall_fixup_for_fork_exec(struct tcb *tcp)
Roland McGrathc1e45922008-05-27 23:18:29 +00001558{
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001559 /*
1560 * We must always trace a few critical system calls in order to
1561 * correctly support following forks in the presence of tracing
1562 * qualifiers.
1563 */
1564 int (*func)();
1565
Denys Vlasenko5721cdb2013-02-16 13:22:38 +01001566 if (!SCNO_IS_VALID(tcp->scno))
Denys Vlasenko146b9442012-03-18 22:10:48 +01001567 return;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001568
1569 func = sysent[tcp->scno].sys_func;
1570
1571 if ( sys_fork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001572 || sys_vfork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001573 || sys_clone == func
Denys Vlasenko146b9442012-03-18 22:10:48 +01001574 ) {
1575 internal_fork(tcp);
1576 return;
1577 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001578
Denys Vlasenko84703742012-02-25 02:38:52 +01001579#if defined(TCB_WAITEXECVE)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001580 if ( sys_execve == func
Denys Vlasenko84703742012-02-25 02:38:52 +01001581# if defined(SPARC) || defined(SPARC64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001582 || sys_execv == func
Denys Vlasenkoa7949742011-08-21 17:26:55 +02001583# endif
Denys Vlasenko146b9442012-03-18 22:10:48 +01001584 ) {
1585 internal_exec(tcp);
1586 return;
1587 }
Roland McGrathc1e45922008-05-27 23:18:29 +00001588#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001589}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001590
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001591/* Return -1 on error or 1 on success (never 0!) */
Roland McGratha4d48532005-06-08 20:45:28 +00001592static int
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001593get_syscall_args(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001594{
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001595 int i, nargs;
1596
Denys Vlasenko5721cdb2013-02-16 13:22:38 +01001597 if (SCNO_IS_VALID(tcp->scno))
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001598 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001599 else
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001600 nargs = tcp->u_nargs = MAX_ARGS;
1601
Denys Vlasenko523635f2012-02-25 02:44:25 +01001602#if defined(S390) || defined(S390X)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001603 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001604 if (upeek(tcp, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
1605 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001606#elif defined(ALPHA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001607 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001608 if (upeek(tcp, REG_A0+i, &tcp->u_arg[i]) < 0)
1609 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001610#elif defined(IA64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001611 if (!ia32) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001612 unsigned long *out0, cfm, sof, sol;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001613 long rbs_end;
1614 /* be backwards compatible with kernel < 2.4.4... */
1615# ifndef PT_RBS_END
1616# define PT_RBS_END PT_AR_BSP
1617# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001618
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001619 if (upeek(tcp, PT_RBS_END, &rbs_end) < 0)
1620 return -1;
1621 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001622 return -1;
1623
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001624 sof = (cfm >> 0) & 0x7f;
1625 sol = (cfm >> 7) & 0x7f;
1626 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1627
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001628 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001629 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1630 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1631 return -1;
1632 }
1633 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001634 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1635 PT_R9 /* ECX = out1 */,
1636 PT_R10 /* EDX = out2 */,
1637 PT_R14 /* ESI = out3 */,
1638 PT_R15 /* EDI = out4 */,
1639 PT_R13 /* EBP = out5 */};
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001640
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001641 for (i = 0; i < nargs; ++i) {
1642 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1643 return -1;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001644 /* truncate away IVE sign-extension */
1645 tcp->u_arg[i] &= 0xffffffff;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001646 }
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001647 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001648#elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001649 /* N32 and N64 both use up to six registers. */
1650 unsigned long long regs[38];
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001651
1652 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1653 return -1;
1654
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001655 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001656 tcp->u_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001657# if defined(LINUX_MIPSN32)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001658 tcp->ext_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001659# endif
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001660 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001661#elif defined(MIPS)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001662 if (nargs > 4) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001663 long sp;
1664
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001665 if (upeek(tcp, REG_SP, &sp) < 0)
1666 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001667 for (i = 0; i < 4; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001668 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1669 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001670 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001671 (char *)(tcp->u_arg + 4));
1672 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001673 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001674 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001675 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001676 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001677#elif defined(POWERPC)
1678# ifndef PT_ORIG_R3
1679# define PT_ORIG_R3 34
1680# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001681 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001682 if (upeek(tcp, (i==0) ?
1683 (sizeof(unsigned long) * PT_ORIG_R3) :
1684 ((i+PT_R3) * sizeof(unsigned long)),
1685 &tcp->u_arg[i]) < 0)
1686 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001687 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001688#elif defined(SPARC) || defined(SPARC64)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001689 for (i = 0; i < nargs; ++i)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001690 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001691#elif defined(HPPA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001692 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001693 if (upeek(tcp, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1694 return -1;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001695#elif defined(ARM) || defined(AARCH64)
1696# if defined(AARCH64)
1697 if (tcp->currpers == 1)
1698 for (i = 0; i < nargs; ++i)
1699 tcp->u_arg[i] = aarch64_regs.regs[i];
1700 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001701# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001702 for (i = 0; i < nargs; ++i)
Denys Vlasenko401374e2013-02-06 18:24:39 +01001703 tcp->u_arg[i] = arm_regs.uregs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001704#elif defined(AVR32)
Denys Vlasenkob5b25892011-08-30 19:04:54 +02001705 (void)i;
1706 (void)nargs;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001707 tcp->u_arg[0] = avr32_regs.r12;
1708 tcp->u_arg[1] = avr32_regs.r11;
1709 tcp->u_arg[2] = avr32_regs.r10;
1710 tcp->u_arg[3] = avr32_regs.r9;
1711 tcp->u_arg[4] = avr32_regs.r5;
1712 tcp->u_arg[5] = avr32_regs.r3;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001713#elif defined(BFIN)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001714 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 +02001715
Denys Vlasenko4b887a52011-08-23 13:32:38 +02001716 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001717 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1718 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001719#elif defined(SH)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001720 static const int syscall_regs[MAX_ARGS] = {
1721 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1722 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001723 };
1724
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001725 for (i = 0; i < nargs; ++i)
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001726 if (upeek(tcp, syscall_regs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001727 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001728#elif defined(SH64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001729 int i;
1730 /* Registers used by SH5 Linux system calls for parameters */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001731 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001732
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001733 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001734 if (upeek(tcp, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1735 return -1;
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001736#elif defined(I386)
1737 (void)i;
1738 (void)nargs;
1739 tcp->u_arg[0] = i386_regs.ebx;
1740 tcp->u_arg[1] = i386_regs.ecx;
1741 tcp->u_arg[2] = i386_regs.edx;
1742 tcp->u_arg[3] = i386_regs.esi;
1743 tcp->u_arg[4] = i386_regs.edi;
1744 tcp->u_arg[5] = i386_regs.ebp;
H.J. Lu35be5812012-04-16 13:00:01 +02001745#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001746 (void)i;
1747 (void)nargs;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001748 if (x86_io.iov_len != sizeof(i386_regs)) {
1749 /* x86-64 or x32 ABI */
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001750 tcp->u_arg[0] = x86_64_regs.rdi;
1751 tcp->u_arg[1] = x86_64_regs.rsi;
1752 tcp->u_arg[2] = x86_64_regs.rdx;
1753 tcp->u_arg[3] = x86_64_regs.r10;
1754 tcp->u_arg[4] = x86_64_regs.r8;
1755 tcp->u_arg[5] = x86_64_regs.r9;
H.J. Lu35be5812012-04-16 13:00:01 +02001756# ifdef X32
1757 tcp->ext_arg[0] = x86_64_regs.rdi;
1758 tcp->ext_arg[1] = x86_64_regs.rsi;
1759 tcp->ext_arg[2] = x86_64_regs.rdx;
1760 tcp->ext_arg[3] = x86_64_regs.r10;
1761 tcp->ext_arg[4] = x86_64_regs.r8;
1762 tcp->ext_arg[5] = x86_64_regs.r9;
1763# endif
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001764 } else {
1765 /* i386 ABI */
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001766 /* Zero-extend from 32 bits */
1767 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1768 * if you need to use *sign-extended* parameter.
1769 */
1770 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1771 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1772 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1773 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1774 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1775 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001776 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001777#elif defined(MICROBLAZE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001778 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001779 if (upeek(tcp, (5 + i) * 4, &tcp->u_arg[i]) < 0)
1780 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001781#elif defined(CRISV10) || defined(CRISV32)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001782 static const int crisregs[MAX_ARGS] = {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001783 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001784 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001785 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001786
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, crisregs[i], &tcp->u_arg[i]) < 0)
1789 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001790#elif defined(TILE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001791 for (i = 0; i < nargs; ++i)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001792 tcp->u_arg[i] = tile_regs.regs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001793#elif defined(M68K)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001794 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001795 if (upeek(tcp, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
1796 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001797#elif defined(OR1K)
1798 (void)nargs;
1799 for (i = 0; i < 6; ++i)
1800 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001801#else /* Other architecture (32bits specific) */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001802 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001803 if (upeek(tcp, i*4, &tcp->u_arg[i]) < 0)
1804 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001805#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001806 return 1;
1807}
1808
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00001809static int
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001810trace_syscall_entering(struct tcb *tcp)
1811{
1812 int res, scno_good;
1813
Denys Vlasenko2ce12ed2011-08-24 17:25:32 +02001814#if defined TCB_WAITEXECVE
1815 if (tcp->flags & TCB_WAITEXECVE) {
1816 /* This is the post-execve SIGTRAP. */
1817 tcp->flags &= ~TCB_WAITEXECVE;
1818 return 0;
1819 }
1820#endif
1821
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001822 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001823 if (res == 0)
1824 return res;
Denys Vlasenko907735a2012-03-21 00:23:16 +01001825 if (res == 1) {
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001826 res = syscall_fixup_on_sysenter(tcp);
Denys Vlasenko907735a2012-03-21 00:23:16 +01001827 if (res == 0)
1828 return res;
1829 if (res == 1)
1830 res = get_syscall_args(tcp);
1831 }
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001832
1833 if (res != 1) {
1834 printleader(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001835 if (scno_good != 1)
Denys Vlasenkob7a6dae2012-03-20 16:48:35 +01001836 tprints("????" /* anti-trigraph gap */ "(");
Denys Vlasenko5721cdb2013-02-16 13:22:38 +01001837 else if (!SCNO_IS_VALID(tcp->scno))
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001838 tprintf("syscall_%lu(", tcp->scno);
1839 else
1840 tprintf("%s(", sysent[tcp->scno].sys_name);
1841 /*
1842 * " <unavailable>" will be added later by the code which
1843 * detects ptrace errors.
1844 */
1845 goto ret;
1846 }
1847
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001848#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
Denys Vlasenko5721cdb2013-02-16 13:22:38 +01001849 while (SCNO_IS_VALID(tcp->scno)) {
Denys Vlasenko523635f2012-02-25 02:44:25 +01001850# ifdef SYS_socket_subcall
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001851 if (sysent[tcp->scno].sys_func == sys_socketcall) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00001852 decode_socket_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001853 break;
1854 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001855# endif
1856# ifdef SYS_ipc_subcall
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001857 if (sysent[tcp->scno].sys_func == sys_ipc) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00001858 decode_ipc_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001859 break;
1860 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001861# endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001862 break;
1863 }
1864#endif /* SYS_socket_subcall || SYS_ipc_subcall */
1865
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001866 if (need_fork_exec_workarounds)
1867 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001868
Denys Vlasenko5721cdb2013-02-16 13:22:38 +01001869 if ((SCNO_IS_VALID(tcp->scno) &&
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001870 !(qual_flags[tcp->scno] & QUAL_TRACE)) ||
1871 (tracing_paths && !pathtrace_match(tcp))) {
1872 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
1873 return 0;
1874 }
1875
1876 tcp->flags &= ~TCB_FILTERED;
1877
1878 if (cflag == CFLAG_ONLY_STATS) {
1879 res = 0;
1880 goto ret;
1881 }
1882
1883 printleader(tcp);
Denys Vlasenko5721cdb2013-02-16 13:22:38 +01001884 if (!SCNO_IS_VALID(tcp->scno))
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001885 tprintf("syscall_%lu(", tcp->scno);
1886 else
1887 tprintf("%s(", sysent[tcp->scno].sys_name);
Denys Vlasenko5721cdb2013-02-16 13:22:38 +01001888 if (!SCNO_IS_VALID(tcp->scno) ||
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001889 ((qual_flags[tcp->scno] & QUAL_RAW) &&
1890 sysent[tcp->scno].sys_func != sys_exit))
1891 res = printargs(tcp);
1892 else
1893 res = (*sysent[tcp->scno].sys_func)(tcp);
1894
Dmitry V. Levinb742d8c2012-09-17 22:40:12 +00001895 fflush(tcp->outf);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001896 ret:
1897 tcp->flags |= TCB_INSYSCALL;
1898 /* Measure the entrance time as late as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001899 if (Tflag || cflag)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001900 gettimeofday(&tcp->etime, NULL);
1901 return res;
1902}
1903
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001904/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001905 * 1: ok, continue in trace_syscall_exiting().
1906 * -1: error, trace_syscall_exiting() should print error indicator
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001907 * ("????" etc) and bail out.
1908 */
1909static int
1910get_syscall_result(struct tcb *tcp)
1911{
Denys Vlasenko523635f2012-02-25 02:44:25 +01001912#if defined(S390) || defined(S390X)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001913 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
1914 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001915#elif defined(POWERPC)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001916# define SO_MASK 0x10000000
1917 {
1918 long flags;
1919 if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1920 return -1;
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01001921 if (upeek(tcp, sizeof(unsigned long)*PT_R3, &ppc_result) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001922 return -1;
1923 if (flags & SO_MASK)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01001924 ppc_result = -ppc_result;
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001925 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001926#elif defined(AVR32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001927 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001928#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001929 if (upeek(tcp, PT_R0, &bfin_r0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001930 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001931#elif defined(I386)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001932 /* already done by get_regs */
H.J. Lu35be5812012-04-16 13:00:01 +02001933#elif defined(X86_64) || defined(X32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001934 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001935#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001936# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01001937 long psr;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001938 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
1939 ia32 = (psr & IA64_PSR_IS) != 0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001940 if (upeek(tcp, PT_R8, &ia64_r8) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001941 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001942 if (upeek(tcp, PT_R10, &ia64_r10) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001943 return -1;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001944#elif defined(ARM)
1945 /* already done by get_regs */
Steve McIntyred8d3bd32012-10-24 17:58:16 +01001946#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001947 /* register reading already done by get_regs */
1948
1949 /* Used to do this, but we did it on syscall entry already: */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001950 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
1951 * else it's personality 0.
1952 */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001953 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
Denys Vlasenko523635f2012-02-25 02:44:25 +01001954#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001955 if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001956 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001957#elif defined(LINUX_MIPSN32)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001958 unsigned long long regs[38];
1959
1960 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1961 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001962 mips_a3 = regs[REG_A3];
1963 mips_r2 = regs[REG_V0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001964#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001965 if (upeek(tcp, REG_A3, &mips_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001966 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001967 if (upeek(tcp, REG_V0, &mips_r2) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001968 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001969#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001970 if (upeek(tcp, REG_A3, &alpha_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001971 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001972 if (upeek(tcp, REG_R0, &alpha_r0) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001973 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001974#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001975 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001976#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001977 if (upeek(tcp, PT_GR28, &hppa_r28) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001978 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001979#elif defined(SH)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001980 /* new syscall ABI returns result in R0 */
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001981 if (upeek(tcp, 4*REG_REG0, (long *)&sh_r0) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001982 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001983#elif defined(SH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001984 /* ABI defines result returned in r9 */
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001985 if (upeek(tcp, REG_GENERAL(9), (long *)&sh64_r9) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001986 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001987#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001988 if (upeek(tcp, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001989 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001990#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001991 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001992#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001993 if (upeek(tcp, 3 * 4, &microblaze_r3) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001994 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001995#elif defined(OR1K)
1996 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001997#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001998 return 1;
1999}
2000
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002001/* Called at each syscall exit */
2002static void
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002003syscall_fixup_on_sysexit(struct tcb *tcp)
2004{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002005#if defined(S390) || defined(S390X)
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002006 if (syscall_mode != -ENOSYS)
2007 syscall_mode = tcp->scno;
Denys Vlasenkoece98792011-08-25 10:25:35 +02002008 if ((tcp->flags & TCB_WAITEXECVE)
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002009 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
2010 /*
2011 * Return from execve.
2012 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2013 * flag set for the post-execve SIGTRAP to see and reset.
2014 */
2015 gpr2 = 0;
2016 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002017#endif
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002018}
2019
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002020/*
2021 * Check the syscall return value register value for whether it is
2022 * a negated errno code indicating an error, or a success return value.
2023 */
2024static inline int
2025is_negated_errno(unsigned long int val)
2026{
2027 unsigned long int max = -(long int) nerrnos;
Denys Vlasenko2544f982013-02-19 17:39:56 +01002028#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01002029 if (current_wordsize < sizeof(val)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002030 val = (unsigned int) val;
2031 max = (unsigned int) max;
2032 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002033#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002034 return val > max;
2035}
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002036
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002037#if defined(X32)
2038static inline int
2039is_negated_errno_x32(unsigned long long val)
2040{
2041 unsigned long long max = -(long long) nerrnos;
2042 /*
2043 * current_wordsize is 4 even in personality 0 (native X32)
2044 * but truncation _must not_ be done in it.
2045 * can't check current_wordsize here!
2046 */
2047 if (current_personality != 0) {
2048 val = (uint32_t) val;
2049 max = (uint32_t) max;
2050 }
2051 return val > max;
2052}
2053#endif
2054
Denys Vlasenko907735a2012-03-21 00:23:16 +01002055/* Returns:
2056 * 1: ok, continue in trace_syscall_exiting().
2057 * -1: error, trace_syscall_exiting() should print error indicator
2058 * ("????" etc) and bail out.
2059 */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002060static void
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002061get_error(struct tcb *tcp)
2062{
2063 int u_error = 0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002064 int check_errno = 1;
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002065 if (SCNO_IN_RANGE(tcp->scno)
Denys Vlasenko5721cdb2013-02-16 13:22:38 +01002066 && (sysent[tcp->scno].sys_flags & SYSCALL_NEVER_FAILS)
2067 ) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002068 check_errno = 0;
2069 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002070#if defined(S390) || defined(S390X)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002071 if (check_errno && is_negated_errno(gpr2)) {
2072 tcp->u_rval = -1;
2073 u_error = -gpr2;
2074 }
2075 else {
2076 tcp->u_rval = gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002077 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002078#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002079 if (check_errno && is_negated_errno(i386_regs.eax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002080 tcp->u_rval = -1;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002081 u_error = -i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002082 }
2083 else {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002084 tcp->u_rval = i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002085 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002086#elif defined(X86_64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002087 long rax;
2088 if (x86_io.iov_len == sizeof(i386_regs)) {
2089 /* Sign extend from 32 bits */
2090 rax = (int32_t)i386_regs.eax;
2091 } else {
2092 rax = x86_64_regs.rax;
2093 }
2094 if (check_errno && is_negated_errno(rax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002095 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002096 u_error = -rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002097 }
2098 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002099 tcp->u_rval = rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002100 }
2101#elif defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002102 /* In X32, return value is 64-bit (llseek uses one).
2103 * Using merely "long rax" would not work.
2104 */
2105 long long rax;
2106 if (x86_io.iov_len == sizeof(i386_regs)) {
2107 /* Sign extend from 32 bits */
2108 rax = (int32_t)i386_regs.eax;
2109 } else {
2110 rax = x86_64_regs.rax;
2111 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002112 /* Careful: is_negated_errno() works only on longs */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002113 if (check_errno && is_negated_errno_x32(rax)) {
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002114 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002115 u_error = -rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002116 }
2117 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002118 tcp->u_rval = rax; /* truncating */
2119 tcp->u_lrval = rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002120 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002121#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002122 if (ia32) {
2123 int err;
2124
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002125 err = (int)ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002126 if (check_errno && is_negated_errno(err)) {
2127 tcp->u_rval = -1;
2128 u_error = -err;
2129 }
2130 else {
2131 tcp->u_rval = err;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002132 }
2133 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002134 if (check_errno && ia64_r10) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002135 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002136 u_error = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002137 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002138 tcp->u_rval = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002139 }
2140 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002141#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002142 if (check_errno && mips_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002143 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002144 u_error = mips_r2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002145 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002146 tcp->u_rval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002147# if defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002148 tcp->u_lrval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002149# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002150 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002151#elif defined(POWERPC)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002152 if (check_errno && is_negated_errno(ppc_result)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002153 tcp->u_rval = -1;
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002154 u_error = -ppc_result;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002155 }
2156 else {
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002157 tcp->u_rval = ppc_result;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002158 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002159#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002160 if (check_errno && is_negated_errno(m68k_d0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002161 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002162 u_error = -m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002163 }
2164 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002165 tcp->u_rval = m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002166 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002167#elif defined(ARM) || defined(AARCH64)
2168# if defined(AARCH64)
2169 if (tcp->currpers == 1) {
2170 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2171 tcp->u_rval = -1;
2172 u_error = -aarch64_regs.regs[0];
2173 }
2174 else {
2175 tcp->u_rval = aarch64_regs.regs[0];
2176 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002177 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002178 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002179# endif
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002180 {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002181 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002182 tcp->u_rval = -1;
Denys Vlasenko401374e2013-02-06 18:24:39 +01002183 u_error = -arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002184 }
2185 else {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002186 tcp->u_rval = arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002187 }
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002188 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002189#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002190 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002191 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002192 u_error = -avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002193 }
2194 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002195 tcp->u_rval = avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002196 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002197#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002198 if (check_errno && is_negated_errno(bfin_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002199 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002200 u_error = -bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002201 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002202 tcp->u_rval = bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002203 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002204#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002205 if (check_errno && alpha_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002206 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002207 u_error = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002208 }
2209 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002210 tcp->u_rval = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002211 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002212#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002213 if (check_errno && sparc_regs.psr & PSR_C) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002214 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002215 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002216 }
2217 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002218 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002219 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002220#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002221 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002222 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002223 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002224 }
2225 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002226 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002227 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002228#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002229 if (check_errno && is_negated_errno(hppa_r28)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002230 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002231 u_error = -hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002232 }
2233 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002234 tcp->u_rval = hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002235 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002236#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002237 if (check_errno && is_negated_errno(sh_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002238 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002239 u_error = -sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002240 }
2241 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002242 tcp->u_rval = sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002243 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002244#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002245 if (check_errno && is_negated_errno(sh64_r9)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002246 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002247 u_error = -sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002248 }
2249 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002250 tcp->u_rval = sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002251 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002252#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002253 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002254 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002255 u_error = -cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002256 }
2257 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002258 tcp->u_rval = cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002259 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002260#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002261 /*
2262 * The standard tile calling convention returns the value (or negative
2263 * errno) in r0, and zero (or positive errno) in r1.
2264 * Until at least kernel 3.8, however, the r1 value is not reflected
2265 * in ptregs at this point, so we use r0 here.
2266 */
2267 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002268 tcp->u_rval = -1;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002269 u_error = -tile_regs.regs[0];
2270 } else {
2271 tcp->u_rval = tile_regs.regs[0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002272 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002273#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002274 if (check_errno && is_negated_errno(microblaze_r3)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002275 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002276 u_error = -microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002277 }
2278 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002279 tcp->u_rval = microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002280 }
Christian Svensson492f81f2013-02-14 13:26:27 +01002281#elif defined(OR1K)
2282 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2283 tcp->u_rval = -1;
2284 u_error = -or1k_regs.gpr[11];
2285 }
2286 else {
2287 tcp->u_rval = or1k_regs.gpr[11];
2288 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002289#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002290 tcp->u_error = u_error;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002291}
2292
2293static void
2294dumpio(struct tcb *tcp)
2295{
2296 if (syserror(tcp))
2297 return;
2298 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
2299 return;
Denys Vlasenko5721cdb2013-02-16 13:22:38 +01002300 if (!SCNO_IS_VALID(tcp->scno))
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002301 return;
2302 if (sysent[tcp->scno].sys_func == printargs)
2303 return;
2304 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
2305 if (sysent[tcp->scno].sys_func == sys_read ||
2306 sysent[tcp->scno].sys_func == sys_pread ||
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002307 sysent[tcp->scno].sys_func == sys_recv ||
2308 sysent[tcp->scno].sys_func == sys_recvfrom)
2309 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
2310 else if (sysent[tcp->scno].sys_func == sys_readv)
2311 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2312 return;
2313 }
2314 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
2315 if (sysent[tcp->scno].sys_func == sys_write ||
2316 sysent[tcp->scno].sys_func == sys_pwrite ||
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002317 sysent[tcp->scno].sys_func == sys_send ||
2318 sysent[tcp->scno].sys_func == sys_sendto)
2319 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
2320 else if (sysent[tcp->scno].sys_func == sys_writev)
2321 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2322 return;
2323 }
2324}
2325
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002326static int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002327trace_syscall_exiting(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002328{
2329 int sys_res;
2330 struct timeval tv;
Denys Vlasenko1a5b5a72011-08-25 00:29:56 +02002331 int res;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002332 long u_error;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002333
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002334 /* Measure the exit time as early as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002335 if (Tflag || cflag)
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002336 gettimeofday(&tv, NULL);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002337
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00002338#if SUPPORTED_PERSONALITIES > 1
2339 update_personality(tcp, tcp->currpers);
2340#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002341 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002342 if (res == 1) {
2343 syscall_fixup_on_sysexit(tcp); /* never fails */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002344 get_error(tcp); /* never fails */
2345 if (need_fork_exec_workarounds)
2346 syscall_fixup_for_fork_exec(tcp);
2347 if (filtered(tcp))
2348 goto ret;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002349 }
2350
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002351 if (cflag) {
2352 struct timeval t = tv;
Denys Vlasenkoc95a88f2011-08-21 17:47:40 +02002353 count_syscall(tcp, &t);
Denys Vlasenko7b609d52011-06-22 14:32:43 +02002354 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002355 goto ret;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002356 }
2357 }
2358
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002359 /* If not in -ff mode, and printing_tcp != tcp,
2360 * then the log currently does not end with output
2361 * of _our syscall entry_, but with something else.
2362 * We need to say which syscall's return is this.
2363 *
2364 * Forced reprinting via TCB_REPRINT is used only by
2365 * "strace -ff -oLOG test/threaded_execve" corner case.
2366 * It's the only case when -ff mode needs reprinting.
2367 */
2368 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2369 tcp->flags &= ~TCB_REPRINT;
2370 printleader(tcp);
Denys Vlasenko5721cdb2013-02-16 13:22:38 +01002371 if (!SCNO_IS_VALID(tcp->scno))
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002372 tprintf("<... syscall_%lu resumed> ", tcp->scno);
2373 else
2374 tprintf("<... %s resumed> ", sysent[tcp->scno].sys_name);
2375 }
2376 printing_tcp = tcp;
2377
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002378 if (res != 1) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002379 /* There was error in one of prior ptrace ops */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002380 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002381 tabto();
Denys Vlasenko000b6012012-01-28 01:25:03 +01002382 tprints("= ? <unavailable>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002383 line_ended();
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002384 tcp->flags &= ~TCB_INSYSCALL;
2385 return res;
2386 }
2387
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002388 sys_res = 0;
Denys Vlasenko5721cdb2013-02-16 13:22:38 +01002389 if (!SCNO_IS_VALID(tcp->scno)
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002390 || (qual_flags[tcp->scno] & QUAL_RAW)) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002391 /* sys_res = printargs(tcp); - but it's nop on sysexit */
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002392 } else {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002393 /* FIXME: not_failing_only (IOW, option -z) is broken:
2394 * failure of syscall is known only after syscall return.
2395 * Thus we end up with something like this on, say, ENOENT:
2396 * open("doesnt_exist", O_RDONLY <unfinished ...>
2397 * {next syscall decode}
2398 * whereas the intended result is that open(...) line
2399 * is not shown at all.
2400 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002401 if (not_failing_only && tcp->u_error)
Denys Vlasenko3b738812011-08-22 02:06:35 +02002402 goto ret; /* ignore failed syscalls */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002403 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2404 }
2405
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002406 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002407 tabto();
Denys Vlasenko3b738812011-08-22 02:06:35 +02002408 u_error = tcp->u_error;
Denys Vlasenko5721cdb2013-02-16 13:22:38 +01002409 if (!SCNO_IS_VALID(tcp->scno) ||
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002410 qual_flags[tcp->scno] & QUAL_RAW) {
2411 if (u_error)
2412 tprintf("= -1 (errno %ld)", u_error);
2413 else
2414 tprintf("= %#lx", tcp->u_rval);
2415 }
2416 else if (!(sys_res & RVAL_NONE) && u_error) {
2417 switch (u_error) {
Denys Vlasenkofe585652012-01-12 11:26:34 +01002418 /* Blocked signals do not interrupt any syscalls.
2419 * In this case syscalls don't return ERESTARTfoo codes.
2420 *
2421 * Deadly signals set to SIG_DFL interrupt syscalls
2422 * and kill the process regardless of which of the codes below
2423 * is returned by the interrupted syscall.
2424 * In some cases, kernel forces a kernel-generated deadly
2425 * signal to be unblocked and set to SIG_DFL (and thus cause
2426 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2427 * or SIGILL. (The alternative is to leave process spinning
2428 * forever on the faulty instruction - not useful).
2429 *
2430 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2431 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2432 * but kernel will always restart them.
2433 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002434 case ERESTARTSYS:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002435 /* Most common type of signal-interrupted syscall exit code.
2436 * The system call will be restarted with the same arguments
2437 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2438 */
2439 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002440 break;
2441 case ERESTARTNOINTR:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002442 /* Rare. For example, fork() returns this if interrupted.
2443 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2444 */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002445 tprints("= ? ERESTARTNOINTR (To be restarted)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002446 break;
2447 case ERESTARTNOHAND:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002448 /* pause(), rt_sigsuspend() etc use this code.
2449 * SA_RESTART is ignored (assumed not set):
2450 * syscall won't restart (will return EINTR instead)
Denys Vlasenko30c03232013-02-19 16:59:26 +01002451 * even after signal with SA_RESTART set. However,
2452 * after SIG_IGN or SIG_DFL signal it will restart
2453 * (thus the name "restart only if has no handler").
Denys Vlasenkofe585652012-01-12 11:26:34 +01002454 */
2455 tprints("= ? ERESTARTNOHAND (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002456 break;
2457 case ERESTART_RESTARTBLOCK:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002458 /* Syscalls like nanosleep(), poll() which can't be
2459 * restarted with their original arguments use this
2460 * code. Kernel will execute restart_syscall() instead,
2461 * which changes arguments before restarting syscall.
2462 * SA_RESTART is ignored (assumed not set) similarly
2463 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2464 * since restart data is saved in "restart block"
2465 * in task struct, and if signal handler uses a syscall
2466 * which in turn saves another such restart block,
2467 * old data is lost and restart becomes impossible)
2468 */
2469 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002470 break;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002471 default:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002472 if (u_error < 0)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002473 tprintf("= -1 E??? (errno %ld)", u_error);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002474 else if (u_error < nerrnos)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002475 tprintf("= -1 %s (%s)", errnoent[u_error],
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002476 strerror(u_error));
2477 else
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002478 tprintf("= -1 ERRNO_%ld (%s)", u_error,
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002479 strerror(u_error));
2480 break;
2481 }
2482 if ((sys_res & RVAL_STR) && tcp->auxstr)
2483 tprintf(" (%s)", tcp->auxstr);
2484 }
2485 else {
2486 if (sys_res & RVAL_NONE)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002487 tprints("= ?");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002488 else {
2489 switch (sys_res & RVAL_MASK) {
2490 case RVAL_HEX:
2491 tprintf("= %#lx", tcp->u_rval);
2492 break;
2493 case RVAL_OCTAL:
2494 tprintf("= %#lo", tcp->u_rval);
2495 break;
2496 case RVAL_UDECIMAL:
2497 tprintf("= %lu", tcp->u_rval);
2498 break;
2499 case RVAL_DECIMAL:
2500 tprintf("= %ld", tcp->u_rval);
2501 break;
H.J. Ludd0130b2012-04-16 12:16:45 +02002502#if defined(LINUX_MIPSN32) || defined(X32)
2503 /*
2504 case RVAL_LHEX:
2505 tprintf("= %#llx", tcp->u_lrval);
2506 break;
2507 case RVAL_LOCTAL:
2508 tprintf("= %#llo", tcp->u_lrval);
2509 break;
2510 */
2511 case RVAL_LUDECIMAL:
2512 tprintf("= %llu", tcp->u_lrval);
2513 break;
2514 /*
2515 case RVAL_LDECIMAL:
2516 tprintf("= %lld", tcp->u_lrval);
2517 break;
2518 */
2519#endif
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002520 default:
2521 fprintf(stderr,
2522 "invalid rval format\n");
2523 break;
2524 }
2525 }
2526 if ((sys_res & RVAL_STR) && tcp->auxstr)
2527 tprintf(" (%s)", tcp->auxstr);
2528 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002529 if (Tflag) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002530 tv_sub(&tv, &tv, &tcp->etime);
2531 tprintf(" <%ld.%06ld>",
2532 (long) tv.tv_sec, (long) tv.tv_usec);
2533 }
Denys Vlasenko000b6012012-01-28 01:25:03 +01002534 tprints("\n");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002535 dumpio(tcp);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002536 line_ended();
2537
Denys Vlasenko3b738812011-08-22 02:06:35 +02002538 ret:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002539 tcp->flags &= ~TCB_INSYSCALL;
2540 return 0;
2541}
2542
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002543int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002544trace_syscall(struct tcb *tcp)
2545{
2546 return exiting(tcp) ?
2547 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2548}