blob: 56da0c0cd25d93f027c7351928e7763de3a551d3 [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>
Maarten ter Huurne40c174b2014-10-20 01:02:48 +020040#endif
41
42#ifdef HAVE_LINUX_PTRACE_H
Denys Vlasenko523635f2012-02-25 02:44:25 +010043# undef PTRACE_SYSCALL
Roland McGrathce9f0742004-03-01 21:29:22 +000044# ifdef HAVE_STRUCT_IA64_FPREG
45# define ia64_fpreg XXX_ia64_fpreg
46# endif
47# ifdef HAVE_STRUCT_PT_ALL_USER_REGS
48# define pt_all_user_regs XXX_pt_all_user_regs
49# endif
Ali Polatel0b4060f2013-09-24 20:04:32 +030050# ifdef HAVE_STRUCT_PTRACE_PEEKSIGINFO_ARGS
51# define ptrace_peeksiginfo_args XXX_ptrace_peeksiginfo_args
52# endif
Denys Vlasenko523635f2012-02-25 02:44:25 +010053# include <linux/ptrace.h>
Ali Polatel0b4060f2013-09-24 20:04:32 +030054# undef ptrace_peeksiginfo_args
Roland McGrathce9f0742004-03-01 21:29:22 +000055# undef ia64_fpreg
56# undef pt_all_user_regs
Wichert Akkerman15dea971999-10-06 13:06:34 +000057#endif
58
Denys Vlasenko84703742012-02-25 02:38:52 +010059#if defined(SPARC64)
Roland McGrath6d1a65c2004-07-12 07:44:08 +000060# undef PTRACE_GETREGS
61# define PTRACE_GETREGS PTRACE_GETREGS64
62# undef PTRACE_SETREGS
63# define PTRACE_SETREGS PTRACE_SETREGS64
Denys Vlasenko84703742012-02-25 02:38:52 +010064#endif
Roland McGrath6d1a65c2004-07-12 07:44:08 +000065
Denys Vlasenko84703742012-02-25 02:38:52 +010066#if defined(IA64)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000067# include <asm/ptrace_offsets.h>
68# include <asm/rse.h>
69#endif
70
Dmitry V. Levin32c049a2013-03-18 00:59:27 +000071/* for struct iovec */
72#include <sys/uio.h>
73/* for NT_PRSTATUS */
74#ifdef HAVE_ELF_H
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010075# include <elf.h>
76#endif
77
Steve McIntyred8d3bd32012-10-24 17:58:16 +010078#if defined(AARCH64)
79# include <asm/ptrace.h>
James Hogan5f999a82013-02-22 14:44:10 +000080#endif
81
Chris Zankel8f636ed2013-03-25 10:22:07 -070082#if defined(XTENSA)
83# include <asm/ptrace.h>
84#endif
85
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000086#ifndef NSIG
Denys Vlasenko523635f2012-02-25 02:44:25 +010087# warning: NSIG is not defined, using 32
88# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000089#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000090
91#include "syscall.h"
92
93/* Define these shorthand notations to simplify the syscallent files. */
Roland McGrath2fe7b132005-07-05 03:25:35 +000094#define TD TRACE_DESC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000095#define TF TRACE_FILE
96#define TI TRACE_IPC
97#define TN TRACE_NETWORK
98#define TP TRACE_PROCESS
99#define TS TRACE_SIGNAL
Namhyung Kim96792962012-10-24 11:41:57 +0900100#define TM TRACE_MEMORY
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000101#define NF SYSCALL_NEVER_FAILS
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200102#define MA MAX_ARGS
Masatake YAMATO1d78d222014-04-16 15:33:11 +0900103#define SI STACKTRACE_INVALIDATE_CACHE
104#define SE STACKTRACE_CAPTURE_ON_ENTER
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
Masatake YAMATO1d78d222014-04-16 15:33:11 +0900132#undef SI
133#undef SE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000134
Denys Vlasenko39fca622011-08-20 02:12:33 +0200135/*
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +0000136 * `ioctlent[012].h' files are automatically generated by the auxiliary
Denys Vlasenko39fca622011-08-20 02:12:33 +0200137 * program `ioctlsort', such that the list is sorted by the `code' field.
138 * This has the side-effect of resolving the _IO.. macros into
139 * plain integers, eliminating the need to include here everything
140 * in "/usr/include".
141 */
142
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100143const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000144#include "errnoent.h"
145};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100146const char *const signalent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200147#include "signalent.h"
148};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100149const struct_ioctlent ioctlent0[] = {
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +0000150#include "ioctlent0.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200151};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000152
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100153#if SUPPORTED_PERSONALITIES > 1
Roland McGrathee36ce12004-09-04 03:53:10 +0000154static const char *const errnoent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100155# include "errnoent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000156};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200157static const char *const signalent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100158# include "signalent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200159};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100160static const struct_ioctlent ioctlent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100161# include "ioctlent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200162};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200163#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000164
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100165#if SUPPORTED_PERSONALITIES > 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000166static const char *const errnoent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100167# include "errnoent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000168};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200169static const char *const signalent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100170# include "signalent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200171};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100172static const struct_ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100173# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200174};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200175#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000176
Dmitry V. Levine6f55242013-02-26 22:02:30 +0000177enum {
178 nsyscalls0 = ARRAY_SIZE(sysent0)
179#if SUPPORTED_PERSONALITIES > 1
180 , nsyscalls1 = ARRAY_SIZE(sysent1)
181# if SUPPORTED_PERSONALITIES > 2
182 , nsyscalls2 = ARRAY_SIZE(sysent2)
183# endif
184#endif
185};
186
187enum {
188 nerrnos0 = ARRAY_SIZE(errnoent0)
189#if SUPPORTED_PERSONALITIES > 1
190 , nerrnos1 = ARRAY_SIZE(errnoent1)
191# if SUPPORTED_PERSONALITIES > 2
192 , nerrnos2 = ARRAY_SIZE(errnoent2)
193# endif
194#endif
195};
196
197enum {
198 nsignals0 = ARRAY_SIZE(signalent0)
199#if SUPPORTED_PERSONALITIES > 1
200 , nsignals1 = ARRAY_SIZE(signalent1)
201# if SUPPORTED_PERSONALITIES > 2
202 , nsignals2 = ARRAY_SIZE(signalent2)
203# endif
204#endif
205};
206
207enum {
208 nioctlents0 = ARRAY_SIZE(ioctlent0)
209#if SUPPORTED_PERSONALITIES > 1
210 , nioctlents1 = ARRAY_SIZE(ioctlent1)
211# if SUPPORTED_PERSONALITIES > 2
212 , nioctlents2 = ARRAY_SIZE(ioctlent2)
213# endif
214#endif
215};
216
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100217#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100218const struct_sysent *sysent = sysent0;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100219const char *const *errnoent = errnoent0;
220const char *const *signalent = signalent0;
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100221const struct_ioctlent *ioctlent = ioctlent0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100222#endif
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100223unsigned nsyscalls = nsyscalls0;
224unsigned nerrnos = nerrnos0;
225unsigned nsignals = nsignals0;
226unsigned nioctlents = nioctlents0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100227
228unsigned num_quals;
229qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
230
231static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
232 nsyscalls0,
233#if SUPPORTED_PERSONALITIES > 1
234 nsyscalls1,
235#endif
236#if SUPPORTED_PERSONALITIES > 2
237 nsyscalls2,
238#endif
239};
240static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
241 sysent0,
242#if SUPPORTED_PERSONALITIES > 1
243 sysent1,
244#endif
245#if SUPPORTED_PERSONALITIES > 2
246 sysent2,
247#endif
248};
249
250enum {
251 MAX_NSYSCALLS1 = (nsyscalls0
252#if SUPPORTED_PERSONALITIES > 1
253 > nsyscalls1 ? nsyscalls0 : nsyscalls1
254#endif
255 ),
256 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
257#if SUPPORTED_PERSONALITIES > 2
258 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
259#endif
260 ),
261 MAX_NSYSCALLS = MAX_NSYSCALLS2,
262 /* We are ready for arches with up to 255 signals,
263 * even though the largest known signo is on MIPS and it is 128.
264 * The number of existing syscalls on all arches is
265 * larger that 255 anyway, so it is just a pedantic matter.
266 */
267 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
268};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000269
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100270#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100271unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000272
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100273# ifndef current_wordsize
274unsigned current_wordsize;
275static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000276 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000277 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100278# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000279 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100280# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200281};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100282# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000283
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200284void
Dmitry V. Levin3abe8b22006-12-20 22:37:21 +0000285set_personality(int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000286{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100287 nsyscalls = nsyscall_vec[personality];
288 sysent = sysent_vec[personality];
289
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000290 switch (personality) {
291 case 0:
292 errnoent = errnoent0;
293 nerrnos = nerrnos0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000294 ioctlent = ioctlent0;
295 nioctlents = nioctlents0;
296 signalent = signalent0;
297 nsignals = nsignals0;
298 break;
299
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000300 case 1:
301 errnoent = errnoent1;
302 nerrnos = nerrnos1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000303 ioctlent = ioctlent1;
304 nioctlents = nioctlents1;
305 signalent = signalent1;
306 nsignals = nsignals1;
307 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000308
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100309# if SUPPORTED_PERSONALITIES > 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000310 case 2:
311 errnoent = errnoent2;
312 nerrnos = nerrnos2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000313 ioctlent = ioctlent2;
314 nioctlents = nioctlents2;
315 signalent = signalent2;
316 nsignals = nsignals2;
317 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100318# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000319 }
320
321 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100322# ifndef current_wordsize
323 current_wordsize = personality_wordsize[personality];
324# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000325}
326
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000327static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000328update_personality(struct tcb *tcp, unsigned int personality)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000329{
330 if (personality == current_personality)
331 return;
332 set_personality(personality);
333
334 if (personality == tcp->currpers)
335 return;
336 tcp->currpers = personality;
337
H.J. Lu35be5812012-04-16 13:00:01 +0200338# if defined(POWERPC64)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000339 if (!qflag) {
340 static const char *const names[] = {"64 bit", "32 bit"};
341 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
342 tcp->pid, names[personality]);
343 }
H.J. Lu35be5812012-04-16 13:00:01 +0200344# elif defined(X86_64)
345 if (!qflag) {
346 static const char *const names[] = {"64 bit", "32 bit", "x32"};
347 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
348 tcp->pid, names[personality]);
349 }
H.J. Lu085e4282012-04-17 11:05:04 -0700350# elif defined(X32)
351 if (!qflag) {
352 static const char *const names[] = {"x32", "32 bit"};
353 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
354 tcp->pid, names[personality]);
355 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000356# elif defined(AARCH64)
357 if (!qflag) {
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100358 static const char *const names[] = {"32-bit", "AArch64"};
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000359 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
360 tcp->pid, names[personality]);
361 }
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100362# elif defined(TILE)
363 if (!qflag) {
364 static const char *const names[] = {"64-bit", "32-bit"};
365 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
366 tcp->pid, names[personality]);
367 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100368# endif
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000369}
370#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000371
Denys Vlasenkoc1540fe2013-02-21 16:17:08 +0100372static int qual_syscall(), qual_signal(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000373
Roland McGrathe10e62a2004-09-04 04:20:43 +0000374static const struct qual_options {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000375 unsigned int bitflag;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000376 const char *option_name;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000377 int (*qualify)(const char *, int, int);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000378 const char *argument_name;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000379} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000380 { QUAL_TRACE, "trace", qual_syscall, "system call" },
381 { QUAL_TRACE, "t", qual_syscall, "system call" },
382 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
383 { QUAL_ABBREV, "a", qual_syscall, "system call" },
384 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
385 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
386 { QUAL_RAW, "raw", qual_syscall, "system call" },
387 { QUAL_RAW, "x", qual_syscall, "system call" },
388 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
389 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
390 { QUAL_SIGNAL, "s", qual_signal, "signal" },
Roland McGrath9797ceb2002-12-30 10:23:00 +0000391 { QUAL_READ, "read", qual_desc, "descriptor" },
392 { QUAL_READ, "reads", qual_desc, "descriptor" },
393 { QUAL_READ, "r", qual_desc, "descriptor" },
394 { QUAL_WRITE, "write", qual_desc, "descriptor" },
395 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
396 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000397 { 0, NULL, NULL, NULL },
398};
399
Roland McGrath9797ceb2002-12-30 10:23:00 +0000400static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000401reallocate_qual(const unsigned int n)
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100402{
403 unsigned p;
404 qualbits_t *qp;
405 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
406 qp = qual_vec[p] = realloc(qual_vec[p], n * sizeof(qualbits_t));
407 if (!qp)
408 die_out_of_memory();
409 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
410 }
411 num_quals = n;
412}
413
414static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000415qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000416{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000417 int p;
Roland McGrath138c6a32006-01-12 09:50:49 +0000418
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100419 if (num_quals <= n)
420 reallocate_qual(n + 1);
Roland McGrath138c6a32006-01-12 09:50:49 +0000421
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100422 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
423 if (pers == p || pers < 0) {
424 if (not)
425 qual_vec[p][n] &= ~bitflag;
426 else
427 qual_vec[p][n] |= bitflag;
428 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000429 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000430}
431
432static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000433qual_syscall(const char *s, const unsigned int bitflag, const int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000434{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000435 int p;
436 unsigned int i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000437 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000438
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100439 if (*s >= '0' && *s <= '9') {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100440 i = string_to_uint(s);
Denys Vlasenkob43dacd2013-02-23 18:19:28 +0100441 if (i >= MAX_NSYSCALLS)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000442 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000443 qualify_one(i, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000444 return 0;
Roland McGrath48a035f2006-01-12 09:45:56 +0000445 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000446
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100447 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
448 for (i = 0; i < nsyscall_vec[p]; i++) {
449 if (sysent_vec[p][i].sys_name
450 && strcmp(s, sysent_vec[p][i].sys_name) == 0
451 ) {
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000452 qualify_one(i, bitflag, not, p);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100453 rc = 0;
454 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000455 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100456 }
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000457
Roland McGrathfe6b3522005-02-02 04:40:11 +0000458 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000459}
460
461static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000462qual_signal(const char *s, const unsigned int bitflag, const int not)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000463{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000464 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000465
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100466 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000467 int signo = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100468 if (signo < 0 || signo > 255)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000469 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000470 qualify_one(signo, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000471 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000472 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000473 if (strncasecmp(s, "SIG", 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000474 s += 3;
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100475 for (i = 0; i <= NSIG; i++) {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000476 if (strcasecmp(s, signame(i) + 3) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000477 qualify_one(i, bitflag, not, -1);
Roland McGrath76421df2005-02-02 03:51:18 +0000478 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000479 }
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100480 }
Roland McGrath76421df2005-02-02 03:51:18 +0000481 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000482}
483
484static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000485qual_desc(const char *s, const unsigned int bitflag, const int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000486{
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100487 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000488 int desc = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100489 if (desc < 0 || desc > 0x7fff) /* paranoia */
Roland McGrathfe6b3522005-02-02 04:40:11 +0000490 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000491 qualify_one(desc, bitflag, not, -1);
Roland McGrath2b619022003-04-10 18:58:20 +0000492 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000493 }
494 return -1;
495}
496
497static int
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000498lookup_class(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000499{
500 if (strcmp(s, "file") == 0)
501 return TRACE_FILE;
502 if (strcmp(s, "ipc") == 0)
503 return TRACE_IPC;
504 if (strcmp(s, "network") == 0)
505 return TRACE_NETWORK;
506 if (strcmp(s, "process") == 0)
507 return TRACE_PROCESS;
508 if (strcmp(s, "signal") == 0)
509 return TRACE_SIGNAL;
Roland McGrath2fe7b132005-07-05 03:25:35 +0000510 if (strcmp(s, "desc") == 0)
511 return TRACE_DESC;
Namhyung Kim96792962012-10-24 11:41:57 +0900512 if (strcmp(s, "memory") == 0)
513 return TRACE_MEMORY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000514 return -1;
515}
516
517void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000518qualify(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000519{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000520 const struct qual_options *opt;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000521 char *copy;
522 const char *p;
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000523 int not;
524 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000525
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100526 if (num_quals == 0)
527 reallocate_qual(MIN_QUALS);
528
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000529 opt = &qual_options[0];
530 for (i = 0; (p = qual_options[i].option_name); i++) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000531 unsigned int len = strlen(p);
532 if (strncmp(s, p, len) == 0 && s[len] == '=') {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000533 opt = &qual_options[i];
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000534 s += len + 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000535 break;
536 }
537 }
538 not = 0;
539 if (*s == '!') {
540 not = 1;
541 s++;
542 }
543 if (strcmp(s, "none") == 0) {
544 not = 1 - not;
545 s = "all";
546 }
547 if (strcmp(s, "all") == 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100548 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000549 qualify_one(i, opt->bitflag, not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000550 }
551 return;
552 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100553 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000554 qualify_one(i, opt->bitflag, !not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000555 }
Denys Vlasenko5d645812011-08-20 12:48:18 +0200556 copy = strdup(s);
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200557 if (!copy)
558 die_out_of_memory();
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000559 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000560 int n;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000561 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100562 unsigned pers;
563 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
564 for (i = 0; i < nsyscall_vec[pers]; i++)
565 if (sysent_vec[pers][i].sys_flags & n)
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000566 qualify_one(i, opt->bitflag, not, pers);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100567 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000568 continue;
569 }
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000570 if (opt->qualify(p, opt->bitflag, not)) {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100571 error_msg_and_die("invalid %s '%s'",
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000572 opt->argument_name, p);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000573 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000574 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000575 free(copy);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000576 return;
577}
578
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000579#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000580static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000581decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000582{
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000583 unsigned long addr;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100584 unsigned int i, n, size;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000585
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000586 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
587 return;
588
589 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100590 tcp->qual_flg = qual_flags[tcp->scno];
591 tcp->s_ent = &sysent[tcp->scno];
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000592 addr = tcp->u_arg[1];
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100593 size = current_wordsize;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100594 n = tcp->s_ent->nargs;
595 for (i = 0; i < n; ++i) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000596 if (size == sizeof(int)) {
597 unsigned int arg;
598 if (umove(tcp, addr, &arg) < 0)
599 arg = 0;
600 tcp->u_arg[i] = arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000601 }
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000602 else {
603 unsigned long arg;
604 if (umove(tcp, addr, &arg) < 0)
605 arg = 0;
606 tcp->u_arg[i] = arg;
607 }
608 addr += size;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000609 }
610}
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000611#endif
Mike Frysinger3362e892012-03-15 01:09:19 -0400612
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000613#ifdef SYS_ipc_subcall
614static void
615decode_ipc_subcall(struct tcb *tcp)
616{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100617 unsigned int i, n;
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000618
619 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
620 return;
621
622 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100623 tcp->qual_flg = qual_flags[tcp->scno];
624 tcp->s_ent = &sysent[tcp->scno];
625 n = tcp->s_ent->nargs;
626 for (i = 0; i < n; i++)
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000627 tcp->u_arg[i] = tcp->u_arg[i + 1];
628}
629#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000630
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200631int
632printargs(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000633{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200634 if (entering(tcp)) {
635 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100636 int n = tcp->s_ent->nargs;
637 for (i = 0; i < n; i++)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200638 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
639 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000640 return 0;
641}
642
Denys Vlasenko72879c62012-02-27 14:18:02 +0100643int
644printargs_lu(struct tcb *tcp)
645{
646 if (entering(tcp)) {
647 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100648 int n = tcp->s_ent->nargs;
649 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100650 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
651 }
652 return 0;
653}
654
655int
656printargs_ld(struct tcb *tcp)
657{
658 if (entering(tcp)) {
659 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100660 int n = tcp->s_ent->nargs;
661 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100662 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
663 }
664 return 0;
665}
666
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100667#if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200668long
669getrval2(struct tcb *tcp)
670{
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100671 long val;
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200672
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100673# if defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100674 val = sparc_regs.u_regs[U_REG_O1];
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100675# elif defined(SH)
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200676 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200677 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100678# elif defined(IA64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200679 if (upeek(tcp->pid, PT_R9, &val) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200680 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100681# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200682
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200683 return val;
684}
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100685#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200686
Denys Vlasenko523635f2012-02-25 02:44:25 +0100687#if defined(I386)
Denys Vlasenkob51f3642013-07-16 12:06:25 +0200688static struct user_regs_struct i386_regs;
Denys Vlasenkoe600ac62013-07-18 17:40:45 +0200689/* Cast suppresses signedness warning (.esp is long, not unsigned long) */
690uint32_t *const i386_esp_ptr = (uint32_t*)&i386_regs.esp;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000691# define ARCH_REGS_FOR_GETREGSET i386_regs
H.J. Lu35be5812012-04-16 13:00:01 +0200692#elif defined(X86_64) || defined(X32)
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100693/*
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100694 * On i386, pt_regs and user_regs_struct are the same,
695 * but on 64 bit x86, user_regs_struct has six more fields:
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100696 * fs_base, gs_base, ds, es, fs, gs.
697 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
698 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100699struct i386_user_regs_struct {
700 uint32_t ebx;
701 uint32_t ecx;
702 uint32_t edx;
703 uint32_t esi;
704 uint32_t edi;
705 uint32_t ebp;
706 uint32_t eax;
707 uint32_t xds;
708 uint32_t xes;
709 uint32_t xfs;
710 uint32_t xgs;
711 uint32_t orig_eax;
712 uint32_t eip;
713 uint32_t xcs;
714 uint32_t eflags;
715 uint32_t esp;
716 uint32_t xss;
717};
718static union {
719 struct user_regs_struct x86_64_r;
720 struct i386_user_regs_struct i386_r;
721} x86_regs_union;
722# define x86_64_regs x86_regs_union.x86_64_r
723# define i386_regs x86_regs_union.i386_r
Denys Vlasenkob51f3642013-07-16 12:06:25 +0200724uint32_t *const i386_esp_ptr = &i386_regs.esp;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100725static struct iovec x86_io = {
726 .iov_base = &x86_regs_union
727};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100728#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +0200729bool ia64_ia32mode = 0; /* not static */
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100730static long ia64_r8, ia64_r10;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100731#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +0200732struct pt_regs ppc_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100733#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100734static long m68k_d0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100735#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100736static long bfin_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100737#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100738struct pt_regs arm_regs; /* not static */
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000739# define ARCH_REGS_FOR_GETREGSET arm_regs
Steve McIntyred8d3bd32012-10-24 17:58:16 +0100740#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100741static union {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100742 struct user_pt_regs aarch64_r;
743 struct arm_pt_regs arm_r;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100744} arm_regs_union;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100745# define aarch64_regs arm_regs_union.aarch64_r
746# define arm_regs arm_regs_union.arm_r
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100747static struct iovec aarch64_io = {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100748 .iov_base = &arm_regs_union
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100749};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100750#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100751static long alpha_r0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100752static long alpha_a3;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100753#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100754static struct pt_regs avr32_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100755#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100756struct pt_regs sparc_regs; /* not static */
Denys Vlasenko523635f2012-02-25 02:44:25 +0100757#elif defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100758static long long mips_a3;
759static long long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100760#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100761static long mips_a3;
762static long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100763#elif defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +0200764static long s390_gpr2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100765#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100766static long hppa_r28;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100767#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100768static long sh_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100769#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100770static long sh64_r9;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100771#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100772static long cris_r10;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100773#elif defined(TILE)
774struct pt_regs tile_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100775#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100776static long microblaze_r3;
Christian Svensson492f81f2013-02-14 13:26:27 +0100777#elif defined(OR1K)
778static struct user_regs_struct or1k_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000779# define ARCH_REGS_FOR_GETREGSET or1k_regs
James Hogan5f999a82013-02-22 14:44:10 +0000780#elif defined(METAG)
781static struct user_gp_regs metag_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000782# define ARCH_REGS_FOR_GETREGSET metag_regs
Chris Zankel8f636ed2013-03-25 10:22:07 -0700783#elif defined(XTENSA)
784static long xtensa_a2;
Vineet Gupta7daacbb2013-08-16 12:47:06 +0530785# elif defined(ARC)
786static struct user_regs_struct arc_regs;
787# define ARCH_REGS_FOR_GETREGSET arc_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +0100788#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000789
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100790void
Denys Vlasenko5a2483b2013-07-01 12:49:14 +0200791print_pc(struct tcb *tcp)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100792{
793#define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
794 sizeof(long) == 8 ? "[????????????????] " : \
795 NULL /* crash */)
796 if (get_regs_error) {
797 PRINTBADPC;
798 return;
799 }
800#if defined(I386)
801 tprintf("[%08lx] ", i386_regs.eip);
802#elif defined(S390) || defined(S390X)
803 long psw;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200804 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100805 PRINTBADPC;
806 return;
807 }
808# ifdef S390
809 tprintf("[%08lx] ", psw);
810# elif S390X
Dmitry V. Levinddba73e2013-02-05 19:01:58 +0000811 tprintf("[%016lx] ", psw);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100812# endif
813#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100814 if (x86_io.iov_len == sizeof(i386_regs)) {
815 tprintf("[%08x] ", (unsigned) i386_regs.eip);
816 } else {
817# if defined(X86_64)
818 tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
819# elif defined(X32)
820 /* Note: this truncates 64-bit rip to 32 bits */
821 tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip);
822# endif
823 }
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100824#elif defined(IA64)
825 long ip;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200826 if (upeek(tcp->pid, PT_B0, &ip) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100827 PRINTBADPC;
828 return;
829 }
830 tprintf("[%08lx] ", ip);
831#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +0200832 long pc = ppc_regs.nip;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100833# ifdef POWERPC64
834 tprintf("[%016lx] ", pc);
835# else
836 tprintf("[%08lx] ", pc);
837# endif
838#elif defined(M68K)
839 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200840 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100841 tprints("[????????] ");
842 return;
843 }
844 tprintf("[%08lx] ", pc);
845#elif defined(ALPHA)
846 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200847 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100848 tprints("[????????????????] ");
849 return;
850 }
851 tprintf("[%08lx] ", pc);
852#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100853 tprintf("[%08lx] ", sparc_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100854#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100855 tprintf("[%08lx] ", sparc_regs.tpc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100856#elif defined(HPPA)
857 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200858 if (upeek(tcp->pid, PT_IAOQ0, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100859 tprints("[????????] ");
860 return;
861 }
862 tprintf("[%08lx] ", pc);
863#elif defined(MIPS)
864 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200865 if (upeek(tcp->pid, REG_EPC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100866 tprints("[????????] ");
867 return;
868 }
869 tprintf("[%08lx] ", pc);
870#elif defined(SH)
871 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200872 if (upeek(tcp->pid, 4*REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100873 tprints("[????????] ");
874 return;
875 }
876 tprintf("[%08lx] ", pc);
877#elif defined(SH64)
878 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200879 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100880 tprints("[????????????????] ");
881 return;
882 }
883 tprintf("[%08lx] ", pc);
884#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100885 tprintf("[%08lx] ", arm_regs.ARM_pc);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100886#elif defined(AARCH64)
887 /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100888#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100889 tprintf("[%08lx] ", avr32_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100890#elif defined(BFIN)
891 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200892 if (upeek(tcp->pid, PT_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100893 PRINTBADPC;
894 return;
895 }
896 tprintf("[%08lx] ", pc);
897#elif defined(CRISV10)
898 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200899 if (upeek(tcp->pid, 4*PT_IRP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100900 PRINTBADPC;
901 return;
902 }
903 tprintf("[%08lx] ", pc);
904#elif defined(CRISV32)
905 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200906 if (upeek(tcp->pid, 4*PT_ERP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100907 PRINTBADPC;
908 return;
909 }
910 tprintf("[%08lx] ", pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100911#elif defined(TILE)
912# ifdef _LP64
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500913 tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100914# else
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500915 tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100916# endif
Christian Svensson492f81f2013-02-14 13:26:27 +0100917#elif defined(OR1K)
918 tprintf("[%08lx] ", or1k_regs.pc);
James Hogan5f999a82013-02-22 14:44:10 +0000919#elif defined(METAG)
920 tprintf("[%08lx] ", metag_regs.pc);
Chris Zankel8f636ed2013-03-25 10:22:07 -0700921#elif defined(XTENSA)
922 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200923 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Chris Zankel8f636ed2013-03-25 10:22:07 -0700924 PRINTBADPC;
925 return;
926 }
927 tprintf("[%08lx] ", pc);
Vineet Gupta7daacbb2013-08-16 12:47:06 +0530928#elif defined(ARC)
929 tprintf("[%08lx] ", arc_regs.efa);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100930#endif /* architecture */
931}
932
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000933/*
934 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
935 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
Denys Vlasenko7270de52013-02-21 15:46:34 +0100936 */
Elliott Hughes7a28f7f2014-03-03 23:45:26 +0000937#if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
Denys Vlasenko7270de52013-02-21 15:46:34 +0100938static long
939shuffle_scno(unsigned long scno)
940{
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000941 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
Denys Vlasenko7270de52013-02-21 15:46:34 +0100942 return scno;
943
944 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000945 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
Denys Vlasenko7270de52013-02-21 15:46:34 +0100946 return 0x000ffff0;
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000947 if (scno == 0x000ffff0)
948 return ARM_FIRST_SHUFFLED_SYSCALL;
Denys Vlasenko7270de52013-02-21 15:46:34 +0100949
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000950#define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
951 /*
952 * Is it ARM specific syscall?
953 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
954 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
Denys Vlasenko7270de52013-02-21 15:46:34 +0100955 */
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000956 if (scno >= 0x000f0000 &&
957 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
958 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
Denys Vlasenko7270de52013-02-21 15:46:34 +0100959 }
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000960 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
961 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
Denys Vlasenko7270de52013-02-21 15:46:34 +0100962 }
963
964 return scno;
965}
966#else
967# define shuffle_scno(scno) ((long)(scno))
968#endif
969
970static char*
971undefined_scno_name(struct tcb *tcp)
972{
973 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
974
975 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
976 return buf;
977}
978
Anton Blanchard14d51a62013-06-26 14:42:37 +1000979#ifdef POWERPC
980/*
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200981 * PTRACE_GETREGS was added to the PowerPC kernel in v2.6.23,
982 * we provide a slow fallback for old kernels.
Anton Blanchard14d51a62013-06-26 14:42:37 +1000983 */
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200984static int powerpc_getregs_old(pid_t pid)
Anton Blanchard14d51a62013-06-26 14:42:37 +1000985{
986 int i;
987 long r;
988
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200989 if (iflag) {
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200990 r = upeek(pid, sizeof(long) * PT_NIP, (long *)&ppc_regs.nip);
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200991 if (r)
992 goto out;
993 }
994#ifdef POWERPC64 /* else we never use it */
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200995 r = upeek(pid, sizeof(long) * PT_MSR, (long *)&ppc_regs.msr);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000996 if (r)
997 goto out;
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200998#endif
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200999 r = upeek(pid, sizeof(long) * PT_CCR, (long *)&ppc_regs.ccr);
Anton Blanchard14d51a62013-06-26 14:42:37 +10001000 if (r)
1001 goto out;
Anton Blanchard9459dfb2013-07-12 12:24:02 +02001002 r = upeek(pid, sizeof(long) * PT_ORIG_R3, (long *)&ppc_regs.orig_gpr3);
Anton Blanchard14d51a62013-06-26 14:42:37 +10001003 if (r)
1004 goto out;
Anton Blanchard14d51a62013-06-26 14:42:37 +10001005 for (i = 0; i <= 8; i++) {
Anton Blanchard9459dfb2013-07-12 12:24:02 +02001006 r = upeek(pid, sizeof(long) * (PT_R0 + i),
1007 (long *)&ppc_regs.gpr[i]);
Anton Blanchard14d51a62013-06-26 14:42:37 +10001008 if (r)
1009 goto out;
1010 }
Denys Vlasenko7f5a1322013-06-28 14:36:39 +02001011 out:
Anton Blanchard14d51a62013-06-26 14:42:37 +10001012 return r;
1013}
1014#endif
1015
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001016#ifndef get_regs
1017long get_regs_error;
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001018
1019#if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001020static void get_regset(pid_t pid)
1021{
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001022/* constant iovec */
1023# if defined(ARM) \
1024 || defined(I386) \
1025 || defined(METAG) \
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301026 || defined(OR1K) \
1027 || defined(ARC)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001028 static struct iovec io = {
1029 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1030 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001031 };
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001032 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1033
1034/* variable iovec */
1035# elif defined(X86_64) || defined(X32)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001036 /* x86_io.iov_base = &x86_regs_union; - already is */
1037 x86_io.iov_len = sizeof(x86_regs_union);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001038 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &x86_io);
1039# elif defined(AARCH64)
1040 /* aarch64_io.iov_base = &arm_regs_union; - already is */
1041 aarch64_io.iov_len = sizeof(arm_regs_union);
1042 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &aarch64_io);
1043# else
1044# warning both PTRACE_GETREGSET and NT_PRSTATUS are available but not yet used
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001045# endif
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001046}
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001047#endif /* PTRACE_GETREGSET && NT_PRSTATUS */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001048
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001049void
1050get_regs(pid_t pid)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001051{
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001052/* PTRACE_GETREGSET only */
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301053# if defined(METAG) || defined(OR1K) || defined(X32) || defined(AARCH64) || defined(ARC)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001054 get_regset(pid);
1055
1056/* PTRACE_GETREGS only */
1057# elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001058 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001059# elif defined(TILE)
1060 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &tile_regs);
1061# elif defined(SPARC) || defined(SPARC64)
1062 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001063# elif defined(POWERPC)
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001064 static bool old_kernel = 0;
1065 if (old_kernel)
1066 goto old;
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001067 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &ppc_regs);
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001068 if (get_regs_error && errno == EIO) {
1069 old_kernel = 1;
1070 old:
Anton Blanchard14d51a62013-06-26 14:42:37 +10001071 get_regs_error = powerpc_getregs_old(pid);
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001072 }
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001073
1074/* try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS */
1075# else
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001076# if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
1077 static int getregset_support;
1078
1079 if (getregset_support >= 0) {
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001080 get_regset(pid);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001081 if (getregset_support > 0)
1082 return;
1083 if (get_regs_error >= 0) {
1084 getregset_support = 1;
1085 return;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001086 }
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001087 if (errno == EPERM || errno == ESRCH)
1088 return;
1089 getregset_support = -1;
1090 }
1091# endif /* PTRACE_GETREGSET && NT_PRSTATUS */
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001092# if defined(ARM)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001093 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &arm_regs);
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001094# elif defined(I386)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001095 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &i386_regs);
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001096# elif defined(X86_64)
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001097 /* Use old method, with unreliable heuristical detection of 32-bitness. */
1098 x86_io.iov_len = sizeof(x86_64_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001099 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &x86_64_regs);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001100 if (!get_regs_error && x86_64_regs.cs == 0x23) {
1101 x86_io.iov_len = sizeof(i386_regs);
1102 /*
1103 * The order is important: i386_regs and x86_64_regs
1104 * are overlaid in memory!
1105 */
1106 i386_regs.ebx = x86_64_regs.rbx;
1107 i386_regs.ecx = x86_64_regs.rcx;
1108 i386_regs.edx = x86_64_regs.rdx;
1109 i386_regs.esi = x86_64_regs.rsi;
1110 i386_regs.edi = x86_64_regs.rdi;
1111 i386_regs.ebp = x86_64_regs.rbp;
1112 i386_regs.eax = x86_64_regs.rax;
1113 /* i386_regs.xds = x86_64_regs.ds; unused by strace */
1114 /* i386_regs.xes = x86_64_regs.es; ditto... */
1115 /* i386_regs.xfs = x86_64_regs.fs; */
1116 /* i386_regs.xgs = x86_64_regs.gs; */
1117 i386_regs.orig_eax = x86_64_regs.orig_rax;
1118 i386_regs.eip = x86_64_regs.rip;
1119 /* i386_regs.xcs = x86_64_regs.cs; */
1120 /* i386_regs.eflags = x86_64_regs.eflags; */
1121 i386_regs.esp = x86_64_regs.rsp;
1122 /* i386_regs.xss = x86_64_regs.ss; */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001123 }
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001124# else
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001125# error unhandled architecture
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001126# endif /* ARM || I386 || X86_64 */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001127# endif
1128}
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001129#endif /* !get_regs */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001130
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001131/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001132 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1133 * 1: ok, continue in trace_syscall_entering().
1134 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001135 * ("????" etc) and bail out.
1136 */
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01001137static int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001138get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001139{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001140 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001141
Denys Vlasenko523635f2012-02-25 02:44:25 +01001142#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001143 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001144 return -1;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001145
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001146 if (s390_gpr2 != -ENOSYS) {
Roland McGrath2f924ca2003-06-26 22:23:28 +00001147 /*
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001148 * Since kernel version 2.5.44 the scno gets passed in gpr2.
Roland McGrath2f924ca2003-06-26 22:23:28 +00001149 */
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001150 scno = s390_gpr2;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001151 } else {
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001152 /*
Michal Ludvig882eda82002-11-11 12:50:47 +00001153 * Old style of "passing" the scno via the SVC instruction.
1154 */
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001155 long psw;
Michal Ludvig882eda82002-11-11 12:50:47 +00001156 long opcode, offset_reg, tmp;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001157 void *svc_addr;
Denys Vlasenko7c9ba8b2011-08-19 19:46:32 +02001158 static const int gpr_offset[16] = {
1159 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
1160 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
1161 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
1162 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
1163 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001164
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001165 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0)
Michal Ludvig882eda82002-11-11 12:50:47 +00001166 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001167 errno = 0;
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001168 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +00001169 if (errno) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001170 perror_msg("peektext(psw-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +00001171 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001172 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001173
1174 /*
1175 * We have to check if the SVC got executed directly or via an
1176 * EXECUTE instruction. In case of EXECUTE it is necessary to do
1177 * instruction decoding to derive the system call number.
1178 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1179 * so that this doesn't work if a SVC opcode is part of an EXECUTE
1180 * opcode. Since there is no way to find out the opcode size this
1181 * is the best we can do...
1182 */
Michal Ludvig882eda82002-11-11 12:50:47 +00001183 if ((opcode & 0xff00) == 0x0a00) {
1184 /* SVC opcode */
1185 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +00001186 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001187 else {
1188 /* SVC got executed by EXECUTE instruction */
1189
1190 /*
1191 * Do instruction decoding of EXECUTE. If you really want to
1192 * understand this, read the Principles of Operations.
1193 */
1194 svc_addr = (void *) (opcode & 0xfff);
1195
1196 tmp = 0;
1197 offset_reg = (opcode & 0x000f0000) >> 16;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001198 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001199 return -1;
1200 svc_addr += tmp;
1201
1202 tmp = 0;
1203 offset_reg = (opcode & 0x0000f000) >> 12;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001204 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001205 return -1;
1206 svc_addr += tmp;
1207
Denys Vlasenkofb036672009-01-23 16:30:26 +00001208 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
Michal Ludvig882eda82002-11-11 12:50:47 +00001209 if (errno)
1210 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001211# if defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +00001212 scno >>= 48;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001213# else
Michal Ludvig882eda82002-11-11 12:50:47 +00001214 scno >>= 16;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001215# endif
Michal Ludvig882eda82002-11-11 12:50:47 +00001216 tmp = 0;
1217 offset_reg = (opcode & 0x00f00000) >> 20;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001218 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001219 return -1;
1220
1221 scno = (scno | tmp) & 0xff;
1222 }
1223 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001224#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001225 scno = ppc_regs.gpr[0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001226# ifdef POWERPC64
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001227 unsigned int currpers;
Andreas Schwabd69fa492010-07-12 21:39:57 +02001228
James Yanga78d7772014-02-18 15:32:43 -06001229 /*
1230 * Check for 64/32 bit mode.
1231 * Embedded implementations covered by Book E extension of PPC use
1232 * bit 0 (CM) of 32-bit Machine state register (MSR).
1233 * Other implementations use bit 0 (SF) of 64-bit MSR.
1234 */
1235 currpers = (ppc_regs.msr & 0x8000000080000000) ? 0 : 1;
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001236 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001237# endif
1238#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001239 scno = avr32_regs.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001240#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001241 if (upeek(tcp->pid, PT_ORIG_P0, &scno))
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001242 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001243#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001244 scno = i386_regs.orig_eax;
H.J. Lu35be5812012-04-16 13:00:01 +02001245#elif defined(X86_64) || defined(X32)
1246# ifndef __X32_SYSCALL_BIT
1247# define __X32_SYSCALL_BIT 0x40000000
1248# endif
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001249 unsigned int currpers;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001250# if 1
1251 /* GETREGSET of NT_PRSTATUS tells us regset size,
1252 * which unambiguously detects i386.
1253 *
1254 * Linux kernel distinguishes x86-64 and x32 processes
1255 * solely by looking at __X32_SYSCALL_BIT:
1256 * arch/x86/include/asm/compat.h::is_x32_task():
1257 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1258 * return true;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001259 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001260 if (x86_io.iov_len == sizeof(i386_regs)) {
1261 scno = i386_regs.orig_eax;
1262 currpers = 1;
1263 } else {
1264 scno = x86_64_regs.orig_rax;
1265 currpers = 0;
1266 if (scno & __X32_SYSCALL_BIT) {
Dmitry V. Levinc3948322015-02-05 16:15:46 +00001267 /*
1268 * Syscall number -1 requires special treatment:
1269 * it might be a side effect of SECCOMP_RET_ERRNO
1270 * filtering that sets orig_rax to -1
1271 * in some versions of linux kernel.
1272 * If that is the case, then
1273 * __X32_SYSCALL_BIT logic does not apply.
1274 */
1275 if ((long long) x86_64_regs.orig_rax != -1) {
1276 scno -= __X32_SYSCALL_BIT;
1277 currpers = 2;
1278 } else {
1279# ifdef X32
1280 currpers = 2;
1281# endif
1282 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001283 }
1284 }
1285# elif 0
1286 /* cs = 0x33 for long mode (native 64 bit and x32)
1287 * cs = 0x23 for compatibility mode (32 bit)
1288 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1289 */
1290 scno = x86_64_regs.orig_rax;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001291 switch (x86_64_regs.cs) {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001292 case 0x23: currpers = 1; break;
H.J. Lu35be5812012-04-16 13:00:01 +02001293 case 0x33:
1294 if (x86_64_regs.ds == 0x2b) {
1295 currpers = 2;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001296 scno &= ~__X32_SYSCALL_BIT;
H.J. Lu35be5812012-04-16 13:00:01 +02001297 } else
1298 currpers = 0;
1299 break;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001300 default:
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001301 fprintf(stderr, "Unknown value CS=0x%08X while "
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001302 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001303 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001304 currpers = current_personality;
1305 break;
1306 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001307# elif 0
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001308 /* This version analyzes the opcode of a syscall instruction.
1309 * (int 0x80 on i386 vs. syscall on x86-64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001310 * It works, but is too complicated, and strictly speaking, unreliable.
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001311 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001312 unsigned long call, rip = x86_64_regs.rip;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001313 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1314 rip -= 2;
1315 errno = 0;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001316 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001317 if (errno)
1318 fprintf(stderr, "ptrace_peektext failed: %s\n",
1319 strerror(errno));
1320 switch (call & 0xffff) {
1321 /* x86-64: syscall = 0x0f 0x05 */
1322 case 0x050f: currpers = 0; break;
1323 /* i386: int 0x80 = 0xcd 0x80 */
1324 case 0x80cd: currpers = 1; break;
1325 default:
1326 currpers = current_personality;
1327 fprintf(stderr,
1328 "Unknown syscall opcode (0x%04X) while "
1329 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001330 "PID=%d\n", (int)call, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001331 break;
1332 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001333# endif
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001334
H.J. Lu35be5812012-04-16 13:00:01 +02001335# ifdef X32
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001336 /* If we are built for a x32 system, then personality 0 is x32
1337 * (not x86_64), and stracing of x86_64 apps is not supported.
1338 * Stracing of i386 apps is still supported.
H.J. Lu085e4282012-04-17 11:05:04 -07001339 */
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001340 if (currpers == 0) {
1341 fprintf(stderr, "syscall_%lu(...) in unsupported "
1342 "64-bit mode of process PID=%d\n",
1343 scno, tcp->pid);
1344 return 0;
H.J. Lu35be5812012-04-16 13:00:01 +02001345 }
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001346 currpers &= ~2; /* map 2,1 to 0,1 */
H.J. Lu35be5812012-04-16 13:00:01 +02001347# endif
H.J. Lu085e4282012-04-17 11:05:04 -07001348 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001349#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001350# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01001351 long psr;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001352 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001353 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
1354 if (ia64_ia32mode) {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001355 if (upeek(tcp->pid, PT_R1, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001356 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001357 } else {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001358 if (upeek(tcp->pid, PT_R15, &scno) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001359 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001360 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001361#elif defined(AARCH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001362 switch (aarch64_io.iov_len) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001363 case sizeof(aarch64_regs):
1364 /* We are in 64-bit mode */
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001365 scno = aarch64_regs.regs[8];
1366 update_personality(tcp, 1);
1367 break;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001368 case sizeof(arm_regs):
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001369 /* We are in 32-bit mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001370 /* Note: we don't support OABI, unlike 32-bit ARM build */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001371 scno = arm_regs.ARM_r7;
Elliott Hughes7a28f7f2014-03-03 23:45:26 +00001372 scno = shuffle_scno(scno);
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001373 update_personality(tcp, 0);
1374 break;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001375 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001376#elif defined(ARM)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001377 if (arm_regs.ARM_ip != 0) {
1378 /* It is not a syscall entry */
1379 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001380 tcp->flags |= TCB_INSYSCALL;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001381 return 0;
1382 }
1383 /* Note: we support only 32-bit CPUs, not 26-bit */
1384
Dmitry V. Levin0160e162014-03-18 23:37:43 +00001385# if !defined(__ARM_EABI__) || ENABLE_ARM_OABI
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001386 if (arm_regs.ARM_cpsr & 0x20)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001387 /* Thumb mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001388 goto scno_in_r7;
1389 /* ARM mode */
1390 /* Check EABI/OABI by examining SVC insn's low 24 bits */
1391 errno = 0;
1392 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1393 if (errno)
1394 return -1;
1395 /* EABI syscall convention? */
Dmitry V. Levin03b44542015-01-14 07:21:36 +00001396 if ((unsigned long) scno != 0xef000000) {
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001397 /* No, it's OABI */
1398 if ((scno & 0x0ff00000) != 0x0f900000) {
1399 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1400 tcp->pid, scno);
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001401 return -1;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001402 }
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001403 /* Fixup the syscall number */
1404 scno &= 0x000fffff;
1405 } else {
1406 scno_in_r7:
1407 scno = arm_regs.ARM_r7;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001408 }
Dmitry V. Levin0160e162014-03-18 23:37:43 +00001409# else /* __ARM_EABI__ || !ENABLE_ARM_OABI */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001410 scno = arm_regs.ARM_r7;
1411# endif
Denys Vlasenko7270de52013-02-21 15:46:34 +01001412 scno = shuffle_scno(scno);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001413#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001414 if (upeek(tcp->pid, 4*PT_ORIG_D0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001415 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001416#elif defined(LINUX_MIPSN32)
Roland McGrath542c2c62008-05-20 01:11:56 +00001417 unsigned long long regs[38];
1418
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001419 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001420 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001421 mips_a3 = regs[REG_A3];
1422 mips_r2 = regs[REG_V0];
Roland McGrath542c2c62008-05-20 01:11:56 +00001423
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001424 scno = mips_r2;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001425 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001426 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001427 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001428 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001429 return 0;
1430 }
Roland McGrath542c2c62008-05-20 01:11:56 +00001431 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001432#elif defined(MIPS)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001433 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001434 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001435 if (upeek(tcp->pid, REG_V0, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001436 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001437
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001438 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001439 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001440 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001441 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001442 return 0;
1443 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001444 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001445#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001446 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001447 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001448 if (upeek(tcp->pid, REG_R0, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001449 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001450
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001451 /*
1452 * Do some sanity checks to figure out if it's
1453 * really a syscall entry
1454 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001455 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001456 if (alpha_a3 == 0 || alpha_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001457 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001458 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001459 return 0;
1460 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001461 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001462#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001463 /* Disassemble the syscall trap. */
1464 /* Retrieve the syscall trap instruction. */
Denys Vlasenko46455822013-02-05 17:02:59 +01001465 unsigned long trap;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001466 errno = 0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001467# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001468 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001469 trap >>= 32;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001470# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001471 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001472# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001473 if (errno)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001474 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001475
1476 /* Disassemble the trap to see what personality to use. */
1477 switch (trap) {
1478 case 0x91d02010:
1479 /* Linux/SPARC syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001480 update_personality(tcp, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001481 break;
1482 case 0x91d0206d:
1483 /* Linux/SPARC64 syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001484 update_personality(tcp, 2);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001485 break;
1486 case 0x91d02000:
1487 /* SunOS syscall trap. (pers 1) */
1488 fprintf(stderr, "syscall: SunOS no support\n");
1489 return -1;
1490 case 0x91d02008:
1491 /* Solaris 2.x syscall trap. (per 2) */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001492 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001493 break;
1494 case 0x91d02009:
1495 /* NetBSD/FreeBSD syscall trap. */
1496 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1497 return -1;
1498 case 0x91d02027:
1499 /* Solaris 2.x gettimeofday */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001500 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001501 break;
1502 default:
Denys Vlasenko523635f2012-02-25 02:44:25 +01001503# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001504 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001505# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001506 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001507# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001508 return -1;
1509 }
1510
1511 /* Extract the system call number from the registers. */
1512 if (trap == 0x91d02027)
1513 scno = 156;
1514 else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001515 scno = sparc_regs.u_regs[U_REG_G1];
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001516 if (scno == 0) {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001517 scno = sparc_regs.u_regs[U_REG_O0];
1518 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 +02001519 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001520#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001521 if (upeek(tcp->pid, PT_GR20, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001522 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001523#elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001524 /*
1525 * In the new syscall ABI, the system call number is in R3.
1526 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001527 if (upeek(tcp->pid, 4*(REG_REG0+3), &scno) < 0)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001528 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001529
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001530 if (scno < 0) {
1531 /* Odd as it may seem, a glibc bug has been known to cause
1532 glibc to issue bogus negative syscall numbers. So for
1533 our purposes, make strace print what it *should* have been */
1534 long correct_scno = (scno & 0xff);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001535 if (debug_flag)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001536 fprintf(stderr,
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001537 "Detected glibc bug: bogus system call"
1538 " number = %ld, correcting to %ld\n",
1539 scno,
1540 correct_scno);
1541 scno = correct_scno;
1542 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001543#elif defined(SH64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001544 if (upeek(tcp->pid, REG_SYSCALL, &scno) < 0)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001545 return -1;
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001546 scno &= 0xFFFF;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001547#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001548 if (upeek(tcp->pid, 4*PT_R9, &scno) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001549 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001550#elif defined(TILE)
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001551 unsigned int currpers;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001552 scno = tile_regs.regs[10];
1553# ifdef __tilepro__
1554 currpers = 1;
1555# else
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001556# ifndef PT_FLAGS_COMPAT
1557# define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1558# endif
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001559 if (tile_regs.flags & PT_FLAGS_COMPAT)
1560 currpers = 1;
1561 else
1562 currpers = 0;
1563# endif
1564 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001565#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001566 if (upeek(tcp->pid, 0, &scno) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001567 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001568#elif defined(OR1K)
1569 scno = or1k_regs.gpr[11];
James Hogan5f999a82013-02-22 14:44:10 +00001570#elif defined(METAG)
1571 scno = metag_regs.dx[0][1]; /* syscall number in D1Re0 (D1.0) */
Chris Zankel8f636ed2013-03-25 10:22:07 -07001572#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001573 if (upeek(tcp->pid, SYSCALL_NR, &scno) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001574 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301575# elif defined(ARC)
1576 scno = arc_regs.scratch.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001577#endif
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001578
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001579 tcp->scno = scno;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001580 if (SCNO_IS_VALID(tcp->scno)) {
1581 tcp->s_ent = &sysent[scno];
1582 tcp->qual_flg = qual_flags[scno];
1583 } else {
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01001584 static const struct_sysent unknown = {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001585 .nargs = MAX_ARGS,
1586 .sys_flags = 0,
1587 .sys_func = printargs,
1588 .sys_name = "unknown", /* not used */
1589 };
1590 tcp->s_ent = &unknown;
1591 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1592 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001593 return 1;
1594}
1595
Dmitry V. Levind70c41d2015-02-05 23:43:06 +00001596/*
1597 * Cannot rely on __kernel_[u]long_t being defined,
1598 * it is quite a recent feature of <asm/posix_types.h>.
1599 */
1600#ifdef __kernel_long_t
1601typedef __kernel_long_t kernel_long_t;
1602typedef __kernel_ulong_t kernel_ulong_t;
1603#else
1604# ifdef X32
1605typedef long long kernel_long_t;
1606typedef unsigned long long kernel_ulong_t;
1607# else
1608typedef long kernel_long_t;
1609typedef unsigned long kernel_ulong_t;
1610# endif
1611#endif
1612
1613/*
1614 * Check the syscall return value register value for whether it is
1615 * a negated errno code indicating an error, or a success return value.
1616 */
1617static inline bool
1618is_negated_errno(kernel_ulong_t val)
1619{
Dmitry V. Levin09460522015-02-05 23:47:36 +00001620 /*
1621 * Thanks to SECCOMP_RET_DATA == 0xffff, abnormally large errno
1622 * values could be easily seen when a seccomp filter is used, e.g.
1623 * BPF_STMT(BPF_RET, SECCOMP_RET_ERRNO | SECCOMP_RET_DATA)
1624 */
1625 kernel_ulong_t max = -(kernel_long_t) 0x10000; /* SECCOMP_RET_DATA + 1 */
Dmitry V. Levind70c41d2015-02-05 23:43:06 +00001626
1627#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1628 if (current_wordsize < sizeof(val)) {
1629 val = (uint32_t) val;
1630 max = (uint32_t) max;
1631 }
1632#elif defined X32
1633 /*
1634 * current_wordsize is 4 even in personality 0 (native X32)
1635 * but truncation _must not_ be done in it.
1636 * can't check current_wordsize here!
1637 */
1638 if (current_personality != 0) {
1639 val = (uint32_t) val;
1640 max = (uint32_t) max;
1641 }
1642#endif
1643
1644 return val > max;
1645}
1646
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02001647/* Called at each syscall entry.
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001648 * Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001649 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1650 * 1: ok, continue in trace_syscall_entering().
1651 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001652 * ("????" etc) and bail out.
1653 */
Roland McGratha4d48532005-06-08 20:45:28 +00001654static int
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001655syscall_fixup_on_sysenter(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001656{
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001657 /* Do we have post-execve SIGTRAP suppressed? */
1658 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1659 return 1;
1660
Dmitry V. Levin8e398b62015-02-05 19:31:13 +00001661 /*
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001662 * No, unfortunately. Apply -ENOSYS heuristics.
1663 * We don't have to workaround SECCOMP_RET_ERRNO side effects
1664 * because any kernel with SECCOMP_RET_ERRNO support surely
1665 * implements PTRACE_O_TRACEEXEC.
Dmitry V. Levin8e398b62015-02-05 19:31:13 +00001666 */
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001667#if defined(I386)
1668 if (i386_regs.eax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001669 if (debug_flag)
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001670 fprintf(stderr, "not a syscall entry (eax = %ld)\n",
1671 i386_regs.eax);
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001672 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001673 }
H.J. Lu35be5812012-04-16 13:00:01 +02001674#elif defined(X86_64) || defined(X32)
Dmitry V. Levin8e398b62015-02-05 19:31:13 +00001675 if (x86_io.iov_len == sizeof(i386_regs)) {
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001676 if ((int) i386_regs.eax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001677 if (debug_flag)
Dmitry V. Levin8e398b62015-02-05 19:31:13 +00001678 fprintf(stderr,
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001679 "not a syscall entry (eax = %d)\n",
1680 (int) i386_regs.eax);
Dmitry V. Levin8e398b62015-02-05 19:31:13 +00001681 return 0;
1682 }
1683 } else {
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001684 if ((long long) x86_64_regs.rax != -ENOSYS) {
Dmitry V. Levin8e398b62015-02-05 19:31:13 +00001685 if (debug_flag)
1686 fprintf(stderr,
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001687 "not a syscall entry (rax = %lld)\n",
1688 (long long) x86_64_regs.rax);
Denys Vlasenko18beb982011-08-24 16:59:23 +02001689 return 0;
1690 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001691 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001692#elif defined(M68K)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001693 /* TODO? Eliminate upeek's in arches below like we did in x86 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001694 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001695 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001696 if (m68k_d0 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001697 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001698 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001699 return 0;
1700 }
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001701#elif defined(IA64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001702 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001703 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001704 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001705 return -1;
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001706 if (ia64_ia32mode && ia64_r8 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001707 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001708 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001709 return 0;
1710 }
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001711#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001712 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001713 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001714 if (cris_r10 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001715 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001716 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001717 return 0;
1718 }
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001719#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001720 if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001721 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001722 if (microblaze_r3 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001723 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001724 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001725 return 0;
1726 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001727#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001728 return 1;
1729}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001730
Denys Vlasenko146b9442012-03-18 22:10:48 +01001731static void
1732internal_fork(struct tcb *tcp)
1733{
1734#if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1735# define ARG_FLAGS 1
1736#else
1737# define ARG_FLAGS 0
1738#endif
1739#ifndef CLONE_UNTRACED
1740# define CLONE_UNTRACED 0x00800000
1741#endif
1742 if ((ptrace_setoptions
1743 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1744 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1745 return;
1746
1747 if (!followfork)
1748 return;
1749
1750 if (entering(tcp)) {
1751 /*
1752 * We won't see the new child if clone is called with
1753 * CLONE_UNTRACED, so we keep the same logic with that option
1754 * and don't trace it.
1755 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001756 if ((tcp->s_ent->sys_func == sys_clone)
1757 && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
1758 )
Denys Vlasenko146b9442012-03-18 22:10:48 +01001759 return;
1760 setbpt(tcp);
1761 } else {
1762 if (tcp->flags & TCB_BPTSET)
1763 clearbpt(tcp);
1764 }
1765}
1766
1767#if defined(TCB_WAITEXECVE)
1768static void
1769internal_exec(struct tcb *tcp)
1770{
1771 /* Maybe we have post-execve SIGTRAP suppressed? */
1772 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1773 return; /* yes, no need to do anything */
1774
1775 if (exiting(tcp) && syserror(tcp))
1776 /* Error in execve, no post-execve SIGTRAP expected */
1777 tcp->flags &= ~TCB_WAITEXECVE;
1778 else
1779 tcp->flags |= TCB_WAITEXECVE;
1780}
1781#endif
1782
1783static void
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001784syscall_fixup_for_fork_exec(struct tcb *tcp)
Roland McGrathc1e45922008-05-27 23:18:29 +00001785{
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001786 /*
1787 * We must always trace a few critical system calls in order to
1788 * correctly support following forks in the presence of tracing
1789 * qualifiers.
1790 */
1791 int (*func)();
1792
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001793 func = tcp->s_ent->sys_func;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001794
1795 if ( sys_fork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001796 || sys_clone == func
Denys Vlasenko146b9442012-03-18 22:10:48 +01001797 ) {
1798 internal_fork(tcp);
1799 return;
1800 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001801
Denys Vlasenko84703742012-02-25 02:38:52 +01001802#if defined(TCB_WAITEXECVE)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001803 if ( sys_execve == func
Denys Vlasenko84703742012-02-25 02:38:52 +01001804# if defined(SPARC) || defined(SPARC64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001805 || sys_execv == func
Denys Vlasenkoa7949742011-08-21 17:26:55 +02001806# endif
Denys Vlasenko146b9442012-03-18 22:10:48 +01001807 ) {
1808 internal_exec(tcp);
1809 return;
1810 }
Roland McGrathc1e45922008-05-27 23:18:29 +00001811#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001812}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001813
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001814/* Return -1 on error or 1 on success (never 0!) */
Roland McGratha4d48532005-06-08 20:45:28 +00001815static int
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001816get_syscall_args(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001817{
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001818 int i, nargs;
1819
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001820 nargs = tcp->s_ent->nargs;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001821
Denys Vlasenko523635f2012-02-25 02:44:25 +01001822#if defined(S390) || defined(S390X)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001823 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001824 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 +02001825 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001826#elif defined(ALPHA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001827 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001828 if (upeek(tcp->pid, REG_A0+i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001829 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001830#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001831 if (!ia64_ia32mode) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001832 unsigned long *out0, cfm, sof, sol;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001833 long rbs_end;
1834 /* be backwards compatible with kernel < 2.4.4... */
1835# ifndef PT_RBS_END
1836# define PT_RBS_END PT_AR_BSP
1837# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001838
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001839 if (upeek(tcp->pid, PT_RBS_END, &rbs_end) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001840 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001841 if (upeek(tcp->pid, PT_CFM, (long *) &cfm) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001842 return -1;
1843
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001844 sof = (cfm >> 0) & 0x7f;
1845 sol = (cfm >> 7) & 0x7f;
1846 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1847
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001848 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001849 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1850 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1851 return -1;
1852 }
1853 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001854 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1855 PT_R9 /* ECX = out1 */,
1856 PT_R10 /* EDX = out2 */,
1857 PT_R14 /* ESI = out3 */,
1858 PT_R15 /* EDI = out4 */,
1859 PT_R13 /* EBP = out5 */};
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001860
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001861 for (i = 0; i < nargs; ++i) {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001862 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001863 return -1;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001864 /* truncate away IVE sign-extension */
1865 tcp->u_arg[i] &= 0xffffffff;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001866 }
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001867 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001868#elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001869 /* N32 and N64 both use up to six registers. */
1870 unsigned long long regs[38];
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001871
1872 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1873 return -1;
1874
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001875 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001876 tcp->u_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001877# if defined(LINUX_MIPSN32)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001878 tcp->ext_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001879# endif
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001880 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001881#elif defined(MIPS)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001882 if (nargs > 4) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001883 long sp;
1884
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001885 if (upeek(tcp->pid, REG_SP, &sp) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001886 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001887 for (i = 0; i < 4; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001888 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001889 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001890 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001891 (char *)(tcp->u_arg + 4));
1892 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001893 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001894 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001895 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001896 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001897#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001898 (void)i;
1899 (void)nargs;
1900 tcp->u_arg[0] = ppc_regs.orig_gpr3;
1901 tcp->u_arg[1] = ppc_regs.gpr[4];
1902 tcp->u_arg[2] = ppc_regs.gpr[5];
1903 tcp->u_arg[3] = ppc_regs.gpr[6];
1904 tcp->u_arg[4] = ppc_regs.gpr[7];
1905 tcp->u_arg[5] = ppc_regs.gpr[8];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001906#elif defined(SPARC) || defined(SPARC64)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001907 for (i = 0; i < nargs; ++i)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001908 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001909#elif defined(HPPA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001910 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001911 if (upeek(tcp->pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001912 return -1;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001913#elif defined(ARM) || defined(AARCH64)
1914# if defined(AARCH64)
1915 if (tcp->currpers == 1)
1916 for (i = 0; i < nargs; ++i)
1917 tcp->u_arg[i] = aarch64_regs.regs[i];
1918 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001919# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001920 for (i = 0; i < nargs; ++i)
Denys Vlasenko401374e2013-02-06 18:24:39 +01001921 tcp->u_arg[i] = arm_regs.uregs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001922#elif defined(AVR32)
Denys Vlasenkob5b25892011-08-30 19:04:54 +02001923 (void)i;
1924 (void)nargs;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001925 tcp->u_arg[0] = avr32_regs.r12;
1926 tcp->u_arg[1] = avr32_regs.r11;
1927 tcp->u_arg[2] = avr32_regs.r10;
1928 tcp->u_arg[3] = avr32_regs.r9;
1929 tcp->u_arg[4] = avr32_regs.r5;
1930 tcp->u_arg[5] = avr32_regs.r3;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001931#elif defined(BFIN)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001932 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 +02001933
Denys Vlasenko4b887a52011-08-23 13:32:38 +02001934 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001935 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001936 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001937#elif defined(SH)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001938 static const int syscall_regs[MAX_ARGS] = {
1939 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1940 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001941 };
1942
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001943 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001944 if (upeek(tcp->pid, syscall_regs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001945 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001946#elif defined(SH64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001947 int i;
1948 /* Registers used by SH5 Linux system calls for parameters */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001949 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001950
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001951 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001952 if (upeek(tcp->pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001953 return -1;
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001954#elif defined(I386)
1955 (void)i;
1956 (void)nargs;
1957 tcp->u_arg[0] = i386_regs.ebx;
1958 tcp->u_arg[1] = i386_regs.ecx;
1959 tcp->u_arg[2] = i386_regs.edx;
1960 tcp->u_arg[3] = i386_regs.esi;
1961 tcp->u_arg[4] = i386_regs.edi;
1962 tcp->u_arg[5] = i386_regs.ebp;
H.J. Lu35be5812012-04-16 13:00:01 +02001963#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001964 (void)i;
1965 (void)nargs;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001966 if (x86_io.iov_len != sizeof(i386_regs)) {
1967 /* x86-64 or x32 ABI */
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001968 tcp->u_arg[0] = x86_64_regs.rdi;
1969 tcp->u_arg[1] = x86_64_regs.rsi;
1970 tcp->u_arg[2] = x86_64_regs.rdx;
1971 tcp->u_arg[3] = x86_64_regs.r10;
1972 tcp->u_arg[4] = x86_64_regs.r8;
1973 tcp->u_arg[5] = x86_64_regs.r9;
H.J. Lu35be5812012-04-16 13:00:01 +02001974# ifdef X32
1975 tcp->ext_arg[0] = x86_64_regs.rdi;
1976 tcp->ext_arg[1] = x86_64_regs.rsi;
1977 tcp->ext_arg[2] = x86_64_regs.rdx;
1978 tcp->ext_arg[3] = x86_64_regs.r10;
1979 tcp->ext_arg[4] = x86_64_regs.r8;
1980 tcp->ext_arg[5] = x86_64_regs.r9;
1981# endif
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001982 } else {
1983 /* i386 ABI */
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001984 /* Zero-extend from 32 bits */
1985 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1986 * if you need to use *sign-extended* parameter.
1987 */
1988 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1989 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1990 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1991 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1992 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1993 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001994 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001995#elif defined(MICROBLAZE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001996 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001997 if (upeek(tcp->pid, (5 + i) * 4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001998 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001999#elif defined(CRISV10) || defined(CRISV32)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00002000 static const int crisregs[MAX_ARGS] = {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02002001 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02002002 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02002003 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00002004
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00002005 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002006 if (upeek(tcp->pid, crisregs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02002007 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002008#elif defined(TILE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00002009 for (i = 0; i < nargs; ++i)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002010 tcp->u_arg[i] = tile_regs.regs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01002011#elif defined(M68K)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00002012 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002013 if (upeek(tcp->pid, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02002014 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01002015#elif defined(OR1K)
2016 (void)nargs;
2017 for (i = 0; i < 6; ++i)
2018 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
James Hogan5f999a82013-02-22 14:44:10 +00002019#elif defined(METAG)
2020 for (i = 0; i < nargs; i++)
2021 /* arguments go backwards from D1Ar1 (D1.3) */
2022 tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
Chris Zankel8f636ed2013-03-25 10:22:07 -07002023#elif defined(XTENSA)
2024 /* arg0: a6, arg1: a3, arg2: a4, arg3: a5, arg4: a8, arg5: a9 */
2025 static const int xtensaregs[MAX_ARGS] = { 6, 3, 4, 5, 8, 9 };
2026 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002027 if (upeek(tcp->pid, REG_A_BASE + xtensaregs[i], &tcp->u_arg[i]) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07002028 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05302029# elif defined(ARC)
2030 long *arc_args = &arc_regs.scratch.r0;
2031 for (i = 0; i < nargs; ++i)
2032 tcp->u_arg[i] = *arc_args--;
2033
Denys Vlasenko523635f2012-02-25 02:44:25 +01002034#else /* Other architecture (32bits specific) */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00002035 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002036 if (upeek(tcp->pid, i*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02002037 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002038#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00002039 return 1;
2040}
2041
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002042static int
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002043trace_syscall_entering(struct tcb *tcp)
2044{
2045 int res, scno_good;
2046
Denys Vlasenko2ce12ed2011-08-24 17:25:32 +02002047#if defined TCB_WAITEXECVE
2048 if (tcp->flags & TCB_WAITEXECVE) {
2049 /* This is the post-execve SIGTRAP. */
2050 tcp->flags &= ~TCB_WAITEXECVE;
2051 return 0;
2052 }
2053#endif
2054
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002055 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002056 if (res == 0)
2057 return res;
Denys Vlasenko907735a2012-03-21 00:23:16 +01002058 if (res == 1) {
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02002059 res = syscall_fixup_on_sysenter(tcp);
Denys Vlasenko907735a2012-03-21 00:23:16 +01002060 if (res == 0)
2061 return res;
2062 if (res == 1)
2063 res = get_syscall_args(tcp);
2064 }
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002065
2066 if (res != 1) {
2067 printleader(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002068 if (scno_good != 1)
Denys Vlasenkob7a6dae2012-03-20 16:48:35 +01002069 tprints("????" /* anti-trigraph gap */ "(");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002070 else if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002071 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002072 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002073 tprintf("%s(", tcp->s_ent->sys_name);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002074 /*
2075 * " <unavailable>" will be added later by the code which
2076 * detects ptrace errors.
2077 */
2078 goto ret;
2079 }
2080
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002081 if ( sys_execve == tcp->s_ent->sys_func
2082# if defined(SPARC) || defined(SPARC64)
2083 || sys_execv == tcp->s_ent->sys_func
2084# endif
2085 ) {
2086 hide_log_until_execve = 0;
2087 }
2088
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002089#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002090 while (1) {
Denys Vlasenko523635f2012-02-25 02:44:25 +01002091# ifdef SYS_socket_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002092 if (tcp->s_ent->sys_func == sys_socketcall) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00002093 decode_socket_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002094 break;
2095 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002096# endif
2097# ifdef SYS_ipc_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002098 if (tcp->s_ent->sys_func == sys_ipc) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00002099 decode_ipc_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002100 break;
2101 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002102# endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002103 break;
2104 }
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002105#endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002106
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01002107 if (need_fork_exec_workarounds)
2108 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002109
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002110 if (!(tcp->qual_flg & QUAL_TRACE)
2111 || (tracing_paths && !pathtrace_match(tcp))
2112 ) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002113 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
2114 return 0;
2115 }
2116
2117 tcp->flags &= ~TCB_FILTERED;
2118
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002119 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002120 res = 0;
2121 goto ret;
2122 }
2123
Masatake YAMATOed69fc22014-04-16 15:33:35 +09002124#ifdef USE_LIBUNWIND
2125 if (stack_trace_enabled) {
2126 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
2127 unwind_capture_stacktrace(tcp);
2128 }
2129#endif
2130
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002131 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002132 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002133 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002134 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002135 tprintf("%s(", tcp->s_ent->sys_name);
2136 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002137 res = printargs(tcp);
2138 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002139 res = tcp->s_ent->sys_func(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002140
Dmitry V. Levinb742d8c2012-09-17 22:40:12 +00002141 fflush(tcp->outf);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002142 ret:
2143 tcp->flags |= TCB_INSYSCALL;
2144 /* Measure the entrance time as late as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002145 if (Tflag || cflag)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002146 gettimeofday(&tcp->etime, NULL);
2147 return res;
2148}
2149
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002150/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01002151 * 1: ok, continue in trace_syscall_exiting().
2152 * -1: error, trace_syscall_exiting() should print error indicator
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002153 * ("????" etc) and bail out.
2154 */
2155static int
2156get_syscall_result(struct tcb *tcp)
2157{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002158#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002159 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002160 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002161#elif defined(POWERPC)
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002162 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002163#elif defined(AVR32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002164 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002165#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002166 if (upeek(tcp->pid, PT_R0, &bfin_r0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002167 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002168#elif defined(I386)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002169 /* already done by get_regs */
H.J. Lu35be5812012-04-16 13:00:01 +02002170#elif defined(X86_64) || defined(X32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002171 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002172#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002173# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01002174 long psr;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002175 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02002176 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002177 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002178 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002179 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002180 return -1;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002181#elif defined(ARM)
2182 /* already done by get_regs */
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002183#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002184 /* register reading already done by get_regs */
2185
2186 /* Used to do this, but we did it on syscall entry already: */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002187 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
2188 * else it's personality 0.
2189 */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002190 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
Denys Vlasenko523635f2012-02-25 02:44:25 +01002191#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002192 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002193 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002194#elif defined(LINUX_MIPSN32)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002195 unsigned long long regs[38];
2196
2197 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
2198 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002199 mips_a3 = regs[REG_A3];
2200 mips_r2 = regs[REG_V0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01002201#elif defined(MIPS)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002202 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002203 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002204 if (upeek(tcp->pid, REG_V0, &mips_r2) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002205 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002206#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002207 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002208 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002209 if (upeek(tcp->pid, REG_R0, &alpha_r0) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002210 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002211#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002212 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002213#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002214 if (upeek(tcp->pid, PT_GR28, &hppa_r28) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002215 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002216#elif defined(SH)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002217 /* new syscall ABI returns result in R0 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002218 if (upeek(tcp->pid, 4*REG_REG0, (long *)&sh_r0) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002219 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002220#elif defined(SH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002221 /* ABI defines result returned in r9 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002222 if (upeek(tcp->pid, REG_GENERAL(9), (long *)&sh64_r9) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002223 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002224#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002225 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002226 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002227#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002228 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002229#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002230 if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002231 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01002232#elif defined(OR1K)
2233 /* already done by get_regs */
James Hogan5f999a82013-02-22 14:44:10 +00002234#elif defined(METAG)
2235 /* already done by get_regs */
Chris Zankel8f636ed2013-03-25 10:22:07 -07002236#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002237 if (upeek(tcp->pid, REG_A_BASE + 2, &xtensa_a2) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07002238 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05302239#elif defined(ARC)
2240 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002241#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002242 return 1;
2243}
2244
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002245/* Called at each syscall exit */
2246static void
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002247syscall_fixup_on_sysexit(struct tcb *tcp)
2248{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002249#if defined(S390) || defined(S390X)
Denys Vlasenkoece98792011-08-25 10:25:35 +02002250 if ((tcp->flags & TCB_WAITEXECVE)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002251 && (s390_gpr2 == -ENOSYS || s390_gpr2 == tcp->scno)) {
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002252 /*
2253 * Return from execve.
2254 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2255 * flag set for the post-execve SIGTRAP to see and reset.
2256 */
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002257 s390_gpr2 = 0;
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002258 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002259#endif
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002260}
2261
Denys Vlasenko907735a2012-03-21 00:23:16 +01002262/* Returns:
2263 * 1: ok, continue in trace_syscall_exiting().
2264 * -1: error, trace_syscall_exiting() should print error indicator
2265 * ("????" etc) and bail out.
2266 */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002267static void
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002268get_error(struct tcb *tcp)
2269{
2270 int u_error = 0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002271 int check_errno = 1;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002272 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002273 check_errno = 0;
2274 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002275#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002276 if (check_errno && is_negated_errno(s390_gpr2)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002277 tcp->u_rval = -1;
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002278 u_error = -s390_gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002279 }
2280 else {
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002281 tcp->u_rval = s390_gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002282 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002283#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002284 if (check_errno && is_negated_errno(i386_regs.eax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002285 tcp->u_rval = -1;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002286 u_error = -i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002287 }
2288 else {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002289 tcp->u_rval = i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002290 }
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00002291#elif defined(X86_64) || defined(X32)
2292 /*
2293 * In X32, return value is 64-bit (llseek uses one).
2294 * Using merely "long rax" would not work.
2295 */
2296 kernel_long_t rax;
2297
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002298 if (x86_io.iov_len == sizeof(i386_regs)) {
2299 /* Sign extend from 32 bits */
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00002300 rax = (int32_t) i386_regs.eax;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002301 } else {
2302 rax = x86_64_regs.rax;
2303 }
2304 if (check_errno && is_negated_errno(rax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002305 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002306 u_error = -rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002307 }
2308 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002309 tcp->u_rval = rax;
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00002310# ifdef X32
2311 /* tcp->u_rval contains a truncated value */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002312 tcp->u_lrval = rax;
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00002313# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002314 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002315#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02002316 if (ia64_ia32mode) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002317 int err;
2318
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002319 err = (int)ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002320 if (check_errno && is_negated_errno(err)) {
2321 tcp->u_rval = -1;
2322 u_error = -err;
2323 }
2324 else {
2325 tcp->u_rval = err;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002326 }
2327 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002328 if (check_errno && ia64_r10) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002329 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002330 u_error = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002331 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002332 tcp->u_rval = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002333 }
2334 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002335#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002336 if (check_errno && mips_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002337 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002338 u_error = mips_r2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002339 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002340 tcp->u_rval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002341# if defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002342 tcp->u_lrval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002343# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002344 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002345#elif defined(POWERPC)
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002346 if (check_errno && (ppc_regs.ccr & 0x10000000)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002347 tcp->u_rval = -1;
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002348 u_error = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002349 }
2350 else {
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002351 tcp->u_rval = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002352 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002353#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002354 if (check_errno && is_negated_errno(m68k_d0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002355 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002356 u_error = -m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002357 }
2358 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002359 tcp->u_rval = m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002360 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002361#elif defined(ARM) || defined(AARCH64)
2362# if defined(AARCH64)
2363 if (tcp->currpers == 1) {
2364 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2365 tcp->u_rval = -1;
2366 u_error = -aarch64_regs.regs[0];
2367 }
2368 else {
2369 tcp->u_rval = aarch64_regs.regs[0];
2370 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002371 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002372 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002373# endif
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002374 {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002375 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002376 tcp->u_rval = -1;
Denys Vlasenko401374e2013-02-06 18:24:39 +01002377 u_error = -arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002378 }
2379 else {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002380 tcp->u_rval = arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002381 }
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002382 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002383#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002384 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002385 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002386 u_error = -avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002387 }
2388 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002389 tcp->u_rval = avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002390 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002391#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002392 if (check_errno && is_negated_errno(bfin_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002393 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002394 u_error = -bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002395 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002396 tcp->u_rval = bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002397 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002398#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002399 if (check_errno && alpha_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002400 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002401 u_error = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002402 }
2403 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002404 tcp->u_rval = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002405 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002406#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002407 if (check_errno && sparc_regs.psr & PSR_C) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002408 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002409 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002410 }
2411 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002412 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002413 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002414#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002415 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002416 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002417 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002418 }
2419 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002420 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002421 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002422#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002423 if (check_errno && is_negated_errno(hppa_r28)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002424 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002425 u_error = -hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002426 }
2427 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002428 tcp->u_rval = hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002429 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002430#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002431 if (check_errno && is_negated_errno(sh_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002432 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002433 u_error = -sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002434 }
2435 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002436 tcp->u_rval = sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002437 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002438#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002439 if (check_errno && is_negated_errno(sh64_r9)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002440 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002441 u_error = -sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002442 }
2443 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002444 tcp->u_rval = sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002445 }
James Hogan5f999a82013-02-22 14:44:10 +00002446#elif defined(METAG)
2447 /* result pointer in D0Re0 (D0.0) */
2448 if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2449 tcp->u_rval = -1;
2450 u_error = -metag_regs.dx[0][0];
2451 }
2452 else {
2453 tcp->u_rval = metag_regs.dx[0][0];
2454 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002455#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002456 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002457 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002458 u_error = -cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002459 }
2460 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002461 tcp->u_rval = cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002462 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002463#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002464 /*
2465 * The standard tile calling convention returns the value (or negative
2466 * errno) in r0, and zero (or positive errno) in r1.
2467 * Until at least kernel 3.8, however, the r1 value is not reflected
2468 * in ptregs at this point, so we use r0 here.
2469 */
2470 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002471 tcp->u_rval = -1;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002472 u_error = -tile_regs.regs[0];
2473 } else {
2474 tcp->u_rval = tile_regs.regs[0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002475 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002476#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002477 if (check_errno && is_negated_errno(microblaze_r3)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002478 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002479 u_error = -microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002480 }
2481 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002482 tcp->u_rval = microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002483 }
Christian Svensson492f81f2013-02-14 13:26:27 +01002484#elif defined(OR1K)
2485 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2486 tcp->u_rval = -1;
2487 u_error = -or1k_regs.gpr[11];
2488 }
2489 else {
2490 tcp->u_rval = or1k_regs.gpr[11];
2491 }
Chris Zankel8f636ed2013-03-25 10:22:07 -07002492#elif defined(XTENSA)
2493 if (check_errno && is_negated_errno(xtensa_a2)) {
2494 tcp->u_rval = -1;
2495 u_error = -xtensa_a2;
2496 }
2497 else {
2498 tcp->u_rval = xtensa_a2;
2499 }
Vineet Gupta7daacbb2013-08-16 12:47:06 +05302500#elif defined(ARC)
2501 if (check_errno && is_negated_errno(arc_regs.scratch.r0)) {
2502 tcp->u_rval = -1;
2503 u_error = -arc_regs.scratch.r0;
2504 }
2505 else {
2506 tcp->u_rval = arc_regs.scratch.r0;
2507 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002508#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002509 tcp->u_error = u_error;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002510}
2511
2512static void
2513dumpio(struct tcb *tcp)
2514{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002515 int (*func)();
2516
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002517 if (syserror(tcp))
2518 return;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +01002519 if ((unsigned long) tcp->u_arg[0] >= num_quals)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002520 return;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002521 func = tcp->s_ent->sys_func;
2522 if (func == printargs)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002523 return;
2524 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002525 if (func == sys_read ||
2526 func == sys_pread ||
2527 func == sys_recv ||
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002528 func == sys_recvfrom) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002529 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002530 return;
2531 } else if (func == sys_readv) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002532 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002533 return;
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002534#if HAVE_SENDMSG
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002535 } else if (func == sys_recvmsg) {
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002536 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002537 return;
2538 } else if (func == sys_recvmmsg) {
Masatake YAMATOa807dce2014-11-07 01:23:26 +09002539 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002540 return;
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002541#endif
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002542 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002543 }
2544 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002545 if (func == sys_write ||
2546 func == sys_pwrite ||
2547 func == sys_send ||
2548 func == sys_sendto)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002549 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002550 else if (func == sys_writev)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002551 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002552#if HAVE_SENDMSG
2553 else if (func == sys_sendmsg)
2554 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
Masatake YAMATOa807dce2014-11-07 01:23:26 +09002555 else if (func == sys_sendmmsg)
2556 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002557#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002558 }
2559}
2560
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002561static int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002562trace_syscall_exiting(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002563{
2564 int sys_res;
2565 struct timeval tv;
Denys Vlasenko1a5b5a72011-08-25 00:29:56 +02002566 int res;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002567 long u_error;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002568
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002569 /* Measure the exit time as early as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002570 if (Tflag || cflag)
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002571 gettimeofday(&tv, NULL);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002572
Masatake YAMATOed69fc22014-04-16 15:33:35 +09002573#ifdef USE_LIBUNWIND
2574 if (stack_trace_enabled) {
2575 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
2576 unwind_cache_invalidate(tcp);
2577 }
2578#endif
2579
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00002580#if SUPPORTED_PERSONALITIES > 1
2581 update_personality(tcp, tcp->currpers);
2582#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002583 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002584 if (res == 1) {
2585 syscall_fixup_on_sysexit(tcp); /* never fails */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002586 get_error(tcp); /* never fails */
2587 if (need_fork_exec_workarounds)
2588 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002589 if (filtered(tcp) || hide_log_until_execve)
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002590 goto ret;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002591 }
2592
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002593 if (cflag) {
Dmitry V. Levinac5133d2014-05-29 18:10:00 +00002594 count_syscall(tcp, &tv);
Denys Vlasenko7b609d52011-06-22 14:32:43 +02002595 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002596 goto ret;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002597 }
2598 }
2599
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002600 /* If not in -ff mode, and printing_tcp != tcp,
2601 * then the log currently does not end with output
2602 * of _our syscall entry_, but with something else.
2603 * We need to say which syscall's return is this.
2604 *
2605 * Forced reprinting via TCB_REPRINT is used only by
2606 * "strace -ff -oLOG test/threaded_execve" corner case.
2607 * It's the only case when -ff mode needs reprinting.
2608 */
2609 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2610 tcp->flags &= ~TCB_REPRINT;
2611 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002612 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002613 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002614 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002615 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002616 }
2617 printing_tcp = tcp;
2618
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002619 if (res != 1) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002620 /* There was error in one of prior ptrace ops */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002621 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002622 tabto();
Denys Vlasenko000b6012012-01-28 01:25:03 +01002623 tprints("= ? <unavailable>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002624 line_ended();
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002625 tcp->flags &= ~TCB_INSYSCALL;
2626 return res;
2627 }
2628
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002629 sys_res = 0;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002630 if (tcp->qual_flg & QUAL_RAW) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002631 /* sys_res = printargs(tcp); - but it's nop on sysexit */
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002632 } else {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002633 /* FIXME: not_failing_only (IOW, option -z) is broken:
2634 * failure of syscall is known only after syscall return.
2635 * Thus we end up with something like this on, say, ENOENT:
2636 * open("doesnt_exist", O_RDONLY <unfinished ...>
2637 * {next syscall decode}
2638 * whereas the intended result is that open(...) line
2639 * is not shown at all.
2640 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002641 if (not_failing_only && tcp->u_error)
Denys Vlasenko3b738812011-08-22 02:06:35 +02002642 goto ret; /* ignore failed syscalls */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002643 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002644 }
2645
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002646 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002647 tabto();
Denys Vlasenko3b738812011-08-22 02:06:35 +02002648 u_error = tcp->u_error;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002649 if (tcp->qual_flg & QUAL_RAW) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002650 if (u_error)
2651 tprintf("= -1 (errno %ld)", u_error);
2652 else
2653 tprintf("= %#lx", tcp->u_rval);
2654 }
2655 else if (!(sys_res & RVAL_NONE) && u_error) {
2656 switch (u_error) {
Denys Vlasenkofe585652012-01-12 11:26:34 +01002657 /* Blocked signals do not interrupt any syscalls.
2658 * In this case syscalls don't return ERESTARTfoo codes.
2659 *
2660 * Deadly signals set to SIG_DFL interrupt syscalls
2661 * and kill the process regardless of which of the codes below
2662 * is returned by the interrupted syscall.
2663 * In some cases, kernel forces a kernel-generated deadly
2664 * signal to be unblocked and set to SIG_DFL (and thus cause
2665 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2666 * or SIGILL. (The alternative is to leave process spinning
2667 * forever on the faulty instruction - not useful).
2668 *
2669 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2670 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2671 * but kernel will always restart them.
2672 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002673 case ERESTARTSYS:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002674 /* Most common type of signal-interrupted syscall exit code.
2675 * The system call will be restarted with the same arguments
2676 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2677 */
2678 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002679 break;
2680 case ERESTARTNOINTR:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002681 /* Rare. For example, fork() returns this if interrupted.
2682 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2683 */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002684 tprints("= ? ERESTARTNOINTR (To be restarted)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002685 break;
2686 case ERESTARTNOHAND:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002687 /* pause(), rt_sigsuspend() etc use this code.
2688 * SA_RESTART is ignored (assumed not set):
2689 * syscall won't restart (will return EINTR instead)
Denys Vlasenko30c03232013-02-19 16:59:26 +01002690 * even after signal with SA_RESTART set. However,
2691 * after SIG_IGN or SIG_DFL signal it will restart
2692 * (thus the name "restart only if has no handler").
Denys Vlasenkofe585652012-01-12 11:26:34 +01002693 */
Denys Vlasenkoaba62922013-03-05 16:56:35 +01002694 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002695 break;
2696 case ERESTART_RESTARTBLOCK:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002697 /* Syscalls like nanosleep(), poll() which can't be
2698 * restarted with their original arguments use this
2699 * code. Kernel will execute restart_syscall() instead,
2700 * which changes arguments before restarting syscall.
2701 * SA_RESTART is ignored (assumed not set) similarly
2702 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2703 * since restart data is saved in "restart block"
2704 * in task struct, and if signal handler uses a syscall
2705 * which in turn saves another such restart block,
2706 * old data is lost and restart becomes impossible)
2707 */
2708 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002709 break;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002710 default:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002711 if (u_error < 0)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002712 tprintf("= -1 E??? (errno %ld)", u_error);
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00002713 else if ((unsigned long) u_error < nerrnos)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002714 tprintf("= -1 %s (%s)", errnoent[u_error],
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002715 strerror(u_error));
2716 else
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002717 tprintf("= -1 ERRNO_%ld (%s)", u_error,
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002718 strerror(u_error));
2719 break;
2720 }
2721 if ((sys_res & RVAL_STR) && tcp->auxstr)
2722 tprintf(" (%s)", tcp->auxstr);
2723 }
2724 else {
2725 if (sys_res & RVAL_NONE)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002726 tprints("= ?");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002727 else {
2728 switch (sys_res & RVAL_MASK) {
2729 case RVAL_HEX:
2730 tprintf("= %#lx", tcp->u_rval);
2731 break;
2732 case RVAL_OCTAL:
2733 tprintf("= %#lo", tcp->u_rval);
2734 break;
2735 case RVAL_UDECIMAL:
2736 tprintf("= %lu", tcp->u_rval);
2737 break;
2738 case RVAL_DECIMAL:
2739 tprintf("= %ld", tcp->u_rval);
2740 break;
Zubin Mithra64aa1b12014-06-04 08:30:41 +05302741 case RVAL_FD:
2742 if (show_fd_path) {
2743 tprints("= ");
2744 printfd(tcp, tcp->u_rval);
2745 }
2746 else
2747 tprintf("= %ld", tcp->u_rval);
2748 break;
H.J. Ludd0130b2012-04-16 12:16:45 +02002749#if defined(LINUX_MIPSN32) || defined(X32)
2750 /*
2751 case RVAL_LHEX:
2752 tprintf("= %#llx", tcp->u_lrval);
2753 break;
2754 case RVAL_LOCTAL:
2755 tprintf("= %#llo", tcp->u_lrval);
2756 break;
2757 */
2758 case RVAL_LUDECIMAL:
2759 tprintf("= %llu", tcp->u_lrval);
2760 break;
2761 /*
2762 case RVAL_LDECIMAL:
2763 tprintf("= %lld", tcp->u_lrval);
2764 break;
2765 */
2766#endif
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002767 default:
2768 fprintf(stderr,
2769 "invalid rval format\n");
2770 break;
2771 }
2772 }
2773 if ((sys_res & RVAL_STR) && tcp->auxstr)
2774 tprintf(" (%s)", tcp->auxstr);
2775 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002776 if (Tflag) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002777 tv_sub(&tv, &tv, &tcp->etime);
2778 tprintf(" <%ld.%06ld>",
2779 (long) tv.tv_sec, (long) tv.tv_usec);
2780 }
Denys Vlasenko000b6012012-01-28 01:25:03 +01002781 tprints("\n");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002782 dumpio(tcp);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002783 line_ended();
2784
Luca Clementi327064b2013-07-23 00:11:35 -07002785#ifdef USE_LIBUNWIND
2786 if (stack_trace_enabled)
Masatake YAMATO61413922014-04-16 15:33:02 +09002787 unwind_print_stacktrace(tcp);
Luca Clementi327064b2013-07-23 00:11:35 -07002788#endif
2789
Denys Vlasenko3b738812011-08-22 02:06:35 +02002790 ret:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002791 tcp->flags &= ~TCB_INSYSCALL;
2792 return 0;
2793}
2794
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002795int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002796trace_syscall(struct tcb *tcp)
2797{
2798 return exiting(tcp) ?
2799 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2800}