blob: 42aad40add514acb8d0f63e8cb5b21873562f77c [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00005 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Linux for s390 port by D.J. Barrow
8 * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00009 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000032 */
33
34#include "defs.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000035#include <sys/user.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000036#include <sys/param.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000037
Wichert Akkerman15dea971999-10-06 13:06:34 +000038#ifdef HAVE_SYS_REG_H
Denys Vlasenko523635f2012-02-25 02:44:25 +010039# include <sys/reg.h>
40# ifndef PTRACE_PEEKUSR
41# define PTRACE_PEEKUSR PTRACE_PEEKUSER
42# endif
Wichert Akkermanfaf72222000-02-19 23:59:03 +000043#elif defined(HAVE_LINUX_PTRACE_H)
Denys Vlasenko523635f2012-02-25 02:44:25 +010044# undef PTRACE_SYSCALL
Roland McGrathce9f0742004-03-01 21:29:22 +000045# ifdef HAVE_STRUCT_IA64_FPREG
46# define ia64_fpreg XXX_ia64_fpreg
47# endif
48# ifdef HAVE_STRUCT_PT_ALL_USER_REGS
49# define pt_all_user_regs XXX_pt_all_user_regs
50# endif
Denys Vlasenko523635f2012-02-25 02:44:25 +010051# include <linux/ptrace.h>
Roland McGrathce9f0742004-03-01 21:29:22 +000052# undef ia64_fpreg
53# undef pt_all_user_regs
Wichert Akkerman15dea971999-10-06 13:06:34 +000054#endif
55
Denys Vlasenko84703742012-02-25 02:38:52 +010056#if defined(SPARC64)
Roland McGrath6d1a65c2004-07-12 07:44:08 +000057# undef PTRACE_GETREGS
58# define PTRACE_GETREGS PTRACE_GETREGS64
59# undef PTRACE_SETREGS
60# define PTRACE_SETREGS PTRACE_SETREGS64
Denys Vlasenko84703742012-02-25 02:38:52 +010061#endif
Roland McGrath6d1a65c2004-07-12 07:44:08 +000062
Denys Vlasenko84703742012-02-25 02:38:52 +010063#if defined(IA64)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000064# include <asm/ptrace_offsets.h>
65# include <asm/rse.h>
66#endif
67
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010068#if defined(X86_64) || defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010069# include <sys/uio.h>
70# include <elf.h>
71#endif
72
Steve McIntyred8d3bd32012-10-24 17:58:16 +010073#if defined(AARCH64)
74# include <asm/ptrace.h>
75# include <sys/uio.h>
76# include <elf.h>
77#endif
78
Christian Svensson492f81f2013-02-14 13:26:27 +010079#if defined(OR1K)
80# include <sys/uio.h>
81# include <elf.h>
82#endif
83
James Hogan5f999a82013-02-22 14:44:10 +000084#if defined(METAG)
85# include <sys/uio.h>
86# include <elf.h>
87#endif
88
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000089#ifndef ERESTARTSYS
Denys Vlasenko523635f2012-02-25 02:44:25 +010090# define ERESTARTSYS 512
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000091#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010092#ifndef ERESTARTNOINTR
Denys Vlasenko523635f2012-02-25 02:44:25 +010093# define ERESTARTNOINTR 513
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000094#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010095#ifndef ERESTARTNOHAND
96# define ERESTARTNOHAND 514 /* restart if no handler */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000097#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010098#ifndef ERESTART_RESTARTBLOCK
Denys Vlasenko523635f2012-02-25 02:44:25 +010099# define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
Roland McGrath9c555e72003-07-09 09:47:59 +0000100#endif
Denys Vlasenko523635f2012-02-25 02:44:25 +0100101
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000102#ifndef NSIG
Denys Vlasenko523635f2012-02-25 02:44:25 +0100103# warning: NSIG is not defined, using 32
104# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000105#endif
106#ifdef ARM
Denys Vlasenko041b3ee2011-08-18 12:48:56 +0200107/* Ugh. Is this really correct? ARM has no RT signals?! */
Denys Vlasenko523635f2012-02-25 02:44:25 +0100108# undef NSIG
109# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000110#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000111
112#include "syscall.h"
113
114/* Define these shorthand notations to simplify the syscallent files. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000115#define TD TRACE_DESC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000116#define TF TRACE_FILE
117#define TI TRACE_IPC
118#define TN TRACE_NETWORK
119#define TP TRACE_PROCESS
120#define TS TRACE_SIGNAL
Namhyung Kim96792962012-10-24 11:41:57 +0900121#define TM TRACE_MEMORY
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000122#define NF SYSCALL_NEVER_FAILS
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200123#define MA MAX_ARGS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000124
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100125const struct_sysent sysent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000126#include "syscallent.h"
127};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000128
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100129#if SUPPORTED_PERSONALITIES > 1
130static const struct_sysent sysent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100131# include "syscallent1.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
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100135#if SUPPORTED_PERSONALITIES > 2
136static const struct_sysent sysent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100137# include "syscallent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000138};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200139#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000140
141/* Now undef them since short defines cause wicked namespace pollution. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000142#undef TD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000143#undef TF
144#undef TI
145#undef TN
146#undef TP
147#undef TS
Namhyung Kim96792962012-10-24 11:41:57 +0900148#undef TM
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000149#undef NF
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200150#undef MA
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000151
Denys Vlasenko39fca622011-08-20 02:12:33 +0200152/*
153 * `ioctlent.h' may be generated from `ioctlent.raw' by the auxiliary
154 * program `ioctlsort', such that the list is sorted by the `code' field.
155 * This has the side-effect of resolving the _IO.. macros into
156 * plain integers, eliminating the need to include here everything
157 * in "/usr/include".
158 */
159
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100160const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000161#include "errnoent.h"
162};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100163const char *const signalent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200164#include "signalent.h"
165};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100166const struct_ioctlent ioctlent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200167#include "ioctlent.h"
168};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000169
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100170#if SUPPORTED_PERSONALITIES > 1
Roland McGrathee36ce12004-09-04 03:53:10 +0000171static const char *const errnoent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100172# include "errnoent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000173};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200174static const char *const signalent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100175# include "signalent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200176};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100177static const struct_ioctlent ioctlent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100178# include "ioctlent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200179};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200180#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000181
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100182#if SUPPORTED_PERSONALITIES > 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000183static const char *const errnoent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100184# include "errnoent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000185};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200186static const char *const signalent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100187# include "signalent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200188};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100189static const struct_ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100190# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200191};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200192#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000193
Dmitry V. Levine6f55242013-02-26 22:02:30 +0000194enum {
195 nsyscalls0 = ARRAY_SIZE(sysent0)
196#if SUPPORTED_PERSONALITIES > 1
197 , nsyscalls1 = ARRAY_SIZE(sysent1)
198# if SUPPORTED_PERSONALITIES > 2
199 , nsyscalls2 = ARRAY_SIZE(sysent2)
200# endif
201#endif
202};
203
204enum {
205 nerrnos0 = ARRAY_SIZE(errnoent0)
206#if SUPPORTED_PERSONALITIES > 1
207 , nerrnos1 = ARRAY_SIZE(errnoent1)
208# if SUPPORTED_PERSONALITIES > 2
209 , nerrnos2 = ARRAY_SIZE(errnoent2)
210# endif
211#endif
212};
213
214enum {
215 nsignals0 = ARRAY_SIZE(signalent0)
216#if SUPPORTED_PERSONALITIES > 1
217 , nsignals1 = ARRAY_SIZE(signalent1)
218# if SUPPORTED_PERSONALITIES > 2
219 , nsignals2 = ARRAY_SIZE(signalent2)
220# endif
221#endif
222};
223
224enum {
225 nioctlents0 = ARRAY_SIZE(ioctlent0)
226#if SUPPORTED_PERSONALITIES > 1
227 , nioctlents1 = ARRAY_SIZE(ioctlent1)
228# if SUPPORTED_PERSONALITIES > 2
229 , nioctlents2 = ARRAY_SIZE(ioctlent2)
230# endif
231#endif
232};
233
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100234#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100235const struct_sysent *sysent = sysent0;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100236const char *const *errnoent = errnoent0;
237const char *const *signalent = signalent0;
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100238const struct_ioctlent *ioctlent = ioctlent0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100239#endif
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100240unsigned nsyscalls = nsyscalls0;
241unsigned nerrnos = nerrnos0;
242unsigned nsignals = nsignals0;
243unsigned nioctlents = nioctlents0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100244
245unsigned num_quals;
246qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
247
248static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
249 nsyscalls0,
250#if SUPPORTED_PERSONALITIES > 1
251 nsyscalls1,
252#endif
253#if SUPPORTED_PERSONALITIES > 2
254 nsyscalls2,
255#endif
256};
257static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
258 sysent0,
259#if SUPPORTED_PERSONALITIES > 1
260 sysent1,
261#endif
262#if SUPPORTED_PERSONALITIES > 2
263 sysent2,
264#endif
265};
266
267enum {
268 MAX_NSYSCALLS1 = (nsyscalls0
269#if SUPPORTED_PERSONALITIES > 1
270 > nsyscalls1 ? nsyscalls0 : nsyscalls1
271#endif
272 ),
273 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
274#if SUPPORTED_PERSONALITIES > 2
275 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
276#endif
277 ),
278 MAX_NSYSCALLS = MAX_NSYSCALLS2,
279 /* We are ready for arches with up to 255 signals,
280 * even though the largest known signo is on MIPS and it is 128.
281 * The number of existing syscalls on all arches is
282 * larger that 255 anyway, so it is just a pedantic matter.
283 */
284 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
285};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000286
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100287#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100288unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000289
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100290# ifndef current_wordsize
291unsigned current_wordsize;
292static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000293 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000294 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100295# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000296 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100297# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200298};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100299# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000300
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200301void
Dmitry V. Levin3abe8b22006-12-20 22:37:21 +0000302set_personality(int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000303{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100304 nsyscalls = nsyscall_vec[personality];
305 sysent = sysent_vec[personality];
306
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000307 switch (personality) {
308 case 0:
309 errnoent = errnoent0;
310 nerrnos = nerrnos0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000311 ioctlent = ioctlent0;
312 nioctlents = nioctlents0;
313 signalent = signalent0;
314 nsignals = nsignals0;
315 break;
316
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000317 case 1:
318 errnoent = errnoent1;
319 nerrnos = nerrnos1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000320 ioctlent = ioctlent1;
321 nioctlents = nioctlents1;
322 signalent = signalent1;
323 nsignals = nsignals1;
324 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000325
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100326# if SUPPORTED_PERSONALITIES > 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000327 case 2:
328 errnoent = errnoent2;
329 nerrnos = nerrnos2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000330 ioctlent = ioctlent2;
331 nioctlents = nioctlents2;
332 signalent = signalent2;
333 nsignals = nsignals2;
334 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100335# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000336 }
337
338 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100339# ifndef current_wordsize
340 current_wordsize = personality_wordsize[personality];
341# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000342}
343
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000344static void
345update_personality(struct tcb *tcp, int personality)
346{
347 if (personality == current_personality)
348 return;
349 set_personality(personality);
350
351 if (personality == tcp->currpers)
352 return;
353 tcp->currpers = personality;
354
H.J. Lu35be5812012-04-16 13:00:01 +0200355# if defined(POWERPC64)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000356 if (!qflag) {
357 static const char *const names[] = {"64 bit", "32 bit"};
358 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
359 tcp->pid, names[personality]);
360 }
H.J. Lu35be5812012-04-16 13:00:01 +0200361# elif defined(X86_64)
362 if (!qflag) {
363 static const char *const names[] = {"64 bit", "32 bit", "x32"};
364 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
365 tcp->pid, names[personality]);
366 }
H.J. Lu085e4282012-04-17 11:05:04 -0700367# elif defined(X32)
368 if (!qflag) {
369 static const char *const names[] = {"x32", "32 bit"};
370 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
371 tcp->pid, names[personality]);
372 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000373# elif defined(AARCH64)
374 if (!qflag) {
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100375 static const char *const names[] = {"32-bit", "AArch64"};
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000376 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
377 tcp->pid, names[personality]);
378 }
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100379# elif defined(TILE)
380 if (!qflag) {
381 static const char *const names[] = {"64-bit", "32-bit"};
382 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
383 tcp->pid, names[personality]);
384 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100385# endif
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000386}
387#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000388
Denys Vlasenkoc1540fe2013-02-21 16:17:08 +0100389static int qual_syscall(), qual_signal(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000390
Roland McGrathe10e62a2004-09-04 04:20:43 +0000391static const struct qual_options {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000392 int bitflag;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000393 const char *option_name;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000394 int (*qualify)(const char *, int, int);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000395 const char *argument_name;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000396} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000397 { QUAL_TRACE, "trace", qual_syscall, "system call" },
398 { QUAL_TRACE, "t", qual_syscall, "system call" },
399 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
400 { QUAL_ABBREV, "a", qual_syscall, "system call" },
401 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
402 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
403 { QUAL_RAW, "raw", qual_syscall, "system call" },
404 { QUAL_RAW, "x", qual_syscall, "system call" },
405 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
406 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
407 { QUAL_SIGNAL, "s", qual_signal, "signal" },
Roland McGrath9797ceb2002-12-30 10:23:00 +0000408 { QUAL_READ, "read", qual_desc, "descriptor" },
409 { QUAL_READ, "reads", qual_desc, "descriptor" },
410 { QUAL_READ, "r", qual_desc, "descriptor" },
411 { QUAL_WRITE, "write", qual_desc, "descriptor" },
412 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
413 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000414 { 0, NULL, NULL, NULL },
415};
416
Roland McGrath9797ceb2002-12-30 10:23:00 +0000417static void
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100418reallocate_qual(int n)
419{
420 unsigned p;
421 qualbits_t *qp;
422 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
423 qp = qual_vec[p] = realloc(qual_vec[p], n * sizeof(qualbits_t));
424 if (!qp)
425 die_out_of_memory();
426 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
427 }
428 num_quals = n;
429}
430
431static void
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000432qualify_one(int n, int bitflag, int not, int pers)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000433{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100434 unsigned p;
Roland McGrath138c6a32006-01-12 09:50:49 +0000435
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100436 if (num_quals <= n)
437 reallocate_qual(n + 1);
Roland McGrath138c6a32006-01-12 09:50:49 +0000438
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100439 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
440 if (pers == p || pers < 0) {
441 if (not)
442 qual_vec[p][n] &= ~bitflag;
443 else
444 qual_vec[p][n] |= bitflag;
445 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000446 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000447}
448
449static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000450qual_syscall(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000451{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100452 unsigned p;
453 unsigned i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000454 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000455
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100456 if (*s >= '0' && *s <= '9') {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100457 i = string_to_uint(s);
Denys Vlasenkob43dacd2013-02-23 18:19:28 +0100458 if (i >= MAX_NSYSCALLS)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000459 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000460 qualify_one(i, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000461 return 0;
Roland McGrath48a035f2006-01-12 09:45:56 +0000462 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000463
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100464 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
465 for (i = 0; i < nsyscall_vec[p]; i++) {
466 if (sysent_vec[p][i].sys_name
467 && strcmp(s, sysent_vec[p][i].sys_name) == 0
468 ) {
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000469 qualify_one(i, bitflag, not, p);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100470 rc = 0;
471 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000472 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100473 }
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000474
Roland McGrathfe6b3522005-02-02 04:40:11 +0000475 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000476}
477
478static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000479qual_signal(const char *s, int bitflag, int not)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000480{
481 int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000482
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100483 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000484 int signo = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100485 if (signo < 0 || signo > 255)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000486 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000487 qualify_one(signo, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000488 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000489 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000490 if (strncasecmp(s, "SIG", 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000491 s += 3;
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100492 for (i = 0; i <= NSIG; i++) {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000493 if (strcasecmp(s, signame(i) + 3) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000494 qualify_one(i, bitflag, not, -1);
Roland McGrath76421df2005-02-02 03:51:18 +0000495 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000496 }
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100497 }
Roland McGrath76421df2005-02-02 03:51:18 +0000498 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000499}
500
501static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000502qual_desc(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000503{
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100504 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000505 int desc = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100506 if (desc < 0 || desc > 0x7fff) /* paranoia */
Roland McGrathfe6b3522005-02-02 04:40:11 +0000507 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000508 qualify_one(desc, bitflag, not, -1);
Roland McGrath2b619022003-04-10 18:58:20 +0000509 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000510 }
511 return -1;
512}
513
514static int
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000515lookup_class(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000516{
517 if (strcmp(s, "file") == 0)
518 return TRACE_FILE;
519 if (strcmp(s, "ipc") == 0)
520 return TRACE_IPC;
521 if (strcmp(s, "network") == 0)
522 return TRACE_NETWORK;
523 if (strcmp(s, "process") == 0)
524 return TRACE_PROCESS;
525 if (strcmp(s, "signal") == 0)
526 return TRACE_SIGNAL;
Roland McGrath2fe7b132005-07-05 03:25:35 +0000527 if (strcmp(s, "desc") == 0)
528 return TRACE_DESC;
Namhyung Kim96792962012-10-24 11:41:57 +0900529 if (strcmp(s, "memory") == 0)
530 return TRACE_MEMORY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000531 return -1;
532}
533
534void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000535qualify(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000536{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000537 const struct qual_options *opt;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000538 int not;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000539 char *copy;
540 const char *p;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000541 int i, n;
542
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100543 if (num_quals == 0)
544 reallocate_qual(MIN_QUALS);
545
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000546 opt = &qual_options[0];
547 for (i = 0; (p = qual_options[i].option_name); i++) {
548 n = strlen(p);
549 if (strncmp(s, p, n) == 0 && s[n] == '=') {
550 opt = &qual_options[i];
551 s += n + 1;
552 break;
553 }
554 }
555 not = 0;
556 if (*s == '!') {
557 not = 1;
558 s++;
559 }
560 if (strcmp(s, "none") == 0) {
561 not = 1 - not;
562 s = "all";
563 }
564 if (strcmp(s, "all") == 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100565 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000566 qualify_one(i, opt->bitflag, not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000567 }
568 return;
569 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100570 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000571 qualify_one(i, opt->bitflag, !not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000572 }
Denys Vlasenko5d645812011-08-20 12:48:18 +0200573 copy = strdup(s);
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200574 if (!copy)
575 die_out_of_memory();
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000576 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000577 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100578 unsigned pers;
579 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
580 for (i = 0; i < nsyscall_vec[pers]; i++)
581 if (sysent_vec[pers][i].sys_flags & n)
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000582 qualify_one(i, opt->bitflag, not, pers);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100583 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000584 continue;
585 }
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000586 if (opt->qualify(p, opt->bitflag, not)) {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100587 error_msg_and_die("invalid %s '%s'",
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000588 opt->argument_name, p);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000589 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000590 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000591 free(copy);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000592 return;
593}
594
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000595#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000596static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000597decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000598{
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000599 unsigned long addr;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100600 unsigned int i, n, size;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000601
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000602 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
603 return;
604
605 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100606 tcp->qual_flg = qual_flags[tcp->scno];
607 tcp->s_ent = &sysent[tcp->scno];
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000608 addr = tcp->u_arg[1];
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100609 size = current_wordsize;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100610 n = tcp->s_ent->nargs;
611 for (i = 0; i < n; ++i) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000612 if (size == sizeof(int)) {
613 unsigned int arg;
614 if (umove(tcp, addr, &arg) < 0)
615 arg = 0;
616 tcp->u_arg[i] = arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000617 }
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000618 else {
619 unsigned long arg;
620 if (umove(tcp, addr, &arg) < 0)
621 arg = 0;
622 tcp->u_arg[i] = arg;
623 }
624 addr += size;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000625 }
626}
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000627#endif
Mike Frysinger3362e892012-03-15 01:09:19 -0400628
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000629#ifdef SYS_ipc_subcall
630static void
631decode_ipc_subcall(struct tcb *tcp)
632{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100633 unsigned int i, n;
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000634
635 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
636 return;
637
638 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100639 tcp->qual_flg = qual_flags[tcp->scno];
640 tcp->s_ent = &sysent[tcp->scno];
641 n = tcp->s_ent->nargs;
642 for (i = 0; i < n; i++)
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000643 tcp->u_arg[i] = tcp->u_arg[i + 1];
644}
645#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000646
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200647int
648printargs(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000649{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200650 if (entering(tcp)) {
651 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100652 int n = tcp->s_ent->nargs;
653 for (i = 0; i < n; i++)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200654 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
655 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000656 return 0;
657}
658
Denys Vlasenko72879c62012-02-27 14:18:02 +0100659int
660printargs_lu(struct tcb *tcp)
661{
662 if (entering(tcp)) {
663 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100664 int n = tcp->s_ent->nargs;
665 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100666 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
667 }
668 return 0;
669}
670
671int
672printargs_ld(struct tcb *tcp)
673{
674 if (entering(tcp)) {
675 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100676 int n = tcp->s_ent->nargs;
677 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100678 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
679 }
680 return 0;
681}
682
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100683#if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200684long
685getrval2(struct tcb *tcp)
686{
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100687 long val;
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200688
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100689# if defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100690 val = sparc_regs.u_regs[U_REG_O1];
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100691# elif defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200692 if (upeek(tcp, 4*(REG_REG0+1), &val) < 0)
693 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100694# elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200695 if (upeek(tcp, PT_R9, &val) < 0)
696 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100697# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200698
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200699 return val;
700}
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100701#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200702
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200703int
704is_restart_error(struct tcb *tcp)
705{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200706 switch (tcp->u_error) {
707 case ERESTARTSYS:
708 case ERESTARTNOINTR:
709 case ERESTARTNOHAND:
710 case ERESTART_RESTARTBLOCK:
711 return 1;
712 default:
713 break;
714 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200715 return 0;
716}
717
Denys Vlasenko523635f2012-02-25 02:44:25 +0100718#if defined(I386)
Denys Vlasenko2550d482013-02-15 21:04:28 +0100719struct user_regs_struct i386_regs;
H.J. Lu35be5812012-04-16 13:00:01 +0200720#elif defined(X86_64) || defined(X32)
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100721/*
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100722 * On i386, pt_regs and user_regs_struct are the same,
723 * but on 64 bit x86, user_regs_struct has six more fields:
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100724 * fs_base, gs_base, ds, es, fs, gs.
725 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
726 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100727struct i386_user_regs_struct {
728 uint32_t ebx;
729 uint32_t ecx;
730 uint32_t edx;
731 uint32_t esi;
732 uint32_t edi;
733 uint32_t ebp;
734 uint32_t eax;
735 uint32_t xds;
736 uint32_t xes;
737 uint32_t xfs;
738 uint32_t xgs;
739 uint32_t orig_eax;
740 uint32_t eip;
741 uint32_t xcs;
742 uint32_t eflags;
743 uint32_t esp;
744 uint32_t xss;
745};
746static union {
747 struct user_regs_struct x86_64_r;
748 struct i386_user_regs_struct i386_r;
749} x86_regs_union;
750# define x86_64_regs x86_regs_union.x86_64_r
751# define i386_regs x86_regs_union.i386_r
752static struct iovec x86_io = {
753 .iov_base = &x86_regs_union
754};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100755#elif defined(IA64)
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200756long ia32 = 0; /* not static */
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100757static long ia64_r8, ia64_r10;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100758#elif defined(POWERPC)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +0100759static long ppc_result;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100760#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100761static long m68k_d0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100762#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100763static long bfin_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100764#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100765struct pt_regs arm_regs; /* not static */
Steve McIntyred8d3bd32012-10-24 17:58:16 +0100766#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100767static union {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100768 struct user_pt_regs aarch64_r;
769 struct arm_pt_regs arm_r;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100770} arm_regs_union;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100771# define aarch64_regs arm_regs_union.aarch64_r
772# define arm_regs arm_regs_union.arm_r
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100773static struct iovec aarch64_io = {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100774 .iov_base = &arm_regs_union
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100775};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100776#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100777static long alpha_r0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100778static long alpha_a3;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100779#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100780static struct pt_regs avr32_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100781#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100782struct pt_regs sparc_regs; /* not static */
Denys Vlasenko523635f2012-02-25 02:44:25 +0100783#elif defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100784static long long mips_a3;
785static long long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100786#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100787static long mips_a3;
788static long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100789#elif defined(S390) || defined(S390X)
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200790static long gpr2;
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200791static long syscall_mode;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100792#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100793static long hppa_r28;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100794#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100795static long sh_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100796#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100797static long sh64_r9;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100798#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100799static long cris_r10;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100800#elif defined(TILE)
801struct pt_regs tile_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100802#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100803static long microblaze_r3;
Christian Svensson492f81f2013-02-14 13:26:27 +0100804#elif defined(OR1K)
805static struct user_regs_struct or1k_regs;
806static struct iovec or1k_io = {
807 .iov_base = &or1k_regs
808};
James Hogan5f999a82013-02-22 14:44:10 +0000809#elif defined(METAG)
810static struct user_gp_regs metag_regs;
811static struct iovec metag_io = {
812 .iov_base = &metag_regs
813};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100814#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000815
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100816void
817printcall(struct tcb *tcp)
818{
819#define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
820 sizeof(long) == 8 ? "[????????????????] " : \
821 NULL /* crash */)
822 if (get_regs_error) {
823 PRINTBADPC;
824 return;
825 }
826#if defined(I386)
827 tprintf("[%08lx] ", i386_regs.eip);
828#elif defined(S390) || defined(S390X)
829 long psw;
830 if (upeek(tcp, PT_PSWADDR, &psw) < 0) {
831 PRINTBADPC;
832 return;
833 }
834# ifdef S390
835 tprintf("[%08lx] ", psw);
836# elif S390X
Dmitry V. Levinddba73e2013-02-05 19:01:58 +0000837 tprintf("[%016lx] ", psw);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100838# endif
839#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100840 if (x86_io.iov_len == sizeof(i386_regs)) {
841 tprintf("[%08x] ", (unsigned) i386_regs.eip);
842 } else {
843# if defined(X86_64)
844 tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
845# elif defined(X32)
846 /* Note: this truncates 64-bit rip to 32 bits */
847 tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip);
848# endif
849 }
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100850#elif defined(IA64)
851 long ip;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100852 if (upeek(tcp, PT_B0, &ip) < 0) {
853 PRINTBADPC;
854 return;
855 }
856 tprintf("[%08lx] ", ip);
857#elif defined(POWERPC)
858 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100859 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) {
860 PRINTBADPC;
861 return;
862 }
863# ifdef POWERPC64
864 tprintf("[%016lx] ", pc);
865# else
866 tprintf("[%08lx] ", pc);
867# endif
868#elif defined(M68K)
869 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100870 if (upeek(tcp, 4*PT_PC, &pc) < 0) {
871 tprints("[????????] ");
872 return;
873 }
874 tprintf("[%08lx] ", pc);
875#elif defined(ALPHA)
876 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100877 if (upeek(tcp, REG_PC, &pc) < 0) {
878 tprints("[????????????????] ");
879 return;
880 }
881 tprintf("[%08lx] ", pc);
882#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100883 tprintf("[%08lx] ", sparc_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100884#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100885 tprintf("[%08lx] ", sparc_regs.tpc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100886#elif defined(HPPA)
887 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100888 if (upeek(tcp, PT_IAOQ0, &pc) < 0) {
889 tprints("[????????] ");
890 return;
891 }
892 tprintf("[%08lx] ", pc);
893#elif defined(MIPS)
894 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100895 if (upeek(tcp, REG_EPC, &pc) < 0) {
896 tprints("[????????] ");
897 return;
898 }
899 tprintf("[%08lx] ", pc);
900#elif defined(SH)
901 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100902 if (upeek(tcp, 4*REG_PC, &pc) < 0) {
903 tprints("[????????] ");
904 return;
905 }
906 tprintf("[%08lx] ", pc);
907#elif defined(SH64)
908 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100909 if (upeek(tcp, REG_PC, &pc) < 0) {
910 tprints("[????????????????] ");
911 return;
912 }
913 tprintf("[%08lx] ", pc);
914#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100915 tprintf("[%08lx] ", arm_regs.ARM_pc);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100916#elif defined(AARCH64)
917 /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100918#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100919 tprintf("[%08lx] ", avr32_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100920#elif defined(BFIN)
921 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100922 if (upeek(tcp, PT_PC, &pc) < 0) {
923 PRINTBADPC;
924 return;
925 }
926 tprintf("[%08lx] ", pc);
927#elif defined(CRISV10)
928 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100929 if (upeek(tcp, 4*PT_IRP, &pc) < 0) {
930 PRINTBADPC;
931 return;
932 }
933 tprintf("[%08lx] ", pc);
934#elif defined(CRISV32)
935 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100936 if (upeek(tcp, 4*PT_ERP, &pc) < 0) {
937 PRINTBADPC;
938 return;
939 }
940 tprintf("[%08lx] ", pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100941#elif defined(TILE)
942# ifdef _LP64
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500943 tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100944# else
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500945 tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100946# endif
Christian Svensson492f81f2013-02-14 13:26:27 +0100947#elif defined(OR1K)
948 tprintf("[%08lx] ", or1k_regs.pc);
James Hogan5f999a82013-02-22 14:44:10 +0000949#elif defined(METAG)
950 tprintf("[%08lx] ", metag_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100951#endif /* architecture */
952}
953
Denys Vlasenko7270de52013-02-21 15:46:34 +0100954/* Shuffle syscall numbers so that we don't have huge gaps in syscall table.
955 * The shuffling should be reversible: shuffle_scno(shuffle_scno(n)) == n.
956 */
957#if defined(ARM) /* So far only ARM needs this */
958static long
959shuffle_scno(unsigned long scno)
960{
961 if (scno <= ARM_LAST_ORDINARY_SYSCALL)
962 return scno;
963
964 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
965 if (scno == 0x000ffff0)
966 return ARM_LAST_ORDINARY_SYSCALL+1;
967 if (scno == ARM_LAST_ORDINARY_SYSCALL+1)
968 return 0x000ffff0;
969
970 /* Is it ARM specific syscall?
971 * Swap with [LAST_ORDINARY+2, LAST_ORDINARY+2 + LAST_SPECIAL] range.
972 */
973 if (scno >= 0x000f0000
974 && scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL
975 ) {
976 return scno - 0x000f0000 + (ARM_LAST_ORDINARY_SYSCALL+2);
977 }
978 if (/* scno >= ARM_LAST_ORDINARY_SYSCALL+2 - always true */ 1
979 && scno <= (ARM_LAST_ORDINARY_SYSCALL+2) + ARM_LAST_SPECIAL_SYSCALL
980 ) {
981 return scno + 0x000f0000 - (ARM_LAST_ORDINARY_SYSCALL+2);
982 }
983
984 return scno;
985}
986#else
987# define shuffle_scno(scno) ((long)(scno))
988#endif
989
990static char*
991undefined_scno_name(struct tcb *tcp)
992{
993 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
994
995 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
996 return buf;
997}
998
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100999#ifndef get_regs
1000long get_regs_error;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001001void
1002get_regs(pid_t pid)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001003{
1004# if defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001005 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001006# elif defined(I386)
1007 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &i386_regs);
Dmitry V. Levinee348c62013-03-17 23:15:18 +00001008# elif defined(X32)
1009 /* x86_io.iov_base = &x86_regs_union; - already is */
1010 x86_io.iov_len = sizeof(x86_regs_union);
1011 get_regs_error = ptrace(PTRACE_GETREGSET, pid,
1012 NT_PRSTATUS, (long) &x86_io);
1013# elif defined(X86_64)
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001014# if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
1015 static int getregset_support;
1016
1017 if (getregset_support >= 0) {
1018 /* x86_io.iov_base = &x86_regs_union; - already is */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001019 x86_io.iov_len = sizeof(x86_regs_union);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001020 get_regs_error = ptrace(PTRACE_GETREGSET, pid,
1021 NT_PRSTATUS, (long) &x86_io);
1022 if (getregset_support > 0)
1023 return;
1024 if (get_regs_error >= 0) {
1025 getregset_support = 1;
1026 return;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001027 }
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001028 if (errno == EPERM || errno == ESRCH)
1029 return;
1030 getregset_support = -1;
1031 }
1032# endif /* PTRACE_GETREGSET && NT_PRSTATUS */
1033 /* Use old method, with unreliable heuristical detection of 32-bitness. */
1034 x86_io.iov_len = sizeof(x86_64_regs);
1035 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &x86_64_regs);
1036 if (!get_regs_error && x86_64_regs.cs == 0x23) {
1037 x86_io.iov_len = sizeof(i386_regs);
1038 /*
1039 * The order is important: i386_regs and x86_64_regs
1040 * are overlaid in memory!
1041 */
1042 i386_regs.ebx = x86_64_regs.rbx;
1043 i386_regs.ecx = x86_64_regs.rcx;
1044 i386_regs.edx = x86_64_regs.rdx;
1045 i386_regs.esi = x86_64_regs.rsi;
1046 i386_regs.edi = x86_64_regs.rdi;
1047 i386_regs.ebp = x86_64_regs.rbp;
1048 i386_regs.eax = x86_64_regs.rax;
1049 /* i386_regs.xds = x86_64_regs.ds; unused by strace */
1050 /* i386_regs.xes = x86_64_regs.es; ditto... */
1051 /* i386_regs.xfs = x86_64_regs.fs; */
1052 /* i386_regs.xgs = x86_64_regs.gs; */
1053 i386_regs.orig_eax = x86_64_regs.orig_rax;
1054 i386_regs.eip = x86_64_regs.rip;
1055 /* i386_regs.xcs = x86_64_regs.cs; */
1056 /* i386_regs.eflags = x86_64_regs.eflags; */
1057 i386_regs.esp = x86_64_regs.rsp;
1058 /* i386_regs.xss = x86_64_regs.ss; */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001059 }
Denys Vlasenko401374e2013-02-06 18:24:39 +01001060# elif defined(ARM)
1061 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (void *)&arm_regs);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001062# elif defined(AARCH64)
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001063 /*aarch64_io.iov_base = &arm_regs_union; - already is */
1064 aarch64_io.iov_len = sizeof(arm_regs_union);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001065 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (void *)&aarch64_io);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001066# if 0
1067 /* Paranoia checks */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001068 if (get_regs_error)
1069 return;
1070 switch (aarch64_io.iov_len) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001071 case sizeof(aarch64_regs):
1072 /* We are in 64-bit mode */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001073 break;
1074 case sizeof(arm_regs):
1075 /* We are in 32-bit mode */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001076 break;
1077 default:
1078 get_regs_error = -1;
1079 break;
1080 }
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001081# endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001082# elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001083 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001084# elif defined(TILE)
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -05001085 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &tile_regs);
Christian Svensson492f81f2013-02-14 13:26:27 +01001086# elif defined(OR1K)
1087 or1k_io.iov_len = sizeof(or1k_regs);
1088 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &or1k_io);
James Hogan5f999a82013-02-22 14:44:10 +00001089# elif defined(METAG)
1090 metag_io.iov_len = sizeof(metag_regs);
1091 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &metag_io);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001092# endif
1093}
1094#endif
1095
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001096/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001097 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1098 * 1: ok, continue in trace_syscall_entering().
1099 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001100 * ("????" etc) and bail out.
1101 */
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01001102static int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001103get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001104{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001105 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001106
Denys Vlasenko523635f2012-02-25 02:44:25 +01001107#if defined(S390) || defined(S390X)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001108 if (upeek(tcp, PT_GPR2, &syscall_mode) < 0)
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001109 return -1;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001110
1111 if (syscall_mode != -ENOSYS) {
1112 /*
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001113 * Since kernel version 2.5.44 the scno gets passed in gpr2.
Roland McGrath2f924ca2003-06-26 22:23:28 +00001114 */
1115 scno = syscall_mode;
1116 } else {
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001117 /*
Michal Ludvig882eda82002-11-11 12:50:47 +00001118 * Old style of "passing" the scno via the SVC instruction.
1119 */
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001120 long psw;
Michal Ludvig882eda82002-11-11 12:50:47 +00001121 long opcode, offset_reg, tmp;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001122 void *svc_addr;
Denys Vlasenko7c9ba8b2011-08-19 19:46:32 +02001123 static const int gpr_offset[16] = {
1124 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
1125 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
1126 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
1127 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
1128 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001129
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001130 if (upeek(tcp, PT_PSWADDR, &psw) < 0)
Michal Ludvig882eda82002-11-11 12:50:47 +00001131 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001132 errno = 0;
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001133 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +00001134 if (errno) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001135 perror_msg("peektext(psw-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +00001136 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001137 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001138
1139 /*
1140 * We have to check if the SVC got executed directly or via an
1141 * EXECUTE instruction. In case of EXECUTE it is necessary to do
1142 * instruction decoding to derive the system call number.
1143 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1144 * so that this doesn't work if a SVC opcode is part of an EXECUTE
1145 * opcode. Since there is no way to find out the opcode size this
1146 * is the best we can do...
1147 */
Michal Ludvig882eda82002-11-11 12:50:47 +00001148 if ((opcode & 0xff00) == 0x0a00) {
1149 /* SVC opcode */
1150 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +00001151 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001152 else {
1153 /* SVC got executed by EXECUTE instruction */
1154
1155 /*
1156 * Do instruction decoding of EXECUTE. If you really want to
1157 * understand this, read the Principles of Operations.
1158 */
1159 svc_addr = (void *) (opcode & 0xfff);
1160
1161 tmp = 0;
1162 offset_reg = (opcode & 0x000f0000) >> 16;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001163 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001164 return -1;
1165 svc_addr += tmp;
1166
1167 tmp = 0;
1168 offset_reg = (opcode & 0x0000f000) >> 12;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001169 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001170 return -1;
1171 svc_addr += tmp;
1172
Denys Vlasenkofb036672009-01-23 16:30:26 +00001173 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
Michal Ludvig882eda82002-11-11 12:50:47 +00001174 if (errno)
1175 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001176# if defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +00001177 scno >>= 48;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001178# else
Michal Ludvig882eda82002-11-11 12:50:47 +00001179 scno >>= 16;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001180# endif
Michal Ludvig882eda82002-11-11 12:50:47 +00001181 tmp = 0;
1182 offset_reg = (opcode & 0x00f00000) >> 20;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001183 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001184 return -1;
1185
1186 scno = (scno | tmp) & 0xff;
1187 }
1188 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001189#elif defined(POWERPC)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001190 if (upeek(tcp, sizeof(unsigned long)*PT_R0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001191 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001192# ifdef POWERPC64
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001193 /* TODO: speed up strace by not doing this at every syscall.
1194 * We only need to do it after execve.
1195 */
1196 int currpers;
1197 long val;
Andreas Schwabd69fa492010-07-12 21:39:57 +02001198
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001199 /* Check for 64/32 bit mode. */
1200 if (upeek(tcp, sizeof(unsigned long)*PT_MSR, &val) < 0)
1201 return -1;
1202 /* SF is bit 0 of MSR */
1203 if (val < 0)
1204 currpers = 0;
1205 else
1206 currpers = 1;
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001207 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001208# endif
1209#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001210 scno = avr32_regs.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001211#elif defined(BFIN)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001212 if (upeek(tcp, PT_ORIG_P0, &scno))
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001213 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001214#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001215 scno = i386_regs.orig_eax;
H.J. Lu35be5812012-04-16 13:00:01 +02001216#elif defined(X86_64) || defined(X32)
1217# ifndef __X32_SYSCALL_BIT
1218# define __X32_SYSCALL_BIT 0x40000000
1219# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001220 int currpers;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001221# if 1
1222 /* GETREGSET of NT_PRSTATUS tells us regset size,
1223 * which unambiguously detects i386.
1224 *
1225 * Linux kernel distinguishes x86-64 and x32 processes
1226 * solely by looking at __X32_SYSCALL_BIT:
1227 * arch/x86/include/asm/compat.h::is_x32_task():
1228 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1229 * return true;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001230 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001231 if (x86_io.iov_len == sizeof(i386_regs)) {
1232 scno = i386_regs.orig_eax;
1233 currpers = 1;
1234 } else {
1235 scno = x86_64_regs.orig_rax;
1236 currpers = 0;
1237 if (scno & __X32_SYSCALL_BIT) {
1238 scno -= __X32_SYSCALL_BIT;
1239 currpers = 2;
1240 }
1241 }
1242# elif 0
1243 /* cs = 0x33 for long mode (native 64 bit and x32)
1244 * cs = 0x23 for compatibility mode (32 bit)
1245 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1246 */
1247 scno = x86_64_regs.orig_rax;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001248 switch (x86_64_regs.cs) {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001249 case 0x23: currpers = 1; break;
H.J. Lu35be5812012-04-16 13:00:01 +02001250 case 0x33:
1251 if (x86_64_regs.ds == 0x2b) {
1252 currpers = 2;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001253 scno &= ~__X32_SYSCALL_BIT;
H.J. Lu35be5812012-04-16 13:00:01 +02001254 } else
1255 currpers = 0;
1256 break;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001257 default:
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001258 fprintf(stderr, "Unknown value CS=0x%08X while "
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001259 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001260 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001261 currpers = current_personality;
1262 break;
1263 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001264# elif 0
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001265 /* This version analyzes the opcode of a syscall instruction.
1266 * (int 0x80 on i386 vs. syscall on x86-64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001267 * It works, but is too complicated, and strictly speaking, unreliable.
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001268 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001269 unsigned long call, rip = x86_64_regs.rip;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001270 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1271 rip -= 2;
1272 errno = 0;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001273 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001274 if (errno)
1275 fprintf(stderr, "ptrace_peektext failed: %s\n",
1276 strerror(errno));
1277 switch (call & 0xffff) {
1278 /* x86-64: syscall = 0x0f 0x05 */
1279 case 0x050f: currpers = 0; break;
1280 /* i386: int 0x80 = 0xcd 0x80 */
1281 case 0x80cd: currpers = 1; break;
1282 default:
1283 currpers = current_personality;
1284 fprintf(stderr,
1285 "Unknown syscall opcode (0x%04X) while "
1286 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001287 "PID=%d\n", (int)call, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001288 break;
1289 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001290# endif
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001291
H.J. Lu35be5812012-04-16 13:00:01 +02001292# ifdef X32
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001293 /* If we are built for a x32 system, then personality 0 is x32
1294 * (not x86_64), and stracing of x86_64 apps is not supported.
1295 * Stracing of i386 apps is still supported.
H.J. Lu085e4282012-04-17 11:05:04 -07001296 */
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001297 if (currpers == 0) {
1298 fprintf(stderr, "syscall_%lu(...) in unsupported "
1299 "64-bit mode of process PID=%d\n",
1300 scno, tcp->pid);
1301 return 0;
H.J. Lu35be5812012-04-16 13:00:01 +02001302 }
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001303 currpers &= ~2; /* map 2,1 to 0,1 */
H.J. Lu35be5812012-04-16 13:00:01 +02001304# endif
H.J. Lu085e4282012-04-17 11:05:04 -07001305 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001306#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001307# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01001308 long psr;
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001309 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001310 ia32 = (psr & IA64_PSR_IS) != 0;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001311 if (ia32) {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001312 if (upeek(tcp, PT_R1, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001313 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001314 } else {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001315 if (upeek(tcp, PT_R15, &scno) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001316 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001317 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001318#elif defined(AARCH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001319 switch (aarch64_io.iov_len) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001320 case sizeof(aarch64_regs):
1321 /* We are in 64-bit mode */
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001322 scno = aarch64_regs.regs[8];
1323 update_personality(tcp, 1);
1324 break;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001325 case sizeof(arm_regs):
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001326 /* We are in 32-bit mode */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001327 scno = arm_regs.ARM_r7;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001328 update_personality(tcp, 0);
1329 break;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001330 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001331#elif defined(ARM)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001332 if (arm_regs.ARM_ip != 0) {
1333 /* It is not a syscall entry */
1334 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001335 tcp->flags |= TCB_INSYSCALL;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001336 return 0;
1337 }
1338 /* Note: we support only 32-bit CPUs, not 26-bit */
1339
1340 if (arm_regs.ARM_cpsr & 0x20) {
1341 /* Thumb mode */
1342 scno = arm_regs.ARM_r7;
1343 } else {
1344 /* ARM mode */
1345 errno = 0;
1346 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1347 if (errno)
1348 return -1;
1349
1350 /* EABI syscall convention? */
1351 if (scno == 0xef000000) {
1352 scno = arm_regs.ARM_r7; /* yes */
1353 } else {
1354 if ((scno & 0x0ff00000) != 0x0f900000) {
1355 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1356 tcp->pid, scno);
1357 return -1;
1358 }
1359 /* Fixup the syscall number */
1360 scno &= 0x000fffff;
1361 }
1362 }
Denys Vlasenko7270de52013-02-21 15:46:34 +01001363
1364 scno = shuffle_scno(scno);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001365#elif defined(M68K)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001366 if (upeek(tcp, 4*PT_ORIG_D0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001367 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001368#elif defined(LINUX_MIPSN32)
Roland McGrath542c2c62008-05-20 01:11:56 +00001369 unsigned long long regs[38];
1370
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001371 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001372 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001373 mips_a3 = regs[REG_A3];
1374 mips_r2 = regs[REG_V0];
Roland McGrath542c2c62008-05-20 01:11:56 +00001375
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001376 scno = mips_r2;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001377 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001378 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001379 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001380 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001381 return 0;
1382 }
Roland McGrath542c2c62008-05-20 01:11:56 +00001383 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001384#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001385 if (upeek(tcp, REG_A3, &mips_a3) < 0)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001386 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001387 if (upeek(tcp, REG_V0, &scno) < 0)
1388 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001389
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001390 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001391 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001392 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001393 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001394 return 0;
1395 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001396 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001397#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001398 if (upeek(tcp, REG_A3, &alpha_a3) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001399 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001400 if (upeek(tcp, REG_R0, &scno) < 0)
1401 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001402
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001403 /*
1404 * Do some sanity checks to figure out if it's
1405 * really a syscall entry
1406 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001407 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001408 if (alpha_a3 == 0 || alpha_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001409 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001410 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001411 return 0;
1412 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001413 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001414#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001415 /* Disassemble the syscall trap. */
1416 /* Retrieve the syscall trap instruction. */
Denys Vlasenko46455822013-02-05 17:02:59 +01001417 unsigned long trap;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001418 errno = 0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001419# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001420 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001421 trap >>= 32;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001422# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001423 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001424# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001425 if (errno)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001426 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001427
1428 /* Disassemble the trap to see what personality to use. */
1429 switch (trap) {
1430 case 0x91d02010:
1431 /* Linux/SPARC syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001432 update_personality(tcp, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001433 break;
1434 case 0x91d0206d:
1435 /* Linux/SPARC64 syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001436 update_personality(tcp, 2);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001437 break;
1438 case 0x91d02000:
1439 /* SunOS syscall trap. (pers 1) */
1440 fprintf(stderr, "syscall: SunOS no support\n");
1441 return -1;
1442 case 0x91d02008:
1443 /* Solaris 2.x syscall trap. (per 2) */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001444 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001445 break;
1446 case 0x91d02009:
1447 /* NetBSD/FreeBSD syscall trap. */
1448 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1449 return -1;
1450 case 0x91d02027:
1451 /* Solaris 2.x gettimeofday */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001452 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001453 break;
1454 default:
Denys Vlasenko523635f2012-02-25 02:44:25 +01001455# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001456 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001457# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001458 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001459# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001460 return -1;
1461 }
1462
1463 /* Extract the system call number from the registers. */
1464 if (trap == 0x91d02027)
1465 scno = 156;
1466 else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001467 scno = sparc_regs.u_regs[U_REG_G1];
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001468 if (scno == 0) {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001469 scno = sparc_regs.u_regs[U_REG_O0];
1470 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 +02001471 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001472#elif defined(HPPA)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001473 if (upeek(tcp, PT_GR20, &scno) < 0)
1474 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001475#elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001476 /*
1477 * In the new syscall ABI, the system call number is in R3.
1478 */
1479 if (upeek(tcp, 4*(REG_REG0+3), &scno) < 0)
1480 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001481
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001482 if (scno < 0) {
1483 /* Odd as it may seem, a glibc bug has been known to cause
1484 glibc to issue bogus negative syscall numbers. So for
1485 our purposes, make strace print what it *should* have been */
1486 long correct_scno = (scno & 0xff);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001487 if (debug_flag)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001488 fprintf(stderr,
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001489 "Detected glibc bug: bogus system call"
1490 " number = %ld, correcting to %ld\n",
1491 scno,
1492 correct_scno);
1493 scno = correct_scno;
1494 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001495#elif defined(SH64)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001496 if (upeek(tcp, REG_SYSCALL, &scno) < 0)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001497 return -1;
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001498 scno &= 0xFFFF;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001499#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001500 if (upeek(tcp, 4*PT_R9, &scno) < 0)
1501 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001502#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001503 int currpers;
1504 scno = tile_regs.regs[10];
1505# ifdef __tilepro__
1506 currpers = 1;
1507# else
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001508# ifndef PT_FLAGS_COMPAT
1509# define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1510# endif
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001511 if (tile_regs.flags & PT_FLAGS_COMPAT)
1512 currpers = 1;
1513 else
1514 currpers = 0;
1515# endif
1516 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001517#elif defined(MICROBLAZE)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001518 if (upeek(tcp, 0, &scno) < 0)
1519 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001520#elif defined(OR1K)
1521 scno = or1k_regs.gpr[11];
James Hogan5f999a82013-02-22 14:44:10 +00001522#elif defined(METAG)
1523 scno = metag_regs.dx[0][1]; /* syscall number in D1Re0 (D1.0) */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001524#endif
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001525
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001526 tcp->scno = scno;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001527 if (SCNO_IS_VALID(tcp->scno)) {
1528 tcp->s_ent = &sysent[scno];
1529 tcp->qual_flg = qual_flags[scno];
1530 } else {
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01001531 static const struct_sysent unknown = {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001532 .nargs = MAX_ARGS,
1533 .sys_flags = 0,
1534 .sys_func = printargs,
1535 .sys_name = "unknown", /* not used */
1536 };
1537 tcp->s_ent = &unknown;
1538 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1539 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001540 return 1;
1541}
1542
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02001543/* Called at each syscall entry.
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001544 * Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001545 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1546 * 1: ok, continue in trace_syscall_entering().
1547 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001548 * ("????" etc) and bail out.
1549 */
Roland McGratha4d48532005-06-08 20:45:28 +00001550static int
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001551syscall_fixup_on_sysenter(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001552{
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001553 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001554#if defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001555 if (i386_regs.eax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001556 if (debug_flag)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001557 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1558 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001559 }
H.J. Lu35be5812012-04-16 13:00:01 +02001560#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001561 {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001562 long rax;
1563 if (x86_io.iov_len == sizeof(i386_regs)) {
1564 /* Sign extend from 32 bits */
1565 rax = (int32_t)i386_regs.eax;
1566 } else {
1567 /* Note: in X32 build, this truncates 64 to 32 bits */
1568 rax = x86_64_regs.rax;
1569 }
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001570 if (rax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001571 if (debug_flag)
Denys Vlasenko18beb982011-08-24 16:59:23 +02001572 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1573 return 0;
1574 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001575 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001576#elif defined(S390) || defined(S390X)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001577 /* TODO: we already fetched PT_GPR2 in get_scno
1578 * and stored it in syscall_mode, reuse it here
1579 * instead of re-fetching?
1580 */
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001581 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001582 return -1;
Michal Ludvig882eda82002-11-11 12:50:47 +00001583 if (syscall_mode != -ENOSYS)
1584 syscall_mode = tcp->scno;
Denys Vlasenkoece98792011-08-25 10:25:35 +02001585 if (gpr2 != syscall_mode) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001586 if (debug_flag)
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001587 fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2);
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001588 return 0;
1589 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001590#elif defined(M68K)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001591 /* TODO? Eliminate upeek's in arches below like we did in x86 */
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001592 if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001593 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001594 if (m68k_d0 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001595 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001596 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001597 return 0;
1598 }
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001599#elif defined(IA64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001600 if (upeek(tcp, PT_R10, &ia64_r10) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001601 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001602 if (upeek(tcp, PT_R8, &ia64_r8) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001603 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001604 if (ia32 && ia64_r8 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001605 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001606 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001607 return 0;
1608 }
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001609#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001610 if (upeek(tcp, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001611 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001612 if (cris_r10 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001613 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001614 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001615 return 0;
1616 }
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001617#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001618 if (upeek(tcp, 3 * 4, &microblaze_r3) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001619 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001620 if (microblaze_r3 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001621 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001622 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001623 return 0;
1624 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001625#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001626 return 1;
1627}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001628
Denys Vlasenko146b9442012-03-18 22:10:48 +01001629static void
1630internal_fork(struct tcb *tcp)
1631{
1632#if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1633# define ARG_FLAGS 1
1634#else
1635# define ARG_FLAGS 0
1636#endif
1637#ifndef CLONE_UNTRACED
1638# define CLONE_UNTRACED 0x00800000
1639#endif
1640 if ((ptrace_setoptions
1641 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1642 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1643 return;
1644
1645 if (!followfork)
1646 return;
1647
1648 if (entering(tcp)) {
1649 /*
1650 * We won't see the new child if clone is called with
1651 * CLONE_UNTRACED, so we keep the same logic with that option
1652 * and don't trace it.
1653 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001654 if ((tcp->s_ent->sys_func == sys_clone)
1655 && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
1656 )
Denys Vlasenko146b9442012-03-18 22:10:48 +01001657 return;
1658 setbpt(tcp);
1659 } else {
1660 if (tcp->flags & TCB_BPTSET)
1661 clearbpt(tcp);
1662 }
1663}
1664
1665#if defined(TCB_WAITEXECVE)
1666static void
1667internal_exec(struct tcb *tcp)
1668{
1669 /* Maybe we have post-execve SIGTRAP suppressed? */
1670 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1671 return; /* yes, no need to do anything */
1672
1673 if (exiting(tcp) && syserror(tcp))
1674 /* Error in execve, no post-execve SIGTRAP expected */
1675 tcp->flags &= ~TCB_WAITEXECVE;
1676 else
1677 tcp->flags |= TCB_WAITEXECVE;
1678}
1679#endif
1680
1681static void
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001682syscall_fixup_for_fork_exec(struct tcb *tcp)
Roland McGrathc1e45922008-05-27 23:18:29 +00001683{
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001684 /*
1685 * We must always trace a few critical system calls in order to
1686 * correctly support following forks in the presence of tracing
1687 * qualifiers.
1688 */
1689 int (*func)();
1690
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001691 func = tcp->s_ent->sys_func;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001692
1693 if ( sys_fork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001694 || sys_vfork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001695 || sys_clone == func
Denys Vlasenko146b9442012-03-18 22:10:48 +01001696 ) {
1697 internal_fork(tcp);
1698 return;
1699 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001700
Denys Vlasenko84703742012-02-25 02:38:52 +01001701#if defined(TCB_WAITEXECVE)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001702 if ( sys_execve == func
Denys Vlasenko84703742012-02-25 02:38:52 +01001703# if defined(SPARC) || defined(SPARC64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001704 || sys_execv == func
Denys Vlasenkoa7949742011-08-21 17:26:55 +02001705# endif
Denys Vlasenko146b9442012-03-18 22:10:48 +01001706 ) {
1707 internal_exec(tcp);
1708 return;
1709 }
Roland McGrathc1e45922008-05-27 23:18:29 +00001710#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001711}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001712
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001713/* Return -1 on error or 1 on success (never 0!) */
Roland McGratha4d48532005-06-08 20:45:28 +00001714static int
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001715get_syscall_args(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001716{
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001717 int i, nargs;
1718
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001719 nargs = tcp->s_ent->nargs;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001720
Denys Vlasenko523635f2012-02-25 02:44:25 +01001721#if defined(S390) || defined(S390X)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001722 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001723 if (upeek(tcp, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
1724 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001725#elif defined(ALPHA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001726 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001727 if (upeek(tcp, REG_A0+i, &tcp->u_arg[i]) < 0)
1728 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001729#elif defined(IA64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001730 if (!ia32) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001731 unsigned long *out0, cfm, sof, sol;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001732 long rbs_end;
1733 /* be backwards compatible with kernel < 2.4.4... */
1734# ifndef PT_RBS_END
1735# define PT_RBS_END PT_AR_BSP
1736# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001737
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001738 if (upeek(tcp, PT_RBS_END, &rbs_end) < 0)
1739 return -1;
1740 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001741 return -1;
1742
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001743 sof = (cfm >> 0) & 0x7f;
1744 sol = (cfm >> 7) & 0x7f;
1745 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1746
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001747 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001748 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1749 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1750 return -1;
1751 }
1752 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001753 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1754 PT_R9 /* ECX = out1 */,
1755 PT_R10 /* EDX = out2 */,
1756 PT_R14 /* ESI = out3 */,
1757 PT_R15 /* EDI = out4 */,
1758 PT_R13 /* EBP = out5 */};
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001759
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001760 for (i = 0; i < nargs; ++i) {
1761 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1762 return -1;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001763 /* truncate away IVE sign-extension */
1764 tcp->u_arg[i] &= 0xffffffff;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001765 }
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001766 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001767#elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001768 /* N32 and N64 both use up to six registers. */
1769 unsigned long long regs[38];
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001770
1771 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1772 return -1;
1773
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001774 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001775 tcp->u_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001776# if defined(LINUX_MIPSN32)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001777 tcp->ext_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001778# endif
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001779 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001780#elif defined(MIPS)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001781 if (nargs > 4) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001782 long sp;
1783
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001784 if (upeek(tcp, REG_SP, &sp) < 0)
1785 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001786 for (i = 0; i < 4; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001787 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1788 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001789 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001790 (char *)(tcp->u_arg + 4));
1791 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001792 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001793 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001794 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001795 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001796#elif defined(POWERPC)
1797# ifndef PT_ORIG_R3
1798# define PT_ORIG_R3 34
1799# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001800 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001801 if (upeek(tcp, (i==0) ?
1802 (sizeof(unsigned long) * PT_ORIG_R3) :
1803 ((i+PT_R3) * sizeof(unsigned long)),
1804 &tcp->u_arg[i]) < 0)
1805 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001806 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001807#elif defined(SPARC) || defined(SPARC64)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001808 for (i = 0; i < nargs; ++i)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001809 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001810#elif defined(HPPA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001811 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001812 if (upeek(tcp, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1813 return -1;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001814#elif defined(ARM) || defined(AARCH64)
1815# if defined(AARCH64)
1816 if (tcp->currpers == 1)
1817 for (i = 0; i < nargs; ++i)
1818 tcp->u_arg[i] = aarch64_regs.regs[i];
1819 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001820# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001821 for (i = 0; i < nargs; ++i)
Denys Vlasenko401374e2013-02-06 18:24:39 +01001822 tcp->u_arg[i] = arm_regs.uregs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001823#elif defined(AVR32)
Denys Vlasenkob5b25892011-08-30 19:04:54 +02001824 (void)i;
1825 (void)nargs;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001826 tcp->u_arg[0] = avr32_regs.r12;
1827 tcp->u_arg[1] = avr32_regs.r11;
1828 tcp->u_arg[2] = avr32_regs.r10;
1829 tcp->u_arg[3] = avr32_regs.r9;
1830 tcp->u_arg[4] = avr32_regs.r5;
1831 tcp->u_arg[5] = avr32_regs.r3;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001832#elif defined(BFIN)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001833 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 +02001834
Denys Vlasenko4b887a52011-08-23 13:32:38 +02001835 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001836 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1837 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001838#elif defined(SH)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001839 static const int syscall_regs[MAX_ARGS] = {
1840 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1841 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001842 };
1843
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001844 for (i = 0; i < nargs; ++i)
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001845 if (upeek(tcp, syscall_regs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001846 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001847#elif defined(SH64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001848 int i;
1849 /* Registers used by SH5 Linux system calls for parameters */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001850 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001851
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001852 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001853 if (upeek(tcp, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1854 return -1;
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001855#elif defined(I386)
1856 (void)i;
1857 (void)nargs;
1858 tcp->u_arg[0] = i386_regs.ebx;
1859 tcp->u_arg[1] = i386_regs.ecx;
1860 tcp->u_arg[2] = i386_regs.edx;
1861 tcp->u_arg[3] = i386_regs.esi;
1862 tcp->u_arg[4] = i386_regs.edi;
1863 tcp->u_arg[5] = i386_regs.ebp;
H.J. Lu35be5812012-04-16 13:00:01 +02001864#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001865 (void)i;
1866 (void)nargs;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001867 if (x86_io.iov_len != sizeof(i386_regs)) {
1868 /* x86-64 or x32 ABI */
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001869 tcp->u_arg[0] = x86_64_regs.rdi;
1870 tcp->u_arg[1] = x86_64_regs.rsi;
1871 tcp->u_arg[2] = x86_64_regs.rdx;
1872 tcp->u_arg[3] = x86_64_regs.r10;
1873 tcp->u_arg[4] = x86_64_regs.r8;
1874 tcp->u_arg[5] = x86_64_regs.r9;
H.J. Lu35be5812012-04-16 13:00:01 +02001875# ifdef X32
1876 tcp->ext_arg[0] = x86_64_regs.rdi;
1877 tcp->ext_arg[1] = x86_64_regs.rsi;
1878 tcp->ext_arg[2] = x86_64_regs.rdx;
1879 tcp->ext_arg[3] = x86_64_regs.r10;
1880 tcp->ext_arg[4] = x86_64_regs.r8;
1881 tcp->ext_arg[5] = x86_64_regs.r9;
1882# endif
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001883 } else {
1884 /* i386 ABI */
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001885 /* Zero-extend from 32 bits */
1886 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1887 * if you need to use *sign-extended* parameter.
1888 */
1889 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1890 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1891 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1892 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1893 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1894 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001895 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001896#elif defined(MICROBLAZE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001897 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001898 if (upeek(tcp, (5 + i) * 4, &tcp->u_arg[i]) < 0)
1899 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001900#elif defined(CRISV10) || defined(CRISV32)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001901 static const int crisregs[MAX_ARGS] = {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001902 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001903 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001904 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001905
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001906 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001907 if (upeek(tcp, crisregs[i], &tcp->u_arg[i]) < 0)
1908 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001909#elif defined(TILE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001910 for (i = 0; i < nargs; ++i)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001911 tcp->u_arg[i] = tile_regs.regs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001912#elif defined(M68K)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001913 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001914 if (upeek(tcp, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
1915 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001916#elif defined(OR1K)
1917 (void)nargs;
1918 for (i = 0; i < 6; ++i)
1919 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
James Hogan5f999a82013-02-22 14:44:10 +00001920#elif defined(METAG)
1921 for (i = 0; i < nargs; i++)
1922 /* arguments go backwards from D1Ar1 (D1.3) */
1923 tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001924#else /* Other architecture (32bits specific) */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001925 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001926 if (upeek(tcp, i*4, &tcp->u_arg[i]) < 0)
1927 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001928#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001929 return 1;
1930}
1931
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00001932static int
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001933trace_syscall_entering(struct tcb *tcp)
1934{
1935 int res, scno_good;
1936
Denys Vlasenko2ce12ed2011-08-24 17:25:32 +02001937#if defined TCB_WAITEXECVE
1938 if (tcp->flags & TCB_WAITEXECVE) {
1939 /* This is the post-execve SIGTRAP. */
1940 tcp->flags &= ~TCB_WAITEXECVE;
1941 return 0;
1942 }
1943#endif
1944
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001945 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001946 if (res == 0)
1947 return res;
Denys Vlasenko907735a2012-03-21 00:23:16 +01001948 if (res == 1) {
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001949 res = syscall_fixup_on_sysenter(tcp);
Denys Vlasenko907735a2012-03-21 00:23:16 +01001950 if (res == 0)
1951 return res;
1952 if (res == 1)
1953 res = get_syscall_args(tcp);
1954 }
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001955
1956 if (res != 1) {
1957 printleader(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001958 if (scno_good != 1)
Denys Vlasenkob7a6dae2012-03-20 16:48:35 +01001959 tprints("????" /* anti-trigraph gap */ "(");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001960 else if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01001961 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001962 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001963 tprintf("%s(", tcp->s_ent->sys_name);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001964 /*
1965 * " <unavailable>" will be added later by the code which
1966 * detects ptrace errors.
1967 */
1968 goto ret;
1969 }
1970
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001971#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001972 while (1) {
Denys Vlasenko523635f2012-02-25 02:44:25 +01001973# ifdef SYS_socket_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001974 if (tcp->s_ent->sys_func == sys_socketcall) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00001975 decode_socket_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001976 break;
1977 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001978# endif
1979# ifdef SYS_ipc_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001980 if (tcp->s_ent->sys_func == sys_ipc) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00001981 decode_ipc_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001982 break;
1983 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001984# endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001985 break;
1986 }
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001987#endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001988
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001989 if (need_fork_exec_workarounds)
1990 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001991
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001992 if (!(tcp->qual_flg & QUAL_TRACE)
1993 || (tracing_paths && !pathtrace_match(tcp))
1994 ) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001995 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
1996 return 0;
1997 }
1998
1999 tcp->flags &= ~TCB_FILTERED;
2000
2001 if (cflag == CFLAG_ONLY_STATS) {
2002 res = 0;
2003 goto ret;
2004 }
2005
2006 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002007 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002008 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002009 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002010 tprintf("%s(", tcp->s_ent->sys_name);
2011 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002012 res = printargs(tcp);
2013 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002014 res = tcp->s_ent->sys_func(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002015
Dmitry V. Levinb742d8c2012-09-17 22:40:12 +00002016 fflush(tcp->outf);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002017 ret:
2018 tcp->flags |= TCB_INSYSCALL;
2019 /* Measure the entrance time as late as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002020 if (Tflag || cflag)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002021 gettimeofday(&tcp->etime, NULL);
2022 return res;
2023}
2024
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002025/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01002026 * 1: ok, continue in trace_syscall_exiting().
2027 * -1: error, trace_syscall_exiting() should print error indicator
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002028 * ("????" etc) and bail out.
2029 */
2030static int
2031get_syscall_result(struct tcb *tcp)
2032{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002033#if defined(S390) || defined(S390X)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002034 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
2035 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002036#elif defined(POWERPC)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002037# define SO_MASK 0x10000000
2038 {
2039 long flags;
2040 if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0)
2041 return -1;
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002042 if (upeek(tcp, sizeof(unsigned long)*PT_R3, &ppc_result) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002043 return -1;
2044 if (flags & SO_MASK)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002045 ppc_result = -ppc_result;
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002046 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002047#elif defined(AVR32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002048 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002049#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002050 if (upeek(tcp, PT_R0, &bfin_r0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002051 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002052#elif defined(I386)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002053 /* already done by get_regs */
H.J. Lu35be5812012-04-16 13:00:01 +02002054#elif defined(X86_64) || defined(X32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002055 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002056#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002057# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01002058 long psr;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002059 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
2060 ia32 = (psr & IA64_PSR_IS) != 0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002061 if (upeek(tcp, PT_R8, &ia64_r8) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002062 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002063 if (upeek(tcp, PT_R10, &ia64_r10) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002064 return -1;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002065#elif defined(ARM)
2066 /* already done by get_regs */
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002067#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002068 /* register reading already done by get_regs */
2069
2070 /* Used to do this, but we did it on syscall entry already: */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002071 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
2072 * else it's personality 0.
2073 */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002074 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
Denys Vlasenko523635f2012-02-25 02:44:25 +01002075#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002076 if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002077 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002078#elif defined(LINUX_MIPSN32)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002079 unsigned long long regs[38];
2080
2081 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
2082 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002083 mips_a3 = regs[REG_A3];
2084 mips_r2 = regs[REG_V0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01002085#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002086 if (upeek(tcp, REG_A3, &mips_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002087 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002088 if (upeek(tcp, REG_V0, &mips_r2) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002089 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002090#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002091 if (upeek(tcp, REG_A3, &alpha_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002092 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002093 if (upeek(tcp, REG_R0, &alpha_r0) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002094 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002095#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002096 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002097#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002098 if (upeek(tcp, PT_GR28, &hppa_r28) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002099 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002100#elif defined(SH)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002101 /* new syscall ABI returns result in R0 */
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002102 if (upeek(tcp, 4*REG_REG0, (long *)&sh_r0) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002103 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002104#elif defined(SH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002105 /* ABI defines result returned in r9 */
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002106 if (upeek(tcp, REG_GENERAL(9), (long *)&sh64_r9) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002107 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002108#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002109 if (upeek(tcp, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002110 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002111#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002112 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002113#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002114 if (upeek(tcp, 3 * 4, &microblaze_r3) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002115 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01002116#elif defined(OR1K)
2117 /* already done by get_regs */
James Hogan5f999a82013-02-22 14:44:10 +00002118#elif defined(METAG)
2119 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002120#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002121 return 1;
2122}
2123
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002124/* Called at each syscall exit */
2125static void
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002126syscall_fixup_on_sysexit(struct tcb *tcp)
2127{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002128#if defined(S390) || defined(S390X)
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002129 if (syscall_mode != -ENOSYS)
2130 syscall_mode = tcp->scno;
Denys Vlasenkoece98792011-08-25 10:25:35 +02002131 if ((tcp->flags & TCB_WAITEXECVE)
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002132 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
2133 /*
2134 * Return from execve.
2135 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2136 * flag set for the post-execve SIGTRAP to see and reset.
2137 */
2138 gpr2 = 0;
2139 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002140#endif
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002141}
2142
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002143/*
2144 * Check the syscall return value register value for whether it is
2145 * a negated errno code indicating an error, or a success return value.
2146 */
2147static inline int
2148is_negated_errno(unsigned long int val)
2149{
2150 unsigned long int max = -(long int) nerrnos;
Denys Vlasenko2544f982013-02-19 17:39:56 +01002151#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01002152 if (current_wordsize < sizeof(val)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002153 val = (unsigned int) val;
2154 max = (unsigned int) max;
2155 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002156#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002157 return val > max;
2158}
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002159
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002160#if defined(X32)
2161static inline int
2162is_negated_errno_x32(unsigned long long val)
2163{
2164 unsigned long long max = -(long long) nerrnos;
2165 /*
2166 * current_wordsize is 4 even in personality 0 (native X32)
2167 * but truncation _must not_ be done in it.
2168 * can't check current_wordsize here!
2169 */
2170 if (current_personality != 0) {
2171 val = (uint32_t) val;
2172 max = (uint32_t) max;
2173 }
2174 return val > max;
2175}
2176#endif
2177
Denys Vlasenko907735a2012-03-21 00:23:16 +01002178/* Returns:
2179 * 1: ok, continue in trace_syscall_exiting().
2180 * -1: error, trace_syscall_exiting() should print error indicator
2181 * ("????" etc) and bail out.
2182 */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002183static void
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002184get_error(struct tcb *tcp)
2185{
2186 int u_error = 0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002187 int check_errno = 1;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002188 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002189 check_errno = 0;
2190 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002191#if defined(S390) || defined(S390X)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002192 if (check_errno && is_negated_errno(gpr2)) {
2193 tcp->u_rval = -1;
2194 u_error = -gpr2;
2195 }
2196 else {
2197 tcp->u_rval = gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002198 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002199#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002200 if (check_errno && is_negated_errno(i386_regs.eax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002201 tcp->u_rval = -1;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002202 u_error = -i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002203 }
2204 else {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002205 tcp->u_rval = i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002206 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002207#elif defined(X86_64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002208 long rax;
2209 if (x86_io.iov_len == sizeof(i386_regs)) {
2210 /* Sign extend from 32 bits */
2211 rax = (int32_t)i386_regs.eax;
2212 } else {
2213 rax = x86_64_regs.rax;
2214 }
2215 if (check_errno && is_negated_errno(rax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002216 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002217 u_error = -rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002218 }
2219 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002220 tcp->u_rval = rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002221 }
2222#elif defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002223 /* In X32, return value is 64-bit (llseek uses one).
2224 * Using merely "long rax" would not work.
2225 */
2226 long long rax;
2227 if (x86_io.iov_len == sizeof(i386_regs)) {
2228 /* Sign extend from 32 bits */
2229 rax = (int32_t)i386_regs.eax;
2230 } else {
2231 rax = x86_64_regs.rax;
2232 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002233 /* Careful: is_negated_errno() works only on longs */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002234 if (check_errno && is_negated_errno_x32(rax)) {
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002235 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002236 u_error = -rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002237 }
2238 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002239 tcp->u_rval = rax; /* truncating */
2240 tcp->u_lrval = rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002241 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002242#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002243 if (ia32) {
2244 int err;
2245
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002246 err = (int)ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002247 if (check_errno && is_negated_errno(err)) {
2248 tcp->u_rval = -1;
2249 u_error = -err;
2250 }
2251 else {
2252 tcp->u_rval = err;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002253 }
2254 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002255 if (check_errno && ia64_r10) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002256 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002257 u_error = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002258 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002259 tcp->u_rval = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002260 }
2261 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002262#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002263 if (check_errno && mips_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002264 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002265 u_error = mips_r2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002266 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002267 tcp->u_rval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002268# if defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002269 tcp->u_lrval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002270# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002271 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002272#elif defined(POWERPC)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002273 if (check_errno && is_negated_errno(ppc_result)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002274 tcp->u_rval = -1;
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002275 u_error = -ppc_result;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002276 }
2277 else {
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002278 tcp->u_rval = ppc_result;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002279 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002280#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002281 if (check_errno && is_negated_errno(m68k_d0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002282 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002283 u_error = -m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002284 }
2285 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002286 tcp->u_rval = m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002287 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002288#elif defined(ARM) || defined(AARCH64)
2289# if defined(AARCH64)
2290 if (tcp->currpers == 1) {
2291 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2292 tcp->u_rval = -1;
2293 u_error = -aarch64_regs.regs[0];
2294 }
2295 else {
2296 tcp->u_rval = aarch64_regs.regs[0];
2297 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002298 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002299 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002300# endif
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002301 {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002302 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002303 tcp->u_rval = -1;
Denys Vlasenko401374e2013-02-06 18:24:39 +01002304 u_error = -arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002305 }
2306 else {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002307 tcp->u_rval = arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002308 }
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002309 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002310#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002311 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002312 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002313 u_error = -avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002314 }
2315 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002316 tcp->u_rval = avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002317 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002318#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002319 if (check_errno && is_negated_errno(bfin_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002320 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002321 u_error = -bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002322 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002323 tcp->u_rval = bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002324 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002325#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002326 if (check_errno && alpha_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002327 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002328 u_error = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002329 }
2330 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002331 tcp->u_rval = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002332 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002333#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002334 if (check_errno && sparc_regs.psr & PSR_C) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002335 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002336 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002337 }
2338 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002339 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002340 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002341#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002342 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002343 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002344 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002345 }
2346 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002347 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002348 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002349#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002350 if (check_errno && is_negated_errno(hppa_r28)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002351 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002352 u_error = -hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002353 }
2354 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002355 tcp->u_rval = hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002356 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002357#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002358 if (check_errno && is_negated_errno(sh_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002359 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002360 u_error = -sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002361 }
2362 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002363 tcp->u_rval = sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002364 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002365#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002366 if (check_errno && is_negated_errno(sh64_r9)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002367 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002368 u_error = -sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002369 }
2370 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002371 tcp->u_rval = sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002372 }
James Hogan5f999a82013-02-22 14:44:10 +00002373#elif defined(METAG)
2374 /* result pointer in D0Re0 (D0.0) */
2375 if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2376 tcp->u_rval = -1;
2377 u_error = -metag_regs.dx[0][0];
2378 }
2379 else {
2380 tcp->u_rval = metag_regs.dx[0][0];
2381 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002382#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002383 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002384 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002385 u_error = -cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002386 }
2387 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002388 tcp->u_rval = cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002389 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002390#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002391 /*
2392 * The standard tile calling convention returns the value (or negative
2393 * errno) in r0, and zero (or positive errno) in r1.
2394 * Until at least kernel 3.8, however, the r1 value is not reflected
2395 * in ptregs at this point, so we use r0 here.
2396 */
2397 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002398 tcp->u_rval = -1;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002399 u_error = -tile_regs.regs[0];
2400 } else {
2401 tcp->u_rval = tile_regs.regs[0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002402 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002403#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002404 if (check_errno && is_negated_errno(microblaze_r3)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002405 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002406 u_error = -microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002407 }
2408 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002409 tcp->u_rval = microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002410 }
Christian Svensson492f81f2013-02-14 13:26:27 +01002411#elif defined(OR1K)
2412 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2413 tcp->u_rval = -1;
2414 u_error = -or1k_regs.gpr[11];
2415 }
2416 else {
2417 tcp->u_rval = or1k_regs.gpr[11];
2418 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002419#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002420 tcp->u_error = u_error;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002421}
2422
2423static void
2424dumpio(struct tcb *tcp)
2425{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002426 int (*func)();
2427
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002428 if (syserror(tcp))
2429 return;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +01002430 if ((unsigned long) tcp->u_arg[0] >= num_quals)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002431 return;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002432 func = tcp->s_ent->sys_func;
2433 if (func == printargs)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002434 return;
2435 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002436 if (func == sys_read ||
2437 func == sys_pread ||
2438 func == sys_recv ||
2439 func == sys_recvfrom)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002440 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002441 else if (func == sys_readv)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002442 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2443 return;
2444 }
2445 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002446 if (func == sys_write ||
2447 func == sys_pwrite ||
2448 func == sys_send ||
2449 func == sys_sendto)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002450 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002451 else if (func == sys_writev)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002452 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2453 return;
2454 }
2455}
2456
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002457static int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002458trace_syscall_exiting(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002459{
2460 int sys_res;
2461 struct timeval tv;
Denys Vlasenko1a5b5a72011-08-25 00:29:56 +02002462 int res;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002463 long u_error;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002464
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002465 /* Measure the exit time as early as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002466 if (Tflag || cflag)
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002467 gettimeofday(&tv, NULL);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002468
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00002469#if SUPPORTED_PERSONALITIES > 1
2470 update_personality(tcp, tcp->currpers);
2471#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002472 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002473 if (res == 1) {
2474 syscall_fixup_on_sysexit(tcp); /* never fails */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002475 get_error(tcp); /* never fails */
2476 if (need_fork_exec_workarounds)
2477 syscall_fixup_for_fork_exec(tcp);
2478 if (filtered(tcp))
2479 goto ret;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002480 }
2481
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002482 if (cflag) {
2483 struct timeval t = tv;
Denys Vlasenkoc95a88f2011-08-21 17:47:40 +02002484 count_syscall(tcp, &t);
Denys Vlasenko7b609d52011-06-22 14:32:43 +02002485 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002486 goto ret;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002487 }
2488 }
2489
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002490 /* If not in -ff mode, and printing_tcp != tcp,
2491 * then the log currently does not end with output
2492 * of _our syscall entry_, but with something else.
2493 * We need to say which syscall's return is this.
2494 *
2495 * Forced reprinting via TCB_REPRINT is used only by
2496 * "strace -ff -oLOG test/threaded_execve" corner case.
2497 * It's the only case when -ff mode needs reprinting.
2498 */
2499 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2500 tcp->flags &= ~TCB_REPRINT;
2501 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002502 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002503 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002504 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002505 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002506 }
2507 printing_tcp = tcp;
2508
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002509 if (res != 1) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002510 /* There was error in one of prior ptrace ops */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002511 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002512 tabto();
Denys Vlasenko000b6012012-01-28 01:25:03 +01002513 tprints("= ? <unavailable>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002514 line_ended();
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002515 tcp->flags &= ~TCB_INSYSCALL;
2516 return res;
2517 }
2518
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002519 sys_res = 0;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002520 if (tcp->qual_flg & QUAL_RAW) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002521 /* sys_res = printargs(tcp); - but it's nop on sysexit */
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002522 } else {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002523 /* FIXME: not_failing_only (IOW, option -z) is broken:
2524 * failure of syscall is known only after syscall return.
2525 * Thus we end up with something like this on, say, ENOENT:
2526 * open("doesnt_exist", O_RDONLY <unfinished ...>
2527 * {next syscall decode}
2528 * whereas the intended result is that open(...) line
2529 * is not shown at all.
2530 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002531 if (not_failing_only && tcp->u_error)
Denys Vlasenko3b738812011-08-22 02:06:35 +02002532 goto ret; /* ignore failed syscalls */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002533 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002534 }
2535
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002536 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002537 tabto();
Denys Vlasenko3b738812011-08-22 02:06:35 +02002538 u_error = tcp->u_error;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002539 if (tcp->qual_flg & QUAL_RAW) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002540 if (u_error)
2541 tprintf("= -1 (errno %ld)", u_error);
2542 else
2543 tprintf("= %#lx", tcp->u_rval);
2544 }
2545 else if (!(sys_res & RVAL_NONE) && u_error) {
2546 switch (u_error) {
Denys Vlasenkofe585652012-01-12 11:26:34 +01002547 /* Blocked signals do not interrupt any syscalls.
2548 * In this case syscalls don't return ERESTARTfoo codes.
2549 *
2550 * Deadly signals set to SIG_DFL interrupt syscalls
2551 * and kill the process regardless of which of the codes below
2552 * is returned by the interrupted syscall.
2553 * In some cases, kernel forces a kernel-generated deadly
2554 * signal to be unblocked and set to SIG_DFL (and thus cause
2555 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2556 * or SIGILL. (The alternative is to leave process spinning
2557 * forever on the faulty instruction - not useful).
2558 *
2559 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2560 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2561 * but kernel will always restart them.
2562 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002563 case ERESTARTSYS:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002564 /* Most common type of signal-interrupted syscall exit code.
2565 * The system call will be restarted with the same arguments
2566 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2567 */
2568 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002569 break;
2570 case ERESTARTNOINTR:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002571 /* Rare. For example, fork() returns this if interrupted.
2572 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2573 */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002574 tprints("= ? ERESTARTNOINTR (To be restarted)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002575 break;
2576 case ERESTARTNOHAND:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002577 /* pause(), rt_sigsuspend() etc use this code.
2578 * SA_RESTART is ignored (assumed not set):
2579 * syscall won't restart (will return EINTR instead)
Denys Vlasenko30c03232013-02-19 16:59:26 +01002580 * even after signal with SA_RESTART set. However,
2581 * after SIG_IGN or SIG_DFL signal it will restart
2582 * (thus the name "restart only if has no handler").
Denys Vlasenkofe585652012-01-12 11:26:34 +01002583 */
Denys Vlasenkoaba62922013-03-05 16:56:35 +01002584 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002585 break;
2586 case ERESTART_RESTARTBLOCK:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002587 /* Syscalls like nanosleep(), poll() which can't be
2588 * restarted with their original arguments use this
2589 * code. Kernel will execute restart_syscall() instead,
2590 * which changes arguments before restarting syscall.
2591 * SA_RESTART is ignored (assumed not set) similarly
2592 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2593 * since restart data is saved in "restart block"
2594 * in task struct, and if signal handler uses a syscall
2595 * which in turn saves another such restart block,
2596 * old data is lost and restart becomes impossible)
2597 */
2598 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002599 break;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002600 default:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002601 if (u_error < 0)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002602 tprintf("= -1 E??? (errno %ld)", u_error);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002603 else if (u_error < nerrnos)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002604 tprintf("= -1 %s (%s)", errnoent[u_error],
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002605 strerror(u_error));
2606 else
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002607 tprintf("= -1 ERRNO_%ld (%s)", u_error,
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002608 strerror(u_error));
2609 break;
2610 }
2611 if ((sys_res & RVAL_STR) && tcp->auxstr)
2612 tprintf(" (%s)", tcp->auxstr);
2613 }
2614 else {
2615 if (sys_res & RVAL_NONE)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002616 tprints("= ?");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002617 else {
2618 switch (sys_res & RVAL_MASK) {
2619 case RVAL_HEX:
2620 tprintf("= %#lx", tcp->u_rval);
2621 break;
2622 case RVAL_OCTAL:
2623 tprintf("= %#lo", tcp->u_rval);
2624 break;
2625 case RVAL_UDECIMAL:
2626 tprintf("= %lu", tcp->u_rval);
2627 break;
2628 case RVAL_DECIMAL:
2629 tprintf("= %ld", tcp->u_rval);
2630 break;
H.J. Ludd0130b2012-04-16 12:16:45 +02002631#if defined(LINUX_MIPSN32) || defined(X32)
2632 /*
2633 case RVAL_LHEX:
2634 tprintf("= %#llx", tcp->u_lrval);
2635 break;
2636 case RVAL_LOCTAL:
2637 tprintf("= %#llo", tcp->u_lrval);
2638 break;
2639 */
2640 case RVAL_LUDECIMAL:
2641 tprintf("= %llu", tcp->u_lrval);
2642 break;
2643 /*
2644 case RVAL_LDECIMAL:
2645 tprintf("= %lld", tcp->u_lrval);
2646 break;
2647 */
2648#endif
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002649 default:
2650 fprintf(stderr,
2651 "invalid rval format\n");
2652 break;
2653 }
2654 }
2655 if ((sys_res & RVAL_STR) && tcp->auxstr)
2656 tprintf(" (%s)", tcp->auxstr);
2657 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002658 if (Tflag) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002659 tv_sub(&tv, &tv, &tcp->etime);
2660 tprintf(" <%ld.%06ld>",
2661 (long) tv.tv_sec, (long) tv.tv_usec);
2662 }
Denys Vlasenko000b6012012-01-28 01:25:03 +01002663 tprints("\n");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002664 dumpio(tcp);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002665 line_ended();
2666
Denys Vlasenko3b738812011-08-22 02:06:35 +02002667 ret:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002668 tcp->flags &= ~TCB_INSYSCALL;
2669 return 0;
2670}
2671
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002672int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002673trace_syscall(struct tcb *tcp)
2674{
2675 return exiting(tcp) ?
2676 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2677}