blob: 3477dcdad7dd3c8f68320ff241459869ad8c4a78 [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>
40# ifndef PTRACE_PEEKUSR
41# define PTRACE_PEEKUSR PTRACE_PEEKUSER
42# endif
Wichert Akkermanfaf72222000-02-19 23:59:03 +000043#elif defined(HAVE_LINUX_PTRACE_H)
Denys Vlasenko523635f2012-02-25 02:44:25 +010044# undef PTRACE_SYSCALL
Roland McGrathce9f0742004-03-01 21:29:22 +000045# ifdef HAVE_STRUCT_IA64_FPREG
46# define ia64_fpreg XXX_ia64_fpreg
47# endif
48# ifdef HAVE_STRUCT_PT_ALL_USER_REGS
49# define pt_all_user_regs XXX_pt_all_user_regs
50# endif
Ali Polatel0b4060f2013-09-24 20:04:32 +030051# ifdef HAVE_STRUCT_PTRACE_PEEKSIGINFO_ARGS
52# define ptrace_peeksiginfo_args XXX_ptrace_peeksiginfo_args
53# endif
Denys Vlasenko523635f2012-02-25 02:44:25 +010054# include <linux/ptrace.h>
Ali Polatel0b4060f2013-09-24 20:04:32 +030055# undef ptrace_peeksiginfo_args
Roland McGrathce9f0742004-03-01 21:29:22 +000056# undef ia64_fpreg
57# undef pt_all_user_regs
Wichert Akkerman15dea971999-10-06 13:06:34 +000058#endif
59
Denys Vlasenko84703742012-02-25 02:38:52 +010060#if defined(SPARC64)
Roland McGrath6d1a65c2004-07-12 07:44:08 +000061# undef PTRACE_GETREGS
62# define PTRACE_GETREGS PTRACE_GETREGS64
63# undef PTRACE_SETREGS
64# define PTRACE_SETREGS PTRACE_SETREGS64
Denys Vlasenko84703742012-02-25 02:38:52 +010065#endif
Roland McGrath6d1a65c2004-07-12 07:44:08 +000066
Denys Vlasenko84703742012-02-25 02:38:52 +010067#if defined(IA64)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000068# include <asm/ptrace_offsets.h>
69# include <asm/rse.h>
70#endif
71
Dmitry V. Levin32c049a2013-03-18 00:59:27 +000072/* for struct iovec */
73#include <sys/uio.h>
74/* for NT_PRSTATUS */
75#ifdef HAVE_ELF_H
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010076# include <elf.h>
77#endif
78
Steve McIntyred8d3bd32012-10-24 17:58:16 +010079#if defined(AARCH64)
80# include <asm/ptrace.h>
James Hogan5f999a82013-02-22 14:44:10 +000081#endif
82
Chris Zankel8f636ed2013-03-25 10:22:07 -070083#if defined(XTENSA)
84# include <asm/ptrace.h>
85#endif
86
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000087#ifndef NSIG
Denys Vlasenko523635f2012-02-25 02:44:25 +010088# warning: NSIG is not defined, using 32
89# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000090#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000091
92#include "syscall.h"
93
94/* Define these shorthand notations to simplify the syscallent files. */
Roland McGrath2fe7b132005-07-05 03:25:35 +000095#define TD TRACE_DESC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000096#define TF TRACE_FILE
97#define TI TRACE_IPC
98#define TN TRACE_NETWORK
99#define TP TRACE_PROCESS
100#define TS TRACE_SIGNAL
Namhyung Kim96792962012-10-24 11:41:57 +0900101#define TM TRACE_MEMORY
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000102#define NF SYSCALL_NEVER_FAILS
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200103#define MA MAX_ARGS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000104
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100105const struct_sysent sysent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000106#include "syscallent.h"
107};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000108
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100109#if SUPPORTED_PERSONALITIES > 1
110static const struct_sysent sysent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100111# include "syscallent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000112};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200113#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000114
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100115#if SUPPORTED_PERSONALITIES > 2
116static const struct_sysent sysent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100117# include "syscallent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000118};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200119#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000120
121/* Now undef them since short defines cause wicked namespace pollution. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000122#undef TD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000123#undef TF
124#undef TI
125#undef TN
126#undef TP
127#undef TS
Namhyung Kim96792962012-10-24 11:41:57 +0900128#undef TM
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000129#undef NF
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200130#undef MA
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000131
Denys Vlasenko39fca622011-08-20 02:12:33 +0200132/*
133 * `ioctlent.h' may be generated from `ioctlent.raw' by the auxiliary
134 * program `ioctlsort', such that the list is sorted by the `code' field.
135 * This has the side-effect of resolving the _IO.. macros into
136 * plain integers, eliminating the need to include here everything
137 * in "/usr/include".
138 */
139
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100140const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000141#include "errnoent.h"
142};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100143const char *const signalent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200144#include "signalent.h"
145};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100146const struct_ioctlent ioctlent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200147#include "ioctlent.h"
148};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000149
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100150#if SUPPORTED_PERSONALITIES > 1
Roland McGrathee36ce12004-09-04 03:53:10 +0000151static const char *const errnoent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100152# include "errnoent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000153};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200154static const char *const signalent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100155# include "signalent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200156};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100157static const struct_ioctlent ioctlent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100158# include "ioctlent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200159};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200160#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000161
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100162#if SUPPORTED_PERSONALITIES > 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000163static const char *const errnoent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100164# include "errnoent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000165};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200166static const char *const signalent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100167# include "signalent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200168};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100169static const struct_ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100170# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200171};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200172#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000173
Dmitry V. Levine6f55242013-02-26 22:02:30 +0000174enum {
175 nsyscalls0 = ARRAY_SIZE(sysent0)
176#if SUPPORTED_PERSONALITIES > 1
177 , nsyscalls1 = ARRAY_SIZE(sysent1)
178# if SUPPORTED_PERSONALITIES > 2
179 , nsyscalls2 = ARRAY_SIZE(sysent2)
180# endif
181#endif
182};
183
184enum {
185 nerrnos0 = ARRAY_SIZE(errnoent0)
186#if SUPPORTED_PERSONALITIES > 1
187 , nerrnos1 = ARRAY_SIZE(errnoent1)
188# if SUPPORTED_PERSONALITIES > 2
189 , nerrnos2 = ARRAY_SIZE(errnoent2)
190# endif
191#endif
192};
193
194enum {
195 nsignals0 = ARRAY_SIZE(signalent0)
196#if SUPPORTED_PERSONALITIES > 1
197 , nsignals1 = ARRAY_SIZE(signalent1)
198# if SUPPORTED_PERSONALITIES > 2
199 , nsignals2 = ARRAY_SIZE(signalent2)
200# endif
201#endif
202};
203
204enum {
205 nioctlents0 = ARRAY_SIZE(ioctlent0)
206#if SUPPORTED_PERSONALITIES > 1
207 , nioctlents1 = ARRAY_SIZE(ioctlent1)
208# if SUPPORTED_PERSONALITIES > 2
209 , nioctlents2 = ARRAY_SIZE(ioctlent2)
210# endif
211#endif
212};
213
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100214#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100215const struct_sysent *sysent = sysent0;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100216const char *const *errnoent = errnoent0;
217const char *const *signalent = signalent0;
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100218const struct_ioctlent *ioctlent = ioctlent0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100219#endif
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100220unsigned nsyscalls = nsyscalls0;
221unsigned nerrnos = nerrnos0;
222unsigned nsignals = nsignals0;
223unsigned nioctlents = nioctlents0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100224
225unsigned num_quals;
226qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
227
228static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
229 nsyscalls0,
230#if SUPPORTED_PERSONALITIES > 1
231 nsyscalls1,
232#endif
233#if SUPPORTED_PERSONALITIES > 2
234 nsyscalls2,
235#endif
236};
237static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
238 sysent0,
239#if SUPPORTED_PERSONALITIES > 1
240 sysent1,
241#endif
242#if SUPPORTED_PERSONALITIES > 2
243 sysent2,
244#endif
245};
246
247enum {
248 MAX_NSYSCALLS1 = (nsyscalls0
249#if SUPPORTED_PERSONALITIES > 1
250 > nsyscalls1 ? nsyscalls0 : nsyscalls1
251#endif
252 ),
253 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
254#if SUPPORTED_PERSONALITIES > 2
255 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
256#endif
257 ),
258 MAX_NSYSCALLS = MAX_NSYSCALLS2,
259 /* We are ready for arches with up to 255 signals,
260 * even though the largest known signo is on MIPS and it is 128.
261 * The number of existing syscalls on all arches is
262 * larger that 255 anyway, so it is just a pedantic matter.
263 */
264 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
265};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000266
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100267#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100268unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000269
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100270# ifndef current_wordsize
271unsigned current_wordsize;
272static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000273 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000274 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100275# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000276 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100277# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200278};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100279# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000280
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200281void
Dmitry V. Levin3abe8b22006-12-20 22:37:21 +0000282set_personality(int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000283{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100284 nsyscalls = nsyscall_vec[personality];
285 sysent = sysent_vec[personality];
286
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000287 switch (personality) {
288 case 0:
289 errnoent = errnoent0;
290 nerrnos = nerrnos0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000291 ioctlent = ioctlent0;
292 nioctlents = nioctlents0;
293 signalent = signalent0;
294 nsignals = nsignals0;
295 break;
296
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000297 case 1:
298 errnoent = errnoent1;
299 nerrnos = nerrnos1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000300 ioctlent = ioctlent1;
301 nioctlents = nioctlents1;
302 signalent = signalent1;
303 nsignals = nsignals1;
304 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000305
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100306# if SUPPORTED_PERSONALITIES > 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000307 case 2:
308 errnoent = errnoent2;
309 nerrnos = nerrnos2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000310 ioctlent = ioctlent2;
311 nioctlents = nioctlents2;
312 signalent = signalent2;
313 nsignals = nsignals2;
314 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100315# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000316 }
317
318 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100319# ifndef current_wordsize
320 current_wordsize = personality_wordsize[personality];
321# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000322}
323
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000324static void
325update_personality(struct tcb *tcp, int personality)
326{
327 if (personality == current_personality)
328 return;
329 set_personality(personality);
330
331 if (personality == tcp->currpers)
332 return;
333 tcp->currpers = personality;
334
H.J. Lu35be5812012-04-16 13:00:01 +0200335# if defined(POWERPC64)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000336 if (!qflag) {
337 static const char *const names[] = {"64 bit", "32 bit"};
338 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
339 tcp->pid, names[personality]);
340 }
H.J. Lu35be5812012-04-16 13:00:01 +0200341# elif defined(X86_64)
342 if (!qflag) {
343 static const char *const names[] = {"64 bit", "32 bit", "x32"};
344 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
345 tcp->pid, names[personality]);
346 }
H.J. Lu085e4282012-04-17 11:05:04 -0700347# elif defined(X32)
348 if (!qflag) {
349 static const char *const names[] = {"x32", "32 bit"};
350 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
351 tcp->pid, names[personality]);
352 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000353# elif defined(AARCH64)
354 if (!qflag) {
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100355 static const char *const names[] = {"32-bit", "AArch64"};
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000356 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
357 tcp->pid, names[personality]);
358 }
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100359# elif defined(TILE)
360 if (!qflag) {
361 static const char *const names[] = {"64-bit", "32-bit"};
362 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
363 tcp->pid, names[personality]);
364 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100365# endif
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000366}
367#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000368
Denys Vlasenkoc1540fe2013-02-21 16:17:08 +0100369static int qual_syscall(), qual_signal(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000370
Roland McGrathe10e62a2004-09-04 04:20:43 +0000371static const struct qual_options {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000372 int bitflag;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000373 const char *option_name;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000374 int (*qualify)(const char *, int, int);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000375 const char *argument_name;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000376} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000377 { QUAL_TRACE, "trace", qual_syscall, "system call" },
378 { QUAL_TRACE, "t", qual_syscall, "system call" },
379 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
380 { QUAL_ABBREV, "a", qual_syscall, "system call" },
381 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
382 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
383 { QUAL_RAW, "raw", qual_syscall, "system call" },
384 { QUAL_RAW, "x", qual_syscall, "system call" },
385 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
386 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
387 { QUAL_SIGNAL, "s", qual_signal, "signal" },
Roland McGrath9797ceb2002-12-30 10:23:00 +0000388 { QUAL_READ, "read", qual_desc, "descriptor" },
389 { QUAL_READ, "reads", qual_desc, "descriptor" },
390 { QUAL_READ, "r", qual_desc, "descriptor" },
391 { QUAL_WRITE, "write", qual_desc, "descriptor" },
392 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
393 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000394 { 0, NULL, NULL, NULL },
395};
396
Roland McGrath9797ceb2002-12-30 10:23:00 +0000397static void
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100398reallocate_qual(int n)
399{
400 unsigned p;
401 qualbits_t *qp;
402 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
403 qp = qual_vec[p] = realloc(qual_vec[p], n * sizeof(qualbits_t));
404 if (!qp)
405 die_out_of_memory();
406 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
407 }
408 num_quals = n;
409}
410
411static void
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000412qualify_one(int n, int bitflag, int not, int pers)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000413{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100414 unsigned p;
Roland McGrath138c6a32006-01-12 09:50:49 +0000415
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100416 if (num_quals <= n)
417 reallocate_qual(n + 1);
Roland McGrath138c6a32006-01-12 09:50:49 +0000418
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100419 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
420 if (pers == p || pers < 0) {
421 if (not)
422 qual_vec[p][n] &= ~bitflag;
423 else
424 qual_vec[p][n] |= bitflag;
425 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000426 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000427}
428
429static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000430qual_syscall(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000431{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100432 unsigned p;
433 unsigned i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000434 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000435
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100436 if (*s >= '0' && *s <= '9') {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100437 i = string_to_uint(s);
Denys Vlasenkob43dacd2013-02-23 18:19:28 +0100438 if (i >= MAX_NSYSCALLS)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000439 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000440 qualify_one(i, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000441 return 0;
Roland McGrath48a035f2006-01-12 09:45:56 +0000442 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000443
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100444 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
445 for (i = 0; i < nsyscall_vec[p]; i++) {
446 if (sysent_vec[p][i].sys_name
447 && strcmp(s, sysent_vec[p][i].sys_name) == 0
448 ) {
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000449 qualify_one(i, bitflag, not, p);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100450 rc = 0;
451 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000452 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100453 }
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000454
Roland McGrathfe6b3522005-02-02 04:40:11 +0000455 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000456}
457
458static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000459qual_signal(const char *s, int bitflag, int not)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000460{
461 int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000462
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100463 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000464 int signo = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100465 if (signo < 0 || signo > 255)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000466 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000467 qualify_one(signo, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000468 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000469 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000470 if (strncasecmp(s, "SIG", 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000471 s += 3;
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100472 for (i = 0; i <= NSIG; i++) {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000473 if (strcasecmp(s, signame(i) + 3) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000474 qualify_one(i, bitflag, not, -1);
Roland McGrath76421df2005-02-02 03:51:18 +0000475 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000476 }
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100477 }
Roland McGrath76421df2005-02-02 03:51:18 +0000478 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000479}
480
481static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000482qual_desc(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000483{
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100484 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000485 int desc = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100486 if (desc < 0 || desc > 0x7fff) /* paranoia */
Roland McGrathfe6b3522005-02-02 04:40:11 +0000487 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000488 qualify_one(desc, bitflag, not, -1);
Roland McGrath2b619022003-04-10 18:58:20 +0000489 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000490 }
491 return -1;
492}
493
494static int
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000495lookup_class(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000496{
497 if (strcmp(s, "file") == 0)
498 return TRACE_FILE;
499 if (strcmp(s, "ipc") == 0)
500 return TRACE_IPC;
501 if (strcmp(s, "network") == 0)
502 return TRACE_NETWORK;
503 if (strcmp(s, "process") == 0)
504 return TRACE_PROCESS;
505 if (strcmp(s, "signal") == 0)
506 return TRACE_SIGNAL;
Roland McGrath2fe7b132005-07-05 03:25:35 +0000507 if (strcmp(s, "desc") == 0)
508 return TRACE_DESC;
Namhyung Kim96792962012-10-24 11:41:57 +0900509 if (strcmp(s, "memory") == 0)
510 return TRACE_MEMORY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000511 return -1;
512}
513
514void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000515qualify(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000516{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000517 const struct qual_options *opt;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000518 int not;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000519 char *copy;
520 const char *p;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000521 int i, n;
522
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100523 if (num_quals == 0)
524 reallocate_qual(MIN_QUALS);
525
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000526 opt = &qual_options[0];
527 for (i = 0; (p = qual_options[i].option_name); i++) {
528 n = strlen(p);
529 if (strncmp(s, p, n) == 0 && s[n] == '=') {
530 opt = &qual_options[i];
531 s += n + 1;
532 break;
533 }
534 }
535 not = 0;
536 if (*s == '!') {
537 not = 1;
538 s++;
539 }
540 if (strcmp(s, "none") == 0) {
541 not = 1 - not;
542 s = "all";
543 }
544 if (strcmp(s, "all") == 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100545 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000546 qualify_one(i, opt->bitflag, not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000547 }
548 return;
549 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100550 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000551 qualify_one(i, opt->bitflag, !not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000552 }
Denys Vlasenko5d645812011-08-20 12:48:18 +0200553 copy = strdup(s);
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200554 if (!copy)
555 die_out_of_memory();
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000556 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000557 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100558 unsigned pers;
559 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
560 for (i = 0; i < nsyscall_vec[pers]; i++)
561 if (sysent_vec[pers][i].sys_flags & n)
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000562 qualify_one(i, opt->bitflag, not, pers);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100563 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000564 continue;
565 }
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000566 if (opt->qualify(p, opt->bitflag, not)) {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100567 error_msg_and_die("invalid %s '%s'",
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000568 opt->argument_name, p);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000569 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000570 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000571 free(copy);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000572 return;
573}
574
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000575#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000576static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000577decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000578{
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000579 unsigned long addr;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100580 unsigned int i, n, size;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000581
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000582 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
583 return;
584
585 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100586 tcp->qual_flg = qual_flags[tcp->scno];
587 tcp->s_ent = &sysent[tcp->scno];
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000588 addr = tcp->u_arg[1];
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100589 size = current_wordsize;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100590 n = tcp->s_ent->nargs;
591 for (i = 0; i < n; ++i) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000592 if (size == sizeof(int)) {
593 unsigned int arg;
594 if (umove(tcp, addr, &arg) < 0)
595 arg = 0;
596 tcp->u_arg[i] = arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000597 }
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000598 else {
599 unsigned long arg;
600 if (umove(tcp, addr, &arg) < 0)
601 arg = 0;
602 tcp->u_arg[i] = arg;
603 }
604 addr += size;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000605 }
606}
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000607#endif
Mike Frysinger3362e892012-03-15 01:09:19 -0400608
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000609#ifdef SYS_ipc_subcall
610static void
611decode_ipc_subcall(struct tcb *tcp)
612{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100613 unsigned int i, n;
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000614
615 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
616 return;
617
618 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100619 tcp->qual_flg = qual_flags[tcp->scno];
620 tcp->s_ent = &sysent[tcp->scno];
621 n = tcp->s_ent->nargs;
622 for (i = 0; i < n; i++)
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000623 tcp->u_arg[i] = tcp->u_arg[i + 1];
624}
625#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000626
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200627int
628printargs(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000629{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200630 if (entering(tcp)) {
631 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100632 int n = tcp->s_ent->nargs;
633 for (i = 0; i < n; i++)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200634 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
635 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000636 return 0;
637}
638
Denys Vlasenko72879c62012-02-27 14:18:02 +0100639int
640printargs_lu(struct tcb *tcp)
641{
642 if (entering(tcp)) {
643 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100644 int n = tcp->s_ent->nargs;
645 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100646 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
647 }
648 return 0;
649}
650
651int
652printargs_ld(struct tcb *tcp)
653{
654 if (entering(tcp)) {
655 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100656 int n = tcp->s_ent->nargs;
657 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100658 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
659 }
660 return 0;
661}
662
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100663#if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200664long
665getrval2(struct tcb *tcp)
666{
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100667 long val;
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200668
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100669# if defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100670 val = sparc_regs.u_regs[U_REG_O1];
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100671# elif defined(SH)
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200672 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200673 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100674# elif defined(IA64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200675 if (upeek(tcp->pid, PT_R9, &val) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200676 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100677# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200678
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200679 return val;
680}
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100681#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200682
Denys Vlasenko523635f2012-02-25 02:44:25 +0100683#if defined(I386)
Denys Vlasenkob51f3642013-07-16 12:06:25 +0200684static struct user_regs_struct i386_regs;
Denys Vlasenkoe600ac62013-07-18 17:40:45 +0200685/* Cast suppresses signedness warning (.esp is long, not unsigned long) */
686uint32_t *const i386_esp_ptr = (uint32_t*)&i386_regs.esp;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000687# define ARCH_REGS_FOR_GETREGSET i386_regs
H.J. Lu35be5812012-04-16 13:00:01 +0200688#elif defined(X86_64) || defined(X32)
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100689/*
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100690 * On i386, pt_regs and user_regs_struct are the same,
691 * but on 64 bit x86, user_regs_struct has six more fields:
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100692 * fs_base, gs_base, ds, es, fs, gs.
693 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
694 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100695struct i386_user_regs_struct {
696 uint32_t ebx;
697 uint32_t ecx;
698 uint32_t edx;
699 uint32_t esi;
700 uint32_t edi;
701 uint32_t ebp;
702 uint32_t eax;
703 uint32_t xds;
704 uint32_t xes;
705 uint32_t xfs;
706 uint32_t xgs;
707 uint32_t orig_eax;
708 uint32_t eip;
709 uint32_t xcs;
710 uint32_t eflags;
711 uint32_t esp;
712 uint32_t xss;
713};
714static union {
715 struct user_regs_struct x86_64_r;
716 struct i386_user_regs_struct i386_r;
717} x86_regs_union;
718# define x86_64_regs x86_regs_union.x86_64_r
719# define i386_regs x86_regs_union.i386_r
Denys Vlasenkob51f3642013-07-16 12:06:25 +0200720uint32_t *const i386_esp_ptr = &i386_regs.esp;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100721static struct iovec x86_io = {
722 .iov_base = &x86_regs_union
723};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100724#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +0200725bool ia64_ia32mode = 0; /* not static */
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100726static long ia64_r8, ia64_r10;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100727#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +0200728struct pt_regs ppc_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100729#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100730static long m68k_d0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100731#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100732static long bfin_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100733#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100734struct pt_regs arm_regs; /* not static */
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000735# define ARCH_REGS_FOR_GETREGSET arm_regs
Steve McIntyred8d3bd32012-10-24 17:58:16 +0100736#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100737static union {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100738 struct user_pt_regs aarch64_r;
739 struct arm_pt_regs arm_r;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100740} arm_regs_union;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100741# define aarch64_regs arm_regs_union.aarch64_r
742# define arm_regs arm_regs_union.arm_r
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100743static struct iovec aarch64_io = {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100744 .iov_base = &arm_regs_union
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100745};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100746#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100747static long alpha_r0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100748static long alpha_a3;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100749#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100750static struct pt_regs avr32_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100751#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100752struct pt_regs sparc_regs; /* not static */
Denys Vlasenko523635f2012-02-25 02:44:25 +0100753#elif defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100754static long long mips_a3;
755static long long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100756#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100757static long mips_a3;
758static long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100759#elif defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +0200760static long s390_gpr2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100761#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100762static long hppa_r28;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100763#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100764static long sh_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100765#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100766static long sh64_r9;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100767#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100768static long cris_r10;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100769#elif defined(TILE)
770struct pt_regs tile_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100771#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100772static long microblaze_r3;
Christian Svensson492f81f2013-02-14 13:26:27 +0100773#elif defined(OR1K)
774static struct user_regs_struct or1k_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000775# define ARCH_REGS_FOR_GETREGSET or1k_regs
James Hogan5f999a82013-02-22 14:44:10 +0000776#elif defined(METAG)
777static struct user_gp_regs metag_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000778# define ARCH_REGS_FOR_GETREGSET metag_regs
Chris Zankel8f636ed2013-03-25 10:22:07 -0700779#elif defined(XTENSA)
780static long xtensa_a2;
Vineet Gupta7daacbb2013-08-16 12:47:06 +0530781# elif defined(ARC)
782static struct user_regs_struct arc_regs;
783# define ARCH_REGS_FOR_GETREGSET arc_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +0100784#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000785
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100786void
Denys Vlasenko5a2483b2013-07-01 12:49:14 +0200787print_pc(struct tcb *tcp)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100788{
789#define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
790 sizeof(long) == 8 ? "[????????????????] " : \
791 NULL /* crash */)
792 if (get_regs_error) {
793 PRINTBADPC;
794 return;
795 }
796#if defined(I386)
797 tprintf("[%08lx] ", i386_regs.eip);
798#elif defined(S390) || defined(S390X)
799 long psw;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200800 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100801 PRINTBADPC;
802 return;
803 }
804# ifdef S390
805 tprintf("[%08lx] ", psw);
806# elif S390X
Dmitry V. Levinddba73e2013-02-05 19:01:58 +0000807 tprintf("[%016lx] ", psw);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100808# endif
809#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100810 if (x86_io.iov_len == sizeof(i386_regs)) {
811 tprintf("[%08x] ", (unsigned) i386_regs.eip);
812 } else {
813# if defined(X86_64)
814 tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
815# elif defined(X32)
816 /* Note: this truncates 64-bit rip to 32 bits */
817 tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip);
818# endif
819 }
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100820#elif defined(IA64)
821 long ip;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200822 if (upeek(tcp->pid, PT_B0, &ip) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100823 PRINTBADPC;
824 return;
825 }
826 tprintf("[%08lx] ", ip);
827#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +0200828 long pc = ppc_regs.nip;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100829# ifdef POWERPC64
830 tprintf("[%016lx] ", pc);
831# else
832 tprintf("[%08lx] ", pc);
833# endif
834#elif defined(M68K)
835 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200836 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100837 tprints("[????????] ");
838 return;
839 }
840 tprintf("[%08lx] ", pc);
841#elif defined(ALPHA)
842 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200843 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100844 tprints("[????????????????] ");
845 return;
846 }
847 tprintf("[%08lx] ", pc);
848#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100849 tprintf("[%08lx] ", sparc_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100850#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100851 tprintf("[%08lx] ", sparc_regs.tpc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100852#elif defined(HPPA)
853 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200854 if (upeek(tcp->pid, PT_IAOQ0, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100855 tprints("[????????] ");
856 return;
857 }
858 tprintf("[%08lx] ", pc);
859#elif defined(MIPS)
860 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200861 if (upeek(tcp->pid, REG_EPC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100862 tprints("[????????] ");
863 return;
864 }
865 tprintf("[%08lx] ", pc);
866#elif defined(SH)
867 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200868 if (upeek(tcp->pid, 4*REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100869 tprints("[????????] ");
870 return;
871 }
872 tprintf("[%08lx] ", pc);
873#elif defined(SH64)
874 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200875 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100876 tprints("[????????????????] ");
877 return;
878 }
879 tprintf("[%08lx] ", pc);
880#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100881 tprintf("[%08lx] ", arm_regs.ARM_pc);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100882#elif defined(AARCH64)
883 /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100884#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100885 tprintf("[%08lx] ", avr32_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100886#elif defined(BFIN)
887 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200888 if (upeek(tcp->pid, PT_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100889 PRINTBADPC;
890 return;
891 }
892 tprintf("[%08lx] ", pc);
893#elif defined(CRISV10)
894 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200895 if (upeek(tcp->pid, 4*PT_IRP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100896 PRINTBADPC;
897 return;
898 }
899 tprintf("[%08lx] ", pc);
900#elif defined(CRISV32)
901 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200902 if (upeek(tcp->pid, 4*PT_ERP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100903 PRINTBADPC;
904 return;
905 }
906 tprintf("[%08lx] ", pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100907#elif defined(TILE)
908# ifdef _LP64
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500909 tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100910# else
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500911 tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100912# endif
Christian Svensson492f81f2013-02-14 13:26:27 +0100913#elif defined(OR1K)
914 tprintf("[%08lx] ", or1k_regs.pc);
James Hogan5f999a82013-02-22 14:44:10 +0000915#elif defined(METAG)
916 tprintf("[%08lx] ", metag_regs.pc);
Chris Zankel8f636ed2013-03-25 10:22:07 -0700917#elif defined(XTENSA)
918 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200919 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Chris Zankel8f636ed2013-03-25 10:22:07 -0700920 PRINTBADPC;
921 return;
922 }
923 tprintf("[%08lx] ", pc);
Vineet Gupta7daacbb2013-08-16 12:47:06 +0530924#elif defined(ARC)
925 tprintf("[%08lx] ", arc_regs.efa);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100926#endif /* architecture */
927}
928
Denys Vlasenko7270de52013-02-21 15:46:34 +0100929/* Shuffle syscall numbers so that we don't have huge gaps in syscall table.
930 * The shuffling should be reversible: shuffle_scno(shuffle_scno(n)) == n.
931 */
932#if defined(ARM) /* So far only ARM needs this */
933static long
934shuffle_scno(unsigned long scno)
935{
936 if (scno <= ARM_LAST_ORDINARY_SYSCALL)
937 return scno;
938
939 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
940 if (scno == 0x000ffff0)
941 return ARM_LAST_ORDINARY_SYSCALL+1;
942 if (scno == ARM_LAST_ORDINARY_SYSCALL+1)
943 return 0x000ffff0;
944
945 /* Is it ARM specific syscall?
946 * Swap with [LAST_ORDINARY+2, LAST_ORDINARY+2 + LAST_SPECIAL] range.
947 */
948 if (scno >= 0x000f0000
949 && scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL
950 ) {
951 return scno - 0x000f0000 + (ARM_LAST_ORDINARY_SYSCALL+2);
952 }
953 if (/* scno >= ARM_LAST_ORDINARY_SYSCALL+2 - always true */ 1
954 && scno <= (ARM_LAST_ORDINARY_SYSCALL+2) + ARM_LAST_SPECIAL_SYSCALL
955 ) {
956 return scno + 0x000f0000 - (ARM_LAST_ORDINARY_SYSCALL+2);
957 }
958
959 return scno;
960}
961#else
962# define shuffle_scno(scno) ((long)(scno))
963#endif
964
965static char*
966undefined_scno_name(struct tcb *tcp)
967{
968 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
969
970 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
971 return buf;
972}
973
Anton Blanchard14d51a62013-06-26 14:42:37 +1000974#ifdef POWERPC
975/*
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200976 * PTRACE_GETREGS was added to the PowerPC kernel in v2.6.23,
977 * we provide a slow fallback for old kernels.
Anton Blanchard14d51a62013-06-26 14:42:37 +1000978 */
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200979static int powerpc_getregs_old(pid_t pid)
Anton Blanchard14d51a62013-06-26 14:42:37 +1000980{
981 int i;
982 long r;
983
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200984 if (iflag) {
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200985 r = upeek(pid, sizeof(long) * PT_NIP, (long *)&ppc_regs.nip);
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200986 if (r)
987 goto out;
988 }
989#ifdef POWERPC64 /* else we never use it */
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200990 r = upeek(pid, sizeof(long) * PT_MSR, (long *)&ppc_regs.msr);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000991 if (r)
992 goto out;
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200993#endif
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200994 r = upeek(pid, sizeof(long) * PT_CCR, (long *)&ppc_regs.ccr);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000995 if (r)
996 goto out;
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200997 r = upeek(pid, sizeof(long) * PT_ORIG_R3, (long *)&ppc_regs.orig_gpr3);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000998 if (r)
999 goto out;
Anton Blanchard14d51a62013-06-26 14:42:37 +10001000 for (i = 0; i <= 8; i++) {
Anton Blanchard9459dfb2013-07-12 12:24:02 +02001001 r = upeek(pid, sizeof(long) * (PT_R0 + i),
1002 (long *)&ppc_regs.gpr[i]);
Anton Blanchard14d51a62013-06-26 14:42:37 +10001003 if (r)
1004 goto out;
1005 }
Denys Vlasenko7f5a1322013-06-28 14:36:39 +02001006 out:
Anton Blanchard14d51a62013-06-26 14:42:37 +10001007 return r;
1008}
1009#endif
1010
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001011#ifndef get_regs
1012long get_regs_error;
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001013
1014#if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001015static void get_regset(pid_t pid)
1016{
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001017/* constant iovec */
1018# if defined(ARM) \
1019 || defined(I386) \
1020 || defined(METAG) \
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301021 || defined(OR1K) \
1022 || defined(ARC)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001023 static struct iovec io = {
1024 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1025 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001026 };
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001027 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1028
1029/* variable iovec */
1030# elif defined(X86_64) || defined(X32)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001031 /* x86_io.iov_base = &x86_regs_union; - already is */
1032 x86_io.iov_len = sizeof(x86_regs_union);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001033 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &x86_io);
1034# elif defined(AARCH64)
1035 /* aarch64_io.iov_base = &arm_regs_union; - already is */
1036 aarch64_io.iov_len = sizeof(arm_regs_union);
1037 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &aarch64_io);
1038# else
1039# warning both PTRACE_GETREGSET and NT_PRSTATUS are available but not yet used
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001040# endif
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001041}
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001042#endif /* PTRACE_GETREGSET && NT_PRSTATUS */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001043
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001044void
1045get_regs(pid_t pid)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001046{
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001047/* PTRACE_GETREGSET only */
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301048# if defined(METAG) || defined(OR1K) || defined(X32) || defined(AARCH64) || defined(ARC)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001049 get_regset(pid);
1050
1051/* PTRACE_GETREGS only */
1052# elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001053 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001054# elif defined(TILE)
1055 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &tile_regs);
1056# elif defined(SPARC) || defined(SPARC64)
1057 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001058# elif defined(POWERPC)
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001059 static bool old_kernel = 0;
1060 if (old_kernel)
1061 goto old;
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001062 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &ppc_regs);
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001063 if (get_regs_error && errno == EIO) {
1064 old_kernel = 1;
1065 old:
Anton Blanchard14d51a62013-06-26 14:42:37 +10001066 get_regs_error = powerpc_getregs_old(pid);
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001067 }
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001068
1069/* try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS */
1070# else
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001071# if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
1072 static int getregset_support;
1073
1074 if (getregset_support >= 0) {
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001075 get_regset(pid);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001076 if (getregset_support > 0)
1077 return;
1078 if (get_regs_error >= 0) {
1079 getregset_support = 1;
1080 return;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001081 }
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001082 if (errno == EPERM || errno == ESRCH)
1083 return;
1084 getregset_support = -1;
1085 }
1086# endif /* PTRACE_GETREGSET && NT_PRSTATUS */
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001087# if defined(ARM)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001088 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &arm_regs);
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001089# elif defined(I386)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001090 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &i386_regs);
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001091# elif defined(X86_64)
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001092 /* Use old method, with unreliable heuristical detection of 32-bitness. */
1093 x86_io.iov_len = sizeof(x86_64_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001094 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &x86_64_regs);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001095 if (!get_regs_error && x86_64_regs.cs == 0x23) {
1096 x86_io.iov_len = sizeof(i386_regs);
1097 /*
1098 * The order is important: i386_regs and x86_64_regs
1099 * are overlaid in memory!
1100 */
1101 i386_regs.ebx = x86_64_regs.rbx;
1102 i386_regs.ecx = x86_64_regs.rcx;
1103 i386_regs.edx = x86_64_regs.rdx;
1104 i386_regs.esi = x86_64_regs.rsi;
1105 i386_regs.edi = x86_64_regs.rdi;
1106 i386_regs.ebp = x86_64_regs.rbp;
1107 i386_regs.eax = x86_64_regs.rax;
1108 /* i386_regs.xds = x86_64_regs.ds; unused by strace */
1109 /* i386_regs.xes = x86_64_regs.es; ditto... */
1110 /* i386_regs.xfs = x86_64_regs.fs; */
1111 /* i386_regs.xgs = x86_64_regs.gs; */
1112 i386_regs.orig_eax = x86_64_regs.orig_rax;
1113 i386_regs.eip = x86_64_regs.rip;
1114 /* i386_regs.xcs = x86_64_regs.cs; */
1115 /* i386_regs.eflags = x86_64_regs.eflags; */
1116 i386_regs.esp = x86_64_regs.rsp;
1117 /* i386_regs.xss = x86_64_regs.ss; */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001118 }
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001119# else
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001120# error unhandled architecture
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001121# endif /* ARM || I386 || X86_64 */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001122# endif
1123}
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001124#endif /* !get_regs */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001125
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001126/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001127 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1128 * 1: ok, continue in trace_syscall_entering().
1129 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001130 * ("????" etc) and bail out.
1131 */
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01001132static int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001133get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001134{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001135 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001136
Denys Vlasenko523635f2012-02-25 02:44:25 +01001137#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001138 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001139 return -1;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001140
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001141 if (s390_gpr2 != -ENOSYS) {
Roland McGrath2f924ca2003-06-26 22:23:28 +00001142 /*
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001143 * Since kernel version 2.5.44 the scno gets passed in gpr2.
Roland McGrath2f924ca2003-06-26 22:23:28 +00001144 */
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001145 scno = s390_gpr2;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001146 } else {
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001147 /*
Michal Ludvig882eda82002-11-11 12:50:47 +00001148 * Old style of "passing" the scno via the SVC instruction.
1149 */
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001150 long psw;
Michal Ludvig882eda82002-11-11 12:50:47 +00001151 long opcode, offset_reg, tmp;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001152 void *svc_addr;
Denys Vlasenko7c9ba8b2011-08-19 19:46:32 +02001153 static const int gpr_offset[16] = {
1154 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
1155 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
1156 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
1157 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
1158 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001159
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001160 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0)
Michal Ludvig882eda82002-11-11 12:50:47 +00001161 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001162 errno = 0;
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001163 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +00001164 if (errno) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001165 perror_msg("peektext(psw-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +00001166 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001167 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001168
1169 /*
1170 * We have to check if the SVC got executed directly or via an
1171 * EXECUTE instruction. In case of EXECUTE it is necessary to do
1172 * instruction decoding to derive the system call number.
1173 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1174 * so that this doesn't work if a SVC opcode is part of an EXECUTE
1175 * opcode. Since there is no way to find out the opcode size this
1176 * is the best we can do...
1177 */
Michal Ludvig882eda82002-11-11 12:50:47 +00001178 if ((opcode & 0xff00) == 0x0a00) {
1179 /* SVC opcode */
1180 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +00001181 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001182 else {
1183 /* SVC got executed by EXECUTE instruction */
1184
1185 /*
1186 * Do instruction decoding of EXECUTE. If you really want to
1187 * understand this, read the Principles of Operations.
1188 */
1189 svc_addr = (void *) (opcode & 0xfff);
1190
1191 tmp = 0;
1192 offset_reg = (opcode & 0x000f0000) >> 16;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001193 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001194 return -1;
1195 svc_addr += tmp;
1196
1197 tmp = 0;
1198 offset_reg = (opcode & 0x0000f000) >> 12;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001199 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001200 return -1;
1201 svc_addr += tmp;
1202
Denys Vlasenkofb036672009-01-23 16:30:26 +00001203 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
Michal Ludvig882eda82002-11-11 12:50:47 +00001204 if (errno)
1205 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001206# if defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +00001207 scno >>= 48;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001208# else
Michal Ludvig882eda82002-11-11 12:50:47 +00001209 scno >>= 16;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001210# endif
Michal Ludvig882eda82002-11-11 12:50:47 +00001211 tmp = 0;
1212 offset_reg = (opcode & 0x00f00000) >> 20;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001213 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001214 return -1;
1215
1216 scno = (scno | tmp) & 0xff;
1217 }
1218 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001219#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001220 scno = ppc_regs.gpr[0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001221# ifdef POWERPC64
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001222 int currpers;
Andreas Schwabd69fa492010-07-12 21:39:57 +02001223
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001224 /* Check for 64/32 bit mode. */
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001225 /* SF is bit 0 of MSR */
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001226 if ((ppc_regs.msr >> 63) & 1)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001227 currpers = 0;
1228 else
1229 currpers = 1;
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001230 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001231# endif
1232#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001233 scno = avr32_regs.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001234#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001235 if (upeek(tcp->pid, PT_ORIG_P0, &scno))
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001236 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001237#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001238 scno = i386_regs.orig_eax;
H.J. Lu35be5812012-04-16 13:00:01 +02001239#elif defined(X86_64) || defined(X32)
1240# ifndef __X32_SYSCALL_BIT
1241# define __X32_SYSCALL_BIT 0x40000000
1242# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001243 int currpers;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001244# if 1
1245 /* GETREGSET of NT_PRSTATUS tells us regset size,
1246 * which unambiguously detects i386.
1247 *
1248 * Linux kernel distinguishes x86-64 and x32 processes
1249 * solely by looking at __X32_SYSCALL_BIT:
1250 * arch/x86/include/asm/compat.h::is_x32_task():
1251 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1252 * return true;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001253 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001254 if (x86_io.iov_len == sizeof(i386_regs)) {
1255 scno = i386_regs.orig_eax;
1256 currpers = 1;
1257 } else {
1258 scno = x86_64_regs.orig_rax;
1259 currpers = 0;
1260 if (scno & __X32_SYSCALL_BIT) {
1261 scno -= __X32_SYSCALL_BIT;
1262 currpers = 2;
1263 }
1264 }
1265# elif 0
1266 /* cs = 0x33 for long mode (native 64 bit and x32)
1267 * cs = 0x23 for compatibility mode (32 bit)
1268 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1269 */
1270 scno = x86_64_regs.orig_rax;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001271 switch (x86_64_regs.cs) {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001272 case 0x23: currpers = 1; break;
H.J. Lu35be5812012-04-16 13:00:01 +02001273 case 0x33:
1274 if (x86_64_regs.ds == 0x2b) {
1275 currpers = 2;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001276 scno &= ~__X32_SYSCALL_BIT;
H.J. Lu35be5812012-04-16 13:00:01 +02001277 } else
1278 currpers = 0;
1279 break;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001280 default:
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001281 fprintf(stderr, "Unknown value CS=0x%08X while "
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001282 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001283 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001284 currpers = current_personality;
1285 break;
1286 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001287# elif 0
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001288 /* This version analyzes the opcode of a syscall instruction.
1289 * (int 0x80 on i386 vs. syscall on x86-64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001290 * It works, but is too complicated, and strictly speaking, unreliable.
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001291 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001292 unsigned long call, rip = x86_64_regs.rip;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001293 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1294 rip -= 2;
1295 errno = 0;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001296 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001297 if (errno)
1298 fprintf(stderr, "ptrace_peektext failed: %s\n",
1299 strerror(errno));
1300 switch (call & 0xffff) {
1301 /* x86-64: syscall = 0x0f 0x05 */
1302 case 0x050f: currpers = 0; break;
1303 /* i386: int 0x80 = 0xcd 0x80 */
1304 case 0x80cd: currpers = 1; break;
1305 default:
1306 currpers = current_personality;
1307 fprintf(stderr,
1308 "Unknown syscall opcode (0x%04X) while "
1309 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001310 "PID=%d\n", (int)call, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001311 break;
1312 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001313# endif
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001314
H.J. Lu35be5812012-04-16 13:00:01 +02001315# ifdef X32
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001316 /* If we are built for a x32 system, then personality 0 is x32
1317 * (not x86_64), and stracing of x86_64 apps is not supported.
1318 * Stracing of i386 apps is still supported.
H.J. Lu085e4282012-04-17 11:05:04 -07001319 */
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001320 if (currpers == 0) {
1321 fprintf(stderr, "syscall_%lu(...) in unsupported "
1322 "64-bit mode of process PID=%d\n",
1323 scno, tcp->pid);
1324 return 0;
H.J. Lu35be5812012-04-16 13:00:01 +02001325 }
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001326 currpers &= ~2; /* map 2,1 to 0,1 */
H.J. Lu35be5812012-04-16 13:00:01 +02001327# endif
H.J. Lu085e4282012-04-17 11:05:04 -07001328 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001329#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001330# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01001331 long psr;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001332 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001333 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
1334 if (ia64_ia32mode) {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001335 if (upeek(tcp->pid, PT_R1, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001336 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001337 } else {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001338 if (upeek(tcp->pid, PT_R15, &scno) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001339 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001340 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001341#elif defined(AARCH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001342 switch (aarch64_io.iov_len) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001343 case sizeof(aarch64_regs):
1344 /* We are in 64-bit mode */
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001345 scno = aarch64_regs.regs[8];
1346 update_personality(tcp, 1);
1347 break;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001348 case sizeof(arm_regs):
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001349 /* We are in 32-bit mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001350 /* Note: we don't support OABI, unlike 32-bit ARM build */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001351 scno = arm_regs.ARM_r7;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001352 update_personality(tcp, 0);
1353 break;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001354 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001355#elif defined(ARM)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001356 if (arm_regs.ARM_ip != 0) {
1357 /* It is not a syscall entry */
1358 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001359 tcp->flags |= TCB_INSYSCALL;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001360 return 0;
1361 }
1362 /* Note: we support only 32-bit CPUs, not 26-bit */
1363
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001364# ifndef STRACE_KNOWS_ONLY_EABI
1365# warning STRACE_KNOWS_ONLY_EABI not set, will PTRACE_PEEKTEXT on every syscall (slower tracing)
1366 if (arm_regs.ARM_cpsr & 0x20)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001367 /* Thumb mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001368 goto scno_in_r7;
1369 /* ARM mode */
1370 /* Check EABI/OABI by examining SVC insn's low 24 bits */
1371 errno = 0;
1372 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1373 if (errno)
1374 return -1;
1375 /* EABI syscall convention? */
1376 if (scno != 0xef000000) {
1377 /* No, it's OABI */
1378 if ((scno & 0x0ff00000) != 0x0f900000) {
1379 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1380 tcp->pid, scno);
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001381 return -1;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001382 }
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001383 /* Fixup the syscall number */
1384 scno &= 0x000fffff;
1385 } else {
1386 scno_in_r7:
1387 scno = arm_regs.ARM_r7;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001388 }
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001389# else
1390 scno = arm_regs.ARM_r7;
1391# endif
Denys Vlasenko7270de52013-02-21 15:46:34 +01001392 scno = shuffle_scno(scno);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001393#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001394 if (upeek(tcp->pid, 4*PT_ORIG_D0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001395 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001396#elif defined(LINUX_MIPSN32)
Roland McGrath542c2c62008-05-20 01:11:56 +00001397 unsigned long long regs[38];
1398
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001399 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001400 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001401 mips_a3 = regs[REG_A3];
1402 mips_r2 = regs[REG_V0];
Roland McGrath542c2c62008-05-20 01:11:56 +00001403
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001404 scno = mips_r2;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001405 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001406 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001407 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001408 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001409 return 0;
1410 }
Roland McGrath542c2c62008-05-20 01:11:56 +00001411 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001412#elif defined(MIPS)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001413 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001414 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001415 if (upeek(tcp->pid, REG_V0, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001416 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001417
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001418 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001419 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001420 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001421 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001422 return 0;
1423 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001424 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001425#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001426 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001427 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001428 if (upeek(tcp->pid, REG_R0, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001429 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001430
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001431 /*
1432 * Do some sanity checks to figure out if it's
1433 * really a syscall entry
1434 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001435 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001436 if (alpha_a3 == 0 || alpha_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001437 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001438 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001439 return 0;
1440 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001441 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001442#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001443 /* Disassemble the syscall trap. */
1444 /* Retrieve the syscall trap instruction. */
Denys Vlasenko46455822013-02-05 17:02:59 +01001445 unsigned long trap;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001446 errno = 0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001447# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001448 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001449 trap >>= 32;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001450# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001451 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001452# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001453 if (errno)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001454 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001455
1456 /* Disassemble the trap to see what personality to use. */
1457 switch (trap) {
1458 case 0x91d02010:
1459 /* Linux/SPARC syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001460 update_personality(tcp, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001461 break;
1462 case 0x91d0206d:
1463 /* Linux/SPARC64 syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001464 update_personality(tcp, 2);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001465 break;
1466 case 0x91d02000:
1467 /* SunOS syscall trap. (pers 1) */
1468 fprintf(stderr, "syscall: SunOS no support\n");
1469 return -1;
1470 case 0x91d02008:
1471 /* Solaris 2.x syscall trap. (per 2) */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001472 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001473 break;
1474 case 0x91d02009:
1475 /* NetBSD/FreeBSD syscall trap. */
1476 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1477 return -1;
1478 case 0x91d02027:
1479 /* Solaris 2.x gettimeofday */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001480 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001481 break;
1482 default:
Denys Vlasenko523635f2012-02-25 02:44:25 +01001483# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001484 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001485# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001486 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001487# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001488 return -1;
1489 }
1490
1491 /* Extract the system call number from the registers. */
1492 if (trap == 0x91d02027)
1493 scno = 156;
1494 else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001495 scno = sparc_regs.u_regs[U_REG_G1];
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001496 if (scno == 0) {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001497 scno = sparc_regs.u_regs[U_REG_O0];
1498 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 +02001499 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001500#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001501 if (upeek(tcp->pid, PT_GR20, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001502 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001503#elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001504 /*
1505 * In the new syscall ABI, the system call number is in R3.
1506 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001507 if (upeek(tcp->pid, 4*(REG_REG0+3), &scno) < 0)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001508 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001509
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001510 if (scno < 0) {
1511 /* Odd as it may seem, a glibc bug has been known to cause
1512 glibc to issue bogus negative syscall numbers. So for
1513 our purposes, make strace print what it *should* have been */
1514 long correct_scno = (scno & 0xff);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001515 if (debug_flag)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001516 fprintf(stderr,
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001517 "Detected glibc bug: bogus system call"
1518 " number = %ld, correcting to %ld\n",
1519 scno,
1520 correct_scno);
1521 scno = correct_scno;
1522 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001523#elif defined(SH64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001524 if (upeek(tcp->pid, REG_SYSCALL, &scno) < 0)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001525 return -1;
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001526 scno &= 0xFFFF;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001527#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001528 if (upeek(tcp->pid, 4*PT_R9, &scno) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001529 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001530#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001531 int currpers;
1532 scno = tile_regs.regs[10];
1533# ifdef __tilepro__
1534 currpers = 1;
1535# else
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001536# ifndef PT_FLAGS_COMPAT
1537# define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1538# endif
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001539 if (tile_regs.flags & PT_FLAGS_COMPAT)
1540 currpers = 1;
1541 else
1542 currpers = 0;
1543# endif
1544 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001545#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001546 if (upeek(tcp->pid, 0, &scno) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001547 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001548#elif defined(OR1K)
1549 scno = or1k_regs.gpr[11];
James Hogan5f999a82013-02-22 14:44:10 +00001550#elif defined(METAG)
1551 scno = metag_regs.dx[0][1]; /* syscall number in D1Re0 (D1.0) */
Chris Zankel8f636ed2013-03-25 10:22:07 -07001552#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001553 if (upeek(tcp->pid, SYSCALL_NR, &scno) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001554 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301555# elif defined(ARC)
1556 scno = arc_regs.scratch.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001557#endif
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001558
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001559 tcp->scno = scno;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001560 if (SCNO_IS_VALID(tcp->scno)) {
1561 tcp->s_ent = &sysent[scno];
1562 tcp->qual_flg = qual_flags[scno];
1563 } else {
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01001564 static const struct_sysent unknown = {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001565 .nargs = MAX_ARGS,
1566 .sys_flags = 0,
1567 .sys_func = printargs,
1568 .sys_name = "unknown", /* not used */
1569 };
1570 tcp->s_ent = &unknown;
1571 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1572 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001573 return 1;
1574}
1575
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02001576/* Called at each syscall entry.
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001577 * Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001578 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1579 * 1: ok, continue in trace_syscall_entering().
1580 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001581 * ("????" etc) and bail out.
1582 */
Roland McGratha4d48532005-06-08 20:45:28 +00001583static int
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001584syscall_fixup_on_sysenter(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001585{
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001586 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001587#if defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001588 if (i386_regs.eax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001589 if (debug_flag)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001590 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1591 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001592 }
H.J. Lu35be5812012-04-16 13:00:01 +02001593#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001594 {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001595 long rax;
1596 if (x86_io.iov_len == sizeof(i386_regs)) {
1597 /* Sign extend from 32 bits */
1598 rax = (int32_t)i386_regs.eax;
1599 } else {
1600 /* Note: in X32 build, this truncates 64 to 32 bits */
1601 rax = x86_64_regs.rax;
1602 }
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001603 if (rax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001604 if (debug_flag)
Denys Vlasenko18beb982011-08-24 16:59:23 +02001605 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1606 return 0;
1607 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001608 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001609#elif defined(M68K)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001610 /* TODO? Eliminate upeek's in arches below like we did in x86 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001611 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001612 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001613 if (m68k_d0 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001614 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001615 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001616 return 0;
1617 }
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001618#elif defined(IA64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001619 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001620 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001621 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001622 return -1;
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001623 if (ia64_ia32mode && ia64_r8 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001624 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001625 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001626 return 0;
1627 }
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001628#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001629 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001630 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001631 if (cris_r10 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001632 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001633 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001634 return 0;
1635 }
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001636#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001637 if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001638 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001639 if (microblaze_r3 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001640 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001641 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001642 return 0;
1643 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001644#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001645 return 1;
1646}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001647
Denys Vlasenko146b9442012-03-18 22:10:48 +01001648static void
1649internal_fork(struct tcb *tcp)
1650{
1651#if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1652# define ARG_FLAGS 1
1653#else
1654# define ARG_FLAGS 0
1655#endif
1656#ifndef CLONE_UNTRACED
1657# define CLONE_UNTRACED 0x00800000
1658#endif
1659 if ((ptrace_setoptions
1660 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1661 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1662 return;
1663
1664 if (!followfork)
1665 return;
1666
1667 if (entering(tcp)) {
1668 /*
1669 * We won't see the new child if clone is called with
1670 * CLONE_UNTRACED, so we keep the same logic with that option
1671 * and don't trace it.
1672 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001673 if ((tcp->s_ent->sys_func == sys_clone)
1674 && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
1675 )
Denys Vlasenko146b9442012-03-18 22:10:48 +01001676 return;
1677 setbpt(tcp);
1678 } else {
1679 if (tcp->flags & TCB_BPTSET)
1680 clearbpt(tcp);
1681 }
1682}
1683
1684#if defined(TCB_WAITEXECVE)
1685static void
1686internal_exec(struct tcb *tcp)
1687{
1688 /* Maybe we have post-execve SIGTRAP suppressed? */
1689 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1690 return; /* yes, no need to do anything */
1691
1692 if (exiting(tcp) && syserror(tcp))
1693 /* Error in execve, no post-execve SIGTRAP expected */
1694 tcp->flags &= ~TCB_WAITEXECVE;
1695 else
1696 tcp->flags |= TCB_WAITEXECVE;
1697}
1698#endif
1699
1700static void
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001701syscall_fixup_for_fork_exec(struct tcb *tcp)
Roland McGrathc1e45922008-05-27 23:18:29 +00001702{
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001703 /*
1704 * We must always trace a few critical system calls in order to
1705 * correctly support following forks in the presence of tracing
1706 * qualifiers.
1707 */
1708 int (*func)();
1709
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001710 func = tcp->s_ent->sys_func;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001711
1712 if ( sys_fork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001713 || sys_vfork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001714 || sys_clone == func
Denys Vlasenko146b9442012-03-18 22:10:48 +01001715 ) {
1716 internal_fork(tcp);
1717 return;
1718 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001719
Denys Vlasenko84703742012-02-25 02:38:52 +01001720#if defined(TCB_WAITEXECVE)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001721 if ( sys_execve == func
Denys Vlasenko84703742012-02-25 02:38:52 +01001722# if defined(SPARC) || defined(SPARC64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001723 || sys_execv == func
Denys Vlasenkoa7949742011-08-21 17:26:55 +02001724# endif
Denys Vlasenko146b9442012-03-18 22:10:48 +01001725 ) {
1726 internal_exec(tcp);
1727 return;
1728 }
Roland McGrathc1e45922008-05-27 23:18:29 +00001729#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001730}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001731
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001732/* Return -1 on error or 1 on success (never 0!) */
Roland McGratha4d48532005-06-08 20:45:28 +00001733static int
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001734get_syscall_args(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001735{
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001736 int i, nargs;
1737
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001738 nargs = tcp->s_ent->nargs;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001739
Denys Vlasenko523635f2012-02-25 02:44:25 +01001740#if defined(S390) || defined(S390X)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001741 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001742 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 +02001743 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001744#elif defined(ALPHA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001745 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001746 if (upeek(tcp->pid, REG_A0+i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001747 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001748#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001749 if (!ia64_ia32mode) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001750 unsigned long *out0, cfm, sof, sol;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001751 long rbs_end;
1752 /* be backwards compatible with kernel < 2.4.4... */
1753# ifndef PT_RBS_END
1754# define PT_RBS_END PT_AR_BSP
1755# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001756
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001757 if (upeek(tcp->pid, PT_RBS_END, &rbs_end) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001758 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001759 if (upeek(tcp->pid, PT_CFM, (long *) &cfm) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001760 return -1;
1761
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001762 sof = (cfm >> 0) & 0x7f;
1763 sol = (cfm >> 7) & 0x7f;
1764 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1765
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001766 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001767 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1768 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1769 return -1;
1770 }
1771 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001772 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1773 PT_R9 /* ECX = out1 */,
1774 PT_R10 /* EDX = out2 */,
1775 PT_R14 /* ESI = out3 */,
1776 PT_R15 /* EDI = out4 */,
1777 PT_R13 /* EBP = out5 */};
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001778
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001779 for (i = 0; i < nargs; ++i) {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001780 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001781 return -1;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001782 /* truncate away IVE sign-extension */
1783 tcp->u_arg[i] &= 0xffffffff;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001784 }
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001785 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001786#elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001787 /* N32 and N64 both use up to six registers. */
1788 unsigned long long regs[38];
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001789
1790 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1791 return -1;
1792
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001793 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001794 tcp->u_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001795# if defined(LINUX_MIPSN32)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001796 tcp->ext_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001797# endif
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001798 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001799#elif defined(MIPS)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001800 if (nargs > 4) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001801 long sp;
1802
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001803 if (upeek(tcp->pid, REG_SP, &sp) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001804 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001805 for (i = 0; i < 4; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001806 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001807 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001808 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001809 (char *)(tcp->u_arg + 4));
1810 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001811 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001812 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001813 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001814 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001815#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001816 (void)i;
1817 (void)nargs;
1818 tcp->u_arg[0] = ppc_regs.orig_gpr3;
1819 tcp->u_arg[1] = ppc_regs.gpr[4];
1820 tcp->u_arg[2] = ppc_regs.gpr[5];
1821 tcp->u_arg[3] = ppc_regs.gpr[6];
1822 tcp->u_arg[4] = ppc_regs.gpr[7];
1823 tcp->u_arg[5] = ppc_regs.gpr[8];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001824#elif defined(SPARC) || defined(SPARC64)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001825 for (i = 0; i < nargs; ++i)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001826 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001827#elif defined(HPPA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001828 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001829 if (upeek(tcp->pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001830 return -1;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001831#elif defined(ARM) || defined(AARCH64)
1832# if defined(AARCH64)
1833 if (tcp->currpers == 1)
1834 for (i = 0; i < nargs; ++i)
1835 tcp->u_arg[i] = aarch64_regs.regs[i];
1836 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001837# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001838 for (i = 0; i < nargs; ++i)
Denys Vlasenko401374e2013-02-06 18:24:39 +01001839 tcp->u_arg[i] = arm_regs.uregs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001840#elif defined(AVR32)
Denys Vlasenkob5b25892011-08-30 19:04:54 +02001841 (void)i;
1842 (void)nargs;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001843 tcp->u_arg[0] = avr32_regs.r12;
1844 tcp->u_arg[1] = avr32_regs.r11;
1845 tcp->u_arg[2] = avr32_regs.r10;
1846 tcp->u_arg[3] = avr32_regs.r9;
1847 tcp->u_arg[4] = avr32_regs.r5;
1848 tcp->u_arg[5] = avr32_regs.r3;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001849#elif defined(BFIN)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001850 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 +02001851
Denys Vlasenko4b887a52011-08-23 13:32:38 +02001852 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001853 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001854 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001855#elif defined(SH)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001856 static const int syscall_regs[MAX_ARGS] = {
1857 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1858 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001859 };
1860
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001861 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001862 if (upeek(tcp->pid, syscall_regs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001863 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001864#elif defined(SH64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001865 int i;
1866 /* Registers used by SH5 Linux system calls for parameters */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001867 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001868
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001869 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001870 if (upeek(tcp->pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001871 return -1;
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001872#elif defined(I386)
1873 (void)i;
1874 (void)nargs;
1875 tcp->u_arg[0] = i386_regs.ebx;
1876 tcp->u_arg[1] = i386_regs.ecx;
1877 tcp->u_arg[2] = i386_regs.edx;
1878 tcp->u_arg[3] = i386_regs.esi;
1879 tcp->u_arg[4] = i386_regs.edi;
1880 tcp->u_arg[5] = i386_regs.ebp;
H.J. Lu35be5812012-04-16 13:00:01 +02001881#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001882 (void)i;
1883 (void)nargs;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001884 if (x86_io.iov_len != sizeof(i386_regs)) {
1885 /* x86-64 or x32 ABI */
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001886 tcp->u_arg[0] = x86_64_regs.rdi;
1887 tcp->u_arg[1] = x86_64_regs.rsi;
1888 tcp->u_arg[2] = x86_64_regs.rdx;
1889 tcp->u_arg[3] = x86_64_regs.r10;
1890 tcp->u_arg[4] = x86_64_regs.r8;
1891 tcp->u_arg[5] = x86_64_regs.r9;
H.J. Lu35be5812012-04-16 13:00:01 +02001892# ifdef X32
1893 tcp->ext_arg[0] = x86_64_regs.rdi;
1894 tcp->ext_arg[1] = x86_64_regs.rsi;
1895 tcp->ext_arg[2] = x86_64_regs.rdx;
1896 tcp->ext_arg[3] = x86_64_regs.r10;
1897 tcp->ext_arg[4] = x86_64_regs.r8;
1898 tcp->ext_arg[5] = x86_64_regs.r9;
1899# endif
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001900 } else {
1901 /* i386 ABI */
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001902 /* Zero-extend from 32 bits */
1903 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1904 * if you need to use *sign-extended* parameter.
1905 */
1906 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1907 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1908 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1909 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1910 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1911 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001912 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001913#elif defined(MICROBLAZE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001914 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001915 if (upeek(tcp->pid, (5 + i) * 4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001916 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001917#elif defined(CRISV10) || defined(CRISV32)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001918 static const int crisregs[MAX_ARGS] = {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001919 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001920 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001921 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001922
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001923 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001924 if (upeek(tcp->pid, crisregs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001925 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001926#elif defined(TILE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001927 for (i = 0; i < nargs; ++i)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001928 tcp->u_arg[i] = tile_regs.regs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001929#elif defined(M68K)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001930 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001931 if (upeek(tcp->pid, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001932 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001933#elif defined(OR1K)
1934 (void)nargs;
1935 for (i = 0; i < 6; ++i)
1936 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
James Hogan5f999a82013-02-22 14:44:10 +00001937#elif defined(METAG)
1938 for (i = 0; i < nargs; i++)
1939 /* arguments go backwards from D1Ar1 (D1.3) */
1940 tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
Chris Zankel8f636ed2013-03-25 10:22:07 -07001941#elif defined(XTENSA)
1942 /* arg0: a6, arg1: a3, arg2: a4, arg3: a5, arg4: a8, arg5: a9 */
1943 static const int xtensaregs[MAX_ARGS] = { 6, 3, 4, 5, 8, 9 };
1944 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001945 if (upeek(tcp->pid, REG_A_BASE + xtensaregs[i], &tcp->u_arg[i]) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001946 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301947# elif defined(ARC)
1948 long *arc_args = &arc_regs.scratch.r0;
1949 for (i = 0; i < nargs; ++i)
1950 tcp->u_arg[i] = *arc_args--;
1951
Denys Vlasenko523635f2012-02-25 02:44:25 +01001952#else /* Other architecture (32bits specific) */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001953 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001954 if (upeek(tcp->pid, i*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001955 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001956#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001957 return 1;
1958}
1959
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00001960static int
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001961trace_syscall_entering(struct tcb *tcp)
1962{
1963 int res, scno_good;
1964
Denys Vlasenko2ce12ed2011-08-24 17:25:32 +02001965#if defined TCB_WAITEXECVE
1966 if (tcp->flags & TCB_WAITEXECVE) {
1967 /* This is the post-execve SIGTRAP. */
1968 tcp->flags &= ~TCB_WAITEXECVE;
1969 return 0;
1970 }
1971#endif
1972
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001973 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001974 if (res == 0)
1975 return res;
Denys Vlasenko907735a2012-03-21 00:23:16 +01001976 if (res == 1) {
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001977 res = syscall_fixup_on_sysenter(tcp);
Denys Vlasenko907735a2012-03-21 00:23:16 +01001978 if (res == 0)
1979 return res;
1980 if (res == 1)
1981 res = get_syscall_args(tcp);
1982 }
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001983
1984 if (res != 1) {
1985 printleader(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001986 if (scno_good != 1)
Denys Vlasenkob7a6dae2012-03-20 16:48:35 +01001987 tprints("????" /* anti-trigraph gap */ "(");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001988 else if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01001989 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001990 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001991 tprintf("%s(", tcp->s_ent->sys_name);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001992 /*
1993 * " <unavailable>" will be added later by the code which
1994 * detects ptrace errors.
1995 */
1996 goto ret;
1997 }
1998
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02001999 if ( sys_execve == tcp->s_ent->sys_func
2000# if defined(SPARC) || defined(SPARC64)
2001 || sys_execv == tcp->s_ent->sys_func
2002# endif
2003 ) {
2004 hide_log_until_execve = 0;
2005 }
2006
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002007#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002008 while (1) {
Denys Vlasenko523635f2012-02-25 02:44:25 +01002009# ifdef SYS_socket_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002010 if (tcp->s_ent->sys_func == sys_socketcall) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00002011 decode_socket_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002012 break;
2013 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002014# endif
2015# ifdef SYS_ipc_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002016 if (tcp->s_ent->sys_func == sys_ipc) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00002017 decode_ipc_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002018 break;
2019 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002020# endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002021 break;
2022 }
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002023#endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002024
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01002025 if (need_fork_exec_workarounds)
2026 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002027
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002028 if (!(tcp->qual_flg & QUAL_TRACE)
2029 || (tracing_paths && !pathtrace_match(tcp))
2030 ) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002031 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
2032 return 0;
2033 }
2034
2035 tcp->flags &= ~TCB_FILTERED;
2036
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002037 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002038 res = 0;
2039 goto ret;
2040 }
2041
2042 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002043 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002044 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002045 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002046 tprintf("%s(", tcp->s_ent->sys_name);
2047 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002048 res = printargs(tcp);
2049 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002050 res = tcp->s_ent->sys_func(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002051
Dmitry V. Levinb742d8c2012-09-17 22:40:12 +00002052 fflush(tcp->outf);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002053 ret:
2054 tcp->flags |= TCB_INSYSCALL;
2055 /* Measure the entrance time as late as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002056 if (Tflag || cflag)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002057 gettimeofday(&tcp->etime, NULL);
2058 return res;
2059}
2060
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002061/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01002062 * 1: ok, continue in trace_syscall_exiting().
2063 * -1: error, trace_syscall_exiting() should print error indicator
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002064 * ("????" etc) and bail out.
2065 */
2066static int
2067get_syscall_result(struct tcb *tcp)
2068{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002069#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002070 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002071 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002072#elif defined(POWERPC)
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002073 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002074#elif defined(AVR32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002075 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002076#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002077 if (upeek(tcp->pid, PT_R0, &bfin_r0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002078 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002079#elif defined(I386)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002080 /* already done by get_regs */
H.J. Lu35be5812012-04-16 13:00:01 +02002081#elif defined(X86_64) || defined(X32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002082 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002083#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002084# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01002085 long psr;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002086 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02002087 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002088 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002089 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002090 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002091 return -1;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002092#elif defined(ARM)
2093 /* already done by get_regs */
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002094#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002095 /* register reading already done by get_regs */
2096
2097 /* Used to do this, but we did it on syscall entry already: */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002098 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
2099 * else it's personality 0.
2100 */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002101 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
Denys Vlasenko523635f2012-02-25 02:44:25 +01002102#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002103 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002104 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002105#elif defined(LINUX_MIPSN32)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002106 unsigned long long regs[38];
2107
2108 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
2109 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002110 mips_a3 = regs[REG_A3];
2111 mips_r2 = regs[REG_V0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01002112#elif defined(MIPS)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002113 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002114 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002115 if (upeek(tcp->pid, REG_V0, &mips_r2) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002116 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002117#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002118 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002119 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002120 if (upeek(tcp->pid, REG_R0, &alpha_r0) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002121 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002122#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002123 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002124#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002125 if (upeek(tcp->pid, PT_GR28, &hppa_r28) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002126 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002127#elif defined(SH)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002128 /* new syscall ABI returns result in R0 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002129 if (upeek(tcp->pid, 4*REG_REG0, (long *)&sh_r0) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002130 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002131#elif defined(SH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002132 /* ABI defines result returned in r9 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002133 if (upeek(tcp->pid, REG_GENERAL(9), (long *)&sh64_r9) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002134 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002135#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002136 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002137 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002138#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002139 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002140#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002141 if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002142 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01002143#elif defined(OR1K)
2144 /* already done by get_regs */
James Hogan5f999a82013-02-22 14:44:10 +00002145#elif defined(METAG)
2146 /* already done by get_regs */
Chris Zankel8f636ed2013-03-25 10:22:07 -07002147#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002148 if (upeek(tcp->pid, REG_A_BASE + 2, &xtensa_a2) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07002149 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05302150#elif defined(ARC)
2151 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002152#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002153 return 1;
2154}
2155
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002156/* Called at each syscall exit */
2157static void
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002158syscall_fixup_on_sysexit(struct tcb *tcp)
2159{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002160#if defined(S390) || defined(S390X)
Denys Vlasenkoece98792011-08-25 10:25:35 +02002161 if ((tcp->flags & TCB_WAITEXECVE)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002162 && (s390_gpr2 == -ENOSYS || s390_gpr2 == tcp->scno)) {
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002163 /*
2164 * Return from execve.
2165 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2166 * flag set for the post-execve SIGTRAP to see and reset.
2167 */
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002168 s390_gpr2 = 0;
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002169 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002170#endif
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002171}
2172
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002173/*
2174 * Check the syscall return value register value for whether it is
2175 * a negated errno code indicating an error, or a success return value.
2176 */
2177static inline int
2178is_negated_errno(unsigned long int val)
2179{
2180 unsigned long int max = -(long int) nerrnos;
Denys Vlasenko2544f982013-02-19 17:39:56 +01002181#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01002182 if (current_wordsize < sizeof(val)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002183 val = (unsigned int) val;
2184 max = (unsigned int) max;
2185 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002186#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002187 return val > max;
2188}
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002189
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002190#if defined(X32)
2191static inline int
2192is_negated_errno_x32(unsigned long long val)
2193{
2194 unsigned long long max = -(long long) nerrnos;
2195 /*
2196 * current_wordsize is 4 even in personality 0 (native X32)
2197 * but truncation _must not_ be done in it.
2198 * can't check current_wordsize here!
2199 */
2200 if (current_personality != 0) {
2201 val = (uint32_t) val;
2202 max = (uint32_t) max;
2203 }
2204 return val > max;
2205}
2206#endif
2207
Denys Vlasenko907735a2012-03-21 00:23:16 +01002208/* Returns:
2209 * 1: ok, continue in trace_syscall_exiting().
2210 * -1: error, trace_syscall_exiting() should print error indicator
2211 * ("????" etc) and bail out.
2212 */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002213static void
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002214get_error(struct tcb *tcp)
2215{
2216 int u_error = 0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002217 int check_errno = 1;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002218 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002219 check_errno = 0;
2220 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002221#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002222 if (check_errno && is_negated_errno(s390_gpr2)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002223 tcp->u_rval = -1;
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002224 u_error = -s390_gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002225 }
2226 else {
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002227 tcp->u_rval = s390_gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002228 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002229#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002230 if (check_errno && is_negated_errno(i386_regs.eax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002231 tcp->u_rval = -1;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002232 u_error = -i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002233 }
2234 else {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002235 tcp->u_rval = i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002236 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002237#elif defined(X86_64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002238 long rax;
2239 if (x86_io.iov_len == sizeof(i386_regs)) {
2240 /* Sign extend from 32 bits */
2241 rax = (int32_t)i386_regs.eax;
2242 } else {
2243 rax = x86_64_regs.rax;
2244 }
2245 if (check_errno && is_negated_errno(rax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002246 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002247 u_error = -rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002248 }
2249 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002250 tcp->u_rval = rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002251 }
2252#elif defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002253 /* In X32, return value is 64-bit (llseek uses one).
2254 * Using merely "long rax" would not work.
2255 */
2256 long long rax;
2257 if (x86_io.iov_len == sizeof(i386_regs)) {
2258 /* Sign extend from 32 bits */
2259 rax = (int32_t)i386_regs.eax;
2260 } else {
2261 rax = x86_64_regs.rax;
2262 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002263 /* Careful: is_negated_errno() works only on longs */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002264 if (check_errno && is_negated_errno_x32(rax)) {
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002265 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002266 u_error = -rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002267 }
2268 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002269 tcp->u_rval = rax; /* truncating */
2270 tcp->u_lrval = rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002271 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002272#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02002273 if (ia64_ia32mode) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002274 int err;
2275
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002276 err = (int)ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002277 if (check_errno && is_negated_errno(err)) {
2278 tcp->u_rval = -1;
2279 u_error = -err;
2280 }
2281 else {
2282 tcp->u_rval = err;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002283 }
2284 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002285 if (check_errno && ia64_r10) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002286 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002287 u_error = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002288 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002289 tcp->u_rval = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002290 }
2291 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002292#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002293 if (check_errno && mips_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002294 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002295 u_error = mips_r2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002296 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002297 tcp->u_rval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002298# if defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002299 tcp->u_lrval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002300# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002301 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002302#elif defined(POWERPC)
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002303 if (check_errno && (ppc_regs.ccr & 0x10000000)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002304 tcp->u_rval = -1;
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002305 u_error = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002306 }
2307 else {
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002308 tcp->u_rval = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002309 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002310#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002311 if (check_errno && is_negated_errno(m68k_d0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002312 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002313 u_error = -m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002314 }
2315 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002316 tcp->u_rval = m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002317 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002318#elif defined(ARM) || defined(AARCH64)
2319# if defined(AARCH64)
2320 if (tcp->currpers == 1) {
2321 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2322 tcp->u_rval = -1;
2323 u_error = -aarch64_regs.regs[0];
2324 }
2325 else {
2326 tcp->u_rval = aarch64_regs.regs[0];
2327 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002328 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002329 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002330# endif
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002331 {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002332 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002333 tcp->u_rval = -1;
Denys Vlasenko401374e2013-02-06 18:24:39 +01002334 u_error = -arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002335 }
2336 else {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002337 tcp->u_rval = arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002338 }
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002339 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002340#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002341 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002342 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002343 u_error = -avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002344 }
2345 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002346 tcp->u_rval = avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002347 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002348#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002349 if (check_errno && is_negated_errno(bfin_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002350 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002351 u_error = -bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002352 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002353 tcp->u_rval = bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002354 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002355#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002356 if (check_errno && alpha_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002357 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002358 u_error = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002359 }
2360 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002361 tcp->u_rval = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002362 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002363#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002364 if (check_errno && sparc_regs.psr & PSR_C) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002365 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002366 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002367 }
2368 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002369 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002370 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002371#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002372 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002373 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002374 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002375 }
2376 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002377 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002378 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002379#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002380 if (check_errno && is_negated_errno(hppa_r28)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002381 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002382 u_error = -hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002383 }
2384 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002385 tcp->u_rval = hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002386 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002387#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002388 if (check_errno && is_negated_errno(sh_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002389 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002390 u_error = -sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002391 }
2392 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002393 tcp->u_rval = sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002394 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002395#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002396 if (check_errno && is_negated_errno(sh64_r9)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002397 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002398 u_error = -sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002399 }
2400 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002401 tcp->u_rval = sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002402 }
James Hogan5f999a82013-02-22 14:44:10 +00002403#elif defined(METAG)
2404 /* result pointer in D0Re0 (D0.0) */
2405 if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2406 tcp->u_rval = -1;
2407 u_error = -metag_regs.dx[0][0];
2408 }
2409 else {
2410 tcp->u_rval = metag_regs.dx[0][0];
2411 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002412#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002413 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002414 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002415 u_error = -cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002416 }
2417 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002418 tcp->u_rval = cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002419 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002420#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002421 /*
2422 * The standard tile calling convention returns the value (or negative
2423 * errno) in r0, and zero (or positive errno) in r1.
2424 * Until at least kernel 3.8, however, the r1 value is not reflected
2425 * in ptregs at this point, so we use r0 here.
2426 */
2427 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002428 tcp->u_rval = -1;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002429 u_error = -tile_regs.regs[0];
2430 } else {
2431 tcp->u_rval = tile_regs.regs[0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002432 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002433#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002434 if (check_errno && is_negated_errno(microblaze_r3)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002435 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002436 u_error = -microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002437 }
2438 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002439 tcp->u_rval = microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002440 }
Christian Svensson492f81f2013-02-14 13:26:27 +01002441#elif defined(OR1K)
2442 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2443 tcp->u_rval = -1;
2444 u_error = -or1k_regs.gpr[11];
2445 }
2446 else {
2447 tcp->u_rval = or1k_regs.gpr[11];
2448 }
Chris Zankel8f636ed2013-03-25 10:22:07 -07002449#elif defined(XTENSA)
2450 if (check_errno && is_negated_errno(xtensa_a2)) {
2451 tcp->u_rval = -1;
2452 u_error = -xtensa_a2;
2453 }
2454 else {
2455 tcp->u_rval = xtensa_a2;
2456 }
Vineet Gupta7daacbb2013-08-16 12:47:06 +05302457#elif defined(ARC)
2458 if (check_errno && is_negated_errno(arc_regs.scratch.r0)) {
2459 tcp->u_rval = -1;
2460 u_error = -arc_regs.scratch.r0;
2461 }
2462 else {
2463 tcp->u_rval = arc_regs.scratch.r0;
2464 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002465#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002466 tcp->u_error = u_error;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002467}
2468
2469static void
2470dumpio(struct tcb *tcp)
2471{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002472 int (*func)();
2473
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002474 if (syserror(tcp))
2475 return;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +01002476 if ((unsigned long) tcp->u_arg[0] >= num_quals)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002477 return;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002478 func = tcp->s_ent->sys_func;
2479 if (func == printargs)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002480 return;
2481 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002482 if (func == sys_read ||
2483 func == sys_pread ||
2484 func == sys_recv ||
2485 func == sys_recvfrom)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002486 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002487 else if (func == sys_readv)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002488 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2489 return;
2490 }
2491 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002492 if (func == sys_write ||
2493 func == sys_pwrite ||
2494 func == sys_send ||
2495 func == sys_sendto)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002496 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002497 else if (func == sys_writev)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002498 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2499 return;
2500 }
2501}
2502
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002503static int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002504trace_syscall_exiting(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002505{
2506 int sys_res;
2507 struct timeval tv;
Denys Vlasenko1a5b5a72011-08-25 00:29:56 +02002508 int res;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002509 long u_error;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002510
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002511 /* Measure the exit time as early as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002512 if (Tflag || cflag)
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002513 gettimeofday(&tv, NULL);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002514
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00002515#if SUPPORTED_PERSONALITIES > 1
2516 update_personality(tcp, tcp->currpers);
2517#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002518 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002519 if (res == 1) {
2520 syscall_fixup_on_sysexit(tcp); /* never fails */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002521 get_error(tcp); /* never fails */
2522 if (need_fork_exec_workarounds)
2523 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002524 if (filtered(tcp) || hide_log_until_execve)
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002525 goto ret;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002526 }
2527
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002528 if (cflag) {
2529 struct timeval t = tv;
Denys Vlasenkoc95a88f2011-08-21 17:47:40 +02002530 count_syscall(tcp, &t);
Denys Vlasenko7b609d52011-06-22 14:32:43 +02002531 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002532 goto ret;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002533 }
2534 }
2535
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002536 /* If not in -ff mode, and printing_tcp != tcp,
2537 * then the log currently does not end with output
2538 * of _our syscall entry_, but with something else.
2539 * We need to say which syscall's return is this.
2540 *
2541 * Forced reprinting via TCB_REPRINT is used only by
2542 * "strace -ff -oLOG test/threaded_execve" corner case.
2543 * It's the only case when -ff mode needs reprinting.
2544 */
2545 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2546 tcp->flags &= ~TCB_REPRINT;
2547 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002548 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002549 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002550 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002551 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002552 }
2553 printing_tcp = tcp;
2554
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002555 if (res != 1) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002556 /* There was error in one of prior ptrace ops */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002557 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002558 tabto();
Denys Vlasenko000b6012012-01-28 01:25:03 +01002559 tprints("= ? <unavailable>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002560 line_ended();
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002561 tcp->flags &= ~TCB_INSYSCALL;
2562 return res;
2563 }
2564
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002565 sys_res = 0;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002566 if (tcp->qual_flg & QUAL_RAW) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002567 /* sys_res = printargs(tcp); - but it's nop on sysexit */
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002568 } else {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002569 /* FIXME: not_failing_only (IOW, option -z) is broken:
2570 * failure of syscall is known only after syscall return.
2571 * Thus we end up with something like this on, say, ENOENT:
2572 * open("doesnt_exist", O_RDONLY <unfinished ...>
2573 * {next syscall decode}
2574 * whereas the intended result is that open(...) line
2575 * is not shown at all.
2576 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002577 if (not_failing_only && tcp->u_error)
Denys Vlasenko3b738812011-08-22 02:06:35 +02002578 goto ret; /* ignore failed syscalls */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002579 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002580 }
2581
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002582 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002583 tabto();
Denys Vlasenko3b738812011-08-22 02:06:35 +02002584 u_error = tcp->u_error;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002585 if (tcp->qual_flg & QUAL_RAW) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002586 if (u_error)
2587 tprintf("= -1 (errno %ld)", u_error);
2588 else
2589 tprintf("= %#lx", tcp->u_rval);
2590 }
2591 else if (!(sys_res & RVAL_NONE) && u_error) {
2592 switch (u_error) {
Denys Vlasenkofe585652012-01-12 11:26:34 +01002593 /* Blocked signals do not interrupt any syscalls.
2594 * In this case syscalls don't return ERESTARTfoo codes.
2595 *
2596 * Deadly signals set to SIG_DFL interrupt syscalls
2597 * and kill the process regardless of which of the codes below
2598 * is returned by the interrupted syscall.
2599 * In some cases, kernel forces a kernel-generated deadly
2600 * signal to be unblocked and set to SIG_DFL (and thus cause
2601 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2602 * or SIGILL. (The alternative is to leave process spinning
2603 * forever on the faulty instruction - not useful).
2604 *
2605 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2606 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2607 * but kernel will always restart them.
2608 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002609 case ERESTARTSYS:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002610 /* Most common type of signal-interrupted syscall exit code.
2611 * The system call will be restarted with the same arguments
2612 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2613 */
2614 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002615 break;
2616 case ERESTARTNOINTR:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002617 /* Rare. For example, fork() returns this if interrupted.
2618 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2619 */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002620 tprints("= ? ERESTARTNOINTR (To be restarted)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002621 break;
2622 case ERESTARTNOHAND:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002623 /* pause(), rt_sigsuspend() etc use this code.
2624 * SA_RESTART is ignored (assumed not set):
2625 * syscall won't restart (will return EINTR instead)
Denys Vlasenko30c03232013-02-19 16:59:26 +01002626 * even after signal with SA_RESTART set. However,
2627 * after SIG_IGN or SIG_DFL signal it will restart
2628 * (thus the name "restart only if has no handler").
Denys Vlasenkofe585652012-01-12 11:26:34 +01002629 */
Denys Vlasenkoaba62922013-03-05 16:56:35 +01002630 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002631 break;
2632 case ERESTART_RESTARTBLOCK:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002633 /* Syscalls like nanosleep(), poll() which can't be
2634 * restarted with their original arguments use this
2635 * code. Kernel will execute restart_syscall() instead,
2636 * which changes arguments before restarting syscall.
2637 * SA_RESTART is ignored (assumed not set) similarly
2638 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2639 * since restart data is saved in "restart block"
2640 * in task struct, and if signal handler uses a syscall
2641 * which in turn saves another such restart block,
2642 * old data is lost and restart becomes impossible)
2643 */
2644 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002645 break;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002646 default:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002647 if (u_error < 0)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002648 tprintf("= -1 E??? (errno %ld)", u_error);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002649 else if (u_error < nerrnos)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002650 tprintf("= -1 %s (%s)", errnoent[u_error],
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002651 strerror(u_error));
2652 else
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002653 tprintf("= -1 ERRNO_%ld (%s)", u_error,
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002654 strerror(u_error));
2655 break;
2656 }
2657 if ((sys_res & RVAL_STR) && tcp->auxstr)
2658 tprintf(" (%s)", tcp->auxstr);
2659 }
2660 else {
2661 if (sys_res & RVAL_NONE)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002662 tprints("= ?");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002663 else {
2664 switch (sys_res & RVAL_MASK) {
2665 case RVAL_HEX:
2666 tprintf("= %#lx", tcp->u_rval);
2667 break;
2668 case RVAL_OCTAL:
2669 tprintf("= %#lo", tcp->u_rval);
2670 break;
2671 case RVAL_UDECIMAL:
2672 tprintf("= %lu", tcp->u_rval);
2673 break;
2674 case RVAL_DECIMAL:
2675 tprintf("= %ld", tcp->u_rval);
2676 break;
H.J. Ludd0130b2012-04-16 12:16:45 +02002677#if defined(LINUX_MIPSN32) || defined(X32)
2678 /*
2679 case RVAL_LHEX:
2680 tprintf("= %#llx", tcp->u_lrval);
2681 break;
2682 case RVAL_LOCTAL:
2683 tprintf("= %#llo", tcp->u_lrval);
2684 break;
2685 */
2686 case RVAL_LUDECIMAL:
2687 tprintf("= %llu", tcp->u_lrval);
2688 break;
2689 /*
2690 case RVAL_LDECIMAL:
2691 tprintf("= %lld", tcp->u_lrval);
2692 break;
2693 */
2694#endif
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002695 default:
2696 fprintf(stderr,
2697 "invalid rval format\n");
2698 break;
2699 }
2700 }
2701 if ((sys_res & RVAL_STR) && tcp->auxstr)
2702 tprintf(" (%s)", tcp->auxstr);
2703 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002704 if (Tflag) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002705 tv_sub(&tv, &tv, &tcp->etime);
2706 tprintf(" <%ld.%06ld>",
2707 (long) tv.tv_sec, (long) tv.tv_usec);
2708 }
Denys Vlasenko000b6012012-01-28 01:25:03 +01002709 tprints("\n");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002710 dumpio(tcp);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002711 line_ended();
2712
Denys Vlasenko3b738812011-08-22 02:06:35 +02002713 ret:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002714 tcp->flags &= ~TCB_INSYSCALL;
2715 return 0;
2716}
2717
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002718int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002719trace_syscall(struct tcb *tcp)
2720{
2721 return exiting(tcp) ?
2722 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2723}