blob: adc17857f35fbd695c08f5c371d7189d444506de [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
Dmitry V. Levinfadf3792015-02-13 00:26:38 +000042#include "ptrace.h"
Wichert Akkerman15dea971999-10-06 13:06:34 +000043
Denys Vlasenko84703742012-02-25 02:38:52 +010044#if defined(SPARC64)
Roland McGrath6d1a65c2004-07-12 07:44:08 +000045# undef PTRACE_GETREGS
46# define PTRACE_GETREGS PTRACE_GETREGS64
47# undef PTRACE_SETREGS
48# define PTRACE_SETREGS PTRACE_SETREGS64
Denys Vlasenko84703742012-02-25 02:38:52 +010049#endif
Roland McGrath6d1a65c2004-07-12 07:44:08 +000050
Denys Vlasenko84703742012-02-25 02:38:52 +010051#if defined(IA64)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000052# include <asm/ptrace_offsets.h>
53# include <asm/rse.h>
54#endif
55
Dmitry V. Levin32c049a2013-03-18 00:59:27 +000056/* for struct iovec */
57#include <sys/uio.h>
58/* for NT_PRSTATUS */
59#ifdef HAVE_ELF_H
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010060# include <elf.h>
61#endif
62
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000063#ifndef NSIG
Denys Vlasenko523635f2012-02-25 02:44:25 +010064# warning: NSIG is not defined, using 32
65# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000066#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000067
68#include "syscall.h"
69
70/* Define these shorthand notations to simplify the syscallent files. */
Roland McGrath2fe7b132005-07-05 03:25:35 +000071#define TD TRACE_DESC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000072#define TF TRACE_FILE
73#define TI TRACE_IPC
74#define TN TRACE_NETWORK
75#define TP TRACE_PROCESS
76#define TS TRACE_SIGNAL
Namhyung Kim96792962012-10-24 11:41:57 +090077#define TM TRACE_MEMORY
Dmitry V. Levin50a218d2011-01-18 17:36:20 +000078#define NF SYSCALL_NEVER_FAILS
Denys Vlasenkoac1ce772011-08-23 13:24:17 +020079#define MA MAX_ARGS
Masatake YAMATO1d78d222014-04-16 15:33:11 +090080#define SI STACKTRACE_INVALIDATE_CACHE
81#define SE STACKTRACE_CAPTURE_ON_ENTER
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000082
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +010083const struct_sysent sysent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000084#include "syscallent.h"
85};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000086
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010087#if SUPPORTED_PERSONALITIES > 1
88static const struct_sysent sysent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +010089# include "syscallent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000090};
Denys Vlasenko39fca622011-08-20 02:12:33 +020091#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000092
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010093#if SUPPORTED_PERSONALITIES > 2
94static const struct_sysent sysent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +010095# include "syscallent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000096};
Denys Vlasenko39fca622011-08-20 02:12:33 +020097#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000098
99/* Now undef them since short defines cause wicked namespace pollution. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000100#undef TD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000101#undef TF
102#undef TI
103#undef TN
104#undef TP
105#undef TS
Namhyung Kim96792962012-10-24 11:41:57 +0900106#undef TM
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000107#undef NF
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200108#undef MA
Masatake YAMATO1d78d222014-04-16 15:33:11 +0900109#undef SI
110#undef SE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000111
Denys Vlasenko39fca622011-08-20 02:12:33 +0200112/*
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +0000113 * `ioctlent[012].h' files are automatically generated by the auxiliary
Denys Vlasenko39fca622011-08-20 02:12:33 +0200114 * program `ioctlsort', such that the list is sorted by the `code' field.
115 * This has the side-effect of resolving the _IO.. macros into
116 * plain integers, eliminating the need to include here everything
117 * in "/usr/include".
118 */
119
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100120const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000121#include "errnoent.h"
122};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100123const char *const signalent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200124#include "signalent.h"
125};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100126const struct_ioctlent ioctlent0[] = {
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +0000127#include "ioctlent0.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200128};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000129
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100130#if SUPPORTED_PERSONALITIES > 1
Roland McGrathee36ce12004-09-04 03:53:10 +0000131static const char *const errnoent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100132# include "errnoent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000133};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200134static const char *const signalent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100135# include "signalent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200136};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100137static const struct_ioctlent ioctlent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100138# include "ioctlent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200139};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200140#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000141
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100142#if SUPPORTED_PERSONALITIES > 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000143static const char *const errnoent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100144# include "errnoent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000145};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200146static const char *const signalent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100147# include "signalent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200148};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100149static const struct_ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100150# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200151};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200152#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000153
Dmitry V. Levine6f55242013-02-26 22:02:30 +0000154enum {
155 nsyscalls0 = ARRAY_SIZE(sysent0)
156#if SUPPORTED_PERSONALITIES > 1
157 , nsyscalls1 = ARRAY_SIZE(sysent1)
158# if SUPPORTED_PERSONALITIES > 2
159 , nsyscalls2 = ARRAY_SIZE(sysent2)
160# endif
161#endif
162};
163
164enum {
165 nerrnos0 = ARRAY_SIZE(errnoent0)
166#if SUPPORTED_PERSONALITIES > 1
167 , nerrnos1 = ARRAY_SIZE(errnoent1)
168# if SUPPORTED_PERSONALITIES > 2
169 , nerrnos2 = ARRAY_SIZE(errnoent2)
170# endif
171#endif
172};
173
174enum {
175 nsignals0 = ARRAY_SIZE(signalent0)
176#if SUPPORTED_PERSONALITIES > 1
177 , nsignals1 = ARRAY_SIZE(signalent1)
178# if SUPPORTED_PERSONALITIES > 2
179 , nsignals2 = ARRAY_SIZE(signalent2)
180# endif
181#endif
182};
183
184enum {
185 nioctlents0 = ARRAY_SIZE(ioctlent0)
186#if SUPPORTED_PERSONALITIES > 1
187 , nioctlents1 = ARRAY_SIZE(ioctlent1)
188# if SUPPORTED_PERSONALITIES > 2
189 , nioctlents2 = ARRAY_SIZE(ioctlent2)
190# endif
191#endif
192};
193
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100194#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100195const struct_sysent *sysent = sysent0;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100196const char *const *errnoent = errnoent0;
197const char *const *signalent = signalent0;
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100198const struct_ioctlent *ioctlent = ioctlent0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100199#endif
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100200unsigned nsyscalls = nsyscalls0;
201unsigned nerrnos = nerrnos0;
202unsigned nsignals = nsignals0;
203unsigned nioctlents = nioctlents0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100204
205unsigned num_quals;
206qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
207
208static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
209 nsyscalls0,
210#if SUPPORTED_PERSONALITIES > 1
211 nsyscalls1,
212#endif
213#if SUPPORTED_PERSONALITIES > 2
214 nsyscalls2,
215#endif
216};
217static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
218 sysent0,
219#if SUPPORTED_PERSONALITIES > 1
220 sysent1,
221#endif
222#if SUPPORTED_PERSONALITIES > 2
223 sysent2,
224#endif
225};
226
227enum {
228 MAX_NSYSCALLS1 = (nsyscalls0
229#if SUPPORTED_PERSONALITIES > 1
230 > nsyscalls1 ? nsyscalls0 : nsyscalls1
231#endif
232 ),
233 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
234#if SUPPORTED_PERSONALITIES > 2
235 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
236#endif
237 ),
238 MAX_NSYSCALLS = MAX_NSYSCALLS2,
239 /* We are ready for arches with up to 255 signals,
240 * even though the largest known signo is on MIPS and it is 128.
241 * The number of existing syscalls on all arches is
242 * larger that 255 anyway, so it is just a pedantic matter.
243 */
244 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
245};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000246
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100247#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100248unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000249
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100250# ifndef current_wordsize
251unsigned current_wordsize;
252static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000253 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000254 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100255# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000256 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100257# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200258};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100259# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000260
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200261void
Dmitry V. Levin3abe8b22006-12-20 22:37:21 +0000262set_personality(int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000263{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100264 nsyscalls = nsyscall_vec[personality];
265 sysent = sysent_vec[personality];
266
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000267 switch (personality) {
268 case 0:
269 errnoent = errnoent0;
270 nerrnos = nerrnos0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000271 ioctlent = ioctlent0;
272 nioctlents = nioctlents0;
273 signalent = signalent0;
274 nsignals = nsignals0;
275 break;
276
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000277 case 1:
278 errnoent = errnoent1;
279 nerrnos = nerrnos1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000280 ioctlent = ioctlent1;
281 nioctlents = nioctlents1;
282 signalent = signalent1;
283 nsignals = nsignals1;
284 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000285
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100286# if SUPPORTED_PERSONALITIES > 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000287 case 2:
288 errnoent = errnoent2;
289 nerrnos = nerrnos2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000290 ioctlent = ioctlent2;
291 nioctlents = nioctlents2;
292 signalent = signalent2;
293 nsignals = nsignals2;
294 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100295# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000296 }
297
298 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100299# ifndef current_wordsize
300 current_wordsize = personality_wordsize[personality];
301# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000302}
303
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000304static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000305update_personality(struct tcb *tcp, unsigned int personality)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000306{
307 if (personality == current_personality)
308 return;
309 set_personality(personality);
310
311 if (personality == tcp->currpers)
312 return;
313 tcp->currpers = personality;
314
H.J. Lu35be5812012-04-16 13:00:01 +0200315# if defined(POWERPC64)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000316 if (!qflag) {
317 static const char *const names[] = {"64 bit", "32 bit"};
318 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
319 tcp->pid, names[personality]);
320 }
H.J. Lu35be5812012-04-16 13:00:01 +0200321# elif defined(X86_64)
322 if (!qflag) {
323 static const char *const names[] = {"64 bit", "32 bit", "x32"};
324 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
325 tcp->pid, names[personality]);
326 }
H.J. Lu085e4282012-04-17 11:05:04 -0700327# elif defined(X32)
328 if (!qflag) {
329 static const char *const names[] = {"x32", "32 bit"};
330 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
331 tcp->pid, names[personality]);
332 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000333# elif defined(AARCH64)
334 if (!qflag) {
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100335 static const char *const names[] = {"32-bit", "AArch64"};
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000336 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
337 tcp->pid, names[personality]);
338 }
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100339# elif defined(TILE)
340 if (!qflag) {
341 static const char *const names[] = {"64-bit", "32-bit"};
342 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
343 tcp->pid, names[personality]);
344 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100345# endif
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000346}
347#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000348
Denys Vlasenkoc1540fe2013-02-21 16:17:08 +0100349static int qual_syscall(), qual_signal(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000350
Roland McGrathe10e62a2004-09-04 04:20:43 +0000351static const struct qual_options {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000352 unsigned int bitflag;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000353 const char *option_name;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000354 int (*qualify)(const char *, int, int);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000355 const char *argument_name;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000356} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000357 { QUAL_TRACE, "trace", qual_syscall, "system call" },
358 { QUAL_TRACE, "t", qual_syscall, "system call" },
359 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
360 { QUAL_ABBREV, "a", qual_syscall, "system call" },
361 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
362 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
363 { QUAL_RAW, "raw", qual_syscall, "system call" },
364 { QUAL_RAW, "x", qual_syscall, "system call" },
365 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
366 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
367 { QUAL_SIGNAL, "s", qual_signal, "signal" },
Roland McGrath9797ceb2002-12-30 10:23:00 +0000368 { QUAL_READ, "read", qual_desc, "descriptor" },
369 { QUAL_READ, "reads", qual_desc, "descriptor" },
370 { QUAL_READ, "r", qual_desc, "descriptor" },
371 { QUAL_WRITE, "write", qual_desc, "descriptor" },
372 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
373 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000374 { 0, NULL, NULL, NULL },
375};
376
Roland McGrath9797ceb2002-12-30 10:23:00 +0000377static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000378reallocate_qual(const unsigned int n)
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100379{
380 unsigned p;
381 qualbits_t *qp;
382 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
383 qp = qual_vec[p] = realloc(qual_vec[p], n * sizeof(qualbits_t));
384 if (!qp)
385 die_out_of_memory();
386 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
387 }
388 num_quals = n;
389}
390
391static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000392qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000393{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000394 int p;
Roland McGrath138c6a32006-01-12 09:50:49 +0000395
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100396 if (num_quals <= n)
397 reallocate_qual(n + 1);
Roland McGrath138c6a32006-01-12 09:50:49 +0000398
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100399 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
400 if (pers == p || pers < 0) {
401 if (not)
402 qual_vec[p][n] &= ~bitflag;
403 else
404 qual_vec[p][n] |= bitflag;
405 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000406 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000407}
408
409static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000410qual_syscall(const char *s, const unsigned int bitflag, const int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000411{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000412 int p;
413 unsigned int i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000414 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000415
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100416 if (*s >= '0' && *s <= '9') {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100417 i = string_to_uint(s);
Denys Vlasenkob43dacd2013-02-23 18:19:28 +0100418 if (i >= MAX_NSYSCALLS)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000419 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000420 qualify_one(i, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000421 return 0;
Roland McGrath48a035f2006-01-12 09:45:56 +0000422 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000423
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100424 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
425 for (i = 0; i < nsyscall_vec[p]; i++) {
426 if (sysent_vec[p][i].sys_name
427 && strcmp(s, sysent_vec[p][i].sys_name) == 0
428 ) {
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000429 qualify_one(i, bitflag, not, p);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100430 rc = 0;
431 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000432 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100433 }
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000434
Roland McGrathfe6b3522005-02-02 04:40:11 +0000435 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000436}
437
438static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000439qual_signal(const char *s, const unsigned int bitflag, const int not)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000440{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000441 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000442
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100443 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000444 int signo = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100445 if (signo < 0 || signo > 255)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000446 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000447 qualify_one(signo, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000448 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000449 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000450 if (strncasecmp(s, "SIG", 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000451 s += 3;
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100452 for (i = 0; i <= NSIG; i++) {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000453 if (strcasecmp(s, signame(i) + 3) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000454 qualify_one(i, bitflag, not, -1);
Roland McGrath76421df2005-02-02 03:51:18 +0000455 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000456 }
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100457 }
Roland McGrath76421df2005-02-02 03:51:18 +0000458 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000459}
460
461static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000462qual_desc(const char *s, const unsigned int bitflag, const int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000463{
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100464 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000465 int desc = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100466 if (desc < 0 || desc > 0x7fff) /* paranoia */
Roland McGrathfe6b3522005-02-02 04:40:11 +0000467 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000468 qualify_one(desc, bitflag, not, -1);
Roland McGrath2b619022003-04-10 18:58:20 +0000469 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000470 }
471 return -1;
472}
473
474static int
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000475lookup_class(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000476{
477 if (strcmp(s, "file") == 0)
478 return TRACE_FILE;
479 if (strcmp(s, "ipc") == 0)
480 return TRACE_IPC;
481 if (strcmp(s, "network") == 0)
482 return TRACE_NETWORK;
483 if (strcmp(s, "process") == 0)
484 return TRACE_PROCESS;
485 if (strcmp(s, "signal") == 0)
486 return TRACE_SIGNAL;
Roland McGrath2fe7b132005-07-05 03:25:35 +0000487 if (strcmp(s, "desc") == 0)
488 return TRACE_DESC;
Namhyung Kim96792962012-10-24 11:41:57 +0900489 if (strcmp(s, "memory") == 0)
490 return TRACE_MEMORY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000491 return -1;
492}
493
494void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000495qualify(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000496{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000497 const struct qual_options *opt;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000498 char *copy;
499 const char *p;
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000500 int not;
501 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000502
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100503 if (num_quals == 0)
504 reallocate_qual(MIN_QUALS);
505
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000506 opt = &qual_options[0];
507 for (i = 0; (p = qual_options[i].option_name); i++) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000508 unsigned int len = strlen(p);
509 if (strncmp(s, p, len) == 0 && s[len] == '=') {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000510 opt = &qual_options[i];
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000511 s += len + 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000512 break;
513 }
514 }
515 not = 0;
516 if (*s == '!') {
517 not = 1;
518 s++;
519 }
520 if (strcmp(s, "none") == 0) {
521 not = 1 - not;
522 s = "all";
523 }
524 if (strcmp(s, "all") == 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100525 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000526 qualify_one(i, opt->bitflag, not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000527 }
528 return;
529 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100530 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000531 qualify_one(i, opt->bitflag, !not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000532 }
Denys Vlasenko5d645812011-08-20 12:48:18 +0200533 copy = strdup(s);
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200534 if (!copy)
535 die_out_of_memory();
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000536 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000537 int n;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000538 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100539 unsigned pers;
540 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
541 for (i = 0; i < nsyscall_vec[pers]; i++)
542 if (sysent_vec[pers][i].sys_flags & n)
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000543 qualify_one(i, opt->bitflag, not, pers);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100544 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000545 continue;
546 }
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000547 if (opt->qualify(p, opt->bitflag, not)) {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100548 error_msg_and_die("invalid %s '%s'",
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000549 opt->argument_name, p);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000550 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000551 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000552 free(copy);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000553 return;
554}
555
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000556#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000557static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000558decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000559{
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000560 unsigned long addr;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100561 unsigned int i, n, size;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000562
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000563 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
564 return;
565
566 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100567 tcp->qual_flg = qual_flags[tcp->scno];
568 tcp->s_ent = &sysent[tcp->scno];
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000569 addr = tcp->u_arg[1];
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100570 size = current_wordsize;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100571 n = tcp->s_ent->nargs;
572 for (i = 0; i < n; ++i) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000573 if (size == sizeof(int)) {
574 unsigned int arg;
575 if (umove(tcp, addr, &arg) < 0)
576 arg = 0;
577 tcp->u_arg[i] = arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000578 }
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000579 else {
580 unsigned long arg;
581 if (umove(tcp, addr, &arg) < 0)
582 arg = 0;
583 tcp->u_arg[i] = arg;
584 }
585 addr += size;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000586 }
587}
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000588#endif
Mike Frysinger3362e892012-03-15 01:09:19 -0400589
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000590#ifdef SYS_ipc_subcall
591static void
592decode_ipc_subcall(struct tcb *tcp)
593{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100594 unsigned int i, n;
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000595
596 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
597 return;
598
599 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100600 tcp->qual_flg = qual_flags[tcp->scno];
601 tcp->s_ent = &sysent[tcp->scno];
602 n = tcp->s_ent->nargs;
603 for (i = 0; i < n; i++)
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000604 tcp->u_arg[i] = tcp->u_arg[i + 1];
605}
606#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000607
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200608int
609printargs(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000610{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200611 if (entering(tcp)) {
612 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100613 int n = tcp->s_ent->nargs;
614 for (i = 0; i < n; i++)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200615 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
616 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000617 return 0;
618}
619
Denys Vlasenko72879c62012-02-27 14:18:02 +0100620int
621printargs_lu(struct tcb *tcp)
622{
623 if (entering(tcp)) {
624 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100625 int n = tcp->s_ent->nargs;
626 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100627 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
628 }
629 return 0;
630}
631
632int
633printargs_ld(struct tcb *tcp)
634{
635 if (entering(tcp)) {
636 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100637 int n = tcp->s_ent->nargs;
638 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100639 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
640 }
641 return 0;
642}
643
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100644#if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200645long
646getrval2(struct tcb *tcp)
647{
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100648 long val;
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200649
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100650# if defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100651 val = sparc_regs.u_regs[U_REG_O1];
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100652# elif defined(SH)
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200653 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200654 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100655# elif defined(IA64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200656 if (upeek(tcp->pid, PT_R9, &val) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200657 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100658# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200659
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200660 return val;
661}
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100662#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200663
Denys Vlasenko523635f2012-02-25 02:44:25 +0100664#if defined(I386)
Denys Vlasenkob51f3642013-07-16 12:06:25 +0200665static struct user_regs_struct i386_regs;
Denys Vlasenkoe600ac62013-07-18 17:40:45 +0200666/* Cast suppresses signedness warning (.esp is long, not unsigned long) */
667uint32_t *const i386_esp_ptr = (uint32_t*)&i386_regs.esp;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000668# define ARCH_REGS_FOR_GETREGSET i386_regs
H.J. Lu35be5812012-04-16 13:00:01 +0200669#elif defined(X86_64) || defined(X32)
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100670/*
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100671 * On i386, pt_regs and user_regs_struct are the same,
672 * but on 64 bit x86, user_regs_struct has six more fields:
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100673 * fs_base, gs_base, ds, es, fs, gs.
674 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
675 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100676struct i386_user_regs_struct {
677 uint32_t ebx;
678 uint32_t ecx;
679 uint32_t edx;
680 uint32_t esi;
681 uint32_t edi;
682 uint32_t ebp;
683 uint32_t eax;
684 uint32_t xds;
685 uint32_t xes;
686 uint32_t xfs;
687 uint32_t xgs;
688 uint32_t orig_eax;
689 uint32_t eip;
690 uint32_t xcs;
691 uint32_t eflags;
692 uint32_t esp;
693 uint32_t xss;
694};
695static union {
696 struct user_regs_struct x86_64_r;
697 struct i386_user_regs_struct i386_r;
698} x86_regs_union;
699# define x86_64_regs x86_regs_union.x86_64_r
700# define i386_regs x86_regs_union.i386_r
Denys Vlasenkob51f3642013-07-16 12:06:25 +0200701uint32_t *const i386_esp_ptr = &i386_regs.esp;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100702static struct iovec x86_io = {
703 .iov_base = &x86_regs_union
704};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100705#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +0200706bool ia64_ia32mode = 0; /* not static */
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100707static long ia64_r8, ia64_r10;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100708#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +0200709struct pt_regs ppc_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100710#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100711static long m68k_d0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100712#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100713static long bfin_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100714#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100715struct pt_regs arm_regs; /* not static */
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000716# define ARCH_REGS_FOR_GETREGSET arm_regs
Steve McIntyred8d3bd32012-10-24 17:58:16 +0100717#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100718static union {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100719 struct user_pt_regs aarch64_r;
720 struct arm_pt_regs arm_r;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100721} arm_regs_union;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100722# define aarch64_regs arm_regs_union.aarch64_r
723# define arm_regs arm_regs_union.arm_r
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100724static struct iovec aarch64_io = {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100725 .iov_base = &arm_regs_union
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100726};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100727#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100728static long alpha_r0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100729static long alpha_a3;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100730#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100731static struct pt_regs avr32_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100732#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100733struct pt_regs sparc_regs; /* not static */
Denys Vlasenko523635f2012-02-25 02:44:25 +0100734#elif defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100735static long long mips_a3;
736static long long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100737#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100738static long mips_a3;
739static long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100740#elif defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +0200741static long s390_gpr2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100742#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100743static long hppa_r28;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100744#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100745static long sh_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100746#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100747static long sh64_r9;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100748#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100749static long cris_r10;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100750#elif defined(TILE)
751struct pt_regs tile_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100752#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100753static long microblaze_r3;
Christian Svensson492f81f2013-02-14 13:26:27 +0100754#elif defined(OR1K)
755static struct user_regs_struct or1k_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000756# define ARCH_REGS_FOR_GETREGSET or1k_regs
James Hogan5f999a82013-02-22 14:44:10 +0000757#elif defined(METAG)
758static struct user_gp_regs metag_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000759# define ARCH_REGS_FOR_GETREGSET metag_regs
Chris Zankel8f636ed2013-03-25 10:22:07 -0700760#elif defined(XTENSA)
761static long xtensa_a2;
Vineet Gupta7daacbb2013-08-16 12:47:06 +0530762# elif defined(ARC)
763static struct user_regs_struct arc_regs;
764# define ARCH_REGS_FOR_GETREGSET arc_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +0100765#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000766
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100767void
Denys Vlasenko5a2483b2013-07-01 12:49:14 +0200768print_pc(struct tcb *tcp)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100769{
770#define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
771 sizeof(long) == 8 ? "[????????????????] " : \
772 NULL /* crash */)
773 if (get_regs_error) {
774 PRINTBADPC;
775 return;
776 }
777#if defined(I386)
778 tprintf("[%08lx] ", i386_regs.eip);
779#elif defined(S390) || defined(S390X)
780 long psw;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200781 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100782 PRINTBADPC;
783 return;
784 }
785# ifdef S390
786 tprintf("[%08lx] ", psw);
787# elif S390X
Dmitry V. Levinddba73e2013-02-05 19:01:58 +0000788 tprintf("[%016lx] ", psw);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100789# endif
790#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100791 if (x86_io.iov_len == sizeof(i386_regs)) {
792 tprintf("[%08x] ", (unsigned) i386_regs.eip);
793 } else {
794# if defined(X86_64)
795 tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
796# elif defined(X32)
797 /* Note: this truncates 64-bit rip to 32 bits */
798 tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip);
799# endif
800 }
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100801#elif defined(IA64)
802 long ip;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200803 if (upeek(tcp->pid, PT_B0, &ip) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100804 PRINTBADPC;
805 return;
806 }
807 tprintf("[%08lx] ", ip);
808#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +0200809 long pc = ppc_regs.nip;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100810# ifdef POWERPC64
811 tprintf("[%016lx] ", pc);
812# else
813 tprintf("[%08lx] ", pc);
814# endif
815#elif defined(M68K)
816 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200817 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100818 tprints("[????????] ");
819 return;
820 }
821 tprintf("[%08lx] ", pc);
822#elif defined(ALPHA)
823 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200824 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100825 tprints("[????????????????] ");
826 return;
827 }
828 tprintf("[%08lx] ", pc);
829#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100830 tprintf("[%08lx] ", sparc_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100831#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100832 tprintf("[%08lx] ", sparc_regs.tpc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100833#elif defined(HPPA)
834 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200835 if (upeek(tcp->pid, PT_IAOQ0, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100836 tprints("[????????] ");
837 return;
838 }
839 tprintf("[%08lx] ", pc);
840#elif defined(MIPS)
841 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200842 if (upeek(tcp->pid, REG_EPC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100843 tprints("[????????] ");
844 return;
845 }
846 tprintf("[%08lx] ", pc);
847#elif defined(SH)
848 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200849 if (upeek(tcp->pid, 4*REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100850 tprints("[????????] ");
851 return;
852 }
853 tprintf("[%08lx] ", pc);
854#elif defined(SH64)
855 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200856 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100857 tprints("[????????????????] ");
858 return;
859 }
860 tprintf("[%08lx] ", pc);
861#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100862 tprintf("[%08lx] ", arm_regs.ARM_pc);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100863#elif defined(AARCH64)
864 /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100865#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100866 tprintf("[%08lx] ", avr32_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100867#elif defined(BFIN)
868 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200869 if (upeek(tcp->pid, PT_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100870 PRINTBADPC;
871 return;
872 }
873 tprintf("[%08lx] ", pc);
874#elif defined(CRISV10)
875 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200876 if (upeek(tcp->pid, 4*PT_IRP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100877 PRINTBADPC;
878 return;
879 }
880 tprintf("[%08lx] ", pc);
881#elif defined(CRISV32)
882 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200883 if (upeek(tcp->pid, 4*PT_ERP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100884 PRINTBADPC;
885 return;
886 }
887 tprintf("[%08lx] ", pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100888#elif defined(TILE)
889# ifdef _LP64
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500890 tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100891# else
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500892 tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100893# endif
Christian Svensson492f81f2013-02-14 13:26:27 +0100894#elif defined(OR1K)
895 tprintf("[%08lx] ", or1k_regs.pc);
James Hogan5f999a82013-02-22 14:44:10 +0000896#elif defined(METAG)
897 tprintf("[%08lx] ", metag_regs.pc);
Chris Zankel8f636ed2013-03-25 10:22:07 -0700898#elif defined(XTENSA)
899 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200900 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Chris Zankel8f636ed2013-03-25 10:22:07 -0700901 PRINTBADPC;
902 return;
903 }
904 tprintf("[%08lx] ", pc);
Vineet Gupta7daacbb2013-08-16 12:47:06 +0530905#elif defined(ARC)
906 tprintf("[%08lx] ", arc_regs.efa);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100907#endif /* architecture */
908}
909
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000910/*
911 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
912 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
Denys Vlasenko7270de52013-02-21 15:46:34 +0100913 */
Elliott Hughes7a28f7f2014-03-03 23:45:26 +0000914#if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
Denys Vlasenko7270de52013-02-21 15:46:34 +0100915static long
916shuffle_scno(unsigned long scno)
917{
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000918 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
Denys Vlasenko7270de52013-02-21 15:46:34 +0100919 return scno;
920
921 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000922 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
Denys Vlasenko7270de52013-02-21 15:46:34 +0100923 return 0x000ffff0;
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000924 if (scno == 0x000ffff0)
925 return ARM_FIRST_SHUFFLED_SYSCALL;
Denys Vlasenko7270de52013-02-21 15:46:34 +0100926
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000927#define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
928 /*
929 * Is it ARM specific syscall?
930 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
931 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
Denys Vlasenko7270de52013-02-21 15:46:34 +0100932 */
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000933 if (scno >= 0x000f0000 &&
934 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
935 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
Denys Vlasenko7270de52013-02-21 15:46:34 +0100936 }
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000937 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
938 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
Denys Vlasenko7270de52013-02-21 15:46:34 +0100939 }
940
941 return scno;
942}
943#else
944# define shuffle_scno(scno) ((long)(scno))
945#endif
946
947static char*
948undefined_scno_name(struct tcb *tcp)
949{
950 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
951
952 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
953 return buf;
954}
955
Anton Blanchard14d51a62013-06-26 14:42:37 +1000956#ifdef POWERPC
957/*
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200958 * PTRACE_GETREGS was added to the PowerPC kernel in v2.6.23,
959 * we provide a slow fallback for old kernels.
Anton Blanchard14d51a62013-06-26 14:42:37 +1000960 */
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200961static int powerpc_getregs_old(pid_t pid)
Anton Blanchard14d51a62013-06-26 14:42:37 +1000962{
963 int i;
964 long r;
965
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200966 if (iflag) {
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200967 r = upeek(pid, sizeof(long) * PT_NIP, (long *)&ppc_regs.nip);
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200968 if (r)
969 goto out;
970 }
971#ifdef POWERPC64 /* else we never use it */
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200972 r = upeek(pid, sizeof(long) * PT_MSR, (long *)&ppc_regs.msr);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000973 if (r)
974 goto out;
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200975#endif
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200976 r = upeek(pid, sizeof(long) * PT_CCR, (long *)&ppc_regs.ccr);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000977 if (r)
978 goto out;
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200979 r = upeek(pid, sizeof(long) * PT_ORIG_R3, (long *)&ppc_regs.orig_gpr3);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000980 if (r)
981 goto out;
Anton Blanchard14d51a62013-06-26 14:42:37 +1000982 for (i = 0; i <= 8; i++) {
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200983 r = upeek(pid, sizeof(long) * (PT_R0 + i),
984 (long *)&ppc_regs.gpr[i]);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000985 if (r)
986 goto out;
987 }
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200988 out:
Anton Blanchard14d51a62013-06-26 14:42:37 +1000989 return r;
990}
991#endif
992
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100993#ifndef get_regs
994long get_regs_error;
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +0000995
996#if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +0000997static void get_regset(pid_t pid)
998{
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000999/* constant iovec */
1000# if defined(ARM) \
1001 || defined(I386) \
1002 || defined(METAG) \
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301003 || defined(OR1K) \
1004 || defined(ARC)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001005 static struct iovec io = {
1006 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1007 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001008 };
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001009 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1010
1011/* variable iovec */
1012# elif defined(X86_64) || defined(X32)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001013 /* x86_io.iov_base = &x86_regs_union; - already is */
1014 x86_io.iov_len = sizeof(x86_regs_union);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001015 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &x86_io);
1016# elif defined(AARCH64)
1017 /* aarch64_io.iov_base = &arm_regs_union; - already is */
1018 aarch64_io.iov_len = sizeof(arm_regs_union);
1019 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &aarch64_io);
1020# else
1021# warning both PTRACE_GETREGSET and NT_PRSTATUS are available but not yet used
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001022# endif
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001023}
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001024#endif /* PTRACE_GETREGSET && NT_PRSTATUS */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001025
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001026void
1027get_regs(pid_t pid)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001028{
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001029/* PTRACE_GETREGSET only */
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301030# if defined(METAG) || defined(OR1K) || defined(X32) || defined(AARCH64) || defined(ARC)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001031 get_regset(pid);
1032
1033/* PTRACE_GETREGS only */
1034# elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001035 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001036# elif defined(TILE)
1037 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &tile_regs);
1038# elif defined(SPARC) || defined(SPARC64)
1039 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001040# elif defined(POWERPC)
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001041 static bool old_kernel = 0;
1042 if (old_kernel)
1043 goto old;
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001044 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &ppc_regs);
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001045 if (get_regs_error && errno == EIO) {
1046 old_kernel = 1;
1047 old:
Anton Blanchard14d51a62013-06-26 14:42:37 +10001048 get_regs_error = powerpc_getregs_old(pid);
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001049 }
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001050
1051/* try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS */
1052# else
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001053# if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
1054 static int getregset_support;
1055
1056 if (getregset_support >= 0) {
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001057 get_regset(pid);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001058 if (getregset_support > 0)
1059 return;
1060 if (get_regs_error >= 0) {
1061 getregset_support = 1;
1062 return;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001063 }
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001064 if (errno == EPERM || errno == ESRCH)
1065 return;
1066 getregset_support = -1;
1067 }
1068# endif /* PTRACE_GETREGSET && NT_PRSTATUS */
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001069# if defined(ARM)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001070 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &arm_regs);
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001071# elif defined(I386)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001072 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &i386_regs);
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001073# elif defined(X86_64)
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001074 /* Use old method, with unreliable heuristical detection of 32-bitness. */
1075 x86_io.iov_len = sizeof(x86_64_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001076 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &x86_64_regs);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001077 if (!get_regs_error && x86_64_regs.cs == 0x23) {
1078 x86_io.iov_len = sizeof(i386_regs);
1079 /*
1080 * The order is important: i386_regs and x86_64_regs
1081 * are overlaid in memory!
1082 */
1083 i386_regs.ebx = x86_64_regs.rbx;
1084 i386_regs.ecx = x86_64_regs.rcx;
1085 i386_regs.edx = x86_64_regs.rdx;
1086 i386_regs.esi = x86_64_regs.rsi;
1087 i386_regs.edi = x86_64_regs.rdi;
1088 i386_regs.ebp = x86_64_regs.rbp;
1089 i386_regs.eax = x86_64_regs.rax;
1090 /* i386_regs.xds = x86_64_regs.ds; unused by strace */
1091 /* i386_regs.xes = x86_64_regs.es; ditto... */
1092 /* i386_regs.xfs = x86_64_regs.fs; */
1093 /* i386_regs.xgs = x86_64_regs.gs; */
1094 i386_regs.orig_eax = x86_64_regs.orig_rax;
1095 i386_regs.eip = x86_64_regs.rip;
1096 /* i386_regs.xcs = x86_64_regs.cs; */
1097 /* i386_regs.eflags = x86_64_regs.eflags; */
1098 i386_regs.esp = x86_64_regs.rsp;
1099 /* i386_regs.xss = x86_64_regs.ss; */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001100 }
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001101# else
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001102# error unhandled architecture
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001103# endif /* ARM || I386 || X86_64 */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001104# endif
1105}
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001106#endif /* !get_regs */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001107
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001108/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001109 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1110 * 1: ok, continue in trace_syscall_entering().
1111 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001112 * ("????" etc) and bail out.
1113 */
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01001114static int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001115get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001116{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001117 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001118
Denys Vlasenko523635f2012-02-25 02:44:25 +01001119#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001120 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001121 return -1;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001122
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001123 if (s390_gpr2 != -ENOSYS) {
Roland McGrath2f924ca2003-06-26 22:23:28 +00001124 /*
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001125 * Since kernel version 2.5.44 the scno gets passed in gpr2.
Roland McGrath2f924ca2003-06-26 22:23:28 +00001126 */
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001127 scno = s390_gpr2;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001128 } else {
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001129 /*
Michal Ludvig882eda82002-11-11 12:50:47 +00001130 * Old style of "passing" the scno via the SVC instruction.
1131 */
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001132 long psw;
Michal Ludvig882eda82002-11-11 12:50:47 +00001133 long opcode, offset_reg, tmp;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001134 void *svc_addr;
Denys Vlasenko7c9ba8b2011-08-19 19:46:32 +02001135 static const int gpr_offset[16] = {
1136 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
1137 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
1138 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
1139 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
1140 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001141
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001142 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0)
Michal Ludvig882eda82002-11-11 12:50:47 +00001143 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001144 errno = 0;
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001145 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +00001146 if (errno) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001147 perror_msg("peektext(psw-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +00001148 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001149 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001150
1151 /*
1152 * We have to check if the SVC got executed directly or via an
1153 * EXECUTE instruction. In case of EXECUTE it is necessary to do
1154 * instruction decoding to derive the system call number.
1155 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1156 * so that this doesn't work if a SVC opcode is part of an EXECUTE
1157 * opcode. Since there is no way to find out the opcode size this
1158 * is the best we can do...
1159 */
Michal Ludvig882eda82002-11-11 12:50:47 +00001160 if ((opcode & 0xff00) == 0x0a00) {
1161 /* SVC opcode */
1162 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +00001163 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001164 else {
1165 /* SVC got executed by EXECUTE instruction */
1166
1167 /*
1168 * Do instruction decoding of EXECUTE. If you really want to
1169 * understand this, read the Principles of Operations.
1170 */
1171 svc_addr = (void *) (opcode & 0xfff);
1172
1173 tmp = 0;
1174 offset_reg = (opcode & 0x000f0000) >> 16;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001175 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001176 return -1;
1177 svc_addr += tmp;
1178
1179 tmp = 0;
1180 offset_reg = (opcode & 0x0000f000) >> 12;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001181 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001182 return -1;
1183 svc_addr += tmp;
1184
Denys Vlasenkofb036672009-01-23 16:30:26 +00001185 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
Michal Ludvig882eda82002-11-11 12:50:47 +00001186 if (errno)
1187 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001188# if defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +00001189 scno >>= 48;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001190# else
Michal Ludvig882eda82002-11-11 12:50:47 +00001191 scno >>= 16;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001192# endif
Michal Ludvig882eda82002-11-11 12:50:47 +00001193 tmp = 0;
1194 offset_reg = (opcode & 0x00f00000) >> 20;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001195 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001196 return -1;
1197
1198 scno = (scno | tmp) & 0xff;
1199 }
1200 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001201#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001202 scno = ppc_regs.gpr[0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001203# ifdef POWERPC64
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001204 unsigned int currpers;
Andreas Schwabd69fa492010-07-12 21:39:57 +02001205
James Yanga78d7772014-02-18 15:32:43 -06001206 /*
1207 * Check for 64/32 bit mode.
1208 * Embedded implementations covered by Book E extension of PPC use
1209 * bit 0 (CM) of 32-bit Machine state register (MSR).
1210 * Other implementations use bit 0 (SF) of 64-bit MSR.
1211 */
1212 currpers = (ppc_regs.msr & 0x8000000080000000) ? 0 : 1;
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001213 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001214# endif
1215#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001216 scno = avr32_regs.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001217#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001218 if (upeek(tcp->pid, PT_ORIG_P0, &scno))
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001219 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001220#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001221 scno = i386_regs.orig_eax;
H.J. Lu35be5812012-04-16 13:00:01 +02001222#elif defined(X86_64) || defined(X32)
1223# ifndef __X32_SYSCALL_BIT
1224# define __X32_SYSCALL_BIT 0x40000000
1225# endif
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001226 unsigned int currpers;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001227# if 1
1228 /* GETREGSET of NT_PRSTATUS tells us regset size,
1229 * which unambiguously detects i386.
1230 *
1231 * Linux kernel distinguishes x86-64 and x32 processes
1232 * solely by looking at __X32_SYSCALL_BIT:
1233 * arch/x86/include/asm/compat.h::is_x32_task():
1234 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1235 * return true;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001236 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001237 if (x86_io.iov_len == sizeof(i386_regs)) {
1238 scno = i386_regs.orig_eax;
1239 currpers = 1;
1240 } else {
1241 scno = x86_64_regs.orig_rax;
1242 currpers = 0;
1243 if (scno & __X32_SYSCALL_BIT) {
Dmitry V. Levinc3948322015-02-05 16:15:46 +00001244 /*
1245 * Syscall number -1 requires special treatment:
1246 * it might be a side effect of SECCOMP_RET_ERRNO
1247 * filtering that sets orig_rax to -1
1248 * in some versions of linux kernel.
1249 * If that is the case, then
1250 * __X32_SYSCALL_BIT logic does not apply.
1251 */
1252 if ((long long) x86_64_regs.orig_rax != -1) {
1253 scno -= __X32_SYSCALL_BIT;
1254 currpers = 2;
1255 } else {
1256# ifdef X32
1257 currpers = 2;
1258# endif
1259 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001260 }
1261 }
1262# elif 0
1263 /* cs = 0x33 for long mode (native 64 bit and x32)
1264 * cs = 0x23 for compatibility mode (32 bit)
1265 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1266 */
1267 scno = x86_64_regs.orig_rax;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001268 switch (x86_64_regs.cs) {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001269 case 0x23: currpers = 1; break;
H.J. Lu35be5812012-04-16 13:00:01 +02001270 case 0x33:
1271 if (x86_64_regs.ds == 0x2b) {
1272 currpers = 2;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001273 scno &= ~__X32_SYSCALL_BIT;
H.J. Lu35be5812012-04-16 13:00:01 +02001274 } else
1275 currpers = 0;
1276 break;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001277 default:
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001278 fprintf(stderr, "Unknown value CS=0x%08X while "
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001279 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001280 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001281 currpers = current_personality;
1282 break;
1283 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001284# elif 0
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001285 /* This version analyzes the opcode of a syscall instruction.
1286 * (int 0x80 on i386 vs. syscall on x86-64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001287 * It works, but is too complicated, and strictly speaking, unreliable.
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001288 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001289 unsigned long call, rip = x86_64_regs.rip;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001290 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1291 rip -= 2;
1292 errno = 0;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001293 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001294 if (errno)
1295 fprintf(stderr, "ptrace_peektext failed: %s\n",
1296 strerror(errno));
1297 switch (call & 0xffff) {
1298 /* x86-64: syscall = 0x0f 0x05 */
1299 case 0x050f: currpers = 0; break;
1300 /* i386: int 0x80 = 0xcd 0x80 */
1301 case 0x80cd: currpers = 1; break;
1302 default:
1303 currpers = current_personality;
1304 fprintf(stderr,
1305 "Unknown syscall opcode (0x%04X) while "
1306 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001307 "PID=%d\n", (int)call, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001308 break;
1309 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001310# endif
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001311
H.J. Lu35be5812012-04-16 13:00:01 +02001312# ifdef X32
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001313 /* If we are built for a x32 system, then personality 0 is x32
1314 * (not x86_64), and stracing of x86_64 apps is not supported.
1315 * Stracing of i386 apps is still supported.
H.J. Lu085e4282012-04-17 11:05:04 -07001316 */
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001317 if (currpers == 0) {
1318 fprintf(stderr, "syscall_%lu(...) in unsupported "
1319 "64-bit mode of process PID=%d\n",
1320 scno, tcp->pid);
1321 return 0;
H.J. Lu35be5812012-04-16 13:00:01 +02001322 }
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001323 currpers &= ~2; /* map 2,1 to 0,1 */
H.J. Lu35be5812012-04-16 13:00:01 +02001324# endif
H.J. Lu085e4282012-04-17 11:05:04 -07001325 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001326#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001327# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01001328 long psr;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001329 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001330 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
1331 if (ia64_ia32mode) {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001332 if (upeek(tcp->pid, PT_R1, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001333 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001334 } else {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001335 if (upeek(tcp->pid, PT_R15, &scno) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001336 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001337 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001338#elif defined(AARCH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001339 switch (aarch64_io.iov_len) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001340 case sizeof(aarch64_regs):
1341 /* We are in 64-bit mode */
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001342 scno = aarch64_regs.regs[8];
1343 update_personality(tcp, 1);
1344 break;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001345 case sizeof(arm_regs):
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001346 /* We are in 32-bit mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001347 /* Note: we don't support OABI, unlike 32-bit ARM build */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001348 scno = arm_regs.ARM_r7;
Elliott Hughes7a28f7f2014-03-03 23:45:26 +00001349 scno = shuffle_scno(scno);
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001350 update_personality(tcp, 0);
1351 break;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001352 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001353#elif defined(ARM)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001354 if (arm_regs.ARM_ip != 0) {
1355 /* It is not a syscall entry */
1356 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001357 tcp->flags |= TCB_INSYSCALL;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001358 return 0;
1359 }
1360 /* Note: we support only 32-bit CPUs, not 26-bit */
1361
Dmitry V. Levin0160e162014-03-18 23:37:43 +00001362# if !defined(__ARM_EABI__) || ENABLE_ARM_OABI
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001363 if (arm_regs.ARM_cpsr & 0x20)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001364 /* Thumb mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001365 goto scno_in_r7;
1366 /* ARM mode */
1367 /* Check EABI/OABI by examining SVC insn's low 24 bits */
1368 errno = 0;
1369 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1370 if (errno)
1371 return -1;
1372 /* EABI syscall convention? */
Dmitry V. Levin03b44542015-01-14 07:21:36 +00001373 if ((unsigned long) scno != 0xef000000) {
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001374 /* No, it's OABI */
1375 if ((scno & 0x0ff00000) != 0x0f900000) {
1376 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1377 tcp->pid, scno);
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001378 return -1;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001379 }
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001380 /* Fixup the syscall number */
1381 scno &= 0x000fffff;
1382 } else {
1383 scno_in_r7:
1384 scno = arm_regs.ARM_r7;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001385 }
Dmitry V. Levin0160e162014-03-18 23:37:43 +00001386# else /* __ARM_EABI__ || !ENABLE_ARM_OABI */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001387 scno = arm_regs.ARM_r7;
1388# endif
Denys Vlasenko7270de52013-02-21 15:46:34 +01001389 scno = shuffle_scno(scno);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001390#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001391 if (upeek(tcp->pid, 4*PT_ORIG_D0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001392 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001393#elif defined(LINUX_MIPSN32)
Roland McGrath542c2c62008-05-20 01:11:56 +00001394 unsigned long long regs[38];
1395
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001396 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001397 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001398 mips_a3 = regs[REG_A3];
1399 mips_r2 = regs[REG_V0];
Roland McGrath542c2c62008-05-20 01:11:56 +00001400
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001401 scno = mips_r2;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001402 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001403 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001404 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001405 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001406 return 0;
1407 }
Roland McGrath542c2c62008-05-20 01:11:56 +00001408 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001409#elif defined(MIPS)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001410 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001411 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001412 if (upeek(tcp->pid, REG_V0, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001413 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001414
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001415 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001416 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001417 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001418 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001419 return 0;
1420 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001421 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001422#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001423 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001424 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001425 if (upeek(tcp->pid, REG_R0, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001426 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001427
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001428 /*
1429 * Do some sanity checks to figure out if it's
1430 * really a syscall entry
1431 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001432 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001433 if (alpha_a3 == 0 || alpha_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001434 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001435 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001436 return 0;
1437 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001438 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001439#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001440 /* Disassemble the syscall trap. */
1441 /* Retrieve the syscall trap instruction. */
Denys Vlasenko46455822013-02-05 17:02:59 +01001442 unsigned long trap;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001443 errno = 0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001444# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001445 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001446 trap >>= 32;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001447# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001448 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001449# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001450 if (errno)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001451 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001452
1453 /* Disassemble the trap to see what personality to use. */
1454 switch (trap) {
1455 case 0x91d02010:
1456 /* Linux/SPARC syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001457 update_personality(tcp, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001458 break;
1459 case 0x91d0206d:
1460 /* Linux/SPARC64 syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001461 update_personality(tcp, 2);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001462 break;
1463 case 0x91d02000:
1464 /* SunOS syscall trap. (pers 1) */
1465 fprintf(stderr, "syscall: SunOS no support\n");
1466 return -1;
1467 case 0x91d02008:
1468 /* Solaris 2.x syscall trap. (per 2) */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001469 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001470 break;
1471 case 0x91d02009:
1472 /* NetBSD/FreeBSD syscall trap. */
1473 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1474 return -1;
1475 case 0x91d02027:
1476 /* Solaris 2.x gettimeofday */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001477 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001478 break;
1479 default:
Denys Vlasenko523635f2012-02-25 02:44:25 +01001480# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001481 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001482# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001483 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001484# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001485 return -1;
1486 }
1487
1488 /* Extract the system call number from the registers. */
1489 if (trap == 0x91d02027)
1490 scno = 156;
1491 else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001492 scno = sparc_regs.u_regs[U_REG_G1];
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001493 if (scno == 0) {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001494 scno = sparc_regs.u_regs[U_REG_O0];
1495 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 +02001496 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001497#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001498 if (upeek(tcp->pid, PT_GR20, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001499 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001500#elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001501 /*
1502 * In the new syscall ABI, the system call number is in R3.
1503 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001504 if (upeek(tcp->pid, 4*(REG_REG0+3), &scno) < 0)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001505 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001506
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001507 if (scno < 0) {
1508 /* Odd as it may seem, a glibc bug has been known to cause
1509 glibc to issue bogus negative syscall numbers. So for
1510 our purposes, make strace print what it *should* have been */
1511 long correct_scno = (scno & 0xff);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001512 if (debug_flag)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001513 fprintf(stderr,
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001514 "Detected glibc bug: bogus system call"
1515 " number = %ld, correcting to %ld\n",
1516 scno,
1517 correct_scno);
1518 scno = correct_scno;
1519 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001520#elif defined(SH64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001521 if (upeek(tcp->pid, REG_SYSCALL, &scno) < 0)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001522 return -1;
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001523 scno &= 0xFFFF;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001524#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001525 if (upeek(tcp->pid, 4*PT_R9, &scno) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001526 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001527#elif defined(TILE)
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001528 unsigned int currpers;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001529 scno = tile_regs.regs[10];
1530# ifdef __tilepro__
1531 currpers = 1;
1532# else
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001533# ifndef PT_FLAGS_COMPAT
1534# define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1535# endif
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001536 if (tile_regs.flags & PT_FLAGS_COMPAT)
1537 currpers = 1;
1538 else
1539 currpers = 0;
1540# endif
1541 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001542#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001543 if (upeek(tcp->pid, 0, &scno) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001544 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001545#elif defined(OR1K)
1546 scno = or1k_regs.gpr[11];
James Hogan5f999a82013-02-22 14:44:10 +00001547#elif defined(METAG)
1548 scno = metag_regs.dx[0][1]; /* syscall number in D1Re0 (D1.0) */
Chris Zankel8f636ed2013-03-25 10:22:07 -07001549#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001550 if (upeek(tcp->pid, SYSCALL_NR, &scno) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001551 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301552# elif defined(ARC)
1553 scno = arc_regs.scratch.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001554#endif
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001555
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001556 tcp->scno = scno;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001557 if (SCNO_IS_VALID(tcp->scno)) {
1558 tcp->s_ent = &sysent[scno];
1559 tcp->qual_flg = qual_flags[scno];
1560 } else {
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01001561 static const struct_sysent unknown = {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001562 .nargs = MAX_ARGS,
1563 .sys_flags = 0,
1564 .sys_func = printargs,
1565 .sys_name = "unknown", /* not used */
1566 };
1567 tcp->s_ent = &unknown;
1568 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1569 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001570 return 1;
1571}
1572
Dmitry V. Levind70c41d2015-02-05 23:43:06 +00001573/*
1574 * Cannot rely on __kernel_[u]long_t being defined,
1575 * it is quite a recent feature of <asm/posix_types.h>.
1576 */
1577#ifdef __kernel_long_t
1578typedef __kernel_long_t kernel_long_t;
1579typedef __kernel_ulong_t kernel_ulong_t;
1580#else
1581# ifdef X32
1582typedef long long kernel_long_t;
1583typedef unsigned long long kernel_ulong_t;
1584# else
1585typedef long kernel_long_t;
1586typedef unsigned long kernel_ulong_t;
1587# endif
1588#endif
1589
1590/*
1591 * Check the syscall return value register value for whether it is
1592 * a negated errno code indicating an error, or a success return value.
1593 */
1594static inline bool
1595is_negated_errno(kernel_ulong_t val)
1596{
Dmitry V. Levin09460522015-02-05 23:47:36 +00001597 /*
1598 * Thanks to SECCOMP_RET_DATA == 0xffff, abnormally large errno
1599 * values could be easily seen when a seccomp filter is used, e.g.
1600 * BPF_STMT(BPF_RET, SECCOMP_RET_ERRNO | SECCOMP_RET_DATA)
1601 */
1602 kernel_ulong_t max = -(kernel_long_t) 0x10000; /* SECCOMP_RET_DATA + 1 */
Dmitry V. Levind70c41d2015-02-05 23:43:06 +00001603
1604#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1605 if (current_wordsize < sizeof(val)) {
1606 val = (uint32_t) val;
1607 max = (uint32_t) max;
1608 }
1609#elif defined X32
1610 /*
1611 * current_wordsize is 4 even in personality 0 (native X32)
1612 * but truncation _must not_ be done in it.
1613 * can't check current_wordsize here!
1614 */
1615 if (current_personality != 0) {
1616 val = (uint32_t) val;
1617 max = (uint32_t) max;
1618 }
1619#endif
1620
1621 return val > max;
1622}
1623
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02001624/* Called at each syscall entry.
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001625 * Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001626 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1627 * 1: ok, continue in trace_syscall_entering().
1628 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001629 * ("????" etc) and bail out.
1630 */
Roland McGratha4d48532005-06-08 20:45:28 +00001631static int
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001632syscall_fixup_on_sysenter(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001633{
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001634 /* Do we have post-execve SIGTRAP suppressed? */
1635 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1636 return 1;
1637
Dmitry V. Levin8e398b62015-02-05 19:31:13 +00001638 /*
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001639 * No, unfortunately. Apply -ENOSYS heuristics.
1640 * We don't have to workaround SECCOMP_RET_ERRNO side effects
1641 * because any kernel with SECCOMP_RET_ERRNO support surely
1642 * implements PTRACE_O_TRACEEXEC.
Dmitry V. Levin8e398b62015-02-05 19:31:13 +00001643 */
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001644#if defined(I386)
1645 if (i386_regs.eax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001646 if (debug_flag)
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001647 fprintf(stderr, "not a syscall entry (eax = %ld)\n",
1648 i386_regs.eax);
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001649 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001650 }
H.J. Lu35be5812012-04-16 13:00:01 +02001651#elif defined(X86_64) || defined(X32)
Dmitry V. Levin8e398b62015-02-05 19:31:13 +00001652 if (x86_io.iov_len == sizeof(i386_regs)) {
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001653 if ((int) i386_regs.eax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001654 if (debug_flag)
Dmitry V. Levin8e398b62015-02-05 19:31:13 +00001655 fprintf(stderr,
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001656 "not a syscall entry (eax = %d)\n",
1657 (int) i386_regs.eax);
Dmitry V. Levin8e398b62015-02-05 19:31:13 +00001658 return 0;
1659 }
1660 } else {
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001661 if ((long long) x86_64_regs.rax != -ENOSYS) {
Dmitry V. Levin8e398b62015-02-05 19:31:13 +00001662 if (debug_flag)
1663 fprintf(stderr,
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001664 "not a syscall entry (rax = %lld)\n",
1665 (long long) x86_64_regs.rax);
Denys Vlasenko18beb982011-08-24 16:59:23 +02001666 return 0;
1667 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001668 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001669#elif defined(M68K)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001670 /* TODO? Eliminate upeek's in arches below like we did in x86 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001671 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001672 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001673 if (m68k_d0 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001674 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001675 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001676 return 0;
1677 }
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001678#elif defined(IA64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001679 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001680 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001681 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001682 return -1;
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001683 if (ia64_ia32mode && ia64_r8 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001684 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001685 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001686 return 0;
1687 }
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001688#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001689 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001690 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001691 if (cris_r10 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001692 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001693 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001694 return 0;
1695 }
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001696#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001697 if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001698 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001699 if (microblaze_r3 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001700 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001701 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001702 return 0;
1703 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001704#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001705 return 1;
1706}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001707
Denys Vlasenko146b9442012-03-18 22:10:48 +01001708static void
1709internal_fork(struct tcb *tcp)
1710{
1711#if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1712# define ARG_FLAGS 1
1713#else
1714# define ARG_FLAGS 0
1715#endif
1716#ifndef CLONE_UNTRACED
1717# define CLONE_UNTRACED 0x00800000
1718#endif
1719 if ((ptrace_setoptions
1720 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1721 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1722 return;
1723
1724 if (!followfork)
1725 return;
1726
1727 if (entering(tcp)) {
1728 /*
1729 * We won't see the new child if clone is called with
1730 * CLONE_UNTRACED, so we keep the same logic with that option
1731 * and don't trace it.
1732 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001733 if ((tcp->s_ent->sys_func == sys_clone)
1734 && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
1735 )
Denys Vlasenko146b9442012-03-18 22:10:48 +01001736 return;
1737 setbpt(tcp);
1738 } else {
1739 if (tcp->flags & TCB_BPTSET)
1740 clearbpt(tcp);
1741 }
1742}
1743
1744#if defined(TCB_WAITEXECVE)
1745static void
1746internal_exec(struct tcb *tcp)
1747{
1748 /* Maybe we have post-execve SIGTRAP suppressed? */
1749 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1750 return; /* yes, no need to do anything */
1751
1752 if (exiting(tcp) && syserror(tcp))
1753 /* Error in execve, no post-execve SIGTRAP expected */
1754 tcp->flags &= ~TCB_WAITEXECVE;
1755 else
1756 tcp->flags |= TCB_WAITEXECVE;
1757}
1758#endif
1759
1760static void
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001761syscall_fixup_for_fork_exec(struct tcb *tcp)
Roland McGrathc1e45922008-05-27 23:18:29 +00001762{
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001763 /*
1764 * We must always trace a few critical system calls in order to
1765 * correctly support following forks in the presence of tracing
1766 * qualifiers.
1767 */
1768 int (*func)();
1769
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001770 func = tcp->s_ent->sys_func;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001771
1772 if ( sys_fork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001773 || sys_clone == func
Denys Vlasenko146b9442012-03-18 22:10:48 +01001774 ) {
1775 internal_fork(tcp);
1776 return;
1777 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001778
Denys Vlasenko84703742012-02-25 02:38:52 +01001779#if defined(TCB_WAITEXECVE)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001780 if ( sys_execve == func
Denys Vlasenko84703742012-02-25 02:38:52 +01001781# if defined(SPARC) || defined(SPARC64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001782 || sys_execv == func
Denys Vlasenkoa7949742011-08-21 17:26:55 +02001783# endif
Denys Vlasenko146b9442012-03-18 22:10:48 +01001784 ) {
1785 internal_exec(tcp);
1786 return;
1787 }
Roland McGrathc1e45922008-05-27 23:18:29 +00001788#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001789}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001790
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001791/* Return -1 on error or 1 on success (never 0!) */
Roland McGratha4d48532005-06-08 20:45:28 +00001792static int
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001793get_syscall_args(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001794{
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001795 int i, nargs;
1796
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001797 nargs = tcp->s_ent->nargs;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001798
Denys Vlasenko523635f2012-02-25 02:44:25 +01001799#if defined(S390) || defined(S390X)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001800 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001801 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 +02001802 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001803#elif defined(ALPHA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001804 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001805 if (upeek(tcp->pid, REG_A0+i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001806 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001807#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001808 if (!ia64_ia32mode) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001809 unsigned long *out0, cfm, sof, sol;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001810 long rbs_end;
1811 /* be backwards compatible with kernel < 2.4.4... */
1812# ifndef PT_RBS_END
1813# define PT_RBS_END PT_AR_BSP
1814# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001815
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001816 if (upeek(tcp->pid, PT_RBS_END, &rbs_end) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001817 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001818 if (upeek(tcp->pid, PT_CFM, (long *) &cfm) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001819 return -1;
1820
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001821 sof = (cfm >> 0) & 0x7f;
1822 sol = (cfm >> 7) & 0x7f;
1823 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1824
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001825 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001826 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1827 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1828 return -1;
1829 }
1830 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001831 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1832 PT_R9 /* ECX = out1 */,
1833 PT_R10 /* EDX = out2 */,
1834 PT_R14 /* ESI = out3 */,
1835 PT_R15 /* EDI = out4 */,
1836 PT_R13 /* EBP = out5 */};
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001837
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001838 for (i = 0; i < nargs; ++i) {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001839 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001840 return -1;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001841 /* truncate away IVE sign-extension */
1842 tcp->u_arg[i] &= 0xffffffff;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001843 }
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001844 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001845#elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001846 /* N32 and N64 both use up to six registers. */
1847 unsigned long long regs[38];
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001848
1849 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1850 return -1;
1851
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001852 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001853 tcp->u_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001854# if defined(LINUX_MIPSN32)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001855 tcp->ext_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001856# endif
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001857 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001858#elif defined(MIPS)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001859 if (nargs > 4) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001860 long sp;
1861
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001862 if (upeek(tcp->pid, REG_SP, &sp) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001863 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001864 for (i = 0; i < 4; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001865 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001866 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001867 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001868 (char *)(tcp->u_arg + 4));
1869 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001870 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001871 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001872 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001873 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001874#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001875 (void)i;
1876 (void)nargs;
1877 tcp->u_arg[0] = ppc_regs.orig_gpr3;
1878 tcp->u_arg[1] = ppc_regs.gpr[4];
1879 tcp->u_arg[2] = ppc_regs.gpr[5];
1880 tcp->u_arg[3] = ppc_regs.gpr[6];
1881 tcp->u_arg[4] = ppc_regs.gpr[7];
1882 tcp->u_arg[5] = ppc_regs.gpr[8];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001883#elif defined(SPARC) || defined(SPARC64)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001884 for (i = 0; i < nargs; ++i)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001885 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001886#elif defined(HPPA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001887 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001888 if (upeek(tcp->pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001889 return -1;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001890#elif defined(ARM) || defined(AARCH64)
1891# if defined(AARCH64)
1892 if (tcp->currpers == 1)
1893 for (i = 0; i < nargs; ++i)
1894 tcp->u_arg[i] = aarch64_regs.regs[i];
1895 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001896# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001897 for (i = 0; i < nargs; ++i)
Denys Vlasenko401374e2013-02-06 18:24:39 +01001898 tcp->u_arg[i] = arm_regs.uregs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001899#elif defined(AVR32)
Denys Vlasenkob5b25892011-08-30 19:04:54 +02001900 (void)i;
1901 (void)nargs;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001902 tcp->u_arg[0] = avr32_regs.r12;
1903 tcp->u_arg[1] = avr32_regs.r11;
1904 tcp->u_arg[2] = avr32_regs.r10;
1905 tcp->u_arg[3] = avr32_regs.r9;
1906 tcp->u_arg[4] = avr32_regs.r5;
1907 tcp->u_arg[5] = avr32_regs.r3;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001908#elif defined(BFIN)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001909 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 +02001910
Denys Vlasenko4b887a52011-08-23 13:32:38 +02001911 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001912 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001913 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001914#elif defined(SH)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001915 static const int syscall_regs[MAX_ARGS] = {
1916 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1917 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001918 };
1919
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001920 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001921 if (upeek(tcp->pid, syscall_regs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001922 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001923#elif defined(SH64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001924 int i;
1925 /* Registers used by SH5 Linux system calls for parameters */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001926 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001927
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001928 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001929 if (upeek(tcp->pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001930 return -1;
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001931#elif defined(I386)
1932 (void)i;
1933 (void)nargs;
1934 tcp->u_arg[0] = i386_regs.ebx;
1935 tcp->u_arg[1] = i386_regs.ecx;
1936 tcp->u_arg[2] = i386_regs.edx;
1937 tcp->u_arg[3] = i386_regs.esi;
1938 tcp->u_arg[4] = i386_regs.edi;
1939 tcp->u_arg[5] = i386_regs.ebp;
H.J. Lu35be5812012-04-16 13:00:01 +02001940#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001941 (void)i;
1942 (void)nargs;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001943 if (x86_io.iov_len != sizeof(i386_regs)) {
1944 /* x86-64 or x32 ABI */
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001945 tcp->u_arg[0] = x86_64_regs.rdi;
1946 tcp->u_arg[1] = x86_64_regs.rsi;
1947 tcp->u_arg[2] = x86_64_regs.rdx;
1948 tcp->u_arg[3] = x86_64_regs.r10;
1949 tcp->u_arg[4] = x86_64_regs.r8;
1950 tcp->u_arg[5] = x86_64_regs.r9;
H.J. Lu35be5812012-04-16 13:00:01 +02001951# ifdef X32
1952 tcp->ext_arg[0] = x86_64_regs.rdi;
1953 tcp->ext_arg[1] = x86_64_regs.rsi;
1954 tcp->ext_arg[2] = x86_64_regs.rdx;
1955 tcp->ext_arg[3] = x86_64_regs.r10;
1956 tcp->ext_arg[4] = x86_64_regs.r8;
1957 tcp->ext_arg[5] = x86_64_regs.r9;
1958# endif
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001959 } else {
1960 /* i386 ABI */
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001961 /* Zero-extend from 32 bits */
1962 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1963 * if you need to use *sign-extended* parameter.
1964 */
1965 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1966 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1967 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1968 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1969 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1970 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001971 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001972#elif defined(MICROBLAZE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001973 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001974 if (upeek(tcp->pid, (5 + i) * 4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001975 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001976#elif defined(CRISV10) || defined(CRISV32)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001977 static const int crisregs[MAX_ARGS] = {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001978 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001979 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001980 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001981
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001982 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001983 if (upeek(tcp->pid, crisregs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001984 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001985#elif defined(TILE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001986 for (i = 0; i < nargs; ++i)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001987 tcp->u_arg[i] = tile_regs.regs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001988#elif defined(M68K)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001989 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001990 if (upeek(tcp->pid, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001991 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001992#elif defined(OR1K)
1993 (void)nargs;
1994 for (i = 0; i < 6; ++i)
1995 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
James Hogan5f999a82013-02-22 14:44:10 +00001996#elif defined(METAG)
1997 for (i = 0; i < nargs; i++)
1998 /* arguments go backwards from D1Ar1 (D1.3) */
1999 tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
Chris Zankel8f636ed2013-03-25 10:22:07 -07002000#elif defined(XTENSA)
2001 /* arg0: a6, arg1: a3, arg2: a4, arg3: a5, arg4: a8, arg5: a9 */
2002 static const int xtensaregs[MAX_ARGS] = { 6, 3, 4, 5, 8, 9 };
2003 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002004 if (upeek(tcp->pid, REG_A_BASE + xtensaregs[i], &tcp->u_arg[i]) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07002005 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05302006# elif defined(ARC)
2007 long *arc_args = &arc_regs.scratch.r0;
2008 for (i = 0; i < nargs; ++i)
2009 tcp->u_arg[i] = *arc_args--;
2010
Denys Vlasenko523635f2012-02-25 02:44:25 +01002011#else /* Other architecture (32bits specific) */
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*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02002014 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002015#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00002016 return 1;
2017}
2018
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002019static int
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002020trace_syscall_entering(struct tcb *tcp)
2021{
2022 int res, scno_good;
2023
Denys Vlasenko2ce12ed2011-08-24 17:25:32 +02002024#if defined TCB_WAITEXECVE
2025 if (tcp->flags & TCB_WAITEXECVE) {
2026 /* This is the post-execve SIGTRAP. */
2027 tcp->flags &= ~TCB_WAITEXECVE;
2028 return 0;
2029 }
2030#endif
2031
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002032 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002033 if (res == 0)
2034 return res;
Denys Vlasenko907735a2012-03-21 00:23:16 +01002035 if (res == 1) {
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02002036 res = syscall_fixup_on_sysenter(tcp);
Denys Vlasenko907735a2012-03-21 00:23:16 +01002037 if (res == 0)
2038 return res;
2039 if (res == 1)
2040 res = get_syscall_args(tcp);
2041 }
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002042
2043 if (res != 1) {
2044 printleader(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002045 if (scno_good != 1)
Denys Vlasenkob7a6dae2012-03-20 16:48:35 +01002046 tprints("????" /* anti-trigraph gap */ "(");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002047 else if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002048 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002049 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002050 tprintf("%s(", tcp->s_ent->sys_name);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002051 /*
2052 * " <unavailable>" will be added later by the code which
2053 * detects ptrace errors.
2054 */
2055 goto ret;
2056 }
2057
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002058 if ( sys_execve == tcp->s_ent->sys_func
2059# if defined(SPARC) || defined(SPARC64)
2060 || sys_execv == tcp->s_ent->sys_func
2061# endif
2062 ) {
2063 hide_log_until_execve = 0;
2064 }
2065
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002066#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002067 while (1) {
Denys Vlasenko523635f2012-02-25 02:44:25 +01002068# ifdef SYS_socket_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002069 if (tcp->s_ent->sys_func == sys_socketcall) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00002070 decode_socket_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002071 break;
2072 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002073# endif
2074# ifdef SYS_ipc_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002075 if (tcp->s_ent->sys_func == sys_ipc) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00002076 decode_ipc_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002077 break;
2078 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002079# endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002080 break;
2081 }
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002082#endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002083
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01002084 if (need_fork_exec_workarounds)
2085 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002086
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002087 if (!(tcp->qual_flg & QUAL_TRACE)
2088 || (tracing_paths && !pathtrace_match(tcp))
2089 ) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002090 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
2091 return 0;
2092 }
2093
2094 tcp->flags &= ~TCB_FILTERED;
2095
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002096 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002097 res = 0;
2098 goto ret;
2099 }
2100
Masatake YAMATOed69fc22014-04-16 15:33:35 +09002101#ifdef USE_LIBUNWIND
2102 if (stack_trace_enabled) {
2103 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
2104 unwind_capture_stacktrace(tcp);
2105 }
2106#endif
2107
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002108 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002109 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002110 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002111 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002112 tprintf("%s(", tcp->s_ent->sys_name);
2113 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002114 res = printargs(tcp);
2115 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002116 res = tcp->s_ent->sys_func(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002117
Dmitry V. Levinb742d8c2012-09-17 22:40:12 +00002118 fflush(tcp->outf);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002119 ret:
2120 tcp->flags |= TCB_INSYSCALL;
2121 /* Measure the entrance time as late as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002122 if (Tflag || cflag)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002123 gettimeofday(&tcp->etime, NULL);
2124 return res;
2125}
2126
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002127/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01002128 * 1: ok, continue in trace_syscall_exiting().
2129 * -1: error, trace_syscall_exiting() should print error indicator
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002130 * ("????" etc) and bail out.
2131 */
2132static int
2133get_syscall_result(struct tcb *tcp)
2134{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002135#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002136 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002137 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002138#elif defined(POWERPC)
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002139 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002140#elif defined(AVR32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002141 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002142#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002143 if (upeek(tcp->pid, PT_R0, &bfin_r0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002144 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002145#elif defined(I386)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002146 /* already done by get_regs */
H.J. Lu35be5812012-04-16 13:00:01 +02002147#elif defined(X86_64) || defined(X32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002148 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002149#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002150# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01002151 long psr;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002152 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02002153 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002154 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002155 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002156 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002157 return -1;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002158#elif defined(ARM)
2159 /* already done by get_regs */
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002160#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002161 /* register reading already done by get_regs */
2162
2163 /* Used to do this, but we did it on syscall entry already: */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002164 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
2165 * else it's personality 0.
2166 */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002167 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
Denys Vlasenko523635f2012-02-25 02:44:25 +01002168#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002169 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002170 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002171#elif defined(LINUX_MIPSN32)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002172 unsigned long long regs[38];
2173
2174 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
2175 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002176 mips_a3 = regs[REG_A3];
2177 mips_r2 = regs[REG_V0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01002178#elif defined(MIPS)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002179 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002180 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002181 if (upeek(tcp->pid, REG_V0, &mips_r2) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002182 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002183#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002184 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002185 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002186 if (upeek(tcp->pid, REG_R0, &alpha_r0) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002187 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002188#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002189 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002190#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002191 if (upeek(tcp->pid, PT_GR28, &hppa_r28) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002192 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002193#elif defined(SH)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002194 /* new syscall ABI returns result in R0 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002195 if (upeek(tcp->pid, 4*REG_REG0, (long *)&sh_r0) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002196 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002197#elif defined(SH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002198 /* ABI defines result returned in r9 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002199 if (upeek(tcp->pid, REG_GENERAL(9), (long *)&sh64_r9) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002200 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002201#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002202 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002203 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002204#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002205 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002206#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002207 if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002208 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01002209#elif defined(OR1K)
2210 /* already done by get_regs */
James Hogan5f999a82013-02-22 14:44:10 +00002211#elif defined(METAG)
2212 /* already done by get_regs */
Chris Zankel8f636ed2013-03-25 10:22:07 -07002213#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002214 if (upeek(tcp->pid, REG_A_BASE + 2, &xtensa_a2) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07002215 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05302216#elif defined(ARC)
2217 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002218#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002219 return 1;
2220}
2221
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002222/* Called at each syscall exit */
2223static void
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002224syscall_fixup_on_sysexit(struct tcb *tcp)
2225{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002226#if defined(S390) || defined(S390X)
Denys Vlasenkoece98792011-08-25 10:25:35 +02002227 if ((tcp->flags & TCB_WAITEXECVE)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002228 && (s390_gpr2 == -ENOSYS || s390_gpr2 == tcp->scno)) {
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002229 /*
2230 * Return from execve.
2231 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2232 * flag set for the post-execve SIGTRAP to see and reset.
2233 */
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002234 s390_gpr2 = 0;
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002235 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002236#endif
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002237}
2238
Denys Vlasenko907735a2012-03-21 00:23:16 +01002239/* Returns:
2240 * 1: ok, continue in trace_syscall_exiting().
2241 * -1: error, trace_syscall_exiting() should print error indicator
2242 * ("????" etc) and bail out.
2243 */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002244static void
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002245get_error(struct tcb *tcp)
2246{
2247 int u_error = 0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002248 int check_errno = 1;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002249 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002250 check_errno = 0;
2251 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002252#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002253 if (check_errno && is_negated_errno(s390_gpr2)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002254 tcp->u_rval = -1;
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002255 u_error = -s390_gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002256 }
2257 else {
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002258 tcp->u_rval = s390_gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002259 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002260#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002261 if (check_errno && is_negated_errno(i386_regs.eax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002262 tcp->u_rval = -1;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002263 u_error = -i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002264 }
2265 else {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002266 tcp->u_rval = i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002267 }
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00002268#elif defined(X86_64) || defined(X32)
2269 /*
2270 * In X32, return value is 64-bit (llseek uses one).
2271 * Using merely "long rax" would not work.
2272 */
2273 kernel_long_t rax;
2274
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002275 if (x86_io.iov_len == sizeof(i386_regs)) {
2276 /* Sign extend from 32 bits */
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00002277 rax = (int32_t) i386_regs.eax;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002278 } else {
2279 rax = x86_64_regs.rax;
2280 }
2281 if (check_errno && is_negated_errno(rax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002282 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002283 u_error = -rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002284 }
2285 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002286 tcp->u_rval = rax;
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00002287# ifdef X32
2288 /* tcp->u_rval contains a truncated value */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002289 tcp->u_lrval = rax;
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00002290# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002291 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002292#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02002293 if (ia64_ia32mode) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002294 int err;
2295
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002296 err = (int)ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002297 if (check_errno && is_negated_errno(err)) {
2298 tcp->u_rval = -1;
2299 u_error = -err;
2300 }
2301 else {
2302 tcp->u_rval = err;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002303 }
2304 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002305 if (check_errno && ia64_r10) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002306 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002307 u_error = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002308 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002309 tcp->u_rval = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002310 }
2311 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002312#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002313 if (check_errno && mips_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002314 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002315 u_error = mips_r2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002316 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002317 tcp->u_rval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002318# if defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002319 tcp->u_lrval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002320# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002321 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002322#elif defined(POWERPC)
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002323 if (check_errno && (ppc_regs.ccr & 0x10000000)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002324 tcp->u_rval = -1;
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002325 u_error = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002326 }
2327 else {
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002328 tcp->u_rval = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002329 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002330#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002331 if (check_errno && is_negated_errno(m68k_d0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002332 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002333 u_error = -m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002334 }
2335 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002336 tcp->u_rval = m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002337 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002338#elif defined(ARM) || defined(AARCH64)
2339# if defined(AARCH64)
2340 if (tcp->currpers == 1) {
2341 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2342 tcp->u_rval = -1;
2343 u_error = -aarch64_regs.regs[0];
2344 }
2345 else {
2346 tcp->u_rval = aarch64_regs.regs[0];
2347 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002348 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002349 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002350# endif
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002351 {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002352 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002353 tcp->u_rval = -1;
Denys Vlasenko401374e2013-02-06 18:24:39 +01002354 u_error = -arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002355 }
2356 else {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002357 tcp->u_rval = arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002358 }
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002359 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002360#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002361 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002362 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002363 u_error = -avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002364 }
2365 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002366 tcp->u_rval = avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002367 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002368#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002369 if (check_errno && is_negated_errno(bfin_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002370 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002371 u_error = -bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002372 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002373 tcp->u_rval = bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002374 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002375#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002376 if (check_errno && alpha_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002377 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002378 u_error = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002379 }
2380 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002381 tcp->u_rval = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002382 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002383#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002384 if (check_errno && sparc_regs.psr & PSR_C) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002385 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002386 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002387 }
2388 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002389 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002390 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002391#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002392 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002393 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002394 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002395 }
2396 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002397 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002398 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002399#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002400 if (check_errno && is_negated_errno(hppa_r28)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002401 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002402 u_error = -hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002403 }
2404 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002405 tcp->u_rval = hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002406 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002407#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002408 if (check_errno && is_negated_errno(sh_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002409 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002410 u_error = -sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002411 }
2412 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002413 tcp->u_rval = sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002414 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002415#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002416 if (check_errno && is_negated_errno(sh64_r9)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002417 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002418 u_error = -sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002419 }
2420 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002421 tcp->u_rval = sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002422 }
James Hogan5f999a82013-02-22 14:44:10 +00002423#elif defined(METAG)
2424 /* result pointer in D0Re0 (D0.0) */
2425 if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2426 tcp->u_rval = -1;
2427 u_error = -metag_regs.dx[0][0];
2428 }
2429 else {
2430 tcp->u_rval = metag_regs.dx[0][0];
2431 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002432#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002433 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002434 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002435 u_error = -cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002436 }
2437 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002438 tcp->u_rval = cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002439 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002440#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002441 /*
2442 * The standard tile calling convention returns the value (or negative
2443 * errno) in r0, and zero (or positive errno) in r1.
2444 * Until at least kernel 3.8, however, the r1 value is not reflected
2445 * in ptregs at this point, so we use r0 here.
2446 */
2447 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002448 tcp->u_rval = -1;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002449 u_error = -tile_regs.regs[0];
2450 } else {
2451 tcp->u_rval = tile_regs.regs[0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002452 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002453#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002454 if (check_errno && is_negated_errno(microblaze_r3)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002455 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002456 u_error = -microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002457 }
2458 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002459 tcp->u_rval = microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002460 }
Christian Svensson492f81f2013-02-14 13:26:27 +01002461#elif defined(OR1K)
2462 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2463 tcp->u_rval = -1;
2464 u_error = -or1k_regs.gpr[11];
2465 }
2466 else {
2467 tcp->u_rval = or1k_regs.gpr[11];
2468 }
Chris Zankel8f636ed2013-03-25 10:22:07 -07002469#elif defined(XTENSA)
2470 if (check_errno && is_negated_errno(xtensa_a2)) {
2471 tcp->u_rval = -1;
2472 u_error = -xtensa_a2;
2473 }
2474 else {
2475 tcp->u_rval = xtensa_a2;
2476 }
Vineet Gupta7daacbb2013-08-16 12:47:06 +05302477#elif defined(ARC)
2478 if (check_errno && is_negated_errno(arc_regs.scratch.r0)) {
2479 tcp->u_rval = -1;
2480 u_error = -arc_regs.scratch.r0;
2481 }
2482 else {
2483 tcp->u_rval = arc_regs.scratch.r0;
2484 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002485#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002486 tcp->u_error = u_error;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002487}
2488
2489static void
2490dumpio(struct tcb *tcp)
2491{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002492 int (*func)();
2493
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002494 if (syserror(tcp))
2495 return;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +01002496 if ((unsigned long) tcp->u_arg[0] >= num_quals)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002497 return;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002498 func = tcp->s_ent->sys_func;
2499 if (func == printargs)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002500 return;
2501 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002502 if (func == sys_read ||
2503 func == sys_pread ||
2504 func == sys_recv ||
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002505 func == sys_recvfrom) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002506 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002507 return;
2508 } else if (func == sys_readv) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002509 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002510 return;
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002511#if HAVE_SENDMSG
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002512 } else if (func == sys_recvmsg) {
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002513 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002514 return;
2515 } else if (func == sys_recvmmsg) {
Masatake YAMATOa807dce2014-11-07 01:23:26 +09002516 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002517 return;
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002518#endif
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002519 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002520 }
2521 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002522 if (func == sys_write ||
2523 func == sys_pwrite ||
2524 func == sys_send ||
2525 func == sys_sendto)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002526 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002527 else if (func == sys_writev)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002528 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002529#if HAVE_SENDMSG
2530 else if (func == sys_sendmsg)
2531 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
Masatake YAMATOa807dce2014-11-07 01:23:26 +09002532 else if (func == sys_sendmmsg)
2533 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002534#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002535 }
2536}
2537
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002538static int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002539trace_syscall_exiting(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002540{
2541 int sys_res;
2542 struct timeval tv;
Denys Vlasenko1a5b5a72011-08-25 00:29:56 +02002543 int res;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002544 long u_error;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002545
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002546 /* Measure the exit time as early as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002547 if (Tflag || cflag)
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002548 gettimeofday(&tv, NULL);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002549
Masatake YAMATOed69fc22014-04-16 15:33:35 +09002550#ifdef USE_LIBUNWIND
2551 if (stack_trace_enabled) {
2552 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
2553 unwind_cache_invalidate(tcp);
2554 }
2555#endif
2556
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00002557#if SUPPORTED_PERSONALITIES > 1
2558 update_personality(tcp, tcp->currpers);
2559#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002560 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002561 if (res == 1) {
2562 syscall_fixup_on_sysexit(tcp); /* never fails */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002563 get_error(tcp); /* never fails */
2564 if (need_fork_exec_workarounds)
2565 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002566 if (filtered(tcp) || hide_log_until_execve)
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002567 goto ret;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002568 }
2569
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002570 if (cflag) {
Dmitry V. Levinac5133d2014-05-29 18:10:00 +00002571 count_syscall(tcp, &tv);
Denys Vlasenko7b609d52011-06-22 14:32:43 +02002572 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002573 goto ret;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002574 }
2575 }
2576
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002577 /* If not in -ff mode, and printing_tcp != tcp,
2578 * then the log currently does not end with output
2579 * of _our syscall entry_, but with something else.
2580 * We need to say which syscall's return is this.
2581 *
2582 * Forced reprinting via TCB_REPRINT is used only by
2583 * "strace -ff -oLOG test/threaded_execve" corner case.
2584 * It's the only case when -ff mode needs reprinting.
2585 */
2586 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2587 tcp->flags &= ~TCB_REPRINT;
2588 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002589 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002590 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002591 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002592 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002593 }
2594 printing_tcp = tcp;
2595
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002596 if (res != 1) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002597 /* There was error in one of prior ptrace ops */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002598 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002599 tabto();
Denys Vlasenko000b6012012-01-28 01:25:03 +01002600 tprints("= ? <unavailable>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002601 line_ended();
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002602 tcp->flags &= ~TCB_INSYSCALL;
2603 return res;
2604 }
2605
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002606 sys_res = 0;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002607 if (tcp->qual_flg & QUAL_RAW) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002608 /* sys_res = printargs(tcp); - but it's nop on sysexit */
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002609 } else {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002610 /* FIXME: not_failing_only (IOW, option -z) is broken:
2611 * failure of syscall is known only after syscall return.
2612 * Thus we end up with something like this on, say, ENOENT:
2613 * open("doesnt_exist", O_RDONLY <unfinished ...>
2614 * {next syscall decode}
2615 * whereas the intended result is that open(...) line
2616 * is not shown at all.
2617 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002618 if (not_failing_only && tcp->u_error)
Denys Vlasenko3b738812011-08-22 02:06:35 +02002619 goto ret; /* ignore failed syscalls */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002620 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002621 }
2622
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002623 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002624 tabto();
Denys Vlasenko3b738812011-08-22 02:06:35 +02002625 u_error = tcp->u_error;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002626 if (tcp->qual_flg & QUAL_RAW) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002627 if (u_error)
2628 tprintf("= -1 (errno %ld)", u_error);
2629 else
2630 tprintf("= %#lx", tcp->u_rval);
2631 }
2632 else if (!(sys_res & RVAL_NONE) && u_error) {
2633 switch (u_error) {
Denys Vlasenkofe585652012-01-12 11:26:34 +01002634 /* Blocked signals do not interrupt any syscalls.
2635 * In this case syscalls don't return ERESTARTfoo codes.
2636 *
2637 * Deadly signals set to SIG_DFL interrupt syscalls
2638 * and kill the process regardless of which of the codes below
2639 * is returned by the interrupted syscall.
2640 * In some cases, kernel forces a kernel-generated deadly
2641 * signal to be unblocked and set to SIG_DFL (and thus cause
2642 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2643 * or SIGILL. (The alternative is to leave process spinning
2644 * forever on the faulty instruction - not useful).
2645 *
2646 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2647 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2648 * but kernel will always restart them.
2649 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002650 case ERESTARTSYS:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002651 /* Most common type of signal-interrupted syscall exit code.
2652 * The system call will be restarted with the same arguments
2653 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2654 */
2655 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002656 break;
2657 case ERESTARTNOINTR:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002658 /* Rare. For example, fork() returns this if interrupted.
2659 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2660 */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002661 tprints("= ? ERESTARTNOINTR (To be restarted)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002662 break;
2663 case ERESTARTNOHAND:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002664 /* pause(), rt_sigsuspend() etc use this code.
2665 * SA_RESTART is ignored (assumed not set):
2666 * syscall won't restart (will return EINTR instead)
Denys Vlasenko30c03232013-02-19 16:59:26 +01002667 * even after signal with SA_RESTART set. However,
2668 * after SIG_IGN or SIG_DFL signal it will restart
2669 * (thus the name "restart only if has no handler").
Denys Vlasenkofe585652012-01-12 11:26:34 +01002670 */
Denys Vlasenkoaba62922013-03-05 16:56:35 +01002671 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002672 break;
2673 case ERESTART_RESTARTBLOCK:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002674 /* Syscalls like nanosleep(), poll() which can't be
2675 * restarted with their original arguments use this
2676 * code. Kernel will execute restart_syscall() instead,
2677 * which changes arguments before restarting syscall.
2678 * SA_RESTART is ignored (assumed not set) similarly
2679 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2680 * since restart data is saved in "restart block"
2681 * in task struct, and if signal handler uses a syscall
2682 * which in turn saves another such restart block,
2683 * old data is lost and restart becomes impossible)
2684 */
2685 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002686 break;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002687 default:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002688 if (u_error < 0)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002689 tprintf("= -1 E??? (errno %ld)", u_error);
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00002690 else if ((unsigned long) u_error < nerrnos)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002691 tprintf("= -1 %s (%s)", errnoent[u_error],
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002692 strerror(u_error));
2693 else
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002694 tprintf("= -1 ERRNO_%ld (%s)", u_error,
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002695 strerror(u_error));
2696 break;
2697 }
2698 if ((sys_res & RVAL_STR) && tcp->auxstr)
2699 tprintf(" (%s)", tcp->auxstr);
2700 }
2701 else {
2702 if (sys_res & RVAL_NONE)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002703 tprints("= ?");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002704 else {
2705 switch (sys_res & RVAL_MASK) {
2706 case RVAL_HEX:
2707 tprintf("= %#lx", tcp->u_rval);
2708 break;
2709 case RVAL_OCTAL:
2710 tprintf("= %#lo", tcp->u_rval);
2711 break;
2712 case RVAL_UDECIMAL:
2713 tprintf("= %lu", tcp->u_rval);
2714 break;
2715 case RVAL_DECIMAL:
2716 tprintf("= %ld", tcp->u_rval);
2717 break;
Zubin Mithra64aa1b12014-06-04 08:30:41 +05302718 case RVAL_FD:
2719 if (show_fd_path) {
2720 tprints("= ");
2721 printfd(tcp, tcp->u_rval);
2722 }
2723 else
2724 tprintf("= %ld", tcp->u_rval);
2725 break;
H.J. Ludd0130b2012-04-16 12:16:45 +02002726#if defined(LINUX_MIPSN32) || defined(X32)
2727 /*
2728 case RVAL_LHEX:
2729 tprintf("= %#llx", tcp->u_lrval);
2730 break;
2731 case RVAL_LOCTAL:
2732 tprintf("= %#llo", tcp->u_lrval);
2733 break;
2734 */
2735 case RVAL_LUDECIMAL:
2736 tprintf("= %llu", tcp->u_lrval);
2737 break;
2738 /*
2739 case RVAL_LDECIMAL:
2740 tprintf("= %lld", tcp->u_lrval);
2741 break;
2742 */
2743#endif
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002744 default:
2745 fprintf(stderr,
2746 "invalid rval format\n");
2747 break;
2748 }
2749 }
2750 if ((sys_res & RVAL_STR) && tcp->auxstr)
2751 tprintf(" (%s)", tcp->auxstr);
2752 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002753 if (Tflag) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002754 tv_sub(&tv, &tv, &tcp->etime);
2755 tprintf(" <%ld.%06ld>",
2756 (long) tv.tv_sec, (long) tv.tv_usec);
2757 }
Denys Vlasenko000b6012012-01-28 01:25:03 +01002758 tprints("\n");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002759 dumpio(tcp);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002760 line_ended();
2761
Luca Clementi327064b2013-07-23 00:11:35 -07002762#ifdef USE_LIBUNWIND
2763 if (stack_trace_enabled)
Masatake YAMATO61413922014-04-16 15:33:02 +09002764 unwind_print_stacktrace(tcp);
Luca Clementi327064b2013-07-23 00:11:35 -07002765#endif
2766
Denys Vlasenko3b738812011-08-22 02:06:35 +02002767 ret:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002768 tcp->flags &= ~TCB_INSYSCALL;
2769 return 0;
2770}
2771
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002772int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002773trace_syscall(struct tcb *tcp)
2774{
2775 return exiting(tcp) ?
2776 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2777}