blob: 4d72f42b36a91d5753a68dd4d584125bbbd88274 [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00005 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Linux for s390 port by D.J. Barrow
8 * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00009 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000032 */
33
34#include "defs.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000035#include <sys/user.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000036#include <sys/param.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000037
Wichert Akkerman15dea971999-10-06 13:06:34 +000038#ifdef HAVE_SYS_REG_H
Denys Vlasenko523635f2012-02-25 02:44:25 +010039# include <sys/reg.h>
40# ifndef PTRACE_PEEKUSR
41# define PTRACE_PEEKUSR PTRACE_PEEKUSER
42# endif
Wichert Akkermanfaf72222000-02-19 23:59:03 +000043#elif defined(HAVE_LINUX_PTRACE_H)
Denys Vlasenko523635f2012-02-25 02:44:25 +010044# undef PTRACE_SYSCALL
Roland McGrathce9f0742004-03-01 21:29:22 +000045# ifdef HAVE_STRUCT_IA64_FPREG
46# define ia64_fpreg XXX_ia64_fpreg
47# endif
48# ifdef HAVE_STRUCT_PT_ALL_USER_REGS
49# define pt_all_user_regs XXX_pt_all_user_regs
50# endif
Denys Vlasenko523635f2012-02-25 02:44:25 +010051# include <linux/ptrace.h>
Roland McGrathce9f0742004-03-01 21:29:22 +000052# undef ia64_fpreg
53# undef pt_all_user_regs
Wichert Akkerman15dea971999-10-06 13:06:34 +000054#endif
55
Denys Vlasenko84703742012-02-25 02:38:52 +010056#if defined(SPARC64)
Roland McGrath6d1a65c2004-07-12 07:44:08 +000057# undef PTRACE_GETREGS
58# define PTRACE_GETREGS PTRACE_GETREGS64
59# undef PTRACE_SETREGS
60# define PTRACE_SETREGS PTRACE_SETREGS64
Denys Vlasenko84703742012-02-25 02:38:52 +010061#endif
Roland McGrath6d1a65c2004-07-12 07:44:08 +000062
Denys Vlasenko84703742012-02-25 02:38:52 +010063#if defined(IA64)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000064# include <asm/ptrace_offsets.h>
65# include <asm/rse.h>
66#endif
67
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010068#if defined(X86_64) || defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010069# include <sys/uio.h>
70# include <elf.h>
71#endif
72
Steve McIntyred8d3bd32012-10-24 17:58:16 +010073#if defined(AARCH64)
74# include <asm/ptrace.h>
75# include <sys/uio.h>
76# include <elf.h>
77#endif
78
Christian Svensson492f81f2013-02-14 13:26:27 +010079#if defined(OR1K)
80# include <sys/uio.h>
81# include <elf.h>
82#endif
83
James Hogan5f999a82013-02-22 14:44:10 +000084#if defined(METAG)
85# include <sys/uio.h>
86# include <elf.h>
87#endif
88
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000089#ifndef ERESTARTSYS
Denys Vlasenko523635f2012-02-25 02:44:25 +010090# define ERESTARTSYS 512
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000091#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010092#ifndef ERESTARTNOINTR
Denys Vlasenko523635f2012-02-25 02:44:25 +010093# define ERESTARTNOINTR 513
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000094#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010095#ifndef ERESTARTNOHAND
96# define ERESTARTNOHAND 514 /* restart if no handler */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000097#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010098#ifndef ERESTART_RESTARTBLOCK
Denys Vlasenko523635f2012-02-25 02:44:25 +010099# define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
Roland McGrath9c555e72003-07-09 09:47:59 +0000100#endif
Denys Vlasenko523635f2012-02-25 02:44:25 +0100101
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000102#ifndef NSIG
Denys Vlasenko523635f2012-02-25 02:44:25 +0100103# warning: NSIG is not defined, using 32
104# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000105#endif
106#ifdef ARM
Denys Vlasenko041b3ee2011-08-18 12:48:56 +0200107/* Ugh. Is this really correct? ARM has no RT signals?! */
Denys Vlasenko523635f2012-02-25 02:44:25 +0100108# undef NSIG
109# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000110#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000111
112#include "syscall.h"
113
114/* Define these shorthand notations to simplify the syscallent files. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000115#define TD TRACE_DESC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000116#define TF TRACE_FILE
117#define TI TRACE_IPC
118#define TN TRACE_NETWORK
119#define TP TRACE_PROCESS
120#define TS TRACE_SIGNAL
Namhyung Kim96792962012-10-24 11:41:57 +0900121#define TM TRACE_MEMORY
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000122#define NF SYSCALL_NEVER_FAILS
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200123#define MA MAX_ARGS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000124
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100125const struct_sysent sysent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000126#include "syscallent.h"
127};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000128
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100129#if SUPPORTED_PERSONALITIES > 1
130static const struct_sysent sysent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100131# include "syscallent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000132};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200133#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000134
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100135#if SUPPORTED_PERSONALITIES > 2
136static const struct_sysent sysent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100137# include "syscallent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000138};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200139#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000140
141/* Now undef them since short defines cause wicked namespace pollution. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000142#undef TD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000143#undef TF
144#undef TI
145#undef TN
146#undef TP
147#undef TS
Namhyung Kim96792962012-10-24 11:41:57 +0900148#undef TM
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000149#undef NF
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200150#undef MA
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000151
Denys Vlasenko39fca622011-08-20 02:12:33 +0200152/*
153 * `ioctlent.h' may be generated from `ioctlent.raw' by the auxiliary
154 * program `ioctlsort', such that the list is sorted by the `code' field.
155 * This has the side-effect of resolving the _IO.. macros into
156 * plain integers, eliminating the need to include here everything
157 * in "/usr/include".
158 */
159
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100160const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000161#include "errnoent.h"
162};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100163const char *const signalent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200164#include "signalent.h"
165};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100166const struct_ioctlent ioctlent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200167#include "ioctlent.h"
168};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000169
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100170#if SUPPORTED_PERSONALITIES > 1
Roland McGrathee36ce12004-09-04 03:53:10 +0000171static const char *const errnoent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100172# include "errnoent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000173};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200174static const char *const signalent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100175# include "signalent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200176};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100177static const struct_ioctlent ioctlent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100178# include "ioctlent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200179};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200180#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000181
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100182#if SUPPORTED_PERSONALITIES > 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000183static const char *const errnoent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100184# include "errnoent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000185};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200186static const char *const signalent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100187# include "signalent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200188};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100189static const struct_ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100190# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200191};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200192#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000193
Dmitry V. Levine6f55242013-02-26 22:02:30 +0000194enum {
195 nsyscalls0 = ARRAY_SIZE(sysent0)
196#if SUPPORTED_PERSONALITIES > 1
197 , nsyscalls1 = ARRAY_SIZE(sysent1)
198# if SUPPORTED_PERSONALITIES > 2
199 , nsyscalls2 = ARRAY_SIZE(sysent2)
200# endif
201#endif
202};
203
204enum {
205 nerrnos0 = ARRAY_SIZE(errnoent0)
206#if SUPPORTED_PERSONALITIES > 1
207 , nerrnos1 = ARRAY_SIZE(errnoent1)
208# if SUPPORTED_PERSONALITIES > 2
209 , nerrnos2 = ARRAY_SIZE(errnoent2)
210# endif
211#endif
212};
213
214enum {
215 nsignals0 = ARRAY_SIZE(signalent0)
216#if SUPPORTED_PERSONALITIES > 1
217 , nsignals1 = ARRAY_SIZE(signalent1)
218# if SUPPORTED_PERSONALITIES > 2
219 , nsignals2 = ARRAY_SIZE(signalent2)
220# endif
221#endif
222};
223
224enum {
225 nioctlents0 = ARRAY_SIZE(ioctlent0)
226#if SUPPORTED_PERSONALITIES > 1
227 , nioctlents1 = ARRAY_SIZE(ioctlent1)
228# if SUPPORTED_PERSONALITIES > 2
229 , nioctlents2 = ARRAY_SIZE(ioctlent2)
230# endif
231#endif
232};
233
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100234#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100235const struct_sysent *sysent = sysent0;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100236const char *const *errnoent = errnoent0;
237const char *const *signalent = signalent0;
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100238const struct_ioctlent *ioctlent = ioctlent0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100239#endif
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100240unsigned nsyscalls = nsyscalls0;
241unsigned nerrnos = nerrnos0;
242unsigned nsignals = nsignals0;
243unsigned nioctlents = nioctlents0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100244
245unsigned num_quals;
246qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
247
248static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
249 nsyscalls0,
250#if SUPPORTED_PERSONALITIES > 1
251 nsyscalls1,
252#endif
253#if SUPPORTED_PERSONALITIES > 2
254 nsyscalls2,
255#endif
256};
257static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
258 sysent0,
259#if SUPPORTED_PERSONALITIES > 1
260 sysent1,
261#endif
262#if SUPPORTED_PERSONALITIES > 2
263 sysent2,
264#endif
265};
266
267enum {
268 MAX_NSYSCALLS1 = (nsyscalls0
269#if SUPPORTED_PERSONALITIES > 1
270 > nsyscalls1 ? nsyscalls0 : nsyscalls1
271#endif
272 ),
273 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
274#if SUPPORTED_PERSONALITIES > 2
275 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
276#endif
277 ),
278 MAX_NSYSCALLS = MAX_NSYSCALLS2,
279 /* We are ready for arches with up to 255 signals,
280 * even though the largest known signo is on MIPS and it is 128.
281 * The number of existing syscalls on all arches is
282 * larger that 255 anyway, so it is just a pedantic matter.
283 */
284 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
285};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000286
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100287#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100288unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000289
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100290# ifndef current_wordsize
291unsigned current_wordsize;
292static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000293 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000294 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100295# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000296 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100297# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200298};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100299# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000300
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200301void
Dmitry V. Levin3abe8b22006-12-20 22:37:21 +0000302set_personality(int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000303{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100304 nsyscalls = nsyscall_vec[personality];
305 sysent = sysent_vec[personality];
306
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000307 switch (personality) {
308 case 0:
309 errnoent = errnoent0;
310 nerrnos = nerrnos0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000311 ioctlent = ioctlent0;
312 nioctlents = nioctlents0;
313 signalent = signalent0;
314 nsignals = nsignals0;
315 break;
316
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000317 case 1:
318 errnoent = errnoent1;
319 nerrnos = nerrnos1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000320 ioctlent = ioctlent1;
321 nioctlents = nioctlents1;
322 signalent = signalent1;
323 nsignals = nsignals1;
324 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000325
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100326# if SUPPORTED_PERSONALITIES > 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000327 case 2:
328 errnoent = errnoent2;
329 nerrnos = nerrnos2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000330 ioctlent = ioctlent2;
331 nioctlents = nioctlents2;
332 signalent = signalent2;
333 nsignals = nsignals2;
334 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100335# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000336 }
337
338 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100339# ifndef current_wordsize
340 current_wordsize = personality_wordsize[personality];
341# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000342}
343
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000344static void
345update_personality(struct tcb *tcp, int personality)
346{
347 if (personality == current_personality)
348 return;
349 set_personality(personality);
350
351 if (personality == tcp->currpers)
352 return;
353 tcp->currpers = personality;
354
H.J. Lu35be5812012-04-16 13:00:01 +0200355# if defined(POWERPC64)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000356 if (!qflag) {
357 static const char *const names[] = {"64 bit", "32 bit"};
358 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
359 tcp->pid, names[personality]);
360 }
H.J. Lu35be5812012-04-16 13:00:01 +0200361# elif defined(X86_64)
362 if (!qflag) {
363 static const char *const names[] = {"64 bit", "32 bit", "x32"};
364 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
365 tcp->pid, names[personality]);
366 }
H.J. Lu085e4282012-04-17 11:05:04 -0700367# elif defined(X32)
368 if (!qflag) {
369 static const char *const names[] = {"x32", "32 bit"};
370 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
371 tcp->pid, names[personality]);
372 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000373# elif defined(AARCH64)
374 if (!qflag) {
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100375 static const char *const names[] = {"32-bit", "AArch64"};
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000376 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
377 tcp->pid, names[personality]);
378 }
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100379# elif defined(TILE)
380 if (!qflag) {
381 static const char *const names[] = {"64-bit", "32-bit"};
382 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
383 tcp->pid, names[personality]);
384 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100385# endif
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000386}
387#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000388
Denys Vlasenkoc1540fe2013-02-21 16:17:08 +0100389static int qual_syscall(), qual_signal(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000390
Roland McGrathe10e62a2004-09-04 04:20:43 +0000391static const struct qual_options {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000392 int bitflag;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000393 const char *option_name;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000394 int (*qualify)(const char *, int, int);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000395 const char *argument_name;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000396} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000397 { QUAL_TRACE, "trace", qual_syscall, "system call" },
398 { QUAL_TRACE, "t", qual_syscall, "system call" },
399 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
400 { QUAL_ABBREV, "a", qual_syscall, "system call" },
401 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
402 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
403 { QUAL_RAW, "raw", qual_syscall, "system call" },
404 { QUAL_RAW, "x", qual_syscall, "system call" },
405 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
406 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
407 { QUAL_SIGNAL, "s", qual_signal, "signal" },
Roland McGrath9797ceb2002-12-30 10:23:00 +0000408 { QUAL_READ, "read", qual_desc, "descriptor" },
409 { QUAL_READ, "reads", qual_desc, "descriptor" },
410 { QUAL_READ, "r", qual_desc, "descriptor" },
411 { QUAL_WRITE, "write", qual_desc, "descriptor" },
412 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
413 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000414 { 0, NULL, NULL, NULL },
415};
416
Roland McGrath9797ceb2002-12-30 10:23:00 +0000417static void
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100418reallocate_qual(int n)
419{
420 unsigned p;
421 qualbits_t *qp;
422 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
423 qp = qual_vec[p] = realloc(qual_vec[p], n * sizeof(qualbits_t));
424 if (!qp)
425 die_out_of_memory();
426 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
427 }
428 num_quals = n;
429}
430
431static void
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000432qualify_one(int n, int bitflag, int not, int pers)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000433{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100434 unsigned p;
Roland McGrath138c6a32006-01-12 09:50:49 +0000435
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100436 if (num_quals <= n)
437 reallocate_qual(n + 1);
Roland McGrath138c6a32006-01-12 09:50:49 +0000438
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100439 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
440 if (pers == p || pers < 0) {
441 if (not)
442 qual_vec[p][n] &= ~bitflag;
443 else
444 qual_vec[p][n] |= bitflag;
445 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000446 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000447}
448
449static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000450qual_syscall(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000451{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100452 unsigned p;
453 unsigned i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000454 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000455
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100456 if (*s >= '0' && *s <= '9') {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100457 i = string_to_uint(s);
Denys Vlasenkob43dacd2013-02-23 18:19:28 +0100458 if (i >= MAX_NSYSCALLS)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000459 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000460 qualify_one(i, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000461 return 0;
Roland McGrath48a035f2006-01-12 09:45:56 +0000462 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000463
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100464 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
465 for (i = 0; i < nsyscall_vec[p]; i++) {
466 if (sysent_vec[p][i].sys_name
467 && strcmp(s, sysent_vec[p][i].sys_name) == 0
468 ) {
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000469 qualify_one(i, bitflag, not, p);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100470 rc = 0;
471 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000472 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100473 }
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000474
Roland McGrathfe6b3522005-02-02 04:40:11 +0000475 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000476}
477
478static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000479qual_signal(const char *s, int bitflag, int not)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000480{
481 int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000482
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100483 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000484 int signo = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100485 if (signo < 0 || signo > 255)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000486 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000487 qualify_one(signo, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000488 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000489 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000490 if (strncasecmp(s, "SIG", 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000491 s += 3;
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100492 for (i = 0; i <= NSIG; i++) {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000493 if (strcasecmp(s, signame(i) + 3) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000494 qualify_one(i, bitflag, not, -1);
Roland McGrath76421df2005-02-02 03:51:18 +0000495 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000496 }
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100497 }
Roland McGrath76421df2005-02-02 03:51:18 +0000498 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000499}
500
501static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000502qual_desc(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000503{
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100504 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000505 int desc = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100506 if (desc < 0 || desc > 0x7fff) /* paranoia */
Roland McGrathfe6b3522005-02-02 04:40:11 +0000507 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000508 qualify_one(desc, bitflag, not, -1);
Roland McGrath2b619022003-04-10 18:58:20 +0000509 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000510 }
511 return -1;
512}
513
514static int
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000515lookup_class(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000516{
517 if (strcmp(s, "file") == 0)
518 return TRACE_FILE;
519 if (strcmp(s, "ipc") == 0)
520 return TRACE_IPC;
521 if (strcmp(s, "network") == 0)
522 return TRACE_NETWORK;
523 if (strcmp(s, "process") == 0)
524 return TRACE_PROCESS;
525 if (strcmp(s, "signal") == 0)
526 return TRACE_SIGNAL;
Roland McGrath2fe7b132005-07-05 03:25:35 +0000527 if (strcmp(s, "desc") == 0)
528 return TRACE_DESC;
Namhyung Kim96792962012-10-24 11:41:57 +0900529 if (strcmp(s, "memory") == 0)
530 return TRACE_MEMORY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000531 return -1;
532}
533
534void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000535qualify(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000536{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000537 const struct qual_options *opt;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000538 int not;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000539 char *copy;
540 const char *p;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000541 int i, n;
542
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100543 if (num_quals == 0)
544 reallocate_qual(MIN_QUALS);
545
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000546 opt = &qual_options[0];
547 for (i = 0; (p = qual_options[i].option_name); i++) {
548 n = strlen(p);
549 if (strncmp(s, p, n) == 0 && s[n] == '=') {
550 opt = &qual_options[i];
551 s += n + 1;
552 break;
553 }
554 }
555 not = 0;
556 if (*s == '!') {
557 not = 1;
558 s++;
559 }
560 if (strcmp(s, "none") == 0) {
561 not = 1 - not;
562 s = "all";
563 }
564 if (strcmp(s, "all") == 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100565 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000566 qualify_one(i, opt->bitflag, not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000567 }
568 return;
569 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100570 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000571 qualify_one(i, opt->bitflag, !not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000572 }
Denys Vlasenko5d645812011-08-20 12:48:18 +0200573 copy = strdup(s);
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200574 if (!copy)
575 die_out_of_memory();
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000576 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000577 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100578 unsigned pers;
579 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
580 for (i = 0; i < nsyscall_vec[pers]; i++)
581 if (sysent_vec[pers][i].sys_flags & n)
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000582 qualify_one(i, opt->bitflag, not, pers);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100583 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000584 continue;
585 }
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000586 if (opt->qualify(p, opt->bitflag, not)) {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100587 error_msg_and_die("invalid %s '%s'",
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000588 opt->argument_name, p);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000589 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000590 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000591 free(copy);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000592 return;
593}
594
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000595#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000596static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000597decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000598{
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000599 unsigned long addr;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100600 unsigned int i, n, size;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000601
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000602 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
603 return;
604
605 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100606 tcp->qual_flg = qual_flags[tcp->scno];
607 tcp->s_ent = &sysent[tcp->scno];
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000608 addr = tcp->u_arg[1];
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100609 size = current_wordsize;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100610 n = tcp->s_ent->nargs;
611 for (i = 0; i < n; ++i) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000612 if (size == sizeof(int)) {
613 unsigned int arg;
614 if (umove(tcp, addr, &arg) < 0)
615 arg = 0;
616 tcp->u_arg[i] = arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000617 }
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000618 else {
619 unsigned long arg;
620 if (umove(tcp, addr, &arg) < 0)
621 arg = 0;
622 tcp->u_arg[i] = arg;
623 }
624 addr += size;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000625 }
626}
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000627#endif
Mike Frysinger3362e892012-03-15 01:09:19 -0400628
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000629#ifdef SYS_ipc_subcall
630static void
631decode_ipc_subcall(struct tcb *tcp)
632{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100633 unsigned int i, n;
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000634
635 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
636 return;
637
638 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100639 tcp->qual_flg = qual_flags[tcp->scno];
640 tcp->s_ent = &sysent[tcp->scno];
641 n = tcp->s_ent->nargs;
642 for (i = 0; i < n; i++)
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000643 tcp->u_arg[i] = tcp->u_arg[i + 1];
644}
645#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000646
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200647int
648printargs(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000649{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200650 if (entering(tcp)) {
651 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100652 int n = tcp->s_ent->nargs;
653 for (i = 0; i < n; i++)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200654 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
655 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000656 return 0;
657}
658
Denys Vlasenko72879c62012-02-27 14:18:02 +0100659int
660printargs_lu(struct tcb *tcp)
661{
662 if (entering(tcp)) {
663 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100664 int n = tcp->s_ent->nargs;
665 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100666 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
667 }
668 return 0;
669}
670
671int
672printargs_ld(struct tcb *tcp)
673{
674 if (entering(tcp)) {
675 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100676 int n = tcp->s_ent->nargs;
677 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100678 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
679 }
680 return 0;
681}
682
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100683#if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200684long
685getrval2(struct tcb *tcp)
686{
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100687 long val;
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200688
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100689# if defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100690 val = sparc_regs.u_regs[U_REG_O1];
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100691# elif defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200692 if (upeek(tcp, 4*(REG_REG0+1), &val) < 0)
693 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100694# elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200695 if (upeek(tcp, PT_R9, &val) < 0)
696 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100697# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200698
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200699 return val;
700}
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100701#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200702
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200703int
704is_restart_error(struct tcb *tcp)
705{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200706 switch (tcp->u_error) {
707 case ERESTARTSYS:
708 case ERESTARTNOINTR:
709 case ERESTARTNOHAND:
710 case ERESTART_RESTARTBLOCK:
711 return 1;
712 default:
713 break;
714 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200715 return 0;
716}
717
Denys Vlasenko523635f2012-02-25 02:44:25 +0100718#if defined(I386)
Denys Vlasenko2550d482013-02-15 21:04:28 +0100719struct user_regs_struct i386_regs;
H.J. Lu35be5812012-04-16 13:00:01 +0200720#elif defined(X86_64) || defined(X32)
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100721/*
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100722 * On i386, pt_regs and user_regs_struct are the same,
723 * but on 64 bit x86, user_regs_struct has six more fields:
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100724 * fs_base, gs_base, ds, es, fs, gs.
725 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
726 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100727struct i386_user_regs_struct {
728 uint32_t ebx;
729 uint32_t ecx;
730 uint32_t edx;
731 uint32_t esi;
732 uint32_t edi;
733 uint32_t ebp;
734 uint32_t eax;
735 uint32_t xds;
736 uint32_t xes;
737 uint32_t xfs;
738 uint32_t xgs;
739 uint32_t orig_eax;
740 uint32_t eip;
741 uint32_t xcs;
742 uint32_t eflags;
743 uint32_t esp;
744 uint32_t xss;
745};
746static union {
747 struct user_regs_struct x86_64_r;
748 struct i386_user_regs_struct i386_r;
749} x86_regs_union;
750# define x86_64_regs x86_regs_union.x86_64_r
751# define i386_regs x86_regs_union.i386_r
752static struct iovec x86_io = {
753 .iov_base = &x86_regs_union
754};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100755#elif defined(IA64)
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200756long ia32 = 0; /* not static */
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100757static long ia64_r8, ia64_r10;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100758#elif defined(POWERPC)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +0100759static long ppc_result;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100760#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100761static long m68k_d0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100762#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100763static long bfin_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100764#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100765struct pt_regs arm_regs; /* not static */
Steve McIntyred8d3bd32012-10-24 17:58:16 +0100766#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100767static union {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100768 struct user_pt_regs aarch64_r;
769 struct arm_pt_regs arm_r;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100770} arm_regs_union;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100771# define aarch64_regs arm_regs_union.aarch64_r
772# define arm_regs arm_regs_union.arm_r
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100773static struct iovec aarch64_io = {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100774 .iov_base = &arm_regs_union
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100775};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100776#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100777static long alpha_r0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100778static long alpha_a3;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100779#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100780static struct pt_regs avr32_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100781#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100782struct pt_regs sparc_regs; /* not static */
Denys Vlasenko523635f2012-02-25 02:44:25 +0100783#elif defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100784static long long mips_a3;
785static long long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100786#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100787static long mips_a3;
788static long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100789#elif defined(S390) || defined(S390X)
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200790static long gpr2;
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200791static long syscall_mode;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100792#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100793static long hppa_r28;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100794#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100795static long sh_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100796#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100797static long sh64_r9;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100798#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100799static long cris_r10;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100800#elif defined(TILE)
801struct pt_regs tile_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100802#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100803static long microblaze_r3;
Christian Svensson492f81f2013-02-14 13:26:27 +0100804#elif defined(OR1K)
805static struct user_regs_struct or1k_regs;
806static struct iovec or1k_io = {
807 .iov_base = &or1k_regs
808};
James Hogan5f999a82013-02-22 14:44:10 +0000809#elif defined(METAG)
810static struct user_gp_regs metag_regs;
811static struct iovec metag_io = {
812 .iov_base = &metag_regs
813};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100814#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000815
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100816void
817printcall(struct tcb *tcp)
818{
819#define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
820 sizeof(long) == 8 ? "[????????????????] " : \
821 NULL /* crash */)
822 if (get_regs_error) {
823 PRINTBADPC;
824 return;
825 }
826#if defined(I386)
827 tprintf("[%08lx] ", i386_regs.eip);
828#elif defined(S390) || defined(S390X)
829 long psw;
830 if (upeek(tcp, PT_PSWADDR, &psw) < 0) {
831 PRINTBADPC;
832 return;
833 }
834# ifdef S390
835 tprintf("[%08lx] ", psw);
836# elif S390X
Dmitry V. Levinddba73e2013-02-05 19:01:58 +0000837 tprintf("[%016lx] ", psw);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100838# endif
839#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100840 if (x86_io.iov_len == sizeof(i386_regs)) {
841 tprintf("[%08x] ", (unsigned) i386_regs.eip);
842 } else {
843# if defined(X86_64)
844 tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
845# elif defined(X32)
846 /* Note: this truncates 64-bit rip to 32 bits */
847 tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip);
848# endif
849 }
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100850#elif defined(IA64)
851 long ip;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100852 if (upeek(tcp, PT_B0, &ip) < 0) {
853 PRINTBADPC;
854 return;
855 }
856 tprintf("[%08lx] ", ip);
857#elif defined(POWERPC)
858 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100859 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) {
860 PRINTBADPC;
861 return;
862 }
863# ifdef POWERPC64
864 tprintf("[%016lx] ", pc);
865# else
866 tprintf("[%08lx] ", pc);
867# endif
868#elif defined(M68K)
869 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100870 if (upeek(tcp, 4*PT_PC, &pc) < 0) {
871 tprints("[????????] ");
872 return;
873 }
874 tprintf("[%08lx] ", pc);
875#elif defined(ALPHA)
876 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100877 if (upeek(tcp, REG_PC, &pc) < 0) {
878 tprints("[????????????????] ");
879 return;
880 }
881 tprintf("[%08lx] ", pc);
882#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100883 tprintf("[%08lx] ", sparc_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100884#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100885 tprintf("[%08lx] ", sparc_regs.tpc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100886#elif defined(HPPA)
887 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100888 if (upeek(tcp, PT_IAOQ0, &pc) < 0) {
889 tprints("[????????] ");
890 return;
891 }
892 tprintf("[%08lx] ", pc);
893#elif defined(MIPS)
894 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100895 if (upeek(tcp, REG_EPC, &pc) < 0) {
896 tprints("[????????] ");
897 return;
898 }
899 tprintf("[%08lx] ", pc);
900#elif defined(SH)
901 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100902 if (upeek(tcp, 4*REG_PC, &pc) < 0) {
903 tprints("[????????] ");
904 return;
905 }
906 tprintf("[%08lx] ", pc);
907#elif defined(SH64)
908 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100909 if (upeek(tcp, REG_PC, &pc) < 0) {
910 tprints("[????????????????] ");
911 return;
912 }
913 tprintf("[%08lx] ", pc);
914#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100915 tprintf("[%08lx] ", arm_regs.ARM_pc);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100916#elif defined(AARCH64)
917 /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100918#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100919 tprintf("[%08lx] ", avr32_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100920#elif defined(BFIN)
921 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100922 if (upeek(tcp, PT_PC, &pc) < 0) {
923 PRINTBADPC;
924 return;
925 }
926 tprintf("[%08lx] ", pc);
927#elif defined(CRISV10)
928 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100929 if (upeek(tcp, 4*PT_IRP, &pc) < 0) {
930 PRINTBADPC;
931 return;
932 }
933 tprintf("[%08lx] ", pc);
934#elif defined(CRISV32)
935 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100936 if (upeek(tcp, 4*PT_ERP, &pc) < 0) {
937 PRINTBADPC;
938 return;
939 }
940 tprintf("[%08lx] ", pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100941#elif defined(TILE)
942# ifdef _LP64
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500943 tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100944# else
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500945 tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100946# endif
Christian Svensson492f81f2013-02-14 13:26:27 +0100947#elif defined(OR1K)
948 tprintf("[%08lx] ", or1k_regs.pc);
James Hogan5f999a82013-02-22 14:44:10 +0000949#elif defined(METAG)
950 tprintf("[%08lx] ", metag_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100951#endif /* architecture */
952}
953
Denys Vlasenko7270de52013-02-21 15:46:34 +0100954/* Shuffle syscall numbers so that we don't have huge gaps in syscall table.
955 * The shuffling should be reversible: shuffle_scno(shuffle_scno(n)) == n.
956 */
957#if defined(ARM) /* So far only ARM needs this */
958static long
959shuffle_scno(unsigned long scno)
960{
961 if (scno <= ARM_LAST_ORDINARY_SYSCALL)
962 return scno;
963
964 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
965 if (scno == 0x000ffff0)
966 return ARM_LAST_ORDINARY_SYSCALL+1;
967 if (scno == ARM_LAST_ORDINARY_SYSCALL+1)
968 return 0x000ffff0;
969
970 /* Is it ARM specific syscall?
971 * Swap with [LAST_ORDINARY+2, LAST_ORDINARY+2 + LAST_SPECIAL] range.
972 */
973 if (scno >= 0x000f0000
974 && scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL
975 ) {
976 return scno - 0x000f0000 + (ARM_LAST_ORDINARY_SYSCALL+2);
977 }
978 if (/* scno >= ARM_LAST_ORDINARY_SYSCALL+2 - always true */ 1
979 && scno <= (ARM_LAST_ORDINARY_SYSCALL+2) + ARM_LAST_SPECIAL_SYSCALL
980 ) {
981 return scno + 0x000f0000 - (ARM_LAST_ORDINARY_SYSCALL+2);
982 }
983
984 return scno;
985}
986#else
987# define shuffle_scno(scno) ((long)(scno))
988#endif
989
990static char*
991undefined_scno_name(struct tcb *tcp)
992{
993 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
994
995 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
996 return buf;
997}
998
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100999#ifndef get_regs
1000long get_regs_error;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001001void
1002get_regs(pid_t pid)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001003{
1004# if defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001005 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001006# elif defined(I386)
1007 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &i386_regs);
Dmitry V. Levinee348c62013-03-17 23:15:18 +00001008# elif defined(X32)
1009 /* x86_io.iov_base = &x86_regs_union; - already is */
1010 x86_io.iov_len = sizeof(x86_regs_union);
1011 get_regs_error = ptrace(PTRACE_GETREGSET, pid,
1012 NT_PRSTATUS, (long) &x86_io);
1013# elif defined(X86_64)
Denys Vlasenkoe3b248d2013-02-15 00:24:19 +01001014 /*
1015 * PTRACE_GETREGSET was introduced in 2.6.33.
1016 * Let's be paranoid and require a bit later kernel.
1017 */
1018 if (os_release >= KERNEL_VERSION(2,6,35)) {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001019 /*x86_io.iov_base = &x86_regs_union; - already is */
1020 x86_io.iov_len = sizeof(x86_regs_union);
1021 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (long) &x86_io);
1022 } else {
1023 /* Use old method, with heuristical detection of 32-bitness */
1024 x86_io.iov_len = sizeof(x86_64_regs);
1025 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &x86_64_regs);
1026 if (!get_regs_error && x86_64_regs.cs == 0x23) {
1027 x86_io.iov_len = sizeof(i386_regs);
1028 /*
1029 * The order is important: i386_regs and x86_64_regs
1030 * are overlaid in memory!
1031 */
1032 i386_regs.ebx = x86_64_regs.rbx;
1033 i386_regs.ecx = x86_64_regs.rcx;
1034 i386_regs.edx = x86_64_regs.rdx;
1035 i386_regs.esi = x86_64_regs.rsi;
1036 i386_regs.edi = x86_64_regs.rdi;
1037 i386_regs.ebp = x86_64_regs.rbp;
1038 i386_regs.eax = x86_64_regs.rax;
1039 /*i386_regs.xds = x86_64_regs.ds; unused by strace */
1040 /*i386_regs.xes = x86_64_regs.es; ditto... */
1041 /*i386_regs.xfs = x86_64_regs.fs;*/
1042 /*i386_regs.xgs = x86_64_regs.gs;*/
1043 i386_regs.orig_eax = x86_64_regs.orig_rax;
1044 i386_regs.eip = x86_64_regs.rip;
1045 /*i386_regs.xcs = x86_64_regs.cs;*/
1046 /*i386_regs.eflags = x86_64_regs.eflags;*/
1047 i386_regs.esp = x86_64_regs.rsp;
1048 /*i386_regs.xss = x86_64_regs.ss;*/
1049 }
1050 }
Denys Vlasenko401374e2013-02-06 18:24:39 +01001051# elif defined(ARM)
1052 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (void *)&arm_regs);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001053# elif defined(AARCH64)
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001054 /*aarch64_io.iov_base = &arm_regs_union; - already is */
1055 aarch64_io.iov_len = sizeof(arm_regs_union);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001056 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (void *)&aarch64_io);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001057# if 0
1058 /* Paranoia checks */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001059 if (get_regs_error)
1060 return;
1061 switch (aarch64_io.iov_len) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001062 case sizeof(aarch64_regs):
1063 /* We are in 64-bit mode */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001064 break;
1065 case sizeof(arm_regs):
1066 /* We are in 32-bit mode */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001067 break;
1068 default:
1069 get_regs_error = -1;
1070 break;
1071 }
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001072# endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001073# elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001074 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001075# elif defined(TILE)
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -05001076 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &tile_regs);
Christian Svensson492f81f2013-02-14 13:26:27 +01001077# elif defined(OR1K)
1078 or1k_io.iov_len = sizeof(or1k_regs);
1079 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &or1k_io);
James Hogan5f999a82013-02-22 14:44:10 +00001080# elif defined(METAG)
1081 metag_io.iov_len = sizeof(metag_regs);
1082 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &metag_io);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001083# endif
1084}
1085#endif
1086
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001087/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001088 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1089 * 1: ok, continue in trace_syscall_entering().
1090 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001091 * ("????" etc) and bail out.
1092 */
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01001093static int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001094get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001095{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001096 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001097
Denys Vlasenko523635f2012-02-25 02:44:25 +01001098#if defined(S390) || defined(S390X)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001099 if (upeek(tcp, PT_GPR2, &syscall_mode) < 0)
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001100 return -1;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001101
1102 if (syscall_mode != -ENOSYS) {
1103 /*
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001104 * Since kernel version 2.5.44 the scno gets passed in gpr2.
Roland McGrath2f924ca2003-06-26 22:23:28 +00001105 */
1106 scno = syscall_mode;
1107 } else {
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001108 /*
Michal Ludvig882eda82002-11-11 12:50:47 +00001109 * Old style of "passing" the scno via the SVC instruction.
1110 */
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001111 long psw;
Michal Ludvig882eda82002-11-11 12:50:47 +00001112 long opcode, offset_reg, tmp;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001113 void *svc_addr;
Denys Vlasenko7c9ba8b2011-08-19 19:46:32 +02001114 static const int gpr_offset[16] = {
1115 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
1116 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
1117 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
1118 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
1119 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001120
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001121 if (upeek(tcp, PT_PSWADDR, &psw) < 0)
Michal Ludvig882eda82002-11-11 12:50:47 +00001122 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001123 errno = 0;
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001124 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +00001125 if (errno) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001126 perror_msg("peektext(psw-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +00001127 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001128 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001129
1130 /*
1131 * We have to check if the SVC got executed directly or via an
1132 * EXECUTE instruction. In case of EXECUTE it is necessary to do
1133 * instruction decoding to derive the system call number.
1134 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1135 * so that this doesn't work if a SVC opcode is part of an EXECUTE
1136 * opcode. Since there is no way to find out the opcode size this
1137 * is the best we can do...
1138 */
Michal Ludvig882eda82002-11-11 12:50:47 +00001139 if ((opcode & 0xff00) == 0x0a00) {
1140 /* SVC opcode */
1141 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +00001142 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001143 else {
1144 /* SVC got executed by EXECUTE instruction */
1145
1146 /*
1147 * Do instruction decoding of EXECUTE. If you really want to
1148 * understand this, read the Principles of Operations.
1149 */
1150 svc_addr = (void *) (opcode & 0xfff);
1151
1152 tmp = 0;
1153 offset_reg = (opcode & 0x000f0000) >> 16;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001154 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001155 return -1;
1156 svc_addr += tmp;
1157
1158 tmp = 0;
1159 offset_reg = (opcode & 0x0000f000) >> 12;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001160 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001161 return -1;
1162 svc_addr += tmp;
1163
Denys Vlasenkofb036672009-01-23 16:30:26 +00001164 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
Michal Ludvig882eda82002-11-11 12:50:47 +00001165 if (errno)
1166 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001167# if defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +00001168 scno >>= 48;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001169# else
Michal Ludvig882eda82002-11-11 12:50:47 +00001170 scno >>= 16;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001171# endif
Michal Ludvig882eda82002-11-11 12:50:47 +00001172 tmp = 0;
1173 offset_reg = (opcode & 0x00f00000) >> 20;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001174 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001175 return -1;
1176
1177 scno = (scno | tmp) & 0xff;
1178 }
1179 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001180#elif defined(POWERPC)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001181 if (upeek(tcp, sizeof(unsigned long)*PT_R0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001182 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001183# ifdef POWERPC64
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001184 /* TODO: speed up strace by not doing this at every syscall.
1185 * We only need to do it after execve.
1186 */
1187 int currpers;
1188 long val;
Andreas Schwabd69fa492010-07-12 21:39:57 +02001189
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001190 /* Check for 64/32 bit mode. */
1191 if (upeek(tcp, sizeof(unsigned long)*PT_MSR, &val) < 0)
1192 return -1;
1193 /* SF is bit 0 of MSR */
1194 if (val < 0)
1195 currpers = 0;
1196 else
1197 currpers = 1;
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001198 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001199# endif
1200#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001201 scno = avr32_regs.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001202#elif defined(BFIN)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001203 if (upeek(tcp, PT_ORIG_P0, &scno))
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001204 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001205#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001206 scno = i386_regs.orig_eax;
H.J. Lu35be5812012-04-16 13:00:01 +02001207#elif defined(X86_64) || defined(X32)
1208# ifndef __X32_SYSCALL_BIT
1209# define __X32_SYSCALL_BIT 0x40000000
1210# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001211 int currpers;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001212# if 1
1213 /* GETREGSET of NT_PRSTATUS tells us regset size,
1214 * which unambiguously detects i386.
1215 *
1216 * Linux kernel distinguishes x86-64 and x32 processes
1217 * solely by looking at __X32_SYSCALL_BIT:
1218 * arch/x86/include/asm/compat.h::is_x32_task():
1219 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1220 * return true;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001221 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001222 if (x86_io.iov_len == sizeof(i386_regs)) {
1223 scno = i386_regs.orig_eax;
1224 currpers = 1;
1225 } else {
1226 scno = x86_64_regs.orig_rax;
1227 currpers = 0;
1228 if (scno & __X32_SYSCALL_BIT) {
1229 scno -= __X32_SYSCALL_BIT;
1230 currpers = 2;
1231 }
1232 }
1233# elif 0
1234 /* cs = 0x33 for long mode (native 64 bit and x32)
1235 * cs = 0x23 for compatibility mode (32 bit)
1236 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1237 */
1238 scno = x86_64_regs.orig_rax;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001239 switch (x86_64_regs.cs) {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001240 case 0x23: currpers = 1; break;
H.J. Lu35be5812012-04-16 13:00:01 +02001241 case 0x33:
1242 if (x86_64_regs.ds == 0x2b) {
1243 currpers = 2;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001244 scno &= ~__X32_SYSCALL_BIT;
H.J. Lu35be5812012-04-16 13:00:01 +02001245 } else
1246 currpers = 0;
1247 break;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001248 default:
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001249 fprintf(stderr, "Unknown value CS=0x%08X while "
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001250 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001251 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001252 currpers = current_personality;
1253 break;
1254 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001255# elif 0
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001256 /* This version analyzes the opcode of a syscall instruction.
1257 * (int 0x80 on i386 vs. syscall on x86-64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001258 * It works, but is too complicated, and strictly speaking, unreliable.
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001259 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001260 unsigned long call, rip = x86_64_regs.rip;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001261 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1262 rip -= 2;
1263 errno = 0;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001264 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001265 if (errno)
1266 fprintf(stderr, "ptrace_peektext failed: %s\n",
1267 strerror(errno));
1268 switch (call & 0xffff) {
1269 /* x86-64: syscall = 0x0f 0x05 */
1270 case 0x050f: currpers = 0; break;
1271 /* i386: int 0x80 = 0xcd 0x80 */
1272 case 0x80cd: currpers = 1; break;
1273 default:
1274 currpers = current_personality;
1275 fprintf(stderr,
1276 "Unknown syscall opcode (0x%04X) while "
1277 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001278 "PID=%d\n", (int)call, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001279 break;
1280 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001281# endif
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001282
H.J. Lu35be5812012-04-16 13:00:01 +02001283# ifdef X32
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001284 /* If we are built for a x32 system, then personality 0 is x32
1285 * (not x86_64), and stracing of x86_64 apps is not supported.
1286 * Stracing of i386 apps is still supported.
H.J. Lu085e4282012-04-17 11:05:04 -07001287 */
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001288 if (currpers == 0) {
1289 fprintf(stderr, "syscall_%lu(...) in unsupported "
1290 "64-bit mode of process PID=%d\n",
1291 scno, tcp->pid);
1292 return 0;
H.J. Lu35be5812012-04-16 13:00:01 +02001293 }
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001294 currpers &= ~2; /* map 2,1 to 0,1 */
H.J. Lu35be5812012-04-16 13:00:01 +02001295# endif
H.J. Lu085e4282012-04-17 11:05:04 -07001296 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001297#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001298# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01001299 long psr;
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001300 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001301 ia32 = (psr & IA64_PSR_IS) != 0;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001302 if (ia32) {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001303 if (upeek(tcp, PT_R1, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001304 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001305 } else {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001306 if (upeek(tcp, PT_R15, &scno) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001307 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001308 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001309#elif defined(AARCH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001310 switch (aarch64_io.iov_len) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001311 case sizeof(aarch64_regs):
1312 /* We are in 64-bit mode */
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001313 scno = aarch64_regs.regs[8];
1314 update_personality(tcp, 1);
1315 break;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001316 case sizeof(arm_regs):
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001317 /* We are in 32-bit mode */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001318 scno = arm_regs.ARM_r7;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001319 update_personality(tcp, 0);
1320 break;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001321 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001322#elif defined(ARM)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001323 if (arm_regs.ARM_ip != 0) {
1324 /* It is not a syscall entry */
1325 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001326 tcp->flags |= TCB_INSYSCALL;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001327 return 0;
1328 }
1329 /* Note: we support only 32-bit CPUs, not 26-bit */
1330
1331 if (arm_regs.ARM_cpsr & 0x20) {
1332 /* Thumb mode */
1333 scno = arm_regs.ARM_r7;
1334 } else {
1335 /* ARM mode */
1336 errno = 0;
1337 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1338 if (errno)
1339 return -1;
1340
1341 /* EABI syscall convention? */
1342 if (scno == 0xef000000) {
1343 scno = arm_regs.ARM_r7; /* yes */
1344 } else {
1345 if ((scno & 0x0ff00000) != 0x0f900000) {
1346 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1347 tcp->pid, scno);
1348 return -1;
1349 }
1350 /* Fixup the syscall number */
1351 scno &= 0x000fffff;
1352 }
1353 }
Denys Vlasenko7270de52013-02-21 15:46:34 +01001354
1355 scno = shuffle_scno(scno);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001356#elif defined(M68K)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001357 if (upeek(tcp, 4*PT_ORIG_D0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001358 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001359#elif defined(LINUX_MIPSN32)
Roland McGrath542c2c62008-05-20 01:11:56 +00001360 unsigned long long regs[38];
1361
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001362 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001363 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001364 mips_a3 = regs[REG_A3];
1365 mips_r2 = regs[REG_V0];
Roland McGrath542c2c62008-05-20 01:11:56 +00001366
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001367 scno = mips_r2;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001368 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001369 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001370 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001371 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001372 return 0;
1373 }
Roland McGrath542c2c62008-05-20 01:11:56 +00001374 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001375#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001376 if (upeek(tcp, REG_A3, &mips_a3) < 0)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001377 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001378 if (upeek(tcp, REG_V0, &scno) < 0)
1379 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001380
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001381 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001382 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001383 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001384 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001385 return 0;
1386 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001387 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001388#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001389 if (upeek(tcp, REG_A3, &alpha_a3) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001390 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001391 if (upeek(tcp, REG_R0, &scno) < 0)
1392 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001393
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001394 /*
1395 * Do some sanity checks to figure out if it's
1396 * really a syscall entry
1397 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001398 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001399 if (alpha_a3 == 0 || alpha_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001400 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001401 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001402 return 0;
1403 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001404 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001405#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001406 /* Disassemble the syscall trap. */
1407 /* Retrieve the syscall trap instruction. */
Denys Vlasenko46455822013-02-05 17:02:59 +01001408 unsigned long trap;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001409 errno = 0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001410# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001411 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001412 trap >>= 32;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001413# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001414 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001415# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001416 if (errno)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001417 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001418
1419 /* Disassemble the trap to see what personality to use. */
1420 switch (trap) {
1421 case 0x91d02010:
1422 /* Linux/SPARC syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001423 update_personality(tcp, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001424 break;
1425 case 0x91d0206d:
1426 /* Linux/SPARC64 syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001427 update_personality(tcp, 2);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001428 break;
1429 case 0x91d02000:
1430 /* SunOS syscall trap. (pers 1) */
1431 fprintf(stderr, "syscall: SunOS no support\n");
1432 return -1;
1433 case 0x91d02008:
1434 /* Solaris 2.x syscall trap. (per 2) */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001435 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001436 break;
1437 case 0x91d02009:
1438 /* NetBSD/FreeBSD syscall trap. */
1439 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1440 return -1;
1441 case 0x91d02027:
1442 /* Solaris 2.x gettimeofday */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001443 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001444 break;
1445 default:
Denys Vlasenko523635f2012-02-25 02:44:25 +01001446# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001447 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001448# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001449 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001450# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001451 return -1;
1452 }
1453
1454 /* Extract the system call number from the registers. */
1455 if (trap == 0x91d02027)
1456 scno = 156;
1457 else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001458 scno = sparc_regs.u_regs[U_REG_G1];
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001459 if (scno == 0) {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001460 scno = sparc_regs.u_regs[U_REG_O0];
1461 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 +02001462 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001463#elif defined(HPPA)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001464 if (upeek(tcp, PT_GR20, &scno) < 0)
1465 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001466#elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001467 /*
1468 * In the new syscall ABI, the system call number is in R3.
1469 */
1470 if (upeek(tcp, 4*(REG_REG0+3), &scno) < 0)
1471 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001472
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001473 if (scno < 0) {
1474 /* Odd as it may seem, a glibc bug has been known to cause
1475 glibc to issue bogus negative syscall numbers. So for
1476 our purposes, make strace print what it *should* have been */
1477 long correct_scno = (scno & 0xff);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001478 if (debug_flag)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001479 fprintf(stderr,
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001480 "Detected glibc bug: bogus system call"
1481 " number = %ld, correcting to %ld\n",
1482 scno,
1483 correct_scno);
1484 scno = correct_scno;
1485 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001486#elif defined(SH64)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001487 if (upeek(tcp, REG_SYSCALL, &scno) < 0)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001488 return -1;
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001489 scno &= 0xFFFF;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001490#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001491 if (upeek(tcp, 4*PT_R9, &scno) < 0)
1492 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001493#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001494 int currpers;
1495 scno = tile_regs.regs[10];
1496# ifdef __tilepro__
1497 currpers = 1;
1498# else
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001499# ifndef PT_FLAGS_COMPAT
1500# define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1501# endif
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001502 if (tile_regs.flags & PT_FLAGS_COMPAT)
1503 currpers = 1;
1504 else
1505 currpers = 0;
1506# endif
1507 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001508#elif defined(MICROBLAZE)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001509 if (upeek(tcp, 0, &scno) < 0)
1510 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001511#elif defined(OR1K)
1512 scno = or1k_regs.gpr[11];
James Hogan5f999a82013-02-22 14:44:10 +00001513#elif defined(METAG)
1514 scno = metag_regs.dx[0][1]; /* syscall number in D1Re0 (D1.0) */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001515#endif
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001516
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001517 tcp->scno = scno;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001518 if (SCNO_IS_VALID(tcp->scno)) {
1519 tcp->s_ent = &sysent[scno];
1520 tcp->qual_flg = qual_flags[scno];
1521 } else {
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01001522 static const struct_sysent unknown = {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001523 .nargs = MAX_ARGS,
1524 .sys_flags = 0,
1525 .sys_func = printargs,
1526 .sys_name = "unknown", /* not used */
1527 };
1528 tcp->s_ent = &unknown;
1529 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1530 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001531 return 1;
1532}
1533
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02001534/* Called at each syscall entry.
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001535 * Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001536 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1537 * 1: ok, continue in trace_syscall_entering().
1538 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001539 * ("????" etc) and bail out.
1540 */
Roland McGratha4d48532005-06-08 20:45:28 +00001541static int
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001542syscall_fixup_on_sysenter(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001543{
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001544 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001545#if defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001546 if (i386_regs.eax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001547 if (debug_flag)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001548 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1549 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001550 }
H.J. Lu35be5812012-04-16 13:00:01 +02001551#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001552 {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001553 long rax;
1554 if (x86_io.iov_len == sizeof(i386_regs)) {
1555 /* Sign extend from 32 bits */
1556 rax = (int32_t)i386_regs.eax;
1557 } else {
1558 /* Note: in X32 build, this truncates 64 to 32 bits */
1559 rax = x86_64_regs.rax;
1560 }
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001561 if (rax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001562 if (debug_flag)
Denys Vlasenko18beb982011-08-24 16:59:23 +02001563 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1564 return 0;
1565 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001566 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001567#elif defined(S390) || defined(S390X)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001568 /* TODO: we already fetched PT_GPR2 in get_scno
1569 * and stored it in syscall_mode, reuse it here
1570 * instead of re-fetching?
1571 */
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001572 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001573 return -1;
Michal Ludvig882eda82002-11-11 12:50:47 +00001574 if (syscall_mode != -ENOSYS)
1575 syscall_mode = tcp->scno;
Denys Vlasenkoece98792011-08-25 10:25:35 +02001576 if (gpr2 != syscall_mode) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001577 if (debug_flag)
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001578 fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2);
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001579 return 0;
1580 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001581#elif defined(M68K)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001582 /* TODO? Eliminate upeek's in arches below like we did in x86 */
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001583 if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001584 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001585 if (m68k_d0 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001586 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001587 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001588 return 0;
1589 }
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001590#elif defined(IA64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001591 if (upeek(tcp, PT_R10, &ia64_r10) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001592 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001593 if (upeek(tcp, PT_R8, &ia64_r8) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001594 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001595 if (ia32 && ia64_r8 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001596 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001597 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001598 return 0;
1599 }
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001600#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001601 if (upeek(tcp, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001602 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001603 if (cris_r10 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001604 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001605 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001606 return 0;
1607 }
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001608#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001609 if (upeek(tcp, 3 * 4, &microblaze_r3) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001610 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001611 if (microblaze_r3 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001612 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001613 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001614 return 0;
1615 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001616#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001617 return 1;
1618}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001619
Denys Vlasenko146b9442012-03-18 22:10:48 +01001620static void
1621internal_fork(struct tcb *tcp)
1622{
1623#if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1624# define ARG_FLAGS 1
1625#else
1626# define ARG_FLAGS 0
1627#endif
1628#ifndef CLONE_UNTRACED
1629# define CLONE_UNTRACED 0x00800000
1630#endif
1631 if ((ptrace_setoptions
1632 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1633 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1634 return;
1635
1636 if (!followfork)
1637 return;
1638
1639 if (entering(tcp)) {
1640 /*
1641 * We won't see the new child if clone is called with
1642 * CLONE_UNTRACED, so we keep the same logic with that option
1643 * and don't trace it.
1644 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001645 if ((tcp->s_ent->sys_func == sys_clone)
1646 && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
1647 )
Denys Vlasenko146b9442012-03-18 22:10:48 +01001648 return;
1649 setbpt(tcp);
1650 } else {
1651 if (tcp->flags & TCB_BPTSET)
1652 clearbpt(tcp);
1653 }
1654}
1655
1656#if defined(TCB_WAITEXECVE)
1657static void
1658internal_exec(struct tcb *tcp)
1659{
1660 /* Maybe we have post-execve SIGTRAP suppressed? */
1661 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1662 return; /* yes, no need to do anything */
1663
1664 if (exiting(tcp) && syserror(tcp))
1665 /* Error in execve, no post-execve SIGTRAP expected */
1666 tcp->flags &= ~TCB_WAITEXECVE;
1667 else
1668 tcp->flags |= TCB_WAITEXECVE;
1669}
1670#endif
1671
1672static void
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001673syscall_fixup_for_fork_exec(struct tcb *tcp)
Roland McGrathc1e45922008-05-27 23:18:29 +00001674{
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001675 /*
1676 * We must always trace a few critical system calls in order to
1677 * correctly support following forks in the presence of tracing
1678 * qualifiers.
1679 */
1680 int (*func)();
1681
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001682 func = tcp->s_ent->sys_func;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001683
1684 if ( sys_fork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001685 || sys_vfork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001686 || sys_clone == func
Denys Vlasenko146b9442012-03-18 22:10:48 +01001687 ) {
1688 internal_fork(tcp);
1689 return;
1690 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001691
Denys Vlasenko84703742012-02-25 02:38:52 +01001692#if defined(TCB_WAITEXECVE)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001693 if ( sys_execve == func
Denys Vlasenko84703742012-02-25 02:38:52 +01001694# if defined(SPARC) || defined(SPARC64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001695 || sys_execv == func
Denys Vlasenkoa7949742011-08-21 17:26:55 +02001696# endif
Denys Vlasenko146b9442012-03-18 22:10:48 +01001697 ) {
1698 internal_exec(tcp);
1699 return;
1700 }
Roland McGrathc1e45922008-05-27 23:18:29 +00001701#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001702}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001703
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001704/* Return -1 on error or 1 on success (never 0!) */
Roland McGratha4d48532005-06-08 20:45:28 +00001705static int
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001706get_syscall_args(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001707{
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001708 int i, nargs;
1709
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001710 nargs = tcp->s_ent->nargs;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001711
Denys Vlasenko523635f2012-02-25 02:44:25 +01001712#if defined(S390) || defined(S390X)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001713 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001714 if (upeek(tcp, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
1715 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001716#elif defined(ALPHA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001717 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001718 if (upeek(tcp, REG_A0+i, &tcp->u_arg[i]) < 0)
1719 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001720#elif defined(IA64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001721 if (!ia32) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001722 unsigned long *out0, cfm, sof, sol;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001723 long rbs_end;
1724 /* be backwards compatible with kernel < 2.4.4... */
1725# ifndef PT_RBS_END
1726# define PT_RBS_END PT_AR_BSP
1727# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001728
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001729 if (upeek(tcp, PT_RBS_END, &rbs_end) < 0)
1730 return -1;
1731 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001732 return -1;
1733
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001734 sof = (cfm >> 0) & 0x7f;
1735 sol = (cfm >> 7) & 0x7f;
1736 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1737
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001738 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001739 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1740 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1741 return -1;
1742 }
1743 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001744 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1745 PT_R9 /* ECX = out1 */,
1746 PT_R10 /* EDX = out2 */,
1747 PT_R14 /* ESI = out3 */,
1748 PT_R15 /* EDI = out4 */,
1749 PT_R13 /* EBP = out5 */};
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001750
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001751 for (i = 0; i < nargs; ++i) {
1752 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1753 return -1;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001754 /* truncate away IVE sign-extension */
1755 tcp->u_arg[i] &= 0xffffffff;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001756 }
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001757 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001758#elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001759 /* N32 and N64 both use up to six registers. */
1760 unsigned long long regs[38];
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001761
1762 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1763 return -1;
1764
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001765 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001766 tcp->u_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001767# if defined(LINUX_MIPSN32)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001768 tcp->ext_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001769# endif
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001770 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001771#elif defined(MIPS)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001772 if (nargs > 4) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001773 long sp;
1774
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001775 if (upeek(tcp, REG_SP, &sp) < 0)
1776 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001777 for (i = 0; i < 4; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001778 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1779 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001780 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001781 (char *)(tcp->u_arg + 4));
1782 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001783 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001784 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001785 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001786 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001787#elif defined(POWERPC)
1788# ifndef PT_ORIG_R3
1789# define PT_ORIG_R3 34
1790# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001791 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001792 if (upeek(tcp, (i==0) ?
1793 (sizeof(unsigned long) * PT_ORIG_R3) :
1794 ((i+PT_R3) * sizeof(unsigned long)),
1795 &tcp->u_arg[i]) < 0)
1796 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001797 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001798#elif defined(SPARC) || defined(SPARC64)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001799 for (i = 0; i < nargs; ++i)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001800 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001801#elif defined(HPPA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001802 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001803 if (upeek(tcp, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1804 return -1;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001805#elif defined(ARM) || defined(AARCH64)
1806# if defined(AARCH64)
1807 if (tcp->currpers == 1)
1808 for (i = 0; i < nargs; ++i)
1809 tcp->u_arg[i] = aarch64_regs.regs[i];
1810 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001811# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001812 for (i = 0; i < nargs; ++i)
Denys Vlasenko401374e2013-02-06 18:24:39 +01001813 tcp->u_arg[i] = arm_regs.uregs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001814#elif defined(AVR32)
Denys Vlasenkob5b25892011-08-30 19:04:54 +02001815 (void)i;
1816 (void)nargs;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001817 tcp->u_arg[0] = avr32_regs.r12;
1818 tcp->u_arg[1] = avr32_regs.r11;
1819 tcp->u_arg[2] = avr32_regs.r10;
1820 tcp->u_arg[3] = avr32_regs.r9;
1821 tcp->u_arg[4] = avr32_regs.r5;
1822 tcp->u_arg[5] = avr32_regs.r3;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001823#elif defined(BFIN)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001824 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 +02001825
Denys Vlasenko4b887a52011-08-23 13:32:38 +02001826 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001827 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1828 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001829#elif defined(SH)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001830 static const int syscall_regs[MAX_ARGS] = {
1831 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1832 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001833 };
1834
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001835 for (i = 0; i < nargs; ++i)
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001836 if (upeek(tcp, syscall_regs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001837 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001838#elif defined(SH64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001839 int i;
1840 /* Registers used by SH5 Linux system calls for parameters */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001841 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001842
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001843 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001844 if (upeek(tcp, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1845 return -1;
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001846#elif defined(I386)
1847 (void)i;
1848 (void)nargs;
1849 tcp->u_arg[0] = i386_regs.ebx;
1850 tcp->u_arg[1] = i386_regs.ecx;
1851 tcp->u_arg[2] = i386_regs.edx;
1852 tcp->u_arg[3] = i386_regs.esi;
1853 tcp->u_arg[4] = i386_regs.edi;
1854 tcp->u_arg[5] = i386_regs.ebp;
H.J. Lu35be5812012-04-16 13:00:01 +02001855#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001856 (void)i;
1857 (void)nargs;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001858 if (x86_io.iov_len != sizeof(i386_regs)) {
1859 /* x86-64 or x32 ABI */
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001860 tcp->u_arg[0] = x86_64_regs.rdi;
1861 tcp->u_arg[1] = x86_64_regs.rsi;
1862 tcp->u_arg[2] = x86_64_regs.rdx;
1863 tcp->u_arg[3] = x86_64_regs.r10;
1864 tcp->u_arg[4] = x86_64_regs.r8;
1865 tcp->u_arg[5] = x86_64_regs.r9;
H.J. Lu35be5812012-04-16 13:00:01 +02001866# ifdef X32
1867 tcp->ext_arg[0] = x86_64_regs.rdi;
1868 tcp->ext_arg[1] = x86_64_regs.rsi;
1869 tcp->ext_arg[2] = x86_64_regs.rdx;
1870 tcp->ext_arg[3] = x86_64_regs.r10;
1871 tcp->ext_arg[4] = x86_64_regs.r8;
1872 tcp->ext_arg[5] = x86_64_regs.r9;
1873# endif
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001874 } else {
1875 /* i386 ABI */
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001876 /* Zero-extend from 32 bits */
1877 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1878 * if you need to use *sign-extended* parameter.
1879 */
1880 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1881 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1882 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1883 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1884 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1885 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001886 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001887#elif defined(MICROBLAZE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001888 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001889 if (upeek(tcp, (5 + i) * 4, &tcp->u_arg[i]) < 0)
1890 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001891#elif defined(CRISV10) || defined(CRISV32)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001892 static const int crisregs[MAX_ARGS] = {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001893 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001894 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001895 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001896
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001897 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001898 if (upeek(tcp, crisregs[i], &tcp->u_arg[i]) < 0)
1899 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001900#elif defined(TILE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001901 for (i = 0; i < nargs; ++i)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001902 tcp->u_arg[i] = tile_regs.regs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001903#elif defined(M68K)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001904 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001905 if (upeek(tcp, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
1906 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001907#elif defined(OR1K)
1908 (void)nargs;
1909 for (i = 0; i < 6; ++i)
1910 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
James Hogan5f999a82013-02-22 14:44:10 +00001911#elif defined(METAG)
1912 for (i = 0; i < nargs; i++)
1913 /* arguments go backwards from D1Ar1 (D1.3) */
1914 tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001915#else /* Other architecture (32bits specific) */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001916 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001917 if (upeek(tcp, i*4, &tcp->u_arg[i]) < 0)
1918 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001919#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001920 return 1;
1921}
1922
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00001923static int
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001924trace_syscall_entering(struct tcb *tcp)
1925{
1926 int res, scno_good;
1927
Denys Vlasenko2ce12ed2011-08-24 17:25:32 +02001928#if defined TCB_WAITEXECVE
1929 if (tcp->flags & TCB_WAITEXECVE) {
1930 /* This is the post-execve SIGTRAP. */
1931 tcp->flags &= ~TCB_WAITEXECVE;
1932 return 0;
1933 }
1934#endif
1935
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001936 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001937 if (res == 0)
1938 return res;
Denys Vlasenko907735a2012-03-21 00:23:16 +01001939 if (res == 1) {
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001940 res = syscall_fixup_on_sysenter(tcp);
Denys Vlasenko907735a2012-03-21 00:23:16 +01001941 if (res == 0)
1942 return res;
1943 if (res == 1)
1944 res = get_syscall_args(tcp);
1945 }
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001946
1947 if (res != 1) {
1948 printleader(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001949 if (scno_good != 1)
Denys Vlasenkob7a6dae2012-03-20 16:48:35 +01001950 tprints("????" /* anti-trigraph gap */ "(");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001951 else if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01001952 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001953 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001954 tprintf("%s(", tcp->s_ent->sys_name);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001955 /*
1956 * " <unavailable>" will be added later by the code which
1957 * detects ptrace errors.
1958 */
1959 goto ret;
1960 }
1961
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001962#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001963 while (1) {
Denys Vlasenko523635f2012-02-25 02:44:25 +01001964# ifdef SYS_socket_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001965 if (tcp->s_ent->sys_func == sys_socketcall) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00001966 decode_socket_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001967 break;
1968 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001969# endif
1970# ifdef SYS_ipc_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001971 if (tcp->s_ent->sys_func == sys_ipc) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00001972 decode_ipc_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001973 break;
1974 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001975# endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001976 break;
1977 }
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001978#endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001979
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001980 if (need_fork_exec_workarounds)
1981 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001982
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001983 if (!(tcp->qual_flg & QUAL_TRACE)
1984 || (tracing_paths && !pathtrace_match(tcp))
1985 ) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001986 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
1987 return 0;
1988 }
1989
1990 tcp->flags &= ~TCB_FILTERED;
1991
1992 if (cflag == CFLAG_ONLY_STATS) {
1993 res = 0;
1994 goto ret;
1995 }
1996
1997 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001998 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01001999 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002000 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002001 tprintf("%s(", tcp->s_ent->sys_name);
2002 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002003 res = printargs(tcp);
2004 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002005 res = tcp->s_ent->sys_func(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002006
Dmitry V. Levinb742d8c2012-09-17 22:40:12 +00002007 fflush(tcp->outf);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002008 ret:
2009 tcp->flags |= TCB_INSYSCALL;
2010 /* Measure the entrance time as late as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002011 if (Tflag || cflag)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002012 gettimeofday(&tcp->etime, NULL);
2013 return res;
2014}
2015
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002016/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01002017 * 1: ok, continue in trace_syscall_exiting().
2018 * -1: error, trace_syscall_exiting() should print error indicator
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002019 * ("????" etc) and bail out.
2020 */
2021static int
2022get_syscall_result(struct tcb *tcp)
2023{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002024#if defined(S390) || defined(S390X)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002025 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
2026 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002027#elif defined(POWERPC)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002028# define SO_MASK 0x10000000
2029 {
2030 long flags;
2031 if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0)
2032 return -1;
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002033 if (upeek(tcp, sizeof(unsigned long)*PT_R3, &ppc_result) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002034 return -1;
2035 if (flags & SO_MASK)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002036 ppc_result = -ppc_result;
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002037 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002038#elif defined(AVR32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002039 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002040#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002041 if (upeek(tcp, PT_R0, &bfin_r0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002042 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002043#elif defined(I386)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002044 /* already done by get_regs */
H.J. Lu35be5812012-04-16 13:00:01 +02002045#elif defined(X86_64) || defined(X32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002046 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002047#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002048# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01002049 long psr;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002050 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
2051 ia32 = (psr & IA64_PSR_IS) != 0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002052 if (upeek(tcp, PT_R8, &ia64_r8) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002053 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002054 if (upeek(tcp, PT_R10, &ia64_r10) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002055 return -1;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002056#elif defined(ARM)
2057 /* already done by get_regs */
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002058#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002059 /* register reading already done by get_regs */
2060
2061 /* Used to do this, but we did it on syscall entry already: */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002062 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
2063 * else it's personality 0.
2064 */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002065 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
Denys Vlasenko523635f2012-02-25 02:44:25 +01002066#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002067 if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002068 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002069#elif defined(LINUX_MIPSN32)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002070 unsigned long long regs[38];
2071
2072 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
2073 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002074 mips_a3 = regs[REG_A3];
2075 mips_r2 = regs[REG_V0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01002076#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002077 if (upeek(tcp, REG_A3, &mips_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002078 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002079 if (upeek(tcp, REG_V0, &mips_r2) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002080 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002081#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002082 if (upeek(tcp, REG_A3, &alpha_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002083 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002084 if (upeek(tcp, REG_R0, &alpha_r0) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002085 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002086#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002087 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002088#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002089 if (upeek(tcp, PT_GR28, &hppa_r28) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002090 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002091#elif defined(SH)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002092 /* new syscall ABI returns result in R0 */
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002093 if (upeek(tcp, 4*REG_REG0, (long *)&sh_r0) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002094 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002095#elif defined(SH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002096 /* ABI defines result returned in r9 */
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002097 if (upeek(tcp, REG_GENERAL(9), (long *)&sh64_r9) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002098 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002099#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002100 if (upeek(tcp, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002101 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002102#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002103 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002104#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002105 if (upeek(tcp, 3 * 4, &microblaze_r3) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002106 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01002107#elif defined(OR1K)
2108 /* already done by get_regs */
James Hogan5f999a82013-02-22 14:44:10 +00002109#elif defined(METAG)
2110 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002111#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002112 return 1;
2113}
2114
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002115/* Called at each syscall exit */
2116static void
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002117syscall_fixup_on_sysexit(struct tcb *tcp)
2118{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002119#if defined(S390) || defined(S390X)
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002120 if (syscall_mode != -ENOSYS)
2121 syscall_mode = tcp->scno;
Denys Vlasenkoece98792011-08-25 10:25:35 +02002122 if ((tcp->flags & TCB_WAITEXECVE)
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002123 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
2124 /*
2125 * Return from execve.
2126 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2127 * flag set for the post-execve SIGTRAP to see and reset.
2128 */
2129 gpr2 = 0;
2130 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002131#endif
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002132}
2133
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002134/*
2135 * Check the syscall return value register value for whether it is
2136 * a negated errno code indicating an error, or a success return value.
2137 */
2138static inline int
2139is_negated_errno(unsigned long int val)
2140{
2141 unsigned long int max = -(long int) nerrnos;
Denys Vlasenko2544f982013-02-19 17:39:56 +01002142#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01002143 if (current_wordsize < sizeof(val)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002144 val = (unsigned int) val;
2145 max = (unsigned int) max;
2146 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002147#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002148 return val > max;
2149}
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002150
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002151#if defined(X32)
2152static inline int
2153is_negated_errno_x32(unsigned long long val)
2154{
2155 unsigned long long max = -(long long) nerrnos;
2156 /*
2157 * current_wordsize is 4 even in personality 0 (native X32)
2158 * but truncation _must not_ be done in it.
2159 * can't check current_wordsize here!
2160 */
2161 if (current_personality != 0) {
2162 val = (uint32_t) val;
2163 max = (uint32_t) max;
2164 }
2165 return val > max;
2166}
2167#endif
2168
Denys Vlasenko907735a2012-03-21 00:23:16 +01002169/* Returns:
2170 * 1: ok, continue in trace_syscall_exiting().
2171 * -1: error, trace_syscall_exiting() should print error indicator
2172 * ("????" etc) and bail out.
2173 */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002174static void
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002175get_error(struct tcb *tcp)
2176{
2177 int u_error = 0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002178 int check_errno = 1;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002179 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002180 check_errno = 0;
2181 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002182#if defined(S390) || defined(S390X)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002183 if (check_errno && is_negated_errno(gpr2)) {
2184 tcp->u_rval = -1;
2185 u_error = -gpr2;
2186 }
2187 else {
2188 tcp->u_rval = gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002189 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002190#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002191 if (check_errno && is_negated_errno(i386_regs.eax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002192 tcp->u_rval = -1;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002193 u_error = -i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002194 }
2195 else {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002196 tcp->u_rval = i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002197 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002198#elif defined(X86_64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002199 long rax;
2200 if (x86_io.iov_len == sizeof(i386_regs)) {
2201 /* Sign extend from 32 bits */
2202 rax = (int32_t)i386_regs.eax;
2203 } else {
2204 rax = x86_64_regs.rax;
2205 }
2206 if (check_errno && is_negated_errno(rax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002207 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002208 u_error = -rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002209 }
2210 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002211 tcp->u_rval = rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002212 }
2213#elif defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002214 /* In X32, return value is 64-bit (llseek uses one).
2215 * Using merely "long rax" would not work.
2216 */
2217 long long rax;
2218 if (x86_io.iov_len == sizeof(i386_regs)) {
2219 /* Sign extend from 32 bits */
2220 rax = (int32_t)i386_regs.eax;
2221 } else {
2222 rax = x86_64_regs.rax;
2223 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002224 /* Careful: is_negated_errno() works only on longs */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002225 if (check_errno && is_negated_errno_x32(rax)) {
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002226 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002227 u_error = -rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002228 }
2229 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002230 tcp->u_rval = rax; /* truncating */
2231 tcp->u_lrval = rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002232 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002233#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002234 if (ia32) {
2235 int err;
2236
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002237 err = (int)ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002238 if (check_errno && is_negated_errno(err)) {
2239 tcp->u_rval = -1;
2240 u_error = -err;
2241 }
2242 else {
2243 tcp->u_rval = err;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002244 }
2245 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002246 if (check_errno && ia64_r10) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002247 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002248 u_error = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002249 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002250 tcp->u_rval = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002251 }
2252 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002253#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002254 if (check_errno && mips_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002255 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002256 u_error = mips_r2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002257 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002258 tcp->u_rval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002259# if defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002260 tcp->u_lrval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002261# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002262 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002263#elif defined(POWERPC)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002264 if (check_errno && is_negated_errno(ppc_result)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002265 tcp->u_rval = -1;
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002266 u_error = -ppc_result;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002267 }
2268 else {
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002269 tcp->u_rval = ppc_result;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002270 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002271#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002272 if (check_errno && is_negated_errno(m68k_d0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002273 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002274 u_error = -m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002275 }
2276 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002277 tcp->u_rval = m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002278 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002279#elif defined(ARM) || defined(AARCH64)
2280# if defined(AARCH64)
2281 if (tcp->currpers == 1) {
2282 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2283 tcp->u_rval = -1;
2284 u_error = -aarch64_regs.regs[0];
2285 }
2286 else {
2287 tcp->u_rval = aarch64_regs.regs[0];
2288 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002289 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002290 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002291# endif
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002292 {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002293 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002294 tcp->u_rval = -1;
Denys Vlasenko401374e2013-02-06 18:24:39 +01002295 u_error = -arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002296 }
2297 else {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002298 tcp->u_rval = arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002299 }
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002300 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002301#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002302 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002303 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002304 u_error = -avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002305 }
2306 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002307 tcp->u_rval = avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002308 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002309#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002310 if (check_errno && is_negated_errno(bfin_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002311 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002312 u_error = -bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002313 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002314 tcp->u_rval = bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002315 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002316#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002317 if (check_errno && alpha_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002318 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002319 u_error = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002320 }
2321 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002322 tcp->u_rval = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002323 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002324#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002325 if (check_errno && sparc_regs.psr & PSR_C) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002326 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002327 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002328 }
2329 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002330 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002331 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002332#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002333 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002334 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002335 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002336 }
2337 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002338 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002339 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002340#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002341 if (check_errno && is_negated_errno(hppa_r28)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002342 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002343 u_error = -hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002344 }
2345 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002346 tcp->u_rval = hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002347 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002348#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002349 if (check_errno && is_negated_errno(sh_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002350 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002351 u_error = -sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002352 }
2353 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002354 tcp->u_rval = sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002355 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002356#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002357 if (check_errno && is_negated_errno(sh64_r9)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002358 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002359 u_error = -sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002360 }
2361 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002362 tcp->u_rval = sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002363 }
James Hogan5f999a82013-02-22 14:44:10 +00002364#elif defined(METAG)
2365 /* result pointer in D0Re0 (D0.0) */
2366 if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2367 tcp->u_rval = -1;
2368 u_error = -metag_regs.dx[0][0];
2369 }
2370 else {
2371 tcp->u_rval = metag_regs.dx[0][0];
2372 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002373#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002374 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002375 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002376 u_error = -cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002377 }
2378 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002379 tcp->u_rval = cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002380 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002381#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002382 /*
2383 * The standard tile calling convention returns the value (or negative
2384 * errno) in r0, and zero (or positive errno) in r1.
2385 * Until at least kernel 3.8, however, the r1 value is not reflected
2386 * in ptregs at this point, so we use r0 here.
2387 */
2388 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002389 tcp->u_rval = -1;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002390 u_error = -tile_regs.regs[0];
2391 } else {
2392 tcp->u_rval = tile_regs.regs[0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002393 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002394#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002395 if (check_errno && is_negated_errno(microblaze_r3)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002396 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002397 u_error = -microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002398 }
2399 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002400 tcp->u_rval = microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002401 }
Christian Svensson492f81f2013-02-14 13:26:27 +01002402#elif defined(OR1K)
2403 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2404 tcp->u_rval = -1;
2405 u_error = -or1k_regs.gpr[11];
2406 }
2407 else {
2408 tcp->u_rval = or1k_regs.gpr[11];
2409 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002410#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002411 tcp->u_error = u_error;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002412}
2413
2414static void
2415dumpio(struct tcb *tcp)
2416{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002417 int (*func)();
2418
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002419 if (syserror(tcp))
2420 return;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +01002421 if ((unsigned long) tcp->u_arg[0] >= num_quals)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002422 return;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002423 func = tcp->s_ent->sys_func;
2424 if (func == printargs)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002425 return;
2426 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002427 if (func == sys_read ||
2428 func == sys_pread ||
2429 func == sys_recv ||
2430 func == sys_recvfrom)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002431 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002432 else if (func == sys_readv)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002433 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2434 return;
2435 }
2436 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002437 if (func == sys_write ||
2438 func == sys_pwrite ||
2439 func == sys_send ||
2440 func == sys_sendto)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002441 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002442 else if (func == sys_writev)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002443 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2444 return;
2445 }
2446}
2447
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002448static int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002449trace_syscall_exiting(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002450{
2451 int sys_res;
2452 struct timeval tv;
Denys Vlasenko1a5b5a72011-08-25 00:29:56 +02002453 int res;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002454 long u_error;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002455
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002456 /* Measure the exit time as early as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002457 if (Tflag || cflag)
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002458 gettimeofday(&tv, NULL);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002459
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00002460#if SUPPORTED_PERSONALITIES > 1
2461 update_personality(tcp, tcp->currpers);
2462#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002463 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002464 if (res == 1) {
2465 syscall_fixup_on_sysexit(tcp); /* never fails */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002466 get_error(tcp); /* never fails */
2467 if (need_fork_exec_workarounds)
2468 syscall_fixup_for_fork_exec(tcp);
2469 if (filtered(tcp))
2470 goto ret;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002471 }
2472
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002473 if (cflag) {
2474 struct timeval t = tv;
Denys Vlasenkoc95a88f2011-08-21 17:47:40 +02002475 count_syscall(tcp, &t);
Denys Vlasenko7b609d52011-06-22 14:32:43 +02002476 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002477 goto ret;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002478 }
2479 }
2480
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002481 /* If not in -ff mode, and printing_tcp != tcp,
2482 * then the log currently does not end with output
2483 * of _our syscall entry_, but with something else.
2484 * We need to say which syscall's return is this.
2485 *
2486 * Forced reprinting via TCB_REPRINT is used only by
2487 * "strace -ff -oLOG test/threaded_execve" corner case.
2488 * It's the only case when -ff mode needs reprinting.
2489 */
2490 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2491 tcp->flags &= ~TCB_REPRINT;
2492 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002493 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002494 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002495 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002496 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002497 }
2498 printing_tcp = tcp;
2499
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002500 if (res != 1) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002501 /* There was error in one of prior ptrace ops */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002502 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002503 tabto();
Denys Vlasenko000b6012012-01-28 01:25:03 +01002504 tprints("= ? <unavailable>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002505 line_ended();
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002506 tcp->flags &= ~TCB_INSYSCALL;
2507 return res;
2508 }
2509
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002510 sys_res = 0;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002511 if (tcp->qual_flg & QUAL_RAW) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002512 /* sys_res = printargs(tcp); - but it's nop on sysexit */
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002513 } else {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002514 /* FIXME: not_failing_only (IOW, option -z) is broken:
2515 * failure of syscall is known only after syscall return.
2516 * Thus we end up with something like this on, say, ENOENT:
2517 * open("doesnt_exist", O_RDONLY <unfinished ...>
2518 * {next syscall decode}
2519 * whereas the intended result is that open(...) line
2520 * is not shown at all.
2521 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002522 if (not_failing_only && tcp->u_error)
Denys Vlasenko3b738812011-08-22 02:06:35 +02002523 goto ret; /* ignore failed syscalls */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002524 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002525 }
2526
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002527 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002528 tabto();
Denys Vlasenko3b738812011-08-22 02:06:35 +02002529 u_error = tcp->u_error;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002530 if (tcp->qual_flg & QUAL_RAW) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002531 if (u_error)
2532 tprintf("= -1 (errno %ld)", u_error);
2533 else
2534 tprintf("= %#lx", tcp->u_rval);
2535 }
2536 else if (!(sys_res & RVAL_NONE) && u_error) {
2537 switch (u_error) {
Denys Vlasenkofe585652012-01-12 11:26:34 +01002538 /* Blocked signals do not interrupt any syscalls.
2539 * In this case syscalls don't return ERESTARTfoo codes.
2540 *
2541 * Deadly signals set to SIG_DFL interrupt syscalls
2542 * and kill the process regardless of which of the codes below
2543 * is returned by the interrupted syscall.
2544 * In some cases, kernel forces a kernel-generated deadly
2545 * signal to be unblocked and set to SIG_DFL (and thus cause
2546 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2547 * or SIGILL. (The alternative is to leave process spinning
2548 * forever on the faulty instruction - not useful).
2549 *
2550 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2551 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2552 * but kernel will always restart them.
2553 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002554 case ERESTARTSYS:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002555 /* Most common type of signal-interrupted syscall exit code.
2556 * The system call will be restarted with the same arguments
2557 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2558 */
2559 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002560 break;
2561 case ERESTARTNOINTR:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002562 /* Rare. For example, fork() returns this if interrupted.
2563 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2564 */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002565 tprints("= ? ERESTARTNOINTR (To be restarted)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002566 break;
2567 case ERESTARTNOHAND:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002568 /* pause(), rt_sigsuspend() etc use this code.
2569 * SA_RESTART is ignored (assumed not set):
2570 * syscall won't restart (will return EINTR instead)
Denys Vlasenko30c03232013-02-19 16:59:26 +01002571 * even after signal with SA_RESTART set. However,
2572 * after SIG_IGN or SIG_DFL signal it will restart
2573 * (thus the name "restart only if has no handler").
Denys Vlasenkofe585652012-01-12 11:26:34 +01002574 */
Denys Vlasenkoaba62922013-03-05 16:56:35 +01002575 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002576 break;
2577 case ERESTART_RESTARTBLOCK:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002578 /* Syscalls like nanosleep(), poll() which can't be
2579 * restarted with their original arguments use this
2580 * code. Kernel will execute restart_syscall() instead,
2581 * which changes arguments before restarting syscall.
2582 * SA_RESTART is ignored (assumed not set) similarly
2583 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2584 * since restart data is saved in "restart block"
2585 * in task struct, and if signal handler uses a syscall
2586 * which in turn saves another such restart block,
2587 * old data is lost and restart becomes impossible)
2588 */
2589 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002590 break;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002591 default:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002592 if (u_error < 0)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002593 tprintf("= -1 E??? (errno %ld)", u_error);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002594 else if (u_error < nerrnos)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002595 tprintf("= -1 %s (%s)", errnoent[u_error],
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002596 strerror(u_error));
2597 else
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002598 tprintf("= -1 ERRNO_%ld (%s)", u_error,
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002599 strerror(u_error));
2600 break;
2601 }
2602 if ((sys_res & RVAL_STR) && tcp->auxstr)
2603 tprintf(" (%s)", tcp->auxstr);
2604 }
2605 else {
2606 if (sys_res & RVAL_NONE)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002607 tprints("= ?");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002608 else {
2609 switch (sys_res & RVAL_MASK) {
2610 case RVAL_HEX:
2611 tprintf("= %#lx", tcp->u_rval);
2612 break;
2613 case RVAL_OCTAL:
2614 tprintf("= %#lo", tcp->u_rval);
2615 break;
2616 case RVAL_UDECIMAL:
2617 tprintf("= %lu", tcp->u_rval);
2618 break;
2619 case RVAL_DECIMAL:
2620 tprintf("= %ld", tcp->u_rval);
2621 break;
H.J. Ludd0130b2012-04-16 12:16:45 +02002622#if defined(LINUX_MIPSN32) || defined(X32)
2623 /*
2624 case RVAL_LHEX:
2625 tprintf("= %#llx", tcp->u_lrval);
2626 break;
2627 case RVAL_LOCTAL:
2628 tprintf("= %#llo", tcp->u_lrval);
2629 break;
2630 */
2631 case RVAL_LUDECIMAL:
2632 tprintf("= %llu", tcp->u_lrval);
2633 break;
2634 /*
2635 case RVAL_LDECIMAL:
2636 tprintf("= %lld", tcp->u_lrval);
2637 break;
2638 */
2639#endif
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002640 default:
2641 fprintf(stderr,
2642 "invalid rval format\n");
2643 break;
2644 }
2645 }
2646 if ((sys_res & RVAL_STR) && tcp->auxstr)
2647 tprintf(" (%s)", tcp->auxstr);
2648 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002649 if (Tflag) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002650 tv_sub(&tv, &tv, &tcp->etime);
2651 tprintf(" <%ld.%06ld>",
2652 (long) tv.tv_sec, (long) tv.tv_usec);
2653 }
Denys Vlasenko000b6012012-01-28 01:25:03 +01002654 tprints("\n");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002655 dumpio(tcp);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002656 line_ended();
2657
Denys Vlasenko3b738812011-08-22 02:06:35 +02002658 ret:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002659 tcp->flags &= ~TCB_INSYSCALL;
2660 return 0;
2661}
2662
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002663int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002664trace_syscall(struct tcb *tcp)
2665{
2666 return exiting(tcp) ?
2667 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2668}