blob: fa06156b11f01dbbe3e4099bccbf051437c5b73c [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
Denys Vlasenko523635f2012-02-25 02:44:25 +010051# include <linux/ptrace.h>
Roland McGrathce9f0742004-03-01 21:29:22 +000052# undef ia64_fpreg
53# undef pt_all_user_regs
Wichert Akkerman15dea971999-10-06 13:06:34 +000054#endif
55
Denys Vlasenko84703742012-02-25 02:38:52 +010056#if defined(SPARC64)
Roland McGrath6d1a65c2004-07-12 07:44:08 +000057# undef PTRACE_GETREGS
58# define PTRACE_GETREGS PTRACE_GETREGS64
59# undef PTRACE_SETREGS
60# define PTRACE_SETREGS PTRACE_SETREGS64
Denys Vlasenko84703742012-02-25 02:38:52 +010061#endif
Roland McGrath6d1a65c2004-07-12 07:44:08 +000062
Denys Vlasenko84703742012-02-25 02:38:52 +010063#if defined(IA64)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000064# include <asm/ptrace_offsets.h>
65# include <asm/rse.h>
66#endif
67
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010068#if defined(X86_64) || defined(X32)
69# include <linux/ptrace.h>
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010070# include <sys/uio.h>
71# include <elf.h>
72#endif
73
Steve McIntyred8d3bd32012-10-24 17:58:16 +010074#if defined(AARCH64)
75# include <asm/ptrace.h>
76# include <sys/uio.h>
77# include <elf.h>
78#endif
79
Christian Svensson492f81f2013-02-14 13:26:27 +010080#if defined(OR1K)
81# include <sys/uio.h>
82# include <elf.h>
83#endif
84
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000085#ifndef ERESTARTSYS
Denys Vlasenko523635f2012-02-25 02:44:25 +010086# define ERESTARTSYS 512
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000087#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010088#ifndef ERESTARTNOINTR
Denys Vlasenko523635f2012-02-25 02:44:25 +010089# define ERESTARTNOINTR 513
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000090#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010091#ifndef ERESTARTNOHAND
92# define ERESTARTNOHAND 514 /* restart if no handler */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000093#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010094#ifndef ERESTART_RESTARTBLOCK
Denys Vlasenko523635f2012-02-25 02:44:25 +010095# define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
Roland McGrath9c555e72003-07-09 09:47:59 +000096#endif
Denys Vlasenko523635f2012-02-25 02:44:25 +010097
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000098#ifndef NSIG
Denys Vlasenko523635f2012-02-25 02:44:25 +010099# warning: NSIG is not defined, using 32
100# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000101#endif
102#ifdef ARM
Denys Vlasenko041b3ee2011-08-18 12:48:56 +0200103/* Ugh. Is this really correct? ARM has no RT signals?! */
Denys Vlasenko523635f2012-02-25 02:44:25 +0100104# undef NSIG
105# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000106#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000107
108#include "syscall.h"
109
110/* Define these shorthand notations to simplify the syscallent files. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000111#define TD TRACE_DESC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000112#define TF TRACE_FILE
113#define TI TRACE_IPC
114#define TN TRACE_NETWORK
115#define TP TRACE_PROCESS
116#define TS TRACE_SIGNAL
Namhyung Kim96792962012-10-24 11:41:57 +0900117#define TM TRACE_MEMORY
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000118#define NF SYSCALL_NEVER_FAILS
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200119#define MA MAX_ARGS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000120
Roland McGrathee36ce12004-09-04 03:53:10 +0000121static const struct sysent sysent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000122#include "syscallent.h"
123};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000124
125#if SUPPORTED_PERSONALITIES >= 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000126static const struct sysent sysent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100127# include "syscallent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000128};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200129#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000130
131#if SUPPORTED_PERSONALITIES >= 3
Roland McGrathee36ce12004-09-04 03:53:10 +0000132static const struct sysent sysent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100133# include "syscallent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000134};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200135#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000136
137/* Now undef them since short defines cause wicked namespace pollution. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000138#undef TD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000139#undef TF
140#undef TI
141#undef TN
142#undef TP
143#undef TS
Namhyung Kim96792962012-10-24 11:41:57 +0900144#undef TM
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000145#undef NF
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200146#undef MA
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000147
Denys Vlasenko39fca622011-08-20 02:12:33 +0200148/*
149 * `ioctlent.h' may be generated from `ioctlent.raw' by the auxiliary
150 * program `ioctlsort', such that the list is sorted by the `code' field.
151 * This has the side-effect of resolving the _IO.. macros into
152 * plain integers, eliminating the need to include here everything
153 * in "/usr/include".
154 */
155
Roland McGrathee36ce12004-09-04 03:53:10 +0000156static const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000157#include "errnoent.h"
158};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200159static const char *const signalent0[] = {
160#include "signalent.h"
161};
162static const struct ioctlent ioctlent0[] = {
163#include "ioctlent.h"
164};
165enum { nsyscalls0 = ARRAY_SIZE(sysent0) };
166enum { nerrnos0 = ARRAY_SIZE(errnoent0) };
167enum { nsignals0 = ARRAY_SIZE(signalent0) };
168enum { nioctlents0 = ARRAY_SIZE(ioctlent0) };
169int qual_flags0[MAX_QUALS];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000170
171#if SUPPORTED_PERSONALITIES >= 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000172static const char *const errnoent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100173# include "errnoent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000174};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200175static const char *const signalent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100176# include "signalent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200177};
178static const struct ioctlent ioctlent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100179# include "ioctlent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200180};
181enum { nsyscalls1 = ARRAY_SIZE(sysent1) };
182enum { nerrnos1 = ARRAY_SIZE(errnoent1) };
183enum { nsignals1 = ARRAY_SIZE(signalent1) };
184enum { nioctlents1 = ARRAY_SIZE(ioctlent1) };
185int qual_flags1[MAX_QUALS];
186#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000187
188#if SUPPORTED_PERSONALITIES >= 3
Roland McGrathee36ce12004-09-04 03:53:10 +0000189static const char *const errnoent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100190# include "errnoent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000191};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200192static const char *const signalent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100193# include "signalent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200194};
195static const struct ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100196# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200197};
198enum { nsyscalls2 = ARRAY_SIZE(sysent2) };
199enum { nerrnos2 = ARRAY_SIZE(errnoent2) };
200enum { nsignals2 = ARRAY_SIZE(signalent2) };
201enum { nioctlents2 = ARRAY_SIZE(ioctlent2) };
202int qual_flags2[MAX_QUALS];
203#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000204
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100205const struct sysent *sysent = sysent0;
206const char *const *errnoent = errnoent0;
207const char *const *signalent = signalent0;
208const struct ioctlent *ioctlent = ioctlent0;
209unsigned nsyscalls = nsyscalls0;
210unsigned nerrnos = nerrnos0;
211unsigned nsignals = nsignals0;
212unsigned nioctlents = nioctlents0;
213int *qual_flags = qual_flags0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000214
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100215#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100216unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000217
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100218# ifndef current_wordsize
219unsigned current_wordsize;
220static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000221 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000222 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100223# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000224 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100225# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200226};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100227# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000228
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200229void
Dmitry V. Levin3abe8b22006-12-20 22:37:21 +0000230set_personality(int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000231{
232 switch (personality) {
233 case 0:
234 errnoent = errnoent0;
235 nerrnos = nerrnos0;
236 sysent = sysent0;
237 nsyscalls = nsyscalls0;
238 ioctlent = ioctlent0;
239 nioctlents = nioctlents0;
240 signalent = signalent0;
241 nsignals = nsignals0;
Roland McGrath138c6a32006-01-12 09:50:49 +0000242 qual_flags = qual_flags0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000243 break;
244
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000245 case 1:
246 errnoent = errnoent1;
247 nerrnos = nerrnos1;
248 sysent = sysent1;
249 nsyscalls = nsyscalls1;
250 ioctlent = ioctlent1;
251 nioctlents = nioctlents1;
252 signalent = signalent1;
253 nsignals = nsignals1;
Roland McGrath138c6a32006-01-12 09:50:49 +0000254 qual_flags = qual_flags1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000255 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000256
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100257# if SUPPORTED_PERSONALITIES >= 3
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000258 case 2:
259 errnoent = errnoent2;
260 nerrnos = nerrnos2;
261 sysent = sysent2;
262 nsyscalls = nsyscalls2;
263 ioctlent = ioctlent2;
264 nioctlents = nioctlents2;
265 signalent = signalent2;
266 nsignals = nsignals2;
Roland McGrath138c6a32006-01-12 09:50:49 +0000267 qual_flags = qual_flags2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000268 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100269# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000270 }
271
272 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100273# ifndef current_wordsize
274 current_wordsize = personality_wordsize[personality];
275# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000276}
277
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000278static void
279update_personality(struct tcb *tcp, int personality)
280{
281 if (personality == current_personality)
282 return;
283 set_personality(personality);
284
285 if (personality == tcp->currpers)
286 return;
287 tcp->currpers = personality;
288
H.J. Lu35be5812012-04-16 13:00:01 +0200289# if defined(POWERPC64)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000290 if (!qflag) {
291 static const char *const names[] = {"64 bit", "32 bit"};
292 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
293 tcp->pid, names[personality]);
294 }
H.J. Lu35be5812012-04-16 13:00:01 +0200295# elif defined(X86_64)
296 if (!qflag) {
297 static const char *const names[] = {"64 bit", "32 bit", "x32"};
298 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
299 tcp->pid, names[personality]);
300 }
H.J. Lu085e4282012-04-17 11:05:04 -0700301# elif defined(X32)
302 if (!qflag) {
303 static const char *const names[] = {"x32", "32 bit"};
304 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
305 tcp->pid, names[personality]);
306 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000307# elif defined(AARCH64)
308 if (!qflag) {
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100309 static const char *const names[] = {"32-bit", "AArch64"};
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000310 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
311 tcp->pid, names[personality]);
312 }
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100313# elif defined(TILE)
314 if (!qflag) {
315 static const char *const names[] = {"64-bit", "32-bit"};
316 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
317 tcp->pid, names[personality]);
318 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100319# endif
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000320}
321#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000322
Roland McGrath9797ceb2002-12-30 10:23:00 +0000323static int qual_syscall(), qual_signal(), qual_fault(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000324
Roland McGrathe10e62a2004-09-04 04:20:43 +0000325static const struct qual_options {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000326 int bitflag;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000327 const char *option_name;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000328 int (*qualify)(const char *, int, int);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000329 const char *argument_name;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000330} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000331 { QUAL_TRACE, "trace", qual_syscall, "system call" },
332 { QUAL_TRACE, "t", qual_syscall, "system call" },
333 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
334 { QUAL_ABBREV, "a", qual_syscall, "system call" },
335 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
336 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
337 { QUAL_RAW, "raw", qual_syscall, "system call" },
338 { QUAL_RAW, "x", qual_syscall, "system call" },
339 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
340 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
341 { QUAL_SIGNAL, "s", qual_signal, "signal" },
342 { QUAL_FAULT, "fault", qual_fault, "fault" },
343 { QUAL_FAULT, "faults", qual_fault, "fault" },
344 { QUAL_FAULT, "m", qual_fault, "fault" },
345 { QUAL_READ, "read", qual_desc, "descriptor" },
346 { QUAL_READ, "reads", qual_desc, "descriptor" },
347 { QUAL_READ, "r", qual_desc, "descriptor" },
348 { QUAL_WRITE, "write", qual_desc, "descriptor" },
349 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
350 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000351 { 0, NULL, NULL, NULL },
352};
353
Roland McGrath9797ceb2002-12-30 10:23:00 +0000354static void
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000355qualify_one(int n, int bitflag, int not, int pers)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000356{
Roland McGrath138c6a32006-01-12 09:50:49 +0000357 if (pers == 0 || pers < 0) {
358 if (not)
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000359 qual_flags0[n] &= ~bitflag;
Roland McGrath138c6a32006-01-12 09:50:49 +0000360 else
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000361 qual_flags0[n] |= bitflag;
Roland McGrath138c6a32006-01-12 09:50:49 +0000362 }
363
364#if SUPPORTED_PERSONALITIES >= 2
365 if (pers == 1 || pers < 0) {
366 if (not)
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000367 qual_flags1[n] &= ~bitflag;
Roland McGrath138c6a32006-01-12 09:50:49 +0000368 else
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000369 qual_flags1[n] |= bitflag;
Roland McGrath138c6a32006-01-12 09:50:49 +0000370 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100371#endif
Roland McGrath138c6a32006-01-12 09:50:49 +0000372
373#if SUPPORTED_PERSONALITIES >= 3
374 if (pers == 2 || pers < 0) {
375 if (not)
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000376 qual_flags2[n] &= ~bitflag;
Roland McGrath138c6a32006-01-12 09:50:49 +0000377 else
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000378 qual_flags2[n] |= bitflag;
Roland McGrath138c6a32006-01-12 09:50:49 +0000379 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100380#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000381}
382
383static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000384qual_syscall(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000385{
386 int i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000387 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000388
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100389 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000390 int i = string_to_uint(s);
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000391 if (i < 0 || i >= MAX_QUALS)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000392 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000393 qualify_one(i, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000394 return 0;
Roland McGrath48a035f2006-01-12 09:45:56 +0000395 }
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000396 for (i = 0; i < nsyscalls0; i++)
Dmitry V. Levin4372cc92012-03-26 14:14:50 +0000397 if (sysent0[i].sys_name &&
398 strcmp(s, sysent0[i].sys_name) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000399 qualify_one(i, bitflag, not, 0);
Roland McGrathfe6b3522005-02-02 04:40:11 +0000400 rc = 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000401 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000402
403#if SUPPORTED_PERSONALITIES >= 2
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000404 for (i = 0; i < nsyscalls1; i++)
Dmitry V. Levin4372cc92012-03-26 14:14:50 +0000405 if (sysent1[i].sys_name &&
406 strcmp(s, sysent1[i].sys_name) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000407 qualify_one(i, bitflag, not, 1);
Roland McGrath138c6a32006-01-12 09:50:49 +0000408 rc = 0;
409 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100410#endif
Roland McGrath138c6a32006-01-12 09:50:49 +0000411
412#if SUPPORTED_PERSONALITIES >= 3
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000413 for (i = 0; i < nsyscalls2; i++)
Dmitry V. Levin4372cc92012-03-26 14:14:50 +0000414 if (sysent2[i].sys_name &&
415 strcmp(s, sysent2[i].sys_name) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000416 qualify_one(i, bitflag, not, 2);
Roland McGrath138c6a32006-01-12 09:50:49 +0000417 rc = 0;
418 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100419#endif
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000420
Roland McGrathfe6b3522005-02-02 04:40:11 +0000421 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000422}
423
424static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000425qual_signal(const char *s, int bitflag, int not)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000426{
427 int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000428
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100429 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000430 int signo = string_to_uint(s);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000431 if (signo < 0 || signo >= MAX_QUALS)
432 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000433 qualify_one(signo, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000434 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000435 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000436 if (strncasecmp(s, "SIG", 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000437 s += 3;
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100438 for (i = 0; i <= NSIG; i++) {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000439 if (strcasecmp(s, signame(i) + 3) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000440 qualify_one(i, bitflag, not, -1);
Roland McGrath76421df2005-02-02 03:51:18 +0000441 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000442 }
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100443 }
Roland McGrath76421df2005-02-02 03:51:18 +0000444 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000445}
446
447static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000448qual_fault(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000449{
450 return -1;
451}
452
453static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000454qual_desc(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000455{
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100456 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000457 int desc = string_to_uint(s);
Roland McGrathfe6b3522005-02-02 04:40:11 +0000458 if (desc < 0 || desc >= MAX_QUALS)
459 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000460 qualify_one(desc, bitflag, not, -1);
Roland McGrath2b619022003-04-10 18:58:20 +0000461 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000462 }
463 return -1;
464}
465
466static int
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000467lookup_class(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000468{
469 if (strcmp(s, "file") == 0)
470 return TRACE_FILE;
471 if (strcmp(s, "ipc") == 0)
472 return TRACE_IPC;
473 if (strcmp(s, "network") == 0)
474 return TRACE_NETWORK;
475 if (strcmp(s, "process") == 0)
476 return TRACE_PROCESS;
477 if (strcmp(s, "signal") == 0)
478 return TRACE_SIGNAL;
Roland McGrath2fe7b132005-07-05 03:25:35 +0000479 if (strcmp(s, "desc") == 0)
480 return TRACE_DESC;
Namhyung Kim96792962012-10-24 11:41:57 +0900481 if (strcmp(s, "memory") == 0)
482 return TRACE_MEMORY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000483 return -1;
484}
485
486void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000487qualify(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000488{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000489 const struct qual_options *opt;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000490 int not;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000491 char *copy;
492 const char *p;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000493 int i, n;
494
495 opt = &qual_options[0];
496 for (i = 0; (p = qual_options[i].option_name); i++) {
497 n = strlen(p);
498 if (strncmp(s, p, n) == 0 && s[n] == '=') {
499 opt = &qual_options[i];
500 s += n + 1;
501 break;
502 }
503 }
504 not = 0;
505 if (*s == '!') {
506 not = 1;
507 s++;
508 }
509 if (strcmp(s, "none") == 0) {
510 not = 1 - not;
511 s = "all";
512 }
513 if (strcmp(s, "all") == 0) {
514 for (i = 0; i < MAX_QUALS; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000515 qualify_one(i, opt->bitflag, not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000516 }
517 return;
518 }
519 for (i = 0; i < MAX_QUALS; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000520 qualify_one(i, opt->bitflag, !not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000521 }
Denys Vlasenko5d645812011-08-20 12:48:18 +0200522 copy = strdup(s);
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200523 if (!copy)
524 die_out_of_memory();
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000525 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000526 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000527 for (i = 0; i < nsyscalls0; i++)
Roland McGrath138c6a32006-01-12 09:50:49 +0000528 if (sysent0[i].sys_flags & n)
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000529 qualify_one(i, opt->bitflag, not, 0);
Roland McGrath138c6a32006-01-12 09:50:49 +0000530
531#if SUPPORTED_PERSONALITIES >= 2
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000532 for (i = 0; i < nsyscalls1; i++)
Roland McGrath138c6a32006-01-12 09:50:49 +0000533 if (sysent1[i].sys_flags & n)
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000534 qualify_one(i, opt->bitflag, not, 1);
Denys Vlasenko523635f2012-02-25 02:44:25 +0100535#endif
Roland McGrath138c6a32006-01-12 09:50:49 +0000536
537#if SUPPORTED_PERSONALITIES >= 3
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000538 for (i = 0; i < nsyscalls2; i++)
Roland McGrath138c6a32006-01-12 09:50:49 +0000539 if (sysent2[i].sys_flags & n)
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000540 qualify_one(i, opt->bitflag, not, 2);
Denys Vlasenko523635f2012-02-25 02:44:25 +0100541#endif
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000542
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000543 continue;
544 }
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000545 if (opt->qualify(p, opt->bitflag, not)) {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100546 error_msg_and_die("invalid %s '%s'",
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000547 opt->argument_name, p);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000548 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000549 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000550 free(copy);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000551 return;
552}
553
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000554#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000555static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000556decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000557{
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000558 unsigned long addr;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100559 unsigned int i, n, size;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000560
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000561 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
562 return;
563
564 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100565 tcp->qual_flg = qual_flags[tcp->scno];
566 tcp->s_ent = &sysent[tcp->scno];
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000567 addr = tcp->u_arg[1];
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100568 size = current_wordsize;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100569 n = tcp->s_ent->nargs;
570 for (i = 0; i < n; ++i) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000571 if (size == sizeof(int)) {
572 unsigned int arg;
573 if (umove(tcp, addr, &arg) < 0)
574 arg = 0;
575 tcp->u_arg[i] = arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000576 }
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000577 else {
578 unsigned long arg;
579 if (umove(tcp, addr, &arg) < 0)
580 arg = 0;
581 tcp->u_arg[i] = arg;
582 }
583 addr += size;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000584 }
585}
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000586#endif
Mike Frysinger3362e892012-03-15 01:09:19 -0400587
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000588#ifdef SYS_ipc_subcall
589static void
590decode_ipc_subcall(struct tcb *tcp)
591{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100592 unsigned int i, n;
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000593
594 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
595 return;
596
597 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100598 tcp->qual_flg = qual_flags[tcp->scno];
599 tcp->s_ent = &sysent[tcp->scno];
600 n = tcp->s_ent->nargs;
601 for (i = 0; i < n; i++)
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000602 tcp->u_arg[i] = tcp->u_arg[i + 1];
603}
604#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000605
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200606int
607printargs(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000608{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200609 if (entering(tcp)) {
610 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100611 int n = tcp->s_ent->nargs;
612 for (i = 0; i < n; i++)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200613 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
614 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000615 return 0;
616}
617
Denys Vlasenko72879c62012-02-27 14:18:02 +0100618int
619printargs_lu(struct tcb *tcp)
620{
621 if (entering(tcp)) {
622 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100623 int n = tcp->s_ent->nargs;
624 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100625 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
626 }
627 return 0;
628}
629
630int
631printargs_ld(struct tcb *tcp)
632{
633 if (entering(tcp)) {
634 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100635 int n = tcp->s_ent->nargs;
636 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100637 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
638 }
639 return 0;
640}
641
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100642#if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200643long
644getrval2(struct tcb *tcp)
645{
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100646 long val;
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200647
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100648# if defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100649 val = sparc_regs.u_regs[U_REG_O1];
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100650# elif defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200651 if (upeek(tcp, 4*(REG_REG0+1), &val) < 0)
652 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100653# elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200654 if (upeek(tcp, PT_R9, &val) < 0)
655 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100656# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200657
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200658 return val;
659}
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100660#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200661
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200662int
663is_restart_error(struct tcb *tcp)
664{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200665 switch (tcp->u_error) {
666 case ERESTARTSYS:
667 case ERESTARTNOINTR:
668 case ERESTARTNOHAND:
669 case ERESTART_RESTARTBLOCK:
670 return 1;
671 default:
672 break;
673 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200674 return 0;
675}
676
Denys Vlasenko523635f2012-02-25 02:44:25 +0100677#if defined(I386)
Denys Vlasenko2550d482013-02-15 21:04:28 +0100678struct user_regs_struct i386_regs;
H.J. Lu35be5812012-04-16 13:00:01 +0200679#elif defined(X86_64) || defined(X32)
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100680/*
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100681 * On i386, pt_regs and user_regs_struct are the same,
682 * but on 64 bit x86, user_regs_struct has six more fields:
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100683 * fs_base, gs_base, ds, es, fs, gs.
684 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
685 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100686struct i386_user_regs_struct {
687 uint32_t ebx;
688 uint32_t ecx;
689 uint32_t edx;
690 uint32_t esi;
691 uint32_t edi;
692 uint32_t ebp;
693 uint32_t eax;
694 uint32_t xds;
695 uint32_t xes;
696 uint32_t xfs;
697 uint32_t xgs;
698 uint32_t orig_eax;
699 uint32_t eip;
700 uint32_t xcs;
701 uint32_t eflags;
702 uint32_t esp;
703 uint32_t xss;
704};
705static union {
706 struct user_regs_struct x86_64_r;
707 struct i386_user_regs_struct i386_r;
708} x86_regs_union;
709# define x86_64_regs x86_regs_union.x86_64_r
710# define i386_regs x86_regs_union.i386_r
711static struct iovec x86_io = {
712 .iov_base = &x86_regs_union
713};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100714#elif defined(IA64)
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200715long ia32 = 0; /* not static */
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100716static long ia64_r8, ia64_r10;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100717#elif defined(POWERPC)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +0100718static long ppc_result;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100719#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100720static long m68k_d0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100721#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100722static long bfin_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100723#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100724struct pt_regs arm_regs; /* not static */
Steve McIntyred8d3bd32012-10-24 17:58:16 +0100725#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100726static union {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100727 struct user_pt_regs aarch64_r;
728 struct arm_pt_regs arm_r;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100729} arm_regs_union;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100730# define aarch64_regs arm_regs_union.aarch64_r
731# define arm_regs arm_regs_union.arm_r
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100732static struct iovec aarch64_io = {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100733 .iov_base = &arm_regs_union
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100734};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100735#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100736static long alpha_r0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100737static long alpha_a3;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100738#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100739static struct pt_regs avr32_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100740#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100741struct pt_regs sparc_regs; /* not static */
Denys Vlasenko523635f2012-02-25 02:44:25 +0100742#elif defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100743static long long mips_a3;
744static long long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100745#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100746static long mips_a3;
747static long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100748#elif defined(S390) || defined(S390X)
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200749static long gpr2;
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200750static long syscall_mode;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100751#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100752static long hppa_r28;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100753#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100754static long sh_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100755#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100756static long sh64_r9;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100757#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100758static long cris_r10;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100759#elif defined(TILE)
760struct pt_regs tile_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100761#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100762static long microblaze_r3;
Christian Svensson492f81f2013-02-14 13:26:27 +0100763#elif defined(OR1K)
764static struct user_regs_struct or1k_regs;
765static struct iovec or1k_io = {
766 .iov_base = &or1k_regs
767};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100768#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000769
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100770void
771printcall(struct tcb *tcp)
772{
773#define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
774 sizeof(long) == 8 ? "[????????????????] " : \
775 NULL /* crash */)
776 if (get_regs_error) {
777 PRINTBADPC;
778 return;
779 }
780#if defined(I386)
781 tprintf("[%08lx] ", i386_regs.eip);
782#elif defined(S390) || defined(S390X)
783 long psw;
784 if (upeek(tcp, PT_PSWADDR, &psw) < 0) {
785 PRINTBADPC;
786 return;
787 }
788# ifdef S390
789 tprintf("[%08lx] ", psw);
790# elif S390X
Dmitry V. Levinddba73e2013-02-05 19:01:58 +0000791 tprintf("[%016lx] ", psw);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100792# endif
793#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100794 if (x86_io.iov_len == sizeof(i386_regs)) {
795 tprintf("[%08x] ", (unsigned) i386_regs.eip);
796 } else {
797# if defined(X86_64)
798 tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
799# elif defined(X32)
800 /* Note: this truncates 64-bit rip to 32 bits */
801 tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip);
802# endif
803 }
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100804#elif defined(IA64)
805 long ip;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100806 if (upeek(tcp, PT_B0, &ip) < 0) {
807 PRINTBADPC;
808 return;
809 }
810 tprintf("[%08lx] ", ip);
811#elif defined(POWERPC)
812 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100813 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) {
814 PRINTBADPC;
815 return;
816 }
817# ifdef POWERPC64
818 tprintf("[%016lx] ", pc);
819# else
820 tprintf("[%08lx] ", pc);
821# endif
822#elif defined(M68K)
823 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100824 if (upeek(tcp, 4*PT_PC, &pc) < 0) {
825 tprints("[????????] ");
826 return;
827 }
828 tprintf("[%08lx] ", pc);
829#elif defined(ALPHA)
830 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100831 if (upeek(tcp, REG_PC, &pc) < 0) {
832 tprints("[????????????????] ");
833 return;
834 }
835 tprintf("[%08lx] ", pc);
836#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100837 tprintf("[%08lx] ", sparc_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100838#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100839 tprintf("[%08lx] ", sparc_regs.tpc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100840#elif defined(HPPA)
841 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100842 if (upeek(tcp, PT_IAOQ0, &pc) < 0) {
843 tprints("[????????] ");
844 return;
845 }
846 tprintf("[%08lx] ", pc);
847#elif defined(MIPS)
848 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100849 if (upeek(tcp, REG_EPC, &pc) < 0) {
850 tprints("[????????] ");
851 return;
852 }
853 tprintf("[%08lx] ", pc);
854#elif defined(SH)
855 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100856 if (upeek(tcp, 4*REG_PC, &pc) < 0) {
857 tprints("[????????] ");
858 return;
859 }
860 tprintf("[%08lx] ", pc);
861#elif defined(SH64)
862 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100863 if (upeek(tcp, REG_PC, &pc) < 0) {
864 tprints("[????????????????] ");
865 return;
866 }
867 tprintf("[%08lx] ", pc);
868#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100869 tprintf("[%08lx] ", arm_regs.ARM_pc);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100870#elif defined(AARCH64)
871 /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100872#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100873 tprintf("[%08lx] ", avr32_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100874#elif defined(BFIN)
875 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100876 if (upeek(tcp, PT_PC, &pc) < 0) {
877 PRINTBADPC;
878 return;
879 }
880 tprintf("[%08lx] ", pc);
881#elif defined(CRISV10)
882 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100883 if (upeek(tcp, 4*PT_IRP, &pc) < 0) {
884 PRINTBADPC;
885 return;
886 }
887 tprintf("[%08lx] ", pc);
888#elif defined(CRISV32)
889 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100890 if (upeek(tcp, 4*PT_ERP, &pc) < 0) {
891 PRINTBADPC;
892 return;
893 }
894 tprintf("[%08lx] ", pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100895#elif defined(TILE)
896# ifdef _LP64
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500897 tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100898# else
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500899 tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100900# endif
Christian Svensson492f81f2013-02-14 13:26:27 +0100901#elif defined(OR1K)
902 tprintf("[%08lx] ", or1k_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100903#endif /* architecture */
904}
905
Denys Vlasenko7270de52013-02-21 15:46:34 +0100906/* Shuffle syscall numbers so that we don't have huge gaps in syscall table.
907 * The shuffling should be reversible: shuffle_scno(shuffle_scno(n)) == n.
908 */
909#if defined(ARM) /* So far only ARM needs this */
910static long
911shuffle_scno(unsigned long scno)
912{
913 if (scno <= ARM_LAST_ORDINARY_SYSCALL)
914 return scno;
915
916 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
917 if (scno == 0x000ffff0)
918 return ARM_LAST_ORDINARY_SYSCALL+1;
919 if (scno == ARM_LAST_ORDINARY_SYSCALL+1)
920 return 0x000ffff0;
921
922 /* Is it ARM specific syscall?
923 * Swap with [LAST_ORDINARY+2, LAST_ORDINARY+2 + LAST_SPECIAL] range.
924 */
925 if (scno >= 0x000f0000
926 && scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL
927 ) {
928 return scno - 0x000f0000 + (ARM_LAST_ORDINARY_SYSCALL+2);
929 }
930 if (/* scno >= ARM_LAST_ORDINARY_SYSCALL+2 - always true */ 1
931 && scno <= (ARM_LAST_ORDINARY_SYSCALL+2) + ARM_LAST_SPECIAL_SYSCALL
932 ) {
933 return scno + 0x000f0000 - (ARM_LAST_ORDINARY_SYSCALL+2);
934 }
935
936 return scno;
937}
938#else
939# define shuffle_scno(scno) ((long)(scno))
940#endif
941
942static char*
943undefined_scno_name(struct tcb *tcp)
944{
945 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
946
947 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
948 return buf;
949}
950
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100951#ifndef get_regs
952long get_regs_error;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100953void
954get_regs(pid_t pid)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100955{
956# if defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100957 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100958# elif defined(I386)
959 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &i386_regs);
960# elif defined(X86_64) || defined(X32)
Denys Vlasenkoe3b248d2013-02-15 00:24:19 +0100961 /*
962 * PTRACE_GETREGSET was introduced in 2.6.33.
963 * Let's be paranoid and require a bit later kernel.
964 */
965 if (os_release >= KERNEL_VERSION(2,6,35)) {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100966 /*x86_io.iov_base = &x86_regs_union; - already is */
967 x86_io.iov_len = sizeof(x86_regs_union);
968 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (long) &x86_io);
969 } else {
970 /* Use old method, with heuristical detection of 32-bitness */
971 x86_io.iov_len = sizeof(x86_64_regs);
972 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &x86_64_regs);
973 if (!get_regs_error && x86_64_regs.cs == 0x23) {
974 x86_io.iov_len = sizeof(i386_regs);
975 /*
976 * The order is important: i386_regs and x86_64_regs
977 * are overlaid in memory!
978 */
979 i386_regs.ebx = x86_64_regs.rbx;
980 i386_regs.ecx = x86_64_regs.rcx;
981 i386_regs.edx = x86_64_regs.rdx;
982 i386_regs.esi = x86_64_regs.rsi;
983 i386_regs.edi = x86_64_regs.rdi;
984 i386_regs.ebp = x86_64_regs.rbp;
985 i386_regs.eax = x86_64_regs.rax;
986 /*i386_regs.xds = x86_64_regs.ds; unused by strace */
987 /*i386_regs.xes = x86_64_regs.es; ditto... */
988 /*i386_regs.xfs = x86_64_regs.fs;*/
989 /*i386_regs.xgs = x86_64_regs.gs;*/
990 i386_regs.orig_eax = x86_64_regs.orig_rax;
991 i386_regs.eip = x86_64_regs.rip;
992 /*i386_regs.xcs = x86_64_regs.cs;*/
993 /*i386_regs.eflags = x86_64_regs.eflags;*/
994 i386_regs.esp = x86_64_regs.rsp;
995 /*i386_regs.xss = x86_64_regs.ss;*/
996 }
997 }
Denys Vlasenko401374e2013-02-06 18:24:39 +0100998# elif defined(ARM)
999 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (void *)&arm_regs);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001000# elif defined(AARCH64)
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001001 /*aarch64_io.iov_base = &arm_regs_union; - already is */
1002 aarch64_io.iov_len = sizeof(arm_regs_union);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001003 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (void *)&aarch64_io);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001004# if 0
1005 /* Paranoia checks */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001006 if (get_regs_error)
1007 return;
1008 switch (aarch64_io.iov_len) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001009 case sizeof(aarch64_regs):
1010 /* We are in 64-bit mode */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001011 break;
1012 case sizeof(arm_regs):
1013 /* We are in 32-bit mode */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001014 break;
1015 default:
1016 get_regs_error = -1;
1017 break;
1018 }
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001019# endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001020# elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001021 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001022# elif defined(TILE)
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -05001023 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &tile_regs);
Christian Svensson492f81f2013-02-14 13:26:27 +01001024# elif defined(OR1K)
1025 or1k_io.iov_len = sizeof(or1k_regs);
1026 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &or1k_io);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001027# endif
1028}
1029#endif
1030
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001031/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001032 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1033 * 1: ok, continue in trace_syscall_entering().
1034 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001035 * ("????" etc) and bail out.
1036 */
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01001037static int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001038get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001039{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001040 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001041
Denys Vlasenko523635f2012-02-25 02:44:25 +01001042#if defined(S390) || defined(S390X)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001043 if (upeek(tcp, PT_GPR2, &syscall_mode) < 0)
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001044 return -1;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001045
1046 if (syscall_mode != -ENOSYS) {
1047 /*
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001048 * Since kernel version 2.5.44 the scno gets passed in gpr2.
Roland McGrath2f924ca2003-06-26 22:23:28 +00001049 */
1050 scno = syscall_mode;
1051 } else {
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001052 /*
Michal Ludvig882eda82002-11-11 12:50:47 +00001053 * Old style of "passing" the scno via the SVC instruction.
1054 */
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001055 long psw;
Michal Ludvig882eda82002-11-11 12:50:47 +00001056 long opcode, offset_reg, tmp;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001057 void *svc_addr;
Denys Vlasenko7c9ba8b2011-08-19 19:46:32 +02001058 static const int gpr_offset[16] = {
1059 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
1060 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
1061 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
1062 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
1063 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001064
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001065 if (upeek(tcp, PT_PSWADDR, &psw) < 0)
Michal Ludvig882eda82002-11-11 12:50:47 +00001066 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001067 errno = 0;
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001068 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +00001069 if (errno) {
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001070 perror_msg("%s", "peektext(psw-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +00001071 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001072 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001073
1074 /*
1075 * We have to check if the SVC got executed directly or via an
1076 * EXECUTE instruction. In case of EXECUTE it is necessary to do
1077 * instruction decoding to derive the system call number.
1078 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1079 * so that this doesn't work if a SVC opcode is part of an EXECUTE
1080 * opcode. Since there is no way to find out the opcode size this
1081 * is the best we can do...
1082 */
Michal Ludvig882eda82002-11-11 12:50:47 +00001083 if ((opcode & 0xff00) == 0x0a00) {
1084 /* SVC opcode */
1085 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +00001086 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001087 else {
1088 /* SVC got executed by EXECUTE instruction */
1089
1090 /*
1091 * Do instruction decoding of EXECUTE. If you really want to
1092 * understand this, read the Principles of Operations.
1093 */
1094 svc_addr = (void *) (opcode & 0xfff);
1095
1096 tmp = 0;
1097 offset_reg = (opcode & 0x000f0000) >> 16;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001098 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001099 return -1;
1100 svc_addr += tmp;
1101
1102 tmp = 0;
1103 offset_reg = (opcode & 0x0000f000) >> 12;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001104 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001105 return -1;
1106 svc_addr += tmp;
1107
Denys Vlasenkofb036672009-01-23 16:30:26 +00001108 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
Michal Ludvig882eda82002-11-11 12:50:47 +00001109 if (errno)
1110 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001111# if defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +00001112 scno >>= 48;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001113# else
Michal Ludvig882eda82002-11-11 12:50:47 +00001114 scno >>= 16;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001115# endif
Michal Ludvig882eda82002-11-11 12:50:47 +00001116 tmp = 0;
1117 offset_reg = (opcode & 0x00f00000) >> 20;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001118 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001119 return -1;
1120
1121 scno = (scno | tmp) & 0xff;
1122 }
1123 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001124#elif defined(POWERPC)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001125 if (upeek(tcp, sizeof(unsigned long)*PT_R0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001126 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001127# ifdef POWERPC64
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001128 /* TODO: speed up strace by not doing this at every syscall.
1129 * We only need to do it after execve.
1130 */
1131 int currpers;
1132 long val;
Andreas Schwabd69fa492010-07-12 21:39:57 +02001133
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001134 /* Check for 64/32 bit mode. */
1135 if (upeek(tcp, sizeof(unsigned long)*PT_MSR, &val) < 0)
1136 return -1;
1137 /* SF is bit 0 of MSR */
1138 if (val < 0)
1139 currpers = 0;
1140 else
1141 currpers = 1;
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001142 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001143# endif
1144#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001145 scno = avr32_regs.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001146#elif defined(BFIN)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001147 if (upeek(tcp, PT_ORIG_P0, &scno))
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001148 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001149#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001150 scno = i386_regs.orig_eax;
H.J. Lu35be5812012-04-16 13:00:01 +02001151#elif defined(X86_64) || defined(X32)
1152# ifndef __X32_SYSCALL_BIT
1153# define __X32_SYSCALL_BIT 0x40000000
1154# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001155 int currpers;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001156# if 1
1157 /* GETREGSET of NT_PRSTATUS tells us regset size,
1158 * which unambiguously detects i386.
1159 *
1160 * Linux kernel distinguishes x86-64 and x32 processes
1161 * solely by looking at __X32_SYSCALL_BIT:
1162 * arch/x86/include/asm/compat.h::is_x32_task():
1163 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1164 * return true;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001165 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001166 if (x86_io.iov_len == sizeof(i386_regs)) {
1167 scno = i386_regs.orig_eax;
1168 currpers = 1;
1169 } else {
1170 scno = x86_64_regs.orig_rax;
1171 currpers = 0;
1172 if (scno & __X32_SYSCALL_BIT) {
1173 scno -= __X32_SYSCALL_BIT;
1174 currpers = 2;
1175 }
1176 }
1177# elif 0
1178 /* cs = 0x33 for long mode (native 64 bit and x32)
1179 * cs = 0x23 for compatibility mode (32 bit)
1180 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1181 */
1182 scno = x86_64_regs.orig_rax;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001183 switch (x86_64_regs.cs) {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001184 case 0x23: currpers = 1; break;
H.J. Lu35be5812012-04-16 13:00:01 +02001185 case 0x33:
1186 if (x86_64_regs.ds == 0x2b) {
1187 currpers = 2;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001188 scno &= ~__X32_SYSCALL_BIT;
H.J. Lu35be5812012-04-16 13:00:01 +02001189 } else
1190 currpers = 0;
1191 break;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001192 default:
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001193 fprintf(stderr, "Unknown value CS=0x%08X while "
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001194 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001195 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001196 currpers = current_personality;
1197 break;
1198 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001199# elif 0
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001200 /* This version analyzes the opcode of a syscall instruction.
1201 * (int 0x80 on i386 vs. syscall on x86-64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001202 * It works, but is too complicated, and strictly speaking, unreliable.
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001203 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001204 unsigned long call, rip = x86_64_regs.rip;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001205 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1206 rip -= 2;
1207 errno = 0;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001208 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001209 if (errno)
1210 fprintf(stderr, "ptrace_peektext failed: %s\n",
1211 strerror(errno));
1212 switch (call & 0xffff) {
1213 /* x86-64: syscall = 0x0f 0x05 */
1214 case 0x050f: currpers = 0; break;
1215 /* i386: int 0x80 = 0xcd 0x80 */
1216 case 0x80cd: currpers = 1; break;
1217 default:
1218 currpers = current_personality;
1219 fprintf(stderr,
1220 "Unknown syscall opcode (0x%04X) while "
1221 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001222 "PID=%d\n", (int)call, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001223 break;
1224 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001225# endif
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001226
H.J. Lu35be5812012-04-16 13:00:01 +02001227# ifdef X32
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001228 /* If we are built for a x32 system, then personality 0 is x32
1229 * (not x86_64), and stracing of x86_64 apps is not supported.
1230 * Stracing of i386 apps is still supported.
H.J. Lu085e4282012-04-17 11:05:04 -07001231 */
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001232 if (currpers == 0) {
1233 fprintf(stderr, "syscall_%lu(...) in unsupported "
1234 "64-bit mode of process PID=%d\n",
1235 scno, tcp->pid);
1236 return 0;
H.J. Lu35be5812012-04-16 13:00:01 +02001237 }
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001238 currpers &= ~2; /* map 2,1 to 0,1 */
H.J. Lu35be5812012-04-16 13:00:01 +02001239# endif
H.J. Lu085e4282012-04-17 11:05:04 -07001240 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001241#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001242# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01001243 long psr;
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001244 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001245 ia32 = (psr & IA64_PSR_IS) != 0;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001246 if (ia32) {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001247 if (upeek(tcp, PT_R1, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001248 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001249 } else {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001250 if (upeek(tcp, PT_R15, &scno) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001251 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001252 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001253#elif defined(AARCH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001254 switch (aarch64_io.iov_len) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001255 case sizeof(aarch64_regs):
1256 /* We are in 64-bit mode */
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001257 scno = aarch64_regs.regs[8];
1258 update_personality(tcp, 1);
1259 break;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001260 case sizeof(arm_regs):
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001261 /* We are in 32-bit mode */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001262 scno = arm_regs.ARM_r7;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001263 update_personality(tcp, 0);
1264 break;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001265 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001266#elif defined(ARM)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001267 if (arm_regs.ARM_ip != 0) {
1268 /* It is not a syscall entry */
1269 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001270 tcp->flags |= TCB_INSYSCALL;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001271 return 0;
1272 }
1273 /* Note: we support only 32-bit CPUs, not 26-bit */
1274
1275 if (arm_regs.ARM_cpsr & 0x20) {
1276 /* Thumb mode */
1277 scno = arm_regs.ARM_r7;
1278 } else {
1279 /* ARM mode */
1280 errno = 0;
1281 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1282 if (errno)
1283 return -1;
1284
1285 /* EABI syscall convention? */
1286 if (scno == 0xef000000) {
1287 scno = arm_regs.ARM_r7; /* yes */
1288 } else {
1289 if ((scno & 0x0ff00000) != 0x0f900000) {
1290 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1291 tcp->pid, scno);
1292 return -1;
1293 }
1294 /* Fixup the syscall number */
1295 scno &= 0x000fffff;
1296 }
1297 }
Denys Vlasenko7270de52013-02-21 15:46:34 +01001298
1299 scno = shuffle_scno(scno);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001300#elif defined(M68K)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001301 if (upeek(tcp, 4*PT_ORIG_D0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001302 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001303#elif defined(LINUX_MIPSN32)
Roland McGrath542c2c62008-05-20 01:11:56 +00001304 unsigned long long regs[38];
1305
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001306 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001307 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001308 mips_a3 = regs[REG_A3];
1309 mips_r2 = regs[REG_V0];
Roland McGrath542c2c62008-05-20 01:11:56 +00001310
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001311 scno = mips_r2;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001312 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001313 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001314 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001315 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001316 return 0;
1317 }
Roland McGrath542c2c62008-05-20 01:11:56 +00001318 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001319#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001320 if (upeek(tcp, REG_A3, &mips_a3) < 0)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001321 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001322 if (upeek(tcp, REG_V0, &scno) < 0)
1323 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001324
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001325 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001326 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001327 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001328 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001329 return 0;
1330 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001331 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001332#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001333 if (upeek(tcp, REG_A3, &alpha_a3) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001334 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001335 if (upeek(tcp, REG_R0, &scno) < 0)
1336 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001337
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001338 /*
1339 * Do some sanity checks to figure out if it's
1340 * really a syscall entry
1341 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001342 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001343 if (alpha_a3 == 0 || alpha_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001344 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001345 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001346 return 0;
1347 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001348 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001349#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001350 /* Disassemble the syscall trap. */
1351 /* Retrieve the syscall trap instruction. */
Denys Vlasenko46455822013-02-05 17:02:59 +01001352 unsigned long trap;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001353 errno = 0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001354# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001355 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001356 trap >>= 32;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001357# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001358 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001359# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001360 if (errno)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001361 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001362
1363 /* Disassemble the trap to see what personality to use. */
1364 switch (trap) {
1365 case 0x91d02010:
1366 /* Linux/SPARC syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001367 update_personality(tcp, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001368 break;
1369 case 0x91d0206d:
1370 /* Linux/SPARC64 syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001371 update_personality(tcp, 2);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001372 break;
1373 case 0x91d02000:
1374 /* SunOS syscall trap. (pers 1) */
1375 fprintf(stderr, "syscall: SunOS no support\n");
1376 return -1;
1377 case 0x91d02008:
1378 /* Solaris 2.x syscall trap. (per 2) */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001379 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001380 break;
1381 case 0x91d02009:
1382 /* NetBSD/FreeBSD syscall trap. */
1383 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1384 return -1;
1385 case 0x91d02027:
1386 /* Solaris 2.x gettimeofday */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001387 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001388 break;
1389 default:
Denys Vlasenko523635f2012-02-25 02:44:25 +01001390# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001391 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001392# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001393 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001394# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001395 return -1;
1396 }
1397
1398 /* Extract the system call number from the registers. */
1399 if (trap == 0x91d02027)
1400 scno = 156;
1401 else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001402 scno = sparc_regs.u_regs[U_REG_G1];
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001403 if (scno == 0) {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001404 scno = sparc_regs.u_regs[U_REG_O0];
1405 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 +02001406 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001407#elif defined(HPPA)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001408 if (upeek(tcp, PT_GR20, &scno) < 0)
1409 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001410#elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001411 /*
1412 * In the new syscall ABI, the system call number is in R3.
1413 */
1414 if (upeek(tcp, 4*(REG_REG0+3), &scno) < 0)
1415 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001416
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001417 if (scno < 0) {
1418 /* Odd as it may seem, a glibc bug has been known to cause
1419 glibc to issue bogus negative syscall numbers. So for
1420 our purposes, make strace print what it *should* have been */
1421 long correct_scno = (scno & 0xff);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001422 if (debug_flag)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001423 fprintf(stderr,
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001424 "Detected glibc bug: bogus system call"
1425 " number = %ld, correcting to %ld\n",
1426 scno,
1427 correct_scno);
1428 scno = correct_scno;
1429 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001430#elif defined(SH64)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001431 if (upeek(tcp, REG_SYSCALL, &scno) < 0)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001432 return -1;
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001433 scno &= 0xFFFF;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001434#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001435 if (upeek(tcp, 4*PT_R9, &scno) < 0)
1436 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001437#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001438 int currpers;
1439 scno = tile_regs.regs[10];
1440# ifdef __tilepro__
1441 currpers = 1;
1442# else
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001443# ifndef PT_FLAGS_COMPAT
1444# define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1445# endif
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001446 if (tile_regs.flags & PT_FLAGS_COMPAT)
1447 currpers = 1;
1448 else
1449 currpers = 0;
1450# endif
1451 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001452#elif defined(MICROBLAZE)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001453 if (upeek(tcp, 0, &scno) < 0)
1454 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001455#elif defined(OR1K)
1456 scno = or1k_regs.gpr[11];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001457#endif
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001458
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001459 tcp->scno = scno;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001460 if (SCNO_IS_VALID(tcp->scno)) {
1461 tcp->s_ent = &sysent[scno];
1462 tcp->qual_flg = qual_flags[scno];
1463 } else {
1464 static const struct sysent unknown = {
1465 .nargs = MAX_ARGS,
1466 .sys_flags = 0,
1467 .sys_func = printargs,
1468 .sys_name = "unknown", /* not used */
1469 };
1470 tcp->s_ent = &unknown;
1471 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1472 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001473 return 1;
1474}
1475
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02001476/* Called at each syscall entry.
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001477 * Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001478 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1479 * 1: ok, continue in trace_syscall_entering().
1480 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001481 * ("????" etc) and bail out.
1482 */
Roland McGratha4d48532005-06-08 20:45:28 +00001483static int
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001484syscall_fixup_on_sysenter(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001485{
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001486 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001487#if defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001488 if (i386_regs.eax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001489 if (debug_flag)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001490 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1491 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001492 }
H.J. Lu35be5812012-04-16 13:00:01 +02001493#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001494 {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001495 long rax;
1496 if (x86_io.iov_len == sizeof(i386_regs)) {
1497 /* Sign extend from 32 bits */
1498 rax = (int32_t)i386_regs.eax;
1499 } else {
1500 /* Note: in X32 build, this truncates 64 to 32 bits */
1501 rax = x86_64_regs.rax;
1502 }
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001503 if (rax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001504 if (debug_flag)
Denys Vlasenko18beb982011-08-24 16:59:23 +02001505 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1506 return 0;
1507 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001508 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001509#elif defined(S390) || defined(S390X)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001510 /* TODO: we already fetched PT_GPR2 in get_scno
1511 * and stored it in syscall_mode, reuse it here
1512 * instead of re-fetching?
1513 */
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001514 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001515 return -1;
Michal Ludvig882eda82002-11-11 12:50:47 +00001516 if (syscall_mode != -ENOSYS)
1517 syscall_mode = tcp->scno;
Denys Vlasenkoece98792011-08-25 10:25:35 +02001518 if (gpr2 != syscall_mode) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001519 if (debug_flag)
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001520 fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2);
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001521 return 0;
1522 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001523#elif defined(M68K)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001524 /* TODO? Eliminate upeek's in arches below like we did in x86 */
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001525 if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001526 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001527 if (m68k_d0 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001528 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001529 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001530 return 0;
1531 }
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001532#elif defined(IA64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001533 if (upeek(tcp, PT_R10, &ia64_r10) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001534 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001535 if (upeek(tcp, PT_R8, &ia64_r8) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001536 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001537 if (ia32 && ia64_r8 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001538 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001539 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001540 return 0;
1541 }
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001542#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001543 if (upeek(tcp, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001544 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001545 if (cris_r10 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001546 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001547 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001548 return 0;
1549 }
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001550#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001551 if (upeek(tcp, 3 * 4, &microblaze_r3) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001552 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001553 if (microblaze_r3 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001554 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001555 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001556 return 0;
1557 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001558#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001559 return 1;
1560}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001561
Denys Vlasenko146b9442012-03-18 22:10:48 +01001562static void
1563internal_fork(struct tcb *tcp)
1564{
1565#if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1566# define ARG_FLAGS 1
1567#else
1568# define ARG_FLAGS 0
1569#endif
1570#ifndef CLONE_UNTRACED
1571# define CLONE_UNTRACED 0x00800000
1572#endif
1573 if ((ptrace_setoptions
1574 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1575 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1576 return;
1577
1578 if (!followfork)
1579 return;
1580
1581 if (entering(tcp)) {
1582 /*
1583 * We won't see the new child if clone is called with
1584 * CLONE_UNTRACED, so we keep the same logic with that option
1585 * and don't trace it.
1586 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001587 if ((tcp->s_ent->sys_func == sys_clone)
1588 && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
1589 )
Denys Vlasenko146b9442012-03-18 22:10:48 +01001590 return;
1591 setbpt(tcp);
1592 } else {
1593 if (tcp->flags & TCB_BPTSET)
1594 clearbpt(tcp);
1595 }
1596}
1597
1598#if defined(TCB_WAITEXECVE)
1599static void
1600internal_exec(struct tcb *tcp)
1601{
1602 /* Maybe we have post-execve SIGTRAP suppressed? */
1603 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1604 return; /* yes, no need to do anything */
1605
1606 if (exiting(tcp) && syserror(tcp))
1607 /* Error in execve, no post-execve SIGTRAP expected */
1608 tcp->flags &= ~TCB_WAITEXECVE;
1609 else
1610 tcp->flags |= TCB_WAITEXECVE;
1611}
1612#endif
1613
1614static void
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001615syscall_fixup_for_fork_exec(struct tcb *tcp)
Roland McGrathc1e45922008-05-27 23:18:29 +00001616{
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001617 /*
1618 * We must always trace a few critical system calls in order to
1619 * correctly support following forks in the presence of tracing
1620 * qualifiers.
1621 */
1622 int (*func)();
1623
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001624 func = tcp->s_ent->sys_func;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001625
1626 if ( sys_fork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001627 || sys_vfork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001628 || sys_clone == func
Denys Vlasenko146b9442012-03-18 22:10:48 +01001629 ) {
1630 internal_fork(tcp);
1631 return;
1632 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001633
Denys Vlasenko84703742012-02-25 02:38:52 +01001634#if defined(TCB_WAITEXECVE)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001635 if ( sys_execve == func
Denys Vlasenko84703742012-02-25 02:38:52 +01001636# if defined(SPARC) || defined(SPARC64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001637 || sys_execv == func
Denys Vlasenkoa7949742011-08-21 17:26:55 +02001638# endif
Denys Vlasenko146b9442012-03-18 22:10:48 +01001639 ) {
1640 internal_exec(tcp);
1641 return;
1642 }
Roland McGrathc1e45922008-05-27 23:18:29 +00001643#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001644}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001645
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001646/* Return -1 on error or 1 on success (never 0!) */
Roland McGratha4d48532005-06-08 20:45:28 +00001647static int
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001648get_syscall_args(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001649{
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001650 int i, nargs;
1651
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001652 nargs = tcp->s_ent->nargs;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001653
Denys Vlasenko523635f2012-02-25 02:44:25 +01001654#if defined(S390) || defined(S390X)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001655 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001656 if (upeek(tcp, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
1657 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001658#elif defined(ALPHA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001659 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001660 if (upeek(tcp, REG_A0+i, &tcp->u_arg[i]) < 0)
1661 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001662#elif defined(IA64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001663 if (!ia32) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001664 unsigned long *out0, cfm, sof, sol;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001665 long rbs_end;
1666 /* be backwards compatible with kernel < 2.4.4... */
1667# ifndef PT_RBS_END
1668# define PT_RBS_END PT_AR_BSP
1669# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001670
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001671 if (upeek(tcp, PT_RBS_END, &rbs_end) < 0)
1672 return -1;
1673 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001674 return -1;
1675
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001676 sof = (cfm >> 0) & 0x7f;
1677 sol = (cfm >> 7) & 0x7f;
1678 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1679
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001680 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001681 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1682 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1683 return -1;
1684 }
1685 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001686 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1687 PT_R9 /* ECX = out1 */,
1688 PT_R10 /* EDX = out2 */,
1689 PT_R14 /* ESI = out3 */,
1690 PT_R15 /* EDI = out4 */,
1691 PT_R13 /* EBP = out5 */};
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001692
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001693 for (i = 0; i < nargs; ++i) {
1694 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1695 return -1;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001696 /* truncate away IVE sign-extension */
1697 tcp->u_arg[i] &= 0xffffffff;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001698 }
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001699 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001700#elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001701 /* N32 and N64 both use up to six registers. */
1702 unsigned long long regs[38];
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001703
1704 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1705 return -1;
1706
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001707 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001708 tcp->u_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001709# if defined(LINUX_MIPSN32)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001710 tcp->ext_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001711# endif
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001712 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001713#elif defined(MIPS)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001714 if (nargs > 4) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001715 long sp;
1716
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001717 if (upeek(tcp, REG_SP, &sp) < 0)
1718 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001719 for (i = 0; i < 4; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001720 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1721 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001722 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001723 (char *)(tcp->u_arg + 4));
1724 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001725 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001726 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001727 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001728 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001729#elif defined(POWERPC)
1730# ifndef PT_ORIG_R3
1731# define PT_ORIG_R3 34
1732# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001733 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001734 if (upeek(tcp, (i==0) ?
1735 (sizeof(unsigned long) * PT_ORIG_R3) :
1736 ((i+PT_R3) * sizeof(unsigned long)),
1737 &tcp->u_arg[i]) < 0)
1738 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001739 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001740#elif defined(SPARC) || defined(SPARC64)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001741 for (i = 0; i < nargs; ++i)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001742 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001743#elif defined(HPPA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001744 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001745 if (upeek(tcp, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1746 return -1;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001747#elif defined(ARM) || defined(AARCH64)
1748# if defined(AARCH64)
1749 if (tcp->currpers == 1)
1750 for (i = 0; i < nargs; ++i)
1751 tcp->u_arg[i] = aarch64_regs.regs[i];
1752 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001753# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001754 for (i = 0; i < nargs; ++i)
Denys Vlasenko401374e2013-02-06 18:24:39 +01001755 tcp->u_arg[i] = arm_regs.uregs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001756#elif defined(AVR32)
Denys Vlasenkob5b25892011-08-30 19:04:54 +02001757 (void)i;
1758 (void)nargs;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001759 tcp->u_arg[0] = avr32_regs.r12;
1760 tcp->u_arg[1] = avr32_regs.r11;
1761 tcp->u_arg[2] = avr32_regs.r10;
1762 tcp->u_arg[3] = avr32_regs.r9;
1763 tcp->u_arg[4] = avr32_regs.r5;
1764 tcp->u_arg[5] = avr32_regs.r3;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001765#elif defined(BFIN)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001766 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 +02001767
Denys Vlasenko4b887a52011-08-23 13:32:38 +02001768 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001769 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1770 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001771#elif defined(SH)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001772 static const int syscall_regs[MAX_ARGS] = {
1773 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1774 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001775 };
1776
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001777 for (i = 0; i < nargs; ++i)
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001778 if (upeek(tcp, syscall_regs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001779 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001780#elif defined(SH64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001781 int i;
1782 /* Registers used by SH5 Linux system calls for parameters */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001783 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001784
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001785 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001786 if (upeek(tcp, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1787 return -1;
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001788#elif defined(I386)
1789 (void)i;
1790 (void)nargs;
1791 tcp->u_arg[0] = i386_regs.ebx;
1792 tcp->u_arg[1] = i386_regs.ecx;
1793 tcp->u_arg[2] = i386_regs.edx;
1794 tcp->u_arg[3] = i386_regs.esi;
1795 tcp->u_arg[4] = i386_regs.edi;
1796 tcp->u_arg[5] = i386_regs.ebp;
H.J. Lu35be5812012-04-16 13:00:01 +02001797#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001798 (void)i;
1799 (void)nargs;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001800 if (x86_io.iov_len != sizeof(i386_regs)) {
1801 /* x86-64 or x32 ABI */
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001802 tcp->u_arg[0] = x86_64_regs.rdi;
1803 tcp->u_arg[1] = x86_64_regs.rsi;
1804 tcp->u_arg[2] = x86_64_regs.rdx;
1805 tcp->u_arg[3] = x86_64_regs.r10;
1806 tcp->u_arg[4] = x86_64_regs.r8;
1807 tcp->u_arg[5] = x86_64_regs.r9;
H.J. Lu35be5812012-04-16 13:00:01 +02001808# ifdef X32
1809 tcp->ext_arg[0] = x86_64_regs.rdi;
1810 tcp->ext_arg[1] = x86_64_regs.rsi;
1811 tcp->ext_arg[2] = x86_64_regs.rdx;
1812 tcp->ext_arg[3] = x86_64_regs.r10;
1813 tcp->ext_arg[4] = x86_64_regs.r8;
1814 tcp->ext_arg[5] = x86_64_regs.r9;
1815# endif
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001816 } else {
1817 /* i386 ABI */
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001818 /* Zero-extend from 32 bits */
1819 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1820 * if you need to use *sign-extended* parameter.
1821 */
1822 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1823 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1824 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1825 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1826 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1827 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001828 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001829#elif defined(MICROBLAZE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001830 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001831 if (upeek(tcp, (5 + i) * 4, &tcp->u_arg[i]) < 0)
1832 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001833#elif defined(CRISV10) || defined(CRISV32)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001834 static const int crisregs[MAX_ARGS] = {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001835 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001836 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001837 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001838
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001839 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001840 if (upeek(tcp, crisregs[i], &tcp->u_arg[i]) < 0)
1841 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001842#elif defined(TILE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001843 for (i = 0; i < nargs; ++i)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001844 tcp->u_arg[i] = tile_regs.regs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001845#elif defined(M68K)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001846 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001847 if (upeek(tcp, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
1848 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001849#elif defined(OR1K)
1850 (void)nargs;
1851 for (i = 0; i < 6; ++i)
1852 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001853#else /* Other architecture (32bits specific) */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001854 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001855 if (upeek(tcp, i*4, &tcp->u_arg[i]) < 0)
1856 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001857#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001858 return 1;
1859}
1860
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00001861static int
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001862trace_syscall_entering(struct tcb *tcp)
1863{
1864 int res, scno_good;
1865
Denys Vlasenko2ce12ed2011-08-24 17:25:32 +02001866#if defined TCB_WAITEXECVE
1867 if (tcp->flags & TCB_WAITEXECVE) {
1868 /* This is the post-execve SIGTRAP. */
1869 tcp->flags &= ~TCB_WAITEXECVE;
1870 return 0;
1871 }
1872#endif
1873
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001874 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001875 if (res == 0)
1876 return res;
Denys Vlasenko907735a2012-03-21 00:23:16 +01001877 if (res == 1) {
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001878 res = syscall_fixup_on_sysenter(tcp);
Denys Vlasenko907735a2012-03-21 00:23:16 +01001879 if (res == 0)
1880 return res;
1881 if (res == 1)
1882 res = get_syscall_args(tcp);
1883 }
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001884
1885 if (res != 1) {
1886 printleader(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001887 if (scno_good != 1)
Denys Vlasenkob7a6dae2012-03-20 16:48:35 +01001888 tprints("????" /* anti-trigraph gap */ "(");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001889 else if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01001890 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001891 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001892 tprintf("%s(", tcp->s_ent->sys_name);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001893 /*
1894 * " <unavailable>" will be added later by the code which
1895 * detects ptrace errors.
1896 */
1897 goto ret;
1898 }
1899
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001900#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001901 while (1) {
Denys Vlasenko523635f2012-02-25 02:44:25 +01001902# ifdef SYS_socket_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001903 if (tcp->s_ent->sys_func == sys_socketcall) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00001904 decode_socket_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001905 break;
1906 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001907# endif
1908# ifdef SYS_ipc_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001909 if (tcp->s_ent->sys_func == sys_ipc) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00001910 decode_ipc_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001911 break;
1912 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001913# endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001914 break;
1915 }
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001916#endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001917
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001918 if (need_fork_exec_workarounds)
1919 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001920
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001921 if (!(tcp->qual_flg & QUAL_TRACE)
1922 || (tracing_paths && !pathtrace_match(tcp))
1923 ) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001924 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
1925 return 0;
1926 }
1927
1928 tcp->flags &= ~TCB_FILTERED;
1929
1930 if (cflag == CFLAG_ONLY_STATS) {
1931 res = 0;
1932 goto ret;
1933 }
1934
1935 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001936 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01001937 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001938 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001939 tprintf("%s(", tcp->s_ent->sys_name);
1940 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001941 res = printargs(tcp);
1942 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001943 res = tcp->s_ent->sys_func(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001944
Dmitry V. Levinb742d8c2012-09-17 22:40:12 +00001945 fflush(tcp->outf);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001946 ret:
1947 tcp->flags |= TCB_INSYSCALL;
1948 /* Measure the entrance time as late as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001949 if (Tflag || cflag)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001950 gettimeofday(&tcp->etime, NULL);
1951 return res;
1952}
1953
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001954/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001955 * 1: ok, continue in trace_syscall_exiting().
1956 * -1: error, trace_syscall_exiting() should print error indicator
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001957 * ("????" etc) and bail out.
1958 */
1959static int
1960get_syscall_result(struct tcb *tcp)
1961{
Denys Vlasenko523635f2012-02-25 02:44:25 +01001962#if defined(S390) || defined(S390X)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001963 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
1964 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001965#elif defined(POWERPC)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001966# define SO_MASK 0x10000000
1967 {
1968 long flags;
1969 if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1970 return -1;
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01001971 if (upeek(tcp, sizeof(unsigned long)*PT_R3, &ppc_result) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001972 return -1;
1973 if (flags & SO_MASK)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01001974 ppc_result = -ppc_result;
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001975 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001976#elif defined(AVR32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001977 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001978#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001979 if (upeek(tcp, PT_R0, &bfin_r0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001980 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001981#elif defined(I386)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001982 /* already done by get_regs */
H.J. Lu35be5812012-04-16 13:00:01 +02001983#elif defined(X86_64) || defined(X32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001984 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001985#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001986# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01001987 long psr;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001988 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
1989 ia32 = (psr & IA64_PSR_IS) != 0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001990 if (upeek(tcp, PT_R8, &ia64_r8) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001991 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001992 if (upeek(tcp, PT_R10, &ia64_r10) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001993 return -1;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001994#elif defined(ARM)
1995 /* already done by get_regs */
Steve McIntyred8d3bd32012-10-24 17:58:16 +01001996#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001997 /* register reading already done by get_regs */
1998
1999 /* Used to do this, but we did it on syscall entry already: */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002000 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
2001 * else it's personality 0.
2002 */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002003 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
Denys Vlasenko523635f2012-02-25 02:44:25 +01002004#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002005 if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002006 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002007#elif defined(LINUX_MIPSN32)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002008 unsigned long long regs[38];
2009
2010 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
2011 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002012 mips_a3 = regs[REG_A3];
2013 mips_r2 = regs[REG_V0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01002014#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002015 if (upeek(tcp, REG_A3, &mips_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002016 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002017 if (upeek(tcp, REG_V0, &mips_r2) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002018 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002019#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002020 if (upeek(tcp, REG_A3, &alpha_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002021 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002022 if (upeek(tcp, REG_R0, &alpha_r0) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002023 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002024#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002025 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002026#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002027 if (upeek(tcp, PT_GR28, &hppa_r28) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002028 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002029#elif defined(SH)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002030 /* new syscall ABI returns result in R0 */
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002031 if (upeek(tcp, 4*REG_REG0, (long *)&sh_r0) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002032 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002033#elif defined(SH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002034 /* ABI defines result returned in r9 */
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002035 if (upeek(tcp, REG_GENERAL(9), (long *)&sh64_r9) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002036 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002037#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002038 if (upeek(tcp, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002039 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002040#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002041 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002042#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002043 if (upeek(tcp, 3 * 4, &microblaze_r3) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002044 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01002045#elif defined(OR1K)
2046 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002047#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002048 return 1;
2049}
2050
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002051/* Called at each syscall exit */
2052static void
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002053syscall_fixup_on_sysexit(struct tcb *tcp)
2054{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002055#if defined(S390) || defined(S390X)
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002056 if (syscall_mode != -ENOSYS)
2057 syscall_mode = tcp->scno;
Denys Vlasenkoece98792011-08-25 10:25:35 +02002058 if ((tcp->flags & TCB_WAITEXECVE)
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002059 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
2060 /*
2061 * Return from execve.
2062 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2063 * flag set for the post-execve SIGTRAP to see and reset.
2064 */
2065 gpr2 = 0;
2066 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002067#endif
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002068}
2069
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002070/*
2071 * Check the syscall return value register value for whether it is
2072 * a negated errno code indicating an error, or a success return value.
2073 */
2074static inline int
2075is_negated_errno(unsigned long int val)
2076{
2077 unsigned long int max = -(long int) nerrnos;
Denys Vlasenko2544f982013-02-19 17:39:56 +01002078#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01002079 if (current_wordsize < sizeof(val)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002080 val = (unsigned int) val;
2081 max = (unsigned int) max;
2082 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002083#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002084 return val > max;
2085}
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002086
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002087#if defined(X32)
2088static inline int
2089is_negated_errno_x32(unsigned long long val)
2090{
2091 unsigned long long max = -(long long) nerrnos;
2092 /*
2093 * current_wordsize is 4 even in personality 0 (native X32)
2094 * but truncation _must not_ be done in it.
2095 * can't check current_wordsize here!
2096 */
2097 if (current_personality != 0) {
2098 val = (uint32_t) val;
2099 max = (uint32_t) max;
2100 }
2101 return val > max;
2102}
2103#endif
2104
Denys Vlasenko907735a2012-03-21 00:23:16 +01002105/* Returns:
2106 * 1: ok, continue in trace_syscall_exiting().
2107 * -1: error, trace_syscall_exiting() should print error indicator
2108 * ("????" etc) and bail out.
2109 */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002110static void
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002111get_error(struct tcb *tcp)
2112{
2113 int u_error = 0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002114 int check_errno = 1;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002115 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002116 check_errno = 0;
2117 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002118#if defined(S390) || defined(S390X)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002119 if (check_errno && is_negated_errno(gpr2)) {
2120 tcp->u_rval = -1;
2121 u_error = -gpr2;
2122 }
2123 else {
2124 tcp->u_rval = gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002125 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002126#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002127 if (check_errno && is_negated_errno(i386_regs.eax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002128 tcp->u_rval = -1;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002129 u_error = -i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002130 }
2131 else {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002132 tcp->u_rval = i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002133 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002134#elif defined(X86_64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002135 long rax;
2136 if (x86_io.iov_len == sizeof(i386_regs)) {
2137 /* Sign extend from 32 bits */
2138 rax = (int32_t)i386_regs.eax;
2139 } else {
2140 rax = x86_64_regs.rax;
2141 }
2142 if (check_errno && is_negated_errno(rax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002143 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002144 u_error = -rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002145 }
2146 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002147 tcp->u_rval = rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002148 }
2149#elif defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002150 /* In X32, return value is 64-bit (llseek uses one).
2151 * Using merely "long rax" would not work.
2152 */
2153 long long rax;
2154 if (x86_io.iov_len == sizeof(i386_regs)) {
2155 /* Sign extend from 32 bits */
2156 rax = (int32_t)i386_regs.eax;
2157 } else {
2158 rax = x86_64_regs.rax;
2159 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002160 /* Careful: is_negated_errno() works only on longs */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002161 if (check_errno && is_negated_errno_x32(rax)) {
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002162 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002163 u_error = -rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002164 }
2165 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002166 tcp->u_rval = rax; /* truncating */
2167 tcp->u_lrval = rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002168 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002169#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002170 if (ia32) {
2171 int err;
2172
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002173 err = (int)ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002174 if (check_errno && is_negated_errno(err)) {
2175 tcp->u_rval = -1;
2176 u_error = -err;
2177 }
2178 else {
2179 tcp->u_rval = err;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002180 }
2181 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002182 if (check_errno && ia64_r10) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002183 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002184 u_error = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002185 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002186 tcp->u_rval = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002187 }
2188 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002189#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002190 if (check_errno && mips_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002191 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002192 u_error = mips_r2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002193 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002194 tcp->u_rval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002195# if defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002196 tcp->u_lrval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002197# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002198 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002199#elif defined(POWERPC)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002200 if (check_errno && is_negated_errno(ppc_result)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002201 tcp->u_rval = -1;
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002202 u_error = -ppc_result;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002203 }
2204 else {
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002205 tcp->u_rval = ppc_result;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002206 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002207#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002208 if (check_errno && is_negated_errno(m68k_d0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002209 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002210 u_error = -m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002211 }
2212 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002213 tcp->u_rval = m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002214 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002215#elif defined(ARM) || defined(AARCH64)
2216# if defined(AARCH64)
2217 if (tcp->currpers == 1) {
2218 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2219 tcp->u_rval = -1;
2220 u_error = -aarch64_regs.regs[0];
2221 }
2222 else {
2223 tcp->u_rval = aarch64_regs.regs[0];
2224 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002225 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002226 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002227# endif
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002228 {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002229 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002230 tcp->u_rval = -1;
Denys Vlasenko401374e2013-02-06 18:24:39 +01002231 u_error = -arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002232 }
2233 else {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002234 tcp->u_rval = arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002235 }
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002236 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002237#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002238 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002239 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002240 u_error = -avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002241 }
2242 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002243 tcp->u_rval = avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002244 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002245#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002246 if (check_errno && is_negated_errno(bfin_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002247 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002248 u_error = -bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002249 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002250 tcp->u_rval = bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002251 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002252#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002253 if (check_errno && alpha_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002254 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002255 u_error = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002256 }
2257 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002258 tcp->u_rval = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002259 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002260#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002261 if (check_errno && sparc_regs.psr & PSR_C) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002262 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002263 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002264 }
2265 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002266 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002267 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002268#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002269 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002270 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002271 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002272 }
2273 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002274 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002275 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002276#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002277 if (check_errno && is_negated_errno(hppa_r28)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002278 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002279 u_error = -hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002280 }
2281 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002282 tcp->u_rval = hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002283 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002284#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002285 if (check_errno && is_negated_errno(sh_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002286 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002287 u_error = -sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002288 }
2289 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002290 tcp->u_rval = sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002291 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002292#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002293 if (check_errno && is_negated_errno(sh64_r9)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002294 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002295 u_error = -sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002296 }
2297 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002298 tcp->u_rval = sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002299 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002300#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002301 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002302 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002303 u_error = -cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002304 }
2305 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002306 tcp->u_rval = cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002307 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002308#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002309 /*
2310 * The standard tile calling convention returns the value (or negative
2311 * errno) in r0, and zero (or positive errno) in r1.
2312 * Until at least kernel 3.8, however, the r1 value is not reflected
2313 * in ptregs at this point, so we use r0 here.
2314 */
2315 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002316 tcp->u_rval = -1;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002317 u_error = -tile_regs.regs[0];
2318 } else {
2319 tcp->u_rval = tile_regs.regs[0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002320 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002321#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002322 if (check_errno && is_negated_errno(microblaze_r3)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002323 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002324 u_error = -microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002325 }
2326 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002327 tcp->u_rval = microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002328 }
Christian Svensson492f81f2013-02-14 13:26:27 +01002329#elif defined(OR1K)
2330 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2331 tcp->u_rval = -1;
2332 u_error = -or1k_regs.gpr[11];
2333 }
2334 else {
2335 tcp->u_rval = or1k_regs.gpr[11];
2336 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002337#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002338 tcp->u_error = u_error;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002339}
2340
2341static void
2342dumpio(struct tcb *tcp)
2343{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002344 int (*func)();
2345
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002346 if (syserror(tcp))
2347 return;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002348 if ((unsigned long) tcp->u_arg[0] >= MAX_QUALS)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002349 return;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002350 func = tcp->s_ent->sys_func;
2351 if (func == printargs)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002352 return;
2353 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002354 if (func == sys_read ||
2355 func == sys_pread ||
2356 func == sys_recv ||
2357 func == sys_recvfrom)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002358 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002359 else if (func == sys_readv)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002360 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2361 return;
2362 }
2363 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002364 if (func == sys_write ||
2365 func == sys_pwrite ||
2366 func == sys_send ||
2367 func == sys_sendto)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002368 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002369 else if (func == sys_writev)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002370 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2371 return;
2372 }
2373}
2374
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002375static int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002376trace_syscall_exiting(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002377{
2378 int sys_res;
2379 struct timeval tv;
Denys Vlasenko1a5b5a72011-08-25 00:29:56 +02002380 int res;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002381 long u_error;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002382
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002383 /* Measure the exit time as early as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002384 if (Tflag || cflag)
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002385 gettimeofday(&tv, NULL);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002386
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00002387#if SUPPORTED_PERSONALITIES > 1
2388 update_personality(tcp, tcp->currpers);
2389#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002390 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002391 if (res == 1) {
2392 syscall_fixup_on_sysexit(tcp); /* never fails */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002393 get_error(tcp); /* never fails */
2394 if (need_fork_exec_workarounds)
2395 syscall_fixup_for_fork_exec(tcp);
2396 if (filtered(tcp))
2397 goto ret;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002398 }
2399
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002400 if (cflag) {
2401 struct timeval t = tv;
Denys Vlasenkoc95a88f2011-08-21 17:47:40 +02002402 count_syscall(tcp, &t);
Denys Vlasenko7b609d52011-06-22 14:32:43 +02002403 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002404 goto ret;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002405 }
2406 }
2407
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002408 /* If not in -ff mode, and printing_tcp != tcp,
2409 * then the log currently does not end with output
2410 * of _our syscall entry_, but with something else.
2411 * We need to say which syscall's return is this.
2412 *
2413 * Forced reprinting via TCB_REPRINT is used only by
2414 * "strace -ff -oLOG test/threaded_execve" corner case.
2415 * It's the only case when -ff mode needs reprinting.
2416 */
2417 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2418 tcp->flags &= ~TCB_REPRINT;
2419 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002420 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002421 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002422 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002423 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002424 }
2425 printing_tcp = tcp;
2426
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002427 if (res != 1) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002428 /* There was error in one of prior ptrace ops */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002429 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002430 tabto();
Denys Vlasenko000b6012012-01-28 01:25:03 +01002431 tprints("= ? <unavailable>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002432 line_ended();
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002433 tcp->flags &= ~TCB_INSYSCALL;
2434 return res;
2435 }
2436
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002437 sys_res = 0;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002438 if (tcp->qual_flg & QUAL_RAW) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002439 /* sys_res = printargs(tcp); - but it's nop on sysexit */
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002440 } else {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002441 /* FIXME: not_failing_only (IOW, option -z) is broken:
2442 * failure of syscall is known only after syscall return.
2443 * Thus we end up with something like this on, say, ENOENT:
2444 * open("doesnt_exist", O_RDONLY <unfinished ...>
2445 * {next syscall decode}
2446 * whereas the intended result is that open(...) line
2447 * is not shown at all.
2448 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002449 if (not_failing_only && tcp->u_error)
Denys Vlasenko3b738812011-08-22 02:06:35 +02002450 goto ret; /* ignore failed syscalls */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002451 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002452 }
2453
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002454 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002455 tabto();
Denys Vlasenko3b738812011-08-22 02:06:35 +02002456 u_error = tcp->u_error;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002457 if (tcp->qual_flg & QUAL_RAW) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002458 if (u_error)
2459 tprintf("= -1 (errno %ld)", u_error);
2460 else
2461 tprintf("= %#lx", tcp->u_rval);
2462 }
2463 else if (!(sys_res & RVAL_NONE) && u_error) {
2464 switch (u_error) {
Denys Vlasenkofe585652012-01-12 11:26:34 +01002465 /* Blocked signals do not interrupt any syscalls.
2466 * In this case syscalls don't return ERESTARTfoo codes.
2467 *
2468 * Deadly signals set to SIG_DFL interrupt syscalls
2469 * and kill the process regardless of which of the codes below
2470 * is returned by the interrupted syscall.
2471 * In some cases, kernel forces a kernel-generated deadly
2472 * signal to be unblocked and set to SIG_DFL (and thus cause
2473 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2474 * or SIGILL. (The alternative is to leave process spinning
2475 * forever on the faulty instruction - not useful).
2476 *
2477 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2478 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2479 * but kernel will always restart them.
2480 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002481 case ERESTARTSYS:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002482 /* Most common type of signal-interrupted syscall exit code.
2483 * The system call will be restarted with the same arguments
2484 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2485 */
2486 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002487 break;
2488 case ERESTARTNOINTR:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002489 /* Rare. For example, fork() returns this if interrupted.
2490 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2491 */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002492 tprints("= ? ERESTARTNOINTR (To be restarted)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002493 break;
2494 case ERESTARTNOHAND:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002495 /* pause(), rt_sigsuspend() etc use this code.
2496 * SA_RESTART is ignored (assumed not set):
2497 * syscall won't restart (will return EINTR instead)
Denys Vlasenko30c03232013-02-19 16:59:26 +01002498 * even after signal with SA_RESTART set. However,
2499 * after SIG_IGN or SIG_DFL signal it will restart
2500 * (thus the name "restart only if has no handler").
Denys Vlasenkofe585652012-01-12 11:26:34 +01002501 */
2502 tprints("= ? ERESTARTNOHAND (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002503 break;
2504 case ERESTART_RESTARTBLOCK:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002505 /* Syscalls like nanosleep(), poll() which can't be
2506 * restarted with their original arguments use this
2507 * code. Kernel will execute restart_syscall() instead,
2508 * which changes arguments before restarting syscall.
2509 * SA_RESTART is ignored (assumed not set) similarly
2510 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2511 * since restart data is saved in "restart block"
2512 * in task struct, and if signal handler uses a syscall
2513 * which in turn saves another such restart block,
2514 * old data is lost and restart becomes impossible)
2515 */
2516 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002517 break;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002518 default:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002519 if (u_error < 0)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002520 tprintf("= -1 E??? (errno %ld)", u_error);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002521 else if (u_error < nerrnos)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002522 tprintf("= -1 %s (%s)", errnoent[u_error],
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002523 strerror(u_error));
2524 else
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002525 tprintf("= -1 ERRNO_%ld (%s)", u_error,
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002526 strerror(u_error));
2527 break;
2528 }
2529 if ((sys_res & RVAL_STR) && tcp->auxstr)
2530 tprintf(" (%s)", tcp->auxstr);
2531 }
2532 else {
2533 if (sys_res & RVAL_NONE)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002534 tprints("= ?");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002535 else {
2536 switch (sys_res & RVAL_MASK) {
2537 case RVAL_HEX:
2538 tprintf("= %#lx", tcp->u_rval);
2539 break;
2540 case RVAL_OCTAL:
2541 tprintf("= %#lo", tcp->u_rval);
2542 break;
2543 case RVAL_UDECIMAL:
2544 tprintf("= %lu", tcp->u_rval);
2545 break;
2546 case RVAL_DECIMAL:
2547 tprintf("= %ld", tcp->u_rval);
2548 break;
H.J. Ludd0130b2012-04-16 12:16:45 +02002549#if defined(LINUX_MIPSN32) || defined(X32)
2550 /*
2551 case RVAL_LHEX:
2552 tprintf("= %#llx", tcp->u_lrval);
2553 break;
2554 case RVAL_LOCTAL:
2555 tprintf("= %#llo", tcp->u_lrval);
2556 break;
2557 */
2558 case RVAL_LUDECIMAL:
2559 tprintf("= %llu", tcp->u_lrval);
2560 break;
2561 /*
2562 case RVAL_LDECIMAL:
2563 tprintf("= %lld", tcp->u_lrval);
2564 break;
2565 */
2566#endif
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002567 default:
2568 fprintf(stderr,
2569 "invalid rval format\n");
2570 break;
2571 }
2572 }
2573 if ((sys_res & RVAL_STR) && tcp->auxstr)
2574 tprintf(" (%s)", tcp->auxstr);
2575 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002576 if (Tflag) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002577 tv_sub(&tv, &tv, &tcp->etime);
2578 tprintf(" <%ld.%06ld>",
2579 (long) tv.tv_sec, (long) tv.tv_usec);
2580 }
Denys Vlasenko000b6012012-01-28 01:25:03 +01002581 tprints("\n");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002582 dumpio(tcp);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002583 line_ended();
2584
Denys Vlasenko3b738812011-08-22 02:06:35 +02002585 ret:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002586 tcp->flags &= ~TCB_INSYSCALL;
2587 return 0;
2588}
2589
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002590int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002591trace_syscall(struct tcb *tcp)
2592{
2593 return exiting(tcp) ?
2594 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2595}