blob: ef14a0afa04b2554f2d830cecec17c54871b1d30 [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00005 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Linux for s390 port by D.J. Barrow
8 * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00009 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000032 */
33
34#include "defs.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000035#include <sys/user.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000036#include <sys/param.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000037
Wichert Akkerman15dea971999-10-06 13:06:34 +000038#ifdef HAVE_SYS_REG_H
Denys Vlasenko523635f2012-02-25 02:44:25 +010039# include <sys/reg.h>
40# ifndef PTRACE_PEEKUSR
41# define PTRACE_PEEKUSR PTRACE_PEEKUSER
42# endif
Wichert Akkermanfaf72222000-02-19 23:59:03 +000043#elif defined(HAVE_LINUX_PTRACE_H)
Denys Vlasenko523635f2012-02-25 02:44:25 +010044# undef PTRACE_SYSCALL
Roland McGrathce9f0742004-03-01 21:29:22 +000045# ifdef HAVE_STRUCT_IA64_FPREG
46# define ia64_fpreg XXX_ia64_fpreg
47# endif
48# ifdef HAVE_STRUCT_PT_ALL_USER_REGS
49# define pt_all_user_regs XXX_pt_all_user_regs
50# endif
Denys Vlasenko523635f2012-02-25 02:44:25 +010051# include <linux/ptrace.h>
Roland McGrathce9f0742004-03-01 21:29:22 +000052# undef ia64_fpreg
53# undef pt_all_user_regs
Wichert Akkerman15dea971999-10-06 13:06:34 +000054#endif
55
Denys Vlasenko84703742012-02-25 02:38:52 +010056#if defined(SPARC64)
Roland McGrath6d1a65c2004-07-12 07:44:08 +000057# undef PTRACE_GETREGS
58# define PTRACE_GETREGS PTRACE_GETREGS64
59# undef PTRACE_SETREGS
60# define PTRACE_SETREGS PTRACE_SETREGS64
Denys Vlasenko84703742012-02-25 02:38:52 +010061#endif
Roland McGrath6d1a65c2004-07-12 07:44:08 +000062
Denys Vlasenko84703742012-02-25 02:38:52 +010063#if defined(IA64)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000064# include <asm/ptrace_offsets.h>
65# include <asm/rse.h>
66#endif
67
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010068#if defined(X86_64) || defined(X32)
69# include <linux/ptrace.h>
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010070# include <sys/uio.h>
71# include <elf.h>
72#endif
73
Steve McIntyred8d3bd32012-10-24 17:58:16 +010074#if defined(AARCH64)
75# include <asm/ptrace.h>
76# include <sys/uio.h>
77# include <elf.h>
78#endif
79
Christian Svensson492f81f2013-02-14 13:26:27 +010080#if defined(OR1K)
81# include <sys/uio.h>
82# include <elf.h>
83#endif
84
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000085#ifndef ERESTARTSYS
Denys Vlasenko523635f2012-02-25 02:44:25 +010086# define ERESTARTSYS 512
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000087#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010088#ifndef ERESTARTNOINTR
Denys Vlasenko523635f2012-02-25 02:44:25 +010089# define ERESTARTNOINTR 513
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000090#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010091#ifndef ERESTARTNOHAND
92# define ERESTARTNOHAND 514 /* restart if no handler */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000093#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010094#ifndef ERESTART_RESTARTBLOCK
Denys Vlasenko523635f2012-02-25 02:44:25 +010095# define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
Roland McGrath9c555e72003-07-09 09:47:59 +000096#endif
Denys Vlasenko523635f2012-02-25 02:44:25 +010097
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000098#ifndef NSIG
Denys Vlasenko523635f2012-02-25 02:44:25 +010099# warning: NSIG is not defined, using 32
100# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000101#endif
102#ifdef ARM
Denys Vlasenko041b3ee2011-08-18 12:48:56 +0200103/* Ugh. Is this really correct? ARM has no RT signals?! */
Denys Vlasenko523635f2012-02-25 02:44:25 +0100104# undef NSIG
105# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000106#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000107
108#include "syscall.h"
109
110/* Define these shorthand notations to simplify the syscallent files. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000111#define TD TRACE_DESC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000112#define TF TRACE_FILE
113#define TI TRACE_IPC
114#define TN TRACE_NETWORK
115#define TP TRACE_PROCESS
116#define TS TRACE_SIGNAL
Namhyung Kim96792962012-10-24 11:41:57 +0900117#define TM TRACE_MEMORY
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000118#define NF SYSCALL_NEVER_FAILS
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200119#define MA MAX_ARGS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000120
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100121const struct_sysent sysent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000122#include "syscallent.h"
123};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000124
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100125#if SUPPORTED_PERSONALITIES > 1
126static const struct_sysent sysent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100127# include "syscallent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000128};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200129#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000130
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100131#if SUPPORTED_PERSONALITIES > 2
132static const struct_sysent sysent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100133# include "syscallent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000134};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200135#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000136
137/* Now undef them since short defines cause wicked namespace pollution. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000138#undef TD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000139#undef TF
140#undef TI
141#undef TN
142#undef TP
143#undef TS
Namhyung Kim96792962012-10-24 11:41:57 +0900144#undef TM
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000145#undef NF
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200146#undef MA
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000147
Denys Vlasenko39fca622011-08-20 02:12:33 +0200148/*
149 * `ioctlent.h' may be generated from `ioctlent.raw' by the auxiliary
150 * program `ioctlsort', such that the list is sorted by the `code' field.
151 * This has the side-effect of resolving the _IO.. macros into
152 * plain integers, eliminating the need to include here everything
153 * in "/usr/include".
154 */
155
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100156const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000157#include "errnoent.h"
158};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100159const char *const signalent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200160#include "signalent.h"
161};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100162const struct_ioctlent ioctlent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200163#include "ioctlent.h"
164};
165enum { nsyscalls0 = ARRAY_SIZE(sysent0) };
166enum { nerrnos0 = ARRAY_SIZE(errnoent0) };
167enum { nsignals0 = ARRAY_SIZE(signalent0) };
168enum { nioctlents0 = ARRAY_SIZE(ioctlent0) };
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};
180enum { nsyscalls1 = ARRAY_SIZE(sysent1) };
181enum { nerrnos1 = ARRAY_SIZE(errnoent1) };
182enum { nsignals1 = ARRAY_SIZE(signalent1) };
183enum { nioctlents1 = ARRAY_SIZE(ioctlent1) };
Denys Vlasenko39fca622011-08-20 02:12:33 +0200184#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000185
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100186#if SUPPORTED_PERSONALITIES > 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000187static const char *const errnoent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100188# include "errnoent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000189};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200190static const char *const signalent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100191# include "signalent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200192};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100193static const struct_ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100194# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200195};
196enum { nsyscalls2 = ARRAY_SIZE(sysent2) };
197enum { nerrnos2 = ARRAY_SIZE(errnoent2) };
198enum { nsignals2 = ARRAY_SIZE(signalent2) };
199enum { nioctlents2 = ARRAY_SIZE(ioctlent2) };
Denys Vlasenko39fca622011-08-20 02:12:33 +0200200#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000201
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100202#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100203const struct_sysent *sysent = sysent0;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100204const char *const *errnoent = errnoent0;
205const char *const *signalent = signalent0;
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100206const struct_ioctlent *ioctlent = ioctlent0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100207#endif
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100208unsigned nsyscalls = nsyscalls0;
209unsigned nerrnos = nerrnos0;
210unsigned nsignals = nsignals0;
211unsigned nioctlents = nioctlents0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100212
213unsigned num_quals;
214qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
215
216static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
217 nsyscalls0,
218#if SUPPORTED_PERSONALITIES > 1
219 nsyscalls1,
220#endif
221#if SUPPORTED_PERSONALITIES > 2
222 nsyscalls2,
223#endif
224};
225static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
226 sysent0,
227#if SUPPORTED_PERSONALITIES > 1
228 sysent1,
229#endif
230#if SUPPORTED_PERSONALITIES > 2
231 sysent2,
232#endif
233};
234
235enum {
236 MAX_NSYSCALLS1 = (nsyscalls0
237#if SUPPORTED_PERSONALITIES > 1
238 > nsyscalls1 ? nsyscalls0 : nsyscalls1
239#endif
240 ),
241 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
242#if SUPPORTED_PERSONALITIES > 2
243 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
244#endif
245 ),
246 MAX_NSYSCALLS = MAX_NSYSCALLS2,
247 /* We are ready for arches with up to 255 signals,
248 * even though the largest known signo is on MIPS and it is 128.
249 * The number of existing syscalls on all arches is
250 * larger that 255 anyway, so it is just a pedantic matter.
251 */
252 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
253};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000254
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100255#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100256unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000257
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100258# ifndef current_wordsize
259unsigned current_wordsize;
260static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000261 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000262 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100263# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000264 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100265# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200266};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100267# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000268
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200269void
Dmitry V. Levin3abe8b22006-12-20 22:37:21 +0000270set_personality(int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000271{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100272 nsyscalls = nsyscall_vec[personality];
273 sysent = sysent_vec[personality];
274
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000275 switch (personality) {
276 case 0:
277 errnoent = errnoent0;
278 nerrnos = nerrnos0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000279 ioctlent = ioctlent0;
280 nioctlents = nioctlents0;
281 signalent = signalent0;
282 nsignals = nsignals0;
283 break;
284
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000285 case 1:
286 errnoent = errnoent1;
287 nerrnos = nerrnos1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000288 ioctlent = ioctlent1;
289 nioctlents = nioctlents1;
290 signalent = signalent1;
291 nsignals = nsignals1;
292 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000293
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100294# if SUPPORTED_PERSONALITIES > 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000295 case 2:
296 errnoent = errnoent2;
297 nerrnos = nerrnos2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000298 ioctlent = ioctlent2;
299 nioctlents = nioctlents2;
300 signalent = signalent2;
301 nsignals = nsignals2;
302 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100303# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000304 }
305
306 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100307# ifndef current_wordsize
308 current_wordsize = personality_wordsize[personality];
309# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000310}
311
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000312static void
313update_personality(struct tcb *tcp, int personality)
314{
315 if (personality == current_personality)
316 return;
317 set_personality(personality);
318
319 if (personality == tcp->currpers)
320 return;
321 tcp->currpers = personality;
322
H.J. Lu35be5812012-04-16 13:00:01 +0200323# if defined(POWERPC64)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000324 if (!qflag) {
325 static const char *const names[] = {"64 bit", "32 bit"};
326 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
327 tcp->pid, names[personality]);
328 }
H.J. Lu35be5812012-04-16 13:00:01 +0200329# elif defined(X86_64)
330 if (!qflag) {
331 static const char *const names[] = {"64 bit", "32 bit", "x32"};
332 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
333 tcp->pid, names[personality]);
334 }
H.J. Lu085e4282012-04-17 11:05:04 -0700335# elif defined(X32)
336 if (!qflag) {
337 static const char *const names[] = {"x32", "32 bit"};
338 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
339 tcp->pid, names[personality]);
340 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000341# elif defined(AARCH64)
342 if (!qflag) {
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100343 static const char *const names[] = {"32-bit", "AArch64"};
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000344 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
345 tcp->pid, names[personality]);
346 }
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100347# elif defined(TILE)
348 if (!qflag) {
349 static const char *const names[] = {"64-bit", "32-bit"};
350 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
351 tcp->pid, names[personality]);
352 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100353# endif
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000354}
355#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000356
Denys Vlasenkoc1540fe2013-02-21 16:17:08 +0100357static int qual_syscall(), qual_signal(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000358
Roland McGrathe10e62a2004-09-04 04:20:43 +0000359static const struct qual_options {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000360 int bitflag;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000361 const char *option_name;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000362 int (*qualify)(const char *, int, int);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000363 const char *argument_name;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000364} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000365 { QUAL_TRACE, "trace", qual_syscall, "system call" },
366 { QUAL_TRACE, "t", qual_syscall, "system call" },
367 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
368 { QUAL_ABBREV, "a", qual_syscall, "system call" },
369 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
370 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
371 { QUAL_RAW, "raw", qual_syscall, "system call" },
372 { QUAL_RAW, "x", qual_syscall, "system call" },
373 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
374 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
375 { QUAL_SIGNAL, "s", qual_signal, "signal" },
Roland McGrath9797ceb2002-12-30 10:23:00 +0000376 { QUAL_READ, "read", qual_desc, "descriptor" },
377 { QUAL_READ, "reads", qual_desc, "descriptor" },
378 { QUAL_READ, "r", qual_desc, "descriptor" },
379 { QUAL_WRITE, "write", qual_desc, "descriptor" },
380 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
381 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000382 { 0, NULL, NULL, NULL },
383};
384
Roland McGrath9797ceb2002-12-30 10:23:00 +0000385static void
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100386reallocate_qual(int n)
387{
388 unsigned p;
389 qualbits_t *qp;
390 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
391 qp = qual_vec[p] = realloc(qual_vec[p], n * sizeof(qualbits_t));
392 if (!qp)
393 die_out_of_memory();
394 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
395 }
396 num_quals = n;
397}
398
399static void
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000400qualify_one(int n, int bitflag, int not, int pers)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000401{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100402 unsigned p;
Roland McGrath138c6a32006-01-12 09:50:49 +0000403
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100404 if (num_quals <= n)
405 reallocate_qual(n + 1);
Roland McGrath138c6a32006-01-12 09:50:49 +0000406
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100407 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
408 if (pers == p || pers < 0) {
409 if (not)
410 qual_vec[p][n] &= ~bitflag;
411 else
412 qual_vec[p][n] |= bitflag;
413 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000414 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000415}
416
417static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000418qual_syscall(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000419{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100420 unsigned p;
421 unsigned i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000422 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000423
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100424 if (*s >= '0' && *s <= '9') {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100425 i = string_to_uint(s);
426 if (i > MAX_NSYSCALLS)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000427 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000428 qualify_one(i, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000429 return 0;
Roland McGrath48a035f2006-01-12 09:45:56 +0000430 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000431
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100432 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
433 for (i = 0; i < nsyscall_vec[p]; i++) {
434 if (sysent_vec[p][i].sys_name
435 && strcmp(s, sysent_vec[p][i].sys_name) == 0
436 ) {
437 qualify_one(i, bitflag, not, 0);
438 rc = 0;
439 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000440 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100441 }
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000442
Roland McGrathfe6b3522005-02-02 04:40:11 +0000443 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000444}
445
446static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000447qual_signal(const char *s, int bitflag, int not)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000448{
449 int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000450
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100451 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000452 int signo = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100453 if (signo < 0 || signo > 255)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000454 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000455 qualify_one(signo, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000456 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000457 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000458 if (strncasecmp(s, "SIG", 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000459 s += 3;
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100460 for (i = 0; i <= NSIG; i++) {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000461 if (strcasecmp(s, signame(i) + 3) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000462 qualify_one(i, bitflag, not, -1);
Roland McGrath76421df2005-02-02 03:51:18 +0000463 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000464 }
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100465 }
Roland McGrath76421df2005-02-02 03:51:18 +0000466 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000467}
468
469static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000470qual_desc(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000471{
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100472 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000473 int desc = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100474 if (desc < 0 || desc > 0x7fff) /* paranoia */
Roland McGrathfe6b3522005-02-02 04:40:11 +0000475 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000476 qualify_one(desc, bitflag, not, -1);
Roland McGrath2b619022003-04-10 18:58:20 +0000477 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000478 }
479 return -1;
480}
481
482static int
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000483lookup_class(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000484{
485 if (strcmp(s, "file") == 0)
486 return TRACE_FILE;
487 if (strcmp(s, "ipc") == 0)
488 return TRACE_IPC;
489 if (strcmp(s, "network") == 0)
490 return TRACE_NETWORK;
491 if (strcmp(s, "process") == 0)
492 return TRACE_PROCESS;
493 if (strcmp(s, "signal") == 0)
494 return TRACE_SIGNAL;
Roland McGrath2fe7b132005-07-05 03:25:35 +0000495 if (strcmp(s, "desc") == 0)
496 return TRACE_DESC;
Namhyung Kim96792962012-10-24 11:41:57 +0900497 if (strcmp(s, "memory") == 0)
498 return TRACE_MEMORY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000499 return -1;
500}
501
502void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000503qualify(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000504{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000505 const struct qual_options *opt;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000506 int not;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000507 char *copy;
508 const char *p;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000509 int i, n;
510
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100511 if (num_quals == 0)
512 reallocate_qual(MIN_QUALS);
513
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000514 opt = &qual_options[0];
515 for (i = 0; (p = qual_options[i].option_name); i++) {
516 n = strlen(p);
517 if (strncmp(s, p, n) == 0 && s[n] == '=') {
518 opt = &qual_options[i];
519 s += n + 1;
520 break;
521 }
522 }
523 not = 0;
524 if (*s == '!') {
525 not = 1;
526 s++;
527 }
528 if (strcmp(s, "none") == 0) {
529 not = 1 - not;
530 s = "all";
531 }
532 if (strcmp(s, "all") == 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100533 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000534 qualify_one(i, opt->bitflag, not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000535 }
536 return;
537 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100538 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000539 qualify_one(i, opt->bitflag, !not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000540 }
Denys Vlasenko5d645812011-08-20 12:48:18 +0200541 copy = strdup(s);
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200542 if (!copy)
543 die_out_of_memory();
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000544 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000545 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100546 unsigned pers;
547 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
548 for (i = 0; i < nsyscall_vec[pers]; i++)
549 if (sysent_vec[pers][i].sys_flags & n)
550 qualify_one(i, opt->bitflag, not, 0);
551 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000552 continue;
553 }
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000554 if (opt->qualify(p, opt->bitflag, not)) {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100555 error_msg_and_die("invalid %s '%s'",
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000556 opt->argument_name, p);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000557 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000558 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000559 free(copy);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000560 return;
561}
562
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000563#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000564static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000565decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000566{
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000567 unsigned long addr;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100568 unsigned int i, n, size;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000569
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000570 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
571 return;
572
573 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100574 tcp->qual_flg = qual_flags[tcp->scno];
575 tcp->s_ent = &sysent[tcp->scno];
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000576 addr = tcp->u_arg[1];
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100577 size = current_wordsize;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100578 n = tcp->s_ent->nargs;
579 for (i = 0; i < n; ++i) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000580 if (size == sizeof(int)) {
581 unsigned int arg;
582 if (umove(tcp, addr, &arg) < 0)
583 arg = 0;
584 tcp->u_arg[i] = arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000585 }
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000586 else {
587 unsigned long arg;
588 if (umove(tcp, addr, &arg) < 0)
589 arg = 0;
590 tcp->u_arg[i] = arg;
591 }
592 addr += size;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000593 }
594}
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000595#endif
Mike Frysinger3362e892012-03-15 01:09:19 -0400596
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000597#ifdef SYS_ipc_subcall
598static void
599decode_ipc_subcall(struct tcb *tcp)
600{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100601 unsigned int i, n;
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000602
603 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
604 return;
605
606 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100607 tcp->qual_flg = qual_flags[tcp->scno];
608 tcp->s_ent = &sysent[tcp->scno];
609 n = tcp->s_ent->nargs;
610 for (i = 0; i < n; i++)
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000611 tcp->u_arg[i] = tcp->u_arg[i + 1];
612}
613#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000614
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200615int
616printargs(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000617{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200618 if (entering(tcp)) {
619 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100620 int n = tcp->s_ent->nargs;
621 for (i = 0; i < n; i++)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200622 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
623 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000624 return 0;
625}
626
Denys Vlasenko72879c62012-02-27 14:18:02 +0100627int
628printargs_lu(struct tcb *tcp)
629{
630 if (entering(tcp)) {
631 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100632 int n = tcp->s_ent->nargs;
633 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100634 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
635 }
636 return 0;
637}
638
639int
640printargs_ld(struct tcb *tcp)
641{
642 if (entering(tcp)) {
643 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100644 int n = tcp->s_ent->nargs;
645 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100646 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
647 }
648 return 0;
649}
650
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100651#if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200652long
653getrval2(struct tcb *tcp)
654{
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100655 long val;
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200656
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100657# if defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100658 val = sparc_regs.u_regs[U_REG_O1];
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100659# elif defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200660 if (upeek(tcp, 4*(REG_REG0+1), &val) < 0)
661 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100662# elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200663 if (upeek(tcp, PT_R9, &val) < 0)
664 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100665# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200666
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200667 return val;
668}
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100669#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200670
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200671int
672is_restart_error(struct tcb *tcp)
673{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200674 switch (tcp->u_error) {
675 case ERESTARTSYS:
676 case ERESTARTNOINTR:
677 case ERESTARTNOHAND:
678 case ERESTART_RESTARTBLOCK:
679 return 1;
680 default:
681 break;
682 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200683 return 0;
684}
685
Denys Vlasenko523635f2012-02-25 02:44:25 +0100686#if defined(I386)
Denys Vlasenko2550d482013-02-15 21:04:28 +0100687struct user_regs_struct i386_regs;
H.J. Lu35be5812012-04-16 13:00:01 +0200688#elif defined(X86_64) || defined(X32)
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100689/*
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100690 * On i386, pt_regs and user_regs_struct are the same,
691 * but on 64 bit x86, user_regs_struct has six more fields:
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100692 * fs_base, gs_base, ds, es, fs, gs.
693 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
694 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100695struct i386_user_regs_struct {
696 uint32_t ebx;
697 uint32_t ecx;
698 uint32_t edx;
699 uint32_t esi;
700 uint32_t edi;
701 uint32_t ebp;
702 uint32_t eax;
703 uint32_t xds;
704 uint32_t xes;
705 uint32_t xfs;
706 uint32_t xgs;
707 uint32_t orig_eax;
708 uint32_t eip;
709 uint32_t xcs;
710 uint32_t eflags;
711 uint32_t esp;
712 uint32_t xss;
713};
714static union {
715 struct user_regs_struct x86_64_r;
716 struct i386_user_regs_struct i386_r;
717} x86_regs_union;
718# define x86_64_regs x86_regs_union.x86_64_r
719# define i386_regs x86_regs_union.i386_r
720static struct iovec x86_io = {
721 .iov_base = &x86_regs_union
722};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100723#elif defined(IA64)
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200724long ia32 = 0; /* not static */
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100725static long ia64_r8, ia64_r10;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100726#elif defined(POWERPC)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +0100727static long ppc_result;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100728#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100729static long m68k_d0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100730#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100731static long bfin_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100732#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100733struct pt_regs arm_regs; /* not static */
Steve McIntyred8d3bd32012-10-24 17:58:16 +0100734#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100735static union {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100736 struct user_pt_regs aarch64_r;
737 struct arm_pt_regs arm_r;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100738} arm_regs_union;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100739# define aarch64_regs arm_regs_union.aarch64_r
740# define arm_regs arm_regs_union.arm_r
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100741static struct iovec aarch64_io = {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100742 .iov_base = &arm_regs_union
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100743};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100744#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100745static long alpha_r0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100746static long alpha_a3;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100747#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100748static struct pt_regs avr32_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100749#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100750struct pt_regs sparc_regs; /* not static */
Denys Vlasenko523635f2012-02-25 02:44:25 +0100751#elif defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100752static long long mips_a3;
753static long long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100754#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100755static long mips_a3;
756static long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100757#elif defined(S390) || defined(S390X)
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200758static long gpr2;
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200759static long syscall_mode;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100760#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100761static long hppa_r28;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100762#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100763static long sh_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100764#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100765static long sh64_r9;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100766#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100767static long cris_r10;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100768#elif defined(TILE)
769struct pt_regs tile_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100770#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100771static long microblaze_r3;
Christian Svensson492f81f2013-02-14 13:26:27 +0100772#elif defined(OR1K)
773static struct user_regs_struct or1k_regs;
774static struct iovec or1k_io = {
775 .iov_base = &or1k_regs
776};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100777#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000778
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100779void
780printcall(struct tcb *tcp)
781{
782#define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
783 sizeof(long) == 8 ? "[????????????????] " : \
784 NULL /* crash */)
785 if (get_regs_error) {
786 PRINTBADPC;
787 return;
788 }
789#if defined(I386)
790 tprintf("[%08lx] ", i386_regs.eip);
791#elif defined(S390) || defined(S390X)
792 long psw;
793 if (upeek(tcp, PT_PSWADDR, &psw) < 0) {
794 PRINTBADPC;
795 return;
796 }
797# ifdef S390
798 tprintf("[%08lx] ", psw);
799# elif S390X
Dmitry V. Levinddba73e2013-02-05 19:01:58 +0000800 tprintf("[%016lx] ", psw);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100801# endif
802#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100803 if (x86_io.iov_len == sizeof(i386_regs)) {
804 tprintf("[%08x] ", (unsigned) i386_regs.eip);
805 } else {
806# if defined(X86_64)
807 tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
808# elif defined(X32)
809 /* Note: this truncates 64-bit rip to 32 bits */
810 tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip);
811# endif
812 }
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100813#elif defined(IA64)
814 long ip;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100815 if (upeek(tcp, PT_B0, &ip) < 0) {
816 PRINTBADPC;
817 return;
818 }
819 tprintf("[%08lx] ", ip);
820#elif defined(POWERPC)
821 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100822 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) {
823 PRINTBADPC;
824 return;
825 }
826# ifdef POWERPC64
827 tprintf("[%016lx] ", pc);
828# else
829 tprintf("[%08lx] ", pc);
830# endif
831#elif defined(M68K)
832 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100833 if (upeek(tcp, 4*PT_PC, &pc) < 0) {
834 tprints("[????????] ");
835 return;
836 }
837 tprintf("[%08lx] ", pc);
838#elif defined(ALPHA)
839 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100840 if (upeek(tcp, REG_PC, &pc) < 0) {
841 tprints("[????????????????] ");
842 return;
843 }
844 tprintf("[%08lx] ", pc);
845#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100846 tprintf("[%08lx] ", sparc_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100847#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100848 tprintf("[%08lx] ", sparc_regs.tpc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100849#elif defined(HPPA)
850 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100851 if (upeek(tcp, PT_IAOQ0, &pc) < 0) {
852 tprints("[????????] ");
853 return;
854 }
855 tprintf("[%08lx] ", pc);
856#elif defined(MIPS)
857 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100858 if (upeek(tcp, REG_EPC, &pc) < 0) {
859 tprints("[????????] ");
860 return;
861 }
862 tprintf("[%08lx] ", pc);
863#elif defined(SH)
864 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100865 if (upeek(tcp, 4*REG_PC, &pc) < 0) {
866 tprints("[????????] ");
867 return;
868 }
869 tprintf("[%08lx] ", pc);
870#elif defined(SH64)
871 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100872 if (upeek(tcp, REG_PC, &pc) < 0) {
873 tprints("[????????????????] ");
874 return;
875 }
876 tprintf("[%08lx] ", pc);
877#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100878 tprintf("[%08lx] ", arm_regs.ARM_pc);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100879#elif defined(AARCH64)
880 /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100881#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100882 tprintf("[%08lx] ", avr32_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100883#elif defined(BFIN)
884 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100885 if (upeek(tcp, PT_PC, &pc) < 0) {
886 PRINTBADPC;
887 return;
888 }
889 tprintf("[%08lx] ", pc);
890#elif defined(CRISV10)
891 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100892 if (upeek(tcp, 4*PT_IRP, &pc) < 0) {
893 PRINTBADPC;
894 return;
895 }
896 tprintf("[%08lx] ", pc);
897#elif defined(CRISV32)
898 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100899 if (upeek(tcp, 4*PT_ERP, &pc) < 0) {
900 PRINTBADPC;
901 return;
902 }
903 tprintf("[%08lx] ", pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100904#elif defined(TILE)
905# ifdef _LP64
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500906 tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100907# else
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500908 tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100909# endif
Christian Svensson492f81f2013-02-14 13:26:27 +0100910#elif defined(OR1K)
911 tprintf("[%08lx] ", or1k_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100912#endif /* architecture */
913}
914
Denys Vlasenko7270de52013-02-21 15:46:34 +0100915/* Shuffle syscall numbers so that we don't have huge gaps in syscall table.
916 * The shuffling should be reversible: shuffle_scno(shuffle_scno(n)) == n.
917 */
918#if defined(ARM) /* So far only ARM needs this */
919static long
920shuffle_scno(unsigned long scno)
921{
922 if (scno <= ARM_LAST_ORDINARY_SYSCALL)
923 return scno;
924
925 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
926 if (scno == 0x000ffff0)
927 return ARM_LAST_ORDINARY_SYSCALL+1;
928 if (scno == ARM_LAST_ORDINARY_SYSCALL+1)
929 return 0x000ffff0;
930
931 /* Is it ARM specific syscall?
932 * Swap with [LAST_ORDINARY+2, LAST_ORDINARY+2 + LAST_SPECIAL] range.
933 */
934 if (scno >= 0x000f0000
935 && scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL
936 ) {
937 return scno - 0x000f0000 + (ARM_LAST_ORDINARY_SYSCALL+2);
938 }
939 if (/* scno >= ARM_LAST_ORDINARY_SYSCALL+2 - always true */ 1
940 && scno <= (ARM_LAST_ORDINARY_SYSCALL+2) + ARM_LAST_SPECIAL_SYSCALL
941 ) {
942 return scno + 0x000f0000 - (ARM_LAST_ORDINARY_SYSCALL+2);
943 }
944
945 return scno;
946}
947#else
948# define shuffle_scno(scno) ((long)(scno))
949#endif
950
951static char*
952undefined_scno_name(struct tcb *tcp)
953{
954 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
955
956 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
957 return buf;
958}
959
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100960#ifndef get_regs
961long get_regs_error;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100962void
963get_regs(pid_t pid)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100964{
965# if defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100966 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100967# elif defined(I386)
968 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &i386_regs);
969# elif defined(X86_64) || defined(X32)
Denys Vlasenkoe3b248d2013-02-15 00:24:19 +0100970 /*
971 * PTRACE_GETREGSET was introduced in 2.6.33.
972 * Let's be paranoid and require a bit later kernel.
973 */
974 if (os_release >= KERNEL_VERSION(2,6,35)) {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100975 /*x86_io.iov_base = &x86_regs_union; - already is */
976 x86_io.iov_len = sizeof(x86_regs_union);
977 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (long) &x86_io);
978 } else {
979 /* Use old method, with heuristical detection of 32-bitness */
980 x86_io.iov_len = sizeof(x86_64_regs);
981 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &x86_64_regs);
982 if (!get_regs_error && x86_64_regs.cs == 0x23) {
983 x86_io.iov_len = sizeof(i386_regs);
984 /*
985 * The order is important: i386_regs and x86_64_regs
986 * are overlaid in memory!
987 */
988 i386_regs.ebx = x86_64_regs.rbx;
989 i386_regs.ecx = x86_64_regs.rcx;
990 i386_regs.edx = x86_64_regs.rdx;
991 i386_regs.esi = x86_64_regs.rsi;
992 i386_regs.edi = x86_64_regs.rdi;
993 i386_regs.ebp = x86_64_regs.rbp;
994 i386_regs.eax = x86_64_regs.rax;
995 /*i386_regs.xds = x86_64_regs.ds; unused by strace */
996 /*i386_regs.xes = x86_64_regs.es; ditto... */
997 /*i386_regs.xfs = x86_64_regs.fs;*/
998 /*i386_regs.xgs = x86_64_regs.gs;*/
999 i386_regs.orig_eax = x86_64_regs.orig_rax;
1000 i386_regs.eip = x86_64_regs.rip;
1001 /*i386_regs.xcs = x86_64_regs.cs;*/
1002 /*i386_regs.eflags = x86_64_regs.eflags;*/
1003 i386_regs.esp = x86_64_regs.rsp;
1004 /*i386_regs.xss = x86_64_regs.ss;*/
1005 }
1006 }
Denys Vlasenko401374e2013-02-06 18:24:39 +01001007# elif defined(ARM)
1008 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (void *)&arm_regs);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001009# elif defined(AARCH64)
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001010 /*aarch64_io.iov_base = &arm_regs_union; - already is */
1011 aarch64_io.iov_len = sizeof(arm_regs_union);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001012 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (void *)&aarch64_io);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001013# if 0
1014 /* Paranoia checks */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001015 if (get_regs_error)
1016 return;
1017 switch (aarch64_io.iov_len) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001018 case sizeof(aarch64_regs):
1019 /* We are in 64-bit mode */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001020 break;
1021 case sizeof(arm_regs):
1022 /* We are in 32-bit mode */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001023 break;
1024 default:
1025 get_regs_error = -1;
1026 break;
1027 }
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001028# endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001029# elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001030 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001031# elif defined(TILE)
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -05001032 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &tile_regs);
Christian Svensson492f81f2013-02-14 13:26:27 +01001033# elif defined(OR1K)
1034 or1k_io.iov_len = sizeof(or1k_regs);
1035 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &or1k_io);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001036# endif
1037}
1038#endif
1039
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001040/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001041 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1042 * 1: ok, continue in trace_syscall_entering().
1043 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001044 * ("????" etc) and bail out.
1045 */
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01001046static int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001047get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001048{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001049 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001050
Denys Vlasenko523635f2012-02-25 02:44:25 +01001051#if defined(S390) || defined(S390X)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001052 if (upeek(tcp, PT_GPR2, &syscall_mode) < 0)
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001053 return -1;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001054
1055 if (syscall_mode != -ENOSYS) {
1056 /*
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001057 * Since kernel version 2.5.44 the scno gets passed in gpr2.
Roland McGrath2f924ca2003-06-26 22:23:28 +00001058 */
1059 scno = syscall_mode;
1060 } else {
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001061 /*
Michal Ludvig882eda82002-11-11 12:50:47 +00001062 * Old style of "passing" the scno via the SVC instruction.
1063 */
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001064 long psw;
Michal Ludvig882eda82002-11-11 12:50:47 +00001065 long opcode, offset_reg, tmp;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001066 void *svc_addr;
Denys Vlasenko7c9ba8b2011-08-19 19:46:32 +02001067 static const int gpr_offset[16] = {
1068 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
1069 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
1070 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
1071 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
1072 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001073
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001074 if (upeek(tcp, PT_PSWADDR, &psw) < 0)
Michal Ludvig882eda82002-11-11 12:50:47 +00001075 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001076 errno = 0;
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001077 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +00001078 if (errno) {
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001079 perror_msg("%s", "peektext(psw-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +00001080 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001081 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001082
1083 /*
1084 * We have to check if the SVC got executed directly or via an
1085 * EXECUTE instruction. In case of EXECUTE it is necessary to do
1086 * instruction decoding to derive the system call number.
1087 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1088 * so that this doesn't work if a SVC opcode is part of an EXECUTE
1089 * opcode. Since there is no way to find out the opcode size this
1090 * is the best we can do...
1091 */
Michal Ludvig882eda82002-11-11 12:50:47 +00001092 if ((opcode & 0xff00) == 0x0a00) {
1093 /* SVC opcode */
1094 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +00001095 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001096 else {
1097 /* SVC got executed by EXECUTE instruction */
1098
1099 /*
1100 * Do instruction decoding of EXECUTE. If you really want to
1101 * understand this, read the Principles of Operations.
1102 */
1103 svc_addr = (void *) (opcode & 0xfff);
1104
1105 tmp = 0;
1106 offset_reg = (opcode & 0x000f0000) >> 16;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001107 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001108 return -1;
1109 svc_addr += tmp;
1110
1111 tmp = 0;
1112 offset_reg = (opcode & 0x0000f000) >> 12;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001113 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001114 return -1;
1115 svc_addr += tmp;
1116
Denys Vlasenkofb036672009-01-23 16:30:26 +00001117 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
Michal Ludvig882eda82002-11-11 12:50:47 +00001118 if (errno)
1119 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001120# if defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +00001121 scno >>= 48;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001122# else
Michal Ludvig882eda82002-11-11 12:50:47 +00001123 scno >>= 16;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001124# endif
Michal Ludvig882eda82002-11-11 12:50:47 +00001125 tmp = 0;
1126 offset_reg = (opcode & 0x00f00000) >> 20;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001127 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001128 return -1;
1129
1130 scno = (scno | tmp) & 0xff;
1131 }
1132 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001133#elif defined(POWERPC)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001134 if (upeek(tcp, sizeof(unsigned long)*PT_R0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001135 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001136# ifdef POWERPC64
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001137 /* TODO: speed up strace by not doing this at every syscall.
1138 * We only need to do it after execve.
1139 */
1140 int currpers;
1141 long val;
Andreas Schwabd69fa492010-07-12 21:39:57 +02001142
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001143 /* Check for 64/32 bit mode. */
1144 if (upeek(tcp, sizeof(unsigned long)*PT_MSR, &val) < 0)
1145 return -1;
1146 /* SF is bit 0 of MSR */
1147 if (val < 0)
1148 currpers = 0;
1149 else
1150 currpers = 1;
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001151 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001152# endif
1153#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001154 scno = avr32_regs.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001155#elif defined(BFIN)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001156 if (upeek(tcp, PT_ORIG_P0, &scno))
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001157 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001158#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001159 scno = i386_regs.orig_eax;
H.J. Lu35be5812012-04-16 13:00:01 +02001160#elif defined(X86_64) || defined(X32)
1161# ifndef __X32_SYSCALL_BIT
1162# define __X32_SYSCALL_BIT 0x40000000
1163# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001164 int currpers;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001165# if 1
1166 /* GETREGSET of NT_PRSTATUS tells us regset size,
1167 * which unambiguously detects i386.
1168 *
1169 * Linux kernel distinguishes x86-64 and x32 processes
1170 * solely by looking at __X32_SYSCALL_BIT:
1171 * arch/x86/include/asm/compat.h::is_x32_task():
1172 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1173 * return true;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001174 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001175 if (x86_io.iov_len == sizeof(i386_regs)) {
1176 scno = i386_regs.orig_eax;
1177 currpers = 1;
1178 } else {
1179 scno = x86_64_regs.orig_rax;
1180 currpers = 0;
1181 if (scno & __X32_SYSCALL_BIT) {
1182 scno -= __X32_SYSCALL_BIT;
1183 currpers = 2;
1184 }
1185 }
1186# elif 0
1187 /* cs = 0x33 for long mode (native 64 bit and x32)
1188 * cs = 0x23 for compatibility mode (32 bit)
1189 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1190 */
1191 scno = x86_64_regs.orig_rax;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001192 switch (x86_64_regs.cs) {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001193 case 0x23: currpers = 1; break;
H.J. Lu35be5812012-04-16 13:00:01 +02001194 case 0x33:
1195 if (x86_64_regs.ds == 0x2b) {
1196 currpers = 2;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001197 scno &= ~__X32_SYSCALL_BIT;
H.J. Lu35be5812012-04-16 13:00:01 +02001198 } else
1199 currpers = 0;
1200 break;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001201 default:
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001202 fprintf(stderr, "Unknown value CS=0x%08X while "
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001203 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001204 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001205 currpers = current_personality;
1206 break;
1207 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001208# elif 0
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001209 /* This version analyzes the opcode of a syscall instruction.
1210 * (int 0x80 on i386 vs. syscall on x86-64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001211 * It works, but is too complicated, and strictly speaking, unreliable.
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001212 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001213 unsigned long call, rip = x86_64_regs.rip;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001214 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1215 rip -= 2;
1216 errno = 0;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001217 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001218 if (errno)
1219 fprintf(stderr, "ptrace_peektext failed: %s\n",
1220 strerror(errno));
1221 switch (call & 0xffff) {
1222 /* x86-64: syscall = 0x0f 0x05 */
1223 case 0x050f: currpers = 0; break;
1224 /* i386: int 0x80 = 0xcd 0x80 */
1225 case 0x80cd: currpers = 1; break;
1226 default:
1227 currpers = current_personality;
1228 fprintf(stderr,
1229 "Unknown syscall opcode (0x%04X) while "
1230 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001231 "PID=%d\n", (int)call, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001232 break;
1233 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001234# endif
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001235
H.J. Lu35be5812012-04-16 13:00:01 +02001236# ifdef X32
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001237 /* If we are built for a x32 system, then personality 0 is x32
1238 * (not x86_64), and stracing of x86_64 apps is not supported.
1239 * Stracing of i386 apps is still supported.
H.J. Lu085e4282012-04-17 11:05:04 -07001240 */
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001241 if (currpers == 0) {
1242 fprintf(stderr, "syscall_%lu(...) in unsupported "
1243 "64-bit mode of process PID=%d\n",
1244 scno, tcp->pid);
1245 return 0;
H.J. Lu35be5812012-04-16 13:00:01 +02001246 }
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001247 currpers &= ~2; /* map 2,1 to 0,1 */
H.J. Lu35be5812012-04-16 13:00:01 +02001248# endif
H.J. Lu085e4282012-04-17 11:05:04 -07001249 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001250#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001251# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01001252 long psr;
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001253 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001254 ia32 = (psr & IA64_PSR_IS) != 0;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001255 if (ia32) {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001256 if (upeek(tcp, PT_R1, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001257 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001258 } else {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001259 if (upeek(tcp, PT_R15, &scno) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001260 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001261 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001262#elif defined(AARCH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001263 switch (aarch64_io.iov_len) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001264 case sizeof(aarch64_regs):
1265 /* We are in 64-bit mode */
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001266 scno = aarch64_regs.regs[8];
1267 update_personality(tcp, 1);
1268 break;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001269 case sizeof(arm_regs):
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001270 /* We are in 32-bit mode */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001271 scno = arm_regs.ARM_r7;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001272 update_personality(tcp, 0);
1273 break;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001274 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001275#elif defined(ARM)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001276 if (arm_regs.ARM_ip != 0) {
1277 /* It is not a syscall entry */
1278 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001279 tcp->flags |= TCB_INSYSCALL;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001280 return 0;
1281 }
1282 /* Note: we support only 32-bit CPUs, not 26-bit */
1283
1284 if (arm_regs.ARM_cpsr & 0x20) {
1285 /* Thumb mode */
1286 scno = arm_regs.ARM_r7;
1287 } else {
1288 /* ARM mode */
1289 errno = 0;
1290 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1291 if (errno)
1292 return -1;
1293
1294 /* EABI syscall convention? */
1295 if (scno == 0xef000000) {
1296 scno = arm_regs.ARM_r7; /* yes */
1297 } else {
1298 if ((scno & 0x0ff00000) != 0x0f900000) {
1299 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1300 tcp->pid, scno);
1301 return -1;
1302 }
1303 /* Fixup the syscall number */
1304 scno &= 0x000fffff;
1305 }
1306 }
Denys Vlasenko7270de52013-02-21 15:46:34 +01001307
1308 scno = shuffle_scno(scno);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001309#elif defined(M68K)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001310 if (upeek(tcp, 4*PT_ORIG_D0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001311 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001312#elif defined(LINUX_MIPSN32)
Roland McGrath542c2c62008-05-20 01:11:56 +00001313 unsigned long long regs[38];
1314
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001315 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001316 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001317 mips_a3 = regs[REG_A3];
1318 mips_r2 = regs[REG_V0];
Roland McGrath542c2c62008-05-20 01:11:56 +00001319
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001320 scno = mips_r2;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001321 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001322 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001323 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001324 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001325 return 0;
1326 }
Roland McGrath542c2c62008-05-20 01:11:56 +00001327 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001328#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001329 if (upeek(tcp, REG_A3, &mips_a3) < 0)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001330 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001331 if (upeek(tcp, REG_V0, &scno) < 0)
1332 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001333
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001334 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001335 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001336 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001337 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001338 return 0;
1339 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001340 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001341#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001342 if (upeek(tcp, REG_A3, &alpha_a3) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001343 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001344 if (upeek(tcp, REG_R0, &scno) < 0)
1345 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001346
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001347 /*
1348 * Do some sanity checks to figure out if it's
1349 * really a syscall entry
1350 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001351 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001352 if (alpha_a3 == 0 || alpha_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001353 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001354 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001355 return 0;
1356 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001357 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001358#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001359 /* Disassemble the syscall trap. */
1360 /* Retrieve the syscall trap instruction. */
Denys Vlasenko46455822013-02-05 17:02:59 +01001361 unsigned long trap;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001362 errno = 0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001363# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001364 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001365 trap >>= 32;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001366# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001367 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001368# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001369 if (errno)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001370 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001371
1372 /* Disassemble the trap to see what personality to use. */
1373 switch (trap) {
1374 case 0x91d02010:
1375 /* Linux/SPARC syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001376 update_personality(tcp, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001377 break;
1378 case 0x91d0206d:
1379 /* Linux/SPARC64 syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001380 update_personality(tcp, 2);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001381 break;
1382 case 0x91d02000:
1383 /* SunOS syscall trap. (pers 1) */
1384 fprintf(stderr, "syscall: SunOS no support\n");
1385 return -1;
1386 case 0x91d02008:
1387 /* Solaris 2.x syscall trap. (per 2) */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001388 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001389 break;
1390 case 0x91d02009:
1391 /* NetBSD/FreeBSD syscall trap. */
1392 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1393 return -1;
1394 case 0x91d02027:
1395 /* Solaris 2.x gettimeofday */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001396 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001397 break;
1398 default:
Denys Vlasenko523635f2012-02-25 02:44:25 +01001399# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001400 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001401# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001402 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001403# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001404 return -1;
1405 }
1406
1407 /* Extract the system call number from the registers. */
1408 if (trap == 0x91d02027)
1409 scno = 156;
1410 else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001411 scno = sparc_regs.u_regs[U_REG_G1];
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001412 if (scno == 0) {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001413 scno = sparc_regs.u_regs[U_REG_O0];
1414 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 +02001415 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001416#elif defined(HPPA)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001417 if (upeek(tcp, PT_GR20, &scno) < 0)
1418 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001419#elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001420 /*
1421 * In the new syscall ABI, the system call number is in R3.
1422 */
1423 if (upeek(tcp, 4*(REG_REG0+3), &scno) < 0)
1424 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001425
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001426 if (scno < 0) {
1427 /* Odd as it may seem, a glibc bug has been known to cause
1428 glibc to issue bogus negative syscall numbers. So for
1429 our purposes, make strace print what it *should* have been */
1430 long correct_scno = (scno & 0xff);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001431 if (debug_flag)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001432 fprintf(stderr,
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001433 "Detected glibc bug: bogus system call"
1434 " number = %ld, correcting to %ld\n",
1435 scno,
1436 correct_scno);
1437 scno = correct_scno;
1438 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001439#elif defined(SH64)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001440 if (upeek(tcp, REG_SYSCALL, &scno) < 0)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001441 return -1;
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001442 scno &= 0xFFFF;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001443#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001444 if (upeek(tcp, 4*PT_R9, &scno) < 0)
1445 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001446#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001447 int currpers;
1448 scno = tile_regs.regs[10];
1449# ifdef __tilepro__
1450 currpers = 1;
1451# else
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001452# ifndef PT_FLAGS_COMPAT
1453# define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1454# endif
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001455 if (tile_regs.flags & PT_FLAGS_COMPAT)
1456 currpers = 1;
1457 else
1458 currpers = 0;
1459# endif
1460 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001461#elif defined(MICROBLAZE)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001462 if (upeek(tcp, 0, &scno) < 0)
1463 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001464#elif defined(OR1K)
1465 scno = or1k_regs.gpr[11];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001466#endif
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001467
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001468 tcp->scno = scno;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001469 if (SCNO_IS_VALID(tcp->scno)) {
1470 tcp->s_ent = &sysent[scno];
1471 tcp->qual_flg = qual_flags[scno];
1472 } else {
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01001473 static const struct_sysent unknown = {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001474 .nargs = MAX_ARGS,
1475 .sys_flags = 0,
1476 .sys_func = printargs,
1477 .sys_name = "unknown", /* not used */
1478 };
1479 tcp->s_ent = &unknown;
1480 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1481 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001482 return 1;
1483}
1484
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02001485/* Called at each syscall entry.
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001486 * Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001487 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1488 * 1: ok, continue in trace_syscall_entering().
1489 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001490 * ("????" etc) and bail out.
1491 */
Roland McGratha4d48532005-06-08 20:45:28 +00001492static int
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001493syscall_fixup_on_sysenter(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001494{
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001495 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001496#if defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001497 if (i386_regs.eax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001498 if (debug_flag)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001499 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1500 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001501 }
H.J. Lu35be5812012-04-16 13:00:01 +02001502#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001503 {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001504 long rax;
1505 if (x86_io.iov_len == sizeof(i386_regs)) {
1506 /* Sign extend from 32 bits */
1507 rax = (int32_t)i386_regs.eax;
1508 } else {
1509 /* Note: in X32 build, this truncates 64 to 32 bits */
1510 rax = x86_64_regs.rax;
1511 }
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001512 if (rax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001513 if (debug_flag)
Denys Vlasenko18beb982011-08-24 16:59:23 +02001514 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1515 return 0;
1516 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001517 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001518#elif defined(S390) || defined(S390X)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001519 /* TODO: we already fetched PT_GPR2 in get_scno
1520 * and stored it in syscall_mode, reuse it here
1521 * instead of re-fetching?
1522 */
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001523 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001524 return -1;
Michal Ludvig882eda82002-11-11 12:50:47 +00001525 if (syscall_mode != -ENOSYS)
1526 syscall_mode = tcp->scno;
Denys Vlasenkoece98792011-08-25 10:25:35 +02001527 if (gpr2 != syscall_mode) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001528 if (debug_flag)
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001529 fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2);
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001530 return 0;
1531 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001532#elif defined(M68K)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001533 /* TODO? Eliminate upeek's in arches below like we did in x86 */
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001534 if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001535 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001536 if (m68k_d0 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001537 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001538 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001539 return 0;
1540 }
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001541#elif defined(IA64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001542 if (upeek(tcp, PT_R10, &ia64_r10) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001543 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001544 if (upeek(tcp, PT_R8, &ia64_r8) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001545 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001546 if (ia32 && ia64_r8 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001547 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001548 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001549 return 0;
1550 }
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001551#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001552 if (upeek(tcp, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001553 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001554 if (cris_r10 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001555 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001556 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001557 return 0;
1558 }
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001559#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001560 if (upeek(tcp, 3 * 4, &microblaze_r3) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001561 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001562 if (microblaze_r3 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001563 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001564 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001565 return 0;
1566 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001567#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001568 return 1;
1569}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001570
Denys Vlasenko146b9442012-03-18 22:10:48 +01001571static void
1572internal_fork(struct tcb *tcp)
1573{
1574#if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1575# define ARG_FLAGS 1
1576#else
1577# define ARG_FLAGS 0
1578#endif
1579#ifndef CLONE_UNTRACED
1580# define CLONE_UNTRACED 0x00800000
1581#endif
1582 if ((ptrace_setoptions
1583 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1584 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1585 return;
1586
1587 if (!followfork)
1588 return;
1589
1590 if (entering(tcp)) {
1591 /*
1592 * We won't see the new child if clone is called with
1593 * CLONE_UNTRACED, so we keep the same logic with that option
1594 * and don't trace it.
1595 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001596 if ((tcp->s_ent->sys_func == sys_clone)
1597 && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
1598 )
Denys Vlasenko146b9442012-03-18 22:10:48 +01001599 return;
1600 setbpt(tcp);
1601 } else {
1602 if (tcp->flags & TCB_BPTSET)
1603 clearbpt(tcp);
1604 }
1605}
1606
1607#if defined(TCB_WAITEXECVE)
1608static void
1609internal_exec(struct tcb *tcp)
1610{
1611 /* Maybe we have post-execve SIGTRAP suppressed? */
1612 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1613 return; /* yes, no need to do anything */
1614
1615 if (exiting(tcp) && syserror(tcp))
1616 /* Error in execve, no post-execve SIGTRAP expected */
1617 tcp->flags &= ~TCB_WAITEXECVE;
1618 else
1619 tcp->flags |= TCB_WAITEXECVE;
1620}
1621#endif
1622
1623static void
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001624syscall_fixup_for_fork_exec(struct tcb *tcp)
Roland McGrathc1e45922008-05-27 23:18:29 +00001625{
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001626 /*
1627 * We must always trace a few critical system calls in order to
1628 * correctly support following forks in the presence of tracing
1629 * qualifiers.
1630 */
1631 int (*func)();
1632
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001633 func = tcp->s_ent->sys_func;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001634
1635 if ( sys_fork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001636 || sys_vfork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001637 || sys_clone == func
Denys Vlasenko146b9442012-03-18 22:10:48 +01001638 ) {
1639 internal_fork(tcp);
1640 return;
1641 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001642
Denys Vlasenko84703742012-02-25 02:38:52 +01001643#if defined(TCB_WAITEXECVE)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001644 if ( sys_execve == func
Denys Vlasenko84703742012-02-25 02:38:52 +01001645# if defined(SPARC) || defined(SPARC64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001646 || sys_execv == func
Denys Vlasenkoa7949742011-08-21 17:26:55 +02001647# endif
Denys Vlasenko146b9442012-03-18 22:10:48 +01001648 ) {
1649 internal_exec(tcp);
1650 return;
1651 }
Roland McGrathc1e45922008-05-27 23:18:29 +00001652#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001653}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001654
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001655/* Return -1 on error or 1 on success (never 0!) */
Roland McGratha4d48532005-06-08 20:45:28 +00001656static int
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001657get_syscall_args(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001658{
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001659 int i, nargs;
1660
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001661 nargs = tcp->s_ent->nargs;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001662
Denys Vlasenko523635f2012-02-25 02:44:25 +01001663#if defined(S390) || defined(S390X)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001664 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001665 if (upeek(tcp, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
1666 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001667#elif defined(ALPHA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001668 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001669 if (upeek(tcp, REG_A0+i, &tcp->u_arg[i]) < 0)
1670 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001671#elif defined(IA64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001672 if (!ia32) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001673 unsigned long *out0, cfm, sof, sol;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001674 long rbs_end;
1675 /* be backwards compatible with kernel < 2.4.4... */
1676# ifndef PT_RBS_END
1677# define PT_RBS_END PT_AR_BSP
1678# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001679
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001680 if (upeek(tcp, PT_RBS_END, &rbs_end) < 0)
1681 return -1;
1682 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001683 return -1;
1684
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001685 sof = (cfm >> 0) & 0x7f;
1686 sol = (cfm >> 7) & 0x7f;
1687 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1688
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001689 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001690 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1691 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1692 return -1;
1693 }
1694 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001695 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1696 PT_R9 /* ECX = out1 */,
1697 PT_R10 /* EDX = out2 */,
1698 PT_R14 /* ESI = out3 */,
1699 PT_R15 /* EDI = out4 */,
1700 PT_R13 /* EBP = out5 */};
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001701
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001702 for (i = 0; i < nargs; ++i) {
1703 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1704 return -1;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001705 /* truncate away IVE sign-extension */
1706 tcp->u_arg[i] &= 0xffffffff;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001707 }
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001708 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001709#elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001710 /* N32 and N64 both use up to six registers. */
1711 unsigned long long regs[38];
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001712
1713 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1714 return -1;
1715
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001716 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001717 tcp->u_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001718# if defined(LINUX_MIPSN32)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001719 tcp->ext_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001720# endif
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001721 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001722#elif defined(MIPS)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001723 if (nargs > 4) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001724 long sp;
1725
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001726 if (upeek(tcp, REG_SP, &sp) < 0)
1727 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001728 for (i = 0; i < 4; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001729 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1730 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001731 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001732 (char *)(tcp->u_arg + 4));
1733 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001734 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001735 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001736 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001737 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001738#elif defined(POWERPC)
1739# ifndef PT_ORIG_R3
1740# define PT_ORIG_R3 34
1741# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001742 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001743 if (upeek(tcp, (i==0) ?
1744 (sizeof(unsigned long) * PT_ORIG_R3) :
1745 ((i+PT_R3) * sizeof(unsigned long)),
1746 &tcp->u_arg[i]) < 0)
1747 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001748 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001749#elif defined(SPARC) || defined(SPARC64)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001750 for (i = 0; i < nargs; ++i)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001751 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001752#elif defined(HPPA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001753 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001754 if (upeek(tcp, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1755 return -1;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001756#elif defined(ARM) || defined(AARCH64)
1757# if defined(AARCH64)
1758 if (tcp->currpers == 1)
1759 for (i = 0; i < nargs; ++i)
1760 tcp->u_arg[i] = aarch64_regs.regs[i];
1761 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001762# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001763 for (i = 0; i < nargs; ++i)
Denys Vlasenko401374e2013-02-06 18:24:39 +01001764 tcp->u_arg[i] = arm_regs.uregs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001765#elif defined(AVR32)
Denys Vlasenkob5b25892011-08-30 19:04:54 +02001766 (void)i;
1767 (void)nargs;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001768 tcp->u_arg[0] = avr32_regs.r12;
1769 tcp->u_arg[1] = avr32_regs.r11;
1770 tcp->u_arg[2] = avr32_regs.r10;
1771 tcp->u_arg[3] = avr32_regs.r9;
1772 tcp->u_arg[4] = avr32_regs.r5;
1773 tcp->u_arg[5] = avr32_regs.r3;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001774#elif defined(BFIN)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001775 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 +02001776
Denys Vlasenko4b887a52011-08-23 13:32:38 +02001777 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001778 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1779 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001780#elif defined(SH)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001781 static const int syscall_regs[MAX_ARGS] = {
1782 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1783 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001784 };
1785
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001786 for (i = 0; i < nargs; ++i)
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001787 if (upeek(tcp, syscall_regs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001788 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001789#elif defined(SH64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001790 int i;
1791 /* Registers used by SH5 Linux system calls for parameters */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001792 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001793
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001794 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001795 if (upeek(tcp, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1796 return -1;
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001797#elif defined(I386)
1798 (void)i;
1799 (void)nargs;
1800 tcp->u_arg[0] = i386_regs.ebx;
1801 tcp->u_arg[1] = i386_regs.ecx;
1802 tcp->u_arg[2] = i386_regs.edx;
1803 tcp->u_arg[3] = i386_regs.esi;
1804 tcp->u_arg[4] = i386_regs.edi;
1805 tcp->u_arg[5] = i386_regs.ebp;
H.J. Lu35be5812012-04-16 13:00:01 +02001806#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001807 (void)i;
1808 (void)nargs;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001809 if (x86_io.iov_len != sizeof(i386_regs)) {
1810 /* x86-64 or x32 ABI */
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001811 tcp->u_arg[0] = x86_64_regs.rdi;
1812 tcp->u_arg[1] = x86_64_regs.rsi;
1813 tcp->u_arg[2] = x86_64_regs.rdx;
1814 tcp->u_arg[3] = x86_64_regs.r10;
1815 tcp->u_arg[4] = x86_64_regs.r8;
1816 tcp->u_arg[5] = x86_64_regs.r9;
H.J. Lu35be5812012-04-16 13:00:01 +02001817# ifdef X32
1818 tcp->ext_arg[0] = x86_64_regs.rdi;
1819 tcp->ext_arg[1] = x86_64_regs.rsi;
1820 tcp->ext_arg[2] = x86_64_regs.rdx;
1821 tcp->ext_arg[3] = x86_64_regs.r10;
1822 tcp->ext_arg[4] = x86_64_regs.r8;
1823 tcp->ext_arg[5] = x86_64_regs.r9;
1824# endif
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001825 } else {
1826 /* i386 ABI */
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001827 /* Zero-extend from 32 bits */
1828 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1829 * if you need to use *sign-extended* parameter.
1830 */
1831 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1832 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1833 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1834 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1835 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1836 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001837 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001838#elif defined(MICROBLAZE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001839 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001840 if (upeek(tcp, (5 + i) * 4, &tcp->u_arg[i]) < 0)
1841 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001842#elif defined(CRISV10) || defined(CRISV32)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001843 static const int crisregs[MAX_ARGS] = {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001844 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001845 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001846 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001847
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001848 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001849 if (upeek(tcp, crisregs[i], &tcp->u_arg[i]) < 0)
1850 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001851#elif defined(TILE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001852 for (i = 0; i < nargs; ++i)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001853 tcp->u_arg[i] = tile_regs.regs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001854#elif defined(M68K)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001855 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001856 if (upeek(tcp, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
1857 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001858#elif defined(OR1K)
1859 (void)nargs;
1860 for (i = 0; i < 6; ++i)
1861 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001862#else /* Other architecture (32bits specific) */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001863 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001864 if (upeek(tcp, i*4, &tcp->u_arg[i]) < 0)
1865 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001866#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001867 return 1;
1868}
1869
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00001870static int
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001871trace_syscall_entering(struct tcb *tcp)
1872{
1873 int res, scno_good;
1874
Denys Vlasenko2ce12ed2011-08-24 17:25:32 +02001875#if defined TCB_WAITEXECVE
1876 if (tcp->flags & TCB_WAITEXECVE) {
1877 /* This is the post-execve SIGTRAP. */
1878 tcp->flags &= ~TCB_WAITEXECVE;
1879 return 0;
1880 }
1881#endif
1882
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001883 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001884 if (res == 0)
1885 return res;
Denys Vlasenko907735a2012-03-21 00:23:16 +01001886 if (res == 1) {
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001887 res = syscall_fixup_on_sysenter(tcp);
Denys Vlasenko907735a2012-03-21 00:23:16 +01001888 if (res == 0)
1889 return res;
1890 if (res == 1)
1891 res = get_syscall_args(tcp);
1892 }
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001893
1894 if (res != 1) {
1895 printleader(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001896 if (scno_good != 1)
Denys Vlasenkob7a6dae2012-03-20 16:48:35 +01001897 tprints("????" /* anti-trigraph gap */ "(");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001898 else if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01001899 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001900 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001901 tprintf("%s(", tcp->s_ent->sys_name);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001902 /*
1903 * " <unavailable>" will be added later by the code which
1904 * detects ptrace errors.
1905 */
1906 goto ret;
1907 }
1908
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001909#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001910 while (1) {
Denys Vlasenko523635f2012-02-25 02:44:25 +01001911# ifdef SYS_socket_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001912 if (tcp->s_ent->sys_func == sys_socketcall) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00001913 decode_socket_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001914 break;
1915 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001916# endif
1917# ifdef SYS_ipc_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001918 if (tcp->s_ent->sys_func == sys_ipc) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00001919 decode_ipc_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001920 break;
1921 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001922# endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001923 break;
1924 }
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001925#endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001926
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001927 if (need_fork_exec_workarounds)
1928 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001929
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001930 if (!(tcp->qual_flg & QUAL_TRACE)
1931 || (tracing_paths && !pathtrace_match(tcp))
1932 ) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001933 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
1934 return 0;
1935 }
1936
1937 tcp->flags &= ~TCB_FILTERED;
1938
1939 if (cflag == CFLAG_ONLY_STATS) {
1940 res = 0;
1941 goto ret;
1942 }
1943
1944 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001945 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01001946 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001947 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001948 tprintf("%s(", tcp->s_ent->sys_name);
1949 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001950 res = printargs(tcp);
1951 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001952 res = tcp->s_ent->sys_func(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001953
Dmitry V. Levinb742d8c2012-09-17 22:40:12 +00001954 fflush(tcp->outf);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001955 ret:
1956 tcp->flags |= TCB_INSYSCALL;
1957 /* Measure the entrance time as late as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001958 if (Tflag || cflag)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001959 gettimeofday(&tcp->etime, NULL);
1960 return res;
1961}
1962
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001963/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001964 * 1: ok, continue in trace_syscall_exiting().
1965 * -1: error, trace_syscall_exiting() should print error indicator
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001966 * ("????" etc) and bail out.
1967 */
1968static int
1969get_syscall_result(struct tcb *tcp)
1970{
Denys Vlasenko523635f2012-02-25 02:44:25 +01001971#if defined(S390) || defined(S390X)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001972 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
1973 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001974#elif defined(POWERPC)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001975# define SO_MASK 0x10000000
1976 {
1977 long flags;
1978 if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1979 return -1;
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01001980 if (upeek(tcp, sizeof(unsigned long)*PT_R3, &ppc_result) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001981 return -1;
1982 if (flags & SO_MASK)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01001983 ppc_result = -ppc_result;
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001984 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001985#elif defined(AVR32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001986 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001987#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001988 if (upeek(tcp, PT_R0, &bfin_r0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001989 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001990#elif defined(I386)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001991 /* already done by get_regs */
H.J. Lu35be5812012-04-16 13:00:01 +02001992#elif defined(X86_64) || defined(X32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001993 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001994#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001995# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01001996 long psr;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001997 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
1998 ia32 = (psr & IA64_PSR_IS) != 0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001999 if (upeek(tcp, PT_R8, &ia64_r8) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002000 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002001 if (upeek(tcp, PT_R10, &ia64_r10) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002002 return -1;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002003#elif defined(ARM)
2004 /* already done by get_regs */
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002005#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002006 /* register reading already done by get_regs */
2007
2008 /* Used to do this, but we did it on syscall entry already: */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002009 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
2010 * else it's personality 0.
2011 */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002012 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
Denys Vlasenko523635f2012-02-25 02:44:25 +01002013#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002014 if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002015 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002016#elif defined(LINUX_MIPSN32)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002017 unsigned long long regs[38];
2018
2019 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
2020 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002021 mips_a3 = regs[REG_A3];
2022 mips_r2 = regs[REG_V0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01002023#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002024 if (upeek(tcp, REG_A3, &mips_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002025 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002026 if (upeek(tcp, REG_V0, &mips_r2) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002027 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002028#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002029 if (upeek(tcp, REG_A3, &alpha_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002030 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002031 if (upeek(tcp, REG_R0, &alpha_r0) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002032 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002033#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002034 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002035#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002036 if (upeek(tcp, PT_GR28, &hppa_r28) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002037 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002038#elif defined(SH)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002039 /* new syscall ABI returns result in R0 */
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002040 if (upeek(tcp, 4*REG_REG0, (long *)&sh_r0) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002041 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002042#elif defined(SH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002043 /* ABI defines result returned in r9 */
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002044 if (upeek(tcp, REG_GENERAL(9), (long *)&sh64_r9) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002045 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002046#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002047 if (upeek(tcp, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002048 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002049#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002050 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002051#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002052 if (upeek(tcp, 3 * 4, &microblaze_r3) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002053 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01002054#elif defined(OR1K)
2055 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002056#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002057 return 1;
2058}
2059
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002060/* Called at each syscall exit */
2061static void
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002062syscall_fixup_on_sysexit(struct tcb *tcp)
2063{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002064#if defined(S390) || defined(S390X)
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002065 if (syscall_mode != -ENOSYS)
2066 syscall_mode = tcp->scno;
Denys Vlasenkoece98792011-08-25 10:25:35 +02002067 if ((tcp->flags & TCB_WAITEXECVE)
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002068 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
2069 /*
2070 * Return from execve.
2071 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2072 * flag set for the post-execve SIGTRAP to see and reset.
2073 */
2074 gpr2 = 0;
2075 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002076#endif
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002077}
2078
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002079/*
2080 * Check the syscall return value register value for whether it is
2081 * a negated errno code indicating an error, or a success return value.
2082 */
2083static inline int
2084is_negated_errno(unsigned long int val)
2085{
2086 unsigned long int max = -(long int) nerrnos;
Denys Vlasenko2544f982013-02-19 17:39:56 +01002087#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01002088 if (current_wordsize < sizeof(val)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002089 val = (unsigned int) val;
2090 max = (unsigned int) max;
2091 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002092#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002093 return val > max;
2094}
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002095
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002096#if defined(X32)
2097static inline int
2098is_negated_errno_x32(unsigned long long val)
2099{
2100 unsigned long long max = -(long long) nerrnos;
2101 /*
2102 * current_wordsize is 4 even in personality 0 (native X32)
2103 * but truncation _must not_ be done in it.
2104 * can't check current_wordsize here!
2105 */
2106 if (current_personality != 0) {
2107 val = (uint32_t) val;
2108 max = (uint32_t) max;
2109 }
2110 return val > max;
2111}
2112#endif
2113
Denys Vlasenko907735a2012-03-21 00:23:16 +01002114/* Returns:
2115 * 1: ok, continue in trace_syscall_exiting().
2116 * -1: error, trace_syscall_exiting() should print error indicator
2117 * ("????" etc) and bail out.
2118 */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002119static void
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002120get_error(struct tcb *tcp)
2121{
2122 int u_error = 0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002123 int check_errno = 1;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002124 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002125 check_errno = 0;
2126 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002127#if defined(S390) || defined(S390X)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002128 if (check_errno && is_negated_errno(gpr2)) {
2129 tcp->u_rval = -1;
2130 u_error = -gpr2;
2131 }
2132 else {
2133 tcp->u_rval = gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002134 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002135#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002136 if (check_errno && is_negated_errno(i386_regs.eax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002137 tcp->u_rval = -1;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002138 u_error = -i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002139 }
2140 else {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002141 tcp->u_rval = i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002142 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002143#elif defined(X86_64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002144 long rax;
2145 if (x86_io.iov_len == sizeof(i386_regs)) {
2146 /* Sign extend from 32 bits */
2147 rax = (int32_t)i386_regs.eax;
2148 } else {
2149 rax = x86_64_regs.rax;
2150 }
2151 if (check_errno && is_negated_errno(rax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002152 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002153 u_error = -rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002154 }
2155 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002156 tcp->u_rval = rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002157 }
2158#elif defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002159 /* In X32, return value is 64-bit (llseek uses one).
2160 * Using merely "long rax" would not work.
2161 */
2162 long long rax;
2163 if (x86_io.iov_len == sizeof(i386_regs)) {
2164 /* Sign extend from 32 bits */
2165 rax = (int32_t)i386_regs.eax;
2166 } else {
2167 rax = x86_64_regs.rax;
2168 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002169 /* Careful: is_negated_errno() works only on longs */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002170 if (check_errno && is_negated_errno_x32(rax)) {
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002171 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002172 u_error = -rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002173 }
2174 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002175 tcp->u_rval = rax; /* truncating */
2176 tcp->u_lrval = rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002177 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002178#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002179 if (ia32) {
2180 int err;
2181
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002182 err = (int)ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002183 if (check_errno && is_negated_errno(err)) {
2184 tcp->u_rval = -1;
2185 u_error = -err;
2186 }
2187 else {
2188 tcp->u_rval = err;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002189 }
2190 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002191 if (check_errno && ia64_r10) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002192 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002193 u_error = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002194 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002195 tcp->u_rval = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002196 }
2197 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002198#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002199 if (check_errno && mips_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002200 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002201 u_error = mips_r2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002202 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002203 tcp->u_rval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002204# if defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002205 tcp->u_lrval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002206# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002207 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002208#elif defined(POWERPC)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002209 if (check_errno && is_negated_errno(ppc_result)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002210 tcp->u_rval = -1;
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002211 u_error = -ppc_result;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002212 }
2213 else {
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002214 tcp->u_rval = ppc_result;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002215 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002216#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002217 if (check_errno && is_negated_errno(m68k_d0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002218 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002219 u_error = -m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002220 }
2221 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002222 tcp->u_rval = m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002223 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002224#elif defined(ARM) || defined(AARCH64)
2225# if defined(AARCH64)
2226 if (tcp->currpers == 1) {
2227 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2228 tcp->u_rval = -1;
2229 u_error = -aarch64_regs.regs[0];
2230 }
2231 else {
2232 tcp->u_rval = aarch64_regs.regs[0];
2233 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002234 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002235 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002236# endif
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002237 {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002238 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002239 tcp->u_rval = -1;
Denys Vlasenko401374e2013-02-06 18:24:39 +01002240 u_error = -arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002241 }
2242 else {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002243 tcp->u_rval = arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002244 }
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002245 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002246#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002247 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002248 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002249 u_error = -avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002250 }
2251 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002252 tcp->u_rval = avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002253 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002254#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002255 if (check_errno && is_negated_errno(bfin_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002256 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002257 u_error = -bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002258 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002259 tcp->u_rval = bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002260 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002261#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002262 if (check_errno && alpha_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002263 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002264 u_error = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002265 }
2266 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002267 tcp->u_rval = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002268 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002269#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002270 if (check_errno && sparc_regs.psr & PSR_C) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002271 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002272 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002273 }
2274 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002275 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002276 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002277#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002278 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002279 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002280 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002281 }
2282 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002283 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002284 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002285#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002286 if (check_errno && is_negated_errno(hppa_r28)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002287 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002288 u_error = -hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002289 }
2290 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002291 tcp->u_rval = hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002292 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002293#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002294 if (check_errno && is_negated_errno(sh_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002295 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002296 u_error = -sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002297 }
2298 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002299 tcp->u_rval = sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002300 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002301#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002302 if (check_errno && is_negated_errno(sh64_r9)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002303 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002304 u_error = -sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002305 }
2306 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002307 tcp->u_rval = sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002308 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002309#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002310 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002311 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002312 u_error = -cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002313 }
2314 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002315 tcp->u_rval = cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002316 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002317#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002318 /*
2319 * The standard tile calling convention returns the value (or negative
2320 * errno) in r0, and zero (or positive errno) in r1.
2321 * Until at least kernel 3.8, however, the r1 value is not reflected
2322 * in ptregs at this point, so we use r0 here.
2323 */
2324 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002325 tcp->u_rval = -1;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002326 u_error = -tile_regs.regs[0];
2327 } else {
2328 tcp->u_rval = tile_regs.regs[0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002329 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002330#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002331 if (check_errno && is_negated_errno(microblaze_r3)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002332 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002333 u_error = -microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002334 }
2335 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002336 tcp->u_rval = microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002337 }
Christian Svensson492f81f2013-02-14 13:26:27 +01002338#elif defined(OR1K)
2339 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2340 tcp->u_rval = -1;
2341 u_error = -or1k_regs.gpr[11];
2342 }
2343 else {
2344 tcp->u_rval = or1k_regs.gpr[11];
2345 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002346#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002347 tcp->u_error = u_error;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002348}
2349
2350static void
2351dumpio(struct tcb *tcp)
2352{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002353 int (*func)();
2354
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002355 if (syserror(tcp))
2356 return;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +01002357 if ((unsigned long) tcp->u_arg[0] >= num_quals)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002358 return;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002359 func = tcp->s_ent->sys_func;
2360 if (func == printargs)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002361 return;
2362 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002363 if (func == sys_read ||
2364 func == sys_pread ||
2365 func == sys_recv ||
2366 func == sys_recvfrom)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002367 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002368 else if (func == sys_readv)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002369 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2370 return;
2371 }
2372 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002373 if (func == sys_write ||
2374 func == sys_pwrite ||
2375 func == sys_send ||
2376 func == sys_sendto)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002377 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002378 else if (func == sys_writev)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002379 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2380 return;
2381 }
2382}
2383
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002384static int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002385trace_syscall_exiting(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002386{
2387 int sys_res;
2388 struct timeval tv;
Denys Vlasenko1a5b5a72011-08-25 00:29:56 +02002389 int res;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002390 long u_error;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002391
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002392 /* Measure the exit time as early as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002393 if (Tflag || cflag)
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002394 gettimeofday(&tv, NULL);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002395
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00002396#if SUPPORTED_PERSONALITIES > 1
2397 update_personality(tcp, tcp->currpers);
2398#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002399 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002400 if (res == 1) {
2401 syscall_fixup_on_sysexit(tcp); /* never fails */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002402 get_error(tcp); /* never fails */
2403 if (need_fork_exec_workarounds)
2404 syscall_fixup_for_fork_exec(tcp);
2405 if (filtered(tcp))
2406 goto ret;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002407 }
2408
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002409 if (cflag) {
2410 struct timeval t = tv;
Denys Vlasenkoc95a88f2011-08-21 17:47:40 +02002411 count_syscall(tcp, &t);
Denys Vlasenko7b609d52011-06-22 14:32:43 +02002412 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002413 goto ret;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002414 }
2415 }
2416
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002417 /* If not in -ff mode, and printing_tcp != tcp,
2418 * then the log currently does not end with output
2419 * of _our syscall entry_, but with something else.
2420 * We need to say which syscall's return is this.
2421 *
2422 * Forced reprinting via TCB_REPRINT is used only by
2423 * "strace -ff -oLOG test/threaded_execve" corner case.
2424 * It's the only case when -ff mode needs reprinting.
2425 */
2426 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2427 tcp->flags &= ~TCB_REPRINT;
2428 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002429 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002430 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002431 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002432 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002433 }
2434 printing_tcp = tcp;
2435
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002436 if (res != 1) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002437 /* There was error in one of prior ptrace ops */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002438 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002439 tabto();
Denys Vlasenko000b6012012-01-28 01:25:03 +01002440 tprints("= ? <unavailable>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002441 line_ended();
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002442 tcp->flags &= ~TCB_INSYSCALL;
2443 return res;
2444 }
2445
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002446 sys_res = 0;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002447 if (tcp->qual_flg & QUAL_RAW) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002448 /* sys_res = printargs(tcp); - but it's nop on sysexit */
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002449 } else {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002450 /* FIXME: not_failing_only (IOW, option -z) is broken:
2451 * failure of syscall is known only after syscall return.
2452 * Thus we end up with something like this on, say, ENOENT:
2453 * open("doesnt_exist", O_RDONLY <unfinished ...>
2454 * {next syscall decode}
2455 * whereas the intended result is that open(...) line
2456 * is not shown at all.
2457 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002458 if (not_failing_only && tcp->u_error)
Denys Vlasenko3b738812011-08-22 02:06:35 +02002459 goto ret; /* ignore failed syscalls */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002460 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002461 }
2462
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002463 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002464 tabto();
Denys Vlasenko3b738812011-08-22 02:06:35 +02002465 u_error = tcp->u_error;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002466 if (tcp->qual_flg & QUAL_RAW) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002467 if (u_error)
2468 tprintf("= -1 (errno %ld)", u_error);
2469 else
2470 tprintf("= %#lx", tcp->u_rval);
2471 }
2472 else if (!(sys_res & RVAL_NONE) && u_error) {
2473 switch (u_error) {
Denys Vlasenkofe585652012-01-12 11:26:34 +01002474 /* Blocked signals do not interrupt any syscalls.
2475 * In this case syscalls don't return ERESTARTfoo codes.
2476 *
2477 * Deadly signals set to SIG_DFL interrupt syscalls
2478 * and kill the process regardless of which of the codes below
2479 * is returned by the interrupted syscall.
2480 * In some cases, kernel forces a kernel-generated deadly
2481 * signal to be unblocked and set to SIG_DFL (and thus cause
2482 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2483 * or SIGILL. (The alternative is to leave process spinning
2484 * forever on the faulty instruction - not useful).
2485 *
2486 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2487 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2488 * but kernel will always restart them.
2489 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002490 case ERESTARTSYS:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002491 /* Most common type of signal-interrupted syscall exit code.
2492 * The system call will be restarted with the same arguments
2493 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2494 */
2495 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002496 break;
2497 case ERESTARTNOINTR:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002498 /* Rare. For example, fork() returns this if interrupted.
2499 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2500 */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002501 tprints("= ? ERESTARTNOINTR (To be restarted)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002502 break;
2503 case ERESTARTNOHAND:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002504 /* pause(), rt_sigsuspend() etc use this code.
2505 * SA_RESTART is ignored (assumed not set):
2506 * syscall won't restart (will return EINTR instead)
Denys Vlasenko30c03232013-02-19 16:59:26 +01002507 * even after signal with SA_RESTART set. However,
2508 * after SIG_IGN or SIG_DFL signal it will restart
2509 * (thus the name "restart only if has no handler").
Denys Vlasenkofe585652012-01-12 11:26:34 +01002510 */
2511 tprints("= ? ERESTARTNOHAND (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002512 break;
2513 case ERESTART_RESTARTBLOCK:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002514 /* Syscalls like nanosleep(), poll() which can't be
2515 * restarted with their original arguments use this
2516 * code. Kernel will execute restart_syscall() instead,
2517 * which changes arguments before restarting syscall.
2518 * SA_RESTART is ignored (assumed not set) similarly
2519 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2520 * since restart data is saved in "restart block"
2521 * in task struct, and if signal handler uses a syscall
2522 * which in turn saves another such restart block,
2523 * old data is lost and restart becomes impossible)
2524 */
2525 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002526 break;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002527 default:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002528 if (u_error < 0)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002529 tprintf("= -1 E??? (errno %ld)", u_error);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002530 else if (u_error < nerrnos)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002531 tprintf("= -1 %s (%s)", errnoent[u_error],
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002532 strerror(u_error));
2533 else
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002534 tprintf("= -1 ERRNO_%ld (%s)", u_error,
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002535 strerror(u_error));
2536 break;
2537 }
2538 if ((sys_res & RVAL_STR) && tcp->auxstr)
2539 tprintf(" (%s)", tcp->auxstr);
2540 }
2541 else {
2542 if (sys_res & RVAL_NONE)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002543 tprints("= ?");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002544 else {
2545 switch (sys_res & RVAL_MASK) {
2546 case RVAL_HEX:
2547 tprintf("= %#lx", tcp->u_rval);
2548 break;
2549 case RVAL_OCTAL:
2550 tprintf("= %#lo", tcp->u_rval);
2551 break;
2552 case RVAL_UDECIMAL:
2553 tprintf("= %lu", tcp->u_rval);
2554 break;
2555 case RVAL_DECIMAL:
2556 tprintf("= %ld", tcp->u_rval);
2557 break;
H.J. Ludd0130b2012-04-16 12:16:45 +02002558#if defined(LINUX_MIPSN32) || defined(X32)
2559 /*
2560 case RVAL_LHEX:
2561 tprintf("= %#llx", tcp->u_lrval);
2562 break;
2563 case RVAL_LOCTAL:
2564 tprintf("= %#llo", tcp->u_lrval);
2565 break;
2566 */
2567 case RVAL_LUDECIMAL:
2568 tprintf("= %llu", tcp->u_lrval);
2569 break;
2570 /*
2571 case RVAL_LDECIMAL:
2572 tprintf("= %lld", tcp->u_lrval);
2573 break;
2574 */
2575#endif
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002576 default:
2577 fprintf(stderr,
2578 "invalid rval format\n");
2579 break;
2580 }
2581 }
2582 if ((sys_res & RVAL_STR) && tcp->auxstr)
2583 tprintf(" (%s)", tcp->auxstr);
2584 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002585 if (Tflag) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002586 tv_sub(&tv, &tv, &tcp->etime);
2587 tprintf(" <%ld.%06ld>",
2588 (long) tv.tv_sec, (long) tv.tv_usec);
2589 }
Denys Vlasenko000b6012012-01-28 01:25:03 +01002590 tprints("\n");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002591 dumpio(tcp);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002592 line_ended();
2593
Denys Vlasenko3b738812011-08-22 02:06:35 +02002594 ret:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002595 tcp->flags &= ~TCB_INSYSCALL;
2596 return 0;
2597}
2598
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002599int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002600trace_syscall(struct tcb *tcp)
2601{
2602 return exiting(tcp) ?
2603 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2604}