blob: cfedc9f16380d7e3297c2593f75acb5d4261a35b [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. Levin4c3f2ae2015-02-13 22:53:00 +000056#ifndef NT_PRSTATUS
57# define NT_PRSTATUS 1
58#endif
59
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000060#ifndef NSIG
Denys Vlasenko523635f2012-02-25 02:44:25 +010061# warning: NSIG is not defined, using 32
62# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000063#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000064
65#include "syscall.h"
66
67/* Define these shorthand notations to simplify the syscallent files. */
Roland McGrath2fe7b132005-07-05 03:25:35 +000068#define TD TRACE_DESC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000069#define TF TRACE_FILE
70#define TI TRACE_IPC
71#define TN TRACE_NETWORK
72#define TP TRACE_PROCESS
73#define TS TRACE_SIGNAL
Namhyung Kim96792962012-10-24 11:41:57 +090074#define TM TRACE_MEMORY
Dmitry V. Levin50a218d2011-01-18 17:36:20 +000075#define NF SYSCALL_NEVER_FAILS
Denys Vlasenkoac1ce772011-08-23 13:24:17 +020076#define MA MAX_ARGS
Masatake YAMATO1d78d222014-04-16 15:33:11 +090077#define SI STACKTRACE_INVALIDATE_CACHE
78#define SE STACKTRACE_CAPTURE_ON_ENTER
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000079
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +010080const struct_sysent sysent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000081#include "syscallent.h"
82};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000083
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010084#if SUPPORTED_PERSONALITIES > 1
85static const struct_sysent sysent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +010086# include "syscallent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000087};
Denys Vlasenko39fca622011-08-20 02:12:33 +020088#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000089
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010090#if SUPPORTED_PERSONALITIES > 2
91static const struct_sysent sysent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +010092# include "syscallent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000093};
Denys Vlasenko39fca622011-08-20 02:12:33 +020094#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000095
96/* Now undef them since short defines cause wicked namespace pollution. */
Roland McGrath2fe7b132005-07-05 03:25:35 +000097#undef TD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000098#undef TF
99#undef TI
100#undef TN
101#undef TP
102#undef TS
Namhyung Kim96792962012-10-24 11:41:57 +0900103#undef TM
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000104#undef NF
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200105#undef MA
Masatake YAMATO1d78d222014-04-16 15:33:11 +0900106#undef SI
107#undef SE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000108
Denys Vlasenko39fca622011-08-20 02:12:33 +0200109/*
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +0000110 * `ioctlent[012].h' files are automatically generated by the auxiliary
Denys Vlasenko39fca622011-08-20 02:12:33 +0200111 * program `ioctlsort', such that the list is sorted by the `code' field.
112 * This has the side-effect of resolving the _IO.. macros into
113 * plain integers, eliminating the need to include here everything
114 * in "/usr/include".
115 */
116
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100117const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000118#include "errnoent.h"
119};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100120const char *const signalent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200121#include "signalent.h"
122};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100123const struct_ioctlent ioctlent0[] = {
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +0000124#include "ioctlent0.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200125};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000126
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100127#if SUPPORTED_PERSONALITIES > 1
Roland McGrathee36ce12004-09-04 03:53:10 +0000128static const char *const errnoent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100129# include "errnoent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000130};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200131static const char *const signalent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100132# include "signalent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200133};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100134static const struct_ioctlent ioctlent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100135# include "ioctlent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200136};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200137#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000138
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100139#if SUPPORTED_PERSONALITIES > 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000140static const char *const errnoent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100141# include "errnoent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000142};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200143static const char *const signalent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100144# include "signalent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200145};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100146static const struct_ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100147# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200148};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200149#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000150
Dmitry V. Levine6f55242013-02-26 22:02:30 +0000151enum {
152 nsyscalls0 = ARRAY_SIZE(sysent0)
153#if SUPPORTED_PERSONALITIES > 1
154 , nsyscalls1 = ARRAY_SIZE(sysent1)
155# if SUPPORTED_PERSONALITIES > 2
156 , nsyscalls2 = ARRAY_SIZE(sysent2)
157# endif
158#endif
159};
160
161enum {
162 nerrnos0 = ARRAY_SIZE(errnoent0)
163#if SUPPORTED_PERSONALITIES > 1
164 , nerrnos1 = ARRAY_SIZE(errnoent1)
165# if SUPPORTED_PERSONALITIES > 2
166 , nerrnos2 = ARRAY_SIZE(errnoent2)
167# endif
168#endif
169};
170
171enum {
172 nsignals0 = ARRAY_SIZE(signalent0)
173#if SUPPORTED_PERSONALITIES > 1
174 , nsignals1 = ARRAY_SIZE(signalent1)
175# if SUPPORTED_PERSONALITIES > 2
176 , nsignals2 = ARRAY_SIZE(signalent2)
177# endif
178#endif
179};
180
181enum {
182 nioctlents0 = ARRAY_SIZE(ioctlent0)
183#if SUPPORTED_PERSONALITIES > 1
184 , nioctlents1 = ARRAY_SIZE(ioctlent1)
185# if SUPPORTED_PERSONALITIES > 2
186 , nioctlents2 = ARRAY_SIZE(ioctlent2)
187# endif
188#endif
189};
190
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100191#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100192const struct_sysent *sysent = sysent0;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100193const char *const *errnoent = errnoent0;
194const char *const *signalent = signalent0;
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100195const struct_ioctlent *ioctlent = ioctlent0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100196#endif
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100197unsigned nsyscalls = nsyscalls0;
198unsigned nerrnos = nerrnos0;
199unsigned nsignals = nsignals0;
200unsigned nioctlents = nioctlents0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100201
202unsigned num_quals;
203qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
204
205static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
206 nsyscalls0,
207#if SUPPORTED_PERSONALITIES > 1
208 nsyscalls1,
209#endif
210#if SUPPORTED_PERSONALITIES > 2
211 nsyscalls2,
212#endif
213};
214static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
215 sysent0,
216#if SUPPORTED_PERSONALITIES > 1
217 sysent1,
218#endif
219#if SUPPORTED_PERSONALITIES > 2
220 sysent2,
221#endif
222};
223
224enum {
225 MAX_NSYSCALLS1 = (nsyscalls0
226#if SUPPORTED_PERSONALITIES > 1
227 > nsyscalls1 ? nsyscalls0 : nsyscalls1
228#endif
229 ),
230 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
231#if SUPPORTED_PERSONALITIES > 2
232 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
233#endif
234 ),
235 MAX_NSYSCALLS = MAX_NSYSCALLS2,
236 /* We are ready for arches with up to 255 signals,
237 * even though the largest known signo is on MIPS and it is 128.
238 * The number of existing syscalls on all arches is
239 * larger that 255 anyway, so it is just a pedantic matter.
240 */
241 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
242};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000243
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100244#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100245unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000246
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100247# ifndef current_wordsize
248unsigned current_wordsize;
249static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000250 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000251 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100252# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000253 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100254# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200255};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100256# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000257
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200258void
Dmitry V. Levin3abe8b22006-12-20 22:37:21 +0000259set_personality(int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000260{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100261 nsyscalls = nsyscall_vec[personality];
262 sysent = sysent_vec[personality];
263
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000264 switch (personality) {
265 case 0:
266 errnoent = errnoent0;
267 nerrnos = nerrnos0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000268 ioctlent = ioctlent0;
269 nioctlents = nioctlents0;
270 signalent = signalent0;
271 nsignals = nsignals0;
272 break;
273
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000274 case 1:
275 errnoent = errnoent1;
276 nerrnos = nerrnos1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000277 ioctlent = ioctlent1;
278 nioctlents = nioctlents1;
279 signalent = signalent1;
280 nsignals = nsignals1;
281 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000282
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100283# if SUPPORTED_PERSONALITIES > 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000284 case 2:
285 errnoent = errnoent2;
286 nerrnos = nerrnos2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000287 ioctlent = ioctlent2;
288 nioctlents = nioctlents2;
289 signalent = signalent2;
290 nsignals = nsignals2;
291 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100292# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000293 }
294
295 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100296# ifndef current_wordsize
297 current_wordsize = personality_wordsize[personality];
298# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000299}
300
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000301static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000302update_personality(struct tcb *tcp, unsigned int personality)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000303{
304 if (personality == current_personality)
305 return;
306 set_personality(personality);
307
308 if (personality == tcp->currpers)
309 return;
310 tcp->currpers = personality;
311
H.J. Lu35be5812012-04-16 13:00:01 +0200312# if defined(POWERPC64)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000313 if (!qflag) {
314 static const char *const names[] = {"64 bit", "32 bit"};
315 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
316 tcp->pid, names[personality]);
317 }
H.J. Lu35be5812012-04-16 13:00:01 +0200318# elif defined(X86_64)
319 if (!qflag) {
320 static const char *const names[] = {"64 bit", "32 bit", "x32"};
321 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
322 tcp->pid, names[personality]);
323 }
H.J. Lu085e4282012-04-17 11:05:04 -0700324# elif defined(X32)
325 if (!qflag) {
326 static const char *const names[] = {"x32", "32 bit"};
327 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
328 tcp->pid, names[personality]);
329 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000330# elif defined(AARCH64)
331 if (!qflag) {
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100332 static const char *const names[] = {"32-bit", "AArch64"};
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000333 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
334 tcp->pid, names[personality]);
335 }
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100336# elif defined(TILE)
337 if (!qflag) {
338 static const char *const names[] = {"64-bit", "32-bit"};
339 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
340 tcp->pid, names[personality]);
341 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100342# endif
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000343}
344#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000345
Denys Vlasenkoc1540fe2013-02-21 16:17:08 +0100346static int qual_syscall(), qual_signal(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000347
Roland McGrathe10e62a2004-09-04 04:20:43 +0000348static const struct qual_options {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000349 unsigned int bitflag;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000350 const char *option_name;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000351 int (*qualify)(const char *, int, int);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000352 const char *argument_name;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000353} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000354 { QUAL_TRACE, "trace", qual_syscall, "system call" },
355 { QUAL_TRACE, "t", qual_syscall, "system call" },
356 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
357 { QUAL_ABBREV, "a", qual_syscall, "system call" },
358 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
359 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
360 { QUAL_RAW, "raw", qual_syscall, "system call" },
361 { QUAL_RAW, "x", qual_syscall, "system call" },
362 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
363 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
364 { QUAL_SIGNAL, "s", qual_signal, "signal" },
Roland McGrath9797ceb2002-12-30 10:23:00 +0000365 { QUAL_READ, "read", qual_desc, "descriptor" },
366 { QUAL_READ, "reads", qual_desc, "descriptor" },
367 { QUAL_READ, "r", qual_desc, "descriptor" },
368 { QUAL_WRITE, "write", qual_desc, "descriptor" },
369 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
370 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000371 { 0, NULL, NULL, NULL },
372};
373
Roland McGrath9797ceb2002-12-30 10:23:00 +0000374static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000375reallocate_qual(const unsigned int n)
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100376{
377 unsigned p;
378 qualbits_t *qp;
379 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
380 qp = qual_vec[p] = realloc(qual_vec[p], n * sizeof(qualbits_t));
381 if (!qp)
382 die_out_of_memory();
383 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
384 }
385 num_quals = n;
386}
387
388static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000389qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000390{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000391 int p;
Roland McGrath138c6a32006-01-12 09:50:49 +0000392
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100393 if (num_quals <= n)
394 reallocate_qual(n + 1);
Roland McGrath138c6a32006-01-12 09:50:49 +0000395
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100396 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
397 if (pers == p || pers < 0) {
398 if (not)
399 qual_vec[p][n] &= ~bitflag;
400 else
401 qual_vec[p][n] |= bitflag;
402 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000403 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000404}
405
406static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000407qual_syscall(const char *s, const unsigned int bitflag, const int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000408{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000409 int p;
410 unsigned int i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000411 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000412
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100413 if (*s >= '0' && *s <= '9') {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100414 i = string_to_uint(s);
Denys Vlasenkob43dacd2013-02-23 18:19:28 +0100415 if (i >= MAX_NSYSCALLS)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000416 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000417 qualify_one(i, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000418 return 0;
Roland McGrath48a035f2006-01-12 09:45:56 +0000419 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000420
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100421 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
422 for (i = 0; i < nsyscall_vec[p]; i++) {
423 if (sysent_vec[p][i].sys_name
424 && strcmp(s, sysent_vec[p][i].sys_name) == 0
425 ) {
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000426 qualify_one(i, bitflag, not, p);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100427 rc = 0;
428 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000429 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100430 }
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000431
Roland McGrathfe6b3522005-02-02 04:40:11 +0000432 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000433}
434
435static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000436qual_signal(const char *s, const unsigned int bitflag, const int not)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000437{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000438 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000439
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100440 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000441 int signo = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100442 if (signo < 0 || signo > 255)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000443 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000444 qualify_one(signo, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000445 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000446 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000447 if (strncasecmp(s, "SIG", 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000448 s += 3;
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100449 for (i = 0; i <= NSIG; i++) {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000450 if (strcasecmp(s, signame(i) + 3) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000451 qualify_one(i, bitflag, not, -1);
Roland McGrath76421df2005-02-02 03:51:18 +0000452 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000453 }
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100454 }
Roland McGrath76421df2005-02-02 03:51:18 +0000455 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000456}
457
458static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000459qual_desc(const char *s, const unsigned int bitflag, const int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000460{
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100461 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000462 int desc = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100463 if (desc < 0 || desc > 0x7fff) /* paranoia */
Roland McGrathfe6b3522005-02-02 04:40:11 +0000464 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000465 qualify_one(desc, bitflag, not, -1);
Roland McGrath2b619022003-04-10 18:58:20 +0000466 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000467 }
468 return -1;
469}
470
471static int
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000472lookup_class(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000473{
474 if (strcmp(s, "file") == 0)
475 return TRACE_FILE;
476 if (strcmp(s, "ipc") == 0)
477 return TRACE_IPC;
478 if (strcmp(s, "network") == 0)
479 return TRACE_NETWORK;
480 if (strcmp(s, "process") == 0)
481 return TRACE_PROCESS;
482 if (strcmp(s, "signal") == 0)
483 return TRACE_SIGNAL;
Roland McGrath2fe7b132005-07-05 03:25:35 +0000484 if (strcmp(s, "desc") == 0)
485 return TRACE_DESC;
Namhyung Kim96792962012-10-24 11:41:57 +0900486 if (strcmp(s, "memory") == 0)
487 return TRACE_MEMORY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000488 return -1;
489}
490
491void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000492qualify(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000493{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000494 const struct qual_options *opt;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000495 char *copy;
496 const char *p;
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000497 int not;
498 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000499
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100500 if (num_quals == 0)
501 reallocate_qual(MIN_QUALS);
502
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000503 opt = &qual_options[0];
504 for (i = 0; (p = qual_options[i].option_name); i++) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000505 unsigned int len = strlen(p);
506 if (strncmp(s, p, len) == 0 && s[len] == '=') {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000507 opt = &qual_options[i];
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000508 s += len + 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000509 break;
510 }
511 }
512 not = 0;
513 if (*s == '!') {
514 not = 1;
515 s++;
516 }
517 if (strcmp(s, "none") == 0) {
518 not = 1 - not;
519 s = "all";
520 }
521 if (strcmp(s, "all") == 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100522 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000523 qualify_one(i, opt->bitflag, not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000524 }
525 return;
526 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100527 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000528 qualify_one(i, opt->bitflag, !not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000529 }
Denys Vlasenko5d645812011-08-20 12:48:18 +0200530 copy = strdup(s);
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200531 if (!copy)
532 die_out_of_memory();
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000533 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000534 int n;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000535 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100536 unsigned pers;
537 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
538 for (i = 0; i < nsyscall_vec[pers]; i++)
539 if (sysent_vec[pers][i].sys_flags & n)
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000540 qualify_one(i, opt->bitflag, not, pers);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100541 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000542 continue;
543 }
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000544 if (opt->qualify(p, opt->bitflag, not)) {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100545 error_msg_and_die("invalid %s '%s'",
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000546 opt->argument_name, p);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000547 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000548 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000549 free(copy);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000550 return;
551}
552
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000553#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000554static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000555decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000556{
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000557 unsigned long addr;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100558 unsigned int i, n, size;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000559
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000560 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
561 return;
562
563 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100564 tcp->qual_flg = qual_flags[tcp->scno];
565 tcp->s_ent = &sysent[tcp->scno];
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000566 addr = tcp->u_arg[1];
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100567 size = current_wordsize;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100568 n = tcp->s_ent->nargs;
569 for (i = 0; i < n; ++i) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000570 if (size == sizeof(int)) {
571 unsigned int arg;
572 if (umove(tcp, addr, &arg) < 0)
573 arg = 0;
574 tcp->u_arg[i] = arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000575 }
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000576 else {
577 unsigned long arg;
578 if (umove(tcp, addr, &arg) < 0)
579 arg = 0;
580 tcp->u_arg[i] = arg;
581 }
582 addr += size;
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
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100641#if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200642long
643getrval2(struct tcb *tcp)
644{
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100645 long val;
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200646
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100647# if defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100648 val = sparc_regs.u_regs[U_REG_O1];
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100649# elif defined(SH)
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200650 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200651 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100652# elif defined(IA64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200653 if (upeek(tcp->pid, PT_R9, &val) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200654 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100655# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200656
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200657 return val;
658}
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100659#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200660
Denys Vlasenko523635f2012-02-25 02:44:25 +0100661#if defined(I386)
Denys Vlasenkob51f3642013-07-16 12:06:25 +0200662static struct user_regs_struct i386_regs;
Dmitry V. Levinf97a4772015-02-15 00:08:11 +0000663long *const i386_esp_ptr = &i386_regs.esp;
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +0000664# define ARCH_REGS_FOR_GETREGS i386_regs
H.J. Lu35be5812012-04-16 13:00:01 +0200665#elif defined(X86_64) || defined(X32)
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100666/*
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100667 * On i386, pt_regs and user_regs_struct are the same,
668 * but on 64 bit x86, user_regs_struct has six more fields:
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100669 * fs_base, gs_base, ds, es, fs, gs.
670 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
671 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100672struct i386_user_regs_struct {
673 uint32_t ebx;
674 uint32_t ecx;
675 uint32_t edx;
676 uint32_t esi;
677 uint32_t edi;
678 uint32_t ebp;
679 uint32_t eax;
680 uint32_t xds;
681 uint32_t xes;
682 uint32_t xfs;
683 uint32_t xgs;
684 uint32_t orig_eax;
685 uint32_t eip;
686 uint32_t xcs;
687 uint32_t eflags;
688 uint32_t esp;
689 uint32_t xss;
690};
691static union {
692 struct user_regs_struct x86_64_r;
693 struct i386_user_regs_struct i386_r;
694} x86_regs_union;
695# define x86_64_regs x86_regs_union.x86_64_r
696# define i386_regs x86_regs_union.i386_r
Denys Vlasenkob51f3642013-07-16 12:06:25 +0200697uint32_t *const i386_esp_ptr = &i386_regs.esp;
Dmitry V. Levinccb4fda2015-03-04 12:19:55 +0000698uint64_t *const x86_64_rsp_ptr = (uint64_t *) &x86_64_regs.rsp;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100699static struct iovec x86_io = {
700 .iov_base = &x86_regs_union
701};
Dmitry V. Levind6db1db2015-02-13 23:41:04 +0000702# define ARCH_REGS_FOR_GETREGSET x86_regs_union
703# define ARCH_IOVEC_FOR_GETREGSET x86_io
Denys Vlasenko523635f2012-02-25 02:44:25 +0100704#elif defined(IA64)
Dmitry V. Levin23ce9e42015-02-08 13:05:53 +0000705static bool ia64_ia32mode;
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100706static long ia64_r8, ia64_r10;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100707#elif defined(POWERPC)
Dmitry V. Levin23ce9e42015-02-08 13:05:53 +0000708struct pt_regs ppc_regs; /* not static */
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +0000709# define ARCH_REGS_FOR_GETREGS ppc_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +0100710#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100711static long m68k_d0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100712#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100713static long bfin_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100714#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100715struct pt_regs arm_regs; /* not static */
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +0000716# define ARCH_REGS_FOR_GETREGS arm_regs
Steve McIntyred8d3bd32012-10-24 17:58:16 +0100717#elif defined(AARCH64)
Dmitry V. Levin5503dd22015-02-13 02:12:14 +0000718struct arm_pt_regs {
719 int uregs[18];
720};
721# define ARM_cpsr uregs[16]
722# define ARM_pc uregs[15]
723# define ARM_lr uregs[14]
724# define ARM_sp uregs[13]
725# define ARM_ip uregs[12]
726# define ARM_fp uregs[11]
727# define ARM_r10 uregs[10]
728# define ARM_r9 uregs[9]
729# define ARM_r8 uregs[8]
730# define ARM_r7 uregs[7]
731# define ARM_r6 uregs[6]
732# define ARM_r5 uregs[5]
733# define ARM_r4 uregs[4]
734# define ARM_r3 uregs[3]
735# define ARM_r2 uregs[2]
736# define ARM_r1 uregs[1]
737# define ARM_r0 uregs[0]
738# define ARM_ORIG_r0 uregs[17]
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100739static union {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100740 struct user_pt_regs aarch64_r;
741 struct arm_pt_regs arm_r;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100742} arm_regs_union;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100743# define aarch64_regs arm_regs_union.aarch64_r
744# define arm_regs arm_regs_union.arm_r
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100745static struct iovec aarch64_io = {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100746 .iov_base = &arm_regs_union
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100747};
Dmitry V. Levind6db1db2015-02-13 23:41:04 +0000748# define ARCH_REGS_FOR_GETREGSET arm_regs_union
749# define ARCH_IOVEC_FOR_GETREGSET aarch64_io
Denys Vlasenko523635f2012-02-25 02:44:25 +0100750#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100751static long alpha_r0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100752static long alpha_a3;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100753#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100754static struct pt_regs avr32_regs;
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +0000755# define ARCH_REGS_FOR_GETREGS avr32_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +0100756#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100757struct pt_regs sparc_regs; /* not static */
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +0000758# define ARCH_REGS_FOR_GETREGS sparc_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +0100759#elif defined(MIPS)
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +0000760struct mips_regs mips_regs; /* not static */
761/* PTRACE_GETREGS on MIPS is available since linux v2.6.15. */
762# define ARCH_REGS_FOR_GETREGS mips_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +0100763#elif defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +0200764static long s390_gpr2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100765#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100766static long hppa_r28;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100767#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100768static long sh_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100769#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100770static long sh64_r9;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100771#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100772static long cris_r10;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100773#elif defined(TILE)
Dmitry V. Levin23ce9e42015-02-08 13:05:53 +0000774struct pt_regs tile_regs; /* not static */
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +0000775# define ARCH_REGS_FOR_GETREGS tile_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +0100776#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100777static long microblaze_r3;
Christian Svensson492f81f2013-02-14 13:26:27 +0100778#elif defined(OR1K)
779static struct user_regs_struct or1k_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000780# define ARCH_REGS_FOR_GETREGSET or1k_regs
James Hogan5f999a82013-02-22 14:44:10 +0000781#elif defined(METAG)
782static struct user_gp_regs metag_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000783# define ARCH_REGS_FOR_GETREGSET metag_regs
Chris Zankel8f636ed2013-03-25 10:22:07 -0700784#elif defined(XTENSA)
785static long xtensa_a2;
Vineet Gupta7daacbb2013-08-16 12:47:06 +0530786# elif defined(ARC)
787static struct user_regs_struct arc_regs;
788# define ARCH_REGS_FOR_GETREGSET arc_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +0100789#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000790
Dmitry V. Levin9a176c92015-02-13 21:56:50 +0000791static long get_regs_error;
792
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100793void
Denys Vlasenko5a2483b2013-07-01 12:49:14 +0200794print_pc(struct tcb *tcp)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100795{
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000796 const char *fmt;
797 const char *bad;
798
Dmitry V. Levinb2d9ff22015-02-23 13:43:20 +0000799#ifdef current_wordsize
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000800# define pc_wordsize current_wordsize
Dmitry V. Levinb2d9ff22015-02-23 13:43:20 +0000801#else
802# define pc_wordsize personality_wordsize[tcp->currpers]
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000803#endif
804
805 if (pc_wordsize == 4) {
806 fmt = "[%08lx] ";
807 bad = "[????????] ";
808 } else {
809 fmt = "[%016lx] ";
810 bad = "[????????????????] ";
811 }
812
813#undef pc_wordsize
814#define PRINTBADPC tprints(bad)
815
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100816 if (get_regs_error) {
817 PRINTBADPC;
818 return;
819 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000820
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100821#if defined(I386)
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000822 tprintf(fmt, i386_regs.eip);
823#elif defined(X86_64) || defined(X32)
824 if (x86_io.iov_len == sizeof(i386_regs))
825 tprintf(fmt, (unsigned long) i386_regs.eip);
826 else
827 tprintf(fmt, (unsigned long) x86_64_regs.rip);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100828#elif defined(S390) || defined(S390X)
829 long psw;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200830 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100831 PRINTBADPC;
832 return;
833 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000834 tprintf(fmt, psw);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100835#elif defined(IA64)
836 long ip;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200837 if (upeek(tcp->pid, PT_B0, &ip) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100838 PRINTBADPC;
839 return;
840 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000841 tprintf(fmt, ip);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100842#elif defined(POWERPC)
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000843 tprintf(fmt, ppc_regs.nip);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100844#elif defined(M68K)
845 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200846 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000847 PRINTBADPC;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100848 return;
849 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000850 tprintf(fmt, pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100851#elif defined(ALPHA)
852 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200853 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000854 PRINTBADPC;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100855 return;
856 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000857 tprintf(fmt, pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100858#elif defined(SPARC)
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000859 tprintf(fmt, sparc_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100860#elif defined(SPARC64)
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000861 tprintf(fmt, sparc_regs.tpc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100862#elif defined(HPPA)
863 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200864 if (upeek(tcp->pid, PT_IAOQ0, &pc) < 0) {
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000865 PRINTBADPC;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100866 return;
867 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000868 tprintf(fmt, pc);
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +0000869#elif defined MIPS
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000870 tprintf(fmt, (unsigned long) mips_REG_EPC);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100871#elif defined(SH)
872 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200873 if (upeek(tcp->pid, 4*REG_PC, &pc) < 0) {
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000874 PRINTBADPC;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100875 return;
876 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000877 tprintf(fmt, pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100878#elif defined(SH64)
879 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200880 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000881 PRINTBADPC;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100882 return;
883 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000884 tprintf(fmt, pc);
Dmitry V. Levind819fe82015-03-02 03:38:27 +0000885#elif defined(AARCH64)
886 if (aarch64_io.iov_len == sizeof(arm_regs))
887 tprintf(fmt, (unsigned long) arm_regs.ARM_pc);
888 else
889 tprintf(fmt, (unsigned long) aarch64_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100890#elif defined(ARM)
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000891 tprintf(fmt, arm_regs.ARM_pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100892#elif defined(AVR32)
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000893 tprintf(fmt, avr32_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100894#elif defined(BFIN)
895 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200896 if (upeek(tcp->pid, PT_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100897 PRINTBADPC;
898 return;
899 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000900 tprintf(fmt, pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100901#elif defined(CRISV10)
902 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200903 if (upeek(tcp->pid, 4*PT_IRP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100904 PRINTBADPC;
905 return;
906 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000907 tprintf(fmt, pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100908#elif defined(CRISV32)
909 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200910 if (upeek(tcp->pid, 4*PT_ERP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100911 PRINTBADPC;
912 return;
913 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000914 tprintf(fmt, pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100915#elif defined(TILE)
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000916 tprintf(fmt, (unsigned long) tile_regs.pc);
Christian Svensson492f81f2013-02-14 13:26:27 +0100917#elif defined(OR1K)
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000918 tprintf(fmt, or1k_regs.pc);
James Hogan5f999a82013-02-22 14:44:10 +0000919#elif defined(METAG)
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000920 tprintf(fmt, metag_regs.pc);
Chris Zankel8f636ed2013-03-25 10:22:07 -0700921#elif defined(XTENSA)
922 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200923 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Chris Zankel8f636ed2013-03-25 10:22:07 -0700924 PRINTBADPC;
925 return;
926 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000927 tprintf(fmt, pc);
Vineet Gupta7daacbb2013-08-16 12:47:06 +0530928#elif defined(ARC)
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000929 tprintf(fmt, arc_regs.efa);
930#else
931# warning print_pc is not implemented for this architecture
932 PRINTBADPC;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100933#endif /* architecture */
934}
935
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000936/*
937 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
938 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
Denys Vlasenko7270de52013-02-21 15:46:34 +0100939 */
Elliott Hughes7a28f7f2014-03-03 23:45:26 +0000940#if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
Denys Vlasenko7270de52013-02-21 15:46:34 +0100941static long
942shuffle_scno(unsigned long scno)
943{
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000944 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
Denys Vlasenko7270de52013-02-21 15:46:34 +0100945 return scno;
946
947 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000948 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
Denys Vlasenko7270de52013-02-21 15:46:34 +0100949 return 0x000ffff0;
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000950 if (scno == 0x000ffff0)
951 return ARM_FIRST_SHUFFLED_SYSCALL;
Denys Vlasenko7270de52013-02-21 15:46:34 +0100952
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000953#define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
954 /*
955 * Is it ARM specific syscall?
956 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
957 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
Denys Vlasenko7270de52013-02-21 15:46:34 +0100958 */
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000959 if (scno >= 0x000f0000 &&
960 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
961 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
Denys Vlasenko7270de52013-02-21 15:46:34 +0100962 }
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000963 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
964 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
Denys Vlasenko7270de52013-02-21 15:46:34 +0100965 }
966
967 return scno;
968}
969#else
970# define shuffle_scno(scno) ((long)(scno))
971#endif
972
973static char*
974undefined_scno_name(struct tcb *tcp)
975{
976 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
977
978 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
979 return buf;
980}
981
Anton Blanchard14d51a62013-06-26 14:42:37 +1000982#ifdef POWERPC
983/*
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200984 * PTRACE_GETREGS was added to the PowerPC kernel in v2.6.23,
985 * we provide a slow fallback for old kernels.
Anton Blanchard14d51a62013-06-26 14:42:37 +1000986 */
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200987static int powerpc_getregs_old(pid_t pid)
Anton Blanchard14d51a62013-06-26 14:42:37 +1000988{
989 int i;
990 long r;
991
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200992 if (iflag) {
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200993 r = upeek(pid, sizeof(long) * PT_NIP, (long *)&ppc_regs.nip);
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200994 if (r)
995 goto out;
996 }
997#ifdef POWERPC64 /* else we never use it */
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200998 r = upeek(pid, sizeof(long) * PT_MSR, (long *)&ppc_regs.msr);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000999 if (r)
1000 goto out;
Denys Vlasenko6b3016e2013-06-28 14:51:50 +02001001#endif
Anton Blanchard9459dfb2013-07-12 12:24:02 +02001002 r = upeek(pid, sizeof(long) * PT_CCR, (long *)&ppc_regs.ccr);
Anton Blanchard14d51a62013-06-26 14:42:37 +10001003 if (r)
1004 goto out;
Anton Blanchard9459dfb2013-07-12 12:24:02 +02001005 r = upeek(pid, sizeof(long) * PT_ORIG_R3, (long *)&ppc_regs.orig_gpr3);
Anton Blanchard14d51a62013-06-26 14:42:37 +10001006 if (r)
1007 goto out;
Anton Blanchard14d51a62013-06-26 14:42:37 +10001008 for (i = 0; i <= 8; i++) {
Anton Blanchard9459dfb2013-07-12 12:24:02 +02001009 r = upeek(pid, sizeof(long) * (PT_R0 + i),
1010 (long *)&ppc_regs.gpr[i]);
Anton Blanchard14d51a62013-06-26 14:42:37 +10001011 if (r)
1012 goto out;
1013 }
Denys Vlasenko7f5a1322013-06-28 14:36:39 +02001014 out:
Anton Blanchard14d51a62013-06-26 14:42:37 +10001015 return r;
1016}
1017#endif
1018
Dmitry V. Levin9a176c92015-02-13 21:56:50 +00001019void
1020clear_regs(void)
1021{
1022 get_regs_error = -1;
1023}
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001024
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001025#if defined ARCH_REGS_FOR_GETREGSET
1026static long
1027get_regset(pid_t pid)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001028{
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001029# ifdef ARCH_IOVEC_FOR_GETREGSET
1030 /* variable iovec */
1031 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1032 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1033 &ARCH_IOVEC_FOR_GETREGSET);
1034# else
1035 /* constant iovec */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001036 static struct iovec io = {
1037 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1038 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001039 };
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001040 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001041
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001042# endif
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001043}
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001044#endif /* ARCH_REGS_FOR_GETREGSET */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001045
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001046void
1047get_regs(pid_t pid)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001048{
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001049#ifdef ARCH_REGS_FOR_GETREGSET
1050# ifdef X86_64
1051 /* Try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS. */
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001052 static int getregset_support;
1053
1054 if (getregset_support >= 0) {
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001055 get_regs_error = get_regset(pid);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001056 if (getregset_support > 0)
1057 return;
1058 if (get_regs_error >= 0) {
1059 getregset_support = 1;
1060 return;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001061 }
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001062 if (errno == EPERM || errno == ESRCH)
1063 return;
1064 getregset_support = -1;
1065 }
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001066 /* Use old method, with unreliable heuristical detection of 32-bitness. */
1067 x86_io.iov_len = sizeof(x86_64_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001068 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &x86_64_regs);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001069 if (!get_regs_error && x86_64_regs.cs == 0x23) {
1070 x86_io.iov_len = sizeof(i386_regs);
1071 /*
1072 * The order is important: i386_regs and x86_64_regs
1073 * are overlaid in memory!
1074 */
1075 i386_regs.ebx = x86_64_regs.rbx;
1076 i386_regs.ecx = x86_64_regs.rcx;
1077 i386_regs.edx = x86_64_regs.rdx;
1078 i386_regs.esi = x86_64_regs.rsi;
1079 i386_regs.edi = x86_64_regs.rdi;
1080 i386_regs.ebp = x86_64_regs.rbp;
1081 i386_regs.eax = x86_64_regs.rax;
1082 /* i386_regs.xds = x86_64_regs.ds; unused by strace */
1083 /* i386_regs.xes = x86_64_regs.es; ditto... */
1084 /* i386_regs.xfs = x86_64_regs.fs; */
1085 /* i386_regs.xgs = x86_64_regs.gs; */
1086 i386_regs.orig_eax = x86_64_regs.orig_rax;
1087 i386_regs.eip = x86_64_regs.rip;
1088 /* i386_regs.xcs = x86_64_regs.cs; */
1089 /* i386_regs.eflags = x86_64_regs.eflags; */
1090 i386_regs.esp = x86_64_regs.rsp;
1091 /* i386_regs.xss = x86_64_regs.ss; */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001092 }
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001093# else /* !X86_64 */
1094 /* Assume that PTRACE_GETREGSET works. */
1095 get_regs_error = get_regset(pid);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001096# endif
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001097#elif defined ARCH_REGS_FOR_GETREGS
1098# if defined SPARC || defined SPARC64
1099 /* SPARC systems have the meaning of data and addr reversed */
1100 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&ARCH_REGS_FOR_GETREGS, 0);
1101# elif defined POWERPC
1102 static bool old_kernel = 0;
1103 if (old_kernel)
1104 goto old;
1105 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1106 if (get_regs_error && errno == EIO) {
1107 old_kernel = 1;
1108 old:
1109 get_regs_error = powerpc_getregs_old(pid);
1110 }
1111# else
1112 /* Assume that PTRACE_GETREGS works. */
1113 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1114# endif
1115
1116#else /* !ARCH_REGS_FOR_GETREGSET && !ARCH_REGS_FOR_GETREGS */
1117# warning get_regs is not implemented for this architecture yet
1118 get_regs_error = 0;
1119#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001120}
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001121
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001122/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001123 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1124 * 1: ok, continue in trace_syscall_entering().
1125 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001126 * ("????" etc) and bail out.
1127 */
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01001128static int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001129get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001130{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001131 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001132
Denys Vlasenko523635f2012-02-25 02:44:25 +01001133#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001134 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001135 return -1;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001136
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001137 if (s390_gpr2 != -ENOSYS) {
Roland McGrath2f924ca2003-06-26 22:23:28 +00001138 /*
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001139 * Since kernel version 2.5.44 the scno gets passed in gpr2.
Roland McGrath2f924ca2003-06-26 22:23:28 +00001140 */
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001141 scno = s390_gpr2;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001142 } else {
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001143 /*
Michal Ludvig882eda82002-11-11 12:50:47 +00001144 * Old style of "passing" the scno via the SVC instruction.
1145 */
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001146 long psw;
Michal Ludvig882eda82002-11-11 12:50:47 +00001147 long opcode, offset_reg, tmp;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001148 void *svc_addr;
Denys Vlasenko7c9ba8b2011-08-19 19:46:32 +02001149 static const int gpr_offset[16] = {
1150 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
1151 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
1152 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
1153 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
1154 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001155
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001156 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0)
Michal Ludvig882eda82002-11-11 12:50:47 +00001157 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001158 errno = 0;
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001159 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +00001160 if (errno) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001161 perror_msg("peektext(psw-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +00001162 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001163 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001164
1165 /*
1166 * We have to check if the SVC got executed directly or via an
1167 * EXECUTE instruction. In case of EXECUTE it is necessary to do
1168 * instruction decoding to derive the system call number.
1169 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1170 * so that this doesn't work if a SVC opcode is part of an EXECUTE
1171 * opcode. Since there is no way to find out the opcode size this
1172 * is the best we can do...
1173 */
Michal Ludvig882eda82002-11-11 12:50:47 +00001174 if ((opcode & 0xff00) == 0x0a00) {
1175 /* SVC opcode */
1176 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +00001177 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001178 else {
1179 /* SVC got executed by EXECUTE instruction */
1180
1181 /*
1182 * Do instruction decoding of EXECUTE. If you really want to
1183 * understand this, read the Principles of Operations.
1184 */
1185 svc_addr = (void *) (opcode & 0xfff);
1186
1187 tmp = 0;
1188 offset_reg = (opcode & 0x000f0000) >> 16;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001189 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001190 return -1;
1191 svc_addr += tmp;
1192
1193 tmp = 0;
1194 offset_reg = (opcode & 0x0000f000) >> 12;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001195 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001196 return -1;
1197 svc_addr += tmp;
1198
Denys Vlasenkofb036672009-01-23 16:30:26 +00001199 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
Michal Ludvig882eda82002-11-11 12:50:47 +00001200 if (errno)
1201 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001202# if defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +00001203 scno >>= 48;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001204# else
Michal Ludvig882eda82002-11-11 12:50:47 +00001205 scno >>= 16;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001206# endif
Michal Ludvig882eda82002-11-11 12:50:47 +00001207 tmp = 0;
1208 offset_reg = (opcode & 0x00f00000) >> 20;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001209 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001210 return -1;
1211
1212 scno = (scno | tmp) & 0xff;
1213 }
1214 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001215#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001216 scno = ppc_regs.gpr[0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001217# ifdef POWERPC64
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001218 unsigned int currpers;
Andreas Schwabd69fa492010-07-12 21:39:57 +02001219
James Yanga78d7772014-02-18 15:32:43 -06001220 /*
1221 * Check for 64/32 bit mode.
1222 * Embedded implementations covered by Book E extension of PPC use
1223 * bit 0 (CM) of 32-bit Machine state register (MSR).
1224 * Other implementations use bit 0 (SF) of 64-bit MSR.
1225 */
1226 currpers = (ppc_regs.msr & 0x8000000080000000) ? 0 : 1;
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001227 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001228# endif
1229#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001230 scno = avr32_regs.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001231#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001232 if (upeek(tcp->pid, PT_ORIG_P0, &scno))
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001233 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001234#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001235 scno = i386_regs.orig_eax;
H.J. Lu35be5812012-04-16 13:00:01 +02001236#elif defined(X86_64) || defined(X32)
1237# ifndef __X32_SYSCALL_BIT
1238# define __X32_SYSCALL_BIT 0x40000000
1239# endif
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001240 unsigned int currpers;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001241# if 1
1242 /* GETREGSET of NT_PRSTATUS tells us regset size,
1243 * which unambiguously detects i386.
1244 *
1245 * Linux kernel distinguishes x86-64 and x32 processes
1246 * solely by looking at __X32_SYSCALL_BIT:
1247 * arch/x86/include/asm/compat.h::is_x32_task():
1248 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1249 * return true;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001250 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001251 if (x86_io.iov_len == sizeof(i386_regs)) {
1252 scno = i386_regs.orig_eax;
1253 currpers = 1;
1254 } else {
1255 scno = x86_64_regs.orig_rax;
1256 currpers = 0;
1257 if (scno & __X32_SYSCALL_BIT) {
Dmitry V. Levinc3948322015-02-05 16:15:46 +00001258 /*
1259 * Syscall number -1 requires special treatment:
1260 * it might be a side effect of SECCOMP_RET_ERRNO
1261 * filtering that sets orig_rax to -1
1262 * in some versions of linux kernel.
1263 * If that is the case, then
1264 * __X32_SYSCALL_BIT logic does not apply.
1265 */
1266 if ((long long) x86_64_regs.orig_rax != -1) {
1267 scno -= __X32_SYSCALL_BIT;
1268 currpers = 2;
1269 } else {
1270# ifdef X32
1271 currpers = 2;
1272# endif
1273 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001274 }
1275 }
1276# elif 0
1277 /* cs = 0x33 for long mode (native 64 bit and x32)
1278 * cs = 0x23 for compatibility mode (32 bit)
1279 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1280 */
1281 scno = x86_64_regs.orig_rax;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001282 switch (x86_64_regs.cs) {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001283 case 0x23: currpers = 1; break;
H.J. Lu35be5812012-04-16 13:00:01 +02001284 case 0x33:
1285 if (x86_64_regs.ds == 0x2b) {
1286 currpers = 2;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001287 scno &= ~__X32_SYSCALL_BIT;
H.J. Lu35be5812012-04-16 13:00:01 +02001288 } else
1289 currpers = 0;
1290 break;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001291 default:
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001292 fprintf(stderr, "Unknown value CS=0x%08X while "
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001293 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001294 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001295 currpers = current_personality;
1296 break;
1297 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001298# elif 0
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001299 /* This version analyzes the opcode of a syscall instruction.
1300 * (int 0x80 on i386 vs. syscall on x86-64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001301 * It works, but is too complicated, and strictly speaking, unreliable.
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001302 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001303 unsigned long call, rip = x86_64_regs.rip;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001304 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1305 rip -= 2;
1306 errno = 0;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001307 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001308 if (errno)
1309 fprintf(stderr, "ptrace_peektext failed: %s\n",
1310 strerror(errno));
1311 switch (call & 0xffff) {
1312 /* x86-64: syscall = 0x0f 0x05 */
1313 case 0x050f: currpers = 0; break;
1314 /* i386: int 0x80 = 0xcd 0x80 */
1315 case 0x80cd: currpers = 1; break;
1316 default:
1317 currpers = current_personality;
1318 fprintf(stderr,
1319 "Unknown syscall opcode (0x%04X) while "
1320 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001321 "PID=%d\n", (int)call, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001322 break;
1323 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001324# endif
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001325
H.J. Lu35be5812012-04-16 13:00:01 +02001326# ifdef X32
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001327 /* If we are built for a x32 system, then personality 0 is x32
1328 * (not x86_64), and stracing of x86_64 apps is not supported.
1329 * Stracing of i386 apps is still supported.
H.J. Lu085e4282012-04-17 11:05:04 -07001330 */
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001331 if (currpers == 0) {
1332 fprintf(stderr, "syscall_%lu(...) in unsupported "
1333 "64-bit mode of process PID=%d\n",
1334 scno, tcp->pid);
1335 return 0;
H.J. Lu35be5812012-04-16 13:00:01 +02001336 }
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001337 currpers &= ~2; /* map 2,1 to 0,1 */
H.J. Lu35be5812012-04-16 13:00:01 +02001338# endif
H.J. Lu085e4282012-04-17 11:05:04 -07001339 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001340#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001341# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01001342 long psr;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001343 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001344 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
1345 if (ia64_ia32mode) {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001346 if (upeek(tcp->pid, PT_R1, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001347 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001348 } else {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001349 if (upeek(tcp->pid, PT_R15, &scno) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001350 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001351 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001352#elif defined(AARCH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001353 switch (aarch64_io.iov_len) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001354 case sizeof(aarch64_regs):
1355 /* We are in 64-bit mode */
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001356 scno = aarch64_regs.regs[8];
1357 update_personality(tcp, 1);
1358 break;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001359 case sizeof(arm_regs):
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001360 /* We are in 32-bit mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001361 /* Note: we don't support OABI, unlike 32-bit ARM build */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001362 scno = arm_regs.ARM_r7;
Elliott Hughes7a28f7f2014-03-03 23:45:26 +00001363 scno = shuffle_scno(scno);
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001364 update_personality(tcp, 0);
1365 break;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001366 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001367#elif defined(ARM)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001368 if (arm_regs.ARM_ip != 0) {
1369 /* It is not a syscall entry */
1370 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001371 tcp->flags |= TCB_INSYSCALL;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001372 return 0;
1373 }
1374 /* Note: we support only 32-bit CPUs, not 26-bit */
1375
Dmitry V. Levin0160e162014-03-18 23:37:43 +00001376# if !defined(__ARM_EABI__) || ENABLE_ARM_OABI
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001377 if (arm_regs.ARM_cpsr & 0x20)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001378 /* Thumb mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001379 goto scno_in_r7;
1380 /* ARM mode */
1381 /* Check EABI/OABI by examining SVC insn's low 24 bits */
1382 errno = 0;
1383 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1384 if (errno)
1385 return -1;
1386 /* EABI syscall convention? */
Dmitry V. Levin03b44542015-01-14 07:21:36 +00001387 if ((unsigned long) scno != 0xef000000) {
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001388 /* No, it's OABI */
1389 if ((scno & 0x0ff00000) != 0x0f900000) {
1390 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1391 tcp->pid, scno);
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001392 return -1;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001393 }
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001394 /* Fixup the syscall number */
1395 scno &= 0x000fffff;
1396 } else {
1397 scno_in_r7:
1398 scno = arm_regs.ARM_r7;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001399 }
Dmitry V. Levin0160e162014-03-18 23:37:43 +00001400# else /* __ARM_EABI__ || !ENABLE_ARM_OABI */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001401 scno = arm_regs.ARM_r7;
1402# endif
Denys Vlasenko7270de52013-02-21 15:46:34 +01001403 scno = shuffle_scno(scno);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001404#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001405 if (upeek(tcp->pid, 4*PT_ORIG_D0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001406 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001407#elif defined(MIPS)
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +00001408 scno = mips_REG_V0;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001409
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001410 if (!SCNO_IN_RANGE(scno)) {
Dmitry V. Levinf6777152015-03-02 15:06:51 +00001411 if (mips_REG_A3 == 0 || mips_REG_A3 == (uint64_t) -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001412 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001413 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001414 return 0;
1415 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001416 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001417#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001418 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001419 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001420 if (upeek(tcp->pid, REG_R0, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001421 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001422
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001423 /*
1424 * Do some sanity checks to figure out if it's
1425 * really a syscall entry
1426 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001427 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001428 if (alpha_a3 == 0 || alpha_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001429 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001430 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001431 return 0;
1432 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001433 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001434#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001435 /* Disassemble the syscall trap. */
1436 /* Retrieve the syscall trap instruction. */
Denys Vlasenko46455822013-02-05 17:02:59 +01001437 unsigned long trap;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001438 errno = 0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001439# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001440 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001441 trap >>= 32;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001442# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001443 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001444# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001445 if (errno)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001446 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001447
1448 /* Disassemble the trap to see what personality to use. */
1449 switch (trap) {
1450 case 0x91d02010:
1451 /* Linux/SPARC syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001452 update_personality(tcp, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001453 break;
1454 case 0x91d0206d:
1455 /* Linux/SPARC64 syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001456 update_personality(tcp, 2);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001457 break;
1458 case 0x91d02000:
1459 /* SunOS syscall trap. (pers 1) */
1460 fprintf(stderr, "syscall: SunOS no support\n");
1461 return -1;
1462 case 0x91d02008:
1463 /* Solaris 2.x syscall trap. (per 2) */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001464 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001465 break;
1466 case 0x91d02009:
1467 /* NetBSD/FreeBSD syscall trap. */
1468 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1469 return -1;
1470 case 0x91d02027:
1471 /* Solaris 2.x gettimeofday */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001472 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001473 break;
1474 default:
Denys Vlasenko523635f2012-02-25 02:44:25 +01001475# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001476 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001477# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001478 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001479# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001480 return -1;
1481 }
1482
1483 /* Extract the system call number from the registers. */
1484 if (trap == 0x91d02027)
1485 scno = 156;
1486 else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001487 scno = sparc_regs.u_regs[U_REG_G1];
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001488 if (scno == 0) {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001489 scno = sparc_regs.u_regs[U_REG_O0];
1490 memmove(&sparc_regs.u_regs[U_REG_O0], &sparc_regs.u_regs[U_REG_O1], 7*sizeof(sparc_regs.u_regs[0]));
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001491 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001492#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001493 if (upeek(tcp->pid, PT_GR20, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001494 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001495#elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001496 /*
1497 * In the new syscall ABI, the system call number is in R3.
1498 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001499 if (upeek(tcp->pid, 4*(REG_REG0+3), &scno) < 0)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001500 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001501
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001502 if (scno < 0) {
1503 /* Odd as it may seem, a glibc bug has been known to cause
1504 glibc to issue bogus negative syscall numbers. So for
1505 our purposes, make strace print what it *should* have been */
1506 long correct_scno = (scno & 0xff);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001507 if (debug_flag)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001508 fprintf(stderr,
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001509 "Detected glibc bug: bogus system call"
1510 " number = %ld, correcting to %ld\n",
1511 scno,
1512 correct_scno);
1513 scno = correct_scno;
1514 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001515#elif defined(SH64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001516 if (upeek(tcp->pid, REG_SYSCALL, &scno) < 0)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001517 return -1;
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001518 scno &= 0xFFFF;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001519#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001520 if (upeek(tcp->pid, 4*PT_R9, &scno) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001521 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001522#elif defined(TILE)
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001523 unsigned int currpers;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001524 scno = tile_regs.regs[10];
1525# ifdef __tilepro__
1526 currpers = 1;
1527# else
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001528# ifndef PT_FLAGS_COMPAT
1529# define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1530# endif
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001531 if (tile_regs.flags & PT_FLAGS_COMPAT)
1532 currpers = 1;
1533 else
1534 currpers = 0;
1535# endif
1536 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001537#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001538 if (upeek(tcp->pid, 0, &scno) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001539 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001540#elif defined(OR1K)
1541 scno = or1k_regs.gpr[11];
James Hogan5f999a82013-02-22 14:44:10 +00001542#elif defined(METAG)
1543 scno = metag_regs.dx[0][1]; /* syscall number in D1Re0 (D1.0) */
Chris Zankel8f636ed2013-03-25 10:22:07 -07001544#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001545 if (upeek(tcp->pid, SYSCALL_NR, &scno) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001546 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301547# elif defined(ARC)
1548 scno = arc_regs.scratch.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001549#endif
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001550
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001551 tcp->scno = scno;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001552 if (SCNO_IS_VALID(tcp->scno)) {
1553 tcp->s_ent = &sysent[scno];
1554 tcp->qual_flg = qual_flags[scno];
1555 } else {
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01001556 static const struct_sysent unknown = {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001557 .nargs = MAX_ARGS,
1558 .sys_flags = 0,
1559 .sys_func = printargs,
1560 .sys_name = "unknown", /* not used */
1561 };
1562 tcp->s_ent = &unknown;
1563 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1564 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001565 return 1;
1566}
1567
Dmitry V. Levind70c41d2015-02-05 23:43:06 +00001568/*
1569 * Cannot rely on __kernel_[u]long_t being defined,
1570 * it is quite a recent feature of <asm/posix_types.h>.
1571 */
1572#ifdef __kernel_long_t
1573typedef __kernel_long_t kernel_long_t;
1574typedef __kernel_ulong_t kernel_ulong_t;
1575#else
1576# ifdef X32
1577typedef long long kernel_long_t;
1578typedef unsigned long long kernel_ulong_t;
1579# else
1580typedef long kernel_long_t;
1581typedef unsigned long kernel_ulong_t;
1582# endif
1583#endif
1584
1585/*
1586 * Check the syscall return value register value for whether it is
1587 * a negated errno code indicating an error, or a success return value.
1588 */
1589static inline bool
1590is_negated_errno(kernel_ulong_t val)
1591{
Dmitry V. Levinfb585db2015-02-19 16:58:52 +00001592 /* Linux kernel defines MAX_ERRNO to 4095. */
1593 kernel_ulong_t max = -(kernel_long_t) 4095;
Dmitry V. Levind70c41d2015-02-05 23:43:06 +00001594
1595#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1596 if (current_wordsize < sizeof(val)) {
1597 val = (uint32_t) val;
1598 max = (uint32_t) max;
1599 }
1600#elif defined X32
1601 /*
1602 * current_wordsize is 4 even in personality 0 (native X32)
1603 * but truncation _must not_ be done in it.
1604 * can't check current_wordsize here!
1605 */
1606 if (current_personality != 0) {
1607 val = (uint32_t) val;
1608 max = (uint32_t) max;
1609 }
1610#endif
1611
Dmitry V. Levinfb585db2015-02-19 16:58:52 +00001612 return val >= max;
Dmitry V. Levind70c41d2015-02-05 23:43:06 +00001613}
1614
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001615/* Return -1 on error or 1 on success (never 0!) */
Roland McGratha4d48532005-06-08 20:45:28 +00001616static int
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001617get_syscall_args(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001618{
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001619 int i, nargs;
1620
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001621 nargs = tcp->s_ent->nargs;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001622
Denys Vlasenko523635f2012-02-25 02:44:25 +01001623#if defined(S390) || defined(S390X)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001624 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001625 if (upeek(tcp->pid, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001626 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001627#elif defined(ALPHA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001628 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001629 if (upeek(tcp->pid, REG_A0+i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001630 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001631#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001632 if (!ia64_ia32mode) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001633 unsigned long *out0, cfm, sof, sol;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001634 long rbs_end;
1635 /* be backwards compatible with kernel < 2.4.4... */
1636# ifndef PT_RBS_END
1637# define PT_RBS_END PT_AR_BSP
1638# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001639
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001640 if (upeek(tcp->pid, PT_RBS_END, &rbs_end) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001641 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001642 if (upeek(tcp->pid, PT_CFM, (long *) &cfm) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001643 return -1;
1644
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001645 sof = (cfm >> 0) & 0x7f;
1646 sol = (cfm >> 7) & 0x7f;
1647 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1648
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001649 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001650 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1651 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1652 return -1;
1653 }
1654 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001655 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1656 PT_R9 /* ECX = out1 */,
1657 PT_R10 /* EDX = out2 */,
1658 PT_R14 /* ESI = out3 */,
1659 PT_R15 /* EDI = out4 */,
1660 PT_R13 /* EBP = out5 */};
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001661
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001662 for (i = 0; i < nargs; ++i) {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001663 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001664 return -1;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001665 /* truncate away IVE sign-extension */
1666 tcp->u_arg[i] &= 0xffffffff;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001667 }
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001668 }
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +00001669#elif defined LINUX_MIPSN64
1670 (void)i;
1671 (void)nargs;
1672 tcp->u_arg[0] = mips_REG_A0;
1673 tcp->u_arg[1] = mips_REG_A1;
1674 tcp->u_arg[2] = mips_REG_A2;
1675 tcp->u_arg[3] = mips_REG_A3;
1676 tcp->u_arg[4] = mips_REG_A4;
1677 tcp->u_arg[5] = mips_REG_A5;
1678#elif defined LINUX_MIPSN32
1679 (void)i;
1680 (void)nargs;
1681 tcp->u_arg[0] = tcp->ext_arg[0] = mips_REG_A0;
1682 tcp->u_arg[1] = tcp->ext_arg[1] = mips_REG_A1;
1683 tcp->u_arg[2] = tcp->ext_arg[2] = mips_REG_A2;
1684 tcp->u_arg[3] = tcp->ext_arg[3] = mips_REG_A3;
1685 tcp->u_arg[4] = tcp->ext_arg[4] = mips_REG_A4;
1686 tcp->u_arg[5] = tcp->ext_arg[5] = mips_REG_A5;
1687#elif defined LINUX_MIPSO32
1688 (void)i;
1689 (void)nargs;
1690 tcp->u_arg[0] = mips_REG_A0;
1691 tcp->u_arg[1] = mips_REG_A1;
1692 tcp->u_arg[2] = mips_REG_A2;
1693 tcp->u_arg[3] = mips_REG_A3;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001694 if (nargs > 4) {
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +00001695 umoven(tcp, mips_REG_SP + 4 * 4,
1696 (nargs - 4) * sizeof(tcp->u_arg[0]),
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001697 (char *)(tcp->u_arg + 4));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001698 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001699#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001700 (void)i;
1701 (void)nargs;
1702 tcp->u_arg[0] = ppc_regs.orig_gpr3;
1703 tcp->u_arg[1] = ppc_regs.gpr[4];
1704 tcp->u_arg[2] = ppc_regs.gpr[5];
1705 tcp->u_arg[3] = ppc_regs.gpr[6];
1706 tcp->u_arg[4] = ppc_regs.gpr[7];
1707 tcp->u_arg[5] = ppc_regs.gpr[8];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001708#elif defined(SPARC) || defined(SPARC64)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001709 for (i = 0; i < nargs; ++i)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001710 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001711#elif defined(HPPA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001712 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001713 if (upeek(tcp->pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001714 return -1;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001715#elif defined(ARM) || defined(AARCH64)
1716# if defined(AARCH64)
1717 if (tcp->currpers == 1)
1718 for (i = 0; i < nargs; ++i)
1719 tcp->u_arg[i] = aarch64_regs.regs[i];
1720 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001721# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001722 for (i = 0; i < nargs; ++i)
Denys Vlasenko401374e2013-02-06 18:24:39 +01001723 tcp->u_arg[i] = arm_regs.uregs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001724#elif defined(AVR32)
Denys Vlasenkob5b25892011-08-30 19:04:54 +02001725 (void)i;
1726 (void)nargs;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001727 tcp->u_arg[0] = avr32_regs.r12;
1728 tcp->u_arg[1] = avr32_regs.r11;
1729 tcp->u_arg[2] = avr32_regs.r10;
1730 tcp->u_arg[3] = avr32_regs.r9;
1731 tcp->u_arg[4] = avr32_regs.r5;
1732 tcp->u_arg[5] = avr32_regs.r3;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001733#elif defined(BFIN)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001734 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 +02001735
Denys Vlasenko4b887a52011-08-23 13:32:38 +02001736 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001737 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001738 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001739#elif defined(SH)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001740 static const int syscall_regs[MAX_ARGS] = {
1741 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1742 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001743 };
1744
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001745 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001746 if (upeek(tcp->pid, syscall_regs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001747 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001748#elif defined(SH64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001749 int i;
1750 /* Registers used by SH5 Linux system calls for parameters */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001751 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001752
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001753 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001754 if (upeek(tcp->pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001755 return -1;
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001756#elif defined(I386)
1757 (void)i;
1758 (void)nargs;
1759 tcp->u_arg[0] = i386_regs.ebx;
1760 tcp->u_arg[1] = i386_regs.ecx;
1761 tcp->u_arg[2] = i386_regs.edx;
1762 tcp->u_arg[3] = i386_regs.esi;
1763 tcp->u_arg[4] = i386_regs.edi;
1764 tcp->u_arg[5] = i386_regs.ebp;
H.J. Lu35be5812012-04-16 13:00:01 +02001765#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001766 (void)i;
1767 (void)nargs;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001768 if (x86_io.iov_len != sizeof(i386_regs)) {
1769 /* x86-64 or x32 ABI */
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001770 tcp->u_arg[0] = x86_64_regs.rdi;
1771 tcp->u_arg[1] = x86_64_regs.rsi;
1772 tcp->u_arg[2] = x86_64_regs.rdx;
1773 tcp->u_arg[3] = x86_64_regs.r10;
1774 tcp->u_arg[4] = x86_64_regs.r8;
1775 tcp->u_arg[5] = x86_64_regs.r9;
H.J. Lu35be5812012-04-16 13:00:01 +02001776# ifdef X32
1777 tcp->ext_arg[0] = x86_64_regs.rdi;
1778 tcp->ext_arg[1] = x86_64_regs.rsi;
1779 tcp->ext_arg[2] = x86_64_regs.rdx;
1780 tcp->ext_arg[3] = x86_64_regs.r10;
1781 tcp->ext_arg[4] = x86_64_regs.r8;
1782 tcp->ext_arg[5] = x86_64_regs.r9;
1783# endif
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001784 } else {
1785 /* i386 ABI */
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001786 /* Zero-extend from 32 bits */
1787 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1788 * if you need to use *sign-extended* parameter.
1789 */
1790 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1791 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1792 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1793 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1794 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1795 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001796 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001797#elif defined(MICROBLAZE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001798 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001799 if (upeek(tcp->pid, (5 + i) * 4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001800 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001801#elif defined(CRISV10) || defined(CRISV32)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001802 static const int crisregs[MAX_ARGS] = {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001803 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001804 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001805 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001806
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001807 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001808 if (upeek(tcp->pid, crisregs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001809 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001810#elif defined(TILE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001811 for (i = 0; i < nargs; ++i)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001812 tcp->u_arg[i] = tile_regs.regs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001813#elif defined(M68K)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001814 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001815 if (upeek(tcp->pid, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001816 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001817#elif defined(OR1K)
1818 (void)nargs;
1819 for (i = 0; i < 6; ++i)
1820 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
James Hogan5f999a82013-02-22 14:44:10 +00001821#elif defined(METAG)
1822 for (i = 0; i < nargs; i++)
1823 /* arguments go backwards from D1Ar1 (D1.3) */
1824 tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
Chris Zankel8f636ed2013-03-25 10:22:07 -07001825#elif defined(XTENSA)
1826 /* arg0: a6, arg1: a3, arg2: a4, arg3: a5, arg4: a8, arg5: a9 */
1827 static const int xtensaregs[MAX_ARGS] = { 6, 3, 4, 5, 8, 9 };
1828 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001829 if (upeek(tcp->pid, REG_A_BASE + xtensaregs[i], &tcp->u_arg[i]) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001830 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301831# elif defined(ARC)
1832 long *arc_args = &arc_regs.scratch.r0;
1833 for (i = 0; i < nargs; ++i)
1834 tcp->u_arg[i] = *arc_args--;
1835
Denys Vlasenko523635f2012-02-25 02:44:25 +01001836#else /* Other architecture (32bits specific) */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001837 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001838 if (upeek(tcp->pid, i*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001839 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001840#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001841 return 1;
1842}
1843
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00001844static int
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001845trace_syscall_entering(struct tcb *tcp)
1846{
1847 int res, scno_good;
1848
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001849 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001850 if (res == 0)
1851 return res;
Dmitry V. Levin23ce9e42015-02-08 13:05:53 +00001852 if (res == 1)
1853 res = get_syscall_args(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001854
1855 if (res != 1) {
1856 printleader(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001857 if (scno_good != 1)
Denys Vlasenkob7a6dae2012-03-20 16:48:35 +01001858 tprints("????" /* anti-trigraph gap */ "(");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001859 else if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01001860 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001861 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001862 tprintf("%s(", tcp->s_ent->sys_name);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001863 /*
1864 * " <unavailable>" will be added later by the code which
1865 * detects ptrace errors.
1866 */
1867 goto ret;
1868 }
1869
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02001870 if ( sys_execve == tcp->s_ent->sys_func
1871# if defined(SPARC) || defined(SPARC64)
1872 || sys_execv == tcp->s_ent->sys_func
1873# endif
1874 ) {
1875 hide_log_until_execve = 0;
1876 }
1877
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001878#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001879 while (1) {
Denys Vlasenko523635f2012-02-25 02:44:25 +01001880# ifdef SYS_socket_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001881 if (tcp->s_ent->sys_func == sys_socketcall) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00001882 decode_socket_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001883 break;
1884 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001885# endif
1886# ifdef SYS_ipc_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001887 if (tcp->s_ent->sys_func == sys_ipc) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00001888 decode_ipc_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001889 break;
1890 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001891# endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001892 break;
1893 }
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001894#endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001895
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001896 if (!(tcp->qual_flg & QUAL_TRACE)
1897 || (tracing_paths && !pathtrace_match(tcp))
1898 ) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001899 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
1900 return 0;
1901 }
1902
1903 tcp->flags &= ~TCB_FILTERED;
1904
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02001905 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001906 res = 0;
1907 goto ret;
1908 }
1909
Masatake YAMATOed69fc22014-04-16 15:33:35 +09001910#ifdef USE_LIBUNWIND
1911 if (stack_trace_enabled) {
1912 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
1913 unwind_capture_stacktrace(tcp);
1914 }
1915#endif
1916
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001917 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001918 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01001919 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001920 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001921 tprintf("%s(", tcp->s_ent->sys_name);
1922 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001923 res = printargs(tcp);
1924 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001925 res = tcp->s_ent->sys_func(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001926
Dmitry V. Levinb742d8c2012-09-17 22:40:12 +00001927 fflush(tcp->outf);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001928 ret:
1929 tcp->flags |= TCB_INSYSCALL;
1930 /* Measure the entrance time as late as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001931 if (Tflag || cflag)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001932 gettimeofday(&tcp->etime, NULL);
1933 return res;
1934}
1935
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001936/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001937 * 1: ok, continue in trace_syscall_exiting().
1938 * -1: error, trace_syscall_exiting() should print error indicator
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001939 * ("????" etc) and bail out.
1940 */
1941static int
1942get_syscall_result(struct tcb *tcp)
1943{
Dmitry V. Levin5d092242015-02-13 23:06:36 +00001944#if defined ARCH_REGS_FOR_GETREGSET || defined ARCH_REGS_FOR_GETREGS
1945 /* already done by get_regs */
1946#elif defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001947 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001948 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001949#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001950 if (upeek(tcp->pid, PT_R0, &bfin_r0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001951 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001952#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001953# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01001954 long psr;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001955 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001956 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001957 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001958 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001959 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001960 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001961#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001962 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001963 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001964#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001965 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001966 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001967 if (upeek(tcp->pid, REG_R0, &alpha_r0) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001968 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001969#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001970 if (upeek(tcp->pid, PT_GR28, &hppa_r28) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001971 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001972#elif defined(SH)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001973 /* new syscall ABI returns result in R0 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001974 if (upeek(tcp->pid, 4*REG_REG0, (long *)&sh_r0) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001975 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001976#elif defined(SH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001977 /* ABI defines result returned in r9 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001978 if (upeek(tcp->pid, REG_GENERAL(9), (long *)&sh64_r9) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001979 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001980#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001981 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001982 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001983#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001984 if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001985 return -1;
Chris Zankel8f636ed2013-03-25 10:22:07 -07001986#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001987 if (upeek(tcp->pid, REG_A_BASE + 2, &xtensa_a2) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001988 return -1;
Dmitry V. Levin5d092242015-02-13 23:06:36 +00001989#else
1990# error get_syscall_result is not implemented for this architecture
Denys Vlasenko523635f2012-02-25 02:44:25 +01001991#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001992 return 1;
1993}
1994
Denys Vlasenko907735a2012-03-21 00:23:16 +01001995/* Returns:
1996 * 1: ok, continue in trace_syscall_exiting().
1997 * -1: error, trace_syscall_exiting() should print error indicator
1998 * ("????" etc) and bail out.
1999 */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002000static void
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002001get_error(struct tcb *tcp)
2002{
2003 int u_error = 0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002004 int check_errno = 1;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002005 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002006 check_errno = 0;
2007 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002008#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002009 if (check_errno && is_negated_errno(s390_gpr2)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002010 tcp->u_rval = -1;
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002011 u_error = -s390_gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002012 }
2013 else {
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002014 tcp->u_rval = s390_gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002015 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002016#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002017 if (check_errno && is_negated_errno(i386_regs.eax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002018 tcp->u_rval = -1;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002019 u_error = -i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002020 }
2021 else {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002022 tcp->u_rval = i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002023 }
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00002024#elif defined(X86_64) || defined(X32)
2025 /*
2026 * In X32, return value is 64-bit (llseek uses one).
2027 * Using merely "long rax" would not work.
2028 */
2029 kernel_long_t rax;
2030
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002031 if (x86_io.iov_len == sizeof(i386_regs)) {
2032 /* Sign extend from 32 bits */
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00002033 rax = (int32_t) i386_regs.eax;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002034 } else {
2035 rax = x86_64_regs.rax;
2036 }
2037 if (check_errno && is_negated_errno(rax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002038 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002039 u_error = -rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002040 }
2041 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002042 tcp->u_rval = rax;
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00002043# ifdef X32
2044 /* tcp->u_rval contains a truncated value */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002045 tcp->u_lrval = rax;
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00002046# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002047 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002048#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02002049 if (ia64_ia32mode) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002050 int err;
2051
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002052 err = (int)ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002053 if (check_errno && is_negated_errno(err)) {
2054 tcp->u_rval = -1;
2055 u_error = -err;
2056 }
2057 else {
2058 tcp->u_rval = err;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002059 }
2060 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002061 if (check_errno && ia64_r10) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002062 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002063 u_error = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002064 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002065 tcp->u_rval = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002066 }
2067 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002068#elif defined(MIPS)
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +00002069 if (check_errno && mips_REG_A3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002070 tcp->u_rval = -1;
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +00002071 u_error = mips_REG_V0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002072 } else {
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +00002073# if defined LINUX_MIPSN32
2074 tcp->u_lrval = mips_REG_V0;
H.J. Ludd0130b2012-04-16 12:16:45 +02002075# endif
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +00002076 tcp->u_rval = mips_REG_V0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002077 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002078#elif defined(POWERPC)
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002079 if (check_errno && (ppc_regs.ccr & 0x10000000)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002080 tcp->u_rval = -1;
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002081 u_error = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002082 }
2083 else {
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002084 tcp->u_rval = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002085 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002086#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002087 if (check_errno && is_negated_errno(m68k_d0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002088 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002089 u_error = -m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002090 }
2091 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002092 tcp->u_rval = m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002093 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002094#elif defined(ARM) || defined(AARCH64)
2095# if defined(AARCH64)
2096 if (tcp->currpers == 1) {
2097 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2098 tcp->u_rval = -1;
2099 u_error = -aarch64_regs.regs[0];
2100 }
2101 else {
2102 tcp->u_rval = aarch64_regs.regs[0];
2103 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002104 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002105 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002106# endif
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002107 {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002108 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002109 tcp->u_rval = -1;
Denys Vlasenko401374e2013-02-06 18:24:39 +01002110 u_error = -arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002111 }
2112 else {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002113 tcp->u_rval = arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002114 }
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002115 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002116#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002117 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002118 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002119 u_error = -avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002120 }
2121 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002122 tcp->u_rval = avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002123 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002124#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002125 if (check_errno && is_negated_errno(bfin_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002126 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002127 u_error = -bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002128 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002129 tcp->u_rval = bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002130 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002131#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002132 if (check_errno && alpha_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002133 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002134 u_error = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002135 }
2136 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002137 tcp->u_rval = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002138 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002139#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002140 if (check_errno && sparc_regs.psr & PSR_C) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002141 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002142 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002143 }
2144 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002145 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002146 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002147#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002148 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002149 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002150 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002151 }
2152 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002153 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002154 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002155#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002156 if (check_errno && is_negated_errno(hppa_r28)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002157 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002158 u_error = -hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002159 }
2160 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002161 tcp->u_rval = hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002162 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002163#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002164 if (check_errno && is_negated_errno(sh_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002165 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002166 u_error = -sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002167 }
2168 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002169 tcp->u_rval = sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002170 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002171#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002172 if (check_errno && is_negated_errno(sh64_r9)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002173 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002174 u_error = -sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002175 }
2176 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002177 tcp->u_rval = sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002178 }
James Hogan5f999a82013-02-22 14:44:10 +00002179#elif defined(METAG)
2180 /* result pointer in D0Re0 (D0.0) */
2181 if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2182 tcp->u_rval = -1;
2183 u_error = -metag_regs.dx[0][0];
2184 }
2185 else {
2186 tcp->u_rval = metag_regs.dx[0][0];
2187 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002188#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002189 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002190 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002191 u_error = -cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002192 }
2193 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002194 tcp->u_rval = cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002195 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002196#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002197 /*
2198 * The standard tile calling convention returns the value (or negative
2199 * errno) in r0, and zero (or positive errno) in r1.
2200 * Until at least kernel 3.8, however, the r1 value is not reflected
2201 * in ptregs at this point, so we use r0 here.
2202 */
2203 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002204 tcp->u_rval = -1;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002205 u_error = -tile_regs.regs[0];
2206 } else {
2207 tcp->u_rval = tile_regs.regs[0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002208 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002209#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002210 if (check_errno && is_negated_errno(microblaze_r3)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002211 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002212 u_error = -microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002213 }
2214 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002215 tcp->u_rval = microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002216 }
Christian Svensson492f81f2013-02-14 13:26:27 +01002217#elif defined(OR1K)
2218 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2219 tcp->u_rval = -1;
2220 u_error = -or1k_regs.gpr[11];
2221 }
2222 else {
2223 tcp->u_rval = or1k_regs.gpr[11];
2224 }
Chris Zankel8f636ed2013-03-25 10:22:07 -07002225#elif defined(XTENSA)
2226 if (check_errno && is_negated_errno(xtensa_a2)) {
2227 tcp->u_rval = -1;
2228 u_error = -xtensa_a2;
2229 }
2230 else {
2231 tcp->u_rval = xtensa_a2;
2232 }
Vineet Gupta7daacbb2013-08-16 12:47:06 +05302233#elif defined(ARC)
2234 if (check_errno && is_negated_errno(arc_regs.scratch.r0)) {
2235 tcp->u_rval = -1;
2236 u_error = -arc_regs.scratch.r0;
2237 }
2238 else {
2239 tcp->u_rval = arc_regs.scratch.r0;
2240 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002241#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002242 tcp->u_error = u_error;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002243}
2244
2245static void
2246dumpio(struct tcb *tcp)
2247{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002248 int (*func)();
2249
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002250 if (syserror(tcp))
2251 return;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +01002252 if ((unsigned long) tcp->u_arg[0] >= num_quals)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002253 return;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002254 func = tcp->s_ent->sys_func;
2255 if (func == printargs)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002256 return;
2257 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002258 if (func == sys_read ||
2259 func == sys_pread ||
2260 func == sys_recv ||
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002261 func == sys_recvfrom) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002262 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002263 return;
2264 } else if (func == sys_readv) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002265 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002266 return;
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002267#if HAVE_SENDMSG
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002268 } else if (func == sys_recvmsg) {
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002269 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002270 return;
2271 } else if (func == sys_recvmmsg) {
Masatake YAMATOa807dce2014-11-07 01:23:26 +09002272 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002273 return;
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002274#endif
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002275 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002276 }
2277 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002278 if (func == sys_write ||
2279 func == sys_pwrite ||
2280 func == sys_send ||
2281 func == sys_sendto)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002282 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002283 else if (func == sys_writev)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002284 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002285#if HAVE_SENDMSG
2286 else if (func == sys_sendmsg)
2287 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
Masatake YAMATOa807dce2014-11-07 01:23:26 +09002288 else if (func == sys_sendmmsg)
2289 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002290#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002291 }
2292}
2293
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002294static int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002295trace_syscall_exiting(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002296{
2297 int sys_res;
2298 struct timeval tv;
Denys Vlasenko1a5b5a72011-08-25 00:29:56 +02002299 int res;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002300 long u_error;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002301
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002302 /* Measure the exit time as early as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002303 if (Tflag || cflag)
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002304 gettimeofday(&tv, NULL);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002305
Masatake YAMATOed69fc22014-04-16 15:33:35 +09002306#ifdef USE_LIBUNWIND
2307 if (stack_trace_enabled) {
2308 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
2309 unwind_cache_invalidate(tcp);
2310 }
2311#endif
2312
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00002313#if SUPPORTED_PERSONALITIES > 1
2314 update_personality(tcp, tcp->currpers);
2315#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002316 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002317 if (res == 1) {
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002318 get_error(tcp); /* never fails */
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002319 if (filtered(tcp) || hide_log_until_execve)
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002320 goto ret;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002321 }
2322
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002323 if (cflag) {
Dmitry V. Levinac5133d2014-05-29 18:10:00 +00002324 count_syscall(tcp, &tv);
Denys Vlasenko7b609d52011-06-22 14:32:43 +02002325 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002326 goto ret;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002327 }
2328 }
2329
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002330 /* If not in -ff mode, and printing_tcp != tcp,
2331 * then the log currently does not end with output
2332 * of _our syscall entry_, but with something else.
2333 * We need to say which syscall's return is this.
2334 *
2335 * Forced reprinting via TCB_REPRINT is used only by
2336 * "strace -ff -oLOG test/threaded_execve" corner case.
2337 * It's the only case when -ff mode needs reprinting.
2338 */
2339 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2340 tcp->flags &= ~TCB_REPRINT;
2341 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002342 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002343 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002344 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002345 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002346 }
2347 printing_tcp = tcp;
2348
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002349 if (res != 1) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002350 /* There was error in one of prior ptrace ops */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002351 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002352 tabto();
Denys Vlasenko000b6012012-01-28 01:25:03 +01002353 tprints("= ? <unavailable>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002354 line_ended();
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002355 tcp->flags &= ~TCB_INSYSCALL;
2356 return res;
2357 }
2358
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002359 sys_res = 0;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002360 if (tcp->qual_flg & QUAL_RAW) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002361 /* sys_res = printargs(tcp); - but it's nop on sysexit */
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002362 } else {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002363 /* FIXME: not_failing_only (IOW, option -z) is broken:
2364 * failure of syscall is known only after syscall return.
2365 * Thus we end up with something like this on, say, ENOENT:
2366 * open("doesnt_exist", O_RDONLY <unfinished ...>
2367 * {next syscall decode}
2368 * whereas the intended result is that open(...) line
2369 * is not shown at all.
2370 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002371 if (not_failing_only && tcp->u_error)
Denys Vlasenko3b738812011-08-22 02:06:35 +02002372 goto ret; /* ignore failed syscalls */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002373 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002374 }
2375
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002376 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002377 tabto();
Denys Vlasenko3b738812011-08-22 02:06:35 +02002378 u_error = tcp->u_error;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002379 if (tcp->qual_flg & QUAL_RAW) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002380 if (u_error)
2381 tprintf("= -1 (errno %ld)", u_error);
2382 else
2383 tprintf("= %#lx", tcp->u_rval);
2384 }
2385 else if (!(sys_res & RVAL_NONE) && u_error) {
2386 switch (u_error) {
Denys Vlasenkofe585652012-01-12 11:26:34 +01002387 /* Blocked signals do not interrupt any syscalls.
2388 * In this case syscalls don't return ERESTARTfoo codes.
2389 *
2390 * Deadly signals set to SIG_DFL interrupt syscalls
2391 * and kill the process regardless of which of the codes below
2392 * is returned by the interrupted syscall.
2393 * In some cases, kernel forces a kernel-generated deadly
2394 * signal to be unblocked and set to SIG_DFL (and thus cause
2395 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2396 * or SIGILL. (The alternative is to leave process spinning
2397 * forever on the faulty instruction - not useful).
2398 *
2399 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2400 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2401 * but kernel will always restart them.
2402 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002403 case ERESTARTSYS:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002404 /* Most common type of signal-interrupted syscall exit code.
2405 * The system call will be restarted with the same arguments
2406 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2407 */
2408 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002409 break;
2410 case ERESTARTNOINTR:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002411 /* Rare. For example, fork() returns this if interrupted.
2412 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2413 */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002414 tprints("= ? ERESTARTNOINTR (To be restarted)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002415 break;
2416 case ERESTARTNOHAND:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002417 /* pause(), rt_sigsuspend() etc use this code.
2418 * SA_RESTART is ignored (assumed not set):
2419 * syscall won't restart (will return EINTR instead)
Denys Vlasenko30c03232013-02-19 16:59:26 +01002420 * even after signal with SA_RESTART set. However,
2421 * after SIG_IGN or SIG_DFL signal it will restart
2422 * (thus the name "restart only if has no handler").
Denys Vlasenkofe585652012-01-12 11:26:34 +01002423 */
Denys Vlasenkoaba62922013-03-05 16:56:35 +01002424 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002425 break;
2426 case ERESTART_RESTARTBLOCK:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002427 /* Syscalls like nanosleep(), poll() which can't be
2428 * restarted with their original arguments use this
2429 * code. Kernel will execute restart_syscall() instead,
2430 * which changes arguments before restarting syscall.
2431 * SA_RESTART is ignored (assumed not set) similarly
2432 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2433 * since restart data is saved in "restart block"
2434 * in task struct, and if signal handler uses a syscall
2435 * which in turn saves another such restart block,
2436 * old data is lost and restart becomes impossible)
2437 */
2438 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002439 break;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002440 default:
Dmitry V. Levinb2f8c772015-02-23 03:10:25 +00002441 if ((unsigned long) u_error < nerrnos
2442 && errnoent[u_error])
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002443 tprintf("= -1 %s (%s)", errnoent[u_error],
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002444 strerror(u_error));
2445 else
Dmitry V. Levinb2f8c772015-02-23 03:10:25 +00002446 tprintf("= -1 ERRNO_%lu (%s)", u_error,
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002447 strerror(u_error));
2448 break;
2449 }
2450 if ((sys_res & RVAL_STR) && tcp->auxstr)
2451 tprintf(" (%s)", tcp->auxstr);
2452 }
2453 else {
2454 if (sys_res & RVAL_NONE)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002455 tprints("= ?");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002456 else {
2457 switch (sys_res & RVAL_MASK) {
2458 case RVAL_HEX:
Dmitry V. Levind93d9f82015-02-28 23:41:11 +00002459#if SUPPORTED_PERSONALITIES > 1
2460 if (current_wordsize < sizeof(long))
2461 tprintf("= %#x",
2462 (unsigned int) tcp->u_rval);
2463 else
2464#endif
2465 tprintf("= %#lx", tcp->u_rval);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002466 break;
2467 case RVAL_OCTAL:
2468 tprintf("= %#lo", tcp->u_rval);
2469 break;
2470 case RVAL_UDECIMAL:
2471 tprintf("= %lu", tcp->u_rval);
2472 break;
2473 case RVAL_DECIMAL:
2474 tprintf("= %ld", tcp->u_rval);
2475 break;
Zubin Mithra64aa1b12014-06-04 08:30:41 +05302476 case RVAL_FD:
2477 if (show_fd_path) {
2478 tprints("= ");
2479 printfd(tcp, tcp->u_rval);
2480 }
2481 else
2482 tprintf("= %ld", tcp->u_rval);
2483 break;
H.J. Ludd0130b2012-04-16 12:16:45 +02002484#if defined(LINUX_MIPSN32) || defined(X32)
2485 /*
2486 case RVAL_LHEX:
2487 tprintf("= %#llx", tcp->u_lrval);
2488 break;
2489 case RVAL_LOCTAL:
2490 tprintf("= %#llo", tcp->u_lrval);
2491 break;
2492 */
2493 case RVAL_LUDECIMAL:
2494 tprintf("= %llu", tcp->u_lrval);
2495 break;
2496 /*
2497 case RVAL_LDECIMAL:
2498 tprintf("= %lld", tcp->u_lrval);
2499 break;
2500 */
2501#endif
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002502 default:
2503 fprintf(stderr,
2504 "invalid rval format\n");
2505 break;
2506 }
2507 }
2508 if ((sys_res & RVAL_STR) && tcp->auxstr)
2509 tprintf(" (%s)", tcp->auxstr);
2510 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002511 if (Tflag) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002512 tv_sub(&tv, &tv, &tcp->etime);
2513 tprintf(" <%ld.%06ld>",
2514 (long) tv.tv_sec, (long) tv.tv_usec);
2515 }
Denys Vlasenko000b6012012-01-28 01:25:03 +01002516 tprints("\n");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002517 dumpio(tcp);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002518 line_ended();
2519
Luca Clementi327064b2013-07-23 00:11:35 -07002520#ifdef USE_LIBUNWIND
2521 if (stack_trace_enabled)
Masatake YAMATO61413922014-04-16 15:33:02 +09002522 unwind_print_stacktrace(tcp);
Luca Clementi327064b2013-07-23 00:11:35 -07002523#endif
2524
Denys Vlasenko3b738812011-08-22 02:06:35 +02002525 ret:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002526 tcp->flags &= ~TCB_INSYSCALL;
2527 return 0;
2528}
2529
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002530int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002531trace_syscall(struct tcb *tcp)
2532{
2533 return exiting(tcp) ?
2534 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2535}