blob: af9c2a0b29508b69445f054e50b6d30001e95d22 [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00005 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Linux for s390 port by D.J. Barrow
8 * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00009 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000032 */
33
34#include "defs.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000035#include <sys/user.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000036#include <sys/param.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000037
Wichert Akkerman15dea971999-10-06 13:06:34 +000038#ifdef HAVE_SYS_REG_H
Denys Vlasenko523635f2012-02-25 02:44:25 +010039# include <sys/reg.h>
40# ifndef PTRACE_PEEKUSR
41# define PTRACE_PEEKUSR PTRACE_PEEKUSER
42# endif
Wichert Akkermanfaf72222000-02-19 23:59:03 +000043#elif defined(HAVE_LINUX_PTRACE_H)
Denys Vlasenko523635f2012-02-25 02:44:25 +010044# undef PTRACE_SYSCALL
Roland McGrathce9f0742004-03-01 21:29:22 +000045# ifdef HAVE_STRUCT_IA64_FPREG
46# define ia64_fpreg XXX_ia64_fpreg
47# endif
48# ifdef HAVE_STRUCT_PT_ALL_USER_REGS
49# define pt_all_user_regs XXX_pt_all_user_regs
50# endif
Denys Vlasenko523635f2012-02-25 02:44:25 +010051# include <linux/ptrace.h>
Roland McGrathce9f0742004-03-01 21:29:22 +000052# undef ia64_fpreg
53# undef pt_all_user_regs
Wichert Akkerman15dea971999-10-06 13:06:34 +000054#endif
55
Denys Vlasenko84703742012-02-25 02:38:52 +010056#if defined(SPARC64)
Roland McGrath6d1a65c2004-07-12 07:44:08 +000057# undef PTRACE_GETREGS
58# define PTRACE_GETREGS PTRACE_GETREGS64
59# undef PTRACE_SETREGS
60# define PTRACE_SETREGS PTRACE_SETREGS64
Denys Vlasenko84703742012-02-25 02:38:52 +010061#endif
Roland McGrath6d1a65c2004-07-12 07:44:08 +000062
Denys Vlasenko84703742012-02-25 02:38:52 +010063#if defined(IA64)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000064# include <asm/ptrace_offsets.h>
65# include <asm/rse.h>
66#endif
67
Dmitry V. Levin32c049a2013-03-18 00:59:27 +000068/* for struct iovec */
69#include <sys/uio.h>
70/* for NT_PRSTATUS */
71#ifdef HAVE_ELF_H
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010072# include <elf.h>
73#endif
74
Steve McIntyred8d3bd32012-10-24 17:58:16 +010075#if defined(AARCH64)
76# include <asm/ptrace.h>
James Hogan5f999a82013-02-22 14:44:10 +000077#endif
78
Chris Zankel8f636ed2013-03-25 10:22:07 -070079#if defined(XTENSA)
80# include <asm/ptrace.h>
81#endif
82
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000083#ifndef NSIG
Denys Vlasenko523635f2012-02-25 02:44:25 +010084# warning: NSIG is not defined, using 32
85# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000086#endif
87#ifdef ARM
Denys Vlasenko041b3ee2011-08-18 12:48:56 +020088/* Ugh. Is this really correct? ARM has no RT signals?! */
Denys Vlasenko523635f2012-02-25 02:44:25 +010089# undef NSIG
90# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000091#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000092
93#include "syscall.h"
94
95/* Define these shorthand notations to simplify the syscallent files. */
Roland McGrath2fe7b132005-07-05 03:25:35 +000096#define TD TRACE_DESC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000097#define TF TRACE_FILE
98#define TI TRACE_IPC
99#define TN TRACE_NETWORK
100#define TP TRACE_PROCESS
101#define TS TRACE_SIGNAL
Namhyung Kim96792962012-10-24 11:41:57 +0900102#define TM TRACE_MEMORY
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000103#define NF SYSCALL_NEVER_FAILS
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200104#define MA MAX_ARGS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000105
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100106const struct_sysent sysent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000107#include "syscallent.h"
108};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000109
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100110#if SUPPORTED_PERSONALITIES > 1
111static const struct_sysent sysent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100112# include "syscallent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000113};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200114#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000115
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100116#if SUPPORTED_PERSONALITIES > 2
117static const struct_sysent sysent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100118# include "syscallent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000119};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200120#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000121
122/* Now undef them since short defines cause wicked namespace pollution. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000123#undef TD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000124#undef TF
125#undef TI
126#undef TN
127#undef TP
128#undef TS
Namhyung Kim96792962012-10-24 11:41:57 +0900129#undef TM
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000130#undef NF
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200131#undef MA
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000132
Denys Vlasenko39fca622011-08-20 02:12:33 +0200133/*
134 * `ioctlent.h' may be generated from `ioctlent.raw' by the auxiliary
135 * program `ioctlsort', such that the list is sorted by the `code' field.
136 * This has the side-effect of resolving the _IO.. macros into
137 * plain integers, eliminating the need to include here everything
138 * in "/usr/include".
139 */
140
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100141const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000142#include "errnoent.h"
143};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100144const char *const signalent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200145#include "signalent.h"
146};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100147const struct_ioctlent ioctlent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200148#include "ioctlent.h"
149};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000150
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100151#if SUPPORTED_PERSONALITIES > 1
Roland McGrathee36ce12004-09-04 03:53:10 +0000152static const char *const errnoent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100153# include "errnoent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000154};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200155static const char *const signalent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100156# include "signalent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200157};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100158static const struct_ioctlent ioctlent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100159# include "ioctlent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200160};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200161#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000162
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100163#if SUPPORTED_PERSONALITIES > 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000164static const char *const errnoent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100165# include "errnoent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000166};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200167static const char *const signalent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100168# include "signalent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200169};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100170static const struct_ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100171# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200172};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200173#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000174
Dmitry V. Levine6f55242013-02-26 22:02:30 +0000175enum {
176 nsyscalls0 = ARRAY_SIZE(sysent0)
177#if SUPPORTED_PERSONALITIES > 1
178 , nsyscalls1 = ARRAY_SIZE(sysent1)
179# if SUPPORTED_PERSONALITIES > 2
180 , nsyscalls2 = ARRAY_SIZE(sysent2)
181# endif
182#endif
183};
184
185enum {
186 nerrnos0 = ARRAY_SIZE(errnoent0)
187#if SUPPORTED_PERSONALITIES > 1
188 , nerrnos1 = ARRAY_SIZE(errnoent1)
189# if SUPPORTED_PERSONALITIES > 2
190 , nerrnos2 = ARRAY_SIZE(errnoent2)
191# endif
192#endif
193};
194
195enum {
196 nsignals0 = ARRAY_SIZE(signalent0)
197#if SUPPORTED_PERSONALITIES > 1
198 , nsignals1 = ARRAY_SIZE(signalent1)
199# if SUPPORTED_PERSONALITIES > 2
200 , nsignals2 = ARRAY_SIZE(signalent2)
201# endif
202#endif
203};
204
205enum {
206 nioctlents0 = ARRAY_SIZE(ioctlent0)
207#if SUPPORTED_PERSONALITIES > 1
208 , nioctlents1 = ARRAY_SIZE(ioctlent1)
209# if SUPPORTED_PERSONALITIES > 2
210 , nioctlents2 = ARRAY_SIZE(ioctlent2)
211# endif
212#endif
213};
214
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100215#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100216const struct_sysent *sysent = sysent0;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100217const char *const *errnoent = errnoent0;
218const char *const *signalent = signalent0;
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100219const struct_ioctlent *ioctlent = ioctlent0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100220#endif
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100221unsigned nsyscalls = nsyscalls0;
222unsigned nerrnos = nerrnos0;
223unsigned nsignals = nsignals0;
224unsigned nioctlents = nioctlents0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100225
226unsigned num_quals;
227qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
228
229static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
230 nsyscalls0,
231#if SUPPORTED_PERSONALITIES > 1
232 nsyscalls1,
233#endif
234#if SUPPORTED_PERSONALITIES > 2
235 nsyscalls2,
236#endif
237};
238static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
239 sysent0,
240#if SUPPORTED_PERSONALITIES > 1
241 sysent1,
242#endif
243#if SUPPORTED_PERSONALITIES > 2
244 sysent2,
245#endif
246};
247
248enum {
249 MAX_NSYSCALLS1 = (nsyscalls0
250#if SUPPORTED_PERSONALITIES > 1
251 > nsyscalls1 ? nsyscalls0 : nsyscalls1
252#endif
253 ),
254 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
255#if SUPPORTED_PERSONALITIES > 2
256 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
257#endif
258 ),
259 MAX_NSYSCALLS = MAX_NSYSCALLS2,
260 /* We are ready for arches with up to 255 signals,
261 * even though the largest known signo is on MIPS and it is 128.
262 * The number of existing syscalls on all arches is
263 * larger that 255 anyway, so it is just a pedantic matter.
264 */
265 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
266};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000267
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100268#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100269unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000270
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100271# ifndef current_wordsize
272unsigned current_wordsize;
273static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000274 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000275 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100276# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000277 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100278# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200279};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100280# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000281
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200282void
Dmitry V. Levin3abe8b22006-12-20 22:37:21 +0000283set_personality(int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000284{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100285 nsyscalls = nsyscall_vec[personality];
286 sysent = sysent_vec[personality];
287
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000288 switch (personality) {
289 case 0:
290 errnoent = errnoent0;
291 nerrnos = nerrnos0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000292 ioctlent = ioctlent0;
293 nioctlents = nioctlents0;
294 signalent = signalent0;
295 nsignals = nsignals0;
296 break;
297
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000298 case 1:
299 errnoent = errnoent1;
300 nerrnos = nerrnos1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000301 ioctlent = ioctlent1;
302 nioctlents = nioctlents1;
303 signalent = signalent1;
304 nsignals = nsignals1;
305 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000306
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100307# if SUPPORTED_PERSONALITIES > 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000308 case 2:
309 errnoent = errnoent2;
310 nerrnos = nerrnos2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000311 ioctlent = ioctlent2;
312 nioctlents = nioctlents2;
313 signalent = signalent2;
314 nsignals = nsignals2;
315 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100316# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000317 }
318
319 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100320# ifndef current_wordsize
321 current_wordsize = personality_wordsize[personality];
322# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000323}
324
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000325static void
326update_personality(struct tcb *tcp, int personality)
327{
328 if (personality == current_personality)
329 return;
330 set_personality(personality);
331
332 if (personality == tcp->currpers)
333 return;
334 tcp->currpers = personality;
335
H.J. Lu35be5812012-04-16 13:00:01 +0200336# if defined(POWERPC64)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000337 if (!qflag) {
338 static const char *const names[] = {"64 bit", "32 bit"};
339 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
340 tcp->pid, names[personality]);
341 }
H.J. Lu35be5812012-04-16 13:00:01 +0200342# elif defined(X86_64)
343 if (!qflag) {
344 static const char *const names[] = {"64 bit", "32 bit", "x32"};
345 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
346 tcp->pid, names[personality]);
347 }
H.J. Lu085e4282012-04-17 11:05:04 -0700348# elif defined(X32)
349 if (!qflag) {
350 static const char *const names[] = {"x32", "32 bit"};
351 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
352 tcp->pid, names[personality]);
353 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000354# elif defined(AARCH64)
355 if (!qflag) {
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100356 static const char *const names[] = {"32-bit", "AArch64"};
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000357 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
358 tcp->pid, names[personality]);
359 }
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100360# elif defined(TILE)
361 if (!qflag) {
362 static const char *const names[] = {"64-bit", "32-bit"};
363 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
364 tcp->pid, names[personality]);
365 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100366# endif
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000367}
368#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000369
Denys Vlasenkoc1540fe2013-02-21 16:17:08 +0100370static int qual_syscall(), qual_signal(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000371
Roland McGrathe10e62a2004-09-04 04:20:43 +0000372static const struct qual_options {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000373 int bitflag;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000374 const char *option_name;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000375 int (*qualify)(const char *, int, int);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000376 const char *argument_name;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000377} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000378 { QUAL_TRACE, "trace", qual_syscall, "system call" },
379 { QUAL_TRACE, "t", qual_syscall, "system call" },
380 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
381 { QUAL_ABBREV, "a", qual_syscall, "system call" },
382 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
383 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
384 { QUAL_RAW, "raw", qual_syscall, "system call" },
385 { QUAL_RAW, "x", qual_syscall, "system call" },
386 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
387 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
388 { QUAL_SIGNAL, "s", qual_signal, "signal" },
Roland McGrath9797ceb2002-12-30 10:23:00 +0000389 { QUAL_READ, "read", qual_desc, "descriptor" },
390 { QUAL_READ, "reads", qual_desc, "descriptor" },
391 { QUAL_READ, "r", qual_desc, "descriptor" },
392 { QUAL_WRITE, "write", qual_desc, "descriptor" },
393 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
394 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000395 { 0, NULL, NULL, NULL },
396};
397
Roland McGrath9797ceb2002-12-30 10:23:00 +0000398static void
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100399reallocate_qual(int n)
400{
401 unsigned p;
402 qualbits_t *qp;
403 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
404 qp = qual_vec[p] = realloc(qual_vec[p], n * sizeof(qualbits_t));
405 if (!qp)
406 die_out_of_memory();
407 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
408 }
409 num_quals = n;
410}
411
412static void
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000413qualify_one(int n, int bitflag, int not, int pers)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000414{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100415 unsigned p;
Roland McGrath138c6a32006-01-12 09:50:49 +0000416
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100417 if (num_quals <= n)
418 reallocate_qual(n + 1);
Roland McGrath138c6a32006-01-12 09:50:49 +0000419
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100420 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
421 if (pers == p || pers < 0) {
422 if (not)
423 qual_vec[p][n] &= ~bitflag;
424 else
425 qual_vec[p][n] |= bitflag;
426 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000427 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000428}
429
430static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000431qual_syscall(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000432{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100433 unsigned p;
434 unsigned i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000435 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000436
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100437 if (*s >= '0' && *s <= '9') {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100438 i = string_to_uint(s);
Denys Vlasenkob43dacd2013-02-23 18:19:28 +0100439 if (i >= MAX_NSYSCALLS)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000440 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000441 qualify_one(i, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000442 return 0;
Roland McGrath48a035f2006-01-12 09:45:56 +0000443 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000444
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100445 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
446 for (i = 0; i < nsyscall_vec[p]; i++) {
447 if (sysent_vec[p][i].sys_name
448 && strcmp(s, sysent_vec[p][i].sys_name) == 0
449 ) {
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000450 qualify_one(i, bitflag, not, p);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100451 rc = 0;
452 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000453 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100454 }
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000455
Roland McGrathfe6b3522005-02-02 04:40:11 +0000456 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000457}
458
459static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000460qual_signal(const char *s, int bitflag, int not)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000461{
462 int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000463
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100464 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000465 int signo = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100466 if (signo < 0 || signo > 255)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000467 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000468 qualify_one(signo, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000469 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000470 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000471 if (strncasecmp(s, "SIG", 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000472 s += 3;
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100473 for (i = 0; i <= NSIG; i++) {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000474 if (strcasecmp(s, signame(i) + 3) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000475 qualify_one(i, bitflag, not, -1);
Roland McGrath76421df2005-02-02 03:51:18 +0000476 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000477 }
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100478 }
Roland McGrath76421df2005-02-02 03:51:18 +0000479 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000480}
481
482static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000483qual_desc(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000484{
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100485 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000486 int desc = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100487 if (desc < 0 || desc > 0x7fff) /* paranoia */
Roland McGrathfe6b3522005-02-02 04:40:11 +0000488 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000489 qualify_one(desc, bitflag, not, -1);
Roland McGrath2b619022003-04-10 18:58:20 +0000490 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000491 }
492 return -1;
493}
494
495static int
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000496lookup_class(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000497{
498 if (strcmp(s, "file") == 0)
499 return TRACE_FILE;
500 if (strcmp(s, "ipc") == 0)
501 return TRACE_IPC;
502 if (strcmp(s, "network") == 0)
503 return TRACE_NETWORK;
504 if (strcmp(s, "process") == 0)
505 return TRACE_PROCESS;
506 if (strcmp(s, "signal") == 0)
507 return TRACE_SIGNAL;
Roland McGrath2fe7b132005-07-05 03:25:35 +0000508 if (strcmp(s, "desc") == 0)
509 return TRACE_DESC;
Namhyung Kim96792962012-10-24 11:41:57 +0900510 if (strcmp(s, "memory") == 0)
511 return TRACE_MEMORY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000512 return -1;
513}
514
515void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000516qualify(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000517{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000518 const struct qual_options *opt;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000519 int not;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000520 char *copy;
521 const char *p;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000522 int i, n;
523
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100524 if (num_quals == 0)
525 reallocate_qual(MIN_QUALS);
526
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000527 opt = &qual_options[0];
528 for (i = 0; (p = qual_options[i].option_name); i++) {
529 n = strlen(p);
530 if (strncmp(s, p, n) == 0 && s[n] == '=') {
531 opt = &qual_options[i];
532 s += n + 1;
533 break;
534 }
535 }
536 not = 0;
537 if (*s == '!') {
538 not = 1;
539 s++;
540 }
541 if (strcmp(s, "none") == 0) {
542 not = 1 - not;
543 s = "all";
544 }
545 if (strcmp(s, "all") == 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100546 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000547 qualify_one(i, opt->bitflag, not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000548 }
549 return;
550 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100551 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000552 qualify_one(i, opt->bitflag, !not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000553 }
Denys Vlasenko5d645812011-08-20 12:48:18 +0200554 copy = strdup(s);
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200555 if (!copy)
556 die_out_of_memory();
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000557 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000558 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100559 unsigned pers;
560 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
561 for (i = 0; i < nsyscall_vec[pers]; i++)
562 if (sysent_vec[pers][i].sys_flags & n)
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000563 qualify_one(i, opt->bitflag, not, pers);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100564 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000565 continue;
566 }
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000567 if (opt->qualify(p, opt->bitflag, not)) {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100568 error_msg_and_die("invalid %s '%s'",
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000569 opt->argument_name, p);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000570 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000571 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000572 free(copy);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000573 return;
574}
575
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000576#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000577static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000578decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000579{
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000580 unsigned long addr;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100581 unsigned int i, n, size;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000582
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000583 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
584 return;
585
586 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100587 tcp->qual_flg = qual_flags[tcp->scno];
588 tcp->s_ent = &sysent[tcp->scno];
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000589 addr = tcp->u_arg[1];
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100590 size = current_wordsize;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100591 n = tcp->s_ent->nargs;
592 for (i = 0; i < n; ++i) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000593 if (size == sizeof(int)) {
594 unsigned int arg;
595 if (umove(tcp, addr, &arg) < 0)
596 arg = 0;
597 tcp->u_arg[i] = arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000598 }
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000599 else {
600 unsigned long arg;
601 if (umove(tcp, addr, &arg) < 0)
602 arg = 0;
603 tcp->u_arg[i] = arg;
604 }
605 addr += size;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000606 }
607}
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000608#endif
Mike Frysinger3362e892012-03-15 01:09:19 -0400609
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000610#ifdef SYS_ipc_subcall
611static void
612decode_ipc_subcall(struct tcb *tcp)
613{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100614 unsigned int i, n;
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000615
616 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
617 return;
618
619 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100620 tcp->qual_flg = qual_flags[tcp->scno];
621 tcp->s_ent = &sysent[tcp->scno];
622 n = tcp->s_ent->nargs;
623 for (i = 0; i < n; i++)
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000624 tcp->u_arg[i] = tcp->u_arg[i + 1];
625}
626#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000627
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200628int
629printargs(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000630{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200631 if (entering(tcp)) {
632 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100633 int n = tcp->s_ent->nargs;
634 for (i = 0; i < n; i++)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200635 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
636 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000637 return 0;
638}
639
Denys Vlasenko72879c62012-02-27 14:18:02 +0100640int
641printargs_lu(struct tcb *tcp)
642{
643 if (entering(tcp)) {
644 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100645 int n = tcp->s_ent->nargs;
646 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100647 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
648 }
649 return 0;
650}
651
652int
653printargs_ld(struct tcb *tcp)
654{
655 if (entering(tcp)) {
656 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100657 int n = tcp->s_ent->nargs;
658 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100659 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
660 }
661 return 0;
662}
663
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100664#if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200665long
666getrval2(struct tcb *tcp)
667{
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100668 long val;
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200669
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100670# if defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100671 val = sparc_regs.u_regs[U_REG_O1];
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100672# elif defined(SH)
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200673 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200674 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100675# elif defined(IA64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200676 if (upeek(tcp->pid, PT_R9, &val) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200677 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100678# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200679
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200680 return val;
681}
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100682#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200683
Denys Vlasenko523635f2012-02-25 02:44:25 +0100684#if defined(I386)
Denys Vlasenko2550d482013-02-15 21:04:28 +0100685struct user_regs_struct i386_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000686# define ARCH_REGS_FOR_GETREGSET i386_regs
H.J. Lu35be5812012-04-16 13:00:01 +0200687#elif defined(X86_64) || defined(X32)
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100688/*
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100689 * On i386, pt_regs and user_regs_struct are the same,
690 * but on 64 bit x86, user_regs_struct has six more fields:
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100691 * fs_base, gs_base, ds, es, fs, gs.
692 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
693 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100694struct i386_user_regs_struct {
695 uint32_t ebx;
696 uint32_t ecx;
697 uint32_t edx;
698 uint32_t esi;
699 uint32_t edi;
700 uint32_t ebp;
701 uint32_t eax;
702 uint32_t xds;
703 uint32_t xes;
704 uint32_t xfs;
705 uint32_t xgs;
706 uint32_t orig_eax;
707 uint32_t eip;
708 uint32_t xcs;
709 uint32_t eflags;
710 uint32_t esp;
711 uint32_t xss;
712};
713static union {
714 struct user_regs_struct x86_64_r;
715 struct i386_user_regs_struct i386_r;
716} x86_regs_union;
717# define x86_64_regs x86_regs_union.x86_64_r
718# define i386_regs x86_regs_union.i386_r
719static struct iovec x86_io = {
720 .iov_base = &x86_regs_union
721};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100722#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +0200723bool ia64_ia32mode = 0; /* not static */
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100724static long ia64_r8, ia64_r10;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100725#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +0200726struct pt_regs ppc_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100727#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100728static long m68k_d0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100729#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100730static long bfin_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100731#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100732struct pt_regs arm_regs; /* not static */
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000733# define ARCH_REGS_FOR_GETREGSET arm_regs
Steve McIntyred8d3bd32012-10-24 17:58:16 +0100734#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100735static union {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100736 struct user_pt_regs aarch64_r;
737 struct arm_pt_regs arm_r;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100738} arm_regs_union;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100739# define aarch64_regs arm_regs_union.aarch64_r
740# define arm_regs arm_regs_union.arm_r
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100741static struct iovec aarch64_io = {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100742 .iov_base = &arm_regs_union
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100743};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100744#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100745static long alpha_r0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100746static long alpha_a3;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100747#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100748static struct pt_regs avr32_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100749#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100750struct pt_regs sparc_regs; /* not static */
Denys Vlasenko523635f2012-02-25 02:44:25 +0100751#elif defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100752static long long mips_a3;
753static long long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100754#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100755static long mips_a3;
756static long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100757#elif defined(S390) || defined(S390X)
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200758static long gpr2;
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200759static long syscall_mode;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100760#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100761static long hppa_r28;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100762#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100763static long sh_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100764#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100765static long sh64_r9;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100766#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100767static long cris_r10;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100768#elif defined(TILE)
769struct pt_regs tile_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100770#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100771static long microblaze_r3;
Christian Svensson492f81f2013-02-14 13:26:27 +0100772#elif defined(OR1K)
773static struct user_regs_struct or1k_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000774# define ARCH_REGS_FOR_GETREGSET or1k_regs
James Hogan5f999a82013-02-22 14:44:10 +0000775#elif defined(METAG)
776static struct user_gp_regs metag_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000777# define ARCH_REGS_FOR_GETREGSET metag_regs
Chris Zankel8f636ed2013-03-25 10:22:07 -0700778#elif defined(XTENSA)
779static long xtensa_a2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100780#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000781
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100782void
Denys Vlasenko5a2483b2013-07-01 12:49:14 +0200783print_pc(struct tcb *tcp)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100784{
785#define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
786 sizeof(long) == 8 ? "[????????????????] " : \
787 NULL /* crash */)
788 if (get_regs_error) {
789 PRINTBADPC;
790 return;
791 }
792#if defined(I386)
793 tprintf("[%08lx] ", i386_regs.eip);
794#elif defined(S390) || defined(S390X)
795 long psw;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200796 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100797 PRINTBADPC;
798 return;
799 }
800# ifdef S390
801 tprintf("[%08lx] ", psw);
802# elif S390X
Dmitry V. Levinddba73e2013-02-05 19:01:58 +0000803 tprintf("[%016lx] ", psw);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100804# endif
805#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100806 if (x86_io.iov_len == sizeof(i386_regs)) {
807 tprintf("[%08x] ", (unsigned) i386_regs.eip);
808 } else {
809# if defined(X86_64)
810 tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
811# elif defined(X32)
812 /* Note: this truncates 64-bit rip to 32 bits */
813 tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip);
814# endif
815 }
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100816#elif defined(IA64)
817 long ip;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200818 if (upeek(tcp->pid, PT_B0, &ip) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100819 PRINTBADPC;
820 return;
821 }
822 tprintf("[%08lx] ", ip);
823#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +0200824 long pc = ppc_regs.nip;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100825# ifdef POWERPC64
826 tprintf("[%016lx] ", pc);
827# else
828 tprintf("[%08lx] ", pc);
829# endif
830#elif defined(M68K)
831 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200832 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100833 tprints("[????????] ");
834 return;
835 }
836 tprintf("[%08lx] ", pc);
837#elif defined(ALPHA)
838 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200839 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100840 tprints("[????????????????] ");
841 return;
842 }
843 tprintf("[%08lx] ", pc);
844#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100845 tprintf("[%08lx] ", sparc_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100846#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100847 tprintf("[%08lx] ", sparc_regs.tpc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100848#elif defined(HPPA)
849 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200850 if (upeek(tcp->pid, PT_IAOQ0, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100851 tprints("[????????] ");
852 return;
853 }
854 tprintf("[%08lx] ", pc);
855#elif defined(MIPS)
856 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200857 if (upeek(tcp->pid, REG_EPC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100858 tprints("[????????] ");
859 return;
860 }
861 tprintf("[%08lx] ", pc);
862#elif defined(SH)
863 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200864 if (upeek(tcp->pid, 4*REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100865 tprints("[????????] ");
866 return;
867 }
868 tprintf("[%08lx] ", pc);
869#elif defined(SH64)
870 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200871 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100872 tprints("[????????????????] ");
873 return;
874 }
875 tprintf("[%08lx] ", pc);
876#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100877 tprintf("[%08lx] ", arm_regs.ARM_pc);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100878#elif defined(AARCH64)
879 /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100880#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100881 tprintf("[%08lx] ", avr32_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100882#elif defined(BFIN)
883 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200884 if (upeek(tcp->pid, PT_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100885 PRINTBADPC;
886 return;
887 }
888 tprintf("[%08lx] ", pc);
889#elif defined(CRISV10)
890 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200891 if (upeek(tcp->pid, 4*PT_IRP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100892 PRINTBADPC;
893 return;
894 }
895 tprintf("[%08lx] ", pc);
896#elif defined(CRISV32)
897 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200898 if (upeek(tcp->pid, 4*PT_ERP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100899 PRINTBADPC;
900 return;
901 }
902 tprintf("[%08lx] ", pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100903#elif defined(TILE)
904# ifdef _LP64
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500905 tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100906# else
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500907 tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100908# endif
Christian Svensson492f81f2013-02-14 13:26:27 +0100909#elif defined(OR1K)
910 tprintf("[%08lx] ", or1k_regs.pc);
James Hogan5f999a82013-02-22 14:44:10 +0000911#elif defined(METAG)
912 tprintf("[%08lx] ", metag_regs.pc);
Chris Zankel8f636ed2013-03-25 10:22:07 -0700913#elif defined(XTENSA)
914 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200915 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Chris Zankel8f636ed2013-03-25 10:22:07 -0700916 PRINTBADPC;
917 return;
918 }
919 tprintf("[%08lx] ", pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100920#endif /* architecture */
921}
922
Denys Vlasenko7270de52013-02-21 15:46:34 +0100923/* Shuffle syscall numbers so that we don't have huge gaps in syscall table.
924 * The shuffling should be reversible: shuffle_scno(shuffle_scno(n)) == n.
925 */
926#if defined(ARM) /* So far only ARM needs this */
927static long
928shuffle_scno(unsigned long scno)
929{
930 if (scno <= ARM_LAST_ORDINARY_SYSCALL)
931 return scno;
932
933 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
934 if (scno == 0x000ffff0)
935 return ARM_LAST_ORDINARY_SYSCALL+1;
936 if (scno == ARM_LAST_ORDINARY_SYSCALL+1)
937 return 0x000ffff0;
938
939 /* Is it ARM specific syscall?
940 * Swap with [LAST_ORDINARY+2, LAST_ORDINARY+2 + LAST_SPECIAL] range.
941 */
942 if (scno >= 0x000f0000
943 && scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL
944 ) {
945 return scno - 0x000f0000 + (ARM_LAST_ORDINARY_SYSCALL+2);
946 }
947 if (/* scno >= ARM_LAST_ORDINARY_SYSCALL+2 - always true */ 1
948 && scno <= (ARM_LAST_ORDINARY_SYSCALL+2) + ARM_LAST_SPECIAL_SYSCALL
949 ) {
950 return scno + 0x000f0000 - (ARM_LAST_ORDINARY_SYSCALL+2);
951 }
952
953 return scno;
954}
955#else
956# define shuffle_scno(scno) ((long)(scno))
957#endif
958
959static char*
960undefined_scno_name(struct tcb *tcp)
961{
962 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
963
964 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
965 return buf;
966}
967
Anton Blanchard14d51a62013-06-26 14:42:37 +1000968#ifdef POWERPC
969/*
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200970 * PTRACE_GETREGS was added to the PowerPC kernel in v2.6.23,
971 * we provide a slow fallback for old kernels.
Anton Blanchard14d51a62013-06-26 14:42:37 +1000972 */
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200973static int powerpc_getregs_old(pid_t pid)
Anton Blanchard14d51a62013-06-26 14:42:37 +1000974{
975 int i;
976 long r;
977
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200978 if (iflag) {
979 r = upeek(pid, sizeof(long) * PT_NIP, &ppc_regs.nip);
980 if (r)
981 goto out;
982 }
983#ifdef POWERPC64 /* else we never use it */
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200984 r = upeek(pid, sizeof(long) * PT_MSR, &ppc_regs.msr);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000985 if (r)
986 goto out;
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200987#endif
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200988 r = upeek(pid, sizeof(long) * PT_CCR, &ppc_regs.ccr);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000989 if (r)
990 goto out;
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200991 r = upeek(pid, sizeof(long) * PT_ORIG_R3, &ppc_regs.orig_gpr3);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000992 if (r)
993 goto out;
Anton Blanchard14d51a62013-06-26 14:42:37 +1000994 for (i = 0; i <= 8; i++) {
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200995 r = upeek(pid, sizeof(long) * (PT_R0 + i), &ppc_regs.gpr[i]);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000996 if (r)
997 goto out;
998 }
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200999 out:
Anton Blanchard14d51a62013-06-26 14:42:37 +10001000 return r;
1001}
1002#endif
1003
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001004#ifndef get_regs
1005long get_regs_error;
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001006
1007#if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001008static void get_regset(pid_t pid)
1009{
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001010/* constant iovec */
1011# if defined(ARM) \
1012 || defined(I386) \
1013 || defined(METAG) \
1014 || defined(OR1K)
1015 static struct iovec io = {
1016 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1017 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001018 };
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001019 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1020
1021/* variable iovec */
1022# elif defined(X86_64) || defined(X32)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001023 /* x86_io.iov_base = &x86_regs_union; - already is */
1024 x86_io.iov_len = sizeof(x86_regs_union);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001025 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &x86_io);
1026# elif defined(AARCH64)
1027 /* aarch64_io.iov_base = &arm_regs_union; - already is */
1028 aarch64_io.iov_len = sizeof(arm_regs_union);
1029 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &aarch64_io);
1030# else
1031# warning both PTRACE_GETREGSET and NT_PRSTATUS are available but not yet used
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001032# endif
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001033}
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001034#endif /* PTRACE_GETREGSET && NT_PRSTATUS */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001035
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001036void
1037get_regs(pid_t pid)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001038{
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001039/* PTRACE_GETREGSET only */
1040# if defined(METAG) || defined(OR1K) || defined(X32) || defined(AARCH64)
1041 get_regset(pid);
1042
1043/* PTRACE_GETREGS only */
1044# elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001045 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001046# elif defined(TILE)
1047 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &tile_regs);
1048# elif defined(SPARC) || defined(SPARC64)
1049 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001050# elif defined(POWERPC)
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001051 static bool old_kernel = 0;
1052 if (old_kernel)
1053 goto old;
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001054 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &ppc_regs);
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001055 if (get_regs_error && errno == EIO) {
1056 old_kernel = 1;
1057 old:
Anton Blanchard14d51a62013-06-26 14:42:37 +10001058 get_regs_error = powerpc_getregs_old(pid);
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001059 }
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001060
1061/* try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS */
1062# else
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001063# if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
1064 static int getregset_support;
1065
1066 if (getregset_support >= 0) {
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001067 get_regset(pid);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001068 if (getregset_support > 0)
1069 return;
1070 if (get_regs_error >= 0) {
1071 getregset_support = 1;
1072 return;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001073 }
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001074 if (errno == EPERM || errno == ESRCH)
1075 return;
1076 getregset_support = -1;
1077 }
1078# endif /* PTRACE_GETREGSET && NT_PRSTATUS */
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001079# if defined(ARM)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001080 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &arm_regs);
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001081# elif defined(I386)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001082 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &i386_regs);
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001083# elif defined(X86_64)
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001084 /* Use old method, with unreliable heuristical detection of 32-bitness. */
1085 x86_io.iov_len = sizeof(x86_64_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001086 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &x86_64_regs);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001087 if (!get_regs_error && x86_64_regs.cs == 0x23) {
1088 x86_io.iov_len = sizeof(i386_regs);
1089 /*
1090 * The order is important: i386_regs and x86_64_regs
1091 * are overlaid in memory!
1092 */
1093 i386_regs.ebx = x86_64_regs.rbx;
1094 i386_regs.ecx = x86_64_regs.rcx;
1095 i386_regs.edx = x86_64_regs.rdx;
1096 i386_regs.esi = x86_64_regs.rsi;
1097 i386_regs.edi = x86_64_regs.rdi;
1098 i386_regs.ebp = x86_64_regs.rbp;
1099 i386_regs.eax = x86_64_regs.rax;
1100 /* i386_regs.xds = x86_64_regs.ds; unused by strace */
1101 /* i386_regs.xes = x86_64_regs.es; ditto... */
1102 /* i386_regs.xfs = x86_64_regs.fs; */
1103 /* i386_regs.xgs = x86_64_regs.gs; */
1104 i386_regs.orig_eax = x86_64_regs.orig_rax;
1105 i386_regs.eip = x86_64_regs.rip;
1106 /* i386_regs.xcs = x86_64_regs.cs; */
1107 /* i386_regs.eflags = x86_64_regs.eflags; */
1108 i386_regs.esp = x86_64_regs.rsp;
1109 /* i386_regs.xss = x86_64_regs.ss; */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001110 }
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001111# else
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001112# error unhandled architecture
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001113# endif /* ARM || I386 || X86_64 */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001114# endif
1115}
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001116#endif /* !get_regs */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001117
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001118/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001119 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1120 * 1: ok, continue in trace_syscall_entering().
1121 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001122 * ("????" etc) and bail out.
1123 */
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01001124static int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001125get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001126{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001127 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001128
Denys Vlasenko523635f2012-02-25 02:44:25 +01001129#if defined(S390) || defined(S390X)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001130 if (upeek(tcp->pid, PT_GPR2, &syscall_mode) < 0)
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001131 return -1;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001132
1133 if (syscall_mode != -ENOSYS) {
1134 /*
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001135 * Since kernel version 2.5.44 the scno gets passed in gpr2.
Roland McGrath2f924ca2003-06-26 22:23:28 +00001136 */
1137 scno = syscall_mode;
1138 } else {
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001139 /*
Michal Ludvig882eda82002-11-11 12:50:47 +00001140 * Old style of "passing" the scno via the SVC instruction.
1141 */
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001142 long psw;
Michal Ludvig882eda82002-11-11 12:50:47 +00001143 long opcode, offset_reg, tmp;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001144 void *svc_addr;
Denys Vlasenko7c9ba8b2011-08-19 19:46:32 +02001145 static const int gpr_offset[16] = {
1146 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
1147 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
1148 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
1149 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
1150 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001151
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001152 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0)
Michal Ludvig882eda82002-11-11 12:50:47 +00001153 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001154 errno = 0;
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001155 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +00001156 if (errno) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001157 perror_msg("peektext(psw-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +00001158 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001159 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001160
1161 /*
1162 * We have to check if the SVC got executed directly or via an
1163 * EXECUTE instruction. In case of EXECUTE it is necessary to do
1164 * instruction decoding to derive the system call number.
1165 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1166 * so that this doesn't work if a SVC opcode is part of an EXECUTE
1167 * opcode. Since there is no way to find out the opcode size this
1168 * is the best we can do...
1169 */
Michal Ludvig882eda82002-11-11 12:50:47 +00001170 if ((opcode & 0xff00) == 0x0a00) {
1171 /* SVC opcode */
1172 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +00001173 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001174 else {
1175 /* SVC got executed by EXECUTE instruction */
1176
1177 /*
1178 * Do instruction decoding of EXECUTE. If you really want to
1179 * understand this, read the Principles of Operations.
1180 */
1181 svc_addr = (void *) (opcode & 0xfff);
1182
1183 tmp = 0;
1184 offset_reg = (opcode & 0x000f0000) >> 16;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001185 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001186 return -1;
1187 svc_addr += tmp;
1188
1189 tmp = 0;
1190 offset_reg = (opcode & 0x0000f000) >> 12;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001191 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001192 return -1;
1193 svc_addr += tmp;
1194
Denys Vlasenkofb036672009-01-23 16:30:26 +00001195 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
Michal Ludvig882eda82002-11-11 12:50:47 +00001196 if (errno)
1197 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001198# if defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +00001199 scno >>= 48;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001200# else
Michal Ludvig882eda82002-11-11 12:50:47 +00001201 scno >>= 16;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001202# endif
Michal Ludvig882eda82002-11-11 12:50:47 +00001203 tmp = 0;
1204 offset_reg = (opcode & 0x00f00000) >> 20;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001205 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001206 return -1;
1207
1208 scno = (scno | tmp) & 0xff;
1209 }
1210 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001211#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001212 scno = ppc_regs.gpr[0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001213# ifdef POWERPC64
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001214 int currpers;
Andreas Schwabd69fa492010-07-12 21:39:57 +02001215
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001216 /* Check for 64/32 bit mode. */
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001217 /* SF is bit 0 of MSR */
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001218 if ((ppc_regs.msr >> 63) & 1)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001219 currpers = 0;
1220 else
1221 currpers = 1;
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001222 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001223# endif
1224#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001225 scno = avr32_regs.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001226#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001227 if (upeek(tcp->pid, PT_ORIG_P0, &scno))
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001228 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001229#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001230 scno = i386_regs.orig_eax;
H.J. Lu35be5812012-04-16 13:00:01 +02001231#elif defined(X86_64) || defined(X32)
1232# ifndef __X32_SYSCALL_BIT
1233# define __X32_SYSCALL_BIT 0x40000000
1234# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001235 int currpers;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001236# if 1
1237 /* GETREGSET of NT_PRSTATUS tells us regset size,
1238 * which unambiguously detects i386.
1239 *
1240 * Linux kernel distinguishes x86-64 and x32 processes
1241 * solely by looking at __X32_SYSCALL_BIT:
1242 * arch/x86/include/asm/compat.h::is_x32_task():
1243 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1244 * return true;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001245 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001246 if (x86_io.iov_len == sizeof(i386_regs)) {
1247 scno = i386_regs.orig_eax;
1248 currpers = 1;
1249 } else {
1250 scno = x86_64_regs.orig_rax;
1251 currpers = 0;
1252 if (scno & __X32_SYSCALL_BIT) {
1253 scno -= __X32_SYSCALL_BIT;
1254 currpers = 2;
1255 }
1256 }
1257# elif 0
1258 /* cs = 0x33 for long mode (native 64 bit and x32)
1259 * cs = 0x23 for compatibility mode (32 bit)
1260 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1261 */
1262 scno = x86_64_regs.orig_rax;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001263 switch (x86_64_regs.cs) {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001264 case 0x23: currpers = 1; break;
H.J. Lu35be5812012-04-16 13:00:01 +02001265 case 0x33:
1266 if (x86_64_regs.ds == 0x2b) {
1267 currpers = 2;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001268 scno &= ~__X32_SYSCALL_BIT;
H.J. Lu35be5812012-04-16 13:00:01 +02001269 } else
1270 currpers = 0;
1271 break;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001272 default:
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001273 fprintf(stderr, "Unknown value CS=0x%08X while "
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001274 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001275 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001276 currpers = current_personality;
1277 break;
1278 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001279# elif 0
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001280 /* This version analyzes the opcode of a syscall instruction.
1281 * (int 0x80 on i386 vs. syscall on x86-64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001282 * It works, but is too complicated, and strictly speaking, unreliable.
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001283 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001284 unsigned long call, rip = x86_64_regs.rip;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001285 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1286 rip -= 2;
1287 errno = 0;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001288 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001289 if (errno)
1290 fprintf(stderr, "ptrace_peektext failed: %s\n",
1291 strerror(errno));
1292 switch (call & 0xffff) {
1293 /* x86-64: syscall = 0x0f 0x05 */
1294 case 0x050f: currpers = 0; break;
1295 /* i386: int 0x80 = 0xcd 0x80 */
1296 case 0x80cd: currpers = 1; break;
1297 default:
1298 currpers = current_personality;
1299 fprintf(stderr,
1300 "Unknown syscall opcode (0x%04X) while "
1301 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001302 "PID=%d\n", (int)call, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001303 break;
1304 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001305# endif
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001306
H.J. Lu35be5812012-04-16 13:00:01 +02001307# ifdef X32
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001308 /* If we are built for a x32 system, then personality 0 is x32
1309 * (not x86_64), and stracing of x86_64 apps is not supported.
1310 * Stracing of i386 apps is still supported.
H.J. Lu085e4282012-04-17 11:05:04 -07001311 */
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001312 if (currpers == 0) {
1313 fprintf(stderr, "syscall_%lu(...) in unsupported "
1314 "64-bit mode of process PID=%d\n",
1315 scno, tcp->pid);
1316 return 0;
H.J. Lu35be5812012-04-16 13:00:01 +02001317 }
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001318 currpers &= ~2; /* map 2,1 to 0,1 */
H.J. Lu35be5812012-04-16 13:00:01 +02001319# endif
H.J. Lu085e4282012-04-17 11:05:04 -07001320 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001321#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001322# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01001323 long psr;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001324 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001325 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
1326 if (ia64_ia32mode) {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001327 if (upeek(tcp->pid, PT_R1, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001328 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001329 } else {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001330 if (upeek(tcp->pid, PT_R15, &scno) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001331 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001332 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001333#elif defined(AARCH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001334 switch (aarch64_io.iov_len) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001335 case sizeof(aarch64_regs):
1336 /* We are in 64-bit mode */
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001337 scno = aarch64_regs.regs[8];
1338 update_personality(tcp, 1);
1339 break;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001340 case sizeof(arm_regs):
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001341 /* We are in 32-bit mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001342 /* Note: we don't support OABI, unlike 32-bit ARM build */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001343 scno = arm_regs.ARM_r7;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001344 update_personality(tcp, 0);
1345 break;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001346 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001347#elif defined(ARM)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001348 if (arm_regs.ARM_ip != 0) {
1349 /* It is not a syscall entry */
1350 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001351 tcp->flags |= TCB_INSYSCALL;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001352 return 0;
1353 }
1354 /* Note: we support only 32-bit CPUs, not 26-bit */
1355
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001356# ifndef STRACE_KNOWS_ONLY_EABI
1357# warning STRACE_KNOWS_ONLY_EABI not set, will PTRACE_PEEKTEXT on every syscall (slower tracing)
1358 if (arm_regs.ARM_cpsr & 0x20)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001359 /* Thumb mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001360 goto scno_in_r7;
1361 /* ARM mode */
1362 /* Check EABI/OABI by examining SVC insn's low 24 bits */
1363 errno = 0;
1364 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1365 if (errno)
1366 return -1;
1367 /* EABI syscall convention? */
1368 if (scno != 0xef000000) {
1369 /* No, it's OABI */
1370 if ((scno & 0x0ff00000) != 0x0f900000) {
1371 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1372 tcp->pid, scno);
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001373 return -1;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001374 }
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001375 /* Fixup the syscall number */
1376 scno &= 0x000fffff;
1377 } else {
1378 scno_in_r7:
1379 scno = arm_regs.ARM_r7;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001380 }
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001381# else
1382 scno = arm_regs.ARM_r7;
1383# endif
Denys Vlasenko7270de52013-02-21 15:46:34 +01001384 scno = shuffle_scno(scno);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001385#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001386 if (upeek(tcp->pid, 4*PT_ORIG_D0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001387 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001388#elif defined(LINUX_MIPSN32)
Roland McGrath542c2c62008-05-20 01:11:56 +00001389 unsigned long long regs[38];
1390
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001391 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001392 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001393 mips_a3 = regs[REG_A3];
1394 mips_r2 = regs[REG_V0];
Roland McGrath542c2c62008-05-20 01:11:56 +00001395
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001396 scno = mips_r2;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001397 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001398 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001399 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001400 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001401 return 0;
1402 }
Roland McGrath542c2c62008-05-20 01:11:56 +00001403 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001404#elif defined(MIPS)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001405 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001406 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001407 if (upeek(tcp->pid, REG_V0, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001408 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001409
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001410 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001411 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001412 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001413 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001414 return 0;
1415 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001416 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001417#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001418 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001419 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001420 if (upeek(tcp->pid, REG_R0, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001421 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001422
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001423 /*
1424 * Do some sanity checks to figure out if it's
1425 * really a syscall entry
1426 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001427 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001428 if (alpha_a3 == 0 || alpha_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001429 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001430 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001431 return 0;
1432 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001433 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001434#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001435 /* Disassemble the syscall trap. */
1436 /* Retrieve the syscall trap instruction. */
Denys Vlasenko46455822013-02-05 17:02:59 +01001437 unsigned long trap;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001438 errno = 0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001439# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001440 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001441 trap >>= 32;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001442# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001443 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001444# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001445 if (errno)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001446 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001447
1448 /* Disassemble the trap to see what personality to use. */
1449 switch (trap) {
1450 case 0x91d02010:
1451 /* Linux/SPARC syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001452 update_personality(tcp, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001453 break;
1454 case 0x91d0206d:
1455 /* Linux/SPARC64 syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001456 update_personality(tcp, 2);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001457 break;
1458 case 0x91d02000:
1459 /* SunOS syscall trap. (pers 1) */
1460 fprintf(stderr, "syscall: SunOS no support\n");
1461 return -1;
1462 case 0x91d02008:
1463 /* Solaris 2.x syscall trap. (per 2) */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001464 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001465 break;
1466 case 0x91d02009:
1467 /* NetBSD/FreeBSD syscall trap. */
1468 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1469 return -1;
1470 case 0x91d02027:
1471 /* Solaris 2.x gettimeofday */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001472 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001473 break;
1474 default:
Denys Vlasenko523635f2012-02-25 02:44:25 +01001475# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001476 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001477# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001478 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001479# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001480 return -1;
1481 }
1482
1483 /* Extract the system call number from the registers. */
1484 if (trap == 0x91d02027)
1485 scno = 156;
1486 else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001487 scno = sparc_regs.u_regs[U_REG_G1];
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001488 if (scno == 0) {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001489 scno = sparc_regs.u_regs[U_REG_O0];
1490 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 +02001491 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001492#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001493 if (upeek(tcp->pid, PT_GR20, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001494 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001495#elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001496 /*
1497 * In the new syscall ABI, the system call number is in R3.
1498 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001499 if (upeek(tcp->pid, 4*(REG_REG0+3), &scno) < 0)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001500 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001501
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001502 if (scno < 0) {
1503 /* Odd as it may seem, a glibc bug has been known to cause
1504 glibc to issue bogus negative syscall numbers. So for
1505 our purposes, make strace print what it *should* have been */
1506 long correct_scno = (scno & 0xff);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001507 if (debug_flag)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001508 fprintf(stderr,
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001509 "Detected glibc bug: bogus system call"
1510 " number = %ld, correcting to %ld\n",
1511 scno,
1512 correct_scno);
1513 scno = correct_scno;
1514 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001515#elif defined(SH64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001516 if (upeek(tcp->pid, REG_SYSCALL, &scno) < 0)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001517 return -1;
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001518 scno &= 0xFFFF;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001519#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001520 if (upeek(tcp->pid, 4*PT_R9, &scno) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001521 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001522#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001523 int currpers;
1524 scno = tile_regs.regs[10];
1525# ifdef __tilepro__
1526 currpers = 1;
1527# else
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001528# ifndef PT_FLAGS_COMPAT
1529# define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1530# endif
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001531 if (tile_regs.flags & PT_FLAGS_COMPAT)
1532 currpers = 1;
1533 else
1534 currpers = 0;
1535# endif
1536 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001537#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001538 if (upeek(tcp->pid, 0, &scno) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001539 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001540#elif defined(OR1K)
1541 scno = or1k_regs.gpr[11];
James Hogan5f999a82013-02-22 14:44:10 +00001542#elif defined(METAG)
1543 scno = metag_regs.dx[0][1]; /* syscall number in D1Re0 (D1.0) */
Chris Zankel8f636ed2013-03-25 10:22:07 -07001544#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001545 if (upeek(tcp->pid, SYSCALL_NR, &scno) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001546 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001547#endif
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001548
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001549 tcp->scno = scno;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001550 if (SCNO_IS_VALID(tcp->scno)) {
1551 tcp->s_ent = &sysent[scno];
1552 tcp->qual_flg = qual_flags[scno];
1553 } else {
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01001554 static const struct_sysent unknown = {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001555 .nargs = MAX_ARGS,
1556 .sys_flags = 0,
1557 .sys_func = printargs,
1558 .sys_name = "unknown", /* not used */
1559 };
1560 tcp->s_ent = &unknown;
1561 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1562 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001563 return 1;
1564}
1565
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02001566/* Called at each syscall entry.
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001567 * Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001568 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1569 * 1: ok, continue in trace_syscall_entering().
1570 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001571 * ("????" etc) and bail out.
1572 */
Roland McGratha4d48532005-06-08 20:45:28 +00001573static int
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001574syscall_fixup_on_sysenter(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001575{
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001576 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001577#if defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001578 if (i386_regs.eax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001579 if (debug_flag)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001580 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1581 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001582 }
H.J. Lu35be5812012-04-16 13:00:01 +02001583#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001584 {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001585 long rax;
1586 if (x86_io.iov_len == sizeof(i386_regs)) {
1587 /* Sign extend from 32 bits */
1588 rax = (int32_t)i386_regs.eax;
1589 } else {
1590 /* Note: in X32 build, this truncates 64 to 32 bits */
1591 rax = x86_64_regs.rax;
1592 }
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001593 if (rax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001594 if (debug_flag)
Denys Vlasenko18beb982011-08-24 16:59:23 +02001595 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1596 return 0;
1597 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001598 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001599#elif defined(S390) || defined(S390X)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001600 /* TODO: we already fetched PT_GPR2 in get_scno
1601 * and stored it in syscall_mode, reuse it here
1602 * instead of re-fetching?
1603 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001604 if (upeek(tcp->pid, PT_GPR2, &gpr2) < 0)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001605 return -1;
Michal Ludvig882eda82002-11-11 12:50:47 +00001606 if (syscall_mode != -ENOSYS)
1607 syscall_mode = tcp->scno;
Denys Vlasenkoece98792011-08-25 10:25:35 +02001608 if (gpr2 != syscall_mode) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001609 if (debug_flag)
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001610 fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2);
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001611 return 0;
1612 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001613#elif defined(M68K)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001614 /* TODO? Eliminate upeek's in arches below like we did in x86 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001615 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001616 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001617 if (m68k_d0 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001618 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001619 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001620 return 0;
1621 }
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001622#elif defined(IA64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001623 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001624 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001625 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001626 return -1;
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001627 if (ia64_ia32mode && ia64_r8 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001628 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001629 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001630 return 0;
1631 }
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001632#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001633 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001634 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001635 if (cris_r10 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001636 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001637 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001638 return 0;
1639 }
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001640#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001641 if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001642 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001643 if (microblaze_r3 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001644 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001645 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001646 return 0;
1647 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001648#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001649 return 1;
1650}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001651
Denys Vlasenko146b9442012-03-18 22:10:48 +01001652static void
1653internal_fork(struct tcb *tcp)
1654{
1655#if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1656# define ARG_FLAGS 1
1657#else
1658# define ARG_FLAGS 0
1659#endif
1660#ifndef CLONE_UNTRACED
1661# define CLONE_UNTRACED 0x00800000
1662#endif
1663 if ((ptrace_setoptions
1664 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1665 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1666 return;
1667
1668 if (!followfork)
1669 return;
1670
1671 if (entering(tcp)) {
1672 /*
1673 * We won't see the new child if clone is called with
1674 * CLONE_UNTRACED, so we keep the same logic with that option
1675 * and don't trace it.
1676 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001677 if ((tcp->s_ent->sys_func == sys_clone)
1678 && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
1679 )
Denys Vlasenko146b9442012-03-18 22:10:48 +01001680 return;
1681 setbpt(tcp);
1682 } else {
1683 if (tcp->flags & TCB_BPTSET)
1684 clearbpt(tcp);
1685 }
1686}
1687
1688#if defined(TCB_WAITEXECVE)
1689static void
1690internal_exec(struct tcb *tcp)
1691{
1692 /* Maybe we have post-execve SIGTRAP suppressed? */
1693 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1694 return; /* yes, no need to do anything */
1695
1696 if (exiting(tcp) && syserror(tcp))
1697 /* Error in execve, no post-execve SIGTRAP expected */
1698 tcp->flags &= ~TCB_WAITEXECVE;
1699 else
1700 tcp->flags |= TCB_WAITEXECVE;
1701}
1702#endif
1703
1704static void
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001705syscall_fixup_for_fork_exec(struct tcb *tcp)
Roland McGrathc1e45922008-05-27 23:18:29 +00001706{
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001707 /*
1708 * We must always trace a few critical system calls in order to
1709 * correctly support following forks in the presence of tracing
1710 * qualifiers.
1711 */
1712 int (*func)();
1713
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001714 func = tcp->s_ent->sys_func;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001715
1716 if ( sys_fork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001717 || sys_vfork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001718 || sys_clone == func
Denys Vlasenko146b9442012-03-18 22:10:48 +01001719 ) {
1720 internal_fork(tcp);
1721 return;
1722 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001723
Denys Vlasenko84703742012-02-25 02:38:52 +01001724#if defined(TCB_WAITEXECVE)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001725 if ( sys_execve == func
Denys Vlasenko84703742012-02-25 02:38:52 +01001726# if defined(SPARC) || defined(SPARC64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001727 || sys_execv == func
Denys Vlasenkoa7949742011-08-21 17:26:55 +02001728# endif
Denys Vlasenko146b9442012-03-18 22:10:48 +01001729 ) {
1730 internal_exec(tcp);
1731 return;
1732 }
Roland McGrathc1e45922008-05-27 23:18:29 +00001733#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001734}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001735
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001736/* Return -1 on error or 1 on success (never 0!) */
Roland McGratha4d48532005-06-08 20:45:28 +00001737static int
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001738get_syscall_args(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001739{
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001740 int i, nargs;
1741
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001742 nargs = tcp->s_ent->nargs;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001743
Denys Vlasenko523635f2012-02-25 02:44:25 +01001744#if defined(S390) || defined(S390X)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001745 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001746 if (upeek(tcp->pid, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001747 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001748#elif defined(ALPHA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001749 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001750 if (upeek(tcp->pid, REG_A0+i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001751 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001752#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001753 if (!ia64_ia32mode) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001754 unsigned long *out0, cfm, sof, sol;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001755 long rbs_end;
1756 /* be backwards compatible with kernel < 2.4.4... */
1757# ifndef PT_RBS_END
1758# define PT_RBS_END PT_AR_BSP
1759# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001760
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001761 if (upeek(tcp->pid, PT_RBS_END, &rbs_end) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001762 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001763 if (upeek(tcp->pid, PT_CFM, (long *) &cfm) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001764 return -1;
1765
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001766 sof = (cfm >> 0) & 0x7f;
1767 sol = (cfm >> 7) & 0x7f;
1768 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1769
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001770 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001771 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1772 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1773 return -1;
1774 }
1775 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001776 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1777 PT_R9 /* ECX = out1 */,
1778 PT_R10 /* EDX = out2 */,
1779 PT_R14 /* ESI = out3 */,
1780 PT_R15 /* EDI = out4 */,
1781 PT_R13 /* EBP = out5 */};
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001782
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001783 for (i = 0; i < nargs; ++i) {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001784 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001785 return -1;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001786 /* truncate away IVE sign-extension */
1787 tcp->u_arg[i] &= 0xffffffff;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001788 }
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001789 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001790#elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001791 /* N32 and N64 both use up to six registers. */
1792 unsigned long long regs[38];
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001793
1794 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1795 return -1;
1796
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001797 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001798 tcp->u_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001799# if defined(LINUX_MIPSN32)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001800 tcp->ext_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001801# endif
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001802 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001803#elif defined(MIPS)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001804 if (nargs > 4) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001805 long sp;
1806
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001807 if (upeek(tcp->pid, REG_SP, &sp) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001808 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001809 for (i = 0; i < 4; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001810 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001811 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001812 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001813 (char *)(tcp->u_arg + 4));
1814 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001815 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001816 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001817 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001818 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001819#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001820 (void)i;
1821 (void)nargs;
1822 tcp->u_arg[0] = ppc_regs.orig_gpr3;
1823 tcp->u_arg[1] = ppc_regs.gpr[4];
1824 tcp->u_arg[2] = ppc_regs.gpr[5];
1825 tcp->u_arg[3] = ppc_regs.gpr[6];
1826 tcp->u_arg[4] = ppc_regs.gpr[7];
1827 tcp->u_arg[5] = ppc_regs.gpr[8];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001828#elif defined(SPARC) || defined(SPARC64)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001829 for (i = 0; i < nargs; ++i)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001830 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001831#elif defined(HPPA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001832 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001833 if (upeek(tcp->pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001834 return -1;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001835#elif defined(ARM) || defined(AARCH64)
1836# if defined(AARCH64)
1837 if (tcp->currpers == 1)
1838 for (i = 0; i < nargs; ++i)
1839 tcp->u_arg[i] = aarch64_regs.regs[i];
1840 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001841# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001842 for (i = 0; i < nargs; ++i)
Denys Vlasenko401374e2013-02-06 18:24:39 +01001843 tcp->u_arg[i] = arm_regs.uregs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001844#elif defined(AVR32)
Denys Vlasenkob5b25892011-08-30 19:04:54 +02001845 (void)i;
1846 (void)nargs;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001847 tcp->u_arg[0] = avr32_regs.r12;
1848 tcp->u_arg[1] = avr32_regs.r11;
1849 tcp->u_arg[2] = avr32_regs.r10;
1850 tcp->u_arg[3] = avr32_regs.r9;
1851 tcp->u_arg[4] = avr32_regs.r5;
1852 tcp->u_arg[5] = avr32_regs.r3;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001853#elif defined(BFIN)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001854 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 +02001855
Denys Vlasenko4b887a52011-08-23 13:32:38 +02001856 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001857 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001858 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001859#elif defined(SH)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001860 static const int syscall_regs[MAX_ARGS] = {
1861 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1862 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001863 };
1864
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001865 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001866 if (upeek(tcp->pid, syscall_regs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001867 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001868#elif defined(SH64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001869 int i;
1870 /* Registers used by SH5 Linux system calls for parameters */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001871 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001872
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001873 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001874 if (upeek(tcp->pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001875 return -1;
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001876#elif defined(I386)
1877 (void)i;
1878 (void)nargs;
1879 tcp->u_arg[0] = i386_regs.ebx;
1880 tcp->u_arg[1] = i386_regs.ecx;
1881 tcp->u_arg[2] = i386_regs.edx;
1882 tcp->u_arg[3] = i386_regs.esi;
1883 tcp->u_arg[4] = i386_regs.edi;
1884 tcp->u_arg[5] = i386_regs.ebp;
H.J. Lu35be5812012-04-16 13:00:01 +02001885#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001886 (void)i;
1887 (void)nargs;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001888 if (x86_io.iov_len != sizeof(i386_regs)) {
1889 /* x86-64 or x32 ABI */
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001890 tcp->u_arg[0] = x86_64_regs.rdi;
1891 tcp->u_arg[1] = x86_64_regs.rsi;
1892 tcp->u_arg[2] = x86_64_regs.rdx;
1893 tcp->u_arg[3] = x86_64_regs.r10;
1894 tcp->u_arg[4] = x86_64_regs.r8;
1895 tcp->u_arg[5] = x86_64_regs.r9;
H.J. Lu35be5812012-04-16 13:00:01 +02001896# ifdef X32
1897 tcp->ext_arg[0] = x86_64_regs.rdi;
1898 tcp->ext_arg[1] = x86_64_regs.rsi;
1899 tcp->ext_arg[2] = x86_64_regs.rdx;
1900 tcp->ext_arg[3] = x86_64_regs.r10;
1901 tcp->ext_arg[4] = x86_64_regs.r8;
1902 tcp->ext_arg[5] = x86_64_regs.r9;
1903# endif
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001904 } else {
1905 /* i386 ABI */
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001906 /* Zero-extend from 32 bits */
1907 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1908 * if you need to use *sign-extended* parameter.
1909 */
1910 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1911 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1912 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1913 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1914 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1915 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001916 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001917#elif defined(MICROBLAZE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001918 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001919 if (upeek(tcp->pid, (5 + i) * 4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001920 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001921#elif defined(CRISV10) || defined(CRISV32)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001922 static const int crisregs[MAX_ARGS] = {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001923 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001924 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001925 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001926
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001927 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001928 if (upeek(tcp->pid, crisregs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001929 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001930#elif defined(TILE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001931 for (i = 0; i < nargs; ++i)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001932 tcp->u_arg[i] = tile_regs.regs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001933#elif defined(M68K)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001934 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001935 if (upeek(tcp->pid, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001936 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001937#elif defined(OR1K)
1938 (void)nargs;
1939 for (i = 0; i < 6; ++i)
1940 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
James Hogan5f999a82013-02-22 14:44:10 +00001941#elif defined(METAG)
1942 for (i = 0; i < nargs; i++)
1943 /* arguments go backwards from D1Ar1 (D1.3) */
1944 tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
Chris Zankel8f636ed2013-03-25 10:22:07 -07001945#elif defined(XTENSA)
1946 /* arg0: a6, arg1: a3, arg2: a4, arg3: a5, arg4: a8, arg5: a9 */
1947 static const int xtensaregs[MAX_ARGS] = { 6, 3, 4, 5, 8, 9 };
1948 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001949 if (upeek(tcp->pid, REG_A_BASE + xtensaregs[i], &tcp->u_arg[i]) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001950 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001951#else /* Other architecture (32bits specific) */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001952 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001953 if (upeek(tcp->pid, i*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001954 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001955#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001956 return 1;
1957}
1958
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00001959static int
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001960trace_syscall_entering(struct tcb *tcp)
1961{
1962 int res, scno_good;
1963
Denys Vlasenko2ce12ed2011-08-24 17:25:32 +02001964#if defined TCB_WAITEXECVE
1965 if (tcp->flags & TCB_WAITEXECVE) {
1966 /* This is the post-execve SIGTRAP. */
1967 tcp->flags &= ~TCB_WAITEXECVE;
1968 return 0;
1969 }
1970#endif
1971
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001972 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001973 if (res == 0)
1974 return res;
Denys Vlasenko907735a2012-03-21 00:23:16 +01001975 if (res == 1) {
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001976 res = syscall_fixup_on_sysenter(tcp);
Denys Vlasenko907735a2012-03-21 00:23:16 +01001977 if (res == 0)
1978 return res;
1979 if (res == 1)
1980 res = get_syscall_args(tcp);
1981 }
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001982
1983 if (res != 1) {
1984 printleader(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001985 if (scno_good != 1)
Denys Vlasenkob7a6dae2012-03-20 16:48:35 +01001986 tprints("????" /* anti-trigraph gap */ "(");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001987 else if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01001988 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001989 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001990 tprintf("%s(", tcp->s_ent->sys_name);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001991 /*
1992 * " <unavailable>" will be added later by the code which
1993 * detects ptrace errors.
1994 */
1995 goto ret;
1996 }
1997
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02001998 if ( sys_execve == tcp->s_ent->sys_func
1999# if defined(SPARC) || defined(SPARC64)
2000 || sys_execv == tcp->s_ent->sys_func
2001# endif
2002 ) {
2003 hide_log_until_execve = 0;
2004 }
2005
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002006#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002007 while (1) {
Denys Vlasenko523635f2012-02-25 02:44:25 +01002008# ifdef SYS_socket_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002009 if (tcp->s_ent->sys_func == sys_socketcall) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00002010 decode_socket_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002011 break;
2012 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002013# endif
2014# ifdef SYS_ipc_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002015 if (tcp->s_ent->sys_func == sys_ipc) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00002016 decode_ipc_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002017 break;
2018 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002019# endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002020 break;
2021 }
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002022#endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002023
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01002024 if (need_fork_exec_workarounds)
2025 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002026
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002027 if (!(tcp->qual_flg & QUAL_TRACE)
2028 || (tracing_paths && !pathtrace_match(tcp))
2029 ) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002030 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
2031 return 0;
2032 }
2033
2034 tcp->flags &= ~TCB_FILTERED;
2035
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002036 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002037 res = 0;
2038 goto ret;
2039 }
2040
2041 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002042 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002043 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002044 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002045 tprintf("%s(", tcp->s_ent->sys_name);
2046 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002047 res = printargs(tcp);
2048 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002049 res = tcp->s_ent->sys_func(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002050
Dmitry V. Levinb742d8c2012-09-17 22:40:12 +00002051 fflush(tcp->outf);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002052 ret:
2053 tcp->flags |= TCB_INSYSCALL;
2054 /* Measure the entrance time as late as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002055 if (Tflag || cflag)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002056 gettimeofday(&tcp->etime, NULL);
2057 return res;
2058}
2059
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002060/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01002061 * 1: ok, continue in trace_syscall_exiting().
2062 * -1: error, trace_syscall_exiting() should print error indicator
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002063 * ("????" etc) and bail out.
2064 */
2065static int
2066get_syscall_result(struct tcb *tcp)
2067{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002068#if defined(S390) || defined(S390X)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002069 if (upeek(tcp->pid, PT_GPR2, &gpr2) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002070 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002071#elif defined(POWERPC)
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002072 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002073#elif defined(AVR32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002074 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002075#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002076 if (upeek(tcp->pid, PT_R0, &bfin_r0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002077 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002078#elif defined(I386)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002079 /* already done by get_regs */
H.J. Lu35be5812012-04-16 13:00:01 +02002080#elif defined(X86_64) || defined(X32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002081 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002082#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002083# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01002084 long psr;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002085 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02002086 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002087 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002088 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002089 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002090 return -1;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002091#elif defined(ARM)
2092 /* already done by get_regs */
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002093#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002094 /* register reading already done by get_regs */
2095
2096 /* Used to do this, but we did it on syscall entry already: */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002097 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
2098 * else it's personality 0.
2099 */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002100 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
Denys Vlasenko523635f2012-02-25 02:44:25 +01002101#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002102 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002103 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002104#elif defined(LINUX_MIPSN32)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002105 unsigned long long regs[38];
2106
2107 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
2108 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002109 mips_a3 = regs[REG_A3];
2110 mips_r2 = regs[REG_V0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01002111#elif defined(MIPS)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002112 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002113 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002114 if (upeek(tcp->pid, REG_V0, &mips_r2) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002115 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002116#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002117 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002118 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002119 if (upeek(tcp->pid, REG_R0, &alpha_r0) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002120 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002121#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002122 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002123#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002124 if (upeek(tcp->pid, PT_GR28, &hppa_r28) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002125 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002126#elif defined(SH)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002127 /* new syscall ABI returns result in R0 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002128 if (upeek(tcp->pid, 4*REG_REG0, (long *)&sh_r0) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002129 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002130#elif defined(SH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002131 /* ABI defines result returned in r9 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002132 if (upeek(tcp->pid, REG_GENERAL(9), (long *)&sh64_r9) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002133 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002134#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002135 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002136 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002137#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002138 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002139#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002140 if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002141 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01002142#elif defined(OR1K)
2143 /* already done by get_regs */
James Hogan5f999a82013-02-22 14:44:10 +00002144#elif defined(METAG)
2145 /* already done by get_regs */
Chris Zankel8f636ed2013-03-25 10:22:07 -07002146#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002147 if (upeek(tcp->pid, REG_A_BASE + 2, &xtensa_a2) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07002148 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002149#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002150 return 1;
2151}
2152
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002153/* Called at each syscall exit */
2154static void
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002155syscall_fixup_on_sysexit(struct tcb *tcp)
2156{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002157#if defined(S390) || defined(S390X)
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002158 if (syscall_mode != -ENOSYS)
2159 syscall_mode = tcp->scno;
Denys Vlasenkoece98792011-08-25 10:25:35 +02002160 if ((tcp->flags & TCB_WAITEXECVE)
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002161 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
2162 /*
2163 * Return from execve.
2164 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2165 * flag set for the post-execve SIGTRAP to see and reset.
2166 */
2167 gpr2 = 0;
2168 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002169#endif
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002170}
2171
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002172/*
2173 * Check the syscall return value register value for whether it is
2174 * a negated errno code indicating an error, or a success return value.
2175 */
2176static inline int
2177is_negated_errno(unsigned long int val)
2178{
2179 unsigned long int max = -(long int) nerrnos;
Denys Vlasenko2544f982013-02-19 17:39:56 +01002180#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01002181 if (current_wordsize < sizeof(val)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002182 val = (unsigned int) val;
2183 max = (unsigned int) max;
2184 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002185#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002186 return val > max;
2187}
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002188
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002189#if defined(X32)
2190static inline int
2191is_negated_errno_x32(unsigned long long val)
2192{
2193 unsigned long long max = -(long long) nerrnos;
2194 /*
2195 * current_wordsize is 4 even in personality 0 (native X32)
2196 * but truncation _must not_ be done in it.
2197 * can't check current_wordsize here!
2198 */
2199 if (current_personality != 0) {
2200 val = (uint32_t) val;
2201 max = (uint32_t) max;
2202 }
2203 return val > max;
2204}
2205#endif
2206
Denys Vlasenko907735a2012-03-21 00:23:16 +01002207/* Returns:
2208 * 1: ok, continue in trace_syscall_exiting().
2209 * -1: error, trace_syscall_exiting() should print error indicator
2210 * ("????" etc) and bail out.
2211 */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002212static void
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002213get_error(struct tcb *tcp)
2214{
2215 int u_error = 0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002216 int check_errno = 1;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002217 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002218 check_errno = 0;
2219 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002220#if defined(S390) || defined(S390X)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002221 if (check_errno && is_negated_errno(gpr2)) {
2222 tcp->u_rval = -1;
2223 u_error = -gpr2;
2224 }
2225 else {
2226 tcp->u_rval = gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002227 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002228#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002229 if (check_errno && is_negated_errno(i386_regs.eax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002230 tcp->u_rval = -1;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002231 u_error = -i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002232 }
2233 else {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002234 tcp->u_rval = i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002235 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002236#elif defined(X86_64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002237 long rax;
2238 if (x86_io.iov_len == sizeof(i386_regs)) {
2239 /* Sign extend from 32 bits */
2240 rax = (int32_t)i386_regs.eax;
2241 } else {
2242 rax = x86_64_regs.rax;
2243 }
2244 if (check_errno && is_negated_errno(rax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002245 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002246 u_error = -rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002247 }
2248 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002249 tcp->u_rval = rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002250 }
2251#elif defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002252 /* In X32, return value is 64-bit (llseek uses one).
2253 * Using merely "long rax" would not work.
2254 */
2255 long long rax;
2256 if (x86_io.iov_len == sizeof(i386_regs)) {
2257 /* Sign extend from 32 bits */
2258 rax = (int32_t)i386_regs.eax;
2259 } else {
2260 rax = x86_64_regs.rax;
2261 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002262 /* Careful: is_negated_errno() works only on longs */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002263 if (check_errno && is_negated_errno_x32(rax)) {
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002264 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002265 u_error = -rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002266 }
2267 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002268 tcp->u_rval = rax; /* truncating */
2269 tcp->u_lrval = rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002270 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002271#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02002272 if (ia64_ia32mode) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002273 int err;
2274
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002275 err = (int)ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002276 if (check_errno && is_negated_errno(err)) {
2277 tcp->u_rval = -1;
2278 u_error = -err;
2279 }
2280 else {
2281 tcp->u_rval = err;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002282 }
2283 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002284 if (check_errno && ia64_r10) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002285 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002286 u_error = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002287 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002288 tcp->u_rval = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002289 }
2290 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002291#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002292 if (check_errno && mips_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002293 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002294 u_error = mips_r2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002295 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002296 tcp->u_rval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002297# if defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002298 tcp->u_lrval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002299# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002300 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002301#elif defined(POWERPC)
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002302 if (check_errno && (ppc_regs.ccr & 0x10000000)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002303 tcp->u_rval = -1;
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002304 u_error = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002305 }
2306 else {
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002307 tcp->u_rval = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002308 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002309#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002310 if (check_errno && is_negated_errno(m68k_d0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002311 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002312 u_error = -m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002313 }
2314 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002315 tcp->u_rval = m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002316 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002317#elif defined(ARM) || defined(AARCH64)
2318# if defined(AARCH64)
2319 if (tcp->currpers == 1) {
2320 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2321 tcp->u_rval = -1;
2322 u_error = -aarch64_regs.regs[0];
2323 }
2324 else {
2325 tcp->u_rval = aarch64_regs.regs[0];
2326 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002327 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002328 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002329# endif
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002330 {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002331 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002332 tcp->u_rval = -1;
Denys Vlasenko401374e2013-02-06 18:24:39 +01002333 u_error = -arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002334 }
2335 else {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002336 tcp->u_rval = arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002337 }
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002338 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002339#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002340 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002341 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002342 u_error = -avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002343 }
2344 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002345 tcp->u_rval = avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002346 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002347#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002348 if (check_errno && is_negated_errno(bfin_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002349 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002350 u_error = -bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002351 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002352 tcp->u_rval = bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002353 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002354#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002355 if (check_errno && alpha_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002356 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002357 u_error = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002358 }
2359 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002360 tcp->u_rval = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002361 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002362#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002363 if (check_errno && sparc_regs.psr & PSR_C) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002364 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002365 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002366 }
2367 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002368 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002369 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002370#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002371 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002372 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002373 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002374 }
2375 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002376 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002377 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002378#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002379 if (check_errno && is_negated_errno(hppa_r28)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002380 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002381 u_error = -hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002382 }
2383 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002384 tcp->u_rval = hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002385 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002386#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002387 if (check_errno && is_negated_errno(sh_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002388 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002389 u_error = -sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002390 }
2391 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002392 tcp->u_rval = sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002393 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002394#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002395 if (check_errno && is_negated_errno(sh64_r9)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002396 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002397 u_error = -sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002398 }
2399 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002400 tcp->u_rval = sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002401 }
James Hogan5f999a82013-02-22 14:44:10 +00002402#elif defined(METAG)
2403 /* result pointer in D0Re0 (D0.0) */
2404 if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2405 tcp->u_rval = -1;
2406 u_error = -metag_regs.dx[0][0];
2407 }
2408 else {
2409 tcp->u_rval = metag_regs.dx[0][0];
2410 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002411#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002412 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002413 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002414 u_error = -cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002415 }
2416 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002417 tcp->u_rval = cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002418 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002419#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002420 /*
2421 * The standard tile calling convention returns the value (or negative
2422 * errno) in r0, and zero (or positive errno) in r1.
2423 * Until at least kernel 3.8, however, the r1 value is not reflected
2424 * in ptregs at this point, so we use r0 here.
2425 */
2426 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002427 tcp->u_rval = -1;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002428 u_error = -tile_regs.regs[0];
2429 } else {
2430 tcp->u_rval = tile_regs.regs[0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002431 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002432#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002433 if (check_errno && is_negated_errno(microblaze_r3)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002434 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002435 u_error = -microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002436 }
2437 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002438 tcp->u_rval = microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002439 }
Christian Svensson492f81f2013-02-14 13:26:27 +01002440#elif defined(OR1K)
2441 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2442 tcp->u_rval = -1;
2443 u_error = -or1k_regs.gpr[11];
2444 }
2445 else {
2446 tcp->u_rval = or1k_regs.gpr[11];
2447 }
Chris Zankel8f636ed2013-03-25 10:22:07 -07002448#elif defined(XTENSA)
2449 if (check_errno && is_negated_errno(xtensa_a2)) {
2450 tcp->u_rval = -1;
2451 u_error = -xtensa_a2;
2452 }
2453 else {
2454 tcp->u_rval = xtensa_a2;
2455 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002456#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002457 tcp->u_error = u_error;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002458}
2459
2460static void
2461dumpio(struct tcb *tcp)
2462{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002463 int (*func)();
2464
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002465 if (syserror(tcp))
2466 return;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +01002467 if ((unsigned long) tcp->u_arg[0] >= num_quals)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002468 return;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002469 func = tcp->s_ent->sys_func;
2470 if (func == printargs)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002471 return;
2472 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002473 if (func == sys_read ||
2474 func == sys_pread ||
2475 func == sys_recv ||
2476 func == sys_recvfrom)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002477 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002478 else if (func == sys_readv)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002479 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2480 return;
2481 }
2482 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002483 if (func == sys_write ||
2484 func == sys_pwrite ||
2485 func == sys_send ||
2486 func == sys_sendto)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002487 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002488 else if (func == sys_writev)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002489 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2490 return;
2491 }
2492}
2493
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002494static int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002495trace_syscall_exiting(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002496{
2497 int sys_res;
2498 struct timeval tv;
Denys Vlasenko1a5b5a72011-08-25 00:29:56 +02002499 int res;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002500 long u_error;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002501
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002502 /* Measure the exit time as early as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002503 if (Tflag || cflag)
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002504 gettimeofday(&tv, NULL);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002505
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00002506#if SUPPORTED_PERSONALITIES > 1
2507 update_personality(tcp, tcp->currpers);
2508#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002509 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002510 if (res == 1) {
2511 syscall_fixup_on_sysexit(tcp); /* never fails */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002512 get_error(tcp); /* never fails */
2513 if (need_fork_exec_workarounds)
2514 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002515 if (filtered(tcp) || hide_log_until_execve)
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002516 goto ret;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002517 }
2518
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002519 if (cflag) {
2520 struct timeval t = tv;
Denys Vlasenkoc95a88f2011-08-21 17:47:40 +02002521 count_syscall(tcp, &t);
Denys Vlasenko7b609d52011-06-22 14:32:43 +02002522 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002523 goto ret;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002524 }
2525 }
2526
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002527 /* If not in -ff mode, and printing_tcp != tcp,
2528 * then the log currently does not end with output
2529 * of _our syscall entry_, but with something else.
2530 * We need to say which syscall's return is this.
2531 *
2532 * Forced reprinting via TCB_REPRINT is used only by
2533 * "strace -ff -oLOG test/threaded_execve" corner case.
2534 * It's the only case when -ff mode needs reprinting.
2535 */
2536 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2537 tcp->flags &= ~TCB_REPRINT;
2538 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002539 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002540 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002541 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002542 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002543 }
2544 printing_tcp = tcp;
2545
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002546 if (res != 1) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002547 /* There was error in one of prior ptrace ops */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002548 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002549 tabto();
Denys Vlasenko000b6012012-01-28 01:25:03 +01002550 tprints("= ? <unavailable>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002551 line_ended();
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002552 tcp->flags &= ~TCB_INSYSCALL;
2553 return res;
2554 }
2555
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002556 sys_res = 0;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002557 if (tcp->qual_flg & QUAL_RAW) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002558 /* sys_res = printargs(tcp); - but it's nop on sysexit */
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002559 } else {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002560 /* FIXME: not_failing_only (IOW, option -z) is broken:
2561 * failure of syscall is known only after syscall return.
2562 * Thus we end up with something like this on, say, ENOENT:
2563 * open("doesnt_exist", O_RDONLY <unfinished ...>
2564 * {next syscall decode}
2565 * whereas the intended result is that open(...) line
2566 * is not shown at all.
2567 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002568 if (not_failing_only && tcp->u_error)
Denys Vlasenko3b738812011-08-22 02:06:35 +02002569 goto ret; /* ignore failed syscalls */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002570 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002571 }
2572
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002573 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002574 tabto();
Denys Vlasenko3b738812011-08-22 02:06:35 +02002575 u_error = tcp->u_error;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002576 if (tcp->qual_flg & QUAL_RAW) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002577 if (u_error)
2578 tprintf("= -1 (errno %ld)", u_error);
2579 else
2580 tprintf("= %#lx", tcp->u_rval);
2581 }
2582 else if (!(sys_res & RVAL_NONE) && u_error) {
2583 switch (u_error) {
Denys Vlasenkofe585652012-01-12 11:26:34 +01002584 /* Blocked signals do not interrupt any syscalls.
2585 * In this case syscalls don't return ERESTARTfoo codes.
2586 *
2587 * Deadly signals set to SIG_DFL interrupt syscalls
2588 * and kill the process regardless of which of the codes below
2589 * is returned by the interrupted syscall.
2590 * In some cases, kernel forces a kernel-generated deadly
2591 * signal to be unblocked and set to SIG_DFL (and thus cause
2592 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2593 * or SIGILL. (The alternative is to leave process spinning
2594 * forever on the faulty instruction - not useful).
2595 *
2596 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2597 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2598 * but kernel will always restart them.
2599 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002600 case ERESTARTSYS:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002601 /* Most common type of signal-interrupted syscall exit code.
2602 * The system call will be restarted with the same arguments
2603 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2604 */
2605 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002606 break;
2607 case ERESTARTNOINTR:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002608 /* Rare. For example, fork() returns this if interrupted.
2609 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2610 */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002611 tprints("= ? ERESTARTNOINTR (To be restarted)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002612 break;
2613 case ERESTARTNOHAND:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002614 /* pause(), rt_sigsuspend() etc use this code.
2615 * SA_RESTART is ignored (assumed not set):
2616 * syscall won't restart (will return EINTR instead)
Denys Vlasenko30c03232013-02-19 16:59:26 +01002617 * even after signal with SA_RESTART set. However,
2618 * after SIG_IGN or SIG_DFL signal it will restart
2619 * (thus the name "restart only if has no handler").
Denys Vlasenkofe585652012-01-12 11:26:34 +01002620 */
Denys Vlasenkoaba62922013-03-05 16:56:35 +01002621 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002622 break;
2623 case ERESTART_RESTARTBLOCK:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002624 /* Syscalls like nanosleep(), poll() which can't be
2625 * restarted with their original arguments use this
2626 * code. Kernel will execute restart_syscall() instead,
2627 * which changes arguments before restarting syscall.
2628 * SA_RESTART is ignored (assumed not set) similarly
2629 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2630 * since restart data is saved in "restart block"
2631 * in task struct, and if signal handler uses a syscall
2632 * which in turn saves another such restart block,
2633 * old data is lost and restart becomes impossible)
2634 */
2635 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002636 break;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002637 default:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002638 if (u_error < 0)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002639 tprintf("= -1 E??? (errno %ld)", u_error);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002640 else if (u_error < nerrnos)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002641 tprintf("= -1 %s (%s)", errnoent[u_error],
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002642 strerror(u_error));
2643 else
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002644 tprintf("= -1 ERRNO_%ld (%s)", u_error,
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002645 strerror(u_error));
2646 break;
2647 }
2648 if ((sys_res & RVAL_STR) && tcp->auxstr)
2649 tprintf(" (%s)", tcp->auxstr);
2650 }
2651 else {
2652 if (sys_res & RVAL_NONE)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002653 tprints("= ?");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002654 else {
2655 switch (sys_res & RVAL_MASK) {
2656 case RVAL_HEX:
2657 tprintf("= %#lx", tcp->u_rval);
2658 break;
2659 case RVAL_OCTAL:
2660 tprintf("= %#lo", tcp->u_rval);
2661 break;
2662 case RVAL_UDECIMAL:
2663 tprintf("= %lu", tcp->u_rval);
2664 break;
2665 case RVAL_DECIMAL:
2666 tprintf("= %ld", tcp->u_rval);
2667 break;
H.J. Ludd0130b2012-04-16 12:16:45 +02002668#if defined(LINUX_MIPSN32) || defined(X32)
2669 /*
2670 case RVAL_LHEX:
2671 tprintf("= %#llx", tcp->u_lrval);
2672 break;
2673 case RVAL_LOCTAL:
2674 tprintf("= %#llo", tcp->u_lrval);
2675 break;
2676 */
2677 case RVAL_LUDECIMAL:
2678 tprintf("= %llu", tcp->u_lrval);
2679 break;
2680 /*
2681 case RVAL_LDECIMAL:
2682 tprintf("= %lld", tcp->u_lrval);
2683 break;
2684 */
2685#endif
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002686 default:
2687 fprintf(stderr,
2688 "invalid rval format\n");
2689 break;
2690 }
2691 }
2692 if ((sys_res & RVAL_STR) && tcp->auxstr)
2693 tprintf(" (%s)", tcp->auxstr);
2694 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002695 if (Tflag) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002696 tv_sub(&tv, &tv, &tcp->etime);
2697 tprintf(" <%ld.%06ld>",
2698 (long) tv.tv_sec, (long) tv.tv_usec);
2699 }
Denys Vlasenko000b6012012-01-28 01:25:03 +01002700 tprints("\n");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002701 dumpio(tcp);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002702 line_ended();
2703
Denys Vlasenko3b738812011-08-22 02:06:35 +02002704 ret:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002705 tcp->flags &= ~TCB_INSYSCALL;
2706 return 0;
2707}
2708
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002709int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002710trace_syscall(struct tcb *tcp)
2711{
2712 return exiting(tcp) ?
2713 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2714}