blob: a44ac0c49404dc128ef376afe3d3a581047d66f5 [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
Dmitry V. Levin32c049a2013-03-18 00:59:27 +000068/* for struct iovec */
69#include <sys/uio.h>
70/* for NT_PRSTATUS */
71#ifdef HAVE_ELF_H
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010072# include <elf.h>
73#endif
74
Steve McIntyred8d3bd32012-10-24 17:58:16 +010075#if defined(AARCH64)
76# include <asm/ptrace.h>
James Hogan5f999a82013-02-22 14:44:10 +000077#endif
78
Chris Zankel8f636ed2013-03-25 10:22:07 -070079#if defined(XTENSA)
80# include <asm/ptrace.h>
81#endif
82
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000083#ifndef ERESTARTSYS
Denys Vlasenko523635f2012-02-25 02:44:25 +010084# define ERESTARTSYS 512
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000085#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010086#ifndef ERESTARTNOINTR
Denys Vlasenko523635f2012-02-25 02:44:25 +010087# define ERESTARTNOINTR 513
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000088#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010089#ifndef ERESTARTNOHAND
90# define ERESTARTNOHAND 514 /* restart if no handler */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000091#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010092#ifndef ERESTART_RESTARTBLOCK
Denys Vlasenko523635f2012-02-25 02:44:25 +010093# define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
Roland McGrath9c555e72003-07-09 09:47:59 +000094#endif
Denys Vlasenko523635f2012-02-25 02:44:25 +010095
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000096#ifndef NSIG
Denys Vlasenko523635f2012-02-25 02:44:25 +010097# warning: NSIG is not defined, using 32
98# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000099#endif
100#ifdef ARM
Denys Vlasenko041b3ee2011-08-18 12:48:56 +0200101/* Ugh. Is this really correct? ARM has no RT signals?! */
Denys Vlasenko523635f2012-02-25 02:44:25 +0100102# undef NSIG
103# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000104#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000105
106#include "syscall.h"
107
108/* Define these shorthand notations to simplify the syscallent files. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000109#define TD TRACE_DESC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000110#define TF TRACE_FILE
111#define TI TRACE_IPC
112#define TN TRACE_NETWORK
113#define TP TRACE_PROCESS
114#define TS TRACE_SIGNAL
Namhyung Kim96792962012-10-24 11:41:57 +0900115#define TM TRACE_MEMORY
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000116#define NF SYSCALL_NEVER_FAILS
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200117#define MA MAX_ARGS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000118
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100119const struct_sysent sysent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000120#include "syscallent.h"
121};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000122
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100123#if SUPPORTED_PERSONALITIES > 1
124static const struct_sysent sysent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100125# include "syscallent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000126};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200127#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000128
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100129#if SUPPORTED_PERSONALITIES > 2
130static const struct_sysent sysent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100131# include "syscallent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000132};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200133#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000134
135/* Now undef them since short defines cause wicked namespace pollution. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000136#undef TD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000137#undef TF
138#undef TI
139#undef TN
140#undef TP
141#undef TS
Namhyung Kim96792962012-10-24 11:41:57 +0900142#undef TM
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000143#undef NF
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200144#undef MA
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000145
Denys Vlasenko39fca622011-08-20 02:12:33 +0200146/*
147 * `ioctlent.h' may be generated from `ioctlent.raw' by the auxiliary
148 * program `ioctlsort', such that the list is sorted by the `code' field.
149 * This has the side-effect of resolving the _IO.. macros into
150 * plain integers, eliminating the need to include here everything
151 * in "/usr/include".
152 */
153
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100154const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000155#include "errnoent.h"
156};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100157const char *const signalent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200158#include "signalent.h"
159};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100160const struct_ioctlent ioctlent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200161#include "ioctlent.h"
162};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000163
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100164#if SUPPORTED_PERSONALITIES > 1
Roland McGrathee36ce12004-09-04 03:53:10 +0000165static const char *const errnoent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100166# include "errnoent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000167};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200168static const char *const signalent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100169# include "signalent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200170};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100171static const struct_ioctlent ioctlent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100172# include "ioctlent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200173};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200174#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000175
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100176#if SUPPORTED_PERSONALITIES > 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000177static const char *const errnoent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100178# include "errnoent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000179};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200180static const char *const signalent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100181# include "signalent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200182};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100183static const struct_ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100184# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200185};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200186#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000187
Dmitry V. Levine6f55242013-02-26 22:02:30 +0000188enum {
189 nsyscalls0 = ARRAY_SIZE(sysent0)
190#if SUPPORTED_PERSONALITIES > 1
191 , nsyscalls1 = ARRAY_SIZE(sysent1)
192# if SUPPORTED_PERSONALITIES > 2
193 , nsyscalls2 = ARRAY_SIZE(sysent2)
194# endif
195#endif
196};
197
198enum {
199 nerrnos0 = ARRAY_SIZE(errnoent0)
200#if SUPPORTED_PERSONALITIES > 1
201 , nerrnos1 = ARRAY_SIZE(errnoent1)
202# if SUPPORTED_PERSONALITIES > 2
203 , nerrnos2 = ARRAY_SIZE(errnoent2)
204# endif
205#endif
206};
207
208enum {
209 nsignals0 = ARRAY_SIZE(signalent0)
210#if SUPPORTED_PERSONALITIES > 1
211 , nsignals1 = ARRAY_SIZE(signalent1)
212# if SUPPORTED_PERSONALITIES > 2
213 , nsignals2 = ARRAY_SIZE(signalent2)
214# endif
215#endif
216};
217
218enum {
219 nioctlents0 = ARRAY_SIZE(ioctlent0)
220#if SUPPORTED_PERSONALITIES > 1
221 , nioctlents1 = ARRAY_SIZE(ioctlent1)
222# if SUPPORTED_PERSONALITIES > 2
223 , nioctlents2 = ARRAY_SIZE(ioctlent2)
224# endif
225#endif
226};
227
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100228#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100229const struct_sysent *sysent = sysent0;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100230const char *const *errnoent = errnoent0;
231const char *const *signalent = signalent0;
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100232const struct_ioctlent *ioctlent = ioctlent0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100233#endif
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100234unsigned nsyscalls = nsyscalls0;
235unsigned nerrnos = nerrnos0;
236unsigned nsignals = nsignals0;
237unsigned nioctlents = nioctlents0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100238
239unsigned num_quals;
240qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
241
242static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
243 nsyscalls0,
244#if SUPPORTED_PERSONALITIES > 1
245 nsyscalls1,
246#endif
247#if SUPPORTED_PERSONALITIES > 2
248 nsyscalls2,
249#endif
250};
251static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
252 sysent0,
253#if SUPPORTED_PERSONALITIES > 1
254 sysent1,
255#endif
256#if SUPPORTED_PERSONALITIES > 2
257 sysent2,
258#endif
259};
260
261enum {
262 MAX_NSYSCALLS1 = (nsyscalls0
263#if SUPPORTED_PERSONALITIES > 1
264 > nsyscalls1 ? nsyscalls0 : nsyscalls1
265#endif
266 ),
267 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
268#if SUPPORTED_PERSONALITIES > 2
269 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
270#endif
271 ),
272 MAX_NSYSCALLS = MAX_NSYSCALLS2,
273 /* We are ready for arches with up to 255 signals,
274 * even though the largest known signo is on MIPS and it is 128.
275 * The number of existing syscalls on all arches is
276 * larger that 255 anyway, so it is just a pedantic matter.
277 */
278 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
279};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000280
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100281#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100282unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000283
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100284# ifndef current_wordsize
285unsigned current_wordsize;
286static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000287 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000288 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100289# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000290 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100291# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200292};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100293# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000294
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200295void
Dmitry V. Levin3abe8b22006-12-20 22:37:21 +0000296set_personality(int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000297{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100298 nsyscalls = nsyscall_vec[personality];
299 sysent = sysent_vec[personality];
300
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000301 switch (personality) {
302 case 0:
303 errnoent = errnoent0;
304 nerrnos = nerrnos0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000305 ioctlent = ioctlent0;
306 nioctlents = nioctlents0;
307 signalent = signalent0;
308 nsignals = nsignals0;
309 break;
310
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000311 case 1:
312 errnoent = errnoent1;
313 nerrnos = nerrnos1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000314 ioctlent = ioctlent1;
315 nioctlents = nioctlents1;
316 signalent = signalent1;
317 nsignals = nsignals1;
318 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000319
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100320# if SUPPORTED_PERSONALITIES > 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000321 case 2:
322 errnoent = errnoent2;
323 nerrnos = nerrnos2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000324 ioctlent = ioctlent2;
325 nioctlents = nioctlents2;
326 signalent = signalent2;
327 nsignals = nsignals2;
328 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100329# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000330 }
331
332 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100333# ifndef current_wordsize
334 current_wordsize = personality_wordsize[personality];
335# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000336}
337
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000338static void
339update_personality(struct tcb *tcp, int personality)
340{
341 if (personality == current_personality)
342 return;
343 set_personality(personality);
344
345 if (personality == tcp->currpers)
346 return;
347 tcp->currpers = personality;
348
H.J. Lu35be5812012-04-16 13:00:01 +0200349# if defined(POWERPC64)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000350 if (!qflag) {
351 static const char *const names[] = {"64 bit", "32 bit"};
352 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
353 tcp->pid, names[personality]);
354 }
H.J. Lu35be5812012-04-16 13:00:01 +0200355# elif defined(X86_64)
356 if (!qflag) {
357 static const char *const names[] = {"64 bit", "32 bit", "x32"};
358 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
359 tcp->pid, names[personality]);
360 }
H.J. Lu085e4282012-04-17 11:05:04 -0700361# elif defined(X32)
362 if (!qflag) {
363 static const char *const names[] = {"x32", "32 bit"};
364 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
365 tcp->pid, names[personality]);
366 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000367# elif defined(AARCH64)
368 if (!qflag) {
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100369 static const char *const names[] = {"32-bit", "AArch64"};
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000370 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
371 tcp->pid, names[personality]);
372 }
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100373# elif defined(TILE)
374 if (!qflag) {
375 static const char *const names[] = {"64-bit", "32-bit"};
376 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
377 tcp->pid, names[personality]);
378 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100379# endif
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000380}
381#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000382
Denys Vlasenkoc1540fe2013-02-21 16:17:08 +0100383static int qual_syscall(), qual_signal(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000384
Roland McGrathe10e62a2004-09-04 04:20:43 +0000385static const struct qual_options {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000386 int bitflag;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000387 const char *option_name;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000388 int (*qualify)(const char *, int, int);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000389 const char *argument_name;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000390} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000391 { QUAL_TRACE, "trace", qual_syscall, "system call" },
392 { QUAL_TRACE, "t", qual_syscall, "system call" },
393 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
394 { QUAL_ABBREV, "a", qual_syscall, "system call" },
395 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
396 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
397 { QUAL_RAW, "raw", qual_syscall, "system call" },
398 { QUAL_RAW, "x", qual_syscall, "system call" },
399 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
400 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
401 { QUAL_SIGNAL, "s", qual_signal, "signal" },
Roland McGrath9797ceb2002-12-30 10:23:00 +0000402 { QUAL_READ, "read", qual_desc, "descriptor" },
403 { QUAL_READ, "reads", qual_desc, "descriptor" },
404 { QUAL_READ, "r", qual_desc, "descriptor" },
405 { QUAL_WRITE, "write", qual_desc, "descriptor" },
406 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
407 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000408 { 0, NULL, NULL, NULL },
409};
410
Roland McGrath9797ceb2002-12-30 10:23:00 +0000411static void
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100412reallocate_qual(int n)
413{
414 unsigned p;
415 qualbits_t *qp;
416 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
417 qp = qual_vec[p] = realloc(qual_vec[p], n * sizeof(qualbits_t));
418 if (!qp)
419 die_out_of_memory();
420 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
421 }
422 num_quals = n;
423}
424
425static void
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000426qualify_one(int n, int bitflag, int not, int pers)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000427{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100428 unsigned p;
Roland McGrath138c6a32006-01-12 09:50:49 +0000429
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100430 if (num_quals <= n)
431 reallocate_qual(n + 1);
Roland McGrath138c6a32006-01-12 09:50:49 +0000432
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100433 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
434 if (pers == p || pers < 0) {
435 if (not)
436 qual_vec[p][n] &= ~bitflag;
437 else
438 qual_vec[p][n] |= bitflag;
439 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000440 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000441}
442
443static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000444qual_syscall(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000445{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100446 unsigned p;
447 unsigned i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000448 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000449
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100450 if (*s >= '0' && *s <= '9') {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100451 i = string_to_uint(s);
Denys Vlasenkob43dacd2013-02-23 18:19:28 +0100452 if (i >= MAX_NSYSCALLS)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000453 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000454 qualify_one(i, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000455 return 0;
Roland McGrath48a035f2006-01-12 09:45:56 +0000456 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000457
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100458 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
459 for (i = 0; i < nsyscall_vec[p]; i++) {
460 if (sysent_vec[p][i].sys_name
461 && strcmp(s, sysent_vec[p][i].sys_name) == 0
462 ) {
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000463 qualify_one(i, bitflag, not, p);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100464 rc = 0;
465 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000466 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100467 }
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000468
Roland McGrathfe6b3522005-02-02 04:40:11 +0000469 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000470}
471
472static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000473qual_signal(const char *s, int bitflag, int not)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000474{
475 int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000476
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100477 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000478 int signo = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100479 if (signo < 0 || signo > 255)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000480 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000481 qualify_one(signo, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000482 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000483 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000484 if (strncasecmp(s, "SIG", 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000485 s += 3;
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100486 for (i = 0; i <= NSIG; i++) {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000487 if (strcasecmp(s, signame(i) + 3) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000488 qualify_one(i, bitflag, not, -1);
Roland McGrath76421df2005-02-02 03:51:18 +0000489 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000490 }
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100491 }
Roland McGrath76421df2005-02-02 03:51:18 +0000492 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000493}
494
495static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000496qual_desc(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000497{
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100498 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000499 int desc = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100500 if (desc < 0 || desc > 0x7fff) /* paranoia */
Roland McGrathfe6b3522005-02-02 04:40:11 +0000501 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000502 qualify_one(desc, bitflag, not, -1);
Roland McGrath2b619022003-04-10 18:58:20 +0000503 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000504 }
505 return -1;
506}
507
508static int
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000509lookup_class(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000510{
511 if (strcmp(s, "file") == 0)
512 return TRACE_FILE;
513 if (strcmp(s, "ipc") == 0)
514 return TRACE_IPC;
515 if (strcmp(s, "network") == 0)
516 return TRACE_NETWORK;
517 if (strcmp(s, "process") == 0)
518 return TRACE_PROCESS;
519 if (strcmp(s, "signal") == 0)
520 return TRACE_SIGNAL;
Roland McGrath2fe7b132005-07-05 03:25:35 +0000521 if (strcmp(s, "desc") == 0)
522 return TRACE_DESC;
Namhyung Kim96792962012-10-24 11:41:57 +0900523 if (strcmp(s, "memory") == 0)
524 return TRACE_MEMORY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000525 return -1;
526}
527
528void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000529qualify(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000530{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000531 const struct qual_options *opt;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000532 int not;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000533 char *copy;
534 const char *p;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000535 int i, n;
536
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100537 if (num_quals == 0)
538 reallocate_qual(MIN_QUALS);
539
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000540 opt = &qual_options[0];
541 for (i = 0; (p = qual_options[i].option_name); i++) {
542 n = strlen(p);
543 if (strncmp(s, p, n) == 0 && s[n] == '=') {
544 opt = &qual_options[i];
545 s += n + 1;
546 break;
547 }
548 }
549 not = 0;
550 if (*s == '!') {
551 not = 1;
552 s++;
553 }
554 if (strcmp(s, "none") == 0) {
555 not = 1 - not;
556 s = "all";
557 }
558 if (strcmp(s, "all") == 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100559 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000560 qualify_one(i, opt->bitflag, not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000561 }
562 return;
563 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100564 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000565 qualify_one(i, opt->bitflag, !not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000566 }
Denys Vlasenko5d645812011-08-20 12:48:18 +0200567 copy = strdup(s);
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200568 if (!copy)
569 die_out_of_memory();
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000570 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000571 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100572 unsigned pers;
573 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
574 for (i = 0; i < nsyscall_vec[pers]; i++)
575 if (sysent_vec[pers][i].sys_flags & n)
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000576 qualify_one(i, opt->bitflag, not, pers);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100577 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000578 continue;
579 }
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000580 if (opt->qualify(p, opt->bitflag, not)) {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100581 error_msg_and_die("invalid %s '%s'",
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000582 opt->argument_name, p);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000583 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000584 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000585 free(copy);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000586 return;
587}
588
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000589#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000590static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000591decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000592{
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000593 unsigned long addr;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100594 unsigned int i, n, size;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000595
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000596 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
597 return;
598
599 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100600 tcp->qual_flg = qual_flags[tcp->scno];
601 tcp->s_ent = &sysent[tcp->scno];
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000602 addr = tcp->u_arg[1];
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100603 size = current_wordsize;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100604 n = tcp->s_ent->nargs;
605 for (i = 0; i < n; ++i) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000606 if (size == sizeof(int)) {
607 unsigned int arg;
608 if (umove(tcp, addr, &arg) < 0)
609 arg = 0;
610 tcp->u_arg[i] = arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000611 }
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000612 else {
613 unsigned long arg;
614 if (umove(tcp, addr, &arg) < 0)
615 arg = 0;
616 tcp->u_arg[i] = arg;
617 }
618 addr += size;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000619 }
620}
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000621#endif
Mike Frysinger3362e892012-03-15 01:09:19 -0400622
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000623#ifdef SYS_ipc_subcall
624static void
625decode_ipc_subcall(struct tcb *tcp)
626{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100627 unsigned int i, n;
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000628
629 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
630 return;
631
632 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100633 tcp->qual_flg = qual_flags[tcp->scno];
634 tcp->s_ent = &sysent[tcp->scno];
635 n = tcp->s_ent->nargs;
636 for (i = 0; i < n; i++)
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000637 tcp->u_arg[i] = tcp->u_arg[i + 1];
638}
639#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000640
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200641int
642printargs(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000643{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200644 if (entering(tcp)) {
645 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100646 int n = tcp->s_ent->nargs;
647 for (i = 0; i < n; i++)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200648 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
649 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000650 return 0;
651}
652
Denys Vlasenko72879c62012-02-27 14:18:02 +0100653int
654printargs_lu(struct tcb *tcp)
655{
656 if (entering(tcp)) {
657 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100658 int n = tcp->s_ent->nargs;
659 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100660 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
661 }
662 return 0;
663}
664
665int
666printargs_ld(struct tcb *tcp)
667{
668 if (entering(tcp)) {
669 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100670 int n = tcp->s_ent->nargs;
671 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100672 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
673 }
674 return 0;
675}
676
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100677#if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200678long
679getrval2(struct tcb *tcp)
680{
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100681 long val;
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200682
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100683# if defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100684 val = sparc_regs.u_regs[U_REG_O1];
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100685# elif defined(SH)
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200686 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200687 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100688# elif defined(IA64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200689 if (upeek(tcp->pid, PT_R9, &val) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200690 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100691# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200692
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200693 return val;
694}
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100695#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200696
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200697int
698is_restart_error(struct tcb *tcp)
699{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200700 switch (tcp->u_error) {
701 case ERESTARTSYS:
702 case ERESTARTNOINTR:
703 case ERESTARTNOHAND:
704 case ERESTART_RESTARTBLOCK:
705 return 1;
706 default:
707 break;
708 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200709 return 0;
710}
711
Denys Vlasenko523635f2012-02-25 02:44:25 +0100712#if defined(I386)
Denys Vlasenko2550d482013-02-15 21:04:28 +0100713struct user_regs_struct i386_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000714# define ARCH_REGS_FOR_GETREGSET i386_regs
H.J. Lu35be5812012-04-16 13:00:01 +0200715#elif defined(X86_64) || defined(X32)
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100716/*
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100717 * On i386, pt_regs and user_regs_struct are the same,
718 * but on 64 bit x86, user_regs_struct has six more fields:
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100719 * fs_base, gs_base, ds, es, fs, gs.
720 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
721 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100722struct i386_user_regs_struct {
723 uint32_t ebx;
724 uint32_t ecx;
725 uint32_t edx;
726 uint32_t esi;
727 uint32_t edi;
728 uint32_t ebp;
729 uint32_t eax;
730 uint32_t xds;
731 uint32_t xes;
732 uint32_t xfs;
733 uint32_t xgs;
734 uint32_t orig_eax;
735 uint32_t eip;
736 uint32_t xcs;
737 uint32_t eflags;
738 uint32_t esp;
739 uint32_t xss;
740};
741static union {
742 struct user_regs_struct x86_64_r;
743 struct i386_user_regs_struct i386_r;
744} x86_regs_union;
745# define x86_64_regs x86_regs_union.x86_64_r
746# define i386_regs x86_regs_union.i386_r
747static struct iovec x86_io = {
748 .iov_base = &x86_regs_union
749};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100750#elif defined(IA64)
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200751long ia32 = 0; /* not static */
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100752static long ia64_r8, ia64_r10;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100753#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +0200754struct pt_regs ppc_regs;
Denys Vlasenko46dc8b22012-03-21 00:07:25 +0100755static long ppc_result;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100756#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100757static long m68k_d0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100758#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100759static long bfin_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100760#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100761struct pt_regs arm_regs; /* not static */
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000762# define ARCH_REGS_FOR_GETREGSET arm_regs
Steve McIntyred8d3bd32012-10-24 17:58:16 +0100763#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100764static union {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100765 struct user_pt_regs aarch64_r;
766 struct arm_pt_regs arm_r;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100767} arm_regs_union;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100768# define aarch64_regs arm_regs_union.aarch64_r
769# define arm_regs arm_regs_union.arm_r
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100770static struct iovec aarch64_io = {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100771 .iov_base = &arm_regs_union
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100772};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100773#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100774static long alpha_r0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100775static long alpha_a3;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100776#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100777static struct pt_regs avr32_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100778#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100779struct pt_regs sparc_regs; /* not static */
Denys Vlasenko523635f2012-02-25 02:44:25 +0100780#elif defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100781static long long mips_a3;
782static long long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100783#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100784static long mips_a3;
785static long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100786#elif defined(S390) || defined(S390X)
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200787static long gpr2;
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200788static long syscall_mode;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100789#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100790static long hppa_r28;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100791#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100792static long sh_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100793#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100794static long sh64_r9;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100795#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100796static long cris_r10;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100797#elif defined(TILE)
798struct pt_regs tile_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100799#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100800static long microblaze_r3;
Christian Svensson492f81f2013-02-14 13:26:27 +0100801#elif defined(OR1K)
802static struct user_regs_struct or1k_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000803# define ARCH_REGS_FOR_GETREGSET or1k_regs
James Hogan5f999a82013-02-22 14:44:10 +0000804#elif defined(METAG)
805static struct user_gp_regs metag_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000806# define ARCH_REGS_FOR_GETREGSET metag_regs
Chris Zankel8f636ed2013-03-25 10:22:07 -0700807#elif defined(XTENSA)
808static long xtensa_a2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100809#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000810
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100811void
812printcall(struct tcb *tcp)
813{
814#define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
815 sizeof(long) == 8 ? "[????????????????] " : \
816 NULL /* crash */)
817 if (get_regs_error) {
818 PRINTBADPC;
819 return;
820 }
821#if defined(I386)
822 tprintf("[%08lx] ", i386_regs.eip);
823#elif defined(S390) || defined(S390X)
824 long psw;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200825 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100826 PRINTBADPC;
827 return;
828 }
829# ifdef S390
830 tprintf("[%08lx] ", psw);
831# elif S390X
Dmitry V. Levinddba73e2013-02-05 19:01:58 +0000832 tprintf("[%016lx] ", psw);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100833# endif
834#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100835 if (x86_io.iov_len == sizeof(i386_regs)) {
836 tprintf("[%08x] ", (unsigned) i386_regs.eip);
837 } else {
838# if defined(X86_64)
839 tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
840# elif defined(X32)
841 /* Note: this truncates 64-bit rip to 32 bits */
842 tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip);
843# endif
844 }
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100845#elif defined(IA64)
846 long ip;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200847 if (upeek(tcp->pid, PT_B0, &ip) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100848 PRINTBADPC;
849 return;
850 }
851 tprintf("[%08lx] ", ip);
852#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +0200853 long pc = ppc_regs.nip;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100854# ifdef POWERPC64
855 tprintf("[%016lx] ", pc);
856# else
857 tprintf("[%08lx] ", pc);
858# endif
859#elif defined(M68K)
860 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200861 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100862 tprints("[????????] ");
863 return;
864 }
865 tprintf("[%08lx] ", pc);
866#elif defined(ALPHA)
867 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200868 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100869 tprints("[????????????????] ");
870 return;
871 }
872 tprintf("[%08lx] ", pc);
873#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100874 tprintf("[%08lx] ", sparc_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100875#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100876 tprintf("[%08lx] ", sparc_regs.tpc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100877#elif defined(HPPA)
878 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200879 if (upeek(tcp->pid, PT_IAOQ0, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100880 tprints("[????????] ");
881 return;
882 }
883 tprintf("[%08lx] ", pc);
884#elif defined(MIPS)
885 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200886 if (upeek(tcp->pid, REG_EPC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100887 tprints("[????????] ");
888 return;
889 }
890 tprintf("[%08lx] ", pc);
891#elif defined(SH)
892 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200893 if (upeek(tcp->pid, 4*REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100894 tprints("[????????] ");
895 return;
896 }
897 tprintf("[%08lx] ", pc);
898#elif defined(SH64)
899 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200900 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100901 tprints("[????????????????] ");
902 return;
903 }
904 tprintf("[%08lx] ", pc);
905#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100906 tprintf("[%08lx] ", arm_regs.ARM_pc);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100907#elif defined(AARCH64)
908 /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100909#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100910 tprintf("[%08lx] ", avr32_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100911#elif defined(BFIN)
912 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200913 if (upeek(tcp->pid, PT_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100914 PRINTBADPC;
915 return;
916 }
917 tprintf("[%08lx] ", pc);
918#elif defined(CRISV10)
919 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200920 if (upeek(tcp->pid, 4*PT_IRP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100921 PRINTBADPC;
922 return;
923 }
924 tprintf("[%08lx] ", pc);
925#elif defined(CRISV32)
926 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200927 if (upeek(tcp->pid, 4*PT_ERP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100928 PRINTBADPC;
929 return;
930 }
931 tprintf("[%08lx] ", pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100932#elif defined(TILE)
933# ifdef _LP64
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500934 tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100935# else
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500936 tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100937# endif
Christian Svensson492f81f2013-02-14 13:26:27 +0100938#elif defined(OR1K)
939 tprintf("[%08lx] ", or1k_regs.pc);
James Hogan5f999a82013-02-22 14:44:10 +0000940#elif defined(METAG)
941 tprintf("[%08lx] ", metag_regs.pc);
Chris Zankel8f636ed2013-03-25 10:22:07 -0700942#elif defined(XTENSA)
943 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200944 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Chris Zankel8f636ed2013-03-25 10:22:07 -0700945 PRINTBADPC;
946 return;
947 }
948 tprintf("[%08lx] ", pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100949#endif /* architecture */
950}
951
Denys Vlasenko7270de52013-02-21 15:46:34 +0100952/* Shuffle syscall numbers so that we don't have huge gaps in syscall table.
953 * The shuffling should be reversible: shuffle_scno(shuffle_scno(n)) == n.
954 */
955#if defined(ARM) /* So far only ARM needs this */
956static long
957shuffle_scno(unsigned long scno)
958{
959 if (scno <= ARM_LAST_ORDINARY_SYSCALL)
960 return scno;
961
962 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
963 if (scno == 0x000ffff0)
964 return ARM_LAST_ORDINARY_SYSCALL+1;
965 if (scno == ARM_LAST_ORDINARY_SYSCALL+1)
966 return 0x000ffff0;
967
968 /* Is it ARM specific syscall?
969 * Swap with [LAST_ORDINARY+2, LAST_ORDINARY+2 + LAST_SPECIAL] range.
970 */
971 if (scno >= 0x000f0000
972 && scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL
973 ) {
974 return scno - 0x000f0000 + (ARM_LAST_ORDINARY_SYSCALL+2);
975 }
976 if (/* scno >= ARM_LAST_ORDINARY_SYSCALL+2 - always true */ 1
977 && scno <= (ARM_LAST_ORDINARY_SYSCALL+2) + ARM_LAST_SPECIAL_SYSCALL
978 ) {
979 return scno + 0x000f0000 - (ARM_LAST_ORDINARY_SYSCALL+2);
980 }
981
982 return scno;
983}
984#else
985# define shuffle_scno(scno) ((long)(scno))
986#endif
987
988static char*
989undefined_scno_name(struct tcb *tcp)
990{
991 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
992
993 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
994 return buf;
995}
996
Anton Blanchard14d51a62013-06-26 14:42:37 +1000997#ifdef POWERPC
998/*
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200999 * PTRACE_GETREGS was added to the PowerPC kernel in v2.6.23,
1000 * we provide a slow fallback for old kernels.
Anton Blanchard14d51a62013-06-26 14:42:37 +10001001 */
Denys Vlasenko7f5a1322013-06-28 14:36:39 +02001002static int powerpc_getregs_old(pid_t pid)
Anton Blanchard14d51a62013-06-26 14:42:37 +10001003{
1004 int i;
1005 long r;
1006
Denys Vlasenko7f5a1322013-06-28 14:36:39 +02001007 r = upeek(pid, sizeof(long) * PT_MSR, &ppc_regs.msr);
Anton Blanchard14d51a62013-06-26 14:42:37 +10001008 if (r)
1009 goto out;
Denys Vlasenko7f5a1322013-06-28 14:36:39 +02001010 r = upeek(pid, sizeof(long) * PT_CCR, &ppc_regs.ccr);
Anton Blanchard14d51a62013-06-26 14:42:37 +10001011 if (r)
1012 goto out;
Denys Vlasenko7f5a1322013-06-28 14:36:39 +02001013 r = upeek(pid, sizeof(long) * PT_ORIG_R3, &ppc_regs.orig_gpr3);
Anton Blanchard14d51a62013-06-26 14:42:37 +10001014 if (r)
1015 goto out;
Anton Blanchard14d51a62013-06-26 14:42:37 +10001016 for (i = 0; i <= 8; i++) {
Denys Vlasenko7f5a1322013-06-28 14:36:39 +02001017 r = upeek(pid, sizeof(long) * (PT_R0 + i), &ppc_regs.gpr[i]);
Anton Blanchard14d51a62013-06-26 14:42:37 +10001018 if (r)
1019 goto out;
1020 }
Denys Vlasenko7f5a1322013-06-28 14:36:39 +02001021 out:
Anton Blanchard14d51a62013-06-26 14:42:37 +10001022 return r;
1023}
1024#endif
1025
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001026#ifndef get_regs
1027long get_regs_error;
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001028
1029#if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001030static void get_regset(pid_t pid)
1031{
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001032/* constant iovec */
1033# if defined(ARM) \
1034 || defined(I386) \
1035 || defined(METAG) \
1036 || defined(OR1K)
1037 static struct iovec io = {
1038 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1039 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001040 };
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001041 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1042
1043/* variable iovec */
1044# elif defined(X86_64) || defined(X32)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001045 /* x86_io.iov_base = &x86_regs_union; - already is */
1046 x86_io.iov_len = sizeof(x86_regs_union);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001047 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &x86_io);
1048# elif defined(AARCH64)
1049 /* aarch64_io.iov_base = &arm_regs_union; - already is */
1050 aarch64_io.iov_len = sizeof(arm_regs_union);
1051 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &aarch64_io);
1052# else
1053# warning both PTRACE_GETREGSET and NT_PRSTATUS are available but not yet used
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001054# endif
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001055}
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001056#endif /* PTRACE_GETREGSET && NT_PRSTATUS */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001057
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001058void
1059get_regs(pid_t pid)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001060{
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001061/* PTRACE_GETREGSET only */
1062# if defined(METAG) || defined(OR1K) || defined(X32) || defined(AARCH64)
1063 get_regset(pid);
1064
1065/* PTRACE_GETREGS only */
1066# elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001067 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001068# elif defined(TILE)
1069 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &tile_regs);
1070# elif defined(SPARC) || defined(SPARC64)
1071 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001072# elif defined(POWERPC)
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001073 static bool old_kernel = 0;
1074 if (old_kernel)
1075 goto old;
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001076 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &ppc_regs);
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001077 if (get_regs_error && errno == EIO) {
1078 old_kernel = 1;
1079 old:
Anton Blanchard14d51a62013-06-26 14:42:37 +10001080 get_regs_error = powerpc_getregs_old(pid);
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001081 }
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001082
1083/* try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS */
1084# else
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001085# if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
1086 static int getregset_support;
1087
1088 if (getregset_support >= 0) {
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001089 get_regset(pid);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001090 if (getregset_support > 0)
1091 return;
1092 if (get_regs_error >= 0) {
1093 getregset_support = 1;
1094 return;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001095 }
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001096 if (errno == EPERM || errno == ESRCH)
1097 return;
1098 getregset_support = -1;
1099 }
1100# endif /* PTRACE_GETREGSET && NT_PRSTATUS */
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001101# if defined(ARM)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001102 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &arm_regs);
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001103# elif defined(I386)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001104 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &i386_regs);
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001105# elif defined(X86_64)
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001106 /* Use old method, with unreliable heuristical detection of 32-bitness. */
1107 x86_io.iov_len = sizeof(x86_64_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001108 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &x86_64_regs);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001109 if (!get_regs_error && x86_64_regs.cs == 0x23) {
1110 x86_io.iov_len = sizeof(i386_regs);
1111 /*
1112 * The order is important: i386_regs and x86_64_regs
1113 * are overlaid in memory!
1114 */
1115 i386_regs.ebx = x86_64_regs.rbx;
1116 i386_regs.ecx = x86_64_regs.rcx;
1117 i386_regs.edx = x86_64_regs.rdx;
1118 i386_regs.esi = x86_64_regs.rsi;
1119 i386_regs.edi = x86_64_regs.rdi;
1120 i386_regs.ebp = x86_64_regs.rbp;
1121 i386_regs.eax = x86_64_regs.rax;
1122 /* i386_regs.xds = x86_64_regs.ds; unused by strace */
1123 /* i386_regs.xes = x86_64_regs.es; ditto... */
1124 /* i386_regs.xfs = x86_64_regs.fs; */
1125 /* i386_regs.xgs = x86_64_regs.gs; */
1126 i386_regs.orig_eax = x86_64_regs.orig_rax;
1127 i386_regs.eip = x86_64_regs.rip;
1128 /* i386_regs.xcs = x86_64_regs.cs; */
1129 /* i386_regs.eflags = x86_64_regs.eflags; */
1130 i386_regs.esp = x86_64_regs.rsp;
1131 /* i386_regs.xss = x86_64_regs.ss; */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001132 }
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001133# else
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001134# error unhandled architecture
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001135# endif /* ARM || I386 || X86_64 */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001136# endif
1137}
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001138#endif /* !get_regs */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001139
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001140/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001141 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1142 * 1: ok, continue in trace_syscall_entering().
1143 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001144 * ("????" etc) and bail out.
1145 */
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01001146static int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001147get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001148{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001149 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001150
Denys Vlasenko523635f2012-02-25 02:44:25 +01001151#if defined(S390) || defined(S390X)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001152 if (upeek(tcp->pid, PT_GPR2, &syscall_mode) < 0)
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001153 return -1;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001154
1155 if (syscall_mode != -ENOSYS) {
1156 /*
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001157 * Since kernel version 2.5.44 the scno gets passed in gpr2.
Roland McGrath2f924ca2003-06-26 22:23:28 +00001158 */
1159 scno = syscall_mode;
1160 } else {
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001161 /*
Michal Ludvig882eda82002-11-11 12:50:47 +00001162 * Old style of "passing" the scno via the SVC instruction.
1163 */
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001164 long psw;
Michal Ludvig882eda82002-11-11 12:50:47 +00001165 long opcode, offset_reg, tmp;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001166 void *svc_addr;
Denys Vlasenko7c9ba8b2011-08-19 19:46:32 +02001167 static const int gpr_offset[16] = {
1168 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
1169 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
1170 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
1171 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
1172 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001173
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001174 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0)
Michal Ludvig882eda82002-11-11 12:50:47 +00001175 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001176 errno = 0;
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001177 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +00001178 if (errno) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001179 perror_msg("peektext(psw-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +00001180 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001181 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001182
1183 /*
1184 * We have to check if the SVC got executed directly or via an
1185 * EXECUTE instruction. In case of EXECUTE it is necessary to do
1186 * instruction decoding to derive the system call number.
1187 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1188 * so that this doesn't work if a SVC opcode is part of an EXECUTE
1189 * opcode. Since there is no way to find out the opcode size this
1190 * is the best we can do...
1191 */
Michal Ludvig882eda82002-11-11 12:50:47 +00001192 if ((opcode & 0xff00) == 0x0a00) {
1193 /* SVC opcode */
1194 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +00001195 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001196 else {
1197 /* SVC got executed by EXECUTE instruction */
1198
1199 /*
1200 * Do instruction decoding of EXECUTE. If you really want to
1201 * understand this, read the Principles of Operations.
1202 */
1203 svc_addr = (void *) (opcode & 0xfff);
1204
1205 tmp = 0;
1206 offset_reg = (opcode & 0x000f0000) >> 16;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001207 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001208 return -1;
1209 svc_addr += tmp;
1210
1211 tmp = 0;
1212 offset_reg = (opcode & 0x0000f000) >> 12;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001213 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001214 return -1;
1215 svc_addr += tmp;
1216
Denys Vlasenkofb036672009-01-23 16:30:26 +00001217 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
Michal Ludvig882eda82002-11-11 12:50:47 +00001218 if (errno)
1219 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001220# if defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +00001221 scno >>= 48;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001222# else
Michal Ludvig882eda82002-11-11 12:50:47 +00001223 scno >>= 16;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001224# endif
Michal Ludvig882eda82002-11-11 12:50:47 +00001225 tmp = 0;
1226 offset_reg = (opcode & 0x00f00000) >> 20;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001227 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001228 return -1;
1229
1230 scno = (scno | tmp) & 0xff;
1231 }
1232 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001233#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001234 scno = ppc_regs.gpr[0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001235# ifdef POWERPC64
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001236 int currpers;
Andreas Schwabd69fa492010-07-12 21:39:57 +02001237
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001238 /* Check for 64/32 bit mode. */
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001239 /* SF is bit 0 of MSR */
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001240 if ((ppc_regs.msr >> 63) & 1)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001241 currpers = 0;
1242 else
1243 currpers = 1;
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001244 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001245# endif
1246#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001247 scno = avr32_regs.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001248#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001249 if (upeek(tcp->pid, PT_ORIG_P0, &scno))
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001250 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001251#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001252 scno = i386_regs.orig_eax;
H.J. Lu35be5812012-04-16 13:00:01 +02001253#elif defined(X86_64) || defined(X32)
1254# ifndef __X32_SYSCALL_BIT
1255# define __X32_SYSCALL_BIT 0x40000000
1256# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001257 int currpers;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001258# if 1
1259 /* GETREGSET of NT_PRSTATUS tells us regset size,
1260 * which unambiguously detects i386.
1261 *
1262 * Linux kernel distinguishes x86-64 and x32 processes
1263 * solely by looking at __X32_SYSCALL_BIT:
1264 * arch/x86/include/asm/compat.h::is_x32_task():
1265 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1266 * return true;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001267 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001268 if (x86_io.iov_len == sizeof(i386_regs)) {
1269 scno = i386_regs.orig_eax;
1270 currpers = 1;
1271 } else {
1272 scno = x86_64_regs.orig_rax;
1273 currpers = 0;
1274 if (scno & __X32_SYSCALL_BIT) {
1275 scno -= __X32_SYSCALL_BIT;
1276 currpers = 2;
1277 }
1278 }
1279# elif 0
1280 /* cs = 0x33 for long mode (native 64 bit and x32)
1281 * cs = 0x23 for compatibility mode (32 bit)
1282 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1283 */
1284 scno = x86_64_regs.orig_rax;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001285 switch (x86_64_regs.cs) {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001286 case 0x23: currpers = 1; break;
H.J. Lu35be5812012-04-16 13:00:01 +02001287 case 0x33:
1288 if (x86_64_regs.ds == 0x2b) {
1289 currpers = 2;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001290 scno &= ~__X32_SYSCALL_BIT;
H.J. Lu35be5812012-04-16 13:00:01 +02001291 } else
1292 currpers = 0;
1293 break;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001294 default:
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001295 fprintf(stderr, "Unknown value CS=0x%08X while "
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001296 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001297 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001298 currpers = current_personality;
1299 break;
1300 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001301# elif 0
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001302 /* This version analyzes the opcode of a syscall instruction.
1303 * (int 0x80 on i386 vs. syscall on x86-64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001304 * It works, but is too complicated, and strictly speaking, unreliable.
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001305 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001306 unsigned long call, rip = x86_64_regs.rip;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001307 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1308 rip -= 2;
1309 errno = 0;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001310 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001311 if (errno)
1312 fprintf(stderr, "ptrace_peektext failed: %s\n",
1313 strerror(errno));
1314 switch (call & 0xffff) {
1315 /* x86-64: syscall = 0x0f 0x05 */
1316 case 0x050f: currpers = 0; break;
1317 /* i386: int 0x80 = 0xcd 0x80 */
1318 case 0x80cd: currpers = 1; break;
1319 default:
1320 currpers = current_personality;
1321 fprintf(stderr,
1322 "Unknown syscall opcode (0x%04X) while "
1323 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001324 "PID=%d\n", (int)call, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001325 break;
1326 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001327# endif
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001328
H.J. Lu35be5812012-04-16 13:00:01 +02001329# ifdef X32
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001330 /* If we are built for a x32 system, then personality 0 is x32
1331 * (not x86_64), and stracing of x86_64 apps is not supported.
1332 * Stracing of i386 apps is still supported.
H.J. Lu085e4282012-04-17 11:05:04 -07001333 */
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001334 if (currpers == 0) {
1335 fprintf(stderr, "syscall_%lu(...) in unsupported "
1336 "64-bit mode of process PID=%d\n",
1337 scno, tcp->pid);
1338 return 0;
H.J. Lu35be5812012-04-16 13:00:01 +02001339 }
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001340 currpers &= ~2; /* map 2,1 to 0,1 */
H.J. Lu35be5812012-04-16 13:00:01 +02001341# endif
H.J. Lu085e4282012-04-17 11:05:04 -07001342 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001343#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001344# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01001345 long psr;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001346 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001347 ia32 = (psr & IA64_PSR_IS) != 0;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001348 if (ia32) {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001349 if (upeek(tcp->pid, PT_R1, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001350 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001351 } else {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001352 if (upeek(tcp->pid, PT_R15, &scno) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001353 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001354 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001355#elif defined(AARCH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001356 switch (aarch64_io.iov_len) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001357 case sizeof(aarch64_regs):
1358 /* We are in 64-bit mode */
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001359 scno = aarch64_regs.regs[8];
1360 update_personality(tcp, 1);
1361 break;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001362 case sizeof(arm_regs):
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001363 /* We are in 32-bit mode */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001364 scno = arm_regs.ARM_r7;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001365 update_personality(tcp, 0);
1366 break;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001367 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001368#elif defined(ARM)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001369 if (arm_regs.ARM_ip != 0) {
1370 /* It is not a syscall entry */
1371 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001372 tcp->flags |= TCB_INSYSCALL;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001373 return 0;
1374 }
1375 /* Note: we support only 32-bit CPUs, not 26-bit */
1376
1377 if (arm_regs.ARM_cpsr & 0x20) {
1378 /* Thumb mode */
1379 scno = arm_regs.ARM_r7;
1380 } else {
1381 /* ARM mode */
1382 errno = 0;
1383 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1384 if (errno)
1385 return -1;
1386
1387 /* EABI syscall convention? */
1388 if (scno == 0xef000000) {
1389 scno = arm_regs.ARM_r7; /* yes */
1390 } else {
1391 if ((scno & 0x0ff00000) != 0x0f900000) {
1392 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1393 tcp->pid, scno);
1394 return -1;
1395 }
1396 /* Fixup the syscall number */
1397 scno &= 0x000fffff;
1398 }
1399 }
Denys Vlasenko7270de52013-02-21 15:46:34 +01001400
1401 scno = shuffle_scno(scno);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001402#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001403 if (upeek(tcp->pid, 4*PT_ORIG_D0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001404 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001405#elif defined(LINUX_MIPSN32)
Roland McGrath542c2c62008-05-20 01:11:56 +00001406 unsigned long long regs[38];
1407
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001408 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001409 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001410 mips_a3 = regs[REG_A3];
1411 mips_r2 = regs[REG_V0];
Roland McGrath542c2c62008-05-20 01:11:56 +00001412
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001413 scno = mips_r2;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001414 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001415 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001416 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001417 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001418 return 0;
1419 }
Roland McGrath542c2c62008-05-20 01:11:56 +00001420 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001421#elif defined(MIPS)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001422 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001423 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001424 if (upeek(tcp->pid, REG_V0, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001425 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001426
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001427 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001428 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001429 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001430 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001431 return 0;
1432 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001433 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001434#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001435 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001436 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001437 if (upeek(tcp->pid, REG_R0, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001438 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001439
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001440 /*
1441 * Do some sanity checks to figure out if it's
1442 * really a syscall entry
1443 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001444 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001445 if (alpha_a3 == 0 || alpha_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001446 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001447 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001448 return 0;
1449 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001450 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001451#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001452 /* Disassemble the syscall trap. */
1453 /* Retrieve the syscall trap instruction. */
Denys Vlasenko46455822013-02-05 17:02:59 +01001454 unsigned long trap;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001455 errno = 0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001456# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001457 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001458 trap >>= 32;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001459# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001460 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001461# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001462 if (errno)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001463 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001464
1465 /* Disassemble the trap to see what personality to use. */
1466 switch (trap) {
1467 case 0x91d02010:
1468 /* Linux/SPARC syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001469 update_personality(tcp, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001470 break;
1471 case 0x91d0206d:
1472 /* Linux/SPARC64 syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001473 update_personality(tcp, 2);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001474 break;
1475 case 0x91d02000:
1476 /* SunOS syscall trap. (pers 1) */
1477 fprintf(stderr, "syscall: SunOS no support\n");
1478 return -1;
1479 case 0x91d02008:
1480 /* Solaris 2.x syscall trap. (per 2) */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001481 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001482 break;
1483 case 0x91d02009:
1484 /* NetBSD/FreeBSD syscall trap. */
1485 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1486 return -1;
1487 case 0x91d02027:
1488 /* Solaris 2.x gettimeofday */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001489 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001490 break;
1491 default:
Denys Vlasenko523635f2012-02-25 02:44:25 +01001492# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001493 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001494# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001495 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001496# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001497 return -1;
1498 }
1499
1500 /* Extract the system call number from the registers. */
1501 if (trap == 0x91d02027)
1502 scno = 156;
1503 else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001504 scno = sparc_regs.u_regs[U_REG_G1];
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001505 if (scno == 0) {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001506 scno = sparc_regs.u_regs[U_REG_O0];
1507 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 +02001508 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001509#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001510 if (upeek(tcp->pid, PT_GR20, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001511 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001512#elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001513 /*
1514 * In the new syscall ABI, the system call number is in R3.
1515 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001516 if (upeek(tcp->pid, 4*(REG_REG0+3), &scno) < 0)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001517 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001518
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001519 if (scno < 0) {
1520 /* Odd as it may seem, a glibc bug has been known to cause
1521 glibc to issue bogus negative syscall numbers. So for
1522 our purposes, make strace print what it *should* have been */
1523 long correct_scno = (scno & 0xff);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001524 if (debug_flag)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001525 fprintf(stderr,
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001526 "Detected glibc bug: bogus system call"
1527 " number = %ld, correcting to %ld\n",
1528 scno,
1529 correct_scno);
1530 scno = correct_scno;
1531 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001532#elif defined(SH64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001533 if (upeek(tcp->pid, REG_SYSCALL, &scno) < 0)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001534 return -1;
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001535 scno &= 0xFFFF;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001536#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001537 if (upeek(tcp->pid, 4*PT_R9, &scno) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001538 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001539#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001540 int currpers;
1541 scno = tile_regs.regs[10];
1542# ifdef __tilepro__
1543 currpers = 1;
1544# else
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001545# ifndef PT_FLAGS_COMPAT
1546# define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1547# endif
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001548 if (tile_regs.flags & PT_FLAGS_COMPAT)
1549 currpers = 1;
1550 else
1551 currpers = 0;
1552# endif
1553 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001554#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001555 if (upeek(tcp->pid, 0, &scno) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001556 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001557#elif defined(OR1K)
1558 scno = or1k_regs.gpr[11];
James Hogan5f999a82013-02-22 14:44:10 +00001559#elif defined(METAG)
1560 scno = metag_regs.dx[0][1]; /* syscall number in D1Re0 (D1.0) */
Chris Zankel8f636ed2013-03-25 10:22:07 -07001561#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001562 if (upeek(tcp->pid, SYSCALL_NR, &scno) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001563 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001564#endif
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001565
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001566 tcp->scno = scno;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001567 if (SCNO_IS_VALID(tcp->scno)) {
1568 tcp->s_ent = &sysent[scno];
1569 tcp->qual_flg = qual_flags[scno];
1570 } else {
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01001571 static const struct_sysent unknown = {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001572 .nargs = MAX_ARGS,
1573 .sys_flags = 0,
1574 .sys_func = printargs,
1575 .sys_name = "unknown", /* not used */
1576 };
1577 tcp->s_ent = &unknown;
1578 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1579 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001580 return 1;
1581}
1582
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02001583/* Called at each syscall entry.
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001584 * Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001585 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1586 * 1: ok, continue in trace_syscall_entering().
1587 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001588 * ("????" etc) and bail out.
1589 */
Roland McGratha4d48532005-06-08 20:45:28 +00001590static int
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001591syscall_fixup_on_sysenter(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001592{
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001593 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001594#if defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001595 if (i386_regs.eax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001596 if (debug_flag)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001597 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1598 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001599 }
H.J. Lu35be5812012-04-16 13:00:01 +02001600#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001601 {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001602 long rax;
1603 if (x86_io.iov_len == sizeof(i386_regs)) {
1604 /* Sign extend from 32 bits */
1605 rax = (int32_t)i386_regs.eax;
1606 } else {
1607 /* Note: in X32 build, this truncates 64 to 32 bits */
1608 rax = x86_64_regs.rax;
1609 }
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001610 if (rax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001611 if (debug_flag)
Denys Vlasenko18beb982011-08-24 16:59:23 +02001612 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1613 return 0;
1614 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001615 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001616#elif defined(S390) || defined(S390X)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001617 /* TODO: we already fetched PT_GPR2 in get_scno
1618 * and stored it in syscall_mode, reuse it here
1619 * instead of re-fetching?
1620 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001621 if (upeek(tcp->pid, PT_GPR2, &gpr2) < 0)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001622 return -1;
Michal Ludvig882eda82002-11-11 12:50:47 +00001623 if (syscall_mode != -ENOSYS)
1624 syscall_mode = tcp->scno;
Denys Vlasenkoece98792011-08-25 10:25:35 +02001625 if (gpr2 != syscall_mode) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001626 if (debug_flag)
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001627 fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2);
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001628 return 0;
1629 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001630#elif defined(M68K)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001631 /* TODO? Eliminate upeek's in arches below like we did in x86 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001632 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001633 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001634 if (m68k_d0 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001635 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001636 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001637 return 0;
1638 }
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001639#elif defined(IA64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001640 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001641 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001642 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001643 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001644 if (ia32 && ia64_r8 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001645 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001646 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001647 return 0;
1648 }
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001649#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001650 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001651 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001652 if (cris_r10 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001653 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001654 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001655 return 0;
1656 }
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001657#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001658 if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001659 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001660 if (microblaze_r3 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001661 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001662 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001663 return 0;
1664 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001665#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001666 return 1;
1667}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001668
Denys Vlasenko146b9442012-03-18 22:10:48 +01001669static void
1670internal_fork(struct tcb *tcp)
1671{
1672#if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1673# define ARG_FLAGS 1
1674#else
1675# define ARG_FLAGS 0
1676#endif
1677#ifndef CLONE_UNTRACED
1678# define CLONE_UNTRACED 0x00800000
1679#endif
1680 if ((ptrace_setoptions
1681 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1682 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1683 return;
1684
1685 if (!followfork)
1686 return;
1687
1688 if (entering(tcp)) {
1689 /*
1690 * We won't see the new child if clone is called with
1691 * CLONE_UNTRACED, so we keep the same logic with that option
1692 * and don't trace it.
1693 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001694 if ((tcp->s_ent->sys_func == sys_clone)
1695 && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
1696 )
Denys Vlasenko146b9442012-03-18 22:10:48 +01001697 return;
1698 setbpt(tcp);
1699 } else {
1700 if (tcp->flags & TCB_BPTSET)
1701 clearbpt(tcp);
1702 }
1703}
1704
1705#if defined(TCB_WAITEXECVE)
1706static void
1707internal_exec(struct tcb *tcp)
1708{
1709 /* Maybe we have post-execve SIGTRAP suppressed? */
1710 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1711 return; /* yes, no need to do anything */
1712
1713 if (exiting(tcp) && syserror(tcp))
1714 /* Error in execve, no post-execve SIGTRAP expected */
1715 tcp->flags &= ~TCB_WAITEXECVE;
1716 else
1717 tcp->flags |= TCB_WAITEXECVE;
1718}
1719#endif
1720
1721static void
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001722syscall_fixup_for_fork_exec(struct tcb *tcp)
Roland McGrathc1e45922008-05-27 23:18:29 +00001723{
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001724 /*
1725 * We must always trace a few critical system calls in order to
1726 * correctly support following forks in the presence of tracing
1727 * qualifiers.
1728 */
1729 int (*func)();
1730
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001731 func = tcp->s_ent->sys_func;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001732
1733 if ( sys_fork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001734 || sys_vfork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001735 || sys_clone == func
Denys Vlasenko146b9442012-03-18 22:10:48 +01001736 ) {
1737 internal_fork(tcp);
1738 return;
1739 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001740
Denys Vlasenko84703742012-02-25 02:38:52 +01001741#if defined(TCB_WAITEXECVE)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001742 if ( sys_execve == func
Denys Vlasenko84703742012-02-25 02:38:52 +01001743# if defined(SPARC) || defined(SPARC64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001744 || sys_execv == func
Denys Vlasenkoa7949742011-08-21 17:26:55 +02001745# endif
Denys Vlasenko146b9442012-03-18 22:10:48 +01001746 ) {
1747 internal_exec(tcp);
1748 return;
1749 }
Roland McGrathc1e45922008-05-27 23:18:29 +00001750#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001751}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001752
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001753/* Return -1 on error or 1 on success (never 0!) */
Roland McGratha4d48532005-06-08 20:45:28 +00001754static int
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001755get_syscall_args(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001756{
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001757 int i, nargs;
1758
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001759 nargs = tcp->s_ent->nargs;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001760
Denys Vlasenko523635f2012-02-25 02:44:25 +01001761#if defined(S390) || defined(S390X)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001762 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001763 if (upeek(tcp->pid, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001764 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001765#elif defined(ALPHA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001766 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001767 if (upeek(tcp->pid, REG_A0+i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001768 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001769#elif defined(IA64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001770 if (!ia32) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001771 unsigned long *out0, cfm, sof, sol;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001772 long rbs_end;
1773 /* be backwards compatible with kernel < 2.4.4... */
1774# ifndef PT_RBS_END
1775# define PT_RBS_END PT_AR_BSP
1776# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001777
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001778 if (upeek(tcp->pid, PT_RBS_END, &rbs_end) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001779 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001780 if (upeek(tcp->pid, PT_CFM, (long *) &cfm) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001781 return -1;
1782
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001783 sof = (cfm >> 0) & 0x7f;
1784 sol = (cfm >> 7) & 0x7f;
1785 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1786
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001787 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001788 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1789 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1790 return -1;
1791 }
1792 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001793 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1794 PT_R9 /* ECX = out1 */,
1795 PT_R10 /* EDX = out2 */,
1796 PT_R14 /* ESI = out3 */,
1797 PT_R15 /* EDI = out4 */,
1798 PT_R13 /* EBP = out5 */};
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001799
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001800 for (i = 0; i < nargs; ++i) {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001801 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001802 return -1;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001803 /* truncate away IVE sign-extension */
1804 tcp->u_arg[i] &= 0xffffffff;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001805 }
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001806 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001807#elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001808 /* N32 and N64 both use up to six registers. */
1809 unsigned long long regs[38];
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001810
1811 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1812 return -1;
1813
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001814 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001815 tcp->u_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001816# if defined(LINUX_MIPSN32)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001817 tcp->ext_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001818# endif
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001819 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001820#elif defined(MIPS)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001821 if (nargs > 4) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001822 long sp;
1823
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001824 if (upeek(tcp->pid, REG_SP, &sp) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001825 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001826 for (i = 0; i < 4; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001827 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001828 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001829 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001830 (char *)(tcp->u_arg + 4));
1831 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001832 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001833 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001834 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001835 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001836#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001837 (void)i;
1838 (void)nargs;
1839 tcp->u_arg[0] = ppc_regs.orig_gpr3;
1840 tcp->u_arg[1] = ppc_regs.gpr[4];
1841 tcp->u_arg[2] = ppc_regs.gpr[5];
1842 tcp->u_arg[3] = ppc_regs.gpr[6];
1843 tcp->u_arg[4] = ppc_regs.gpr[7];
1844 tcp->u_arg[5] = ppc_regs.gpr[8];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001845#elif defined(SPARC) || defined(SPARC64)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001846 for (i = 0; i < nargs; ++i)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001847 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001848#elif defined(HPPA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001849 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001850 if (upeek(tcp->pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001851 return -1;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001852#elif defined(ARM) || defined(AARCH64)
1853# if defined(AARCH64)
1854 if (tcp->currpers == 1)
1855 for (i = 0; i < nargs; ++i)
1856 tcp->u_arg[i] = aarch64_regs.regs[i];
1857 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001858# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001859 for (i = 0; i < nargs; ++i)
Denys Vlasenko401374e2013-02-06 18:24:39 +01001860 tcp->u_arg[i] = arm_regs.uregs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001861#elif defined(AVR32)
Denys Vlasenkob5b25892011-08-30 19:04:54 +02001862 (void)i;
1863 (void)nargs;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001864 tcp->u_arg[0] = avr32_regs.r12;
1865 tcp->u_arg[1] = avr32_regs.r11;
1866 tcp->u_arg[2] = avr32_regs.r10;
1867 tcp->u_arg[3] = avr32_regs.r9;
1868 tcp->u_arg[4] = avr32_regs.r5;
1869 tcp->u_arg[5] = avr32_regs.r3;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001870#elif defined(BFIN)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001871 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 +02001872
Denys Vlasenko4b887a52011-08-23 13:32:38 +02001873 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001874 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001875 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001876#elif defined(SH)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001877 static const int syscall_regs[MAX_ARGS] = {
1878 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1879 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001880 };
1881
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001882 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001883 if (upeek(tcp->pid, syscall_regs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001884 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001885#elif defined(SH64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001886 int i;
1887 /* Registers used by SH5 Linux system calls for parameters */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001888 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001889
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001890 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001891 if (upeek(tcp->pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001892 return -1;
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001893#elif defined(I386)
1894 (void)i;
1895 (void)nargs;
1896 tcp->u_arg[0] = i386_regs.ebx;
1897 tcp->u_arg[1] = i386_regs.ecx;
1898 tcp->u_arg[2] = i386_regs.edx;
1899 tcp->u_arg[3] = i386_regs.esi;
1900 tcp->u_arg[4] = i386_regs.edi;
1901 tcp->u_arg[5] = i386_regs.ebp;
H.J. Lu35be5812012-04-16 13:00:01 +02001902#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001903 (void)i;
1904 (void)nargs;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001905 if (x86_io.iov_len != sizeof(i386_regs)) {
1906 /* x86-64 or x32 ABI */
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001907 tcp->u_arg[0] = x86_64_regs.rdi;
1908 tcp->u_arg[1] = x86_64_regs.rsi;
1909 tcp->u_arg[2] = x86_64_regs.rdx;
1910 tcp->u_arg[3] = x86_64_regs.r10;
1911 tcp->u_arg[4] = x86_64_regs.r8;
1912 tcp->u_arg[5] = x86_64_regs.r9;
H.J. Lu35be5812012-04-16 13:00:01 +02001913# ifdef X32
1914 tcp->ext_arg[0] = x86_64_regs.rdi;
1915 tcp->ext_arg[1] = x86_64_regs.rsi;
1916 tcp->ext_arg[2] = x86_64_regs.rdx;
1917 tcp->ext_arg[3] = x86_64_regs.r10;
1918 tcp->ext_arg[4] = x86_64_regs.r8;
1919 tcp->ext_arg[5] = x86_64_regs.r9;
1920# endif
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001921 } else {
1922 /* i386 ABI */
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001923 /* Zero-extend from 32 bits */
1924 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1925 * if you need to use *sign-extended* parameter.
1926 */
1927 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1928 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1929 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1930 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1931 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1932 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001933 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001934#elif defined(MICROBLAZE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001935 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001936 if (upeek(tcp->pid, (5 + i) * 4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001937 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001938#elif defined(CRISV10) || defined(CRISV32)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001939 static const int crisregs[MAX_ARGS] = {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001940 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001941 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001942 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001943
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001944 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001945 if (upeek(tcp->pid, crisregs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001946 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001947#elif defined(TILE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001948 for (i = 0; i < nargs; ++i)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001949 tcp->u_arg[i] = tile_regs.regs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001950#elif defined(M68K)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001951 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001952 if (upeek(tcp->pid, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001953 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001954#elif defined(OR1K)
1955 (void)nargs;
1956 for (i = 0; i < 6; ++i)
1957 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
James Hogan5f999a82013-02-22 14:44:10 +00001958#elif defined(METAG)
1959 for (i = 0; i < nargs; i++)
1960 /* arguments go backwards from D1Ar1 (D1.3) */
1961 tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
Chris Zankel8f636ed2013-03-25 10:22:07 -07001962#elif defined(XTENSA)
1963 /* arg0: a6, arg1: a3, arg2: a4, arg3: a5, arg4: a8, arg5: a9 */
1964 static const int xtensaregs[MAX_ARGS] = { 6, 3, 4, 5, 8, 9 };
1965 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001966 if (upeek(tcp->pid, REG_A_BASE + xtensaregs[i], &tcp->u_arg[i]) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001967 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001968#else /* Other architecture (32bits specific) */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001969 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001970 if (upeek(tcp->pid, i*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001971 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001972#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001973 return 1;
1974}
1975
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00001976static int
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001977trace_syscall_entering(struct tcb *tcp)
1978{
1979 int res, scno_good;
1980
Denys Vlasenko2ce12ed2011-08-24 17:25:32 +02001981#if defined TCB_WAITEXECVE
1982 if (tcp->flags & TCB_WAITEXECVE) {
1983 /* This is the post-execve SIGTRAP. */
1984 tcp->flags &= ~TCB_WAITEXECVE;
1985 return 0;
1986 }
1987#endif
1988
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001989 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001990 if (res == 0)
1991 return res;
Denys Vlasenko907735a2012-03-21 00:23:16 +01001992 if (res == 1) {
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001993 res = syscall_fixup_on_sysenter(tcp);
Denys Vlasenko907735a2012-03-21 00:23:16 +01001994 if (res == 0)
1995 return res;
1996 if (res == 1)
1997 res = get_syscall_args(tcp);
1998 }
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001999
2000 if (res != 1) {
2001 printleader(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002002 if (scno_good != 1)
Denys Vlasenkob7a6dae2012-03-20 16:48:35 +01002003 tprints("????" /* anti-trigraph gap */ "(");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002004 else if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002005 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002006 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002007 tprintf("%s(", tcp->s_ent->sys_name);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002008 /*
2009 * " <unavailable>" will be added later by the code which
2010 * detects ptrace errors.
2011 */
2012 goto ret;
2013 }
2014
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002015 if ( sys_execve == tcp->s_ent->sys_func
2016# if defined(SPARC) || defined(SPARC64)
2017 || sys_execv == tcp->s_ent->sys_func
2018# endif
2019 ) {
2020 hide_log_until_execve = 0;
2021 }
2022
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002023#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002024 while (1) {
Denys Vlasenko523635f2012-02-25 02:44:25 +01002025# ifdef SYS_socket_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002026 if (tcp->s_ent->sys_func == sys_socketcall) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00002027 decode_socket_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002028 break;
2029 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002030# endif
2031# ifdef SYS_ipc_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002032 if (tcp->s_ent->sys_func == sys_ipc) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00002033 decode_ipc_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002034 break;
2035 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002036# endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002037 break;
2038 }
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002039#endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002040
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01002041 if (need_fork_exec_workarounds)
2042 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002043
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002044 if (!(tcp->qual_flg & QUAL_TRACE)
2045 || (tracing_paths && !pathtrace_match(tcp))
2046 ) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002047 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
2048 return 0;
2049 }
2050
2051 tcp->flags &= ~TCB_FILTERED;
2052
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002053 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002054 res = 0;
2055 goto ret;
2056 }
2057
2058 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002059 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002060 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002061 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002062 tprintf("%s(", tcp->s_ent->sys_name);
2063 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002064 res = printargs(tcp);
2065 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002066 res = tcp->s_ent->sys_func(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002067
Dmitry V. Levinb742d8c2012-09-17 22:40:12 +00002068 fflush(tcp->outf);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002069 ret:
2070 tcp->flags |= TCB_INSYSCALL;
2071 /* Measure the entrance time as late as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002072 if (Tflag || cflag)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002073 gettimeofday(&tcp->etime, NULL);
2074 return res;
2075}
2076
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002077/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01002078 * 1: ok, continue in trace_syscall_exiting().
2079 * -1: error, trace_syscall_exiting() should print error indicator
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002080 * ("????" etc) and bail out.
2081 */
2082static int
2083get_syscall_result(struct tcb *tcp)
2084{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002085#if defined(S390) || defined(S390X)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002086 if (upeek(tcp->pid, PT_GPR2, &gpr2) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002087 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002088#elif defined(POWERPC)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002089# define SO_MASK 0x10000000
Anton Blanchardce6e33b2013-06-26 15:53:33 +02002090 ppc_result = ppc_regs.gpr[3];
2091 if (ppc_regs.ccr & SO_MASK)
2092 ppc_result = -ppc_result;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002093#elif defined(AVR32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002094 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002095#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002096 if (upeek(tcp->pid, PT_R0, &bfin_r0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002097 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002098#elif defined(I386)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002099 /* already done by get_regs */
H.J. Lu35be5812012-04-16 13:00:01 +02002100#elif defined(X86_64) || defined(X32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002101 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002102#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002103# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01002104 long psr;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002105 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002106 ia32 = (psr & IA64_PSR_IS) != 0;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002107 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002108 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002109 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002110 return -1;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002111#elif defined(ARM)
2112 /* already done by get_regs */
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002113#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002114 /* register reading already done by get_regs */
2115
2116 /* Used to do this, but we did it on syscall entry already: */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002117 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
2118 * else it's personality 0.
2119 */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002120 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
Denys Vlasenko523635f2012-02-25 02:44:25 +01002121#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002122 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002123 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002124#elif defined(LINUX_MIPSN32)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002125 unsigned long long regs[38];
2126
2127 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
2128 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002129 mips_a3 = regs[REG_A3];
2130 mips_r2 = regs[REG_V0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01002131#elif defined(MIPS)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002132 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002133 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002134 if (upeek(tcp->pid, REG_V0, &mips_r2) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002135 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002136#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002137 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002138 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002139 if (upeek(tcp->pid, REG_R0, &alpha_r0) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002140 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002141#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002142 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002143#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002144 if (upeek(tcp->pid, PT_GR28, &hppa_r28) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002145 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002146#elif defined(SH)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002147 /* new syscall ABI returns result in R0 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002148 if (upeek(tcp->pid, 4*REG_REG0, (long *)&sh_r0) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002149 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002150#elif defined(SH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002151 /* ABI defines result returned in r9 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002152 if (upeek(tcp->pid, REG_GENERAL(9), (long *)&sh64_r9) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002153 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002154#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002155 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002156 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002157#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002158 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002159#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002160 if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002161 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01002162#elif defined(OR1K)
2163 /* already done by get_regs */
James Hogan5f999a82013-02-22 14:44:10 +00002164#elif defined(METAG)
2165 /* already done by get_regs */
Chris Zankel8f636ed2013-03-25 10:22:07 -07002166#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002167 if (upeek(tcp->pid, REG_A_BASE + 2, &xtensa_a2) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07002168 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002169#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002170 return 1;
2171}
2172
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002173/* Called at each syscall exit */
2174static void
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002175syscall_fixup_on_sysexit(struct tcb *tcp)
2176{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002177#if defined(S390) || defined(S390X)
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002178 if (syscall_mode != -ENOSYS)
2179 syscall_mode = tcp->scno;
Denys Vlasenkoece98792011-08-25 10:25:35 +02002180 if ((tcp->flags & TCB_WAITEXECVE)
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002181 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
2182 /*
2183 * Return from execve.
2184 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2185 * flag set for the post-execve SIGTRAP to see and reset.
2186 */
2187 gpr2 = 0;
2188 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002189#endif
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002190}
2191
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002192/*
2193 * Check the syscall return value register value for whether it is
2194 * a negated errno code indicating an error, or a success return value.
2195 */
2196static inline int
2197is_negated_errno(unsigned long int val)
2198{
2199 unsigned long int max = -(long int) nerrnos;
Denys Vlasenko2544f982013-02-19 17:39:56 +01002200#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01002201 if (current_wordsize < sizeof(val)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002202 val = (unsigned int) val;
2203 max = (unsigned int) max;
2204 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002205#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002206 return val > max;
2207}
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002208
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002209#if defined(X32)
2210static inline int
2211is_negated_errno_x32(unsigned long long val)
2212{
2213 unsigned long long max = -(long long) nerrnos;
2214 /*
2215 * current_wordsize is 4 even in personality 0 (native X32)
2216 * but truncation _must not_ be done in it.
2217 * can't check current_wordsize here!
2218 */
2219 if (current_personality != 0) {
2220 val = (uint32_t) val;
2221 max = (uint32_t) max;
2222 }
2223 return val > max;
2224}
2225#endif
2226
Denys Vlasenko907735a2012-03-21 00:23:16 +01002227/* Returns:
2228 * 1: ok, continue in trace_syscall_exiting().
2229 * -1: error, trace_syscall_exiting() should print error indicator
2230 * ("????" etc) and bail out.
2231 */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002232static void
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002233get_error(struct tcb *tcp)
2234{
2235 int u_error = 0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002236 int check_errno = 1;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002237 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002238 check_errno = 0;
2239 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002240#if defined(S390) || defined(S390X)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002241 if (check_errno && is_negated_errno(gpr2)) {
2242 tcp->u_rval = -1;
2243 u_error = -gpr2;
2244 }
2245 else {
2246 tcp->u_rval = gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002247 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002248#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002249 if (check_errno && is_negated_errno(i386_regs.eax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002250 tcp->u_rval = -1;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002251 u_error = -i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002252 }
2253 else {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002254 tcp->u_rval = i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002255 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002256#elif defined(X86_64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002257 long rax;
2258 if (x86_io.iov_len == sizeof(i386_regs)) {
2259 /* Sign extend from 32 bits */
2260 rax = (int32_t)i386_regs.eax;
2261 } else {
2262 rax = x86_64_regs.rax;
2263 }
2264 if (check_errno && is_negated_errno(rax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002265 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002266 u_error = -rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002267 }
2268 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002269 tcp->u_rval = rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002270 }
2271#elif defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002272 /* In X32, return value is 64-bit (llseek uses one).
2273 * Using merely "long rax" would not work.
2274 */
2275 long long rax;
2276 if (x86_io.iov_len == sizeof(i386_regs)) {
2277 /* Sign extend from 32 bits */
2278 rax = (int32_t)i386_regs.eax;
2279 } else {
2280 rax = x86_64_regs.rax;
2281 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002282 /* Careful: is_negated_errno() works only on longs */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002283 if (check_errno && is_negated_errno_x32(rax)) {
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002284 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002285 u_error = -rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002286 }
2287 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002288 tcp->u_rval = rax; /* truncating */
2289 tcp->u_lrval = rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002290 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002291#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002292 if (ia32) {
2293 int err;
2294
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002295 err = (int)ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002296 if (check_errno && is_negated_errno(err)) {
2297 tcp->u_rval = -1;
2298 u_error = -err;
2299 }
2300 else {
2301 tcp->u_rval = err;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002302 }
2303 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002304 if (check_errno && ia64_r10) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002305 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002306 u_error = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002307 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002308 tcp->u_rval = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002309 }
2310 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002311#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002312 if (check_errno && mips_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002313 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002314 u_error = mips_r2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002315 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002316 tcp->u_rval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002317# if defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002318 tcp->u_lrval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002319# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002320 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002321#elif defined(POWERPC)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002322 if (check_errno && is_negated_errno(ppc_result)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002323 tcp->u_rval = -1;
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002324 u_error = -ppc_result;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002325 }
2326 else {
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002327 tcp->u_rval = ppc_result;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002328 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002329#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002330 if (check_errno && is_negated_errno(m68k_d0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002331 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002332 u_error = -m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002333 }
2334 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002335 tcp->u_rval = m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002336 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002337#elif defined(ARM) || defined(AARCH64)
2338# if defined(AARCH64)
2339 if (tcp->currpers == 1) {
2340 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2341 tcp->u_rval = -1;
2342 u_error = -aarch64_regs.regs[0];
2343 }
2344 else {
2345 tcp->u_rval = aarch64_regs.regs[0];
2346 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002347 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002348 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002349# endif
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002350 {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002351 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002352 tcp->u_rval = -1;
Denys Vlasenko401374e2013-02-06 18:24:39 +01002353 u_error = -arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002354 }
2355 else {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002356 tcp->u_rval = arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002357 }
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002358 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002359#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002360 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002361 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002362 u_error = -avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002363 }
2364 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002365 tcp->u_rval = avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002366 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002367#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002368 if (check_errno && is_negated_errno(bfin_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002369 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002370 u_error = -bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002371 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002372 tcp->u_rval = bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002373 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002374#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002375 if (check_errno && alpha_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002376 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002377 u_error = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002378 }
2379 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002380 tcp->u_rval = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002381 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002382#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002383 if (check_errno && sparc_regs.psr & PSR_C) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002384 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002385 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002386 }
2387 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002388 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002389 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002390#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002391 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002392 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002393 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002394 }
2395 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002396 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002397 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002398#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002399 if (check_errno && is_negated_errno(hppa_r28)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002400 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002401 u_error = -hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002402 }
2403 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002404 tcp->u_rval = hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002405 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002406#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002407 if (check_errno && is_negated_errno(sh_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002408 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002409 u_error = -sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002410 }
2411 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002412 tcp->u_rval = sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002413 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002414#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002415 if (check_errno && is_negated_errno(sh64_r9)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002416 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002417 u_error = -sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002418 }
2419 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002420 tcp->u_rval = sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002421 }
James Hogan5f999a82013-02-22 14:44:10 +00002422#elif defined(METAG)
2423 /* result pointer in D0Re0 (D0.0) */
2424 if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2425 tcp->u_rval = -1;
2426 u_error = -metag_regs.dx[0][0];
2427 }
2428 else {
2429 tcp->u_rval = metag_regs.dx[0][0];
2430 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002431#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002432 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002433 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002434 u_error = -cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002435 }
2436 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002437 tcp->u_rval = cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002438 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002439#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002440 /*
2441 * The standard tile calling convention returns the value (or negative
2442 * errno) in r0, and zero (or positive errno) in r1.
2443 * Until at least kernel 3.8, however, the r1 value is not reflected
2444 * in ptregs at this point, so we use r0 here.
2445 */
2446 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002447 tcp->u_rval = -1;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002448 u_error = -tile_regs.regs[0];
2449 } else {
2450 tcp->u_rval = tile_regs.regs[0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002451 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002452#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002453 if (check_errno && is_negated_errno(microblaze_r3)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002454 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002455 u_error = -microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002456 }
2457 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002458 tcp->u_rval = microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002459 }
Christian Svensson492f81f2013-02-14 13:26:27 +01002460#elif defined(OR1K)
2461 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2462 tcp->u_rval = -1;
2463 u_error = -or1k_regs.gpr[11];
2464 }
2465 else {
2466 tcp->u_rval = or1k_regs.gpr[11];
2467 }
Chris Zankel8f636ed2013-03-25 10:22:07 -07002468#elif defined(XTENSA)
2469 if (check_errno && is_negated_errno(xtensa_a2)) {
2470 tcp->u_rval = -1;
2471 u_error = -xtensa_a2;
2472 }
2473 else {
2474 tcp->u_rval = xtensa_a2;
2475 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002476#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002477 tcp->u_error = u_error;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002478}
2479
2480static void
2481dumpio(struct tcb *tcp)
2482{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002483 int (*func)();
2484
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002485 if (syserror(tcp))
2486 return;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +01002487 if ((unsigned long) tcp->u_arg[0] >= num_quals)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002488 return;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002489 func = tcp->s_ent->sys_func;
2490 if (func == printargs)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002491 return;
2492 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002493 if (func == sys_read ||
2494 func == sys_pread ||
2495 func == sys_recv ||
2496 func == sys_recvfrom)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002497 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002498 else if (func == sys_readv)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002499 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2500 return;
2501 }
2502 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002503 if (func == sys_write ||
2504 func == sys_pwrite ||
2505 func == sys_send ||
2506 func == sys_sendto)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002507 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002508 else if (func == sys_writev)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002509 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2510 return;
2511 }
2512}
2513
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002514static int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002515trace_syscall_exiting(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002516{
2517 int sys_res;
2518 struct timeval tv;
Denys Vlasenko1a5b5a72011-08-25 00:29:56 +02002519 int res;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002520 long u_error;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002521
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002522 /* Measure the exit time as early as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002523 if (Tflag || cflag)
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002524 gettimeofday(&tv, NULL);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002525
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00002526#if SUPPORTED_PERSONALITIES > 1
2527 update_personality(tcp, tcp->currpers);
2528#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002529 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002530 if (res == 1) {
2531 syscall_fixup_on_sysexit(tcp); /* never fails */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002532 get_error(tcp); /* never fails */
2533 if (need_fork_exec_workarounds)
2534 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002535 if (filtered(tcp) || hide_log_until_execve)
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002536 goto ret;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002537 }
2538
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002539 if (cflag) {
2540 struct timeval t = tv;
Denys Vlasenkoc95a88f2011-08-21 17:47:40 +02002541 count_syscall(tcp, &t);
Denys Vlasenko7b609d52011-06-22 14:32:43 +02002542 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002543 goto ret;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002544 }
2545 }
2546
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002547 /* If not in -ff mode, and printing_tcp != tcp,
2548 * then the log currently does not end with output
2549 * of _our syscall entry_, but with something else.
2550 * We need to say which syscall's return is this.
2551 *
2552 * Forced reprinting via TCB_REPRINT is used only by
2553 * "strace -ff -oLOG test/threaded_execve" corner case.
2554 * It's the only case when -ff mode needs reprinting.
2555 */
2556 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2557 tcp->flags &= ~TCB_REPRINT;
2558 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002559 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002560 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002561 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002562 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002563 }
2564 printing_tcp = tcp;
2565
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002566 if (res != 1) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002567 /* There was error in one of prior ptrace ops */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002568 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002569 tabto();
Denys Vlasenko000b6012012-01-28 01:25:03 +01002570 tprints("= ? <unavailable>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002571 line_ended();
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002572 tcp->flags &= ~TCB_INSYSCALL;
2573 return res;
2574 }
2575
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002576 sys_res = 0;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002577 if (tcp->qual_flg & QUAL_RAW) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002578 /* sys_res = printargs(tcp); - but it's nop on sysexit */
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002579 } else {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002580 /* FIXME: not_failing_only (IOW, option -z) is broken:
2581 * failure of syscall is known only after syscall return.
2582 * Thus we end up with something like this on, say, ENOENT:
2583 * open("doesnt_exist", O_RDONLY <unfinished ...>
2584 * {next syscall decode}
2585 * whereas the intended result is that open(...) line
2586 * is not shown at all.
2587 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002588 if (not_failing_only && tcp->u_error)
Denys Vlasenko3b738812011-08-22 02:06:35 +02002589 goto ret; /* ignore failed syscalls */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002590 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002591 }
2592
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002593 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002594 tabto();
Denys Vlasenko3b738812011-08-22 02:06:35 +02002595 u_error = tcp->u_error;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002596 if (tcp->qual_flg & QUAL_RAW) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002597 if (u_error)
2598 tprintf("= -1 (errno %ld)", u_error);
2599 else
2600 tprintf("= %#lx", tcp->u_rval);
2601 }
2602 else if (!(sys_res & RVAL_NONE) && u_error) {
2603 switch (u_error) {
Denys Vlasenkofe585652012-01-12 11:26:34 +01002604 /* Blocked signals do not interrupt any syscalls.
2605 * In this case syscalls don't return ERESTARTfoo codes.
2606 *
2607 * Deadly signals set to SIG_DFL interrupt syscalls
2608 * and kill the process regardless of which of the codes below
2609 * is returned by the interrupted syscall.
2610 * In some cases, kernel forces a kernel-generated deadly
2611 * signal to be unblocked and set to SIG_DFL (and thus cause
2612 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2613 * or SIGILL. (The alternative is to leave process spinning
2614 * forever on the faulty instruction - not useful).
2615 *
2616 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2617 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2618 * but kernel will always restart them.
2619 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002620 case ERESTARTSYS:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002621 /* Most common type of signal-interrupted syscall exit code.
2622 * The system call will be restarted with the same arguments
2623 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2624 */
2625 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002626 break;
2627 case ERESTARTNOINTR:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002628 /* Rare. For example, fork() returns this if interrupted.
2629 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2630 */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002631 tprints("= ? ERESTARTNOINTR (To be restarted)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002632 break;
2633 case ERESTARTNOHAND:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002634 /* pause(), rt_sigsuspend() etc use this code.
2635 * SA_RESTART is ignored (assumed not set):
2636 * syscall won't restart (will return EINTR instead)
Denys Vlasenko30c03232013-02-19 16:59:26 +01002637 * even after signal with SA_RESTART set. However,
2638 * after SIG_IGN or SIG_DFL signal it will restart
2639 * (thus the name "restart only if has no handler").
Denys Vlasenkofe585652012-01-12 11:26:34 +01002640 */
Denys Vlasenkoaba62922013-03-05 16:56:35 +01002641 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002642 break;
2643 case ERESTART_RESTARTBLOCK:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002644 /* Syscalls like nanosleep(), poll() which can't be
2645 * restarted with their original arguments use this
2646 * code. Kernel will execute restart_syscall() instead,
2647 * which changes arguments before restarting syscall.
2648 * SA_RESTART is ignored (assumed not set) similarly
2649 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2650 * since restart data is saved in "restart block"
2651 * in task struct, and if signal handler uses a syscall
2652 * which in turn saves another such restart block,
2653 * old data is lost and restart becomes impossible)
2654 */
2655 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002656 break;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002657 default:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002658 if (u_error < 0)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002659 tprintf("= -1 E??? (errno %ld)", u_error);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002660 else if (u_error < nerrnos)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002661 tprintf("= -1 %s (%s)", errnoent[u_error],
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002662 strerror(u_error));
2663 else
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002664 tprintf("= -1 ERRNO_%ld (%s)", u_error,
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002665 strerror(u_error));
2666 break;
2667 }
2668 if ((sys_res & RVAL_STR) && tcp->auxstr)
2669 tprintf(" (%s)", tcp->auxstr);
2670 }
2671 else {
2672 if (sys_res & RVAL_NONE)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002673 tprints("= ?");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002674 else {
2675 switch (sys_res & RVAL_MASK) {
2676 case RVAL_HEX:
2677 tprintf("= %#lx", tcp->u_rval);
2678 break;
2679 case RVAL_OCTAL:
2680 tprintf("= %#lo", tcp->u_rval);
2681 break;
2682 case RVAL_UDECIMAL:
2683 tprintf("= %lu", tcp->u_rval);
2684 break;
2685 case RVAL_DECIMAL:
2686 tprintf("= %ld", tcp->u_rval);
2687 break;
H.J. Ludd0130b2012-04-16 12:16:45 +02002688#if defined(LINUX_MIPSN32) || defined(X32)
2689 /*
2690 case RVAL_LHEX:
2691 tprintf("= %#llx", tcp->u_lrval);
2692 break;
2693 case RVAL_LOCTAL:
2694 tprintf("= %#llo", tcp->u_lrval);
2695 break;
2696 */
2697 case RVAL_LUDECIMAL:
2698 tprintf("= %llu", tcp->u_lrval);
2699 break;
2700 /*
2701 case RVAL_LDECIMAL:
2702 tprintf("= %lld", tcp->u_lrval);
2703 break;
2704 */
2705#endif
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002706 default:
2707 fprintf(stderr,
2708 "invalid rval format\n");
2709 break;
2710 }
2711 }
2712 if ((sys_res & RVAL_STR) && tcp->auxstr)
2713 tprintf(" (%s)", tcp->auxstr);
2714 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002715 if (Tflag) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002716 tv_sub(&tv, &tv, &tcp->etime);
2717 tprintf(" <%ld.%06ld>",
2718 (long) tv.tv_sec, (long) tv.tv_usec);
2719 }
Denys Vlasenko000b6012012-01-28 01:25:03 +01002720 tprints("\n");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002721 dumpio(tcp);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002722 line_ended();
2723
Denys Vlasenko3b738812011-08-22 02:06:35 +02002724 ret:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002725 tcp->flags &= ~TCB_INSYSCALL;
2726 return 0;
2727}
2728
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002729int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002730trace_syscall(struct tcb *tcp)
2731{
2732 return exiting(tcp) ?
2733 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2734}