blob: 72ef2333284b4a8005738c87d70f66deb802fca8 [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);
1008# elif defined(X86_64) || defined(X32)
Denys Vlasenkoe3b248d2013-02-15 00:24:19 +01001009 /*
1010 * PTRACE_GETREGSET was introduced in 2.6.33.
1011 * Let's be paranoid and require a bit later kernel.
1012 */
1013 if (os_release >= KERNEL_VERSION(2,6,35)) {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001014 /*x86_io.iov_base = &x86_regs_union; - already is */
1015 x86_io.iov_len = sizeof(x86_regs_union);
1016 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (long) &x86_io);
1017 } else {
1018 /* Use old method, with heuristical detection of 32-bitness */
1019 x86_io.iov_len = sizeof(x86_64_regs);
1020 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &x86_64_regs);
1021 if (!get_regs_error && x86_64_regs.cs == 0x23) {
1022 x86_io.iov_len = sizeof(i386_regs);
1023 /*
1024 * The order is important: i386_regs and x86_64_regs
1025 * are overlaid in memory!
1026 */
1027 i386_regs.ebx = x86_64_regs.rbx;
1028 i386_regs.ecx = x86_64_regs.rcx;
1029 i386_regs.edx = x86_64_regs.rdx;
1030 i386_regs.esi = x86_64_regs.rsi;
1031 i386_regs.edi = x86_64_regs.rdi;
1032 i386_regs.ebp = x86_64_regs.rbp;
1033 i386_regs.eax = x86_64_regs.rax;
1034 /*i386_regs.xds = x86_64_regs.ds; unused by strace */
1035 /*i386_regs.xes = x86_64_regs.es; ditto... */
1036 /*i386_regs.xfs = x86_64_regs.fs;*/
1037 /*i386_regs.xgs = x86_64_regs.gs;*/
1038 i386_regs.orig_eax = x86_64_regs.orig_rax;
1039 i386_regs.eip = x86_64_regs.rip;
1040 /*i386_regs.xcs = x86_64_regs.cs;*/
1041 /*i386_regs.eflags = x86_64_regs.eflags;*/
1042 i386_regs.esp = x86_64_regs.rsp;
1043 /*i386_regs.xss = x86_64_regs.ss;*/
1044 }
1045 }
Denys Vlasenko401374e2013-02-06 18:24:39 +01001046# elif defined(ARM)
1047 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (void *)&arm_regs);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001048# elif defined(AARCH64)
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001049 /*aarch64_io.iov_base = &arm_regs_union; - already is */
1050 aarch64_io.iov_len = sizeof(arm_regs_union);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001051 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (void *)&aarch64_io);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001052# if 0
1053 /* Paranoia checks */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001054 if (get_regs_error)
1055 return;
1056 switch (aarch64_io.iov_len) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001057 case sizeof(aarch64_regs):
1058 /* We are in 64-bit mode */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001059 break;
1060 case sizeof(arm_regs):
1061 /* We are in 32-bit mode */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001062 break;
1063 default:
1064 get_regs_error = -1;
1065 break;
1066 }
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001067# endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001068# elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001069 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001070# elif defined(TILE)
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -05001071 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &tile_regs);
Christian Svensson492f81f2013-02-14 13:26:27 +01001072# elif defined(OR1K)
1073 or1k_io.iov_len = sizeof(or1k_regs);
1074 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &or1k_io);
James Hogan5f999a82013-02-22 14:44:10 +00001075# elif defined(METAG)
1076 metag_io.iov_len = sizeof(metag_regs);
1077 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &metag_io);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001078# endif
1079}
1080#endif
1081
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001082/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001083 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1084 * 1: ok, continue in trace_syscall_entering().
1085 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001086 * ("????" etc) and bail out.
1087 */
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01001088static int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001089get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001090{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001091 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001092
Denys Vlasenko523635f2012-02-25 02:44:25 +01001093#if defined(S390) || defined(S390X)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001094 if (upeek(tcp, PT_GPR2, &syscall_mode) < 0)
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001095 return -1;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001096
1097 if (syscall_mode != -ENOSYS) {
1098 /*
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001099 * Since kernel version 2.5.44 the scno gets passed in gpr2.
Roland McGrath2f924ca2003-06-26 22:23:28 +00001100 */
1101 scno = syscall_mode;
1102 } else {
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001103 /*
Michal Ludvig882eda82002-11-11 12:50:47 +00001104 * Old style of "passing" the scno via the SVC instruction.
1105 */
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001106 long psw;
Michal Ludvig882eda82002-11-11 12:50:47 +00001107 long opcode, offset_reg, tmp;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001108 void *svc_addr;
Denys Vlasenko7c9ba8b2011-08-19 19:46:32 +02001109 static const int gpr_offset[16] = {
1110 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
1111 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
1112 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
1113 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
1114 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001115
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001116 if (upeek(tcp, PT_PSWADDR, &psw) < 0)
Michal Ludvig882eda82002-11-11 12:50:47 +00001117 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001118 errno = 0;
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001119 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +00001120 if (errno) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001121 perror_msg("peektext(psw-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +00001122 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001123 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001124
1125 /*
1126 * We have to check if the SVC got executed directly or via an
1127 * EXECUTE instruction. In case of EXECUTE it is necessary to do
1128 * instruction decoding to derive the system call number.
1129 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1130 * so that this doesn't work if a SVC opcode is part of an EXECUTE
1131 * opcode. Since there is no way to find out the opcode size this
1132 * is the best we can do...
1133 */
Michal Ludvig882eda82002-11-11 12:50:47 +00001134 if ((opcode & 0xff00) == 0x0a00) {
1135 /* SVC opcode */
1136 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +00001137 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001138 else {
1139 /* SVC got executed by EXECUTE instruction */
1140
1141 /*
1142 * Do instruction decoding of EXECUTE. If you really want to
1143 * understand this, read the Principles of Operations.
1144 */
1145 svc_addr = (void *) (opcode & 0xfff);
1146
1147 tmp = 0;
1148 offset_reg = (opcode & 0x000f0000) >> 16;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001149 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001150 return -1;
1151 svc_addr += tmp;
1152
1153 tmp = 0;
1154 offset_reg = (opcode & 0x0000f000) >> 12;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001155 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001156 return -1;
1157 svc_addr += tmp;
1158
Denys Vlasenkofb036672009-01-23 16:30:26 +00001159 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
Michal Ludvig882eda82002-11-11 12:50:47 +00001160 if (errno)
1161 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001162# if defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +00001163 scno >>= 48;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001164# else
Michal Ludvig882eda82002-11-11 12:50:47 +00001165 scno >>= 16;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001166# endif
Michal Ludvig882eda82002-11-11 12:50:47 +00001167 tmp = 0;
1168 offset_reg = (opcode & 0x00f00000) >> 20;
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
1172 scno = (scno | tmp) & 0xff;
1173 }
1174 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001175#elif defined(POWERPC)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001176 if (upeek(tcp, sizeof(unsigned long)*PT_R0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001177 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001178# ifdef POWERPC64
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001179 /* TODO: speed up strace by not doing this at every syscall.
1180 * We only need to do it after execve.
1181 */
1182 int currpers;
1183 long val;
Andreas Schwabd69fa492010-07-12 21:39:57 +02001184
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001185 /* Check for 64/32 bit mode. */
1186 if (upeek(tcp, sizeof(unsigned long)*PT_MSR, &val) < 0)
1187 return -1;
1188 /* SF is bit 0 of MSR */
1189 if (val < 0)
1190 currpers = 0;
1191 else
1192 currpers = 1;
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001193 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001194# endif
1195#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001196 scno = avr32_regs.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001197#elif defined(BFIN)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001198 if (upeek(tcp, PT_ORIG_P0, &scno))
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001199 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001200#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001201 scno = i386_regs.orig_eax;
H.J. Lu35be5812012-04-16 13:00:01 +02001202#elif defined(X86_64) || defined(X32)
1203# ifndef __X32_SYSCALL_BIT
1204# define __X32_SYSCALL_BIT 0x40000000
1205# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001206 int currpers;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001207# if 1
1208 /* GETREGSET of NT_PRSTATUS tells us regset size,
1209 * which unambiguously detects i386.
1210 *
1211 * Linux kernel distinguishes x86-64 and x32 processes
1212 * solely by looking at __X32_SYSCALL_BIT:
1213 * arch/x86/include/asm/compat.h::is_x32_task():
1214 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1215 * return true;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001216 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001217 if (x86_io.iov_len == sizeof(i386_regs)) {
1218 scno = i386_regs.orig_eax;
1219 currpers = 1;
1220 } else {
1221 scno = x86_64_regs.orig_rax;
1222 currpers = 0;
1223 if (scno & __X32_SYSCALL_BIT) {
1224 scno -= __X32_SYSCALL_BIT;
1225 currpers = 2;
1226 }
1227 }
1228# elif 0
1229 /* cs = 0x33 for long mode (native 64 bit and x32)
1230 * cs = 0x23 for compatibility mode (32 bit)
1231 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1232 */
1233 scno = x86_64_regs.orig_rax;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001234 switch (x86_64_regs.cs) {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001235 case 0x23: currpers = 1; break;
H.J. Lu35be5812012-04-16 13:00:01 +02001236 case 0x33:
1237 if (x86_64_regs.ds == 0x2b) {
1238 currpers = 2;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001239 scno &= ~__X32_SYSCALL_BIT;
H.J. Lu35be5812012-04-16 13:00:01 +02001240 } else
1241 currpers = 0;
1242 break;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001243 default:
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001244 fprintf(stderr, "Unknown value CS=0x%08X while "
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001245 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001246 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001247 currpers = current_personality;
1248 break;
1249 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001250# elif 0
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001251 /* This version analyzes the opcode of a syscall instruction.
1252 * (int 0x80 on i386 vs. syscall on x86-64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001253 * It works, but is too complicated, and strictly speaking, unreliable.
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001254 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001255 unsigned long call, rip = x86_64_regs.rip;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001256 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1257 rip -= 2;
1258 errno = 0;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001259 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001260 if (errno)
1261 fprintf(stderr, "ptrace_peektext failed: %s\n",
1262 strerror(errno));
1263 switch (call & 0xffff) {
1264 /* x86-64: syscall = 0x0f 0x05 */
1265 case 0x050f: currpers = 0; break;
1266 /* i386: int 0x80 = 0xcd 0x80 */
1267 case 0x80cd: currpers = 1; break;
1268 default:
1269 currpers = current_personality;
1270 fprintf(stderr,
1271 "Unknown syscall opcode (0x%04X) while "
1272 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001273 "PID=%d\n", (int)call, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001274 break;
1275 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001276# endif
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001277
H.J. Lu35be5812012-04-16 13:00:01 +02001278# ifdef X32
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001279 /* If we are built for a x32 system, then personality 0 is x32
1280 * (not x86_64), and stracing of x86_64 apps is not supported.
1281 * Stracing of i386 apps is still supported.
H.J. Lu085e4282012-04-17 11:05:04 -07001282 */
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001283 if (currpers == 0) {
1284 fprintf(stderr, "syscall_%lu(...) in unsupported "
1285 "64-bit mode of process PID=%d\n",
1286 scno, tcp->pid);
1287 return 0;
H.J. Lu35be5812012-04-16 13:00:01 +02001288 }
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001289 currpers &= ~2; /* map 2,1 to 0,1 */
H.J. Lu35be5812012-04-16 13:00:01 +02001290# endif
H.J. Lu085e4282012-04-17 11:05:04 -07001291 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001292#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001293# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01001294 long psr;
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001295 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001296 ia32 = (psr & IA64_PSR_IS) != 0;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001297 if (ia32) {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001298 if (upeek(tcp, PT_R1, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001299 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001300 } else {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001301 if (upeek(tcp, PT_R15, &scno) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001302 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001303 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001304#elif defined(AARCH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001305 switch (aarch64_io.iov_len) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001306 case sizeof(aarch64_regs):
1307 /* We are in 64-bit mode */
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001308 scno = aarch64_regs.regs[8];
1309 update_personality(tcp, 1);
1310 break;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001311 case sizeof(arm_regs):
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001312 /* We are in 32-bit mode */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001313 scno = arm_regs.ARM_r7;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001314 update_personality(tcp, 0);
1315 break;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001316 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001317#elif defined(ARM)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001318 if (arm_regs.ARM_ip != 0) {
1319 /* It is not a syscall entry */
1320 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001321 tcp->flags |= TCB_INSYSCALL;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001322 return 0;
1323 }
1324 /* Note: we support only 32-bit CPUs, not 26-bit */
1325
1326 if (arm_regs.ARM_cpsr & 0x20) {
1327 /* Thumb mode */
1328 scno = arm_regs.ARM_r7;
1329 } else {
1330 /* ARM mode */
1331 errno = 0;
1332 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1333 if (errno)
1334 return -1;
1335
1336 /* EABI syscall convention? */
1337 if (scno == 0xef000000) {
1338 scno = arm_regs.ARM_r7; /* yes */
1339 } else {
1340 if ((scno & 0x0ff00000) != 0x0f900000) {
1341 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1342 tcp->pid, scno);
1343 return -1;
1344 }
1345 /* Fixup the syscall number */
1346 scno &= 0x000fffff;
1347 }
1348 }
Denys Vlasenko7270de52013-02-21 15:46:34 +01001349
1350 scno = shuffle_scno(scno);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001351#elif defined(M68K)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001352 if (upeek(tcp, 4*PT_ORIG_D0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001353 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001354#elif defined(LINUX_MIPSN32)
Roland McGrath542c2c62008-05-20 01:11:56 +00001355 unsigned long long regs[38];
1356
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001357 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001358 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001359 mips_a3 = regs[REG_A3];
1360 mips_r2 = regs[REG_V0];
Roland McGrath542c2c62008-05-20 01:11:56 +00001361
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001362 scno = mips_r2;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001363 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001364 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001365 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001366 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001367 return 0;
1368 }
Roland McGrath542c2c62008-05-20 01:11:56 +00001369 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001370#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001371 if (upeek(tcp, REG_A3, &mips_a3) < 0)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001372 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001373 if (upeek(tcp, REG_V0, &scno) < 0)
1374 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001375
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001376 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001377 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001378 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001379 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001380 return 0;
1381 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001382 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001383#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001384 if (upeek(tcp, REG_A3, &alpha_a3) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001385 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001386 if (upeek(tcp, REG_R0, &scno) < 0)
1387 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001388
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001389 /*
1390 * Do some sanity checks to figure out if it's
1391 * really a syscall entry
1392 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001393 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001394 if (alpha_a3 == 0 || alpha_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001395 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001396 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001397 return 0;
1398 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001399 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001400#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001401 /* Disassemble the syscall trap. */
1402 /* Retrieve the syscall trap instruction. */
Denys Vlasenko46455822013-02-05 17:02:59 +01001403 unsigned long trap;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001404 errno = 0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001405# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001406 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001407 trap >>= 32;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001408# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001409 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001410# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001411 if (errno)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001412 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001413
1414 /* Disassemble the trap to see what personality to use. */
1415 switch (trap) {
1416 case 0x91d02010:
1417 /* Linux/SPARC syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001418 update_personality(tcp, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001419 break;
1420 case 0x91d0206d:
1421 /* Linux/SPARC64 syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001422 update_personality(tcp, 2);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001423 break;
1424 case 0x91d02000:
1425 /* SunOS syscall trap. (pers 1) */
1426 fprintf(stderr, "syscall: SunOS no support\n");
1427 return -1;
1428 case 0x91d02008:
1429 /* Solaris 2.x syscall trap. (per 2) */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001430 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001431 break;
1432 case 0x91d02009:
1433 /* NetBSD/FreeBSD syscall trap. */
1434 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1435 return -1;
1436 case 0x91d02027:
1437 /* Solaris 2.x gettimeofday */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001438 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001439 break;
1440 default:
Denys Vlasenko523635f2012-02-25 02:44:25 +01001441# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001442 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001443# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001444 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001445# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001446 return -1;
1447 }
1448
1449 /* Extract the system call number from the registers. */
1450 if (trap == 0x91d02027)
1451 scno = 156;
1452 else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001453 scno = sparc_regs.u_regs[U_REG_G1];
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001454 if (scno == 0) {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001455 scno = sparc_regs.u_regs[U_REG_O0];
1456 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 +02001457 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001458#elif defined(HPPA)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001459 if (upeek(tcp, PT_GR20, &scno) < 0)
1460 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001461#elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001462 /*
1463 * In the new syscall ABI, the system call number is in R3.
1464 */
1465 if (upeek(tcp, 4*(REG_REG0+3), &scno) < 0)
1466 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001467
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001468 if (scno < 0) {
1469 /* Odd as it may seem, a glibc bug has been known to cause
1470 glibc to issue bogus negative syscall numbers. So for
1471 our purposes, make strace print what it *should* have been */
1472 long correct_scno = (scno & 0xff);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001473 if (debug_flag)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001474 fprintf(stderr,
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001475 "Detected glibc bug: bogus system call"
1476 " number = %ld, correcting to %ld\n",
1477 scno,
1478 correct_scno);
1479 scno = correct_scno;
1480 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001481#elif defined(SH64)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001482 if (upeek(tcp, REG_SYSCALL, &scno) < 0)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001483 return -1;
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001484 scno &= 0xFFFF;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001485#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001486 if (upeek(tcp, 4*PT_R9, &scno) < 0)
1487 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001488#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001489 int currpers;
1490 scno = tile_regs.regs[10];
1491# ifdef __tilepro__
1492 currpers = 1;
1493# else
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001494# ifndef PT_FLAGS_COMPAT
1495# define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1496# endif
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001497 if (tile_regs.flags & PT_FLAGS_COMPAT)
1498 currpers = 1;
1499 else
1500 currpers = 0;
1501# endif
1502 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001503#elif defined(MICROBLAZE)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001504 if (upeek(tcp, 0, &scno) < 0)
1505 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001506#elif defined(OR1K)
1507 scno = or1k_regs.gpr[11];
James Hogan5f999a82013-02-22 14:44:10 +00001508#elif defined(METAG)
1509 scno = metag_regs.dx[0][1]; /* syscall number in D1Re0 (D1.0) */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001510#endif
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001511
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001512 tcp->scno = scno;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001513 if (SCNO_IS_VALID(tcp->scno)) {
1514 tcp->s_ent = &sysent[scno];
1515 tcp->qual_flg = qual_flags[scno];
1516 } else {
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01001517 static const struct_sysent unknown = {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001518 .nargs = MAX_ARGS,
1519 .sys_flags = 0,
1520 .sys_func = printargs,
1521 .sys_name = "unknown", /* not used */
1522 };
1523 tcp->s_ent = &unknown;
1524 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1525 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001526 return 1;
1527}
1528
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02001529/* Called at each syscall entry.
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001530 * Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001531 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1532 * 1: ok, continue in trace_syscall_entering().
1533 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001534 * ("????" etc) and bail out.
1535 */
Roland McGratha4d48532005-06-08 20:45:28 +00001536static int
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001537syscall_fixup_on_sysenter(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001538{
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001539 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001540#if defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001541 if (i386_regs.eax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001542 if (debug_flag)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001543 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1544 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001545 }
H.J. Lu35be5812012-04-16 13:00:01 +02001546#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001547 {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001548 long rax;
1549 if (x86_io.iov_len == sizeof(i386_regs)) {
1550 /* Sign extend from 32 bits */
1551 rax = (int32_t)i386_regs.eax;
1552 } else {
1553 /* Note: in X32 build, this truncates 64 to 32 bits */
1554 rax = x86_64_regs.rax;
1555 }
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001556 if (rax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001557 if (debug_flag)
Denys Vlasenko18beb982011-08-24 16:59:23 +02001558 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1559 return 0;
1560 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001561 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001562#elif defined(S390) || defined(S390X)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001563 /* TODO: we already fetched PT_GPR2 in get_scno
1564 * and stored it in syscall_mode, reuse it here
1565 * instead of re-fetching?
1566 */
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001567 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001568 return -1;
Michal Ludvig882eda82002-11-11 12:50:47 +00001569 if (syscall_mode != -ENOSYS)
1570 syscall_mode = tcp->scno;
Denys Vlasenkoece98792011-08-25 10:25:35 +02001571 if (gpr2 != syscall_mode) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001572 if (debug_flag)
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001573 fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2);
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001574 return 0;
1575 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001576#elif defined(M68K)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001577 /* TODO? Eliminate upeek's in arches below like we did in x86 */
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001578 if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001579 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001580 if (m68k_d0 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001581 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001582 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001583 return 0;
1584 }
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001585#elif defined(IA64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001586 if (upeek(tcp, PT_R10, &ia64_r10) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001587 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001588 if (upeek(tcp, PT_R8, &ia64_r8) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001589 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001590 if (ia32 && ia64_r8 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001591 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001592 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001593 return 0;
1594 }
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001595#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001596 if (upeek(tcp, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001597 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001598 if (cris_r10 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001599 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001600 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001601 return 0;
1602 }
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001603#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001604 if (upeek(tcp, 3 * 4, &microblaze_r3) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001605 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001606 if (microblaze_r3 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001607 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001608 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001609 return 0;
1610 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001611#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001612 return 1;
1613}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001614
Denys Vlasenko146b9442012-03-18 22:10:48 +01001615static void
1616internal_fork(struct tcb *tcp)
1617{
1618#if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1619# define ARG_FLAGS 1
1620#else
1621# define ARG_FLAGS 0
1622#endif
1623#ifndef CLONE_UNTRACED
1624# define CLONE_UNTRACED 0x00800000
1625#endif
1626 if ((ptrace_setoptions
1627 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1628 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1629 return;
1630
1631 if (!followfork)
1632 return;
1633
1634 if (entering(tcp)) {
1635 /*
1636 * We won't see the new child if clone is called with
1637 * CLONE_UNTRACED, so we keep the same logic with that option
1638 * and don't trace it.
1639 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001640 if ((tcp->s_ent->sys_func == sys_clone)
1641 && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
1642 )
Denys Vlasenko146b9442012-03-18 22:10:48 +01001643 return;
1644 setbpt(tcp);
1645 } else {
1646 if (tcp->flags & TCB_BPTSET)
1647 clearbpt(tcp);
1648 }
1649}
1650
1651#if defined(TCB_WAITEXECVE)
1652static void
1653internal_exec(struct tcb *tcp)
1654{
1655 /* Maybe we have post-execve SIGTRAP suppressed? */
1656 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1657 return; /* yes, no need to do anything */
1658
1659 if (exiting(tcp) && syserror(tcp))
1660 /* Error in execve, no post-execve SIGTRAP expected */
1661 tcp->flags &= ~TCB_WAITEXECVE;
1662 else
1663 tcp->flags |= TCB_WAITEXECVE;
1664}
1665#endif
1666
1667static void
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001668syscall_fixup_for_fork_exec(struct tcb *tcp)
Roland McGrathc1e45922008-05-27 23:18:29 +00001669{
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001670 /*
1671 * We must always trace a few critical system calls in order to
1672 * correctly support following forks in the presence of tracing
1673 * qualifiers.
1674 */
1675 int (*func)();
1676
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001677 func = tcp->s_ent->sys_func;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001678
1679 if ( sys_fork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001680 || sys_vfork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001681 || sys_clone == func
Denys Vlasenko146b9442012-03-18 22:10:48 +01001682 ) {
1683 internal_fork(tcp);
1684 return;
1685 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001686
Denys Vlasenko84703742012-02-25 02:38:52 +01001687#if defined(TCB_WAITEXECVE)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001688 if ( sys_execve == func
Denys Vlasenko84703742012-02-25 02:38:52 +01001689# if defined(SPARC) || defined(SPARC64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001690 || sys_execv == func
Denys Vlasenkoa7949742011-08-21 17:26:55 +02001691# endif
Denys Vlasenko146b9442012-03-18 22:10:48 +01001692 ) {
1693 internal_exec(tcp);
1694 return;
1695 }
Roland McGrathc1e45922008-05-27 23:18:29 +00001696#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001697}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001698
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001699/* Return -1 on error or 1 on success (never 0!) */
Roland McGratha4d48532005-06-08 20:45:28 +00001700static int
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001701get_syscall_args(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001702{
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001703 int i, nargs;
1704
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001705 nargs = tcp->s_ent->nargs;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001706
Denys Vlasenko523635f2012-02-25 02:44:25 +01001707#if defined(S390) || defined(S390X)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001708 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001709 if (upeek(tcp, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
1710 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001711#elif defined(ALPHA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001712 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001713 if (upeek(tcp, REG_A0+i, &tcp->u_arg[i]) < 0)
1714 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001715#elif defined(IA64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001716 if (!ia32) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001717 unsigned long *out0, cfm, sof, sol;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001718 long rbs_end;
1719 /* be backwards compatible with kernel < 2.4.4... */
1720# ifndef PT_RBS_END
1721# define PT_RBS_END PT_AR_BSP
1722# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001723
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001724 if (upeek(tcp, PT_RBS_END, &rbs_end) < 0)
1725 return -1;
1726 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001727 return -1;
1728
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001729 sof = (cfm >> 0) & 0x7f;
1730 sol = (cfm >> 7) & 0x7f;
1731 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1732
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001733 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001734 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1735 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1736 return -1;
1737 }
1738 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001739 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1740 PT_R9 /* ECX = out1 */,
1741 PT_R10 /* EDX = out2 */,
1742 PT_R14 /* ESI = out3 */,
1743 PT_R15 /* EDI = out4 */,
1744 PT_R13 /* EBP = out5 */};
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001745
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001746 for (i = 0; i < nargs; ++i) {
1747 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1748 return -1;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001749 /* truncate away IVE sign-extension */
1750 tcp->u_arg[i] &= 0xffffffff;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001751 }
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001752 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001753#elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001754 /* N32 and N64 both use up to six registers. */
1755 unsigned long long regs[38];
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001756
1757 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1758 return -1;
1759
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001760 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001761 tcp->u_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001762# if defined(LINUX_MIPSN32)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001763 tcp->ext_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001764# endif
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001765 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001766#elif defined(MIPS)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001767 if (nargs > 4) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001768 long sp;
1769
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001770 if (upeek(tcp, REG_SP, &sp) < 0)
1771 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001772 for (i = 0; i < 4; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001773 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1774 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001775 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001776 (char *)(tcp->u_arg + 4));
1777 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001778 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001779 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001780 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001781 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001782#elif defined(POWERPC)
1783# ifndef PT_ORIG_R3
1784# define PT_ORIG_R3 34
1785# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001786 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001787 if (upeek(tcp, (i==0) ?
1788 (sizeof(unsigned long) * PT_ORIG_R3) :
1789 ((i+PT_R3) * sizeof(unsigned long)),
1790 &tcp->u_arg[i]) < 0)
1791 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001792 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001793#elif defined(SPARC) || defined(SPARC64)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001794 for (i = 0; i < nargs; ++i)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001795 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001796#elif defined(HPPA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001797 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001798 if (upeek(tcp, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1799 return -1;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001800#elif defined(ARM) || defined(AARCH64)
1801# if defined(AARCH64)
1802 if (tcp->currpers == 1)
1803 for (i = 0; i < nargs; ++i)
1804 tcp->u_arg[i] = aarch64_regs.regs[i];
1805 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001806# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001807 for (i = 0; i < nargs; ++i)
Denys Vlasenko401374e2013-02-06 18:24:39 +01001808 tcp->u_arg[i] = arm_regs.uregs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001809#elif defined(AVR32)
Denys Vlasenkob5b25892011-08-30 19:04:54 +02001810 (void)i;
1811 (void)nargs;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001812 tcp->u_arg[0] = avr32_regs.r12;
1813 tcp->u_arg[1] = avr32_regs.r11;
1814 tcp->u_arg[2] = avr32_regs.r10;
1815 tcp->u_arg[3] = avr32_regs.r9;
1816 tcp->u_arg[4] = avr32_regs.r5;
1817 tcp->u_arg[5] = avr32_regs.r3;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001818#elif defined(BFIN)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001819 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 +02001820
Denys Vlasenko4b887a52011-08-23 13:32:38 +02001821 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001822 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1823 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001824#elif defined(SH)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001825 static const int syscall_regs[MAX_ARGS] = {
1826 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1827 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001828 };
1829
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001830 for (i = 0; i < nargs; ++i)
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001831 if (upeek(tcp, syscall_regs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001832 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001833#elif defined(SH64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001834 int i;
1835 /* Registers used by SH5 Linux system calls for parameters */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001836 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001837
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001838 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001839 if (upeek(tcp, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1840 return -1;
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001841#elif defined(I386)
1842 (void)i;
1843 (void)nargs;
1844 tcp->u_arg[0] = i386_regs.ebx;
1845 tcp->u_arg[1] = i386_regs.ecx;
1846 tcp->u_arg[2] = i386_regs.edx;
1847 tcp->u_arg[3] = i386_regs.esi;
1848 tcp->u_arg[4] = i386_regs.edi;
1849 tcp->u_arg[5] = i386_regs.ebp;
H.J. Lu35be5812012-04-16 13:00:01 +02001850#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001851 (void)i;
1852 (void)nargs;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001853 if (x86_io.iov_len != sizeof(i386_regs)) {
1854 /* x86-64 or x32 ABI */
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001855 tcp->u_arg[0] = x86_64_regs.rdi;
1856 tcp->u_arg[1] = x86_64_regs.rsi;
1857 tcp->u_arg[2] = x86_64_regs.rdx;
1858 tcp->u_arg[3] = x86_64_regs.r10;
1859 tcp->u_arg[4] = x86_64_regs.r8;
1860 tcp->u_arg[5] = x86_64_regs.r9;
H.J. Lu35be5812012-04-16 13:00:01 +02001861# ifdef X32
1862 tcp->ext_arg[0] = x86_64_regs.rdi;
1863 tcp->ext_arg[1] = x86_64_regs.rsi;
1864 tcp->ext_arg[2] = x86_64_regs.rdx;
1865 tcp->ext_arg[3] = x86_64_regs.r10;
1866 tcp->ext_arg[4] = x86_64_regs.r8;
1867 tcp->ext_arg[5] = x86_64_regs.r9;
1868# endif
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001869 } else {
1870 /* i386 ABI */
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001871 /* Zero-extend from 32 bits */
1872 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1873 * if you need to use *sign-extended* parameter.
1874 */
1875 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1876 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1877 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1878 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1879 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1880 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001881 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001882#elif defined(MICROBLAZE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001883 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001884 if (upeek(tcp, (5 + i) * 4, &tcp->u_arg[i]) < 0)
1885 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001886#elif defined(CRISV10) || defined(CRISV32)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001887 static const int crisregs[MAX_ARGS] = {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001888 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001889 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001890 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001891
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001892 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001893 if (upeek(tcp, crisregs[i], &tcp->u_arg[i]) < 0)
1894 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001895#elif defined(TILE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001896 for (i = 0; i < nargs; ++i)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001897 tcp->u_arg[i] = tile_regs.regs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001898#elif defined(M68K)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001899 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001900 if (upeek(tcp, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
1901 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001902#elif defined(OR1K)
1903 (void)nargs;
1904 for (i = 0; i < 6; ++i)
1905 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
James Hogan5f999a82013-02-22 14:44:10 +00001906#elif defined(METAG)
1907 for (i = 0; i < nargs; i++)
1908 /* arguments go backwards from D1Ar1 (D1.3) */
1909 tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001910#else /* Other architecture (32bits specific) */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001911 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001912 if (upeek(tcp, i*4, &tcp->u_arg[i]) < 0)
1913 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001914#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001915 return 1;
1916}
1917
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00001918static int
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001919trace_syscall_entering(struct tcb *tcp)
1920{
1921 int res, scno_good;
1922
Denys Vlasenko2ce12ed2011-08-24 17:25:32 +02001923#if defined TCB_WAITEXECVE
1924 if (tcp->flags & TCB_WAITEXECVE) {
1925 /* This is the post-execve SIGTRAP. */
1926 tcp->flags &= ~TCB_WAITEXECVE;
1927 return 0;
1928 }
1929#endif
1930
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001931 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001932 if (res == 0)
1933 return res;
Denys Vlasenko907735a2012-03-21 00:23:16 +01001934 if (res == 1) {
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001935 res = syscall_fixup_on_sysenter(tcp);
Denys Vlasenko907735a2012-03-21 00:23:16 +01001936 if (res == 0)
1937 return res;
1938 if (res == 1)
1939 res = get_syscall_args(tcp);
1940 }
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001941
1942 if (res != 1) {
1943 printleader(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001944 if (scno_good != 1)
Denys Vlasenkob7a6dae2012-03-20 16:48:35 +01001945 tprints("????" /* anti-trigraph gap */ "(");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001946 else if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01001947 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001948 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001949 tprintf("%s(", tcp->s_ent->sys_name);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001950 /*
1951 * " <unavailable>" will be added later by the code which
1952 * detects ptrace errors.
1953 */
1954 goto ret;
1955 }
1956
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001957#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001958 while (1) {
Denys Vlasenko523635f2012-02-25 02:44:25 +01001959# ifdef SYS_socket_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001960 if (tcp->s_ent->sys_func == sys_socketcall) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00001961 decode_socket_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001962 break;
1963 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001964# endif
1965# ifdef SYS_ipc_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001966 if (tcp->s_ent->sys_func == sys_ipc) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00001967 decode_ipc_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001968 break;
1969 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001970# endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001971 break;
1972 }
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001973#endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001974
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001975 if (need_fork_exec_workarounds)
1976 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001977
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001978 if (!(tcp->qual_flg & QUAL_TRACE)
1979 || (tracing_paths && !pathtrace_match(tcp))
1980 ) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001981 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
1982 return 0;
1983 }
1984
1985 tcp->flags &= ~TCB_FILTERED;
1986
1987 if (cflag == CFLAG_ONLY_STATS) {
1988 res = 0;
1989 goto ret;
1990 }
1991
1992 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001993 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01001994 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001995 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001996 tprintf("%s(", tcp->s_ent->sys_name);
1997 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001998 res = printargs(tcp);
1999 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002000 res = tcp->s_ent->sys_func(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002001
Dmitry V. Levinb742d8c2012-09-17 22:40:12 +00002002 fflush(tcp->outf);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002003 ret:
2004 tcp->flags |= TCB_INSYSCALL;
2005 /* Measure the entrance time as late as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002006 if (Tflag || cflag)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002007 gettimeofday(&tcp->etime, NULL);
2008 return res;
2009}
2010
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002011/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01002012 * 1: ok, continue in trace_syscall_exiting().
2013 * -1: error, trace_syscall_exiting() should print error indicator
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002014 * ("????" etc) and bail out.
2015 */
2016static int
2017get_syscall_result(struct tcb *tcp)
2018{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002019#if defined(S390) || defined(S390X)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002020 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
2021 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002022#elif defined(POWERPC)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002023# define SO_MASK 0x10000000
2024 {
2025 long flags;
2026 if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0)
2027 return -1;
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002028 if (upeek(tcp, sizeof(unsigned long)*PT_R3, &ppc_result) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002029 return -1;
2030 if (flags & SO_MASK)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002031 ppc_result = -ppc_result;
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002032 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002033#elif defined(AVR32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002034 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002035#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002036 if (upeek(tcp, PT_R0, &bfin_r0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002037 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002038#elif defined(I386)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002039 /* already done by get_regs */
H.J. Lu35be5812012-04-16 13:00:01 +02002040#elif defined(X86_64) || defined(X32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002041 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002042#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002043# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01002044 long psr;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002045 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
2046 ia32 = (psr & IA64_PSR_IS) != 0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002047 if (upeek(tcp, PT_R8, &ia64_r8) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002048 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002049 if (upeek(tcp, PT_R10, &ia64_r10) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002050 return -1;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002051#elif defined(ARM)
2052 /* already done by get_regs */
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002053#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002054 /* register reading already done by get_regs */
2055
2056 /* Used to do this, but we did it on syscall entry already: */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002057 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
2058 * else it's personality 0.
2059 */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002060 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
Denys Vlasenko523635f2012-02-25 02:44:25 +01002061#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002062 if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002063 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002064#elif defined(LINUX_MIPSN32)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002065 unsigned long long regs[38];
2066
2067 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
2068 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002069 mips_a3 = regs[REG_A3];
2070 mips_r2 = regs[REG_V0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01002071#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002072 if (upeek(tcp, REG_A3, &mips_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002073 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002074 if (upeek(tcp, REG_V0, &mips_r2) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002075 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002076#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002077 if (upeek(tcp, REG_A3, &alpha_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002078 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002079 if (upeek(tcp, REG_R0, &alpha_r0) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002080 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002081#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002082 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002083#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002084 if (upeek(tcp, PT_GR28, &hppa_r28) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002085 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002086#elif defined(SH)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002087 /* new syscall ABI returns result in R0 */
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002088 if (upeek(tcp, 4*REG_REG0, (long *)&sh_r0) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002089 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002090#elif defined(SH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002091 /* ABI defines result returned in r9 */
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002092 if (upeek(tcp, REG_GENERAL(9), (long *)&sh64_r9) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002093 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002094#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002095 if (upeek(tcp, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002096 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002097#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002098 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002099#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002100 if (upeek(tcp, 3 * 4, &microblaze_r3) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002101 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01002102#elif defined(OR1K)
2103 /* already done by get_regs */
James Hogan5f999a82013-02-22 14:44:10 +00002104#elif defined(METAG)
2105 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002106#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002107 return 1;
2108}
2109
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002110/* Called at each syscall exit */
2111static void
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002112syscall_fixup_on_sysexit(struct tcb *tcp)
2113{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002114#if defined(S390) || defined(S390X)
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002115 if (syscall_mode != -ENOSYS)
2116 syscall_mode = tcp->scno;
Denys Vlasenkoece98792011-08-25 10:25:35 +02002117 if ((tcp->flags & TCB_WAITEXECVE)
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002118 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
2119 /*
2120 * Return from execve.
2121 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2122 * flag set for the post-execve SIGTRAP to see and reset.
2123 */
2124 gpr2 = 0;
2125 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002126#endif
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002127}
2128
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002129/*
2130 * Check the syscall return value register value for whether it is
2131 * a negated errno code indicating an error, or a success return value.
2132 */
2133static inline int
2134is_negated_errno(unsigned long int val)
2135{
2136 unsigned long int max = -(long int) nerrnos;
Denys Vlasenko2544f982013-02-19 17:39:56 +01002137#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01002138 if (current_wordsize < sizeof(val)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002139 val = (unsigned int) val;
2140 max = (unsigned int) max;
2141 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002142#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002143 return val > max;
2144}
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002145
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002146#if defined(X32)
2147static inline int
2148is_negated_errno_x32(unsigned long long val)
2149{
2150 unsigned long long max = -(long long) nerrnos;
2151 /*
2152 * current_wordsize is 4 even in personality 0 (native X32)
2153 * but truncation _must not_ be done in it.
2154 * can't check current_wordsize here!
2155 */
2156 if (current_personality != 0) {
2157 val = (uint32_t) val;
2158 max = (uint32_t) max;
2159 }
2160 return val > max;
2161}
2162#endif
2163
Denys Vlasenko907735a2012-03-21 00:23:16 +01002164/* Returns:
2165 * 1: ok, continue in trace_syscall_exiting().
2166 * -1: error, trace_syscall_exiting() should print error indicator
2167 * ("????" etc) and bail out.
2168 */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002169static void
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002170get_error(struct tcb *tcp)
2171{
2172 int u_error = 0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002173 int check_errno = 1;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002174 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002175 check_errno = 0;
2176 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002177#if defined(S390) || defined(S390X)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002178 if (check_errno && is_negated_errno(gpr2)) {
2179 tcp->u_rval = -1;
2180 u_error = -gpr2;
2181 }
2182 else {
2183 tcp->u_rval = gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002184 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002185#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002186 if (check_errno && is_negated_errno(i386_regs.eax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002187 tcp->u_rval = -1;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002188 u_error = -i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002189 }
2190 else {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002191 tcp->u_rval = i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002192 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002193#elif defined(X86_64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002194 long rax;
2195 if (x86_io.iov_len == sizeof(i386_regs)) {
2196 /* Sign extend from 32 bits */
2197 rax = (int32_t)i386_regs.eax;
2198 } else {
2199 rax = x86_64_regs.rax;
2200 }
2201 if (check_errno && is_negated_errno(rax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002202 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002203 u_error = -rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002204 }
2205 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002206 tcp->u_rval = rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002207 }
2208#elif defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002209 /* In X32, return value is 64-bit (llseek uses one).
2210 * Using merely "long rax" would not work.
2211 */
2212 long long rax;
2213 if (x86_io.iov_len == sizeof(i386_regs)) {
2214 /* Sign extend from 32 bits */
2215 rax = (int32_t)i386_regs.eax;
2216 } else {
2217 rax = x86_64_regs.rax;
2218 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002219 /* Careful: is_negated_errno() works only on longs */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002220 if (check_errno && is_negated_errno_x32(rax)) {
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002221 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002222 u_error = -rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002223 }
2224 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002225 tcp->u_rval = rax; /* truncating */
2226 tcp->u_lrval = rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002227 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002228#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002229 if (ia32) {
2230 int err;
2231
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002232 err = (int)ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002233 if (check_errno && is_negated_errno(err)) {
2234 tcp->u_rval = -1;
2235 u_error = -err;
2236 }
2237 else {
2238 tcp->u_rval = err;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002239 }
2240 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002241 if (check_errno && ia64_r10) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002242 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002243 u_error = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002244 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002245 tcp->u_rval = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002246 }
2247 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002248#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002249 if (check_errno && mips_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002250 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002251 u_error = mips_r2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002252 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002253 tcp->u_rval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002254# if defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002255 tcp->u_lrval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002256# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002257 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002258#elif defined(POWERPC)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002259 if (check_errno && is_negated_errno(ppc_result)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002260 tcp->u_rval = -1;
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002261 u_error = -ppc_result;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002262 }
2263 else {
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002264 tcp->u_rval = ppc_result;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002265 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002266#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002267 if (check_errno && is_negated_errno(m68k_d0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002268 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002269 u_error = -m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002270 }
2271 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002272 tcp->u_rval = m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002273 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002274#elif defined(ARM) || defined(AARCH64)
2275# if defined(AARCH64)
2276 if (tcp->currpers == 1) {
2277 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2278 tcp->u_rval = -1;
2279 u_error = -aarch64_regs.regs[0];
2280 }
2281 else {
2282 tcp->u_rval = aarch64_regs.regs[0];
2283 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002284 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002285 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002286# endif
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002287 {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002288 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002289 tcp->u_rval = -1;
Denys Vlasenko401374e2013-02-06 18:24:39 +01002290 u_error = -arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002291 }
2292 else {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002293 tcp->u_rval = arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002294 }
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002295 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002296#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002297 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002298 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002299 u_error = -avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002300 }
2301 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002302 tcp->u_rval = avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002303 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002304#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002305 if (check_errno && is_negated_errno(bfin_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002306 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002307 u_error = -bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002308 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002309 tcp->u_rval = bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002310 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002311#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002312 if (check_errno && alpha_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002313 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002314 u_error = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002315 }
2316 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002317 tcp->u_rval = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002318 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002319#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002320 if (check_errno && sparc_regs.psr & PSR_C) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002321 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002322 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002323 }
2324 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002325 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002326 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002327#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002328 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002329 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002330 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002331 }
2332 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002333 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002334 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002335#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002336 if (check_errno && is_negated_errno(hppa_r28)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002337 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002338 u_error = -hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002339 }
2340 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002341 tcp->u_rval = hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002342 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002343#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002344 if (check_errno && is_negated_errno(sh_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002345 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002346 u_error = -sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002347 }
2348 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002349 tcp->u_rval = sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002350 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002351#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002352 if (check_errno && is_negated_errno(sh64_r9)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002353 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002354 u_error = -sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002355 }
2356 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002357 tcp->u_rval = sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002358 }
James Hogan5f999a82013-02-22 14:44:10 +00002359#elif defined(METAG)
2360 /* result pointer in D0Re0 (D0.0) */
2361 if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2362 tcp->u_rval = -1;
2363 u_error = -metag_regs.dx[0][0];
2364 }
2365 else {
2366 tcp->u_rval = metag_regs.dx[0][0];
2367 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002368#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002369 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002370 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002371 u_error = -cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002372 }
2373 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002374 tcp->u_rval = cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002375 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002376#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002377 /*
2378 * The standard tile calling convention returns the value (or negative
2379 * errno) in r0, and zero (or positive errno) in r1.
2380 * Until at least kernel 3.8, however, the r1 value is not reflected
2381 * in ptregs at this point, so we use r0 here.
2382 */
2383 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002384 tcp->u_rval = -1;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002385 u_error = -tile_regs.regs[0];
2386 } else {
2387 tcp->u_rval = tile_regs.regs[0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002388 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002389#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002390 if (check_errno && is_negated_errno(microblaze_r3)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002391 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002392 u_error = -microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002393 }
2394 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002395 tcp->u_rval = microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002396 }
Christian Svensson492f81f2013-02-14 13:26:27 +01002397#elif defined(OR1K)
2398 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2399 tcp->u_rval = -1;
2400 u_error = -or1k_regs.gpr[11];
2401 }
2402 else {
2403 tcp->u_rval = or1k_regs.gpr[11];
2404 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002405#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002406 tcp->u_error = u_error;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002407}
2408
2409static void
2410dumpio(struct tcb *tcp)
2411{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002412 int (*func)();
2413
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002414 if (syserror(tcp))
2415 return;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +01002416 if ((unsigned long) tcp->u_arg[0] >= num_quals)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002417 return;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002418 func = tcp->s_ent->sys_func;
2419 if (func == printargs)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002420 return;
2421 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002422 if (func == sys_read ||
2423 func == sys_pread ||
2424 func == sys_recv ||
2425 func == sys_recvfrom)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002426 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002427 else if (func == sys_readv)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002428 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2429 return;
2430 }
2431 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002432 if (func == sys_write ||
2433 func == sys_pwrite ||
2434 func == sys_send ||
2435 func == sys_sendto)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002436 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002437 else if (func == sys_writev)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002438 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2439 return;
2440 }
2441}
2442
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002443static int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002444trace_syscall_exiting(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002445{
2446 int sys_res;
2447 struct timeval tv;
Denys Vlasenko1a5b5a72011-08-25 00:29:56 +02002448 int res;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002449 long u_error;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002450
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002451 /* Measure the exit time as early as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002452 if (Tflag || cflag)
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002453 gettimeofday(&tv, NULL);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002454
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00002455#if SUPPORTED_PERSONALITIES > 1
2456 update_personality(tcp, tcp->currpers);
2457#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002458 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002459 if (res == 1) {
2460 syscall_fixup_on_sysexit(tcp); /* never fails */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002461 get_error(tcp); /* never fails */
2462 if (need_fork_exec_workarounds)
2463 syscall_fixup_for_fork_exec(tcp);
2464 if (filtered(tcp))
2465 goto ret;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002466 }
2467
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002468 if (cflag) {
2469 struct timeval t = tv;
Denys Vlasenkoc95a88f2011-08-21 17:47:40 +02002470 count_syscall(tcp, &t);
Denys Vlasenko7b609d52011-06-22 14:32:43 +02002471 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002472 goto ret;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002473 }
2474 }
2475
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002476 /* If not in -ff mode, and printing_tcp != tcp,
2477 * then the log currently does not end with output
2478 * of _our syscall entry_, but with something else.
2479 * We need to say which syscall's return is this.
2480 *
2481 * Forced reprinting via TCB_REPRINT is used only by
2482 * "strace -ff -oLOG test/threaded_execve" corner case.
2483 * It's the only case when -ff mode needs reprinting.
2484 */
2485 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2486 tcp->flags &= ~TCB_REPRINT;
2487 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002488 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002489 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002490 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002491 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002492 }
2493 printing_tcp = tcp;
2494
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002495 if (res != 1) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002496 /* There was error in one of prior ptrace ops */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002497 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002498 tabto();
Denys Vlasenko000b6012012-01-28 01:25:03 +01002499 tprints("= ? <unavailable>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002500 line_ended();
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002501 tcp->flags &= ~TCB_INSYSCALL;
2502 return res;
2503 }
2504
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002505 sys_res = 0;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002506 if (tcp->qual_flg & QUAL_RAW) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002507 /* sys_res = printargs(tcp); - but it's nop on sysexit */
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002508 } else {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002509 /* FIXME: not_failing_only (IOW, option -z) is broken:
2510 * failure of syscall is known only after syscall return.
2511 * Thus we end up with something like this on, say, ENOENT:
2512 * open("doesnt_exist", O_RDONLY <unfinished ...>
2513 * {next syscall decode}
2514 * whereas the intended result is that open(...) line
2515 * is not shown at all.
2516 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002517 if (not_failing_only && tcp->u_error)
Denys Vlasenko3b738812011-08-22 02:06:35 +02002518 goto ret; /* ignore failed syscalls */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002519 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002520 }
2521
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002522 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002523 tabto();
Denys Vlasenko3b738812011-08-22 02:06:35 +02002524 u_error = tcp->u_error;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002525 if (tcp->qual_flg & QUAL_RAW) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002526 if (u_error)
2527 tprintf("= -1 (errno %ld)", u_error);
2528 else
2529 tprintf("= %#lx", tcp->u_rval);
2530 }
2531 else if (!(sys_res & RVAL_NONE) && u_error) {
2532 switch (u_error) {
Denys Vlasenkofe585652012-01-12 11:26:34 +01002533 /* Blocked signals do not interrupt any syscalls.
2534 * In this case syscalls don't return ERESTARTfoo codes.
2535 *
2536 * Deadly signals set to SIG_DFL interrupt syscalls
2537 * and kill the process regardless of which of the codes below
2538 * is returned by the interrupted syscall.
2539 * In some cases, kernel forces a kernel-generated deadly
2540 * signal to be unblocked and set to SIG_DFL (and thus cause
2541 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2542 * or SIGILL. (The alternative is to leave process spinning
2543 * forever on the faulty instruction - not useful).
2544 *
2545 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2546 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2547 * but kernel will always restart them.
2548 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002549 case ERESTARTSYS:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002550 /* Most common type of signal-interrupted syscall exit code.
2551 * The system call will be restarted with the same arguments
2552 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2553 */
2554 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002555 break;
2556 case ERESTARTNOINTR:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002557 /* Rare. For example, fork() returns this if interrupted.
2558 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2559 */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002560 tprints("= ? ERESTARTNOINTR (To be restarted)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002561 break;
2562 case ERESTARTNOHAND:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002563 /* pause(), rt_sigsuspend() etc use this code.
2564 * SA_RESTART is ignored (assumed not set):
2565 * syscall won't restart (will return EINTR instead)
Denys Vlasenko30c03232013-02-19 16:59:26 +01002566 * even after signal with SA_RESTART set. However,
2567 * after SIG_IGN or SIG_DFL signal it will restart
2568 * (thus the name "restart only if has no handler").
Denys Vlasenkofe585652012-01-12 11:26:34 +01002569 */
Denys Vlasenkoaba62922013-03-05 16:56:35 +01002570 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002571 break;
2572 case ERESTART_RESTARTBLOCK:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002573 /* Syscalls like nanosleep(), poll() which can't be
2574 * restarted with their original arguments use this
2575 * code. Kernel will execute restart_syscall() instead,
2576 * which changes arguments before restarting syscall.
2577 * SA_RESTART is ignored (assumed not set) similarly
2578 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2579 * since restart data is saved in "restart block"
2580 * in task struct, and if signal handler uses a syscall
2581 * which in turn saves another such restart block,
2582 * old data is lost and restart becomes impossible)
2583 */
2584 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002585 break;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002586 default:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002587 if (u_error < 0)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002588 tprintf("= -1 E??? (errno %ld)", u_error);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002589 else if (u_error < nerrnos)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002590 tprintf("= -1 %s (%s)", errnoent[u_error],
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002591 strerror(u_error));
2592 else
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002593 tprintf("= -1 ERRNO_%ld (%s)", u_error,
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002594 strerror(u_error));
2595 break;
2596 }
2597 if ((sys_res & RVAL_STR) && tcp->auxstr)
2598 tprintf(" (%s)", tcp->auxstr);
2599 }
2600 else {
2601 if (sys_res & RVAL_NONE)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002602 tprints("= ?");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002603 else {
2604 switch (sys_res & RVAL_MASK) {
2605 case RVAL_HEX:
2606 tprintf("= %#lx", tcp->u_rval);
2607 break;
2608 case RVAL_OCTAL:
2609 tprintf("= %#lo", tcp->u_rval);
2610 break;
2611 case RVAL_UDECIMAL:
2612 tprintf("= %lu", tcp->u_rval);
2613 break;
2614 case RVAL_DECIMAL:
2615 tprintf("= %ld", tcp->u_rval);
2616 break;
H.J. Ludd0130b2012-04-16 12:16:45 +02002617#if defined(LINUX_MIPSN32) || defined(X32)
2618 /*
2619 case RVAL_LHEX:
2620 tprintf("= %#llx", tcp->u_lrval);
2621 break;
2622 case RVAL_LOCTAL:
2623 tprintf("= %#llo", tcp->u_lrval);
2624 break;
2625 */
2626 case RVAL_LUDECIMAL:
2627 tprintf("= %llu", tcp->u_lrval);
2628 break;
2629 /*
2630 case RVAL_LDECIMAL:
2631 tprintf("= %lld", tcp->u_lrval);
2632 break;
2633 */
2634#endif
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002635 default:
2636 fprintf(stderr,
2637 "invalid rval format\n");
2638 break;
2639 }
2640 }
2641 if ((sys_res & RVAL_STR) && tcp->auxstr)
2642 tprintf(" (%s)", tcp->auxstr);
2643 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002644 if (Tflag) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002645 tv_sub(&tv, &tv, &tcp->etime);
2646 tprintf(" <%ld.%06ld>",
2647 (long) tv.tv_sec, (long) tv.tv_usec);
2648 }
Denys Vlasenko000b6012012-01-28 01:25:03 +01002649 tprints("\n");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002650 dumpio(tcp);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002651 line_ended();
2652
Denys Vlasenko3b738812011-08-22 02:06:35 +02002653 ret:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002654 tcp->flags &= ~TCB_INSYSCALL;
2655 return 0;
2656}
2657
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002658int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002659trace_syscall(struct tcb *tcp)
2660{
2661 return exiting(tcp) ?
2662 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2663}