blob: 3c5f4baa2699911da0874b627bf61a2197ca6bd2 [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/param.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000036
Dmitry V. Levin5503dd22015-02-13 02:12:14 +000037/* for struct iovec */
38#include <sys/uio.h>
Maarten ter Huurne40c174b2014-10-20 01:02:48 +020039
Dmitry V. Levin7211dbc2015-02-28 12:20:21 +000040#include "regs.h"
Dmitry V. Levinfadf3792015-02-13 00:26:38 +000041#include "ptrace.h"
Wichert Akkerman15dea971999-10-06 13:06:34 +000042
Denys Vlasenko84703742012-02-25 02:38:52 +010043#if defined(SPARC64)
Roland McGrath6d1a65c2004-07-12 07:44:08 +000044# undef PTRACE_GETREGS
45# define PTRACE_GETREGS PTRACE_GETREGS64
46# undef PTRACE_SETREGS
47# define PTRACE_SETREGS PTRACE_SETREGS64
Denys Vlasenko84703742012-02-25 02:38:52 +010048#endif
Roland McGrath6d1a65c2004-07-12 07:44:08 +000049
Dmitry V. Levin5503dd22015-02-13 02:12:14 +000050#if defined SPARC64
51# include <asm/psrcompat.h>
52#elif defined SPARC
53# include <asm/psr.h>
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010054#endif
55
Dmitry V. Levin48f08902015-03-05 23:30:02 +000056#ifdef IA64
57# include <asm/rse.h>
58#endif
59
Dmitry V. Levin4c3f2ae2015-02-13 22:53:00 +000060#ifndef NT_PRSTATUS
61# define NT_PRSTATUS 1
62#endif
63
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000064#ifndef NSIG
Denys Vlasenko523635f2012-02-25 02:44:25 +010065# warning: NSIG is not defined, using 32
66# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000067#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000068
69#include "syscall.h"
70
71/* Define these shorthand notations to simplify the syscallent files. */
Roland McGrath2fe7b132005-07-05 03:25:35 +000072#define TD TRACE_DESC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000073#define TF TRACE_FILE
74#define TI TRACE_IPC
75#define TN TRACE_NETWORK
76#define TP TRACE_PROCESS
77#define TS TRACE_SIGNAL
Namhyung Kim96792962012-10-24 11:41:57 +090078#define TM TRACE_MEMORY
Dmitry V. Levin50a218d2011-01-18 17:36:20 +000079#define NF SYSCALL_NEVER_FAILS
Denys Vlasenkoac1ce772011-08-23 13:24:17 +020080#define MA MAX_ARGS
Masatake YAMATO1d78d222014-04-16 15:33:11 +090081#define SI STACKTRACE_INVALIDATE_CACHE
82#define SE STACKTRACE_CAPTURE_ON_ENTER
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000083
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +010084const struct_sysent sysent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000085#include "syscallent.h"
86};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000087
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010088#if SUPPORTED_PERSONALITIES > 1
89static const struct_sysent sysent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +010090# include "syscallent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000091};
Denys Vlasenko39fca622011-08-20 02:12:33 +020092#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000093
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010094#if SUPPORTED_PERSONALITIES > 2
95static const struct_sysent sysent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +010096# include "syscallent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000097};
Denys Vlasenko39fca622011-08-20 02:12:33 +020098#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000099
100/* Now undef them since short defines cause wicked namespace pollution. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000101#undef TD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000102#undef TF
103#undef TI
104#undef TN
105#undef TP
106#undef TS
Namhyung Kim96792962012-10-24 11:41:57 +0900107#undef TM
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000108#undef NF
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200109#undef MA
Masatake YAMATO1d78d222014-04-16 15:33:11 +0900110#undef SI
111#undef SE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000112
Denys Vlasenko39fca622011-08-20 02:12:33 +0200113/*
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +0000114 * `ioctlent[012].h' files are automatically generated by the auxiliary
Denys Vlasenko39fca622011-08-20 02:12:33 +0200115 * program `ioctlsort', such that the list is sorted by the `code' field.
116 * This has the side-effect of resolving the _IO.. macros into
117 * plain integers, eliminating the need to include here everything
118 * in "/usr/include".
119 */
120
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100121const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000122#include "errnoent.h"
123};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100124const char *const signalent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200125#include "signalent.h"
126};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100127const struct_ioctlent ioctlent0[] = {
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +0000128#include "ioctlent0.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200129};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000130
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100131#if SUPPORTED_PERSONALITIES > 1
Roland McGrathee36ce12004-09-04 03:53:10 +0000132static const char *const errnoent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100133# include "errnoent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000134};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200135static const char *const signalent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100136# include "signalent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200137};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100138static const struct_ioctlent ioctlent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100139# include "ioctlent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200140};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200141#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000142
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100143#if SUPPORTED_PERSONALITIES > 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000144static const char *const errnoent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100145# include "errnoent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000146};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200147static const char *const signalent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100148# include "signalent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200149};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100150static const struct_ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100151# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200152};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200153#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000154
Dmitry V. Levine6f55242013-02-26 22:02:30 +0000155enum {
156 nsyscalls0 = ARRAY_SIZE(sysent0)
157#if SUPPORTED_PERSONALITIES > 1
158 , nsyscalls1 = ARRAY_SIZE(sysent1)
159# if SUPPORTED_PERSONALITIES > 2
160 , nsyscalls2 = ARRAY_SIZE(sysent2)
161# endif
162#endif
163};
164
165enum {
166 nerrnos0 = ARRAY_SIZE(errnoent0)
167#if SUPPORTED_PERSONALITIES > 1
168 , nerrnos1 = ARRAY_SIZE(errnoent1)
169# if SUPPORTED_PERSONALITIES > 2
170 , nerrnos2 = ARRAY_SIZE(errnoent2)
171# endif
172#endif
173};
174
175enum {
176 nsignals0 = ARRAY_SIZE(signalent0)
177#if SUPPORTED_PERSONALITIES > 1
178 , nsignals1 = ARRAY_SIZE(signalent1)
179# if SUPPORTED_PERSONALITIES > 2
180 , nsignals2 = ARRAY_SIZE(signalent2)
181# endif
182#endif
183};
184
185enum {
186 nioctlents0 = ARRAY_SIZE(ioctlent0)
187#if SUPPORTED_PERSONALITIES > 1
188 , nioctlents1 = ARRAY_SIZE(ioctlent1)
189# if SUPPORTED_PERSONALITIES > 2
190 , nioctlents2 = ARRAY_SIZE(ioctlent2)
191# endif
192#endif
193};
194
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100195#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100196const struct_sysent *sysent = sysent0;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100197const char *const *errnoent = errnoent0;
198const char *const *signalent = signalent0;
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100199const struct_ioctlent *ioctlent = ioctlent0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100200#endif
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100201unsigned nsyscalls = nsyscalls0;
202unsigned nerrnos = nerrnos0;
203unsigned nsignals = nsignals0;
204unsigned nioctlents = nioctlents0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100205
206unsigned num_quals;
207qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
208
209static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
210 nsyscalls0,
211#if SUPPORTED_PERSONALITIES > 1
212 nsyscalls1,
213#endif
214#if SUPPORTED_PERSONALITIES > 2
215 nsyscalls2,
216#endif
217};
218static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
219 sysent0,
220#if SUPPORTED_PERSONALITIES > 1
221 sysent1,
222#endif
223#if SUPPORTED_PERSONALITIES > 2
224 sysent2,
225#endif
226};
227
228enum {
229 MAX_NSYSCALLS1 = (nsyscalls0
230#if SUPPORTED_PERSONALITIES > 1
231 > nsyscalls1 ? nsyscalls0 : nsyscalls1
232#endif
233 ),
234 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
235#if SUPPORTED_PERSONALITIES > 2
236 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
237#endif
238 ),
239 MAX_NSYSCALLS = MAX_NSYSCALLS2,
240 /* We are ready for arches with up to 255 signals,
241 * even though the largest known signo is on MIPS and it is 128.
242 * The number of existing syscalls on all arches is
243 * larger that 255 anyway, so it is just a pedantic matter.
244 */
245 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
246};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000247
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100248#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100249unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000250
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100251# ifndef current_wordsize
252unsigned current_wordsize;
253static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000254 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000255 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100256# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000257 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100258# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200259};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100260# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000261
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200262void
Dmitry V. Levin3abe8b22006-12-20 22:37:21 +0000263set_personality(int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000264{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100265 nsyscalls = nsyscall_vec[personality];
266 sysent = sysent_vec[personality];
267
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000268 switch (personality) {
269 case 0:
270 errnoent = errnoent0;
271 nerrnos = nerrnos0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000272 ioctlent = ioctlent0;
273 nioctlents = nioctlents0;
274 signalent = signalent0;
275 nsignals = nsignals0;
276 break;
277
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000278 case 1:
279 errnoent = errnoent1;
280 nerrnos = nerrnos1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000281 ioctlent = ioctlent1;
282 nioctlents = nioctlents1;
283 signalent = signalent1;
284 nsignals = nsignals1;
285 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000286
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100287# if SUPPORTED_PERSONALITIES > 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000288 case 2:
289 errnoent = errnoent2;
290 nerrnos = nerrnos2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000291 ioctlent = ioctlent2;
292 nioctlents = nioctlents2;
293 signalent = signalent2;
294 nsignals = nsignals2;
295 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100296# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000297 }
298
299 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100300# ifndef current_wordsize
301 current_wordsize = personality_wordsize[personality];
302# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000303}
304
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000305static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000306update_personality(struct tcb *tcp, unsigned int personality)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000307{
308 if (personality == current_personality)
309 return;
310 set_personality(personality);
311
312 if (personality == tcp->currpers)
313 return;
314 tcp->currpers = personality;
315
H.J. Lu35be5812012-04-16 13:00:01 +0200316# if defined(POWERPC64)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000317 if (!qflag) {
318 static const char *const names[] = {"64 bit", "32 bit"};
319 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
320 tcp->pid, names[personality]);
321 }
H.J. Lu35be5812012-04-16 13:00:01 +0200322# elif defined(X86_64)
323 if (!qflag) {
324 static const char *const names[] = {"64 bit", "32 bit", "x32"};
325 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
326 tcp->pid, names[personality]);
327 }
H.J. Lu085e4282012-04-17 11:05:04 -0700328# elif defined(X32)
329 if (!qflag) {
330 static const char *const names[] = {"x32", "32 bit"};
331 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
332 tcp->pid, names[personality]);
333 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000334# elif defined(AARCH64)
335 if (!qflag) {
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100336 static const char *const names[] = {"32-bit", "AArch64"};
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000337 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
338 tcp->pid, names[personality]);
339 }
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100340# elif defined(TILE)
341 if (!qflag) {
342 static const char *const names[] = {"64-bit", "32-bit"};
343 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
344 tcp->pid, names[personality]);
345 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100346# endif
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000347}
348#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000349
Denys Vlasenkoc1540fe2013-02-21 16:17:08 +0100350static int qual_syscall(), qual_signal(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000351
Roland McGrathe10e62a2004-09-04 04:20:43 +0000352static const struct qual_options {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000353 unsigned int bitflag;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000354 const char *option_name;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000355 int (*qualify)(const char *, int, int);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000356 const char *argument_name;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000357} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000358 { QUAL_TRACE, "trace", qual_syscall, "system call" },
359 { QUAL_TRACE, "t", qual_syscall, "system call" },
360 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
361 { QUAL_ABBREV, "a", qual_syscall, "system call" },
362 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
363 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
364 { QUAL_RAW, "raw", qual_syscall, "system call" },
365 { QUAL_RAW, "x", qual_syscall, "system call" },
366 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
367 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
368 { QUAL_SIGNAL, "s", qual_signal, "signal" },
Roland McGrath9797ceb2002-12-30 10:23:00 +0000369 { QUAL_READ, "read", qual_desc, "descriptor" },
370 { QUAL_READ, "reads", qual_desc, "descriptor" },
371 { QUAL_READ, "r", qual_desc, "descriptor" },
372 { QUAL_WRITE, "write", qual_desc, "descriptor" },
373 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
374 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000375 { 0, NULL, NULL, NULL },
376};
377
Roland McGrath9797ceb2002-12-30 10:23:00 +0000378static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000379reallocate_qual(const unsigned int n)
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100380{
381 unsigned p;
382 qualbits_t *qp;
383 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
384 qp = qual_vec[p] = realloc(qual_vec[p], n * sizeof(qualbits_t));
385 if (!qp)
386 die_out_of_memory();
387 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
388 }
389 num_quals = n;
390}
391
392static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000393qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000394{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000395 int p;
Roland McGrath138c6a32006-01-12 09:50:49 +0000396
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100397 if (num_quals <= n)
398 reallocate_qual(n + 1);
Roland McGrath138c6a32006-01-12 09:50:49 +0000399
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100400 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
401 if (pers == p || pers < 0) {
402 if (not)
403 qual_vec[p][n] &= ~bitflag;
404 else
405 qual_vec[p][n] |= bitflag;
406 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000407 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000408}
409
410static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000411qual_syscall(const char *s, const unsigned int bitflag, const int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000412{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000413 int p;
414 unsigned int i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000415 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000416
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100417 if (*s >= '0' && *s <= '9') {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100418 i = string_to_uint(s);
Denys Vlasenkob43dacd2013-02-23 18:19:28 +0100419 if (i >= MAX_NSYSCALLS)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000420 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000421 qualify_one(i, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000422 return 0;
Roland McGrath48a035f2006-01-12 09:45:56 +0000423 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000424
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100425 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
426 for (i = 0; i < nsyscall_vec[p]; i++) {
427 if (sysent_vec[p][i].sys_name
428 && strcmp(s, sysent_vec[p][i].sys_name) == 0
429 ) {
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000430 qualify_one(i, bitflag, not, p);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100431 rc = 0;
432 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000433 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100434 }
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000435
Roland McGrathfe6b3522005-02-02 04:40:11 +0000436 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000437}
438
439static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000440qual_signal(const char *s, const unsigned int bitflag, const int not)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000441{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000442 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000443
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100444 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000445 int signo = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100446 if (signo < 0 || signo > 255)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000447 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000448 qualify_one(signo, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000449 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000450 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000451 if (strncasecmp(s, "SIG", 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000452 s += 3;
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100453 for (i = 0; i <= NSIG; i++) {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000454 if (strcasecmp(s, signame(i) + 3) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000455 qualify_one(i, bitflag, not, -1);
Roland McGrath76421df2005-02-02 03:51:18 +0000456 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000457 }
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100458 }
Roland McGrath76421df2005-02-02 03:51:18 +0000459 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000460}
461
462static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000463qual_desc(const char *s, const unsigned int bitflag, const int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000464{
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100465 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000466 int desc = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100467 if (desc < 0 || desc > 0x7fff) /* paranoia */
Roland McGrathfe6b3522005-02-02 04:40:11 +0000468 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000469 qualify_one(desc, bitflag, not, -1);
Roland McGrath2b619022003-04-10 18:58:20 +0000470 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000471 }
472 return -1;
473}
474
475static int
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000476lookup_class(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000477{
478 if (strcmp(s, "file") == 0)
479 return TRACE_FILE;
480 if (strcmp(s, "ipc") == 0)
481 return TRACE_IPC;
482 if (strcmp(s, "network") == 0)
483 return TRACE_NETWORK;
484 if (strcmp(s, "process") == 0)
485 return TRACE_PROCESS;
486 if (strcmp(s, "signal") == 0)
487 return TRACE_SIGNAL;
Roland McGrath2fe7b132005-07-05 03:25:35 +0000488 if (strcmp(s, "desc") == 0)
489 return TRACE_DESC;
Namhyung Kim96792962012-10-24 11:41:57 +0900490 if (strcmp(s, "memory") == 0)
491 return TRACE_MEMORY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000492 return -1;
493}
494
495void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000496qualify(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000497{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000498 const struct qual_options *opt;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000499 char *copy;
500 const char *p;
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000501 int not;
502 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000503
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100504 if (num_quals == 0)
505 reallocate_qual(MIN_QUALS);
506
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000507 opt = &qual_options[0];
508 for (i = 0; (p = qual_options[i].option_name); i++) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000509 unsigned int len = strlen(p);
510 if (strncmp(s, p, len) == 0 && s[len] == '=') {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000511 opt = &qual_options[i];
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000512 s += len + 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000513 break;
514 }
515 }
516 not = 0;
517 if (*s == '!') {
518 not = 1;
519 s++;
520 }
521 if (strcmp(s, "none") == 0) {
522 not = 1 - not;
523 s = "all";
524 }
525 if (strcmp(s, "all") == 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100526 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000527 qualify_one(i, opt->bitflag, not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000528 }
529 return;
530 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100531 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000532 qualify_one(i, opt->bitflag, !not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000533 }
Denys Vlasenko5d645812011-08-20 12:48:18 +0200534 copy = strdup(s);
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200535 if (!copy)
536 die_out_of_memory();
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000537 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000538 int n;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000539 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100540 unsigned pers;
541 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
542 for (i = 0; i < nsyscall_vec[pers]; i++)
543 if (sysent_vec[pers][i].sys_flags & n)
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000544 qualify_one(i, opt->bitflag, not, pers);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100545 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000546 continue;
547 }
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000548 if (opt->qualify(p, opt->bitflag, not)) {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100549 error_msg_and_die("invalid %s '%s'",
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000550 opt->argument_name, p);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000551 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000552 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000553 free(copy);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000554 return;
555}
556
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000557#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000558static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000559decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000560{
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000561 unsigned long addr;
Dmitry V. Levinc2155692015-03-22 15:52:40 +0000562 unsigned int n;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000563
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000564 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
565 return;
566
567 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100568 tcp->qual_flg = qual_flags[tcp->scno];
569 tcp->s_ent = &sysent[tcp->scno];
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000570 addr = tcp->u_arg[1];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100571 n = tcp->s_ent->nargs;
Dmitry V. Levinc2155692015-03-22 15:52:40 +0000572 if (sizeof(tcp->u_arg[0]) == current_wordsize) {
573 memset(tcp->u_arg, 0, n * sizeof(tcp->u_arg[0]));
574 (void) umoven(tcp, addr, n * sizeof(tcp->u_arg[0]), tcp->u_arg);
575 } else {
576 unsigned int args[n];
577 unsigned int i;
578
579 memset(args, 0, sizeof(args));
580 (void) umove(tcp, addr, &args);
581 for (i = 0; i < n; ++i)
582 tcp->u_arg[i] = args[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000583 }
584}
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000585#endif
Mike Frysinger3362e892012-03-15 01:09:19 -0400586
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000587#ifdef SYS_ipc_subcall
588static void
589decode_ipc_subcall(struct tcb *tcp)
590{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100591 unsigned int i, n;
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000592
593 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
594 return;
595
596 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100597 tcp->qual_flg = qual_flags[tcp->scno];
598 tcp->s_ent = &sysent[tcp->scno];
599 n = tcp->s_ent->nargs;
600 for (i = 0; i < n; i++)
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000601 tcp->u_arg[i] = tcp->u_arg[i + 1];
602}
603#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000604
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200605int
606printargs(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000607{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200608 if (entering(tcp)) {
609 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100610 int n = tcp->s_ent->nargs;
611 for (i = 0; i < n; i++)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200612 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
613 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000614 return 0;
615}
616
Denys Vlasenko72879c62012-02-27 14:18:02 +0100617int
618printargs_lu(struct tcb *tcp)
619{
620 if (entering(tcp)) {
621 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100622 int n = tcp->s_ent->nargs;
623 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100624 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
625 }
626 return 0;
627}
628
629int
630printargs_ld(struct tcb *tcp)
631{
632 if (entering(tcp)) {
633 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100634 int n = tcp->s_ent->nargs;
635 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100636 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
637 }
638 return 0;
639}
640
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000641static void
642dumpio(struct tcb *tcp)
643{
644 int (*func)();
645
646 if (syserror(tcp))
647 return;
648 if ((unsigned long) tcp->u_arg[0] >= num_quals)
649 return;
650 func = tcp->s_ent->sys_func;
651 if (func == printargs)
652 return;
653 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
654 if (func == sys_read ||
655 func == sys_pread ||
656 func == sys_recv ||
657 func == sys_recvfrom) {
658 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
659 return;
660 } else if (func == sys_readv) {
661 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
662 return;
663#if HAVE_SENDMSG
664 } else if (func == sys_recvmsg) {
665 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
666 return;
667 } else if (func == sys_recvmmsg) {
668 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
669 return;
670#endif
671 }
672 }
673 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
674 if (func == sys_write ||
675 func == sys_pwrite ||
676 func == sys_send ||
677 func == sys_sendto)
678 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
679 else if (func == sys_writev)
680 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
681#if HAVE_SENDMSG
682 else if (func == sys_sendmsg)
683 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
684 else if (func == sys_sendmmsg)
685 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
686#endif
687 }
688}
689
690/*
691 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
692 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
693 */
694#if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
695static long
696shuffle_scno(unsigned long scno)
697{
698 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
699 return scno;
700
701 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
702 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
703 return 0x000ffff0;
704 if (scno == 0x000ffff0)
705 return ARM_FIRST_SHUFFLED_SYSCALL;
706
707#define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
708 /*
709 * Is it ARM specific syscall?
710 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
711 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
712 */
713 if (scno >= 0x000f0000 &&
714 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
715 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
716 }
717 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
718 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
719 }
720
721 return scno;
722}
723#else
724# define shuffle_scno(scno) ((long)(scno))
725#endif
726
727static char*
728undefined_scno_name(struct tcb *tcp)
729{
730 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
731
732 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
733 return buf;
734}
735
736static long get_regs_error;
737
738void
739clear_regs(void)
740{
741 get_regs_error = -1;
742}
743
744static int get_syscall_args(struct tcb *);
745static int get_syscall_result(struct tcb *);
746
747static int
748trace_syscall_entering(struct tcb *tcp)
749{
750 int res, scno_good;
751
752 scno_good = res = get_scno(tcp);
753 if (res == 0)
754 return res;
755 if (res == 1)
756 res = get_syscall_args(tcp);
757
758 if (res != 1) {
759 printleader(tcp);
760 if (scno_good != 1)
761 tprints("????" /* anti-trigraph gap */ "(");
762 else if (tcp->qual_flg & UNDEFINED_SCNO)
763 tprintf("%s(", undefined_scno_name(tcp));
764 else
765 tprintf("%s(", tcp->s_ent->sys_name);
766 /*
767 * " <unavailable>" will be added later by the code which
768 * detects ptrace errors.
769 */
770 goto ret;
771 }
772
773 if ( sys_execve == tcp->s_ent->sys_func
774# if defined(SPARC) || defined(SPARC64)
775 || sys_execv == tcp->s_ent->sys_func
776# endif
777 ) {
778 hide_log_until_execve = 0;
779 }
780
781#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
782 while (1) {
783# ifdef SYS_socket_subcall
784 if (tcp->s_ent->sys_func == sys_socketcall) {
785 decode_socket_subcall(tcp);
786 break;
787 }
788# endif
789# ifdef SYS_ipc_subcall
790 if (tcp->s_ent->sys_func == sys_ipc) {
791 decode_ipc_subcall(tcp);
792 break;
793 }
794# endif
795 break;
796 }
797#endif
798
799 if (!(tcp->qual_flg & QUAL_TRACE)
800 || (tracing_paths && !pathtrace_match(tcp))
801 ) {
802 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
803 return 0;
804 }
805
806 tcp->flags &= ~TCB_FILTERED;
807
808 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
809 res = 0;
810 goto ret;
811 }
812
813#ifdef USE_LIBUNWIND
814 if (stack_trace_enabled) {
815 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
816 unwind_capture_stacktrace(tcp);
817 }
818#endif
819
820 printleader(tcp);
821 if (tcp->qual_flg & UNDEFINED_SCNO)
822 tprintf("%s(", undefined_scno_name(tcp));
823 else
824 tprintf("%s(", tcp->s_ent->sys_name);
825 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
826 res = printargs(tcp);
827 else
828 res = tcp->s_ent->sys_func(tcp);
829
830 fflush(tcp->outf);
831 ret:
832 tcp->flags |= TCB_INSYSCALL;
833 /* Measure the entrance time as late as possible to avoid errors. */
834 if (Tflag || cflag)
835 gettimeofday(&tcp->etime, NULL);
836 return res;
837}
838
839static int
840trace_syscall_exiting(struct tcb *tcp)
841{
842 int sys_res;
843 struct timeval tv;
844 int res;
845 long u_error;
846
847 /* Measure the exit time as early as possible to avoid errors. */
848 if (Tflag || cflag)
849 gettimeofday(&tv, NULL);
850
851#ifdef USE_LIBUNWIND
852 if (stack_trace_enabled) {
853 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
854 unwind_cache_invalidate(tcp);
855 }
856#endif
857
858#if SUPPORTED_PERSONALITIES > 1
859 update_personality(tcp, tcp->currpers);
860#endif
861 res = (get_regs_error ? -1 : get_syscall_result(tcp));
862 if (res == 1) {
863 if (filtered(tcp) || hide_log_until_execve)
864 goto ret;
865 }
866
867 if (cflag) {
868 count_syscall(tcp, &tv);
869 if (cflag == CFLAG_ONLY_STATS) {
870 goto ret;
871 }
872 }
873
874 /* If not in -ff mode, and printing_tcp != tcp,
875 * then the log currently does not end with output
876 * of _our syscall entry_, but with something else.
877 * We need to say which syscall's return is this.
878 *
879 * Forced reprinting via TCB_REPRINT is used only by
880 * "strace -ff -oLOG test/threaded_execve" corner case.
881 * It's the only case when -ff mode needs reprinting.
882 */
883 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
884 tcp->flags &= ~TCB_REPRINT;
885 printleader(tcp);
886 if (tcp->qual_flg & UNDEFINED_SCNO)
887 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
888 else
889 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
890 }
891 printing_tcp = tcp;
892
893 tcp->s_prev_ent = NULL;
894 if (res != 1) {
895 /* There was error in one of prior ptrace ops */
896 tprints(") ");
897 tabto();
898 tprints("= ? <unavailable>\n");
899 line_ended();
900 tcp->flags &= ~TCB_INSYSCALL;
901 return res;
902 }
903 tcp->s_prev_ent = tcp->s_ent;
904
905 sys_res = 0;
906 if (tcp->qual_flg & QUAL_RAW) {
907 /* sys_res = printargs(tcp); - but it's nop on sysexit */
908 } else {
909 /* FIXME: not_failing_only (IOW, option -z) is broken:
910 * failure of syscall is known only after syscall return.
911 * Thus we end up with something like this on, say, ENOENT:
912 * open("doesnt_exist", O_RDONLY <unfinished ...>
913 * {next syscall decode}
914 * whereas the intended result is that open(...) line
915 * is not shown at all.
916 */
917 if (not_failing_only && tcp->u_error)
918 goto ret; /* ignore failed syscalls */
919 sys_res = tcp->s_ent->sys_func(tcp);
920 }
921
922 tprints(") ");
923 tabto();
924 u_error = tcp->u_error;
925 if (tcp->qual_flg & QUAL_RAW) {
926 if (u_error)
927 tprintf("= -1 (errno %ld)", u_error);
928 else
929 tprintf("= %#lx", tcp->u_rval);
930 }
931 else if (!(sys_res & RVAL_NONE) && u_error) {
932 switch (u_error) {
933 /* Blocked signals do not interrupt any syscalls.
934 * In this case syscalls don't return ERESTARTfoo codes.
935 *
936 * Deadly signals set to SIG_DFL interrupt syscalls
937 * and kill the process regardless of which of the codes below
938 * is returned by the interrupted syscall.
939 * In some cases, kernel forces a kernel-generated deadly
940 * signal to be unblocked and set to SIG_DFL (and thus cause
941 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
942 * or SIGILL. (The alternative is to leave process spinning
943 * forever on the faulty instruction - not useful).
944 *
945 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
946 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
947 * but kernel will always restart them.
948 */
949 case ERESTARTSYS:
950 /* Most common type of signal-interrupted syscall exit code.
951 * The system call will be restarted with the same arguments
952 * if SA_RESTART is set; otherwise, it will fail with EINTR.
953 */
954 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
955 break;
956 case ERESTARTNOINTR:
957 /* Rare. For example, fork() returns this if interrupted.
958 * SA_RESTART is ignored (assumed set): the restart is unconditional.
959 */
960 tprints("= ? ERESTARTNOINTR (To be restarted)");
961 break;
962 case ERESTARTNOHAND:
963 /* pause(), rt_sigsuspend() etc use this code.
964 * SA_RESTART is ignored (assumed not set):
965 * syscall won't restart (will return EINTR instead)
966 * even after signal with SA_RESTART set. However,
967 * after SIG_IGN or SIG_DFL signal it will restart
968 * (thus the name "restart only if has no handler").
969 */
970 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
971 break;
972 case ERESTART_RESTARTBLOCK:
973 /* Syscalls like nanosleep(), poll() which can't be
974 * restarted with their original arguments use this
975 * code. Kernel will execute restart_syscall() instead,
976 * which changes arguments before restarting syscall.
977 * SA_RESTART is ignored (assumed not set) similarly
978 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
979 * since restart data is saved in "restart block"
980 * in task struct, and if signal handler uses a syscall
981 * which in turn saves another such restart block,
982 * old data is lost and restart becomes impossible)
983 */
984 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
985 break;
986 default:
987 if ((unsigned long) u_error < nerrnos
988 && errnoent[u_error])
989 tprintf("= -1 %s (%s)", errnoent[u_error],
990 strerror(u_error));
991 else
992 tprintf("= -1 ERRNO_%lu (%s)", u_error,
993 strerror(u_error));
994 break;
995 }
996 if ((sys_res & RVAL_STR) && tcp->auxstr)
997 tprintf(" (%s)", tcp->auxstr);
998 }
999 else {
1000 if (sys_res & RVAL_NONE)
1001 tprints("= ?");
1002 else {
1003 switch (sys_res & RVAL_MASK) {
1004 case RVAL_HEX:
1005#if SUPPORTED_PERSONALITIES > 1
1006 if (current_wordsize < sizeof(long))
1007 tprintf("= %#x",
1008 (unsigned int) tcp->u_rval);
1009 else
1010#endif
1011 tprintf("= %#lx", tcp->u_rval);
1012 break;
1013 case RVAL_OCTAL:
1014 tprintf("= %#lo", tcp->u_rval);
1015 break;
1016 case RVAL_UDECIMAL:
1017 tprintf("= %lu", tcp->u_rval);
1018 break;
1019 case RVAL_DECIMAL:
1020 tprintf("= %ld", tcp->u_rval);
1021 break;
1022 case RVAL_FD:
1023 if (show_fd_path) {
1024 tprints("= ");
1025 printfd(tcp, tcp->u_rval);
1026 }
1027 else
1028 tprintf("= %ld", tcp->u_rval);
1029 break;
1030#if defined(LINUX_MIPSN32) || defined(X32)
1031 /*
1032 case RVAL_LHEX:
1033 tprintf("= %#llx", tcp->u_lrval);
1034 break;
1035 case RVAL_LOCTAL:
1036 tprintf("= %#llo", tcp->u_lrval);
1037 break;
1038 */
1039 case RVAL_LUDECIMAL:
1040 tprintf("= %llu", tcp->u_lrval);
1041 break;
1042 /*
1043 case RVAL_LDECIMAL:
1044 tprintf("= %lld", tcp->u_lrval);
1045 break;
1046 */
1047#endif
1048 default:
1049 fprintf(stderr,
1050 "invalid rval format\n");
1051 break;
1052 }
1053 }
1054 if ((sys_res & RVAL_STR) && tcp->auxstr)
1055 tprintf(" (%s)", tcp->auxstr);
1056 }
1057 if (Tflag) {
1058 tv_sub(&tv, &tv, &tcp->etime);
1059 tprintf(" <%ld.%06ld>",
1060 (long) tv.tv_sec, (long) tv.tv_usec);
1061 }
1062 tprints("\n");
1063 dumpio(tcp);
1064 line_ended();
1065
1066#ifdef USE_LIBUNWIND
1067 if (stack_trace_enabled)
1068 unwind_print_stacktrace(tcp);
1069#endif
1070
1071 ret:
1072 tcp->flags &= ~TCB_INSYSCALL;
1073 return 0;
1074}
1075
1076int
1077trace_syscall(struct tcb *tcp)
1078{
1079 return exiting(tcp) ?
1080 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
1081}
1082
1083/*
1084 * Cannot rely on __kernel_[u]long_t being defined,
1085 * it is quite a recent feature of <asm/posix_types.h>.
1086 */
1087#ifdef __kernel_long_t
1088typedef __kernel_long_t kernel_long_t;
1089typedef __kernel_ulong_t kernel_ulong_t;
1090#else
1091# ifdef X32
1092typedef long long kernel_long_t;
1093typedef unsigned long long kernel_ulong_t;
1094# else
1095typedef long kernel_long_t;
1096typedef unsigned long kernel_ulong_t;
1097# endif
1098#endif
1099
1100/*
1101 * Check the syscall return value register value for whether it is
1102 * a negated errno code indicating an error, or a success return value.
1103 */
1104static inline bool
1105is_negated_errno(kernel_ulong_t val)
1106{
1107 /* Linux kernel defines MAX_ERRNO to 4095. */
1108 kernel_ulong_t max = -(kernel_long_t) 4095;
1109
1110#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1111 if (current_wordsize < sizeof(val)) {
1112 val = (uint32_t) val;
1113 max = (uint32_t) max;
1114 }
1115#elif defined X32
1116 /*
1117 * current_wordsize is 4 even in personality 0 (native X32)
1118 * but truncation _must not_ be done in it.
1119 * can't check current_wordsize here!
1120 */
1121 if (current_personality != 0) {
1122 val = (uint32_t) val;
1123 max = (uint32_t) max;
1124 }
1125#endif
1126
1127 return val >= max;
1128}
1129
1130
Denys Vlasenko523635f2012-02-25 02:44:25 +01001131#if defined(I386)
Denys Vlasenkob51f3642013-07-16 12:06:25 +02001132static struct user_regs_struct i386_regs;
Dmitry V. Levinf97a4772015-02-15 00:08:11 +00001133long *const i386_esp_ptr = &i386_regs.esp;
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001134# define ARCH_REGS_FOR_GETREGS i386_regs
H.J. Lu35be5812012-04-16 13:00:01 +02001135#elif defined(X86_64) || defined(X32)
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +01001136/*
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001137 * On i386, pt_regs and user_regs_struct are the same,
1138 * but on 64 bit x86, user_regs_struct has six more fields:
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +01001139 * fs_base, gs_base, ds, es, fs, gs.
1140 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
1141 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001142struct i386_user_regs_struct {
1143 uint32_t ebx;
1144 uint32_t ecx;
1145 uint32_t edx;
1146 uint32_t esi;
1147 uint32_t edi;
1148 uint32_t ebp;
1149 uint32_t eax;
1150 uint32_t xds;
1151 uint32_t xes;
1152 uint32_t xfs;
1153 uint32_t xgs;
1154 uint32_t orig_eax;
1155 uint32_t eip;
1156 uint32_t xcs;
1157 uint32_t eflags;
1158 uint32_t esp;
1159 uint32_t xss;
1160};
1161static union {
1162 struct user_regs_struct x86_64_r;
1163 struct i386_user_regs_struct i386_r;
1164} x86_regs_union;
1165# define x86_64_regs x86_regs_union.x86_64_r
1166# define i386_regs x86_regs_union.i386_r
Denys Vlasenkob51f3642013-07-16 12:06:25 +02001167uint32_t *const i386_esp_ptr = &i386_regs.esp;
Dmitry V. Levinccb4fda2015-03-04 12:19:55 +00001168uint64_t *const x86_64_rsp_ptr = (uint64_t *) &x86_64_regs.rsp;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001169static struct iovec x86_io = {
1170 .iov_base = &x86_regs_union
1171};
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001172# define ARCH_REGS_FOR_GETREGSET x86_regs_union
1173# define ARCH_IOVEC_FOR_GETREGSET x86_io
Denys Vlasenko523635f2012-02-25 02:44:25 +01001174#elif defined(IA64)
Dmitry V. Levin48f08902015-03-05 23:30:02 +00001175static struct pt_all_user_regs ia64_regs;
1176unsigned long *const ia64_frame_ptr = &ia64_regs.gr[12];
1177# define IA64_PSR_IS ((long)1 << 34)
1178# define ia64_ia32mode (ia64_regs.cr_ipsr & IA64_PSR_IS)
1179# define ARCH_REGS_FOR_GETREGS ia64_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +01001180#elif defined(POWERPC)
Dmitry V. Levin23ce9e42015-02-08 13:05:53 +00001181struct pt_regs ppc_regs; /* not static */
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001182# define ARCH_REGS_FOR_GETREGS ppc_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +01001183#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001184static long m68k_d0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001185#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001186static long bfin_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001187#elif defined(ARM)
Dmitry V. Levin5b9b7e12015-03-04 23:05:53 +00001188static struct pt_regs arm_regs;
1189long *const arm_sp_ptr = &arm_regs.ARM_sp;
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001190# define ARCH_REGS_FOR_GETREGS arm_regs
Steve McIntyred8d3bd32012-10-24 17:58:16 +01001191#elif defined(AARCH64)
Dmitry V. Levin5503dd22015-02-13 02:12:14 +00001192struct arm_pt_regs {
1193 int uregs[18];
1194};
1195# define ARM_cpsr uregs[16]
1196# define ARM_pc uregs[15]
1197# define ARM_lr uregs[14]
1198# define ARM_sp uregs[13]
1199# define ARM_ip uregs[12]
1200# define ARM_fp uregs[11]
1201# define ARM_r10 uregs[10]
1202# define ARM_r9 uregs[9]
1203# define ARM_r8 uregs[8]
1204# define ARM_r7 uregs[7]
1205# define ARM_r6 uregs[6]
1206# define ARM_r5 uregs[5]
1207# define ARM_r4 uregs[4]
1208# define ARM_r3 uregs[3]
1209# define ARM_r2 uregs[2]
1210# define ARM_r1 uregs[1]
1211# define ARM_r0 uregs[0]
1212# define ARM_ORIG_r0 uregs[17]
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001213static union {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001214 struct user_pt_regs aarch64_r;
1215 struct arm_pt_regs arm_r;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001216} arm_regs_union;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001217# define aarch64_regs arm_regs_union.aarch64_r
1218# define arm_regs arm_regs_union.arm_r
Dmitry V. Levin47e289b2015-03-05 04:10:52 +00001219uint64_t *const aarch64_sp_ptr = (uint64_t *) &aarch64_regs.sp;
1220uint32_t *const arm_sp_ptr = (uint32_t *) &arm_regs.ARM_sp;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001221static struct iovec aarch64_io = {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001222 .iov_base = &arm_regs_union
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001223};
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001224# define ARCH_REGS_FOR_GETREGSET arm_regs_union
1225# define ARCH_IOVEC_FOR_GETREGSET aarch64_io
Denys Vlasenko523635f2012-02-25 02:44:25 +01001226#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001227static long alpha_r0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001228static long alpha_a3;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001229#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001230static struct pt_regs avr32_regs;
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001231# define ARCH_REGS_FOR_GETREGS avr32_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +01001232#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001233struct pt_regs sparc_regs; /* not static */
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001234# define ARCH_REGS_FOR_GETREGS sparc_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +01001235#elif defined(MIPS)
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +00001236struct mips_regs mips_regs; /* not static */
1237/* PTRACE_GETREGS on MIPS is available since linux v2.6.15. */
1238# define ARCH_REGS_FOR_GETREGS mips_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +01001239#elif defined(S390) || defined(S390X)
Dmitry V. Levin20eca8a2015-03-05 22:10:15 +00001240/* PTRACE_GETREGSET on S390 is available since linux v2.6.27. */
1241static struct user_regs_struct s390_regset;
1242unsigned long *const s390_frame_ptr = &s390_regset.gprs[15];
1243# define ARCH_REGS_FOR_GETREGSET s390_regset
Denys Vlasenko523635f2012-02-25 02:44:25 +01001244#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001245static long hppa_r28;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001246#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001247static long sh_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001248#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001249static long sh64_r9;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001250#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001251static long cris_r10;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001252#elif defined(TILE)
Dmitry V. Levin23ce9e42015-02-08 13:05:53 +00001253struct pt_regs tile_regs; /* not static */
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001254# define ARCH_REGS_FOR_GETREGS tile_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +01001255#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001256static long microblaze_r3;
Christian Svensson492f81f2013-02-14 13:26:27 +01001257#elif defined(OR1K)
1258static struct user_regs_struct or1k_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001259# define ARCH_REGS_FOR_GETREGSET or1k_regs
James Hogan5f999a82013-02-22 14:44:10 +00001260#elif defined(METAG)
1261static struct user_gp_regs metag_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001262# define ARCH_REGS_FOR_GETREGSET metag_regs
Chris Zankel8f636ed2013-03-25 10:22:07 -07001263#elif defined(XTENSA)
1264static long xtensa_a2;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301265# elif defined(ARC)
1266static struct user_regs_struct arc_regs;
1267# define ARCH_REGS_FOR_GETREGSET arc_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +01001268#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +00001269
Dmitry V. Levin78ed3f32015-03-23 00:04:27 +00001270#ifdef HAVE_GETRVAL2
Dmitry V. Levin48f08902015-03-05 23:30:02 +00001271long
1272getrval2(struct tcb *tcp)
1273{
1274 long val;
1275
1276# if defined(SPARC) || defined(SPARC64)
1277 val = sparc_regs.u_regs[U_REG_O1];
1278# elif defined(SH)
1279 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
1280 return -1;
Dmitry V. Levin78ed3f32015-03-23 00:04:27 +00001281# elif defined ALPHA
1282 if (upeek(tcp->pid, 20, &val) < 0)
1283 return -1;
Dmitry V. Levin48f08902015-03-05 23:30:02 +00001284# elif defined(IA64)
1285 val = ia64_regs.gr[9];
1286# endif
1287
1288 return val;
1289}
1290#endif
1291
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001292void
Denys Vlasenko5a2483b2013-07-01 12:49:14 +02001293print_pc(struct tcb *tcp)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001294{
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001295 const char *fmt;
1296 const char *bad;
1297
Dmitry V. Levinb2d9ff22015-02-23 13:43:20 +00001298#ifdef current_wordsize
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001299# define pc_wordsize current_wordsize
Dmitry V. Levinb2d9ff22015-02-23 13:43:20 +00001300#else
1301# define pc_wordsize personality_wordsize[tcp->currpers]
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001302#endif
1303
1304 if (pc_wordsize == 4) {
1305 fmt = "[%08lx] ";
1306 bad = "[????????] ";
1307 } else {
1308 fmt = "[%016lx] ";
1309 bad = "[????????????????] ";
1310 }
1311
1312#undef pc_wordsize
1313#define PRINTBADPC tprints(bad)
1314
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001315 if (get_regs_error) {
1316 PRINTBADPC;
1317 return;
1318 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001319
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001320#if defined(I386)
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001321 tprintf(fmt, i386_regs.eip);
1322#elif defined(X86_64) || defined(X32)
1323 if (x86_io.iov_len == sizeof(i386_regs))
1324 tprintf(fmt, (unsigned long) i386_regs.eip);
1325 else
1326 tprintf(fmt, (unsigned long) x86_64_regs.rip);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001327#elif defined(S390) || defined(S390X)
Dmitry V. Levin20eca8a2015-03-05 22:10:15 +00001328 tprintf(fmt, s390_regset.psw.addr);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001329#elif defined(IA64)
Dmitry V. Levin48f08902015-03-05 23:30:02 +00001330 tprintf(fmt, ia64_regs.br[0]);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001331#elif defined(POWERPC)
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001332 tprintf(fmt, ppc_regs.nip);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001333#elif defined(M68K)
1334 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001335 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001336 PRINTBADPC;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001337 return;
1338 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001339 tprintf(fmt, pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001340#elif defined(ALPHA)
1341 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001342 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001343 PRINTBADPC;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001344 return;
1345 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001346 tprintf(fmt, pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001347#elif defined(SPARC)
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001348 tprintf(fmt, sparc_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001349#elif defined(SPARC64)
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001350 tprintf(fmt, sparc_regs.tpc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001351#elif defined(HPPA)
1352 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001353 if (upeek(tcp->pid, PT_IAOQ0, &pc) < 0) {
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001354 PRINTBADPC;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001355 return;
1356 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001357 tprintf(fmt, pc);
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +00001358#elif defined MIPS
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001359 tprintf(fmt, (unsigned long) mips_REG_EPC);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001360#elif defined(SH)
1361 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001362 if (upeek(tcp->pid, 4*REG_PC, &pc) < 0) {
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001363 PRINTBADPC;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001364 return;
1365 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001366 tprintf(fmt, pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001367#elif defined(SH64)
1368 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001369 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001370 PRINTBADPC;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001371 return;
1372 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001373 tprintf(fmt, pc);
Dmitry V. Levind819fe82015-03-02 03:38:27 +00001374#elif defined(AARCH64)
1375 if (aarch64_io.iov_len == sizeof(arm_regs))
1376 tprintf(fmt, (unsigned long) arm_regs.ARM_pc);
1377 else
1378 tprintf(fmt, (unsigned long) aarch64_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001379#elif defined(ARM)
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001380 tprintf(fmt, arm_regs.ARM_pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001381#elif defined(AVR32)
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001382 tprintf(fmt, avr32_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001383#elif defined(BFIN)
1384 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001385 if (upeek(tcp->pid, PT_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001386 PRINTBADPC;
1387 return;
1388 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001389 tprintf(fmt, pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001390#elif defined(CRISV10)
1391 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001392 if (upeek(tcp->pid, 4*PT_IRP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001393 PRINTBADPC;
1394 return;
1395 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001396 tprintf(fmt, pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001397#elif defined(CRISV32)
1398 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001399 if (upeek(tcp->pid, 4*PT_ERP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001400 PRINTBADPC;
1401 return;
1402 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001403 tprintf(fmt, pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001404#elif defined(TILE)
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001405 tprintf(fmt, (unsigned long) tile_regs.pc);
Christian Svensson492f81f2013-02-14 13:26:27 +01001406#elif defined(OR1K)
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001407 tprintf(fmt, or1k_regs.pc);
James Hogan5f999a82013-02-22 14:44:10 +00001408#elif defined(METAG)
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001409 tprintf(fmt, metag_regs.pc);
Chris Zankel8f636ed2013-03-25 10:22:07 -07001410#elif defined(XTENSA)
1411 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001412 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Chris Zankel8f636ed2013-03-25 10:22:07 -07001413 PRINTBADPC;
1414 return;
1415 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001416 tprintf(fmt, pc);
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301417#elif defined(ARC)
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001418 tprintf(fmt, arc_regs.efa);
1419#else
1420# warning print_pc is not implemented for this architecture
1421 PRINTBADPC;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001422#endif /* architecture */
1423}
1424
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001425#ifdef X86_64
Dmitry V. Levincf7248d2015-01-11 17:26:29 +00001426/*
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001427 * PTRACE_GETREGSET was added to the kernel in v2.6.25,
1428 * a PTRACE_GETREGS based fallback is provided for old kernels.
Denys Vlasenko7270de52013-02-21 15:46:34 +01001429 */
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001430static void
1431x86_64_getregs_old(pid_t pid)
Denys Vlasenko7270de52013-02-21 15:46:34 +01001432{
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001433 /* Use old method, with unreliable heuristical detection of 32-bitness. */
1434 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &x86_64_regs);
1435 if (get_regs_error)
1436 return;
Denys Vlasenko7270de52013-02-21 15:46:34 +01001437
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001438 if (x86_64_regs.cs == 0x23) {
1439 x86_io.iov_len = sizeof(i386_regs);
1440 /*
1441 * The order is important: i386_regs and x86_64_regs
1442 * are overlaid in memory!
1443 */
1444 i386_regs.ebx = x86_64_regs.rbx;
1445 i386_regs.ecx = x86_64_regs.rcx;
1446 i386_regs.edx = x86_64_regs.rdx;
1447 i386_regs.esi = x86_64_regs.rsi;
1448 i386_regs.edi = x86_64_regs.rdi;
1449 i386_regs.ebp = x86_64_regs.rbp;
1450 i386_regs.eax = x86_64_regs.rax;
1451 /* i386_regs.xds = x86_64_regs.ds; unused by strace */
1452 /* i386_regs.xes = x86_64_regs.es; ditto... */
1453 /* i386_regs.xfs = x86_64_regs.fs; */
1454 /* i386_regs.xgs = x86_64_regs.gs; */
1455 i386_regs.orig_eax = x86_64_regs.orig_rax;
1456 i386_regs.eip = x86_64_regs.rip;
1457 /* i386_regs.xcs = x86_64_regs.cs; */
1458 /* i386_regs.eflags = x86_64_regs.eflags; */
1459 i386_regs.esp = x86_64_regs.rsp;
1460 /* i386_regs.xss = x86_64_regs.ss; */
1461 } else {
1462 x86_io.iov_len = sizeof(x86_64_regs);
Denys Vlasenko7270de52013-02-21 15:46:34 +01001463 }
Denys Vlasenko7270de52013-02-21 15:46:34 +01001464}
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001465#endif /* X86_64 */
Denys Vlasenko7270de52013-02-21 15:46:34 +01001466
Anton Blanchard14d51a62013-06-26 14:42:37 +10001467#ifdef POWERPC
1468/*
Denys Vlasenko7f5a1322013-06-28 14:36:39 +02001469 * PTRACE_GETREGS was added to the PowerPC kernel in v2.6.23,
1470 * we provide a slow fallback for old kernels.
Anton Blanchard14d51a62013-06-26 14:42:37 +10001471 */
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001472static int
1473powerpc_getregs_old(pid_t pid)
Anton Blanchard14d51a62013-06-26 14:42:37 +10001474{
1475 int i;
1476 long r;
1477
Denys Vlasenko6b3016e2013-06-28 14:51:50 +02001478 if (iflag) {
Anton Blanchard9459dfb2013-07-12 12:24:02 +02001479 r = upeek(pid, sizeof(long) * PT_NIP, (long *)&ppc_regs.nip);
Denys Vlasenko6b3016e2013-06-28 14:51:50 +02001480 if (r)
1481 goto out;
1482 }
1483#ifdef POWERPC64 /* else we never use it */
Anton Blanchard9459dfb2013-07-12 12:24:02 +02001484 r = upeek(pid, sizeof(long) * PT_MSR, (long *)&ppc_regs.msr);
Anton Blanchard14d51a62013-06-26 14:42:37 +10001485 if (r)
1486 goto out;
Denys Vlasenko6b3016e2013-06-28 14:51:50 +02001487#endif
Anton Blanchard9459dfb2013-07-12 12:24:02 +02001488 r = upeek(pid, sizeof(long) * PT_CCR, (long *)&ppc_regs.ccr);
Anton Blanchard14d51a62013-06-26 14:42:37 +10001489 if (r)
1490 goto out;
Anton Blanchard9459dfb2013-07-12 12:24:02 +02001491 r = upeek(pid, sizeof(long) * PT_ORIG_R3, (long *)&ppc_regs.orig_gpr3);
Anton Blanchard14d51a62013-06-26 14:42:37 +10001492 if (r)
1493 goto out;
Anton Blanchard14d51a62013-06-26 14:42:37 +10001494 for (i = 0; i <= 8; i++) {
Anton Blanchard9459dfb2013-07-12 12:24:02 +02001495 r = upeek(pid, sizeof(long) * (PT_R0 + i),
1496 (long *)&ppc_regs.gpr[i]);
Anton Blanchard14d51a62013-06-26 14:42:37 +10001497 if (r)
1498 goto out;
1499 }
Denys Vlasenko7f5a1322013-06-28 14:36:39 +02001500 out:
Anton Blanchard14d51a62013-06-26 14:42:37 +10001501 return r;
1502}
1503#endif
1504
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001505#if defined ARCH_REGS_FOR_GETREGSET
1506static long
1507get_regset(pid_t pid)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001508{
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001509# ifdef ARCH_IOVEC_FOR_GETREGSET
1510 /* variable iovec */
1511 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1512 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1513 &ARCH_IOVEC_FOR_GETREGSET);
1514# else
1515 /* constant iovec */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001516 static struct iovec io = {
1517 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1518 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001519 };
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001520 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001521
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001522# endif
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001523}
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001524#endif /* ARCH_REGS_FOR_GETREGSET */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001525
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001526void
1527get_regs(pid_t pid)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001528{
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001529#ifdef ARCH_REGS_FOR_GETREGSET
1530# ifdef X86_64
1531 /* Try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS. */
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001532 static int getregset_support;
1533
1534 if (getregset_support >= 0) {
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001535 get_regs_error = get_regset(pid);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001536 if (getregset_support > 0)
1537 return;
1538 if (get_regs_error >= 0) {
1539 getregset_support = 1;
1540 return;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001541 }
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001542 if (errno == EPERM || errno == ESRCH)
1543 return;
1544 getregset_support = -1;
1545 }
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001546 x86_64_getregs_old(pid);
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001547# else /* !X86_64 */
1548 /* Assume that PTRACE_GETREGSET works. */
1549 get_regs_error = get_regset(pid);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001550# endif
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001551#elif defined ARCH_REGS_FOR_GETREGS
1552# if defined SPARC || defined SPARC64
1553 /* SPARC systems have the meaning of data and addr reversed */
1554 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&ARCH_REGS_FOR_GETREGS, 0);
1555# elif defined POWERPC
1556 static bool old_kernel = 0;
1557 if (old_kernel)
1558 goto old;
1559 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1560 if (get_regs_error && errno == EIO) {
1561 old_kernel = 1;
1562 old:
1563 get_regs_error = powerpc_getregs_old(pid);
1564 }
1565# else
1566 /* Assume that PTRACE_GETREGS works. */
1567 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1568# endif
1569
1570#else /* !ARCH_REGS_FOR_GETREGSET && !ARCH_REGS_FOR_GETREGS */
1571# warning get_regs is not implemented for this architecture yet
1572 get_regs_error = 0;
1573#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001574}
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001575
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001576/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001577 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1578 * 1: ok, continue in trace_syscall_entering().
1579 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001580 * ("????" etc) and bail out.
1581 */
Denys Vlasenko8497b622015-03-21 17:51:52 +01001582int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001583get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001584{
Dmitry V. Levin144cda22015-03-23 18:48:32 +00001585 if (get_regs_error)
1586 return -1;
1587
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001588 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001589
Denys Vlasenko523635f2012-02-25 02:44:25 +01001590#if defined(S390) || defined(S390X)
Dmitry V. Levin20eca8a2015-03-05 22:10:15 +00001591 scno = s390_regset.gprs[2];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001592#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001593 scno = ppc_regs.gpr[0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001594# ifdef POWERPC64
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001595 unsigned int currpers;
Andreas Schwabd69fa492010-07-12 21:39:57 +02001596
James Yanga78d7772014-02-18 15:32:43 -06001597 /*
1598 * Check for 64/32 bit mode.
1599 * Embedded implementations covered by Book E extension of PPC use
1600 * bit 0 (CM) of 32-bit Machine state register (MSR).
1601 * Other implementations use bit 0 (SF) of 64-bit MSR.
1602 */
1603 currpers = (ppc_regs.msr & 0x8000000080000000) ? 0 : 1;
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001604 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001605# endif
1606#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001607 scno = avr32_regs.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001608#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001609 if (upeek(tcp->pid, PT_ORIG_P0, &scno))
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001610 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001611#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001612 scno = i386_regs.orig_eax;
H.J. Lu35be5812012-04-16 13:00:01 +02001613#elif defined(X86_64) || defined(X32)
1614# ifndef __X32_SYSCALL_BIT
1615# define __X32_SYSCALL_BIT 0x40000000
1616# endif
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001617 unsigned int currpers;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001618# if 1
1619 /* GETREGSET of NT_PRSTATUS tells us regset size,
1620 * which unambiguously detects i386.
1621 *
1622 * Linux kernel distinguishes x86-64 and x32 processes
1623 * solely by looking at __X32_SYSCALL_BIT:
1624 * arch/x86/include/asm/compat.h::is_x32_task():
1625 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1626 * return true;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001627 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001628 if (x86_io.iov_len == sizeof(i386_regs)) {
1629 scno = i386_regs.orig_eax;
1630 currpers = 1;
1631 } else {
1632 scno = x86_64_regs.orig_rax;
1633 currpers = 0;
1634 if (scno & __X32_SYSCALL_BIT) {
Dmitry V. Levinc3948322015-02-05 16:15:46 +00001635 /*
1636 * Syscall number -1 requires special treatment:
1637 * it might be a side effect of SECCOMP_RET_ERRNO
1638 * filtering that sets orig_rax to -1
1639 * in some versions of linux kernel.
1640 * If that is the case, then
1641 * __X32_SYSCALL_BIT logic does not apply.
1642 */
1643 if ((long long) x86_64_regs.orig_rax != -1) {
1644 scno -= __X32_SYSCALL_BIT;
1645 currpers = 2;
1646 } else {
1647# ifdef X32
1648 currpers = 2;
1649# endif
1650 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001651 }
1652 }
1653# elif 0
1654 /* cs = 0x33 for long mode (native 64 bit and x32)
1655 * cs = 0x23 for compatibility mode (32 bit)
1656 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1657 */
1658 scno = x86_64_regs.orig_rax;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001659 switch (x86_64_regs.cs) {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001660 case 0x23: currpers = 1; break;
H.J. Lu35be5812012-04-16 13:00:01 +02001661 case 0x33:
1662 if (x86_64_regs.ds == 0x2b) {
1663 currpers = 2;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001664 scno &= ~__X32_SYSCALL_BIT;
H.J. Lu35be5812012-04-16 13:00:01 +02001665 } else
1666 currpers = 0;
1667 break;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001668 default:
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001669 fprintf(stderr, "Unknown value CS=0x%08X while "
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001670 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001671 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001672 currpers = current_personality;
1673 break;
1674 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001675# elif 0
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001676 /* This version analyzes the opcode of a syscall instruction.
1677 * (int 0x80 on i386 vs. syscall on x86-64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001678 * It works, but is too complicated, and strictly speaking, unreliable.
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001679 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001680 unsigned long call, rip = x86_64_regs.rip;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001681 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1682 rip -= 2;
1683 errno = 0;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001684 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001685 if (errno)
1686 fprintf(stderr, "ptrace_peektext failed: %s\n",
1687 strerror(errno));
1688 switch (call & 0xffff) {
1689 /* x86-64: syscall = 0x0f 0x05 */
1690 case 0x050f: currpers = 0; break;
1691 /* i386: int 0x80 = 0xcd 0x80 */
1692 case 0x80cd: currpers = 1; break;
1693 default:
1694 currpers = current_personality;
1695 fprintf(stderr,
1696 "Unknown syscall opcode (0x%04X) while "
1697 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001698 "PID=%d\n", (int)call, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001699 break;
1700 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001701# endif
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001702
H.J. Lu35be5812012-04-16 13:00:01 +02001703# ifdef X32
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001704 /* If we are built for a x32 system, then personality 0 is x32
1705 * (not x86_64), and stracing of x86_64 apps is not supported.
1706 * Stracing of i386 apps is still supported.
H.J. Lu085e4282012-04-17 11:05:04 -07001707 */
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001708 if (currpers == 0) {
1709 fprintf(stderr, "syscall_%lu(...) in unsupported "
1710 "64-bit mode of process PID=%d\n",
1711 scno, tcp->pid);
1712 return 0;
H.J. Lu35be5812012-04-16 13:00:01 +02001713 }
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001714 currpers &= ~2; /* map 2,1 to 0,1 */
H.J. Lu35be5812012-04-16 13:00:01 +02001715# endif
H.J. Lu085e4282012-04-17 11:05:04 -07001716 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001717#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001718 if (ia64_ia32mode) {
Dmitry V. Levin48f08902015-03-05 23:30:02 +00001719 scno = ia64_regs.gr[0];
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001720 } else {
Dmitry V. Levin48f08902015-03-05 23:30:02 +00001721 scno = ia64_regs.gr[15];
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001722 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001723#elif defined(AARCH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001724 switch (aarch64_io.iov_len) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001725 case sizeof(aarch64_regs):
1726 /* We are in 64-bit mode */
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001727 scno = aarch64_regs.regs[8];
1728 update_personality(tcp, 1);
1729 break;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001730 case sizeof(arm_regs):
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001731 /* We are in 32-bit mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001732 /* Note: we don't support OABI, unlike 32-bit ARM build */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001733 scno = arm_regs.ARM_r7;
Elliott Hughes7a28f7f2014-03-03 23:45:26 +00001734 scno = shuffle_scno(scno);
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001735 update_personality(tcp, 0);
1736 break;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001737 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001738#elif defined(ARM)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001739 if (arm_regs.ARM_ip != 0) {
1740 /* It is not a syscall entry */
1741 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001742 tcp->flags |= TCB_INSYSCALL;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001743 return 0;
1744 }
1745 /* Note: we support only 32-bit CPUs, not 26-bit */
1746
Dmitry V. Levin0160e162014-03-18 23:37:43 +00001747# if !defined(__ARM_EABI__) || ENABLE_ARM_OABI
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001748 if (arm_regs.ARM_cpsr & 0x20)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001749 /* Thumb mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001750 goto scno_in_r7;
1751 /* ARM mode */
1752 /* Check EABI/OABI by examining SVC insn's low 24 bits */
1753 errno = 0;
1754 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1755 if (errno)
1756 return -1;
1757 /* EABI syscall convention? */
Dmitry V. Levin03b44542015-01-14 07:21:36 +00001758 if ((unsigned long) scno != 0xef000000) {
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001759 /* No, it's OABI */
1760 if ((scno & 0x0ff00000) != 0x0f900000) {
1761 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1762 tcp->pid, scno);
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001763 return -1;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001764 }
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001765 /* Fixup the syscall number */
1766 scno &= 0x000fffff;
1767 } else {
1768 scno_in_r7:
1769 scno = arm_regs.ARM_r7;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001770 }
Dmitry V. Levin0160e162014-03-18 23:37:43 +00001771# else /* __ARM_EABI__ || !ENABLE_ARM_OABI */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001772 scno = arm_regs.ARM_r7;
1773# endif
Denys Vlasenko7270de52013-02-21 15:46:34 +01001774 scno = shuffle_scno(scno);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001775#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001776 if (upeek(tcp->pid, 4*PT_ORIG_D0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001777 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001778#elif defined(MIPS)
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +00001779 scno = mips_REG_V0;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001780
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001781 if (!SCNO_IN_RANGE(scno)) {
Dmitry V. Levinf6777152015-03-02 15:06:51 +00001782 if (mips_REG_A3 == 0 || mips_REG_A3 == (uint64_t) -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001783 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001784 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001785 return 0;
1786 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001787 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001788#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001789 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001790 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001791 if (upeek(tcp->pid, REG_R0, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001792 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001793
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001794 /*
1795 * Do some sanity checks to figure out if it's
1796 * really a syscall entry
1797 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001798 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001799 if (alpha_a3 == 0 || alpha_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001800 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001801 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001802 return 0;
1803 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001804 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001805#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001806 /* Disassemble the syscall trap. */
1807 /* Retrieve the syscall trap instruction. */
Denys Vlasenko46455822013-02-05 17:02:59 +01001808 unsigned long trap;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001809 errno = 0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001810# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001811 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001812 trap >>= 32;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001813# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001814 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001815# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001816 if (errno)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001817 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001818
1819 /* Disassemble the trap to see what personality to use. */
1820 switch (trap) {
1821 case 0x91d02010:
1822 /* Linux/SPARC syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001823 update_personality(tcp, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001824 break;
1825 case 0x91d0206d:
1826 /* Linux/SPARC64 syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001827 update_personality(tcp, 2);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001828 break;
1829 case 0x91d02000:
1830 /* SunOS syscall trap. (pers 1) */
1831 fprintf(stderr, "syscall: SunOS no support\n");
1832 return -1;
1833 case 0x91d02008:
1834 /* Solaris 2.x syscall trap. (per 2) */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001835 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001836 break;
1837 case 0x91d02009:
1838 /* NetBSD/FreeBSD syscall trap. */
1839 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1840 return -1;
1841 case 0x91d02027:
1842 /* Solaris 2.x gettimeofday */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001843 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001844 break;
1845 default:
Denys Vlasenko523635f2012-02-25 02:44:25 +01001846# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001847 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001848# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001849 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001850# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001851 return -1;
1852 }
1853
1854 /* Extract the system call number from the registers. */
1855 if (trap == 0x91d02027)
1856 scno = 156;
1857 else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001858 scno = sparc_regs.u_regs[U_REG_G1];
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001859 if (scno == 0) {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001860 scno = sparc_regs.u_regs[U_REG_O0];
1861 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 +02001862 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001863#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001864 if (upeek(tcp->pid, PT_GR20, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001865 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001866#elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001867 /*
1868 * In the new syscall ABI, the system call number is in R3.
1869 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001870 if (upeek(tcp->pid, 4*(REG_REG0+3), &scno) < 0)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001871 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001872
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001873 if (scno < 0) {
1874 /* Odd as it may seem, a glibc bug has been known to cause
1875 glibc to issue bogus negative syscall numbers. So for
1876 our purposes, make strace print what it *should* have been */
1877 long correct_scno = (scno & 0xff);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001878 if (debug_flag)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001879 fprintf(stderr,
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001880 "Detected glibc bug: bogus system call"
1881 " number = %ld, correcting to %ld\n",
1882 scno,
1883 correct_scno);
1884 scno = correct_scno;
1885 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001886#elif defined(SH64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001887 if (upeek(tcp->pid, REG_SYSCALL, &scno) < 0)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001888 return -1;
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001889 scno &= 0xFFFF;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001890#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001891 if (upeek(tcp->pid, 4*PT_R9, &scno) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001892 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001893#elif defined(TILE)
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001894 unsigned int currpers;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001895 scno = tile_regs.regs[10];
1896# ifdef __tilepro__
1897 currpers = 1;
1898# else
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001899# ifndef PT_FLAGS_COMPAT
1900# define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1901# endif
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001902 if (tile_regs.flags & PT_FLAGS_COMPAT)
1903 currpers = 1;
1904 else
1905 currpers = 0;
1906# endif
1907 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001908#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001909 if (upeek(tcp->pid, 0, &scno) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001910 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001911#elif defined(OR1K)
1912 scno = or1k_regs.gpr[11];
James Hogan5f999a82013-02-22 14:44:10 +00001913#elif defined(METAG)
1914 scno = metag_regs.dx[0][1]; /* syscall number in D1Re0 (D1.0) */
Chris Zankel8f636ed2013-03-25 10:22:07 -07001915#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001916 if (upeek(tcp->pid, SYSCALL_NR, &scno) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001917 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301918# elif defined(ARC)
1919 scno = arc_regs.scratch.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001920#endif
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001921
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001922 tcp->scno = scno;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001923 if (SCNO_IS_VALID(tcp->scno)) {
1924 tcp->s_ent = &sysent[scno];
1925 tcp->qual_flg = qual_flags[scno];
1926 } else {
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01001927 static const struct_sysent unknown = {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001928 .nargs = MAX_ARGS,
1929 .sys_flags = 0,
1930 .sys_func = printargs,
1931 .sys_name = "unknown", /* not used */
1932 };
1933 tcp->s_ent = &unknown;
1934 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1935 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001936 return 1;
1937}
1938
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001939/* Return -1 on error or 1 on success (never 0!) */
Roland McGratha4d48532005-06-08 20:45:28 +00001940static int
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001941get_syscall_args(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001942{
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001943 int i, nargs;
1944
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001945 nargs = tcp->s_ent->nargs;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001946
Denys Vlasenko523635f2012-02-25 02:44:25 +01001947#if defined(S390) || defined(S390X)
Dmitry V. Levin20eca8a2015-03-05 22:10:15 +00001948 (void)i;
1949 (void)nargs;
1950 tcp->u_arg[0] = s390_regset.orig_gpr2;
1951 tcp->u_arg[1] = s390_regset.gprs[3];
1952 tcp->u_arg[2] = s390_regset.gprs[4];
1953 tcp->u_arg[3] = s390_regset.gprs[5];
1954 tcp->u_arg[4] = s390_regset.gprs[6];
1955 tcp->u_arg[5] = s390_regset.gprs[7];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001956#elif defined(ALPHA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001957 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001958 if (upeek(tcp->pid, REG_A0+i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001959 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001960#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001961 if (!ia64_ia32mode) {
Dmitry V. Levin48f08902015-03-05 23:30:02 +00001962 unsigned long *rbs_end =
1963 (unsigned long *) ia64_regs.ar[PT_AUR_BSP];
1964 unsigned long sof = (ia64_regs.cfm >> 0) & 0x7f;
1965 unsigned long sol = (ia64_regs.cfm >> 7) & 0x7f;
1966 unsigned long *out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001967
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001968 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001969 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
Denys Vlasenko7e69ed92015-03-21 19:50:53 +01001970 sizeof(long), &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001971 return -1;
1972 }
1973 } else {
Dmitry V. Levin48f08902015-03-05 23:30:02 +00001974 (void)i;
1975 (void)nargs;
1976 /* truncate away IVE sign-extension */
1977 tcp->u_arg[0] = 0xffffffff & ia64_regs.gr[11]; /* EBX */
1978 tcp->u_arg[1] = 0xffffffff & ia64_regs.gr[ 9]; /* ECX */
1979 tcp->u_arg[2] = 0xffffffff & ia64_regs.gr[10]; /* EDX */
1980 tcp->u_arg[3] = 0xffffffff & ia64_regs.gr[14]; /* ESI */
1981 tcp->u_arg[4] = 0xffffffff & ia64_regs.gr[15]; /* EDI */
1982 tcp->u_arg[5] = 0xffffffff & ia64_regs.gr[13]; /* EBP */
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001983 }
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +00001984#elif defined LINUX_MIPSN64
1985 (void)i;
1986 (void)nargs;
1987 tcp->u_arg[0] = mips_REG_A0;
1988 tcp->u_arg[1] = mips_REG_A1;
1989 tcp->u_arg[2] = mips_REG_A2;
1990 tcp->u_arg[3] = mips_REG_A3;
1991 tcp->u_arg[4] = mips_REG_A4;
1992 tcp->u_arg[5] = mips_REG_A5;
1993#elif defined LINUX_MIPSN32
1994 (void)i;
1995 (void)nargs;
1996 tcp->u_arg[0] = tcp->ext_arg[0] = mips_REG_A0;
1997 tcp->u_arg[1] = tcp->ext_arg[1] = mips_REG_A1;
1998 tcp->u_arg[2] = tcp->ext_arg[2] = mips_REG_A2;
1999 tcp->u_arg[3] = tcp->ext_arg[3] = mips_REG_A3;
2000 tcp->u_arg[4] = tcp->ext_arg[4] = mips_REG_A4;
2001 tcp->u_arg[5] = tcp->ext_arg[5] = mips_REG_A5;
2002#elif defined LINUX_MIPSO32
2003 (void)i;
2004 (void)nargs;
2005 tcp->u_arg[0] = mips_REG_A0;
2006 tcp->u_arg[1] = mips_REG_A1;
2007 tcp->u_arg[2] = mips_REG_A2;
2008 tcp->u_arg[3] = mips_REG_A3;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02002009 if (nargs > 4) {
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +00002010 umoven(tcp, mips_REG_SP + 4 * 4,
2011 (nargs - 4) * sizeof(tcp->u_arg[0]),
Denys Vlasenko7e69ed92015-03-21 19:50:53 +01002012 &tcp->u_arg[4]);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002013 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002014#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02002015 (void)i;
2016 (void)nargs;
2017 tcp->u_arg[0] = ppc_regs.orig_gpr3;
2018 tcp->u_arg[1] = ppc_regs.gpr[4];
2019 tcp->u_arg[2] = ppc_regs.gpr[5];
2020 tcp->u_arg[3] = ppc_regs.gpr[6];
2021 tcp->u_arg[4] = ppc_regs.gpr[7];
2022 tcp->u_arg[5] = ppc_regs.gpr[8];
Denys Vlasenko523635f2012-02-25 02:44:25 +01002023#elif defined(SPARC) || defined(SPARC64)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00002024 for (i = 0; i < nargs; ++i)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002025 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01002026#elif defined(HPPA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00002027 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002028 if (upeek(tcp->pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02002029 return -1;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002030#elif defined(ARM) || defined(AARCH64)
2031# if defined(AARCH64)
2032 if (tcp->currpers == 1)
2033 for (i = 0; i < nargs; ++i)
2034 tcp->u_arg[i] = aarch64_regs.regs[i];
2035 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002036# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00002037 for (i = 0; i < nargs; ++i)
Denys Vlasenko401374e2013-02-06 18:24:39 +01002038 tcp->u_arg[i] = arm_regs.uregs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01002039#elif defined(AVR32)
Denys Vlasenkob5b25892011-08-30 19:04:54 +02002040 (void)i;
2041 (void)nargs;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002042 tcp->u_arg[0] = avr32_regs.r12;
2043 tcp->u_arg[1] = avr32_regs.r11;
2044 tcp->u_arg[2] = avr32_regs.r10;
2045 tcp->u_arg[3] = avr32_regs.r9;
2046 tcp->u_arg[4] = avr32_regs.r5;
2047 tcp->u_arg[5] = avr32_regs.r3;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002048#elif defined(BFIN)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00002049 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 +02002050
Denys Vlasenko4b887a52011-08-23 13:32:38 +02002051 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002052 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02002053 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002054#elif defined(SH)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00002055 static const int syscall_regs[MAX_ARGS] = {
2056 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
2057 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02002058 };
2059
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00002060 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002061 if (upeek(tcp->pid, syscall_regs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02002062 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002063#elif defined(SH64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02002064 int i;
2065 /* Registers used by SH5 Linux system calls for parameters */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00002066 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00002067
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00002068 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002069 if (upeek(tcp->pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02002070 return -1;
Denys Vlasenko6cf36052013-02-15 15:01:38 +01002071#elif defined(I386)
2072 (void)i;
2073 (void)nargs;
2074 tcp->u_arg[0] = i386_regs.ebx;
2075 tcp->u_arg[1] = i386_regs.ecx;
2076 tcp->u_arg[2] = i386_regs.edx;
2077 tcp->u_arg[3] = i386_regs.esi;
2078 tcp->u_arg[4] = i386_regs.edi;
2079 tcp->u_arg[5] = i386_regs.ebp;
H.J. Lu35be5812012-04-16 13:00:01 +02002080#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002081 (void)i;
2082 (void)nargs;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002083 if (x86_io.iov_len != sizeof(i386_regs)) {
2084 /* x86-64 or x32 ABI */
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002085 tcp->u_arg[0] = x86_64_regs.rdi;
2086 tcp->u_arg[1] = x86_64_regs.rsi;
2087 tcp->u_arg[2] = x86_64_regs.rdx;
2088 tcp->u_arg[3] = x86_64_regs.r10;
2089 tcp->u_arg[4] = x86_64_regs.r8;
2090 tcp->u_arg[5] = x86_64_regs.r9;
H.J. Lu35be5812012-04-16 13:00:01 +02002091# ifdef X32
2092 tcp->ext_arg[0] = x86_64_regs.rdi;
2093 tcp->ext_arg[1] = x86_64_regs.rsi;
2094 tcp->ext_arg[2] = x86_64_regs.rdx;
2095 tcp->ext_arg[3] = x86_64_regs.r10;
2096 tcp->ext_arg[4] = x86_64_regs.r8;
2097 tcp->ext_arg[5] = x86_64_regs.r9;
2098# endif
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002099 } else {
2100 /* i386 ABI */
Denys Vlasenko6cf36052013-02-15 15:01:38 +01002101 /* Zero-extend from 32 bits */
2102 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
2103 * if you need to use *sign-extended* parameter.
2104 */
2105 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
2106 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
2107 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
2108 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
2109 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
2110 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002111 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002112#elif defined(MICROBLAZE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00002113 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002114 if (upeek(tcp->pid, (5 + i) * 4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02002115 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002116#elif defined(CRISV10) || defined(CRISV32)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00002117 static const int crisregs[MAX_ARGS] = {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02002118 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02002119 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02002120 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00002121
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00002122 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002123 if (upeek(tcp->pid, crisregs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02002124 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002125#elif defined(TILE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00002126 for (i = 0; i < nargs; ++i)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002127 tcp->u_arg[i] = tile_regs.regs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01002128#elif defined(M68K)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00002129 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002130 if (upeek(tcp->pid, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02002131 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01002132#elif defined(OR1K)
2133 (void)nargs;
2134 for (i = 0; i < 6; ++i)
2135 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
James Hogan5f999a82013-02-22 14:44:10 +00002136#elif defined(METAG)
2137 for (i = 0; i < nargs; i++)
2138 /* arguments go backwards from D1Ar1 (D1.3) */
2139 tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
Chris Zankel8f636ed2013-03-25 10:22:07 -07002140#elif defined(XTENSA)
2141 /* arg0: a6, arg1: a3, arg2: a4, arg3: a5, arg4: a8, arg5: a9 */
2142 static const int xtensaregs[MAX_ARGS] = { 6, 3, 4, 5, 8, 9 };
2143 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002144 if (upeek(tcp->pid, REG_A_BASE + xtensaregs[i], &tcp->u_arg[i]) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07002145 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05302146# elif defined(ARC)
2147 long *arc_args = &arc_regs.scratch.r0;
2148 for (i = 0; i < nargs; ++i)
2149 tcp->u_arg[i] = *arc_args--;
2150
Denys Vlasenko523635f2012-02-25 02:44:25 +01002151#else /* Other architecture (32bits specific) */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00002152 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002153 if (upeek(tcp->pid, i*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02002154 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002155#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00002156 return 1;
2157}
2158
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002159static void
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002160get_error(struct tcb *tcp)
2161{
2162 int u_error = 0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002163 int check_errno = 1;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002164 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002165 check_errno = 0;
2166 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002167#if defined(S390) || defined(S390X)
Dmitry V. Levin20eca8a2015-03-05 22:10:15 +00002168 if (check_errno && is_negated_errno(s390_regset.gprs[2])) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002169 tcp->u_rval = -1;
Dmitry V. Levin20eca8a2015-03-05 22:10:15 +00002170 u_error = -s390_regset.gprs[2];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002171 }
2172 else {
Dmitry V. Levin20eca8a2015-03-05 22:10:15 +00002173 tcp->u_rval = s390_regset.gprs[2];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002174 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002175#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002176 if (check_errno && is_negated_errno(i386_regs.eax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002177 tcp->u_rval = -1;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002178 u_error = -i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002179 }
2180 else {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002181 tcp->u_rval = i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002182 }
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00002183#elif defined(X86_64) || defined(X32)
2184 /*
2185 * In X32, return value is 64-bit (llseek uses one).
2186 * Using merely "long rax" would not work.
2187 */
Dmitry V. Levin1b786072015-03-22 18:09:55 +00002188 long long rax;
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00002189
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002190 if (x86_io.iov_len == sizeof(i386_regs)) {
2191 /* Sign extend from 32 bits */
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00002192 rax = (int32_t) i386_regs.eax;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002193 } else {
2194 rax = x86_64_regs.rax;
2195 }
2196 if (check_errno && is_negated_errno(rax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002197 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002198 u_error = -rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002199 }
2200 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002201 tcp->u_rval = rax;
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00002202# ifdef X32
2203 /* tcp->u_rval contains a truncated value */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002204 tcp->u_lrval = rax;
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00002205# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002206 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002207#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02002208 if (ia64_ia32mode) {
Dmitry V. Levin48f08902015-03-05 23:30:02 +00002209 int err = ia64_regs.gr[8];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002210 if (check_errno && is_negated_errno(err)) {
2211 tcp->u_rval = -1;
2212 u_error = -err;
2213 }
2214 else {
2215 tcp->u_rval = err;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002216 }
2217 } else {
Dmitry V. Levin48f08902015-03-05 23:30:02 +00002218 if (check_errno && ia64_regs.gr[10]) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002219 tcp->u_rval = -1;
Dmitry V. Levin48f08902015-03-05 23:30:02 +00002220 u_error = ia64_regs.gr[8];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002221 } else {
Dmitry V. Levin48f08902015-03-05 23:30:02 +00002222 tcp->u_rval = ia64_regs.gr[8];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002223 }
2224 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002225#elif defined(MIPS)
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +00002226 if (check_errno && mips_REG_A3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002227 tcp->u_rval = -1;
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +00002228 u_error = mips_REG_V0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002229 } else {
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +00002230# if defined LINUX_MIPSN32
2231 tcp->u_lrval = mips_REG_V0;
H.J. Ludd0130b2012-04-16 12:16:45 +02002232# endif
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +00002233 tcp->u_rval = mips_REG_V0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002234 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002235#elif defined(POWERPC)
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002236 if (check_errno && (ppc_regs.ccr & 0x10000000)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002237 tcp->u_rval = -1;
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002238 u_error = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002239 }
2240 else {
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002241 tcp->u_rval = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002242 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002243#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002244 if (check_errno && is_negated_errno(m68k_d0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002245 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002246 u_error = -m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002247 }
2248 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002249 tcp->u_rval = m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002250 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002251#elif defined(ARM) || defined(AARCH64)
2252# if defined(AARCH64)
2253 if (tcp->currpers == 1) {
2254 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2255 tcp->u_rval = -1;
2256 u_error = -aarch64_regs.regs[0];
2257 }
2258 else {
2259 tcp->u_rval = aarch64_regs.regs[0];
2260 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002261 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002262 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002263# endif
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002264 {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002265 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002266 tcp->u_rval = -1;
Denys Vlasenko401374e2013-02-06 18:24:39 +01002267 u_error = -arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002268 }
2269 else {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002270 tcp->u_rval = arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002271 }
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002272 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002273#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002274 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002275 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002276 u_error = -avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002277 }
2278 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002279 tcp->u_rval = avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002280 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002281#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002282 if (check_errno && is_negated_errno(bfin_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002283 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002284 u_error = -bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002285 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002286 tcp->u_rval = bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002287 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002288#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002289 if (check_errno && alpha_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002290 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002291 u_error = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002292 }
2293 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002294 tcp->u_rval = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002295 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002296#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002297 if (check_errno && sparc_regs.psr & PSR_C) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002298 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002299 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002300 }
2301 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002302 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002303 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002304#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002305 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002306 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002307 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002308 }
2309 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002310 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002311 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002312#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002313 if (check_errno && is_negated_errno(hppa_r28)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002314 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002315 u_error = -hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002316 }
2317 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002318 tcp->u_rval = hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002319 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002320#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002321 if (check_errno && is_negated_errno(sh_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002322 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002323 u_error = -sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002324 }
2325 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002326 tcp->u_rval = sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002327 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002328#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002329 if (check_errno && is_negated_errno(sh64_r9)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002330 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002331 u_error = -sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002332 }
2333 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002334 tcp->u_rval = sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002335 }
James Hogan5f999a82013-02-22 14:44:10 +00002336#elif defined(METAG)
2337 /* result pointer in D0Re0 (D0.0) */
2338 if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2339 tcp->u_rval = -1;
2340 u_error = -metag_regs.dx[0][0];
2341 }
2342 else {
2343 tcp->u_rval = metag_regs.dx[0][0];
2344 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002345#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002346 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002347 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002348 u_error = -cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002349 }
2350 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002351 tcp->u_rval = cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002352 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002353#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002354 /*
2355 * The standard tile calling convention returns the value (or negative
2356 * errno) in r0, and zero (or positive errno) in r1.
2357 * Until at least kernel 3.8, however, the r1 value is not reflected
2358 * in ptregs at this point, so we use r0 here.
2359 */
2360 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002361 tcp->u_rval = -1;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002362 u_error = -tile_regs.regs[0];
2363 } else {
2364 tcp->u_rval = tile_regs.regs[0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002365 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002366#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002367 if (check_errno && is_negated_errno(microblaze_r3)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002368 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002369 u_error = -microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002370 }
2371 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002372 tcp->u_rval = microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002373 }
Christian Svensson492f81f2013-02-14 13:26:27 +01002374#elif defined(OR1K)
2375 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2376 tcp->u_rval = -1;
2377 u_error = -or1k_regs.gpr[11];
2378 }
2379 else {
2380 tcp->u_rval = or1k_regs.gpr[11];
2381 }
Chris Zankel8f636ed2013-03-25 10:22:07 -07002382#elif defined(XTENSA)
2383 if (check_errno && is_negated_errno(xtensa_a2)) {
2384 tcp->u_rval = -1;
2385 u_error = -xtensa_a2;
2386 }
2387 else {
2388 tcp->u_rval = xtensa_a2;
2389 }
Vineet Gupta7daacbb2013-08-16 12:47:06 +05302390#elif defined(ARC)
2391 if (check_errno && is_negated_errno(arc_regs.scratch.r0)) {
2392 tcp->u_rval = -1;
2393 u_error = -arc_regs.scratch.r0;
2394 }
2395 else {
2396 tcp->u_rval = arc_regs.scratch.r0;
2397 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002398#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002399 tcp->u_error = u_error;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002400}
2401
Dmitry V. Levin1b786072015-03-22 18:09:55 +00002402/* Returns:
2403 * 1: ok, continue in trace_syscall_exiting().
2404 * -1: error, trace_syscall_exiting() should print error indicator
2405 * ("????" etc) and bail out.
2406 */
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002407static int
Dmitry V. Levin1b786072015-03-22 18:09:55 +00002408get_syscall_result(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002409{
Dmitry V. Levin1b786072015-03-22 18:09:55 +00002410#if defined ARCH_REGS_FOR_GETREGSET || defined ARCH_REGS_FOR_GETREGS
2411 /* already done by get_regs */
2412#elif defined(BFIN)
2413 if (upeek(tcp->pid, PT_R0, &bfin_r0) < 0)
2414 return -1;
2415#elif defined(M68K)
2416 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
2417 return -1;
2418#elif defined(ALPHA)
2419 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
2420 return -1;
2421 if (upeek(tcp->pid, REG_R0, &alpha_r0) < 0)
2422 return -1;
2423#elif defined(HPPA)
2424 if (upeek(tcp->pid, PT_GR28, &hppa_r28) < 0)
2425 return -1;
2426#elif defined(SH)
2427 /* new syscall ABI returns result in R0 */
2428 if (upeek(tcp->pid, 4*REG_REG0, (long *)&sh_r0) < 0)
2429 return -1;
2430#elif defined(SH64)
2431 /* ABI defines result returned in r9 */
2432 if (upeek(tcp->pid, REG_GENERAL(9), (long *)&sh64_r9) < 0)
2433 return -1;
2434#elif defined(CRISV10) || defined(CRISV32)
2435 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
2436 return -1;
2437#elif defined(MICROBLAZE)
2438 if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
2439 return -1;
2440#elif defined(XTENSA)
2441 if (upeek(tcp->pid, REG_A_BASE + 2, &xtensa_a2) < 0)
2442 return -1;
2443#else
2444# error get_syscall_result is not implemented for this architecture
Masatake YAMATOed69fc22014-04-16 15:33:35 +09002445#endif
Dmitry V. Levin1b786072015-03-22 18:09:55 +00002446 get_error(tcp);
2447 return 1;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002448}