blob: e74881d5e1ce4a38d144739ce529cf3d515b83c0 [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00005 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Linux for s390 port by D.J. Barrow
8 * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00009 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000032 */
33
34#include "defs.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000035#include <sys/user.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000036#include <sys/param.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000037
Wichert Akkerman15dea971999-10-06 13:06:34 +000038#ifdef HAVE_SYS_REG_H
Denys Vlasenko523635f2012-02-25 02:44:25 +010039# include <sys/reg.h>
Wichert Akkermanfaf72222000-02-19 23:59:03 +000040#elif defined(HAVE_LINUX_PTRACE_H)
Denys Vlasenko523635f2012-02-25 02:44:25 +010041# undef PTRACE_SYSCALL
Roland McGrathce9f0742004-03-01 21:29:22 +000042# ifdef HAVE_STRUCT_IA64_FPREG
43# define ia64_fpreg XXX_ia64_fpreg
44# endif
45# ifdef HAVE_STRUCT_PT_ALL_USER_REGS
46# define pt_all_user_regs XXX_pt_all_user_regs
47# endif
Ali Polatel0b4060f2013-09-24 20:04:32 +030048# ifdef HAVE_STRUCT_PTRACE_PEEKSIGINFO_ARGS
49# define ptrace_peeksiginfo_args XXX_ptrace_peeksiginfo_args
50# endif
Denys Vlasenko523635f2012-02-25 02:44:25 +010051# include <linux/ptrace.h>
Ali Polatel0b4060f2013-09-24 20:04:32 +030052# undef ptrace_peeksiginfo_args
Roland McGrathce9f0742004-03-01 21:29:22 +000053# undef ia64_fpreg
54# undef pt_all_user_regs
Wichert Akkerman15dea971999-10-06 13:06:34 +000055#endif
56
Denys Vlasenko84703742012-02-25 02:38:52 +010057#if defined(SPARC64)
Roland McGrath6d1a65c2004-07-12 07:44:08 +000058# undef PTRACE_GETREGS
59# define PTRACE_GETREGS PTRACE_GETREGS64
60# undef PTRACE_SETREGS
61# define PTRACE_SETREGS PTRACE_SETREGS64
Denys Vlasenko84703742012-02-25 02:38:52 +010062#endif
Roland McGrath6d1a65c2004-07-12 07:44:08 +000063
Denys Vlasenko84703742012-02-25 02:38:52 +010064#if defined(IA64)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000065# include <asm/ptrace_offsets.h>
66# include <asm/rse.h>
67#endif
68
Dmitry V. Levin32c049a2013-03-18 00:59:27 +000069/* for struct iovec */
70#include <sys/uio.h>
71/* for NT_PRSTATUS */
72#ifdef HAVE_ELF_H
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010073# include <elf.h>
74#endif
75
Steve McIntyred8d3bd32012-10-24 17:58:16 +010076#if defined(AARCH64)
77# include <asm/ptrace.h>
James Hogan5f999a82013-02-22 14:44:10 +000078#endif
79
Chris Zankel8f636ed2013-03-25 10:22:07 -070080#if defined(XTENSA)
81# include <asm/ptrace.h>
82#endif
83
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000084#ifndef NSIG
Denys Vlasenko523635f2012-02-25 02:44:25 +010085# warning: NSIG is not defined, using 32
86# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000087#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000088
89#include "syscall.h"
90
91/* Define these shorthand notations to simplify the syscallent files. */
Roland McGrath2fe7b132005-07-05 03:25:35 +000092#define TD TRACE_DESC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000093#define TF TRACE_FILE
94#define TI TRACE_IPC
95#define TN TRACE_NETWORK
96#define TP TRACE_PROCESS
97#define TS TRACE_SIGNAL
Namhyung Kim96792962012-10-24 11:41:57 +090098#define TM TRACE_MEMORY
Dmitry V. Levin50a218d2011-01-18 17:36:20 +000099#define NF SYSCALL_NEVER_FAILS
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200100#define MA MAX_ARGS
Masatake YAMATO1d78d222014-04-16 15:33:11 +0900101#define SI STACKTRACE_INVALIDATE_CACHE
102#define SE STACKTRACE_CAPTURE_ON_ENTER
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000103
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100104const struct_sysent sysent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000105#include "syscallent.h"
106};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000107
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100108#if SUPPORTED_PERSONALITIES > 1
109static const struct_sysent sysent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100110# include "syscallent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000111};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200112#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000113
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100114#if SUPPORTED_PERSONALITIES > 2
115static const struct_sysent sysent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100116# include "syscallent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000117};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200118#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000119
120/* Now undef them since short defines cause wicked namespace pollution. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000121#undef TD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000122#undef TF
123#undef TI
124#undef TN
125#undef TP
126#undef TS
Namhyung Kim96792962012-10-24 11:41:57 +0900127#undef TM
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000128#undef NF
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200129#undef MA
Masatake YAMATO1d78d222014-04-16 15:33:11 +0900130#undef SI
131#undef SE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000132
Denys Vlasenko39fca622011-08-20 02:12:33 +0200133/*
134 * `ioctlent.h' may be generated from `ioctlent.raw' by the auxiliary
135 * program `ioctlsort', such that the list is sorted by the `code' field.
136 * This has the side-effect of resolving the _IO.. macros into
137 * plain integers, eliminating the need to include here everything
138 * in "/usr/include".
139 */
140
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100141const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000142#include "errnoent.h"
143};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100144const char *const signalent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200145#include "signalent.h"
146};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100147const struct_ioctlent ioctlent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200148#include "ioctlent.h"
149};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000150
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100151#if SUPPORTED_PERSONALITIES > 1
Roland McGrathee36ce12004-09-04 03:53:10 +0000152static const char *const errnoent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100153# include "errnoent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000154};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200155static const char *const signalent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100156# include "signalent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200157};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100158static const struct_ioctlent ioctlent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100159# include "ioctlent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200160};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200161#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000162
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100163#if SUPPORTED_PERSONALITIES > 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000164static const char *const errnoent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100165# include "errnoent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000166};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200167static const char *const signalent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100168# include "signalent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200169};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100170static const struct_ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100171# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200172};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200173#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000174
Dmitry V. Levine6f55242013-02-26 22:02:30 +0000175enum {
176 nsyscalls0 = ARRAY_SIZE(sysent0)
177#if SUPPORTED_PERSONALITIES > 1
178 , nsyscalls1 = ARRAY_SIZE(sysent1)
179# if SUPPORTED_PERSONALITIES > 2
180 , nsyscalls2 = ARRAY_SIZE(sysent2)
181# endif
182#endif
183};
184
185enum {
186 nerrnos0 = ARRAY_SIZE(errnoent0)
187#if SUPPORTED_PERSONALITIES > 1
188 , nerrnos1 = ARRAY_SIZE(errnoent1)
189# if SUPPORTED_PERSONALITIES > 2
190 , nerrnos2 = ARRAY_SIZE(errnoent2)
191# endif
192#endif
193};
194
195enum {
196 nsignals0 = ARRAY_SIZE(signalent0)
197#if SUPPORTED_PERSONALITIES > 1
198 , nsignals1 = ARRAY_SIZE(signalent1)
199# if SUPPORTED_PERSONALITIES > 2
200 , nsignals2 = ARRAY_SIZE(signalent2)
201# endif
202#endif
203};
204
205enum {
206 nioctlents0 = ARRAY_SIZE(ioctlent0)
207#if SUPPORTED_PERSONALITIES > 1
208 , nioctlents1 = ARRAY_SIZE(ioctlent1)
209# if SUPPORTED_PERSONALITIES > 2
210 , nioctlents2 = ARRAY_SIZE(ioctlent2)
211# endif
212#endif
213};
214
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100215#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100216const struct_sysent *sysent = sysent0;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100217const char *const *errnoent = errnoent0;
218const char *const *signalent = signalent0;
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100219const struct_ioctlent *ioctlent = ioctlent0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100220#endif
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100221unsigned nsyscalls = nsyscalls0;
222unsigned nerrnos = nerrnos0;
223unsigned nsignals = nsignals0;
224unsigned nioctlents = nioctlents0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100225
226unsigned num_quals;
227qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
228
229static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
230 nsyscalls0,
231#if SUPPORTED_PERSONALITIES > 1
232 nsyscalls1,
233#endif
234#if SUPPORTED_PERSONALITIES > 2
235 nsyscalls2,
236#endif
237};
238static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
239 sysent0,
240#if SUPPORTED_PERSONALITIES > 1
241 sysent1,
242#endif
243#if SUPPORTED_PERSONALITIES > 2
244 sysent2,
245#endif
246};
247
248enum {
249 MAX_NSYSCALLS1 = (nsyscalls0
250#if SUPPORTED_PERSONALITIES > 1
251 > nsyscalls1 ? nsyscalls0 : nsyscalls1
252#endif
253 ),
254 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
255#if SUPPORTED_PERSONALITIES > 2
256 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
257#endif
258 ),
259 MAX_NSYSCALLS = MAX_NSYSCALLS2,
260 /* We are ready for arches with up to 255 signals,
261 * even though the largest known signo is on MIPS and it is 128.
262 * The number of existing syscalls on all arches is
263 * larger that 255 anyway, so it is just a pedantic matter.
264 */
265 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
266};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000267
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100268#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100269unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000270
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100271# ifndef current_wordsize
272unsigned current_wordsize;
273static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000274 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000275 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100276# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000277 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100278# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200279};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100280# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000281
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200282void
Dmitry V. Levin3abe8b22006-12-20 22:37:21 +0000283set_personality(int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000284{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100285 nsyscalls = nsyscall_vec[personality];
286 sysent = sysent_vec[personality];
287
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000288 switch (personality) {
289 case 0:
290 errnoent = errnoent0;
291 nerrnos = nerrnos0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000292 ioctlent = ioctlent0;
293 nioctlents = nioctlents0;
294 signalent = signalent0;
295 nsignals = nsignals0;
296 break;
297
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000298 case 1:
299 errnoent = errnoent1;
300 nerrnos = nerrnos1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000301 ioctlent = ioctlent1;
302 nioctlents = nioctlents1;
303 signalent = signalent1;
304 nsignals = nsignals1;
305 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000306
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100307# if SUPPORTED_PERSONALITIES > 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000308 case 2:
309 errnoent = errnoent2;
310 nerrnos = nerrnos2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000311 ioctlent = ioctlent2;
312 nioctlents = nioctlents2;
313 signalent = signalent2;
314 nsignals = nsignals2;
315 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100316# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000317 }
318
319 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100320# ifndef current_wordsize
321 current_wordsize = personality_wordsize[personality];
322# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000323}
324
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000325static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000326update_personality(struct tcb *tcp, unsigned int personality)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000327{
328 if (personality == current_personality)
329 return;
330 set_personality(personality);
331
332 if (personality == tcp->currpers)
333 return;
334 tcp->currpers = personality;
335
H.J. Lu35be5812012-04-16 13:00:01 +0200336# if defined(POWERPC64)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000337 if (!qflag) {
338 static const char *const names[] = {"64 bit", "32 bit"};
339 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
340 tcp->pid, names[personality]);
341 }
H.J. Lu35be5812012-04-16 13:00:01 +0200342# elif defined(X86_64)
343 if (!qflag) {
344 static const char *const names[] = {"64 bit", "32 bit", "x32"};
345 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
346 tcp->pid, names[personality]);
347 }
H.J. Lu085e4282012-04-17 11:05:04 -0700348# elif defined(X32)
349 if (!qflag) {
350 static const char *const names[] = {"x32", "32 bit"};
351 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
352 tcp->pid, names[personality]);
353 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000354# elif defined(AARCH64)
355 if (!qflag) {
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100356 static const char *const names[] = {"32-bit", "AArch64"};
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000357 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
358 tcp->pid, names[personality]);
359 }
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100360# elif defined(TILE)
361 if (!qflag) {
362 static const char *const names[] = {"64-bit", "32-bit"};
363 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
364 tcp->pid, names[personality]);
365 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100366# endif
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000367}
368#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000369
Denys Vlasenkoc1540fe2013-02-21 16:17:08 +0100370static int qual_syscall(), qual_signal(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000371
Roland McGrathe10e62a2004-09-04 04:20:43 +0000372static const struct qual_options {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000373 unsigned int bitflag;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000374 const char *option_name;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000375 int (*qualify)(const char *, int, int);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000376 const char *argument_name;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000377} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000378 { QUAL_TRACE, "trace", qual_syscall, "system call" },
379 { QUAL_TRACE, "t", qual_syscall, "system call" },
380 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
381 { QUAL_ABBREV, "a", qual_syscall, "system call" },
382 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
383 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
384 { QUAL_RAW, "raw", qual_syscall, "system call" },
385 { QUAL_RAW, "x", qual_syscall, "system call" },
386 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
387 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
388 { QUAL_SIGNAL, "s", qual_signal, "signal" },
Roland McGrath9797ceb2002-12-30 10:23:00 +0000389 { QUAL_READ, "read", qual_desc, "descriptor" },
390 { QUAL_READ, "reads", qual_desc, "descriptor" },
391 { QUAL_READ, "r", qual_desc, "descriptor" },
392 { QUAL_WRITE, "write", qual_desc, "descriptor" },
393 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
394 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000395 { 0, NULL, NULL, NULL },
396};
397
Roland McGrath9797ceb2002-12-30 10:23:00 +0000398static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000399reallocate_qual(const unsigned int n)
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100400{
401 unsigned p;
402 qualbits_t *qp;
403 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
404 qp = qual_vec[p] = realloc(qual_vec[p], n * sizeof(qualbits_t));
405 if (!qp)
406 die_out_of_memory();
407 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
408 }
409 num_quals = n;
410}
411
412static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000413qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000414{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000415 int p;
Roland McGrath138c6a32006-01-12 09:50:49 +0000416
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100417 if (num_quals <= n)
418 reallocate_qual(n + 1);
Roland McGrath138c6a32006-01-12 09:50:49 +0000419
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100420 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
421 if (pers == p || pers < 0) {
422 if (not)
423 qual_vec[p][n] &= ~bitflag;
424 else
425 qual_vec[p][n] |= bitflag;
426 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000427 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000428}
429
430static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000431qual_syscall(const char *s, const unsigned int bitflag, const int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000432{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000433 int p;
434 unsigned int i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000435 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000436
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100437 if (*s >= '0' && *s <= '9') {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100438 i = string_to_uint(s);
Denys Vlasenkob43dacd2013-02-23 18:19:28 +0100439 if (i >= MAX_NSYSCALLS)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000440 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000441 qualify_one(i, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000442 return 0;
Roland McGrath48a035f2006-01-12 09:45:56 +0000443 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000444
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100445 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
446 for (i = 0; i < nsyscall_vec[p]; i++) {
447 if (sysent_vec[p][i].sys_name
448 && strcmp(s, sysent_vec[p][i].sys_name) == 0
449 ) {
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000450 qualify_one(i, bitflag, not, p);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100451 rc = 0;
452 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000453 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100454 }
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000455
Roland McGrathfe6b3522005-02-02 04:40:11 +0000456 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000457}
458
459static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000460qual_signal(const char *s, const unsigned int bitflag, const int not)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000461{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000462 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000463
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100464 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000465 int signo = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100466 if (signo < 0 || signo > 255)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000467 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000468 qualify_one(signo, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000469 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000470 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000471 if (strncasecmp(s, "SIG", 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000472 s += 3;
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100473 for (i = 0; i <= NSIG; i++) {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000474 if (strcasecmp(s, signame(i) + 3) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000475 qualify_one(i, bitflag, not, -1);
Roland McGrath76421df2005-02-02 03:51:18 +0000476 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000477 }
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100478 }
Roland McGrath76421df2005-02-02 03:51:18 +0000479 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000480}
481
482static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000483qual_desc(const char *s, const unsigned int bitflag, const int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000484{
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100485 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000486 int desc = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100487 if (desc < 0 || desc > 0x7fff) /* paranoia */
Roland McGrathfe6b3522005-02-02 04:40:11 +0000488 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000489 qualify_one(desc, bitflag, not, -1);
Roland McGrath2b619022003-04-10 18:58:20 +0000490 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000491 }
492 return -1;
493}
494
495static int
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000496lookup_class(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000497{
498 if (strcmp(s, "file") == 0)
499 return TRACE_FILE;
500 if (strcmp(s, "ipc") == 0)
501 return TRACE_IPC;
502 if (strcmp(s, "network") == 0)
503 return TRACE_NETWORK;
504 if (strcmp(s, "process") == 0)
505 return TRACE_PROCESS;
506 if (strcmp(s, "signal") == 0)
507 return TRACE_SIGNAL;
Roland McGrath2fe7b132005-07-05 03:25:35 +0000508 if (strcmp(s, "desc") == 0)
509 return TRACE_DESC;
Namhyung Kim96792962012-10-24 11:41:57 +0900510 if (strcmp(s, "memory") == 0)
511 return TRACE_MEMORY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000512 return -1;
513}
514
515void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000516qualify(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000517{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000518 const struct qual_options *opt;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000519 char *copy;
520 const char *p;
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000521 int not;
522 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000523
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100524 if (num_quals == 0)
525 reallocate_qual(MIN_QUALS);
526
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000527 opt = &qual_options[0];
528 for (i = 0; (p = qual_options[i].option_name); i++) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000529 unsigned int len = strlen(p);
530 if (strncmp(s, p, len) == 0 && s[len] == '=') {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000531 opt = &qual_options[i];
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000532 s += len + 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000533 break;
534 }
535 }
536 not = 0;
537 if (*s == '!') {
538 not = 1;
539 s++;
540 }
541 if (strcmp(s, "none") == 0) {
542 not = 1 - not;
543 s = "all";
544 }
545 if (strcmp(s, "all") == 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100546 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000547 qualify_one(i, opt->bitflag, not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000548 }
549 return;
550 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100551 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000552 qualify_one(i, opt->bitflag, !not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000553 }
Denys Vlasenko5d645812011-08-20 12:48:18 +0200554 copy = strdup(s);
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200555 if (!copy)
556 die_out_of_memory();
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000557 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000558 int n;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000559 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100560 unsigned pers;
561 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
562 for (i = 0; i < nsyscall_vec[pers]; i++)
563 if (sysent_vec[pers][i].sys_flags & n)
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000564 qualify_one(i, opt->bitflag, not, pers);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100565 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000566 continue;
567 }
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000568 if (opt->qualify(p, opt->bitflag, not)) {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100569 error_msg_and_die("invalid %s '%s'",
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000570 opt->argument_name, p);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000571 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000572 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000573 free(copy);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000574 return;
575}
576
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000577#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000578static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000579decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000580{
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000581 unsigned long addr;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100582 unsigned int i, n, size;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000583
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000584 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
585 return;
586
587 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100588 tcp->qual_flg = qual_flags[tcp->scno];
589 tcp->s_ent = &sysent[tcp->scno];
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000590 addr = tcp->u_arg[1];
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100591 size = current_wordsize;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100592 n = tcp->s_ent->nargs;
593 for (i = 0; i < n; ++i) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000594 if (size == sizeof(int)) {
595 unsigned int arg;
596 if (umove(tcp, addr, &arg) < 0)
597 arg = 0;
598 tcp->u_arg[i] = arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000599 }
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000600 else {
601 unsigned long arg;
602 if (umove(tcp, addr, &arg) < 0)
603 arg = 0;
604 tcp->u_arg[i] = arg;
605 }
606 addr += size;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000607 }
608}
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000609#endif
Mike Frysinger3362e892012-03-15 01:09:19 -0400610
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000611#ifdef SYS_ipc_subcall
612static void
613decode_ipc_subcall(struct tcb *tcp)
614{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100615 unsigned int i, n;
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000616
617 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
618 return;
619
620 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100621 tcp->qual_flg = qual_flags[tcp->scno];
622 tcp->s_ent = &sysent[tcp->scno];
623 n = tcp->s_ent->nargs;
624 for (i = 0; i < n; i++)
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000625 tcp->u_arg[i] = tcp->u_arg[i + 1];
626}
627#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000628
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200629int
630printargs(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000631{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200632 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 Vlasenkoa6146922011-08-24 18:07:22 +0200636 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
637 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000638 return 0;
639}
640
Denys Vlasenko72879c62012-02-27 14:18:02 +0100641int
642printargs_lu(struct tcb *tcp)
643{
644 if (entering(tcp)) {
645 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100646 int n = tcp->s_ent->nargs;
647 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100648 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
649 }
650 return 0;
651}
652
653int
654printargs_ld(struct tcb *tcp)
655{
656 if (entering(tcp)) {
657 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100658 int n = tcp->s_ent->nargs;
659 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100660 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
661 }
662 return 0;
663}
664
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100665#if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200666long
667getrval2(struct tcb *tcp)
668{
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100669 long val;
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200670
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100671# if defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100672 val = sparc_regs.u_regs[U_REG_O1];
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100673# elif defined(SH)
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200674 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200675 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100676# elif defined(IA64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200677 if (upeek(tcp->pid, PT_R9, &val) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200678 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100679# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200680
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200681 return val;
682}
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100683#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200684
Denys Vlasenko523635f2012-02-25 02:44:25 +0100685#if defined(I386)
Denys Vlasenkob51f3642013-07-16 12:06:25 +0200686static struct user_regs_struct i386_regs;
Denys Vlasenkoe600ac62013-07-18 17:40:45 +0200687/* Cast suppresses signedness warning (.esp is long, not unsigned long) */
688uint32_t *const i386_esp_ptr = (uint32_t*)&i386_regs.esp;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000689# define ARCH_REGS_FOR_GETREGSET i386_regs
H.J. Lu35be5812012-04-16 13:00:01 +0200690#elif defined(X86_64) || defined(X32)
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100691/*
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100692 * On i386, pt_regs and user_regs_struct are the same,
693 * but on 64 bit x86, user_regs_struct has six more fields:
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100694 * fs_base, gs_base, ds, es, fs, gs.
695 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
696 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100697struct i386_user_regs_struct {
698 uint32_t ebx;
699 uint32_t ecx;
700 uint32_t edx;
701 uint32_t esi;
702 uint32_t edi;
703 uint32_t ebp;
704 uint32_t eax;
705 uint32_t xds;
706 uint32_t xes;
707 uint32_t xfs;
708 uint32_t xgs;
709 uint32_t orig_eax;
710 uint32_t eip;
711 uint32_t xcs;
712 uint32_t eflags;
713 uint32_t esp;
714 uint32_t xss;
715};
716static union {
717 struct user_regs_struct x86_64_r;
718 struct i386_user_regs_struct i386_r;
719} x86_regs_union;
720# define x86_64_regs x86_regs_union.x86_64_r
721# define i386_regs x86_regs_union.i386_r
Denys Vlasenkob51f3642013-07-16 12:06:25 +0200722uint32_t *const i386_esp_ptr = &i386_regs.esp;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100723static struct iovec x86_io = {
724 .iov_base = &x86_regs_union
725};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100726#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +0200727bool ia64_ia32mode = 0; /* not static */
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100728static long ia64_r8, ia64_r10;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100729#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +0200730struct pt_regs ppc_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100731#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100732static long m68k_d0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100733#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100734static long bfin_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100735#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100736struct pt_regs arm_regs; /* not static */
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000737# define ARCH_REGS_FOR_GETREGSET arm_regs
Steve McIntyred8d3bd32012-10-24 17:58:16 +0100738#elif defined(AARCH64)
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};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100748#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100749static long alpha_r0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100750static long alpha_a3;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100751#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100752static struct pt_regs avr32_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100753#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100754struct pt_regs sparc_regs; /* not static */
Denys Vlasenko523635f2012-02-25 02:44:25 +0100755#elif defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100756static long long mips_a3;
757static long long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100758#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100759static long mips_a3;
760static long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100761#elif defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +0200762static long s390_gpr2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100763#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100764static long hppa_r28;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100765#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100766static long sh_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100767#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100768static long sh64_r9;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100769#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100770static long cris_r10;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100771#elif defined(TILE)
772struct pt_regs tile_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100773#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100774static long microblaze_r3;
Christian Svensson492f81f2013-02-14 13:26:27 +0100775#elif defined(OR1K)
776static struct user_regs_struct or1k_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000777# define ARCH_REGS_FOR_GETREGSET or1k_regs
James Hogan5f999a82013-02-22 14:44:10 +0000778#elif defined(METAG)
779static struct user_gp_regs metag_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000780# define ARCH_REGS_FOR_GETREGSET metag_regs
Chris Zankel8f636ed2013-03-25 10:22:07 -0700781#elif defined(XTENSA)
782static long xtensa_a2;
Vineet Gupta7daacbb2013-08-16 12:47:06 +0530783# elif defined(ARC)
784static struct user_regs_struct arc_regs;
785# define ARCH_REGS_FOR_GETREGSET arc_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +0100786#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000787
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100788void
Denys Vlasenko5a2483b2013-07-01 12:49:14 +0200789print_pc(struct tcb *tcp)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100790{
791#define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
792 sizeof(long) == 8 ? "[????????????????] " : \
793 NULL /* crash */)
794 if (get_regs_error) {
795 PRINTBADPC;
796 return;
797 }
798#if defined(I386)
799 tprintf("[%08lx] ", i386_regs.eip);
800#elif defined(S390) || defined(S390X)
801 long psw;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200802 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100803 PRINTBADPC;
804 return;
805 }
806# ifdef S390
807 tprintf("[%08lx] ", psw);
808# elif S390X
Dmitry V. Levinddba73e2013-02-05 19:01:58 +0000809 tprintf("[%016lx] ", psw);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100810# endif
811#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100812 if (x86_io.iov_len == sizeof(i386_regs)) {
813 tprintf("[%08x] ", (unsigned) i386_regs.eip);
814 } else {
815# if defined(X86_64)
816 tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
817# elif defined(X32)
818 /* Note: this truncates 64-bit rip to 32 bits */
819 tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip);
820# endif
821 }
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100822#elif defined(IA64)
823 long ip;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200824 if (upeek(tcp->pid, PT_B0, &ip) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100825 PRINTBADPC;
826 return;
827 }
828 tprintf("[%08lx] ", ip);
829#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +0200830 long pc = ppc_regs.nip;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100831# ifdef POWERPC64
832 tprintf("[%016lx] ", pc);
833# else
834 tprintf("[%08lx] ", pc);
835# endif
836#elif defined(M68K)
837 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200838 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100839 tprints("[????????] ");
840 return;
841 }
842 tprintf("[%08lx] ", pc);
843#elif defined(ALPHA)
844 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200845 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100846 tprints("[????????????????] ");
847 return;
848 }
849 tprintf("[%08lx] ", pc);
850#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100851 tprintf("[%08lx] ", sparc_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100852#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100853 tprintf("[%08lx] ", sparc_regs.tpc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100854#elif defined(HPPA)
855 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200856 if (upeek(tcp->pid, PT_IAOQ0, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100857 tprints("[????????] ");
858 return;
859 }
860 tprintf("[%08lx] ", pc);
861#elif defined(MIPS)
862 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200863 if (upeek(tcp->pid, REG_EPC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100864 tprints("[????????] ");
865 return;
866 }
867 tprintf("[%08lx] ", pc);
868#elif defined(SH)
869 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200870 if (upeek(tcp->pid, 4*REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100871 tprints("[????????] ");
872 return;
873 }
874 tprintf("[%08lx] ", pc);
875#elif defined(SH64)
876 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200877 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100878 tprints("[????????????????] ");
879 return;
880 }
881 tprintf("[%08lx] ", pc);
882#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100883 tprintf("[%08lx] ", arm_regs.ARM_pc);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100884#elif defined(AARCH64)
885 /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100886#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100887 tprintf("[%08lx] ", avr32_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100888#elif defined(BFIN)
889 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200890 if (upeek(tcp->pid, PT_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100891 PRINTBADPC;
892 return;
893 }
894 tprintf("[%08lx] ", pc);
895#elif defined(CRISV10)
896 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200897 if (upeek(tcp->pid, 4*PT_IRP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100898 PRINTBADPC;
899 return;
900 }
901 tprintf("[%08lx] ", pc);
902#elif defined(CRISV32)
903 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200904 if (upeek(tcp->pid, 4*PT_ERP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100905 PRINTBADPC;
906 return;
907 }
908 tprintf("[%08lx] ", pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100909#elif defined(TILE)
910# ifdef _LP64
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500911 tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100912# else
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500913 tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100914# endif
Christian Svensson492f81f2013-02-14 13:26:27 +0100915#elif defined(OR1K)
916 tprintf("[%08lx] ", or1k_regs.pc);
James Hogan5f999a82013-02-22 14:44:10 +0000917#elif defined(METAG)
918 tprintf("[%08lx] ", metag_regs.pc);
Chris Zankel8f636ed2013-03-25 10:22:07 -0700919#elif defined(XTENSA)
920 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200921 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Chris Zankel8f636ed2013-03-25 10:22:07 -0700922 PRINTBADPC;
923 return;
924 }
925 tprintf("[%08lx] ", pc);
Vineet Gupta7daacbb2013-08-16 12:47:06 +0530926#elif defined(ARC)
927 tprintf("[%08lx] ", arc_regs.efa);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100928#endif /* architecture */
929}
930
Denys Vlasenko7270de52013-02-21 15:46:34 +0100931/* Shuffle syscall numbers so that we don't have huge gaps in syscall table.
932 * The shuffling should be reversible: shuffle_scno(shuffle_scno(n)) == n.
933 */
Elliott Hughes7a28f7f2014-03-03 23:45:26 +0000934#if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
Denys Vlasenko7270de52013-02-21 15:46:34 +0100935static long
936shuffle_scno(unsigned long scno)
937{
938 if (scno <= ARM_LAST_ORDINARY_SYSCALL)
939 return scno;
940
941 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
942 if (scno == 0x000ffff0)
943 return ARM_LAST_ORDINARY_SYSCALL+1;
944 if (scno == ARM_LAST_ORDINARY_SYSCALL+1)
945 return 0x000ffff0;
946
947 /* Is it ARM specific syscall?
948 * Swap with [LAST_ORDINARY+2, LAST_ORDINARY+2 + LAST_SPECIAL] range.
949 */
950 if (scno >= 0x000f0000
951 && scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL
952 ) {
953 return scno - 0x000f0000 + (ARM_LAST_ORDINARY_SYSCALL+2);
954 }
955 if (/* scno >= ARM_LAST_ORDINARY_SYSCALL+2 - always true */ 1
956 && scno <= (ARM_LAST_ORDINARY_SYSCALL+2) + ARM_LAST_SPECIAL_SYSCALL
957 ) {
958 return scno + 0x000f0000 - (ARM_LAST_ORDINARY_SYSCALL+2);
959 }
960
961 return scno;
962}
963#else
964# define shuffle_scno(scno) ((long)(scno))
965#endif
966
967static char*
968undefined_scno_name(struct tcb *tcp)
969{
970 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
971
972 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
973 return buf;
974}
975
Anton Blanchard14d51a62013-06-26 14:42:37 +1000976#ifdef POWERPC
977/*
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200978 * PTRACE_GETREGS was added to the PowerPC kernel in v2.6.23,
979 * we provide a slow fallback for old kernels.
Anton Blanchard14d51a62013-06-26 14:42:37 +1000980 */
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200981static int powerpc_getregs_old(pid_t pid)
Anton Blanchard14d51a62013-06-26 14:42:37 +1000982{
983 int i;
984 long r;
985
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200986 if (iflag) {
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200987 r = upeek(pid, sizeof(long) * PT_NIP, (long *)&ppc_regs.nip);
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200988 if (r)
989 goto out;
990 }
991#ifdef POWERPC64 /* else we never use it */
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200992 r = upeek(pid, sizeof(long) * PT_MSR, (long *)&ppc_regs.msr);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000993 if (r)
994 goto out;
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200995#endif
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200996 r = upeek(pid, sizeof(long) * PT_CCR, (long *)&ppc_regs.ccr);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000997 if (r)
998 goto out;
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200999 r = upeek(pid, sizeof(long) * PT_ORIG_R3, (long *)&ppc_regs.orig_gpr3);
Anton Blanchard14d51a62013-06-26 14:42:37 +10001000 if (r)
1001 goto out;
Anton Blanchard14d51a62013-06-26 14:42:37 +10001002 for (i = 0; i <= 8; i++) {
Anton Blanchard9459dfb2013-07-12 12:24:02 +02001003 r = upeek(pid, sizeof(long) * (PT_R0 + i),
1004 (long *)&ppc_regs.gpr[i]);
Anton Blanchard14d51a62013-06-26 14:42:37 +10001005 if (r)
1006 goto out;
1007 }
Denys Vlasenko7f5a1322013-06-28 14:36:39 +02001008 out:
Anton Blanchard14d51a62013-06-26 14:42:37 +10001009 return r;
1010}
1011#endif
1012
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001013#ifndef get_regs
1014long get_regs_error;
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001015
1016#if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001017static void get_regset(pid_t pid)
1018{
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001019/* constant iovec */
1020# if defined(ARM) \
1021 || defined(I386) \
1022 || defined(METAG) \
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301023 || defined(OR1K) \
1024 || defined(ARC)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001025 static struct iovec io = {
1026 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1027 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001028 };
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001029 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1030
1031/* variable iovec */
1032# elif defined(X86_64) || defined(X32)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001033 /* x86_io.iov_base = &x86_regs_union; - already is */
1034 x86_io.iov_len = sizeof(x86_regs_union);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001035 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &x86_io);
1036# elif defined(AARCH64)
1037 /* aarch64_io.iov_base = &arm_regs_union; - already is */
1038 aarch64_io.iov_len = sizeof(arm_regs_union);
1039 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &aarch64_io);
1040# else
1041# warning both PTRACE_GETREGSET and NT_PRSTATUS are available but not yet used
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001042# endif
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001043}
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001044#endif /* PTRACE_GETREGSET && NT_PRSTATUS */
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. Levinb787b102013-03-18 10:17:14 +00001049/* PTRACE_GETREGSET only */
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301050# if defined(METAG) || defined(OR1K) || defined(X32) || defined(AARCH64) || defined(ARC)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001051 get_regset(pid);
1052
1053/* PTRACE_GETREGS only */
1054# elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001055 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001056# elif defined(TILE)
1057 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &tile_regs);
1058# elif defined(SPARC) || defined(SPARC64)
1059 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001060# elif defined(POWERPC)
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001061 static bool old_kernel = 0;
1062 if (old_kernel)
1063 goto old;
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001064 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &ppc_regs);
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001065 if (get_regs_error && errno == EIO) {
1066 old_kernel = 1;
1067 old:
Anton Blanchard14d51a62013-06-26 14:42:37 +10001068 get_regs_error = powerpc_getregs_old(pid);
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001069 }
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001070
1071/* try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS */
1072# else
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001073# if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
1074 static int getregset_support;
1075
1076 if (getregset_support >= 0) {
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001077 get_regset(pid);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001078 if (getregset_support > 0)
1079 return;
1080 if (get_regs_error >= 0) {
1081 getregset_support = 1;
1082 return;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001083 }
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001084 if (errno == EPERM || errno == ESRCH)
1085 return;
1086 getregset_support = -1;
1087 }
1088# endif /* PTRACE_GETREGSET && NT_PRSTATUS */
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001089# if defined(ARM)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001090 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &arm_regs);
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001091# elif defined(I386)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001092 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &i386_regs);
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001093# elif defined(X86_64)
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001094 /* Use old method, with unreliable heuristical detection of 32-bitness. */
1095 x86_io.iov_len = sizeof(x86_64_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001096 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &x86_64_regs);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001097 if (!get_regs_error && x86_64_regs.cs == 0x23) {
1098 x86_io.iov_len = sizeof(i386_regs);
1099 /*
1100 * The order is important: i386_regs and x86_64_regs
1101 * are overlaid in memory!
1102 */
1103 i386_regs.ebx = x86_64_regs.rbx;
1104 i386_regs.ecx = x86_64_regs.rcx;
1105 i386_regs.edx = x86_64_regs.rdx;
1106 i386_regs.esi = x86_64_regs.rsi;
1107 i386_regs.edi = x86_64_regs.rdi;
1108 i386_regs.ebp = x86_64_regs.rbp;
1109 i386_regs.eax = x86_64_regs.rax;
1110 /* i386_regs.xds = x86_64_regs.ds; unused by strace */
1111 /* i386_regs.xes = x86_64_regs.es; ditto... */
1112 /* i386_regs.xfs = x86_64_regs.fs; */
1113 /* i386_regs.xgs = x86_64_regs.gs; */
1114 i386_regs.orig_eax = x86_64_regs.orig_rax;
1115 i386_regs.eip = x86_64_regs.rip;
1116 /* i386_regs.xcs = x86_64_regs.cs; */
1117 /* i386_regs.eflags = x86_64_regs.eflags; */
1118 i386_regs.esp = x86_64_regs.rsp;
1119 /* i386_regs.xss = x86_64_regs.ss; */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001120 }
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001121# else
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001122# error unhandled architecture
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001123# endif /* ARM || I386 || X86_64 */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001124# endif
1125}
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001126#endif /* !get_regs */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001127
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001128/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001129 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1130 * 1: ok, continue in trace_syscall_entering().
1131 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001132 * ("????" etc) and bail out.
1133 */
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01001134static int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001135get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001136{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001137 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001138
Denys Vlasenko523635f2012-02-25 02:44:25 +01001139#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001140 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001141 return -1;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001142
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001143 if (s390_gpr2 != -ENOSYS) {
Roland McGrath2f924ca2003-06-26 22:23:28 +00001144 /*
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001145 * Since kernel version 2.5.44 the scno gets passed in gpr2.
Roland McGrath2f924ca2003-06-26 22:23:28 +00001146 */
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001147 scno = s390_gpr2;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001148 } else {
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001149 /*
Michal Ludvig882eda82002-11-11 12:50:47 +00001150 * Old style of "passing" the scno via the SVC instruction.
1151 */
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001152 long psw;
Michal Ludvig882eda82002-11-11 12:50:47 +00001153 long opcode, offset_reg, tmp;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001154 void *svc_addr;
Denys Vlasenko7c9ba8b2011-08-19 19:46:32 +02001155 static const int gpr_offset[16] = {
1156 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
1157 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
1158 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
1159 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
1160 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001161
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001162 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0)
Michal Ludvig882eda82002-11-11 12:50:47 +00001163 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001164 errno = 0;
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001165 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +00001166 if (errno) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001167 perror_msg("peektext(psw-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +00001168 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001169 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001170
1171 /*
1172 * We have to check if the SVC got executed directly or via an
1173 * EXECUTE instruction. In case of EXECUTE it is necessary to do
1174 * instruction decoding to derive the system call number.
1175 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1176 * so that this doesn't work if a SVC opcode is part of an EXECUTE
1177 * opcode. Since there is no way to find out the opcode size this
1178 * is the best we can do...
1179 */
Michal Ludvig882eda82002-11-11 12:50:47 +00001180 if ((opcode & 0xff00) == 0x0a00) {
1181 /* SVC opcode */
1182 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +00001183 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001184 else {
1185 /* SVC got executed by EXECUTE instruction */
1186
1187 /*
1188 * Do instruction decoding of EXECUTE. If you really want to
1189 * understand this, read the Principles of Operations.
1190 */
1191 svc_addr = (void *) (opcode & 0xfff);
1192
1193 tmp = 0;
1194 offset_reg = (opcode & 0x000f0000) >> 16;
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
1199 tmp = 0;
1200 offset_reg = (opcode & 0x0000f000) >> 12;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001201 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001202 return -1;
1203 svc_addr += tmp;
1204
Denys Vlasenkofb036672009-01-23 16:30:26 +00001205 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
Michal Ludvig882eda82002-11-11 12:50:47 +00001206 if (errno)
1207 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001208# if defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +00001209 scno >>= 48;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001210# else
Michal Ludvig882eda82002-11-11 12:50:47 +00001211 scno >>= 16;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001212# endif
Michal Ludvig882eda82002-11-11 12:50:47 +00001213 tmp = 0;
1214 offset_reg = (opcode & 0x00f00000) >> 20;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001215 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001216 return -1;
1217
1218 scno = (scno | tmp) & 0xff;
1219 }
1220 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001221#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001222 scno = ppc_regs.gpr[0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001223# ifdef POWERPC64
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001224 unsigned int currpers;
Andreas Schwabd69fa492010-07-12 21:39:57 +02001225
James Yanga78d7772014-02-18 15:32:43 -06001226 /*
1227 * Check for 64/32 bit mode.
1228 * Embedded implementations covered by Book E extension of PPC use
1229 * bit 0 (CM) of 32-bit Machine state register (MSR).
1230 * Other implementations use bit 0 (SF) of 64-bit MSR.
1231 */
1232 currpers = (ppc_regs.msr & 0x8000000080000000) ? 0 : 1;
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001233 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001234# endif
1235#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001236 scno = avr32_regs.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001237#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001238 if (upeek(tcp->pid, PT_ORIG_P0, &scno))
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001239 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001240#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001241 scno = i386_regs.orig_eax;
H.J. Lu35be5812012-04-16 13:00:01 +02001242#elif defined(X86_64) || defined(X32)
1243# ifndef __X32_SYSCALL_BIT
1244# define __X32_SYSCALL_BIT 0x40000000
1245# endif
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001246 unsigned int currpers;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001247# if 1
1248 /* GETREGSET of NT_PRSTATUS tells us regset size,
1249 * which unambiguously detects i386.
1250 *
1251 * Linux kernel distinguishes x86-64 and x32 processes
1252 * solely by looking at __X32_SYSCALL_BIT:
1253 * arch/x86/include/asm/compat.h::is_x32_task():
1254 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1255 * return true;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001256 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001257 if (x86_io.iov_len == sizeof(i386_regs)) {
1258 scno = i386_regs.orig_eax;
1259 currpers = 1;
1260 } else {
1261 scno = x86_64_regs.orig_rax;
1262 currpers = 0;
1263 if (scno & __X32_SYSCALL_BIT) {
1264 scno -= __X32_SYSCALL_BIT;
1265 currpers = 2;
1266 }
1267 }
1268# elif 0
1269 /* cs = 0x33 for long mode (native 64 bit and x32)
1270 * cs = 0x23 for compatibility mode (32 bit)
1271 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1272 */
1273 scno = x86_64_regs.orig_rax;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001274 switch (x86_64_regs.cs) {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001275 case 0x23: currpers = 1; break;
H.J. Lu35be5812012-04-16 13:00:01 +02001276 case 0x33:
1277 if (x86_64_regs.ds == 0x2b) {
1278 currpers = 2;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001279 scno &= ~__X32_SYSCALL_BIT;
H.J. Lu35be5812012-04-16 13:00:01 +02001280 } else
1281 currpers = 0;
1282 break;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001283 default:
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001284 fprintf(stderr, "Unknown value CS=0x%08X while "
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001285 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001286 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001287 currpers = current_personality;
1288 break;
1289 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001290# elif 0
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001291 /* This version analyzes the opcode of a syscall instruction.
1292 * (int 0x80 on i386 vs. syscall on x86-64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001293 * It works, but is too complicated, and strictly speaking, unreliable.
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001294 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001295 unsigned long call, rip = x86_64_regs.rip;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001296 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1297 rip -= 2;
1298 errno = 0;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001299 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001300 if (errno)
1301 fprintf(stderr, "ptrace_peektext failed: %s\n",
1302 strerror(errno));
1303 switch (call & 0xffff) {
1304 /* x86-64: syscall = 0x0f 0x05 */
1305 case 0x050f: currpers = 0; break;
1306 /* i386: int 0x80 = 0xcd 0x80 */
1307 case 0x80cd: currpers = 1; break;
1308 default:
1309 currpers = current_personality;
1310 fprintf(stderr,
1311 "Unknown syscall opcode (0x%04X) while "
1312 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001313 "PID=%d\n", (int)call, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001314 break;
1315 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001316# endif
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001317
H.J. Lu35be5812012-04-16 13:00:01 +02001318# ifdef X32
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001319 /* If we are built for a x32 system, then personality 0 is x32
1320 * (not x86_64), and stracing of x86_64 apps is not supported.
1321 * Stracing of i386 apps is still supported.
H.J. Lu085e4282012-04-17 11:05:04 -07001322 */
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001323 if (currpers == 0) {
1324 fprintf(stderr, "syscall_%lu(...) in unsupported "
1325 "64-bit mode of process PID=%d\n",
1326 scno, tcp->pid);
1327 return 0;
H.J. Lu35be5812012-04-16 13:00:01 +02001328 }
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001329 currpers &= ~2; /* map 2,1 to 0,1 */
H.J. Lu35be5812012-04-16 13:00:01 +02001330# endif
H.J. Lu085e4282012-04-17 11:05:04 -07001331 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001332#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001333# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01001334 long psr;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001335 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001336 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
1337 if (ia64_ia32mode) {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001338 if (upeek(tcp->pid, PT_R1, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001339 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001340 } else {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001341 if (upeek(tcp->pid, PT_R15, &scno) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001342 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001343 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001344#elif defined(AARCH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001345 switch (aarch64_io.iov_len) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001346 case sizeof(aarch64_regs):
1347 /* We are in 64-bit mode */
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001348 scno = aarch64_regs.regs[8];
1349 update_personality(tcp, 1);
1350 break;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001351 case sizeof(arm_regs):
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001352 /* We are in 32-bit mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001353 /* Note: we don't support OABI, unlike 32-bit ARM build */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001354 scno = arm_regs.ARM_r7;
Elliott Hughes7a28f7f2014-03-03 23:45:26 +00001355 scno = shuffle_scno(scno);
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001356 update_personality(tcp, 0);
1357 break;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001358 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001359#elif defined(ARM)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001360 if (arm_regs.ARM_ip != 0) {
1361 /* It is not a syscall entry */
1362 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001363 tcp->flags |= TCB_INSYSCALL;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001364 return 0;
1365 }
1366 /* Note: we support only 32-bit CPUs, not 26-bit */
1367
Dmitry V. Levin0160e162014-03-18 23:37:43 +00001368# if !defined(__ARM_EABI__) || ENABLE_ARM_OABI
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001369 if (arm_regs.ARM_cpsr & 0x20)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001370 /* Thumb mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001371 goto scno_in_r7;
1372 /* ARM mode */
1373 /* Check EABI/OABI by examining SVC insn's low 24 bits */
1374 errno = 0;
1375 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1376 if (errno)
1377 return -1;
1378 /* EABI syscall convention? */
1379 if (scno != 0xef000000) {
1380 /* No, it's OABI */
1381 if ((scno & 0x0ff00000) != 0x0f900000) {
1382 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1383 tcp->pid, scno);
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001384 return -1;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001385 }
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001386 /* Fixup the syscall number */
1387 scno &= 0x000fffff;
1388 } else {
1389 scno_in_r7:
1390 scno = arm_regs.ARM_r7;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001391 }
Dmitry V. Levin0160e162014-03-18 23:37:43 +00001392# else /* __ARM_EABI__ || !ENABLE_ARM_OABI */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001393 scno = arm_regs.ARM_r7;
1394# endif
Denys Vlasenko7270de52013-02-21 15:46:34 +01001395 scno = shuffle_scno(scno);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001396#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001397 if (upeek(tcp->pid, 4*PT_ORIG_D0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001398 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001399#elif defined(LINUX_MIPSN32)
Roland McGrath542c2c62008-05-20 01:11:56 +00001400 unsigned long long regs[38];
1401
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001402 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001403 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001404 mips_a3 = regs[REG_A3];
1405 mips_r2 = regs[REG_V0];
Roland McGrath542c2c62008-05-20 01:11:56 +00001406
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001407 scno = mips_r2;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001408 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001409 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001410 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001411 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001412 return 0;
1413 }
Roland McGrath542c2c62008-05-20 01:11:56 +00001414 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001415#elif defined(MIPS)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001416 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001417 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001418 if (upeek(tcp->pid, REG_V0, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001419 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001420
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001421 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001422 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001423 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001424 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001425 return 0;
1426 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001427 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001428#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001429 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001430 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001431 if (upeek(tcp->pid, REG_R0, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001432 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001433
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001434 /*
1435 * Do some sanity checks to figure out if it's
1436 * really a syscall entry
1437 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001438 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001439 if (alpha_a3 == 0 || alpha_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001440 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001441 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001442 return 0;
1443 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001444 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001445#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001446 /* Disassemble the syscall trap. */
1447 /* Retrieve the syscall trap instruction. */
Denys Vlasenko46455822013-02-05 17:02:59 +01001448 unsigned long trap;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001449 errno = 0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001450# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001451 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001452 trap >>= 32;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001453# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001454 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001455# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001456 if (errno)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001457 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001458
1459 /* Disassemble the trap to see what personality to use. */
1460 switch (trap) {
1461 case 0x91d02010:
1462 /* Linux/SPARC syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001463 update_personality(tcp, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001464 break;
1465 case 0x91d0206d:
1466 /* Linux/SPARC64 syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001467 update_personality(tcp, 2);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001468 break;
1469 case 0x91d02000:
1470 /* SunOS syscall trap. (pers 1) */
1471 fprintf(stderr, "syscall: SunOS no support\n");
1472 return -1;
1473 case 0x91d02008:
1474 /* Solaris 2.x syscall trap. (per 2) */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001475 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001476 break;
1477 case 0x91d02009:
1478 /* NetBSD/FreeBSD syscall trap. */
1479 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1480 return -1;
1481 case 0x91d02027:
1482 /* Solaris 2.x gettimeofday */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001483 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001484 break;
1485 default:
Denys Vlasenko523635f2012-02-25 02:44:25 +01001486# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001487 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001488# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001489 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001490# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001491 return -1;
1492 }
1493
1494 /* Extract the system call number from the registers. */
1495 if (trap == 0x91d02027)
1496 scno = 156;
1497 else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001498 scno = sparc_regs.u_regs[U_REG_G1];
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001499 if (scno == 0) {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001500 scno = sparc_regs.u_regs[U_REG_O0];
1501 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 +02001502 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001503#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001504 if (upeek(tcp->pid, PT_GR20, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001505 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001506#elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001507 /*
1508 * In the new syscall ABI, the system call number is in R3.
1509 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001510 if (upeek(tcp->pid, 4*(REG_REG0+3), &scno) < 0)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001511 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001512
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001513 if (scno < 0) {
1514 /* Odd as it may seem, a glibc bug has been known to cause
1515 glibc to issue bogus negative syscall numbers. So for
1516 our purposes, make strace print what it *should* have been */
1517 long correct_scno = (scno & 0xff);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001518 if (debug_flag)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001519 fprintf(stderr,
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001520 "Detected glibc bug: bogus system call"
1521 " number = %ld, correcting to %ld\n",
1522 scno,
1523 correct_scno);
1524 scno = correct_scno;
1525 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001526#elif defined(SH64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001527 if (upeek(tcp->pid, REG_SYSCALL, &scno) < 0)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001528 return -1;
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001529 scno &= 0xFFFF;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001530#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001531 if (upeek(tcp->pid, 4*PT_R9, &scno) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001532 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001533#elif defined(TILE)
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001534 unsigned int currpers;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001535 scno = tile_regs.regs[10];
1536# ifdef __tilepro__
1537 currpers = 1;
1538# else
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001539# ifndef PT_FLAGS_COMPAT
1540# define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1541# endif
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001542 if (tile_regs.flags & PT_FLAGS_COMPAT)
1543 currpers = 1;
1544 else
1545 currpers = 0;
1546# endif
1547 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001548#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001549 if (upeek(tcp->pid, 0, &scno) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001550 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001551#elif defined(OR1K)
1552 scno = or1k_regs.gpr[11];
James Hogan5f999a82013-02-22 14:44:10 +00001553#elif defined(METAG)
1554 scno = metag_regs.dx[0][1]; /* syscall number in D1Re0 (D1.0) */
Chris Zankel8f636ed2013-03-25 10:22:07 -07001555#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001556 if (upeek(tcp->pid, SYSCALL_NR, &scno) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001557 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301558# elif defined(ARC)
1559 scno = arc_regs.scratch.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001560#endif
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001561
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001562 tcp->scno = scno;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001563 if (SCNO_IS_VALID(tcp->scno)) {
1564 tcp->s_ent = &sysent[scno];
1565 tcp->qual_flg = qual_flags[scno];
1566 } else {
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01001567 static const struct_sysent unknown = {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001568 .nargs = MAX_ARGS,
1569 .sys_flags = 0,
1570 .sys_func = printargs,
1571 .sys_name = "unknown", /* not used */
1572 };
1573 tcp->s_ent = &unknown;
1574 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1575 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001576 return 1;
1577}
1578
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02001579/* Called at each syscall entry.
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001580 * Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001581 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1582 * 1: ok, continue in trace_syscall_entering().
1583 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001584 * ("????" etc) and bail out.
1585 */
Roland McGratha4d48532005-06-08 20:45:28 +00001586static int
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001587syscall_fixup_on_sysenter(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001588{
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001589 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001590#if defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001591 if (i386_regs.eax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001592 if (debug_flag)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001593 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1594 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001595 }
H.J. Lu35be5812012-04-16 13:00:01 +02001596#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001597 {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001598 long rax;
1599 if (x86_io.iov_len == sizeof(i386_regs)) {
1600 /* Sign extend from 32 bits */
1601 rax = (int32_t)i386_regs.eax;
1602 } else {
1603 /* Note: in X32 build, this truncates 64 to 32 bits */
1604 rax = x86_64_regs.rax;
1605 }
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001606 if (rax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001607 if (debug_flag)
Denys Vlasenko18beb982011-08-24 16:59:23 +02001608 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1609 return 0;
1610 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001611 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001612#elif defined(M68K)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001613 /* TODO? Eliminate upeek's in arches below like we did in x86 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001614 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001615 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001616 if (m68k_d0 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001617 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001618 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001619 return 0;
1620 }
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001621#elif defined(IA64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001622 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001623 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001624 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001625 return -1;
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001626 if (ia64_ia32mode && ia64_r8 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001627 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001628 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001629 return 0;
1630 }
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001631#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001632 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001633 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001634 if (cris_r10 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001635 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001636 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001637 return 0;
1638 }
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001639#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001640 if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001641 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001642 if (microblaze_r3 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001643 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001644 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001645 return 0;
1646 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001647#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001648 return 1;
1649}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001650
Denys Vlasenko146b9442012-03-18 22:10:48 +01001651static void
1652internal_fork(struct tcb *tcp)
1653{
1654#if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1655# define ARG_FLAGS 1
1656#else
1657# define ARG_FLAGS 0
1658#endif
1659#ifndef CLONE_UNTRACED
1660# define CLONE_UNTRACED 0x00800000
1661#endif
1662 if ((ptrace_setoptions
1663 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1664 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1665 return;
1666
1667 if (!followfork)
1668 return;
1669
1670 if (entering(tcp)) {
1671 /*
1672 * We won't see the new child if clone is called with
1673 * CLONE_UNTRACED, so we keep the same logic with that option
1674 * and don't trace it.
1675 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001676 if ((tcp->s_ent->sys_func == sys_clone)
1677 && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
1678 )
Denys Vlasenko146b9442012-03-18 22:10:48 +01001679 return;
1680 setbpt(tcp);
1681 } else {
1682 if (tcp->flags & TCB_BPTSET)
1683 clearbpt(tcp);
1684 }
1685}
1686
1687#if defined(TCB_WAITEXECVE)
1688static void
1689internal_exec(struct tcb *tcp)
1690{
1691 /* Maybe we have post-execve SIGTRAP suppressed? */
1692 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1693 return; /* yes, no need to do anything */
1694
1695 if (exiting(tcp) && syserror(tcp))
1696 /* Error in execve, no post-execve SIGTRAP expected */
1697 tcp->flags &= ~TCB_WAITEXECVE;
1698 else
1699 tcp->flags |= TCB_WAITEXECVE;
1700}
1701#endif
1702
1703static void
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001704syscall_fixup_for_fork_exec(struct tcb *tcp)
Roland McGrathc1e45922008-05-27 23:18:29 +00001705{
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001706 /*
1707 * We must always trace a few critical system calls in order to
1708 * correctly support following forks in the presence of tracing
1709 * qualifiers.
1710 */
1711 int (*func)();
1712
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001713 func = tcp->s_ent->sys_func;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001714
1715 if ( sys_fork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001716 || sys_clone == func
Denys Vlasenko146b9442012-03-18 22:10:48 +01001717 ) {
1718 internal_fork(tcp);
1719 return;
1720 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001721
Denys Vlasenko84703742012-02-25 02:38:52 +01001722#if defined(TCB_WAITEXECVE)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001723 if ( sys_execve == func
Denys Vlasenko84703742012-02-25 02:38:52 +01001724# if defined(SPARC) || defined(SPARC64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001725 || sys_execv == func
Denys Vlasenkoa7949742011-08-21 17:26:55 +02001726# endif
Denys Vlasenko146b9442012-03-18 22:10:48 +01001727 ) {
1728 internal_exec(tcp);
1729 return;
1730 }
Roland McGrathc1e45922008-05-27 23:18:29 +00001731#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001732}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001733
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001734/* Return -1 on error or 1 on success (never 0!) */
Roland McGratha4d48532005-06-08 20:45:28 +00001735static int
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001736get_syscall_args(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001737{
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001738 int i, nargs;
1739
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001740 nargs = tcp->s_ent->nargs;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001741
Denys Vlasenko523635f2012-02-25 02:44:25 +01001742#if defined(S390) || defined(S390X)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001743 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001744 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 +02001745 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001746#elif defined(ALPHA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001747 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001748 if (upeek(tcp->pid, REG_A0+i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001749 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001750#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001751 if (!ia64_ia32mode) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001752 unsigned long *out0, cfm, sof, sol;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001753 long rbs_end;
1754 /* be backwards compatible with kernel < 2.4.4... */
1755# ifndef PT_RBS_END
1756# define PT_RBS_END PT_AR_BSP
1757# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001758
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001759 if (upeek(tcp->pid, PT_RBS_END, &rbs_end) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001760 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001761 if (upeek(tcp->pid, PT_CFM, (long *) &cfm) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001762 return -1;
1763
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001764 sof = (cfm >> 0) & 0x7f;
1765 sol = (cfm >> 7) & 0x7f;
1766 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1767
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001768 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001769 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1770 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1771 return -1;
1772 }
1773 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001774 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1775 PT_R9 /* ECX = out1 */,
1776 PT_R10 /* EDX = out2 */,
1777 PT_R14 /* ESI = out3 */,
1778 PT_R15 /* EDI = out4 */,
1779 PT_R13 /* EBP = out5 */};
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001780
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001781 for (i = 0; i < nargs; ++i) {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001782 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001783 return -1;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001784 /* truncate away IVE sign-extension */
1785 tcp->u_arg[i] &= 0xffffffff;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001786 }
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001787 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001788#elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001789 /* N32 and N64 both use up to six registers. */
1790 unsigned long long regs[38];
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001791
1792 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1793 return -1;
1794
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001795 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001796 tcp->u_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001797# if defined(LINUX_MIPSN32)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001798 tcp->ext_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001799# endif
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001800 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001801#elif defined(MIPS)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001802 if (nargs > 4) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001803 long sp;
1804
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001805 if (upeek(tcp->pid, REG_SP, &sp) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001806 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001807 for (i = 0; i < 4; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001808 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001809 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001810 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001811 (char *)(tcp->u_arg + 4));
1812 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001813 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001814 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001815 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001816 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001817#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001818 (void)i;
1819 (void)nargs;
1820 tcp->u_arg[0] = ppc_regs.orig_gpr3;
1821 tcp->u_arg[1] = ppc_regs.gpr[4];
1822 tcp->u_arg[2] = ppc_regs.gpr[5];
1823 tcp->u_arg[3] = ppc_regs.gpr[6];
1824 tcp->u_arg[4] = ppc_regs.gpr[7];
1825 tcp->u_arg[5] = ppc_regs.gpr[8];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001826#elif defined(SPARC) || defined(SPARC64)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001827 for (i = 0; i < nargs; ++i)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001828 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001829#elif defined(HPPA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001830 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001831 if (upeek(tcp->pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001832 return -1;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001833#elif defined(ARM) || defined(AARCH64)
1834# if defined(AARCH64)
1835 if (tcp->currpers == 1)
1836 for (i = 0; i < nargs; ++i)
1837 tcp->u_arg[i] = aarch64_regs.regs[i];
1838 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001839# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001840 for (i = 0; i < nargs; ++i)
Denys Vlasenko401374e2013-02-06 18:24:39 +01001841 tcp->u_arg[i] = arm_regs.uregs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001842#elif defined(AVR32)
Denys Vlasenkob5b25892011-08-30 19:04:54 +02001843 (void)i;
1844 (void)nargs;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001845 tcp->u_arg[0] = avr32_regs.r12;
1846 tcp->u_arg[1] = avr32_regs.r11;
1847 tcp->u_arg[2] = avr32_regs.r10;
1848 tcp->u_arg[3] = avr32_regs.r9;
1849 tcp->u_arg[4] = avr32_regs.r5;
1850 tcp->u_arg[5] = avr32_regs.r3;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001851#elif defined(BFIN)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001852 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 +02001853
Denys Vlasenko4b887a52011-08-23 13:32:38 +02001854 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001855 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001856 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001857#elif defined(SH)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001858 static const int syscall_regs[MAX_ARGS] = {
1859 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1860 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001861 };
1862
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001863 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001864 if (upeek(tcp->pid, syscall_regs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001865 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001866#elif defined(SH64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001867 int i;
1868 /* Registers used by SH5 Linux system calls for parameters */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001869 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001870
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001871 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001872 if (upeek(tcp->pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001873 return -1;
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001874#elif defined(I386)
1875 (void)i;
1876 (void)nargs;
1877 tcp->u_arg[0] = i386_regs.ebx;
1878 tcp->u_arg[1] = i386_regs.ecx;
1879 tcp->u_arg[2] = i386_regs.edx;
1880 tcp->u_arg[3] = i386_regs.esi;
1881 tcp->u_arg[4] = i386_regs.edi;
1882 tcp->u_arg[5] = i386_regs.ebp;
H.J. Lu35be5812012-04-16 13:00:01 +02001883#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001884 (void)i;
1885 (void)nargs;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001886 if (x86_io.iov_len != sizeof(i386_regs)) {
1887 /* x86-64 or x32 ABI */
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001888 tcp->u_arg[0] = x86_64_regs.rdi;
1889 tcp->u_arg[1] = x86_64_regs.rsi;
1890 tcp->u_arg[2] = x86_64_regs.rdx;
1891 tcp->u_arg[3] = x86_64_regs.r10;
1892 tcp->u_arg[4] = x86_64_regs.r8;
1893 tcp->u_arg[5] = x86_64_regs.r9;
H.J. Lu35be5812012-04-16 13:00:01 +02001894# ifdef X32
1895 tcp->ext_arg[0] = x86_64_regs.rdi;
1896 tcp->ext_arg[1] = x86_64_regs.rsi;
1897 tcp->ext_arg[2] = x86_64_regs.rdx;
1898 tcp->ext_arg[3] = x86_64_regs.r10;
1899 tcp->ext_arg[4] = x86_64_regs.r8;
1900 tcp->ext_arg[5] = x86_64_regs.r9;
1901# endif
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001902 } else {
1903 /* i386 ABI */
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001904 /* Zero-extend from 32 bits */
1905 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1906 * if you need to use *sign-extended* parameter.
1907 */
1908 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1909 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1910 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1911 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1912 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1913 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001914 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001915#elif defined(MICROBLAZE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001916 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001917 if (upeek(tcp->pid, (5 + i) * 4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001918 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001919#elif defined(CRISV10) || defined(CRISV32)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001920 static const int crisregs[MAX_ARGS] = {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001921 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001922 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001923 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001924
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001925 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001926 if (upeek(tcp->pid, crisregs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001927 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001928#elif defined(TILE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001929 for (i = 0; i < nargs; ++i)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001930 tcp->u_arg[i] = tile_regs.regs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001931#elif defined(M68K)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001932 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001933 if (upeek(tcp->pid, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001934 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001935#elif defined(OR1K)
1936 (void)nargs;
1937 for (i = 0; i < 6; ++i)
1938 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
James Hogan5f999a82013-02-22 14:44:10 +00001939#elif defined(METAG)
1940 for (i = 0; i < nargs; i++)
1941 /* arguments go backwards from D1Ar1 (D1.3) */
1942 tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
Chris Zankel8f636ed2013-03-25 10:22:07 -07001943#elif defined(XTENSA)
1944 /* arg0: a6, arg1: a3, arg2: a4, arg3: a5, arg4: a8, arg5: a9 */
1945 static const int xtensaregs[MAX_ARGS] = { 6, 3, 4, 5, 8, 9 };
1946 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001947 if (upeek(tcp->pid, REG_A_BASE + xtensaregs[i], &tcp->u_arg[i]) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001948 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301949# elif defined(ARC)
1950 long *arc_args = &arc_regs.scratch.r0;
1951 for (i = 0; i < nargs; ++i)
1952 tcp->u_arg[i] = *arc_args--;
1953
Denys Vlasenko523635f2012-02-25 02:44:25 +01001954#else /* Other architecture (32bits specific) */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001955 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001956 if (upeek(tcp->pid, i*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001957 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001958#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001959 return 1;
1960}
1961
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00001962static int
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001963trace_syscall_entering(struct tcb *tcp)
1964{
1965 int res, scno_good;
1966
Denys Vlasenko2ce12ed2011-08-24 17:25:32 +02001967#if defined TCB_WAITEXECVE
1968 if (tcp->flags & TCB_WAITEXECVE) {
1969 /* This is the post-execve SIGTRAP. */
1970 tcp->flags &= ~TCB_WAITEXECVE;
1971 return 0;
1972 }
1973#endif
1974
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001975 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001976 if (res == 0)
1977 return res;
Denys Vlasenko907735a2012-03-21 00:23:16 +01001978 if (res == 1) {
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001979 res = syscall_fixup_on_sysenter(tcp);
Denys Vlasenko907735a2012-03-21 00:23:16 +01001980 if (res == 0)
1981 return res;
1982 if (res == 1)
1983 res = get_syscall_args(tcp);
1984 }
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001985
1986 if (res != 1) {
1987 printleader(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001988 if (scno_good != 1)
Denys Vlasenkob7a6dae2012-03-20 16:48:35 +01001989 tprints("????" /* anti-trigraph gap */ "(");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001990 else if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01001991 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001992 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001993 tprintf("%s(", tcp->s_ent->sys_name);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001994 /*
1995 * " <unavailable>" will be added later by the code which
1996 * detects ptrace errors.
1997 */
1998 goto ret;
1999 }
2000
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002001 if ( sys_execve == tcp->s_ent->sys_func
2002# if defined(SPARC) || defined(SPARC64)
2003 || sys_execv == tcp->s_ent->sys_func
2004# endif
2005 ) {
2006 hide_log_until_execve = 0;
2007 }
2008
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002009#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002010 while (1) {
Denys Vlasenko523635f2012-02-25 02:44:25 +01002011# ifdef SYS_socket_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002012 if (tcp->s_ent->sys_func == sys_socketcall) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00002013 decode_socket_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002014 break;
2015 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002016# endif
2017# ifdef SYS_ipc_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002018 if (tcp->s_ent->sys_func == sys_ipc) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00002019 decode_ipc_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002020 break;
2021 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002022# endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002023 break;
2024 }
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002025#endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002026
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01002027 if (need_fork_exec_workarounds)
2028 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002029
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002030 if (!(tcp->qual_flg & QUAL_TRACE)
2031 || (tracing_paths && !pathtrace_match(tcp))
2032 ) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002033 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
2034 return 0;
2035 }
2036
2037 tcp->flags &= ~TCB_FILTERED;
2038
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002039 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002040 res = 0;
2041 goto ret;
2042 }
2043
Masatake YAMATOed69fc22014-04-16 15:33:35 +09002044#ifdef USE_LIBUNWIND
2045 if (stack_trace_enabled) {
2046 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
2047 unwind_capture_stacktrace(tcp);
2048 }
2049#endif
2050
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002051 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002052 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002053 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002054 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002055 tprintf("%s(", tcp->s_ent->sys_name);
2056 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002057 res = printargs(tcp);
2058 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002059 res = tcp->s_ent->sys_func(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002060
Dmitry V. Levinb742d8c2012-09-17 22:40:12 +00002061 fflush(tcp->outf);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002062 ret:
2063 tcp->flags |= TCB_INSYSCALL;
2064 /* Measure the entrance time as late as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002065 if (Tflag || cflag)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002066 gettimeofday(&tcp->etime, NULL);
2067 return res;
2068}
2069
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002070/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01002071 * 1: ok, continue in trace_syscall_exiting().
2072 * -1: error, trace_syscall_exiting() should print error indicator
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002073 * ("????" etc) and bail out.
2074 */
2075static int
2076get_syscall_result(struct tcb *tcp)
2077{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002078#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002079 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002080 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002081#elif defined(POWERPC)
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002082 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002083#elif defined(AVR32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002084 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002085#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002086 if (upeek(tcp->pid, PT_R0, &bfin_r0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002087 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002088#elif defined(I386)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002089 /* already done by get_regs */
H.J. Lu35be5812012-04-16 13:00:01 +02002090#elif defined(X86_64) || defined(X32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002091 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002092#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002093# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01002094 long psr;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002095 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02002096 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002097 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002098 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002099 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002100 return -1;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002101#elif defined(ARM)
2102 /* already done by get_regs */
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002103#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002104 /* register reading already done by get_regs */
2105
2106 /* Used to do this, but we did it on syscall entry already: */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002107 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
2108 * else it's personality 0.
2109 */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002110 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
Denys Vlasenko523635f2012-02-25 02:44:25 +01002111#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002112 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002113 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002114#elif defined(LINUX_MIPSN32)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002115 unsigned long long regs[38];
2116
2117 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
2118 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002119 mips_a3 = regs[REG_A3];
2120 mips_r2 = regs[REG_V0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01002121#elif defined(MIPS)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002122 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002123 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002124 if (upeek(tcp->pid, REG_V0, &mips_r2) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002125 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002126#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002127 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002128 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002129 if (upeek(tcp->pid, REG_R0, &alpha_r0) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002130 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002131#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002132 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002133#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002134 if (upeek(tcp->pid, PT_GR28, &hppa_r28) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002135 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002136#elif defined(SH)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002137 /* new syscall ABI returns result in R0 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002138 if (upeek(tcp->pid, 4*REG_REG0, (long *)&sh_r0) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002139 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002140#elif defined(SH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002141 /* ABI defines result returned in r9 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002142 if (upeek(tcp->pid, REG_GENERAL(9), (long *)&sh64_r9) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002143 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002144#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002145 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002146 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002147#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002148 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002149#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002150 if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002151 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01002152#elif defined(OR1K)
2153 /* already done by get_regs */
James Hogan5f999a82013-02-22 14:44:10 +00002154#elif defined(METAG)
2155 /* already done by get_regs */
Chris Zankel8f636ed2013-03-25 10:22:07 -07002156#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002157 if (upeek(tcp->pid, REG_A_BASE + 2, &xtensa_a2) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07002158 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05302159#elif defined(ARC)
2160 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002161#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002162 return 1;
2163}
2164
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002165/* Called at each syscall exit */
2166static void
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002167syscall_fixup_on_sysexit(struct tcb *tcp)
2168{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002169#if defined(S390) || defined(S390X)
Denys Vlasenkoece98792011-08-25 10:25:35 +02002170 if ((tcp->flags & TCB_WAITEXECVE)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002171 && (s390_gpr2 == -ENOSYS || s390_gpr2 == tcp->scno)) {
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002172 /*
2173 * Return from execve.
2174 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2175 * flag set for the post-execve SIGTRAP to see and reset.
2176 */
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002177 s390_gpr2 = 0;
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002178 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002179#endif
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002180}
2181
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002182/*
2183 * Check the syscall return value register value for whether it is
2184 * a negated errno code indicating an error, or a success return value.
2185 */
2186static inline int
2187is_negated_errno(unsigned long int val)
2188{
2189 unsigned long int max = -(long int) nerrnos;
Denys Vlasenko2544f982013-02-19 17:39:56 +01002190#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01002191 if (current_wordsize < sizeof(val)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002192 val = (unsigned int) val;
2193 max = (unsigned int) max;
2194 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002195#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002196 return val > max;
2197}
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002198
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002199#if defined(X32)
2200static inline int
2201is_negated_errno_x32(unsigned long long val)
2202{
2203 unsigned long long max = -(long long) nerrnos;
2204 /*
2205 * current_wordsize is 4 even in personality 0 (native X32)
2206 * but truncation _must not_ be done in it.
2207 * can't check current_wordsize here!
2208 */
2209 if (current_personality != 0) {
2210 val = (uint32_t) val;
2211 max = (uint32_t) max;
2212 }
2213 return val > max;
2214}
2215#endif
2216
Denys Vlasenko907735a2012-03-21 00:23:16 +01002217/* Returns:
2218 * 1: ok, continue in trace_syscall_exiting().
2219 * -1: error, trace_syscall_exiting() should print error indicator
2220 * ("????" etc) and bail out.
2221 */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002222static void
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002223get_error(struct tcb *tcp)
2224{
2225 int u_error = 0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002226 int check_errno = 1;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002227 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002228 check_errno = 0;
2229 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002230#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002231 if (check_errno && is_negated_errno(s390_gpr2)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002232 tcp->u_rval = -1;
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002233 u_error = -s390_gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002234 }
2235 else {
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002236 tcp->u_rval = s390_gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002237 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002238#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002239 if (check_errno && is_negated_errno(i386_regs.eax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002240 tcp->u_rval = -1;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002241 u_error = -i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002242 }
2243 else {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002244 tcp->u_rval = i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002245 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002246#elif defined(X86_64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002247 long rax;
2248 if (x86_io.iov_len == sizeof(i386_regs)) {
2249 /* Sign extend from 32 bits */
2250 rax = (int32_t)i386_regs.eax;
2251 } else {
2252 rax = x86_64_regs.rax;
2253 }
2254 if (check_errno && is_negated_errno(rax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002255 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002256 u_error = -rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002257 }
2258 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002259 tcp->u_rval = rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002260 }
2261#elif defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002262 /* In X32, return value is 64-bit (llseek uses one).
2263 * Using merely "long rax" would not work.
2264 */
2265 long long rax;
2266 if (x86_io.iov_len == sizeof(i386_regs)) {
2267 /* Sign extend from 32 bits */
2268 rax = (int32_t)i386_regs.eax;
2269 } else {
2270 rax = x86_64_regs.rax;
2271 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002272 /* Careful: is_negated_errno() works only on longs */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002273 if (check_errno && is_negated_errno_x32(rax)) {
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002274 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002275 u_error = -rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002276 }
2277 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002278 tcp->u_rval = rax; /* truncating */
2279 tcp->u_lrval = rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002280 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002281#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02002282 if (ia64_ia32mode) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002283 int err;
2284
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002285 err = (int)ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002286 if (check_errno && is_negated_errno(err)) {
2287 tcp->u_rval = -1;
2288 u_error = -err;
2289 }
2290 else {
2291 tcp->u_rval = err;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002292 }
2293 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002294 if (check_errno && ia64_r10) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002295 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002296 u_error = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002297 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002298 tcp->u_rval = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002299 }
2300 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002301#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002302 if (check_errno && mips_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002303 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002304 u_error = mips_r2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002305 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002306 tcp->u_rval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002307# if defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002308 tcp->u_lrval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002309# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002310 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002311#elif defined(POWERPC)
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002312 if (check_errno && (ppc_regs.ccr & 0x10000000)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002313 tcp->u_rval = -1;
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002314 u_error = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002315 }
2316 else {
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002317 tcp->u_rval = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002318 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002319#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002320 if (check_errno && is_negated_errno(m68k_d0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002321 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002322 u_error = -m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002323 }
2324 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002325 tcp->u_rval = m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002326 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002327#elif defined(ARM) || defined(AARCH64)
2328# if defined(AARCH64)
2329 if (tcp->currpers == 1) {
2330 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2331 tcp->u_rval = -1;
2332 u_error = -aarch64_regs.regs[0];
2333 }
2334 else {
2335 tcp->u_rval = aarch64_regs.regs[0];
2336 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002337 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002338 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002339# endif
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002340 {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002341 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002342 tcp->u_rval = -1;
Denys Vlasenko401374e2013-02-06 18:24:39 +01002343 u_error = -arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002344 }
2345 else {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002346 tcp->u_rval = arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002347 }
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002348 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002349#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002350 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002351 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002352 u_error = -avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002353 }
2354 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002355 tcp->u_rval = avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002356 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002357#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002358 if (check_errno && is_negated_errno(bfin_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002359 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002360 u_error = -bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002361 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002362 tcp->u_rval = bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002363 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002364#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002365 if (check_errno && alpha_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002366 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002367 u_error = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002368 }
2369 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002370 tcp->u_rval = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002371 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002372#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002373 if (check_errno && sparc_regs.psr & PSR_C) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002374 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002375 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002376 }
2377 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002378 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002379 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002380#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002381 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002382 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002383 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002384 }
2385 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002386 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002387 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002388#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002389 if (check_errno && is_negated_errno(hppa_r28)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002390 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002391 u_error = -hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002392 }
2393 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002394 tcp->u_rval = hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002395 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002396#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002397 if (check_errno && is_negated_errno(sh_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002398 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002399 u_error = -sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002400 }
2401 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002402 tcp->u_rval = sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002403 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002404#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002405 if (check_errno && is_negated_errno(sh64_r9)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002406 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002407 u_error = -sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002408 }
2409 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002410 tcp->u_rval = sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002411 }
James Hogan5f999a82013-02-22 14:44:10 +00002412#elif defined(METAG)
2413 /* result pointer in D0Re0 (D0.0) */
2414 if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2415 tcp->u_rval = -1;
2416 u_error = -metag_regs.dx[0][0];
2417 }
2418 else {
2419 tcp->u_rval = metag_regs.dx[0][0];
2420 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002421#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002422 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002423 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002424 u_error = -cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002425 }
2426 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002427 tcp->u_rval = cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002428 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002429#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002430 /*
2431 * The standard tile calling convention returns the value (or negative
2432 * errno) in r0, and zero (or positive errno) in r1.
2433 * Until at least kernel 3.8, however, the r1 value is not reflected
2434 * in ptregs at this point, so we use r0 here.
2435 */
2436 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002437 tcp->u_rval = -1;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002438 u_error = -tile_regs.regs[0];
2439 } else {
2440 tcp->u_rval = tile_regs.regs[0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002441 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002442#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002443 if (check_errno && is_negated_errno(microblaze_r3)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002444 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002445 u_error = -microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002446 }
2447 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002448 tcp->u_rval = microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002449 }
Christian Svensson492f81f2013-02-14 13:26:27 +01002450#elif defined(OR1K)
2451 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2452 tcp->u_rval = -1;
2453 u_error = -or1k_regs.gpr[11];
2454 }
2455 else {
2456 tcp->u_rval = or1k_regs.gpr[11];
2457 }
Chris Zankel8f636ed2013-03-25 10:22:07 -07002458#elif defined(XTENSA)
2459 if (check_errno && is_negated_errno(xtensa_a2)) {
2460 tcp->u_rval = -1;
2461 u_error = -xtensa_a2;
2462 }
2463 else {
2464 tcp->u_rval = xtensa_a2;
2465 }
Vineet Gupta7daacbb2013-08-16 12:47:06 +05302466#elif defined(ARC)
2467 if (check_errno && is_negated_errno(arc_regs.scratch.r0)) {
2468 tcp->u_rval = -1;
2469 u_error = -arc_regs.scratch.r0;
2470 }
2471 else {
2472 tcp->u_rval = arc_regs.scratch.r0;
2473 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002474#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002475 tcp->u_error = u_error;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002476}
2477
2478static void
2479dumpio(struct tcb *tcp)
2480{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002481 int (*func)();
2482
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002483 if (syserror(tcp))
2484 return;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +01002485 if ((unsigned long) tcp->u_arg[0] >= num_quals)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002486 return;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002487 func = tcp->s_ent->sys_func;
2488 if (func == printargs)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002489 return;
2490 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002491 if (func == sys_read ||
2492 func == sys_pread ||
2493 func == sys_recv ||
2494 func == sys_recvfrom)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002495 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002496 else if (func == sys_readv)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002497 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2498 return;
2499 }
2500 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002501 if (func == sys_write ||
2502 func == sys_pwrite ||
2503 func == sys_send ||
2504 func == sys_sendto)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002505 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002506 else if (func == sys_writev)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002507 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2508 return;
2509 }
2510}
2511
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002512static int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002513trace_syscall_exiting(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002514{
2515 int sys_res;
2516 struct timeval tv;
Denys Vlasenko1a5b5a72011-08-25 00:29:56 +02002517 int res;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002518 long u_error;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002519
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002520 /* Measure the exit time as early as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002521 if (Tflag || cflag)
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002522 gettimeofday(&tv, NULL);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002523
Masatake YAMATOed69fc22014-04-16 15:33:35 +09002524#ifdef USE_LIBUNWIND
2525 if (stack_trace_enabled) {
2526 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
2527 unwind_cache_invalidate(tcp);
2528 }
2529#endif
2530
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00002531#if SUPPORTED_PERSONALITIES > 1
2532 update_personality(tcp, tcp->currpers);
2533#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002534 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002535 if (res == 1) {
2536 syscall_fixup_on_sysexit(tcp); /* never fails */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002537 get_error(tcp); /* never fails */
2538 if (need_fork_exec_workarounds)
2539 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002540 if (filtered(tcp) || hide_log_until_execve)
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002541 goto ret;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002542 }
2543
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002544 if (cflag) {
Dmitry V. Levinac5133d2014-05-29 18:10:00 +00002545 count_syscall(tcp, &tv);
Denys Vlasenko7b609d52011-06-22 14:32:43 +02002546 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002547 goto ret;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002548 }
2549 }
2550
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002551 /* If not in -ff mode, and printing_tcp != tcp,
2552 * then the log currently does not end with output
2553 * of _our syscall entry_, but with something else.
2554 * We need to say which syscall's return is this.
2555 *
2556 * Forced reprinting via TCB_REPRINT is used only by
2557 * "strace -ff -oLOG test/threaded_execve" corner case.
2558 * It's the only case when -ff mode needs reprinting.
2559 */
2560 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2561 tcp->flags &= ~TCB_REPRINT;
2562 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002563 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002564 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002565 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002566 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002567 }
2568 printing_tcp = tcp;
2569
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002570 if (res != 1) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002571 /* There was error in one of prior ptrace ops */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002572 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002573 tabto();
Denys Vlasenko000b6012012-01-28 01:25:03 +01002574 tprints("= ? <unavailable>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002575 line_ended();
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002576 tcp->flags &= ~TCB_INSYSCALL;
2577 return res;
2578 }
2579
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002580 sys_res = 0;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002581 if (tcp->qual_flg & QUAL_RAW) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002582 /* sys_res = printargs(tcp); - but it's nop on sysexit */
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002583 } else {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002584 /* FIXME: not_failing_only (IOW, option -z) is broken:
2585 * failure of syscall is known only after syscall return.
2586 * Thus we end up with something like this on, say, ENOENT:
2587 * open("doesnt_exist", O_RDONLY <unfinished ...>
2588 * {next syscall decode}
2589 * whereas the intended result is that open(...) line
2590 * is not shown at all.
2591 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002592 if (not_failing_only && tcp->u_error)
Denys Vlasenko3b738812011-08-22 02:06:35 +02002593 goto ret; /* ignore failed syscalls */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002594 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002595 }
2596
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002597 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002598 tabto();
Denys Vlasenko3b738812011-08-22 02:06:35 +02002599 u_error = tcp->u_error;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002600 if (tcp->qual_flg & QUAL_RAW) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002601 if (u_error)
2602 tprintf("= -1 (errno %ld)", u_error);
2603 else
2604 tprintf("= %#lx", tcp->u_rval);
2605 }
2606 else if (!(sys_res & RVAL_NONE) && u_error) {
2607 switch (u_error) {
Denys Vlasenkofe585652012-01-12 11:26:34 +01002608 /* Blocked signals do not interrupt any syscalls.
2609 * In this case syscalls don't return ERESTARTfoo codes.
2610 *
2611 * Deadly signals set to SIG_DFL interrupt syscalls
2612 * and kill the process regardless of which of the codes below
2613 * is returned by the interrupted syscall.
2614 * In some cases, kernel forces a kernel-generated deadly
2615 * signal to be unblocked and set to SIG_DFL (and thus cause
2616 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2617 * or SIGILL. (The alternative is to leave process spinning
2618 * forever on the faulty instruction - not useful).
2619 *
2620 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2621 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2622 * but kernel will always restart them.
2623 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002624 case ERESTARTSYS:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002625 /* Most common type of signal-interrupted syscall exit code.
2626 * The system call will be restarted with the same arguments
2627 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2628 */
2629 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002630 break;
2631 case ERESTARTNOINTR:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002632 /* Rare. For example, fork() returns this if interrupted.
2633 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2634 */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002635 tprints("= ? ERESTARTNOINTR (To be restarted)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002636 break;
2637 case ERESTARTNOHAND:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002638 /* pause(), rt_sigsuspend() etc use this code.
2639 * SA_RESTART is ignored (assumed not set):
2640 * syscall won't restart (will return EINTR instead)
Denys Vlasenko30c03232013-02-19 16:59:26 +01002641 * even after signal with SA_RESTART set. However,
2642 * after SIG_IGN or SIG_DFL signal it will restart
2643 * (thus the name "restart only if has no handler").
Denys Vlasenkofe585652012-01-12 11:26:34 +01002644 */
Denys Vlasenkoaba62922013-03-05 16:56:35 +01002645 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002646 break;
2647 case ERESTART_RESTARTBLOCK:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002648 /* Syscalls like nanosleep(), poll() which can't be
2649 * restarted with their original arguments use this
2650 * code. Kernel will execute restart_syscall() instead,
2651 * which changes arguments before restarting syscall.
2652 * SA_RESTART is ignored (assumed not set) similarly
2653 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2654 * since restart data is saved in "restart block"
2655 * in task struct, and if signal handler uses a syscall
2656 * which in turn saves another such restart block,
2657 * old data is lost and restart becomes impossible)
2658 */
2659 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002660 break;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002661 default:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002662 if (u_error < 0)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002663 tprintf("= -1 E??? (errno %ld)", u_error);
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00002664 else if ((unsigned long) u_error < nerrnos)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002665 tprintf("= -1 %s (%s)", errnoent[u_error],
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002666 strerror(u_error));
2667 else
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002668 tprintf("= -1 ERRNO_%ld (%s)", u_error,
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002669 strerror(u_error));
2670 break;
2671 }
2672 if ((sys_res & RVAL_STR) && tcp->auxstr)
2673 tprintf(" (%s)", tcp->auxstr);
2674 }
2675 else {
2676 if (sys_res & RVAL_NONE)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002677 tprints("= ?");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002678 else {
2679 switch (sys_res & RVAL_MASK) {
2680 case RVAL_HEX:
2681 tprintf("= %#lx", tcp->u_rval);
2682 break;
2683 case RVAL_OCTAL:
2684 tprintf("= %#lo", tcp->u_rval);
2685 break;
2686 case RVAL_UDECIMAL:
2687 tprintf("= %lu", tcp->u_rval);
2688 break;
2689 case RVAL_DECIMAL:
2690 tprintf("= %ld", tcp->u_rval);
2691 break;
Zubin Mithra64aa1b12014-06-04 08:30:41 +05302692 case RVAL_FD:
2693 if (show_fd_path) {
2694 tprints("= ");
2695 printfd(tcp, tcp->u_rval);
2696 }
2697 else
2698 tprintf("= %ld", tcp->u_rval);
2699 break;
H.J. Ludd0130b2012-04-16 12:16:45 +02002700#if defined(LINUX_MIPSN32) || defined(X32)
2701 /*
2702 case RVAL_LHEX:
2703 tprintf("= %#llx", tcp->u_lrval);
2704 break;
2705 case RVAL_LOCTAL:
2706 tprintf("= %#llo", tcp->u_lrval);
2707 break;
2708 */
2709 case RVAL_LUDECIMAL:
2710 tprintf("= %llu", tcp->u_lrval);
2711 break;
2712 /*
2713 case RVAL_LDECIMAL:
2714 tprintf("= %lld", tcp->u_lrval);
2715 break;
2716 */
2717#endif
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002718 default:
2719 fprintf(stderr,
2720 "invalid rval format\n");
2721 break;
2722 }
2723 }
2724 if ((sys_res & RVAL_STR) && tcp->auxstr)
2725 tprintf(" (%s)", tcp->auxstr);
2726 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002727 if (Tflag) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002728 tv_sub(&tv, &tv, &tcp->etime);
2729 tprintf(" <%ld.%06ld>",
2730 (long) tv.tv_sec, (long) tv.tv_usec);
2731 }
Denys Vlasenko000b6012012-01-28 01:25:03 +01002732 tprints("\n");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002733 dumpio(tcp);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002734 line_ended();
2735
Luca Clementi327064b2013-07-23 00:11:35 -07002736#ifdef USE_LIBUNWIND
2737 if (stack_trace_enabled)
Masatake YAMATO61413922014-04-16 15:33:02 +09002738 unwind_print_stacktrace(tcp);
Luca Clementi327064b2013-07-23 00:11:35 -07002739#endif
2740
Denys Vlasenko3b738812011-08-22 02:06:35 +02002741 ret:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002742 tcp->flags &= ~TCB_INSYSCALL;
2743 return 0;
2744}
2745
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002746int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002747trace_syscall(struct tcb *tcp)
2748{
2749 return exiting(tcp) ?
2750 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2751}