blob: 1b49bb3c159447058762592d14249a8a73ee5ecd [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00005 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Linux for s390 port by D.J. Barrow
8 * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00009 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000032 */
33
34#include "defs.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000035#include <sys/user.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000036#include <sys/param.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000037
Wichert Akkerman15dea971999-10-06 13:06:34 +000038#ifdef HAVE_SYS_REG_H
Denys Vlasenko523635f2012-02-25 02:44:25 +010039# include <sys/reg.h>
40# ifndef PTRACE_PEEKUSR
41# define PTRACE_PEEKUSR PTRACE_PEEKUSER
42# endif
Wichert Akkermanfaf72222000-02-19 23:59:03 +000043#elif defined(HAVE_LINUX_PTRACE_H)
Denys Vlasenko523635f2012-02-25 02:44:25 +010044# undef PTRACE_SYSCALL
Roland McGrathce9f0742004-03-01 21:29:22 +000045# ifdef HAVE_STRUCT_IA64_FPREG
46# define ia64_fpreg XXX_ia64_fpreg
47# endif
48# ifdef HAVE_STRUCT_PT_ALL_USER_REGS
49# define pt_all_user_regs XXX_pt_all_user_regs
50# endif
Denys Vlasenko523635f2012-02-25 02:44:25 +010051# include <linux/ptrace.h>
Roland McGrathce9f0742004-03-01 21:29:22 +000052# undef ia64_fpreg
53# undef pt_all_user_regs
Wichert Akkerman15dea971999-10-06 13:06:34 +000054#endif
55
Denys Vlasenko84703742012-02-25 02:38:52 +010056#if defined(SPARC64)
Roland McGrath6d1a65c2004-07-12 07:44:08 +000057# undef PTRACE_GETREGS
58# define PTRACE_GETREGS PTRACE_GETREGS64
59# undef PTRACE_SETREGS
60# define PTRACE_SETREGS PTRACE_SETREGS64
Denys Vlasenko84703742012-02-25 02:38:52 +010061#endif
Roland McGrath6d1a65c2004-07-12 07:44:08 +000062
Denys Vlasenko84703742012-02-25 02:38:52 +010063#if defined(IA64)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000064# include <asm/ptrace_offsets.h>
65# include <asm/rse.h>
66#endif
67
Dmitry V. Levin32c049a2013-03-18 00:59:27 +000068/* for struct iovec */
69#include <sys/uio.h>
70/* for NT_PRSTATUS */
71#ifdef HAVE_ELF_H
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010072# include <elf.h>
73#endif
74
Steve McIntyred8d3bd32012-10-24 17:58:16 +010075#if defined(AARCH64)
76# include <asm/ptrace.h>
James Hogan5f999a82013-02-22 14:44:10 +000077#endif
78
Chris Zankel8f636ed2013-03-25 10:22:07 -070079#if defined(XTENSA)
80# include <asm/ptrace.h>
81#endif
82
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000083#ifndef NSIG
Denys Vlasenko523635f2012-02-25 02:44:25 +010084# warning: NSIG is not defined, using 32
85# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000086#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000087
88#include "syscall.h"
89
90/* Define these shorthand notations to simplify the syscallent files. */
Roland McGrath2fe7b132005-07-05 03:25:35 +000091#define TD TRACE_DESC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000092#define TF TRACE_FILE
93#define TI TRACE_IPC
94#define TN TRACE_NETWORK
95#define TP TRACE_PROCESS
96#define TS TRACE_SIGNAL
Namhyung Kim96792962012-10-24 11:41:57 +090097#define TM TRACE_MEMORY
Dmitry V. Levin50a218d2011-01-18 17:36:20 +000098#define NF SYSCALL_NEVER_FAILS
Denys Vlasenkoac1ce772011-08-23 13:24:17 +020099#define MA MAX_ARGS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000100
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100101const struct_sysent sysent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000102#include "syscallent.h"
103};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000104
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100105#if SUPPORTED_PERSONALITIES > 1
106static const struct_sysent sysent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100107# include "syscallent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000108};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200109#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000110
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100111#if SUPPORTED_PERSONALITIES > 2
112static const struct_sysent sysent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100113# include "syscallent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000114};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200115#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000116
117/* Now undef them since short defines cause wicked namespace pollution. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000118#undef TD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000119#undef TF
120#undef TI
121#undef TN
122#undef TP
123#undef TS
Namhyung Kim96792962012-10-24 11:41:57 +0900124#undef TM
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000125#undef NF
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200126#undef MA
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000127
Denys Vlasenko39fca622011-08-20 02:12:33 +0200128/*
129 * `ioctlent.h' may be generated from `ioctlent.raw' by the auxiliary
130 * program `ioctlsort', such that the list is sorted by the `code' field.
131 * This has the side-effect of resolving the _IO.. macros into
132 * plain integers, eliminating the need to include here everything
133 * in "/usr/include".
134 */
135
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100136const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000137#include "errnoent.h"
138};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100139const char *const signalent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200140#include "signalent.h"
141};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100142const struct_ioctlent ioctlent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200143#include "ioctlent.h"
144};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000145
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100146#if SUPPORTED_PERSONALITIES > 1
Roland McGrathee36ce12004-09-04 03:53:10 +0000147static const char *const errnoent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100148# include "errnoent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000149};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200150static const char *const signalent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100151# include "signalent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200152};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100153static const struct_ioctlent ioctlent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100154# include "ioctlent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200155};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200156#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000157
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100158#if SUPPORTED_PERSONALITIES > 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000159static const char *const errnoent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100160# include "errnoent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000161};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200162static const char *const signalent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100163# include "signalent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200164};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100165static const struct_ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100166# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200167};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200168#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000169
Dmitry V. Levine6f55242013-02-26 22:02:30 +0000170enum {
171 nsyscalls0 = ARRAY_SIZE(sysent0)
172#if SUPPORTED_PERSONALITIES > 1
173 , nsyscalls1 = ARRAY_SIZE(sysent1)
174# if SUPPORTED_PERSONALITIES > 2
175 , nsyscalls2 = ARRAY_SIZE(sysent2)
176# endif
177#endif
178};
179
180enum {
181 nerrnos0 = ARRAY_SIZE(errnoent0)
182#if SUPPORTED_PERSONALITIES > 1
183 , nerrnos1 = ARRAY_SIZE(errnoent1)
184# if SUPPORTED_PERSONALITIES > 2
185 , nerrnos2 = ARRAY_SIZE(errnoent2)
186# endif
187#endif
188};
189
190enum {
191 nsignals0 = ARRAY_SIZE(signalent0)
192#if SUPPORTED_PERSONALITIES > 1
193 , nsignals1 = ARRAY_SIZE(signalent1)
194# if SUPPORTED_PERSONALITIES > 2
195 , nsignals2 = ARRAY_SIZE(signalent2)
196# endif
197#endif
198};
199
200enum {
201 nioctlents0 = ARRAY_SIZE(ioctlent0)
202#if SUPPORTED_PERSONALITIES > 1
203 , nioctlents1 = ARRAY_SIZE(ioctlent1)
204# if SUPPORTED_PERSONALITIES > 2
205 , nioctlents2 = ARRAY_SIZE(ioctlent2)
206# endif
207#endif
208};
209
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100210#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100211const struct_sysent *sysent = sysent0;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100212const char *const *errnoent = errnoent0;
213const char *const *signalent = signalent0;
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100214const struct_ioctlent *ioctlent = ioctlent0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100215#endif
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100216unsigned nsyscalls = nsyscalls0;
217unsigned nerrnos = nerrnos0;
218unsigned nsignals = nsignals0;
219unsigned nioctlents = nioctlents0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100220
221unsigned num_quals;
222qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
223
224static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
225 nsyscalls0,
226#if SUPPORTED_PERSONALITIES > 1
227 nsyscalls1,
228#endif
229#if SUPPORTED_PERSONALITIES > 2
230 nsyscalls2,
231#endif
232};
233static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
234 sysent0,
235#if SUPPORTED_PERSONALITIES > 1
236 sysent1,
237#endif
238#if SUPPORTED_PERSONALITIES > 2
239 sysent2,
240#endif
241};
242
243enum {
244 MAX_NSYSCALLS1 = (nsyscalls0
245#if SUPPORTED_PERSONALITIES > 1
246 > nsyscalls1 ? nsyscalls0 : nsyscalls1
247#endif
248 ),
249 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
250#if SUPPORTED_PERSONALITIES > 2
251 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
252#endif
253 ),
254 MAX_NSYSCALLS = MAX_NSYSCALLS2,
255 /* We are ready for arches with up to 255 signals,
256 * even though the largest known signo is on MIPS and it is 128.
257 * The number of existing syscalls on all arches is
258 * larger that 255 anyway, so it is just a pedantic matter.
259 */
260 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
261};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000262
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100263#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100264unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000265
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100266# ifndef current_wordsize
267unsigned current_wordsize;
268static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000269 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000270 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100271# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000272 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100273# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200274};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100275# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000276
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200277void
Dmitry V. Levin3abe8b22006-12-20 22:37:21 +0000278set_personality(int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000279{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100280 nsyscalls = nsyscall_vec[personality];
281 sysent = sysent_vec[personality];
282
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000283 switch (personality) {
284 case 0:
285 errnoent = errnoent0;
286 nerrnos = nerrnos0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000287 ioctlent = ioctlent0;
288 nioctlents = nioctlents0;
289 signalent = signalent0;
290 nsignals = nsignals0;
291 break;
292
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000293 case 1:
294 errnoent = errnoent1;
295 nerrnos = nerrnos1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000296 ioctlent = ioctlent1;
297 nioctlents = nioctlents1;
298 signalent = signalent1;
299 nsignals = nsignals1;
300 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000301
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100302# if SUPPORTED_PERSONALITIES > 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000303 case 2:
304 errnoent = errnoent2;
305 nerrnos = nerrnos2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000306 ioctlent = ioctlent2;
307 nioctlents = nioctlents2;
308 signalent = signalent2;
309 nsignals = nsignals2;
310 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100311# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000312 }
313
314 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100315# ifndef current_wordsize
316 current_wordsize = personality_wordsize[personality];
317# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000318}
319
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000320static void
321update_personality(struct tcb *tcp, int personality)
322{
323 if (personality == current_personality)
324 return;
325 set_personality(personality);
326
327 if (personality == tcp->currpers)
328 return;
329 tcp->currpers = personality;
330
H.J. Lu35be5812012-04-16 13:00:01 +0200331# if defined(POWERPC64)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000332 if (!qflag) {
333 static const char *const names[] = {"64 bit", "32 bit"};
334 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
335 tcp->pid, names[personality]);
336 }
H.J. Lu35be5812012-04-16 13:00:01 +0200337# elif defined(X86_64)
338 if (!qflag) {
339 static const char *const names[] = {"64 bit", "32 bit", "x32"};
340 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
341 tcp->pid, names[personality]);
342 }
H.J. Lu085e4282012-04-17 11:05:04 -0700343# elif defined(X32)
344 if (!qflag) {
345 static const char *const names[] = {"x32", "32 bit"};
346 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
347 tcp->pid, names[personality]);
348 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000349# elif defined(AARCH64)
350 if (!qflag) {
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100351 static const char *const names[] = {"32-bit", "AArch64"};
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000352 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
353 tcp->pid, names[personality]);
354 }
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100355# elif defined(TILE)
356 if (!qflag) {
357 static const char *const names[] = {"64-bit", "32-bit"};
358 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
359 tcp->pid, names[personality]);
360 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100361# endif
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000362}
363#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000364
Denys Vlasenkoc1540fe2013-02-21 16:17:08 +0100365static int qual_syscall(), qual_signal(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000366
Roland McGrathe10e62a2004-09-04 04:20:43 +0000367static const struct qual_options {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000368 int bitflag;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000369 const char *option_name;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000370 int (*qualify)(const char *, int, int);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000371 const char *argument_name;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000372} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000373 { QUAL_TRACE, "trace", qual_syscall, "system call" },
374 { QUAL_TRACE, "t", qual_syscall, "system call" },
375 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
376 { QUAL_ABBREV, "a", qual_syscall, "system call" },
377 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
378 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
379 { QUAL_RAW, "raw", qual_syscall, "system call" },
380 { QUAL_RAW, "x", qual_syscall, "system call" },
381 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
382 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
383 { QUAL_SIGNAL, "s", qual_signal, "signal" },
Roland McGrath9797ceb2002-12-30 10:23:00 +0000384 { QUAL_READ, "read", qual_desc, "descriptor" },
385 { QUAL_READ, "reads", qual_desc, "descriptor" },
386 { QUAL_READ, "r", qual_desc, "descriptor" },
387 { QUAL_WRITE, "write", qual_desc, "descriptor" },
388 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
389 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000390 { 0, NULL, NULL, NULL },
391};
392
Roland McGrath9797ceb2002-12-30 10:23:00 +0000393static void
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100394reallocate_qual(int n)
395{
396 unsigned p;
397 qualbits_t *qp;
398 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
399 qp = qual_vec[p] = realloc(qual_vec[p], n * sizeof(qualbits_t));
400 if (!qp)
401 die_out_of_memory();
402 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
403 }
404 num_quals = n;
405}
406
407static void
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000408qualify_one(int n, int bitflag, int not, int pers)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000409{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100410 unsigned p;
Roland McGrath138c6a32006-01-12 09:50:49 +0000411
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100412 if (num_quals <= n)
413 reallocate_qual(n + 1);
Roland McGrath138c6a32006-01-12 09:50:49 +0000414
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100415 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
416 if (pers == p || pers < 0) {
417 if (not)
418 qual_vec[p][n] &= ~bitflag;
419 else
420 qual_vec[p][n] |= bitflag;
421 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000422 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000423}
424
425static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000426qual_syscall(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000427{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100428 unsigned p;
429 unsigned i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000430 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000431
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100432 if (*s >= '0' && *s <= '9') {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100433 i = string_to_uint(s);
Denys Vlasenkob43dacd2013-02-23 18:19:28 +0100434 if (i >= MAX_NSYSCALLS)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000435 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000436 qualify_one(i, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000437 return 0;
Roland McGrath48a035f2006-01-12 09:45:56 +0000438 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000439
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100440 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
441 for (i = 0; i < nsyscall_vec[p]; i++) {
442 if (sysent_vec[p][i].sys_name
443 && strcmp(s, sysent_vec[p][i].sys_name) == 0
444 ) {
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000445 qualify_one(i, bitflag, not, p);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100446 rc = 0;
447 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000448 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100449 }
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000450
Roland McGrathfe6b3522005-02-02 04:40:11 +0000451 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000452}
453
454static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000455qual_signal(const char *s, int bitflag, int not)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000456{
457 int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000458
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100459 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000460 int signo = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100461 if (signo < 0 || signo > 255)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000462 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000463 qualify_one(signo, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000464 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000465 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000466 if (strncasecmp(s, "SIG", 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000467 s += 3;
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100468 for (i = 0; i <= NSIG; i++) {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000469 if (strcasecmp(s, signame(i) + 3) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000470 qualify_one(i, bitflag, not, -1);
Roland McGrath76421df2005-02-02 03:51:18 +0000471 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000472 }
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100473 }
Roland McGrath76421df2005-02-02 03:51:18 +0000474 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000475}
476
477static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000478qual_desc(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000479{
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100480 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000481 int desc = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100482 if (desc < 0 || desc > 0x7fff) /* paranoia */
Roland McGrathfe6b3522005-02-02 04:40:11 +0000483 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000484 qualify_one(desc, bitflag, not, -1);
Roland McGrath2b619022003-04-10 18:58:20 +0000485 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000486 }
487 return -1;
488}
489
490static int
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000491lookup_class(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000492{
493 if (strcmp(s, "file") == 0)
494 return TRACE_FILE;
495 if (strcmp(s, "ipc") == 0)
496 return TRACE_IPC;
497 if (strcmp(s, "network") == 0)
498 return TRACE_NETWORK;
499 if (strcmp(s, "process") == 0)
500 return TRACE_PROCESS;
501 if (strcmp(s, "signal") == 0)
502 return TRACE_SIGNAL;
Roland McGrath2fe7b132005-07-05 03:25:35 +0000503 if (strcmp(s, "desc") == 0)
504 return TRACE_DESC;
Namhyung Kim96792962012-10-24 11:41:57 +0900505 if (strcmp(s, "memory") == 0)
506 return TRACE_MEMORY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000507 return -1;
508}
509
510void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000511qualify(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000512{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000513 const struct qual_options *opt;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000514 int not;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000515 char *copy;
516 const char *p;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000517 int i, n;
518
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100519 if (num_quals == 0)
520 reallocate_qual(MIN_QUALS);
521
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000522 opt = &qual_options[0];
523 for (i = 0; (p = qual_options[i].option_name); i++) {
524 n = strlen(p);
525 if (strncmp(s, p, n) == 0 && s[n] == '=') {
526 opt = &qual_options[i];
527 s += n + 1;
528 break;
529 }
530 }
531 not = 0;
532 if (*s == '!') {
533 not = 1;
534 s++;
535 }
536 if (strcmp(s, "none") == 0) {
537 not = 1 - not;
538 s = "all";
539 }
540 if (strcmp(s, "all") == 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100541 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000542 qualify_one(i, opt->bitflag, not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000543 }
544 return;
545 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100546 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000547 qualify_one(i, opt->bitflag, !not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000548 }
Denys Vlasenko5d645812011-08-20 12:48:18 +0200549 copy = strdup(s);
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200550 if (!copy)
551 die_out_of_memory();
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000552 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000553 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100554 unsigned pers;
555 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
556 for (i = 0; i < nsyscall_vec[pers]; i++)
557 if (sysent_vec[pers][i].sys_flags & n)
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000558 qualify_one(i, opt->bitflag, not, pers);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100559 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000560 continue;
561 }
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000562 if (opt->qualify(p, opt->bitflag, not)) {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100563 error_msg_and_die("invalid %s '%s'",
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000564 opt->argument_name, p);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000565 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000566 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000567 free(copy);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000568 return;
569}
570
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000571#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000572static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000573decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000574{
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000575 unsigned long addr;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100576 unsigned int i, n, size;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000577
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000578 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
579 return;
580
581 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100582 tcp->qual_flg = qual_flags[tcp->scno];
583 tcp->s_ent = &sysent[tcp->scno];
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000584 addr = tcp->u_arg[1];
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100585 size = current_wordsize;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100586 n = tcp->s_ent->nargs;
587 for (i = 0; i < n; ++i) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000588 if (size == sizeof(int)) {
589 unsigned int arg;
590 if (umove(tcp, addr, &arg) < 0)
591 arg = 0;
592 tcp->u_arg[i] = arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000593 }
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000594 else {
595 unsigned long arg;
596 if (umove(tcp, addr, &arg) < 0)
597 arg = 0;
598 tcp->u_arg[i] = arg;
599 }
600 addr += size;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000601 }
602}
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000603#endif
Mike Frysinger3362e892012-03-15 01:09:19 -0400604
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000605#ifdef SYS_ipc_subcall
606static void
607decode_ipc_subcall(struct tcb *tcp)
608{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100609 unsigned int i, n;
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000610
611 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
612 return;
613
614 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100615 tcp->qual_flg = qual_flags[tcp->scno];
616 tcp->s_ent = &sysent[tcp->scno];
617 n = tcp->s_ent->nargs;
618 for (i = 0; i < n; i++)
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000619 tcp->u_arg[i] = tcp->u_arg[i + 1];
620}
621#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000622
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200623int
624printargs(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000625{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200626 if (entering(tcp)) {
627 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100628 int n = tcp->s_ent->nargs;
629 for (i = 0; i < n; i++)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200630 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
631 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000632 return 0;
633}
634
Denys Vlasenko72879c62012-02-27 14:18:02 +0100635int
636printargs_lu(struct tcb *tcp)
637{
638 if (entering(tcp)) {
639 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100640 int n = tcp->s_ent->nargs;
641 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100642 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
643 }
644 return 0;
645}
646
647int
648printargs_ld(struct tcb *tcp)
649{
650 if (entering(tcp)) {
651 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100652 int n = tcp->s_ent->nargs;
653 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100654 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
655 }
656 return 0;
657}
658
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100659#if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200660long
661getrval2(struct tcb *tcp)
662{
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100663 long val;
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200664
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100665# if defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100666 val = sparc_regs.u_regs[U_REG_O1];
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100667# elif defined(SH)
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200668 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200669 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100670# elif defined(IA64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200671 if (upeek(tcp->pid, PT_R9, &val) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200672 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100673# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200674
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200675 return val;
676}
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100677#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200678
Denys Vlasenko523635f2012-02-25 02:44:25 +0100679#if defined(I386)
Denys Vlasenkob51f3642013-07-16 12:06:25 +0200680static struct user_regs_struct i386_regs;
Denys Vlasenkoe600ac62013-07-18 17:40:45 +0200681/* Cast suppresses signedness warning (.esp is long, not unsigned long) */
682uint32_t *const i386_esp_ptr = (uint32_t*)&i386_regs.esp;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000683# define ARCH_REGS_FOR_GETREGSET i386_regs
H.J. Lu35be5812012-04-16 13:00:01 +0200684#elif defined(X86_64) || defined(X32)
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100685/*
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100686 * On i386, pt_regs and user_regs_struct are the same,
687 * but on 64 bit x86, user_regs_struct has six more fields:
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100688 * fs_base, gs_base, ds, es, fs, gs.
689 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
690 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100691struct i386_user_regs_struct {
692 uint32_t ebx;
693 uint32_t ecx;
694 uint32_t edx;
695 uint32_t esi;
696 uint32_t edi;
697 uint32_t ebp;
698 uint32_t eax;
699 uint32_t xds;
700 uint32_t xes;
701 uint32_t xfs;
702 uint32_t xgs;
703 uint32_t orig_eax;
704 uint32_t eip;
705 uint32_t xcs;
706 uint32_t eflags;
707 uint32_t esp;
708 uint32_t xss;
709};
710static union {
711 struct user_regs_struct x86_64_r;
712 struct i386_user_regs_struct i386_r;
713} x86_regs_union;
714# define x86_64_regs x86_regs_union.x86_64_r
715# define i386_regs x86_regs_union.i386_r
Denys Vlasenkob51f3642013-07-16 12:06:25 +0200716uint32_t *const i386_esp_ptr = &i386_regs.esp;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100717static struct iovec x86_io = {
718 .iov_base = &x86_regs_union
719};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100720#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +0200721bool ia64_ia32mode = 0; /* not static */
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100722static long ia64_r8, ia64_r10;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100723#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +0200724struct pt_regs ppc_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100725#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100726static long m68k_d0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100727#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100728static long bfin_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100729#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100730struct pt_regs arm_regs; /* not static */
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000731# define ARCH_REGS_FOR_GETREGSET arm_regs
Steve McIntyred8d3bd32012-10-24 17:58:16 +0100732#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100733static union {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100734 struct user_pt_regs aarch64_r;
735 struct arm_pt_regs arm_r;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100736} arm_regs_union;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100737# define aarch64_regs arm_regs_union.aarch64_r
738# define arm_regs arm_regs_union.arm_r
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100739static struct iovec aarch64_io = {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100740 .iov_base = &arm_regs_union
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100741};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100742#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100743static long alpha_r0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100744static long alpha_a3;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100745#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100746static struct pt_regs avr32_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100747#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100748struct pt_regs sparc_regs; /* not static */
Denys Vlasenko523635f2012-02-25 02:44:25 +0100749#elif defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100750static long long mips_a3;
751static long long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100752#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100753static long mips_a3;
754static long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100755#elif defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +0200756static long s390_gpr2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100757#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100758static long hppa_r28;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100759#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100760static long sh_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100761#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100762static long sh64_r9;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100763#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100764static long cris_r10;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100765#elif defined(TILE)
766struct pt_regs tile_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100767#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100768static long microblaze_r3;
Christian Svensson492f81f2013-02-14 13:26:27 +0100769#elif defined(OR1K)
770static struct user_regs_struct or1k_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000771# define ARCH_REGS_FOR_GETREGSET or1k_regs
James Hogan5f999a82013-02-22 14:44:10 +0000772#elif defined(METAG)
773static struct user_gp_regs metag_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000774# define ARCH_REGS_FOR_GETREGSET metag_regs
Chris Zankel8f636ed2013-03-25 10:22:07 -0700775#elif defined(XTENSA)
776static long xtensa_a2;
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
Denys Vlasenko5a2483b2013-07-01 12:49:14 +0200780print_pc(struct tcb *tcp)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100781{
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;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200793 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100794 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 Vlasenko752e5a02013-06-28 14:35:47 +0200815 if (upeek(tcp->pid, PT_B0, &ip) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100816 PRINTBADPC;
817 return;
818 }
819 tprintf("[%08lx] ", ip);
820#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +0200821 long pc = ppc_regs.nip;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100822# ifdef POWERPC64
823 tprintf("[%016lx] ", pc);
824# else
825 tprintf("[%08lx] ", pc);
826# endif
827#elif defined(M68K)
828 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200829 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100830 tprints("[????????] ");
831 return;
832 }
833 tprintf("[%08lx] ", pc);
834#elif defined(ALPHA)
835 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200836 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100837 tprints("[????????????????] ");
838 return;
839 }
840 tprintf("[%08lx] ", pc);
841#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100842 tprintf("[%08lx] ", sparc_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100843#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100844 tprintf("[%08lx] ", sparc_regs.tpc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100845#elif defined(HPPA)
846 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200847 if (upeek(tcp->pid, PT_IAOQ0, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100848 tprints("[????????] ");
849 return;
850 }
851 tprintf("[%08lx] ", pc);
852#elif defined(MIPS)
853 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200854 if (upeek(tcp->pid, REG_EPC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100855 tprints("[????????] ");
856 return;
857 }
858 tprintf("[%08lx] ", pc);
859#elif defined(SH)
860 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200861 if (upeek(tcp->pid, 4*REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100862 tprints("[????????] ");
863 return;
864 }
865 tprintf("[%08lx] ", pc);
866#elif defined(SH64)
867 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200868 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100869 tprints("[????????????????] ");
870 return;
871 }
872 tprintf("[%08lx] ", pc);
873#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100874 tprintf("[%08lx] ", arm_regs.ARM_pc);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100875#elif defined(AARCH64)
876 /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100877#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100878 tprintf("[%08lx] ", avr32_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100879#elif defined(BFIN)
880 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200881 if (upeek(tcp->pid, PT_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100882 PRINTBADPC;
883 return;
884 }
885 tprintf("[%08lx] ", pc);
886#elif defined(CRISV10)
887 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200888 if (upeek(tcp->pid, 4*PT_IRP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100889 PRINTBADPC;
890 return;
891 }
892 tprintf("[%08lx] ", pc);
893#elif defined(CRISV32)
894 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200895 if (upeek(tcp->pid, 4*PT_ERP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100896 PRINTBADPC;
897 return;
898 }
899 tprintf("[%08lx] ", pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100900#elif defined(TILE)
901# ifdef _LP64
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500902 tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100903# else
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500904 tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100905# endif
Christian Svensson492f81f2013-02-14 13:26:27 +0100906#elif defined(OR1K)
907 tprintf("[%08lx] ", or1k_regs.pc);
James Hogan5f999a82013-02-22 14:44:10 +0000908#elif defined(METAG)
909 tprintf("[%08lx] ", metag_regs.pc);
Chris Zankel8f636ed2013-03-25 10:22:07 -0700910#elif defined(XTENSA)
911 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200912 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Chris Zankel8f636ed2013-03-25 10:22:07 -0700913 PRINTBADPC;
914 return;
915 }
916 tprintf("[%08lx] ", pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100917#endif /* architecture */
918}
919
Denys Vlasenko7270de52013-02-21 15:46:34 +0100920/* Shuffle syscall numbers so that we don't have huge gaps in syscall table.
921 * The shuffling should be reversible: shuffle_scno(shuffle_scno(n)) == n.
922 */
923#if defined(ARM) /* So far only ARM needs this */
924static long
925shuffle_scno(unsigned long scno)
926{
927 if (scno <= ARM_LAST_ORDINARY_SYSCALL)
928 return scno;
929
930 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
931 if (scno == 0x000ffff0)
932 return ARM_LAST_ORDINARY_SYSCALL+1;
933 if (scno == ARM_LAST_ORDINARY_SYSCALL+1)
934 return 0x000ffff0;
935
936 /* Is it ARM specific syscall?
937 * Swap with [LAST_ORDINARY+2, LAST_ORDINARY+2 + LAST_SPECIAL] range.
938 */
939 if (scno >= 0x000f0000
940 && scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL
941 ) {
942 return scno - 0x000f0000 + (ARM_LAST_ORDINARY_SYSCALL+2);
943 }
944 if (/* scno >= ARM_LAST_ORDINARY_SYSCALL+2 - always true */ 1
945 && scno <= (ARM_LAST_ORDINARY_SYSCALL+2) + ARM_LAST_SPECIAL_SYSCALL
946 ) {
947 return scno + 0x000f0000 - (ARM_LAST_ORDINARY_SYSCALL+2);
948 }
949
950 return scno;
951}
952#else
953# define shuffle_scno(scno) ((long)(scno))
954#endif
955
956static char*
957undefined_scno_name(struct tcb *tcp)
958{
959 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
960
961 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
962 return buf;
963}
964
Anton Blanchard14d51a62013-06-26 14:42:37 +1000965#ifdef POWERPC
966/*
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200967 * PTRACE_GETREGS was added to the PowerPC kernel in v2.6.23,
968 * we provide a slow fallback for old kernels.
Anton Blanchard14d51a62013-06-26 14:42:37 +1000969 */
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200970static int powerpc_getregs_old(pid_t pid)
Anton Blanchard14d51a62013-06-26 14:42:37 +1000971{
972 int i;
973 long r;
974
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200975 if (iflag) {
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200976 r = upeek(pid, sizeof(long) * PT_NIP, (long *)&ppc_regs.nip);
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200977 if (r)
978 goto out;
979 }
980#ifdef POWERPC64 /* else we never use it */
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200981 r = upeek(pid, sizeof(long) * PT_MSR, (long *)&ppc_regs.msr);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000982 if (r)
983 goto out;
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200984#endif
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200985 r = upeek(pid, sizeof(long) * PT_CCR, (long *)&ppc_regs.ccr);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000986 if (r)
987 goto out;
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200988 r = upeek(pid, sizeof(long) * PT_ORIG_R3, (long *)&ppc_regs.orig_gpr3);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000989 if (r)
990 goto out;
Anton Blanchard14d51a62013-06-26 14:42:37 +1000991 for (i = 0; i <= 8; i++) {
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200992 r = upeek(pid, sizeof(long) * (PT_R0 + i),
993 (long *)&ppc_regs.gpr[i]);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000994 if (r)
995 goto out;
996 }
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200997 out:
Anton Blanchard14d51a62013-06-26 14:42:37 +1000998 return r;
999}
1000#endif
1001
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001002#ifndef get_regs
1003long get_regs_error;
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001004
1005#if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001006static void get_regset(pid_t pid)
1007{
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001008/* constant iovec */
1009# if defined(ARM) \
1010 || defined(I386) \
1011 || defined(METAG) \
1012 || defined(OR1K)
1013 static struct iovec io = {
1014 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1015 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001016 };
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001017 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1018
1019/* variable iovec */
1020# elif defined(X86_64) || defined(X32)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001021 /* x86_io.iov_base = &x86_regs_union; - already is */
1022 x86_io.iov_len = sizeof(x86_regs_union);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001023 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &x86_io);
1024# elif defined(AARCH64)
1025 /* aarch64_io.iov_base = &arm_regs_union; - already is */
1026 aarch64_io.iov_len = sizeof(arm_regs_union);
1027 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &aarch64_io);
1028# else
1029# warning both PTRACE_GETREGSET and NT_PRSTATUS are available but not yet used
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001030# endif
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001031}
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001032#endif /* PTRACE_GETREGSET && NT_PRSTATUS */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001033
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001034void
1035get_regs(pid_t pid)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001036{
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001037/* PTRACE_GETREGSET only */
1038# if defined(METAG) || defined(OR1K) || defined(X32) || defined(AARCH64)
1039 get_regset(pid);
1040
1041/* PTRACE_GETREGS only */
1042# elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001043 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001044# elif defined(TILE)
1045 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &tile_regs);
1046# elif defined(SPARC) || defined(SPARC64)
1047 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001048# elif defined(POWERPC)
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001049 static bool old_kernel = 0;
1050 if (old_kernel)
1051 goto old;
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001052 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &ppc_regs);
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001053 if (get_regs_error && errno == EIO) {
1054 old_kernel = 1;
1055 old:
Anton Blanchard14d51a62013-06-26 14:42:37 +10001056 get_regs_error = powerpc_getregs_old(pid);
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001057 }
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001058
1059/* try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS */
1060# else
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001061# if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
1062 static int getregset_support;
1063
1064 if (getregset_support >= 0) {
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001065 get_regset(pid);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001066 if (getregset_support > 0)
1067 return;
1068 if (get_regs_error >= 0) {
1069 getregset_support = 1;
1070 return;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001071 }
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001072 if (errno == EPERM || errno == ESRCH)
1073 return;
1074 getregset_support = -1;
1075 }
1076# endif /* PTRACE_GETREGSET && NT_PRSTATUS */
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001077# if defined(ARM)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001078 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &arm_regs);
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001079# elif defined(I386)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001080 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &i386_regs);
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001081# elif defined(X86_64)
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001082 /* Use old method, with unreliable heuristical detection of 32-bitness. */
1083 x86_io.iov_len = sizeof(x86_64_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001084 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &x86_64_regs);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001085 if (!get_regs_error && x86_64_regs.cs == 0x23) {
1086 x86_io.iov_len = sizeof(i386_regs);
1087 /*
1088 * The order is important: i386_regs and x86_64_regs
1089 * are overlaid in memory!
1090 */
1091 i386_regs.ebx = x86_64_regs.rbx;
1092 i386_regs.ecx = x86_64_regs.rcx;
1093 i386_regs.edx = x86_64_regs.rdx;
1094 i386_regs.esi = x86_64_regs.rsi;
1095 i386_regs.edi = x86_64_regs.rdi;
1096 i386_regs.ebp = x86_64_regs.rbp;
1097 i386_regs.eax = x86_64_regs.rax;
1098 /* i386_regs.xds = x86_64_regs.ds; unused by strace */
1099 /* i386_regs.xes = x86_64_regs.es; ditto... */
1100 /* i386_regs.xfs = x86_64_regs.fs; */
1101 /* i386_regs.xgs = x86_64_regs.gs; */
1102 i386_regs.orig_eax = x86_64_regs.orig_rax;
1103 i386_regs.eip = x86_64_regs.rip;
1104 /* i386_regs.xcs = x86_64_regs.cs; */
1105 /* i386_regs.eflags = x86_64_regs.eflags; */
1106 i386_regs.esp = x86_64_regs.rsp;
1107 /* i386_regs.xss = x86_64_regs.ss; */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001108 }
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001109# else
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001110# error unhandled architecture
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001111# endif /* ARM || I386 || X86_64 */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001112# endif
1113}
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001114#endif /* !get_regs */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001115
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001116/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001117 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1118 * 1: ok, continue in trace_syscall_entering().
1119 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001120 * ("????" etc) and bail out.
1121 */
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01001122static int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001123get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001124{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001125 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001126
Denys Vlasenko523635f2012-02-25 02:44:25 +01001127#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001128 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001129 return -1;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001130
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001131 if (s390_gpr2 != -ENOSYS) {
Roland McGrath2f924ca2003-06-26 22:23:28 +00001132 /*
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001133 * Since kernel version 2.5.44 the scno gets passed in gpr2.
Roland McGrath2f924ca2003-06-26 22:23:28 +00001134 */
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001135 scno = s390_gpr2;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001136 } else {
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001137 /*
Michal Ludvig882eda82002-11-11 12:50:47 +00001138 * Old style of "passing" the scno via the SVC instruction.
1139 */
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001140 long psw;
Michal Ludvig882eda82002-11-11 12:50:47 +00001141 long opcode, offset_reg, tmp;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001142 void *svc_addr;
Denys Vlasenko7c9ba8b2011-08-19 19:46:32 +02001143 static const int gpr_offset[16] = {
1144 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
1145 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
1146 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
1147 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
1148 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001149
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001150 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0)
Michal Ludvig882eda82002-11-11 12:50:47 +00001151 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001152 errno = 0;
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001153 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +00001154 if (errno) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001155 perror_msg("peektext(psw-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +00001156 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001157 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001158
1159 /*
1160 * We have to check if the SVC got executed directly or via an
1161 * EXECUTE instruction. In case of EXECUTE it is necessary to do
1162 * instruction decoding to derive the system call number.
1163 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1164 * so that this doesn't work if a SVC opcode is part of an EXECUTE
1165 * opcode. Since there is no way to find out the opcode size this
1166 * is the best we can do...
1167 */
Michal Ludvig882eda82002-11-11 12:50:47 +00001168 if ((opcode & 0xff00) == 0x0a00) {
1169 /* SVC opcode */
1170 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +00001171 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001172 else {
1173 /* SVC got executed by EXECUTE instruction */
1174
1175 /*
1176 * Do instruction decoding of EXECUTE. If you really want to
1177 * understand this, read the Principles of Operations.
1178 */
1179 svc_addr = (void *) (opcode & 0xfff);
1180
1181 tmp = 0;
1182 offset_reg = (opcode & 0x000f0000) >> 16;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001183 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001184 return -1;
1185 svc_addr += tmp;
1186
1187 tmp = 0;
1188 offset_reg = (opcode & 0x0000f000) >> 12;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001189 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001190 return -1;
1191 svc_addr += tmp;
1192
Denys Vlasenkofb036672009-01-23 16:30:26 +00001193 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
Michal Ludvig882eda82002-11-11 12:50:47 +00001194 if (errno)
1195 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001196# if defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +00001197 scno >>= 48;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001198# else
Michal Ludvig882eda82002-11-11 12:50:47 +00001199 scno >>= 16;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001200# endif
Michal Ludvig882eda82002-11-11 12:50:47 +00001201 tmp = 0;
1202 offset_reg = (opcode & 0x00f00000) >> 20;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001203 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001204 return -1;
1205
1206 scno = (scno | tmp) & 0xff;
1207 }
1208 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001209#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001210 scno = ppc_regs.gpr[0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001211# ifdef POWERPC64
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001212 int currpers;
Andreas Schwabd69fa492010-07-12 21:39:57 +02001213
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001214 /* Check for 64/32 bit mode. */
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001215 /* SF is bit 0 of MSR */
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001216 if ((ppc_regs.msr >> 63) & 1)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001217 currpers = 0;
1218 else
1219 currpers = 1;
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001220 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001221# endif
1222#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001223 scno = avr32_regs.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001224#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001225 if (upeek(tcp->pid, PT_ORIG_P0, &scno))
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001226 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001227#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001228 scno = i386_regs.orig_eax;
H.J. Lu35be5812012-04-16 13:00:01 +02001229#elif defined(X86_64) || defined(X32)
1230# ifndef __X32_SYSCALL_BIT
1231# define __X32_SYSCALL_BIT 0x40000000
1232# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001233 int currpers;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001234# if 1
1235 /* GETREGSET of NT_PRSTATUS tells us regset size,
1236 * which unambiguously detects i386.
1237 *
1238 * Linux kernel distinguishes x86-64 and x32 processes
1239 * solely by looking at __X32_SYSCALL_BIT:
1240 * arch/x86/include/asm/compat.h::is_x32_task():
1241 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1242 * return true;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001243 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001244 if (x86_io.iov_len == sizeof(i386_regs)) {
1245 scno = i386_regs.orig_eax;
1246 currpers = 1;
1247 } else {
1248 scno = x86_64_regs.orig_rax;
1249 currpers = 0;
1250 if (scno & __X32_SYSCALL_BIT) {
1251 scno -= __X32_SYSCALL_BIT;
1252 currpers = 2;
1253 }
1254 }
1255# elif 0
1256 /* cs = 0x33 for long mode (native 64 bit and x32)
1257 * cs = 0x23 for compatibility mode (32 bit)
1258 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1259 */
1260 scno = x86_64_regs.orig_rax;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001261 switch (x86_64_regs.cs) {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001262 case 0x23: currpers = 1; break;
H.J. Lu35be5812012-04-16 13:00:01 +02001263 case 0x33:
1264 if (x86_64_regs.ds == 0x2b) {
1265 currpers = 2;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001266 scno &= ~__X32_SYSCALL_BIT;
H.J. Lu35be5812012-04-16 13:00:01 +02001267 } else
1268 currpers = 0;
1269 break;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001270 default:
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001271 fprintf(stderr, "Unknown value CS=0x%08X while "
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001272 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001273 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001274 currpers = current_personality;
1275 break;
1276 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001277# elif 0
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001278 /* This version analyzes the opcode of a syscall instruction.
1279 * (int 0x80 on i386 vs. syscall on x86-64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001280 * It works, but is too complicated, and strictly speaking, unreliable.
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001281 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001282 unsigned long call, rip = x86_64_regs.rip;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001283 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1284 rip -= 2;
1285 errno = 0;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001286 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001287 if (errno)
1288 fprintf(stderr, "ptrace_peektext failed: %s\n",
1289 strerror(errno));
1290 switch (call & 0xffff) {
1291 /* x86-64: syscall = 0x0f 0x05 */
1292 case 0x050f: currpers = 0; break;
1293 /* i386: int 0x80 = 0xcd 0x80 */
1294 case 0x80cd: currpers = 1; break;
1295 default:
1296 currpers = current_personality;
1297 fprintf(stderr,
1298 "Unknown syscall opcode (0x%04X) while "
1299 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001300 "PID=%d\n", (int)call, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001301 break;
1302 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001303# endif
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001304
H.J. Lu35be5812012-04-16 13:00:01 +02001305# ifdef X32
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001306 /* If we are built for a x32 system, then personality 0 is x32
1307 * (not x86_64), and stracing of x86_64 apps is not supported.
1308 * Stracing of i386 apps is still supported.
H.J. Lu085e4282012-04-17 11:05:04 -07001309 */
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001310 if (currpers == 0) {
1311 fprintf(stderr, "syscall_%lu(...) in unsupported "
1312 "64-bit mode of process PID=%d\n",
1313 scno, tcp->pid);
1314 return 0;
H.J. Lu35be5812012-04-16 13:00:01 +02001315 }
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001316 currpers &= ~2; /* map 2,1 to 0,1 */
H.J. Lu35be5812012-04-16 13:00:01 +02001317# endif
H.J. Lu085e4282012-04-17 11:05:04 -07001318 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001319#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001320# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01001321 long psr;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001322 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001323 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
1324 if (ia64_ia32mode) {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001325 if (upeek(tcp->pid, PT_R1, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001326 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001327 } else {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001328 if (upeek(tcp->pid, PT_R15, &scno) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001329 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001330 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001331#elif defined(AARCH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001332 switch (aarch64_io.iov_len) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001333 case sizeof(aarch64_regs):
1334 /* We are in 64-bit mode */
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001335 scno = aarch64_regs.regs[8];
1336 update_personality(tcp, 1);
1337 break;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001338 case sizeof(arm_regs):
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001339 /* We are in 32-bit mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001340 /* Note: we don't support OABI, unlike 32-bit ARM build */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001341 scno = arm_regs.ARM_r7;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001342 update_personality(tcp, 0);
1343 break;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001344 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001345#elif defined(ARM)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001346 if (arm_regs.ARM_ip != 0) {
1347 /* It is not a syscall entry */
1348 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001349 tcp->flags |= TCB_INSYSCALL;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001350 return 0;
1351 }
1352 /* Note: we support only 32-bit CPUs, not 26-bit */
1353
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001354# ifndef STRACE_KNOWS_ONLY_EABI
1355# warning STRACE_KNOWS_ONLY_EABI not set, will PTRACE_PEEKTEXT on every syscall (slower tracing)
1356 if (arm_regs.ARM_cpsr & 0x20)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001357 /* Thumb mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001358 goto scno_in_r7;
1359 /* ARM mode */
1360 /* Check EABI/OABI by examining SVC insn's low 24 bits */
1361 errno = 0;
1362 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1363 if (errno)
1364 return -1;
1365 /* EABI syscall convention? */
1366 if (scno != 0xef000000) {
1367 /* No, it's OABI */
1368 if ((scno & 0x0ff00000) != 0x0f900000) {
1369 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1370 tcp->pid, scno);
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001371 return -1;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001372 }
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001373 /* Fixup the syscall number */
1374 scno &= 0x000fffff;
1375 } else {
1376 scno_in_r7:
1377 scno = arm_regs.ARM_r7;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001378 }
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001379# else
1380 scno = arm_regs.ARM_r7;
1381# endif
Denys Vlasenko7270de52013-02-21 15:46:34 +01001382 scno = shuffle_scno(scno);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001383#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001384 if (upeek(tcp->pid, 4*PT_ORIG_D0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001385 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001386#elif defined(LINUX_MIPSN32)
Roland McGrath542c2c62008-05-20 01:11:56 +00001387 unsigned long long regs[38];
1388
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001389 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001390 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001391 mips_a3 = regs[REG_A3];
1392 mips_r2 = regs[REG_V0];
Roland McGrath542c2c62008-05-20 01:11:56 +00001393
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001394 scno = mips_r2;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001395 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001396 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001397 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001398 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001399 return 0;
1400 }
Roland McGrath542c2c62008-05-20 01:11:56 +00001401 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001402#elif defined(MIPS)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001403 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001404 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001405 if (upeek(tcp->pid, REG_V0, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001406 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001407
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001408 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001409 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001410 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001411 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001412 return 0;
1413 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001414 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001415#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001416 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001417 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001418 if (upeek(tcp->pid, REG_R0, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001419 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001420
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001421 /*
1422 * Do some sanity checks to figure out if it's
1423 * really a syscall entry
1424 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001425 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001426 if (alpha_a3 == 0 || alpha_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001427 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001428 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001429 return 0;
1430 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001431 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001432#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001433 /* Disassemble the syscall trap. */
1434 /* Retrieve the syscall trap instruction. */
Denys Vlasenko46455822013-02-05 17:02:59 +01001435 unsigned long trap;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001436 errno = 0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001437# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001438 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001439 trap >>= 32;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001440# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001441 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001442# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001443 if (errno)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001444 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001445
1446 /* Disassemble the trap to see what personality to use. */
1447 switch (trap) {
1448 case 0x91d02010:
1449 /* Linux/SPARC syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001450 update_personality(tcp, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001451 break;
1452 case 0x91d0206d:
1453 /* Linux/SPARC64 syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001454 update_personality(tcp, 2);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001455 break;
1456 case 0x91d02000:
1457 /* SunOS syscall trap. (pers 1) */
1458 fprintf(stderr, "syscall: SunOS no support\n");
1459 return -1;
1460 case 0x91d02008:
1461 /* Solaris 2.x syscall trap. (per 2) */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001462 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001463 break;
1464 case 0x91d02009:
1465 /* NetBSD/FreeBSD syscall trap. */
1466 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1467 return -1;
1468 case 0x91d02027:
1469 /* Solaris 2.x gettimeofday */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001470 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001471 break;
1472 default:
Denys Vlasenko523635f2012-02-25 02:44:25 +01001473# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001474 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001475# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001476 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001477# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001478 return -1;
1479 }
1480
1481 /* Extract the system call number from the registers. */
1482 if (trap == 0x91d02027)
1483 scno = 156;
1484 else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001485 scno = sparc_regs.u_regs[U_REG_G1];
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001486 if (scno == 0) {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001487 scno = sparc_regs.u_regs[U_REG_O0];
1488 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 +02001489 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001490#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001491 if (upeek(tcp->pid, PT_GR20, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001492 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001493#elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001494 /*
1495 * In the new syscall ABI, the system call number is in R3.
1496 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001497 if (upeek(tcp->pid, 4*(REG_REG0+3), &scno) < 0)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001498 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001499
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001500 if (scno < 0) {
1501 /* Odd as it may seem, a glibc bug has been known to cause
1502 glibc to issue bogus negative syscall numbers. So for
1503 our purposes, make strace print what it *should* have been */
1504 long correct_scno = (scno & 0xff);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001505 if (debug_flag)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001506 fprintf(stderr,
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001507 "Detected glibc bug: bogus system call"
1508 " number = %ld, correcting to %ld\n",
1509 scno,
1510 correct_scno);
1511 scno = correct_scno;
1512 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001513#elif defined(SH64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001514 if (upeek(tcp->pid, REG_SYSCALL, &scno) < 0)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001515 return -1;
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001516 scno &= 0xFFFF;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001517#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001518 if (upeek(tcp->pid, 4*PT_R9, &scno) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001519 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001520#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001521 int currpers;
1522 scno = tile_regs.regs[10];
1523# ifdef __tilepro__
1524 currpers = 1;
1525# else
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001526# ifndef PT_FLAGS_COMPAT
1527# define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1528# endif
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001529 if (tile_regs.flags & PT_FLAGS_COMPAT)
1530 currpers = 1;
1531 else
1532 currpers = 0;
1533# endif
1534 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001535#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001536 if (upeek(tcp->pid, 0, &scno) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001537 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001538#elif defined(OR1K)
1539 scno = or1k_regs.gpr[11];
James Hogan5f999a82013-02-22 14:44:10 +00001540#elif defined(METAG)
1541 scno = metag_regs.dx[0][1]; /* syscall number in D1Re0 (D1.0) */
Chris Zankel8f636ed2013-03-25 10:22:07 -07001542#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001543 if (upeek(tcp->pid, SYSCALL_NR, &scno) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001544 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001545#endif
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001546
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001547 tcp->scno = scno;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001548 if (SCNO_IS_VALID(tcp->scno)) {
1549 tcp->s_ent = &sysent[scno];
1550 tcp->qual_flg = qual_flags[scno];
1551 } else {
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01001552 static const struct_sysent unknown = {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001553 .nargs = MAX_ARGS,
1554 .sys_flags = 0,
1555 .sys_func = printargs,
1556 .sys_name = "unknown", /* not used */
1557 };
1558 tcp->s_ent = &unknown;
1559 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1560 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001561 return 1;
1562}
1563
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02001564/* Called at each syscall entry.
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001565 * Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001566 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1567 * 1: ok, continue in trace_syscall_entering().
1568 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001569 * ("????" etc) and bail out.
1570 */
Roland McGratha4d48532005-06-08 20:45:28 +00001571static int
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001572syscall_fixup_on_sysenter(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001573{
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001574 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001575#if defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001576 if (i386_regs.eax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001577 if (debug_flag)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001578 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1579 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001580 }
H.J. Lu35be5812012-04-16 13:00:01 +02001581#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001582 {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001583 long rax;
1584 if (x86_io.iov_len == sizeof(i386_regs)) {
1585 /* Sign extend from 32 bits */
1586 rax = (int32_t)i386_regs.eax;
1587 } else {
1588 /* Note: in X32 build, this truncates 64 to 32 bits */
1589 rax = x86_64_regs.rax;
1590 }
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001591 if (rax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001592 if (debug_flag)
Denys Vlasenko18beb982011-08-24 16:59:23 +02001593 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1594 return 0;
1595 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001596 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001597#elif defined(M68K)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001598 /* TODO? Eliminate upeek's in arches below like we did in x86 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001599 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001600 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001601 if (m68k_d0 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001602 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001603 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001604 return 0;
1605 }
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001606#elif defined(IA64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001607 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001608 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001609 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001610 return -1;
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001611 if (ia64_ia32mode && ia64_r8 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001612 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001613 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001614 return 0;
1615 }
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001616#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001617 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001618 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001619 if (cris_r10 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001620 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001621 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001622 return 0;
1623 }
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001624#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001625 if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001626 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001627 if (microblaze_r3 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001628 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001629 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001630 return 0;
1631 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001632#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001633 return 1;
1634}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001635
Denys Vlasenko146b9442012-03-18 22:10:48 +01001636static void
1637internal_fork(struct tcb *tcp)
1638{
1639#if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1640# define ARG_FLAGS 1
1641#else
1642# define ARG_FLAGS 0
1643#endif
1644#ifndef CLONE_UNTRACED
1645# define CLONE_UNTRACED 0x00800000
1646#endif
1647 if ((ptrace_setoptions
1648 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1649 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1650 return;
1651
1652 if (!followfork)
1653 return;
1654
1655 if (entering(tcp)) {
1656 /*
1657 * We won't see the new child if clone is called with
1658 * CLONE_UNTRACED, so we keep the same logic with that option
1659 * and don't trace it.
1660 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001661 if ((tcp->s_ent->sys_func == sys_clone)
1662 && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
1663 )
Denys Vlasenko146b9442012-03-18 22:10:48 +01001664 return;
1665 setbpt(tcp);
1666 } else {
1667 if (tcp->flags & TCB_BPTSET)
1668 clearbpt(tcp);
1669 }
1670}
1671
1672#if defined(TCB_WAITEXECVE)
1673static void
1674internal_exec(struct tcb *tcp)
1675{
1676 /* Maybe we have post-execve SIGTRAP suppressed? */
1677 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1678 return; /* yes, no need to do anything */
1679
1680 if (exiting(tcp) && syserror(tcp))
1681 /* Error in execve, no post-execve SIGTRAP expected */
1682 tcp->flags &= ~TCB_WAITEXECVE;
1683 else
1684 tcp->flags |= TCB_WAITEXECVE;
1685}
1686#endif
1687
1688static void
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001689syscall_fixup_for_fork_exec(struct tcb *tcp)
Roland McGrathc1e45922008-05-27 23:18:29 +00001690{
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001691 /*
1692 * We must always trace a few critical system calls in order to
1693 * correctly support following forks in the presence of tracing
1694 * qualifiers.
1695 */
1696 int (*func)();
1697
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001698 func = tcp->s_ent->sys_func;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001699
1700 if ( sys_fork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001701 || sys_vfork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001702 || sys_clone == func
Denys Vlasenko146b9442012-03-18 22:10:48 +01001703 ) {
1704 internal_fork(tcp);
1705 return;
1706 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001707
Denys Vlasenko84703742012-02-25 02:38:52 +01001708#if defined(TCB_WAITEXECVE)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001709 if ( sys_execve == func
Denys Vlasenko84703742012-02-25 02:38:52 +01001710# if defined(SPARC) || defined(SPARC64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001711 || sys_execv == func
Denys Vlasenkoa7949742011-08-21 17:26:55 +02001712# endif
Denys Vlasenko146b9442012-03-18 22:10:48 +01001713 ) {
1714 internal_exec(tcp);
1715 return;
1716 }
Roland McGrathc1e45922008-05-27 23:18:29 +00001717#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001718}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001719
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001720/* Return -1 on error or 1 on success (never 0!) */
Roland McGratha4d48532005-06-08 20:45:28 +00001721static int
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001722get_syscall_args(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001723{
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001724 int i, nargs;
1725
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001726 nargs = tcp->s_ent->nargs;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001727
Denys Vlasenko523635f2012-02-25 02:44:25 +01001728#if defined(S390) || defined(S390X)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001729 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001730 if (upeek(tcp->pid, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001731 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001732#elif defined(ALPHA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001733 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001734 if (upeek(tcp->pid, REG_A0+i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001735 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001736#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001737 if (!ia64_ia32mode) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001738 unsigned long *out0, cfm, sof, sol;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001739 long rbs_end;
1740 /* be backwards compatible with kernel < 2.4.4... */
1741# ifndef PT_RBS_END
1742# define PT_RBS_END PT_AR_BSP
1743# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001744
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001745 if (upeek(tcp->pid, PT_RBS_END, &rbs_end) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001746 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001747 if (upeek(tcp->pid, PT_CFM, (long *) &cfm) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001748 return -1;
1749
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001750 sof = (cfm >> 0) & 0x7f;
1751 sol = (cfm >> 7) & 0x7f;
1752 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1753
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001754 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001755 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1756 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1757 return -1;
1758 }
1759 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001760 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1761 PT_R9 /* ECX = out1 */,
1762 PT_R10 /* EDX = out2 */,
1763 PT_R14 /* ESI = out3 */,
1764 PT_R15 /* EDI = out4 */,
1765 PT_R13 /* EBP = out5 */};
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001766
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001767 for (i = 0; i < nargs; ++i) {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001768 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001769 return -1;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001770 /* truncate away IVE sign-extension */
1771 tcp->u_arg[i] &= 0xffffffff;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001772 }
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001773 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001774#elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001775 /* N32 and N64 both use up to six registers. */
1776 unsigned long long regs[38];
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001777
1778 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1779 return -1;
1780
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001781 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001782 tcp->u_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001783# if defined(LINUX_MIPSN32)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001784 tcp->ext_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001785# endif
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001786 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001787#elif defined(MIPS)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001788 if (nargs > 4) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001789 long sp;
1790
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001791 if (upeek(tcp->pid, REG_SP, &sp) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001792 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001793 for (i = 0; i < 4; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001794 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001795 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001796 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001797 (char *)(tcp->u_arg + 4));
1798 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001799 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001800 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001801 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001802 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001803#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001804 (void)i;
1805 (void)nargs;
1806 tcp->u_arg[0] = ppc_regs.orig_gpr3;
1807 tcp->u_arg[1] = ppc_regs.gpr[4];
1808 tcp->u_arg[2] = ppc_regs.gpr[5];
1809 tcp->u_arg[3] = ppc_regs.gpr[6];
1810 tcp->u_arg[4] = ppc_regs.gpr[7];
1811 tcp->u_arg[5] = ppc_regs.gpr[8];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001812#elif defined(SPARC) || defined(SPARC64)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001813 for (i = 0; i < nargs; ++i)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001814 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001815#elif defined(HPPA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001816 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001817 if (upeek(tcp->pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001818 return -1;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001819#elif defined(ARM) || defined(AARCH64)
1820# if defined(AARCH64)
1821 if (tcp->currpers == 1)
1822 for (i = 0; i < nargs; ++i)
1823 tcp->u_arg[i] = aarch64_regs.regs[i];
1824 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001825# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001826 for (i = 0; i < nargs; ++i)
Denys Vlasenko401374e2013-02-06 18:24:39 +01001827 tcp->u_arg[i] = arm_regs.uregs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001828#elif defined(AVR32)
Denys Vlasenkob5b25892011-08-30 19:04:54 +02001829 (void)i;
1830 (void)nargs;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001831 tcp->u_arg[0] = avr32_regs.r12;
1832 tcp->u_arg[1] = avr32_regs.r11;
1833 tcp->u_arg[2] = avr32_regs.r10;
1834 tcp->u_arg[3] = avr32_regs.r9;
1835 tcp->u_arg[4] = avr32_regs.r5;
1836 tcp->u_arg[5] = avr32_regs.r3;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001837#elif defined(BFIN)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001838 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 +02001839
Denys Vlasenko4b887a52011-08-23 13:32:38 +02001840 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001841 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001842 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001843#elif defined(SH)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001844 static const int syscall_regs[MAX_ARGS] = {
1845 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1846 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001847 };
1848
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001849 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001850 if (upeek(tcp->pid, syscall_regs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001851 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001852#elif defined(SH64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001853 int i;
1854 /* Registers used by SH5 Linux system calls for parameters */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001855 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001856
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001857 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001858 if (upeek(tcp->pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001859 return -1;
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001860#elif defined(I386)
1861 (void)i;
1862 (void)nargs;
1863 tcp->u_arg[0] = i386_regs.ebx;
1864 tcp->u_arg[1] = i386_regs.ecx;
1865 tcp->u_arg[2] = i386_regs.edx;
1866 tcp->u_arg[3] = i386_regs.esi;
1867 tcp->u_arg[4] = i386_regs.edi;
1868 tcp->u_arg[5] = i386_regs.ebp;
H.J. Lu35be5812012-04-16 13:00:01 +02001869#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001870 (void)i;
1871 (void)nargs;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001872 if (x86_io.iov_len != sizeof(i386_regs)) {
1873 /* x86-64 or x32 ABI */
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001874 tcp->u_arg[0] = x86_64_regs.rdi;
1875 tcp->u_arg[1] = x86_64_regs.rsi;
1876 tcp->u_arg[2] = x86_64_regs.rdx;
1877 tcp->u_arg[3] = x86_64_regs.r10;
1878 tcp->u_arg[4] = x86_64_regs.r8;
1879 tcp->u_arg[5] = x86_64_regs.r9;
H.J. Lu35be5812012-04-16 13:00:01 +02001880# ifdef X32
1881 tcp->ext_arg[0] = x86_64_regs.rdi;
1882 tcp->ext_arg[1] = x86_64_regs.rsi;
1883 tcp->ext_arg[2] = x86_64_regs.rdx;
1884 tcp->ext_arg[3] = x86_64_regs.r10;
1885 tcp->ext_arg[4] = x86_64_regs.r8;
1886 tcp->ext_arg[5] = x86_64_regs.r9;
1887# endif
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001888 } else {
1889 /* i386 ABI */
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001890 /* Zero-extend from 32 bits */
1891 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1892 * if you need to use *sign-extended* parameter.
1893 */
1894 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1895 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1896 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1897 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1898 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1899 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001900 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001901#elif defined(MICROBLAZE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001902 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001903 if (upeek(tcp->pid, (5 + i) * 4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001904 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001905#elif defined(CRISV10) || defined(CRISV32)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001906 static const int crisregs[MAX_ARGS] = {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001907 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001908 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001909 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001910
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001911 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001912 if (upeek(tcp->pid, crisregs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001913 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001914#elif defined(TILE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001915 for (i = 0; i < nargs; ++i)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001916 tcp->u_arg[i] = tile_regs.regs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001917#elif defined(M68K)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001918 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001919 if (upeek(tcp->pid, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001920 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001921#elif defined(OR1K)
1922 (void)nargs;
1923 for (i = 0; i < 6; ++i)
1924 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
James Hogan5f999a82013-02-22 14:44:10 +00001925#elif defined(METAG)
1926 for (i = 0; i < nargs; i++)
1927 /* arguments go backwards from D1Ar1 (D1.3) */
1928 tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
Chris Zankel8f636ed2013-03-25 10:22:07 -07001929#elif defined(XTENSA)
1930 /* arg0: a6, arg1: a3, arg2: a4, arg3: a5, arg4: a8, arg5: a9 */
1931 static const int xtensaregs[MAX_ARGS] = { 6, 3, 4, 5, 8, 9 };
1932 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001933 if (upeek(tcp->pid, REG_A_BASE + xtensaregs[i], &tcp->u_arg[i]) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001934 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001935#else /* Other architecture (32bits specific) */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001936 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001937 if (upeek(tcp->pid, i*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001938 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001939#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001940 return 1;
1941}
1942
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00001943static int
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001944trace_syscall_entering(struct tcb *tcp)
1945{
1946 int res, scno_good;
1947
Denys Vlasenko2ce12ed2011-08-24 17:25:32 +02001948#if defined TCB_WAITEXECVE
1949 if (tcp->flags & TCB_WAITEXECVE) {
1950 /* This is the post-execve SIGTRAP. */
1951 tcp->flags &= ~TCB_WAITEXECVE;
1952 return 0;
1953 }
1954#endif
1955
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001956 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001957 if (res == 0)
1958 return res;
Denys Vlasenko907735a2012-03-21 00:23:16 +01001959 if (res == 1) {
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001960 res = syscall_fixup_on_sysenter(tcp);
Denys Vlasenko907735a2012-03-21 00:23:16 +01001961 if (res == 0)
1962 return res;
1963 if (res == 1)
1964 res = get_syscall_args(tcp);
1965 }
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001966
1967 if (res != 1) {
1968 printleader(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001969 if (scno_good != 1)
Denys Vlasenkob7a6dae2012-03-20 16:48:35 +01001970 tprints("????" /* anti-trigraph gap */ "(");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001971 else if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01001972 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001973 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001974 tprintf("%s(", tcp->s_ent->sys_name);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001975 /*
1976 * " <unavailable>" will be added later by the code which
1977 * detects ptrace errors.
1978 */
1979 goto ret;
1980 }
1981
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02001982 if ( sys_execve == tcp->s_ent->sys_func
1983# if defined(SPARC) || defined(SPARC64)
1984 || sys_execv == tcp->s_ent->sys_func
1985# endif
1986 ) {
1987 hide_log_until_execve = 0;
1988 }
1989
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001990#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001991 while (1) {
Denys Vlasenko523635f2012-02-25 02:44:25 +01001992# ifdef SYS_socket_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001993 if (tcp->s_ent->sys_func == sys_socketcall) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00001994 decode_socket_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001995 break;
1996 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001997# endif
1998# ifdef SYS_ipc_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001999 if (tcp->s_ent->sys_func == sys_ipc) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00002000 decode_ipc_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002001 break;
2002 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002003# endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002004 break;
2005 }
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002006#endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002007
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01002008 if (need_fork_exec_workarounds)
2009 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002010
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002011 if (!(tcp->qual_flg & QUAL_TRACE)
2012 || (tracing_paths && !pathtrace_match(tcp))
2013 ) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002014 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
2015 return 0;
2016 }
2017
2018 tcp->flags &= ~TCB_FILTERED;
2019
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002020 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002021 res = 0;
2022 goto ret;
2023 }
2024
2025 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002026 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002027 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002028 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002029 tprintf("%s(", tcp->s_ent->sys_name);
2030 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002031 res = printargs(tcp);
2032 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002033 res = tcp->s_ent->sys_func(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002034
Dmitry V. Levinb742d8c2012-09-17 22:40:12 +00002035 fflush(tcp->outf);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002036 ret:
2037 tcp->flags |= TCB_INSYSCALL;
2038 /* Measure the entrance time as late as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002039 if (Tflag || cflag)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002040 gettimeofday(&tcp->etime, NULL);
2041 return res;
2042}
2043
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002044/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01002045 * 1: ok, continue in trace_syscall_exiting().
2046 * -1: error, trace_syscall_exiting() should print error indicator
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002047 * ("????" etc) and bail out.
2048 */
2049static int
2050get_syscall_result(struct tcb *tcp)
2051{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002052#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002053 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002054 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002055#elif defined(POWERPC)
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002056 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002057#elif defined(AVR32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002058 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002059#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002060 if (upeek(tcp->pid, PT_R0, &bfin_r0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002061 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002062#elif defined(I386)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002063 /* already done by get_regs */
H.J. Lu35be5812012-04-16 13:00:01 +02002064#elif defined(X86_64) || defined(X32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002065 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002066#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002067# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01002068 long psr;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002069 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02002070 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002071 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002072 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002073 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002074 return -1;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002075#elif defined(ARM)
2076 /* already done by get_regs */
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002077#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002078 /* register reading already done by get_regs */
2079
2080 /* Used to do this, but we did it on syscall entry already: */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002081 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
2082 * else it's personality 0.
2083 */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002084 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
Denys Vlasenko523635f2012-02-25 02:44:25 +01002085#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002086 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002087 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002088#elif defined(LINUX_MIPSN32)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002089 unsigned long long regs[38];
2090
2091 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
2092 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002093 mips_a3 = regs[REG_A3];
2094 mips_r2 = regs[REG_V0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01002095#elif defined(MIPS)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002096 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002097 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002098 if (upeek(tcp->pid, REG_V0, &mips_r2) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002099 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002100#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002101 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002102 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002103 if (upeek(tcp->pid, REG_R0, &alpha_r0) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002104 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002105#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002106 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002107#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002108 if (upeek(tcp->pid, PT_GR28, &hppa_r28) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002109 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002110#elif defined(SH)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002111 /* new syscall ABI returns result in R0 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002112 if (upeek(tcp->pid, 4*REG_REG0, (long *)&sh_r0) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002113 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002114#elif defined(SH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002115 /* ABI defines result returned in r9 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002116 if (upeek(tcp->pid, REG_GENERAL(9), (long *)&sh64_r9) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002117 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002118#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002119 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002120 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002121#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002122 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002123#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002124 if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002125 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01002126#elif defined(OR1K)
2127 /* already done by get_regs */
James Hogan5f999a82013-02-22 14:44:10 +00002128#elif defined(METAG)
2129 /* already done by get_regs */
Chris Zankel8f636ed2013-03-25 10:22:07 -07002130#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002131 if (upeek(tcp->pid, REG_A_BASE + 2, &xtensa_a2) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07002132 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002133#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002134 return 1;
2135}
2136
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002137/* Called at each syscall exit */
2138static void
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002139syscall_fixup_on_sysexit(struct tcb *tcp)
2140{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002141#if defined(S390) || defined(S390X)
Denys Vlasenkoece98792011-08-25 10:25:35 +02002142 if ((tcp->flags & TCB_WAITEXECVE)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002143 && (s390_gpr2 == -ENOSYS || s390_gpr2 == tcp->scno)) {
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002144 /*
2145 * Return from execve.
2146 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2147 * flag set for the post-execve SIGTRAP to see and reset.
2148 */
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002149 s390_gpr2 = 0;
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002150 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002151#endif
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002152}
2153
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002154/*
2155 * Check the syscall return value register value for whether it is
2156 * a negated errno code indicating an error, or a success return value.
2157 */
2158static inline int
2159is_negated_errno(unsigned long int val)
2160{
2161 unsigned long int max = -(long int) nerrnos;
Denys Vlasenko2544f982013-02-19 17:39:56 +01002162#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01002163 if (current_wordsize < sizeof(val)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002164 val = (unsigned int) val;
2165 max = (unsigned int) max;
2166 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002167#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002168 return val > max;
2169}
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002170
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002171#if defined(X32)
2172static inline int
2173is_negated_errno_x32(unsigned long long val)
2174{
2175 unsigned long long max = -(long long) nerrnos;
2176 /*
2177 * current_wordsize is 4 even in personality 0 (native X32)
2178 * but truncation _must not_ be done in it.
2179 * can't check current_wordsize here!
2180 */
2181 if (current_personality != 0) {
2182 val = (uint32_t) val;
2183 max = (uint32_t) max;
2184 }
2185 return val > max;
2186}
2187#endif
2188
Denys Vlasenko907735a2012-03-21 00:23:16 +01002189/* Returns:
2190 * 1: ok, continue in trace_syscall_exiting().
2191 * -1: error, trace_syscall_exiting() should print error indicator
2192 * ("????" etc) and bail out.
2193 */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002194static void
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002195get_error(struct tcb *tcp)
2196{
2197 int u_error = 0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002198 int check_errno = 1;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002199 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002200 check_errno = 0;
2201 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002202#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002203 if (check_errno && is_negated_errno(s390_gpr2)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002204 tcp->u_rval = -1;
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002205 u_error = -s390_gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002206 }
2207 else {
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002208 tcp->u_rval = s390_gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002209 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002210#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002211 if (check_errno && is_negated_errno(i386_regs.eax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002212 tcp->u_rval = -1;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002213 u_error = -i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002214 }
2215 else {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002216 tcp->u_rval = i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002217 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002218#elif defined(X86_64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002219 long rax;
2220 if (x86_io.iov_len == sizeof(i386_regs)) {
2221 /* Sign extend from 32 bits */
2222 rax = (int32_t)i386_regs.eax;
2223 } else {
2224 rax = x86_64_regs.rax;
2225 }
2226 if (check_errno && is_negated_errno(rax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002227 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002228 u_error = -rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002229 }
2230 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002231 tcp->u_rval = rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002232 }
2233#elif defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002234 /* In X32, return value is 64-bit (llseek uses one).
2235 * Using merely "long rax" would not work.
2236 */
2237 long long rax;
2238 if (x86_io.iov_len == sizeof(i386_regs)) {
2239 /* Sign extend from 32 bits */
2240 rax = (int32_t)i386_regs.eax;
2241 } else {
2242 rax = x86_64_regs.rax;
2243 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002244 /* Careful: is_negated_errno() works only on longs */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002245 if (check_errno && is_negated_errno_x32(rax)) {
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002246 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002247 u_error = -rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002248 }
2249 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002250 tcp->u_rval = rax; /* truncating */
2251 tcp->u_lrval = rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002252 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002253#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02002254 if (ia64_ia32mode) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002255 int err;
2256
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002257 err = (int)ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002258 if (check_errno && is_negated_errno(err)) {
2259 tcp->u_rval = -1;
2260 u_error = -err;
2261 }
2262 else {
2263 tcp->u_rval = err;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002264 }
2265 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002266 if (check_errno && ia64_r10) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002267 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002268 u_error = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002269 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002270 tcp->u_rval = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002271 }
2272 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002273#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002274 if (check_errno && mips_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002275 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002276 u_error = mips_r2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002277 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002278 tcp->u_rval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002279# if defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002280 tcp->u_lrval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002281# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002282 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002283#elif defined(POWERPC)
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002284 if (check_errno && (ppc_regs.ccr & 0x10000000)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002285 tcp->u_rval = -1;
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002286 u_error = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002287 }
2288 else {
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002289 tcp->u_rval = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002290 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002291#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002292 if (check_errno && is_negated_errno(m68k_d0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002293 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002294 u_error = -m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002295 }
2296 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002297 tcp->u_rval = m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002298 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002299#elif defined(ARM) || defined(AARCH64)
2300# if defined(AARCH64)
2301 if (tcp->currpers == 1) {
2302 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2303 tcp->u_rval = -1;
2304 u_error = -aarch64_regs.regs[0];
2305 }
2306 else {
2307 tcp->u_rval = aarch64_regs.regs[0];
2308 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002309 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002310 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002311# endif
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002312 {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002313 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002314 tcp->u_rval = -1;
Denys Vlasenko401374e2013-02-06 18:24:39 +01002315 u_error = -arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002316 }
2317 else {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002318 tcp->u_rval = arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002319 }
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002320 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002321#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002322 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002323 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002324 u_error = -avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002325 }
2326 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002327 tcp->u_rval = avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002328 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002329#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002330 if (check_errno && is_negated_errno(bfin_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002331 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002332 u_error = -bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002333 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002334 tcp->u_rval = bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002335 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002336#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002337 if (check_errno && alpha_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002338 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002339 u_error = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002340 }
2341 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002342 tcp->u_rval = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002343 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002344#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002345 if (check_errno && sparc_regs.psr & PSR_C) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002346 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002347 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002348 }
2349 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002350 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002351 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002352#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002353 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002354 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002355 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002356 }
2357 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002358 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002359 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002360#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002361 if (check_errno && is_negated_errno(hppa_r28)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002362 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002363 u_error = -hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002364 }
2365 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002366 tcp->u_rval = hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002367 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002368#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002369 if (check_errno && is_negated_errno(sh_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002370 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002371 u_error = -sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002372 }
2373 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002374 tcp->u_rval = sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002375 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002376#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002377 if (check_errno && is_negated_errno(sh64_r9)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002378 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002379 u_error = -sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002380 }
2381 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002382 tcp->u_rval = sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002383 }
James Hogan5f999a82013-02-22 14:44:10 +00002384#elif defined(METAG)
2385 /* result pointer in D0Re0 (D0.0) */
2386 if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2387 tcp->u_rval = -1;
2388 u_error = -metag_regs.dx[0][0];
2389 }
2390 else {
2391 tcp->u_rval = metag_regs.dx[0][0];
2392 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002393#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002394 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002395 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002396 u_error = -cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002397 }
2398 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002399 tcp->u_rval = cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002400 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002401#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002402 /*
2403 * The standard tile calling convention returns the value (or negative
2404 * errno) in r0, and zero (or positive errno) in r1.
2405 * Until at least kernel 3.8, however, the r1 value is not reflected
2406 * in ptregs at this point, so we use r0 here.
2407 */
2408 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002409 tcp->u_rval = -1;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002410 u_error = -tile_regs.regs[0];
2411 } else {
2412 tcp->u_rval = tile_regs.regs[0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002413 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002414#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002415 if (check_errno && is_negated_errno(microblaze_r3)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002416 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002417 u_error = -microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002418 }
2419 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002420 tcp->u_rval = microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002421 }
Christian Svensson492f81f2013-02-14 13:26:27 +01002422#elif defined(OR1K)
2423 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2424 tcp->u_rval = -1;
2425 u_error = -or1k_regs.gpr[11];
2426 }
2427 else {
2428 tcp->u_rval = or1k_regs.gpr[11];
2429 }
Chris Zankel8f636ed2013-03-25 10:22:07 -07002430#elif defined(XTENSA)
2431 if (check_errno && is_negated_errno(xtensa_a2)) {
2432 tcp->u_rval = -1;
2433 u_error = -xtensa_a2;
2434 }
2435 else {
2436 tcp->u_rval = xtensa_a2;
2437 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002438#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002439 tcp->u_error = u_error;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002440}
2441
2442static void
2443dumpio(struct tcb *tcp)
2444{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002445 int (*func)();
2446
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002447 if (syserror(tcp))
2448 return;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +01002449 if ((unsigned long) tcp->u_arg[0] >= num_quals)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002450 return;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002451 func = tcp->s_ent->sys_func;
2452 if (func == printargs)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002453 return;
2454 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002455 if (func == sys_read ||
2456 func == sys_pread ||
2457 func == sys_recv ||
2458 func == sys_recvfrom)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002459 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002460 else if (func == sys_readv)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002461 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2462 return;
2463 }
2464 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002465 if (func == sys_write ||
2466 func == sys_pwrite ||
2467 func == sys_send ||
2468 func == sys_sendto)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002469 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002470 else if (func == sys_writev)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002471 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2472 return;
2473 }
2474}
2475
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002476static int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002477trace_syscall_exiting(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002478{
2479 int sys_res;
2480 struct timeval tv;
Denys Vlasenko1a5b5a72011-08-25 00:29:56 +02002481 int res;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002482 long u_error;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002483
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002484 /* Measure the exit time as early as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002485 if (Tflag || cflag)
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002486 gettimeofday(&tv, NULL);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002487
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00002488#if SUPPORTED_PERSONALITIES > 1
2489 update_personality(tcp, tcp->currpers);
2490#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002491 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002492 if (res == 1) {
2493 syscall_fixup_on_sysexit(tcp); /* never fails */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002494 get_error(tcp); /* never fails */
2495 if (need_fork_exec_workarounds)
2496 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002497 if (filtered(tcp) || hide_log_until_execve)
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002498 goto ret;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002499 }
2500
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002501 if (cflag) {
2502 struct timeval t = tv;
Denys Vlasenkoc95a88f2011-08-21 17:47:40 +02002503 count_syscall(tcp, &t);
Denys Vlasenko7b609d52011-06-22 14:32:43 +02002504 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002505 goto ret;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002506 }
2507 }
2508
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002509 /* If not in -ff mode, and printing_tcp != tcp,
2510 * then the log currently does not end with output
2511 * of _our syscall entry_, but with something else.
2512 * We need to say which syscall's return is this.
2513 *
2514 * Forced reprinting via TCB_REPRINT is used only by
2515 * "strace -ff -oLOG test/threaded_execve" corner case.
2516 * It's the only case when -ff mode needs reprinting.
2517 */
2518 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2519 tcp->flags &= ~TCB_REPRINT;
2520 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002521 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002522 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002523 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002524 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002525 }
2526 printing_tcp = tcp;
2527
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002528 if (res != 1) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002529 /* There was error in one of prior ptrace ops */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002530 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002531 tabto();
Denys Vlasenko000b6012012-01-28 01:25:03 +01002532 tprints("= ? <unavailable>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002533 line_ended();
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002534 tcp->flags &= ~TCB_INSYSCALL;
2535 return res;
2536 }
2537
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002538 sys_res = 0;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002539 if (tcp->qual_flg & QUAL_RAW) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002540 /* sys_res = printargs(tcp); - but it's nop on sysexit */
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002541 } else {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002542 /* FIXME: not_failing_only (IOW, option -z) is broken:
2543 * failure of syscall is known only after syscall return.
2544 * Thus we end up with something like this on, say, ENOENT:
2545 * open("doesnt_exist", O_RDONLY <unfinished ...>
2546 * {next syscall decode}
2547 * whereas the intended result is that open(...) line
2548 * is not shown at all.
2549 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002550 if (not_failing_only && tcp->u_error)
Denys Vlasenko3b738812011-08-22 02:06:35 +02002551 goto ret; /* ignore failed syscalls */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002552 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002553 }
2554
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002555 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002556 tabto();
Denys Vlasenko3b738812011-08-22 02:06:35 +02002557 u_error = tcp->u_error;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002558 if (tcp->qual_flg & QUAL_RAW) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002559 if (u_error)
2560 tprintf("= -1 (errno %ld)", u_error);
2561 else
2562 tprintf("= %#lx", tcp->u_rval);
2563 }
2564 else if (!(sys_res & RVAL_NONE) && u_error) {
2565 switch (u_error) {
Denys Vlasenkofe585652012-01-12 11:26:34 +01002566 /* Blocked signals do not interrupt any syscalls.
2567 * In this case syscalls don't return ERESTARTfoo codes.
2568 *
2569 * Deadly signals set to SIG_DFL interrupt syscalls
2570 * and kill the process regardless of which of the codes below
2571 * is returned by the interrupted syscall.
2572 * In some cases, kernel forces a kernel-generated deadly
2573 * signal to be unblocked and set to SIG_DFL (and thus cause
2574 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2575 * or SIGILL. (The alternative is to leave process spinning
2576 * forever on the faulty instruction - not useful).
2577 *
2578 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2579 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2580 * but kernel will always restart them.
2581 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002582 case ERESTARTSYS:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002583 /* Most common type of signal-interrupted syscall exit code.
2584 * The system call will be restarted with the same arguments
2585 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2586 */
2587 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002588 break;
2589 case ERESTARTNOINTR:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002590 /* Rare. For example, fork() returns this if interrupted.
2591 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2592 */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002593 tprints("= ? ERESTARTNOINTR (To be restarted)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002594 break;
2595 case ERESTARTNOHAND:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002596 /* pause(), rt_sigsuspend() etc use this code.
2597 * SA_RESTART is ignored (assumed not set):
2598 * syscall won't restart (will return EINTR instead)
Denys Vlasenko30c03232013-02-19 16:59:26 +01002599 * even after signal with SA_RESTART set. However,
2600 * after SIG_IGN or SIG_DFL signal it will restart
2601 * (thus the name "restart only if has no handler").
Denys Vlasenkofe585652012-01-12 11:26:34 +01002602 */
Denys Vlasenkoaba62922013-03-05 16:56:35 +01002603 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002604 break;
2605 case ERESTART_RESTARTBLOCK:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002606 /* Syscalls like nanosleep(), poll() which can't be
2607 * restarted with their original arguments use this
2608 * code. Kernel will execute restart_syscall() instead,
2609 * which changes arguments before restarting syscall.
2610 * SA_RESTART is ignored (assumed not set) similarly
2611 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2612 * since restart data is saved in "restart block"
2613 * in task struct, and if signal handler uses a syscall
2614 * which in turn saves another such restart block,
2615 * old data is lost and restart becomes impossible)
2616 */
2617 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002618 break;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002619 default:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002620 if (u_error < 0)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002621 tprintf("= -1 E??? (errno %ld)", u_error);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002622 else if (u_error < nerrnos)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002623 tprintf("= -1 %s (%s)", errnoent[u_error],
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002624 strerror(u_error));
2625 else
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002626 tprintf("= -1 ERRNO_%ld (%s)", u_error,
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002627 strerror(u_error));
2628 break;
2629 }
2630 if ((sys_res & RVAL_STR) && tcp->auxstr)
2631 tprintf(" (%s)", tcp->auxstr);
2632 }
2633 else {
2634 if (sys_res & RVAL_NONE)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002635 tprints("= ?");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002636 else {
2637 switch (sys_res & RVAL_MASK) {
2638 case RVAL_HEX:
2639 tprintf("= %#lx", tcp->u_rval);
2640 break;
2641 case RVAL_OCTAL:
2642 tprintf("= %#lo", tcp->u_rval);
2643 break;
2644 case RVAL_UDECIMAL:
2645 tprintf("= %lu", tcp->u_rval);
2646 break;
2647 case RVAL_DECIMAL:
2648 tprintf("= %ld", tcp->u_rval);
2649 break;
H.J. Ludd0130b2012-04-16 12:16:45 +02002650#if defined(LINUX_MIPSN32) || defined(X32)
2651 /*
2652 case RVAL_LHEX:
2653 tprintf("= %#llx", tcp->u_lrval);
2654 break;
2655 case RVAL_LOCTAL:
2656 tprintf("= %#llo", tcp->u_lrval);
2657 break;
2658 */
2659 case RVAL_LUDECIMAL:
2660 tprintf("= %llu", tcp->u_lrval);
2661 break;
2662 /*
2663 case RVAL_LDECIMAL:
2664 tprintf("= %lld", tcp->u_lrval);
2665 break;
2666 */
2667#endif
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002668 default:
2669 fprintf(stderr,
2670 "invalid rval format\n");
2671 break;
2672 }
2673 }
2674 if ((sys_res & RVAL_STR) && tcp->auxstr)
2675 tprintf(" (%s)", tcp->auxstr);
2676 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002677 if (Tflag) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002678 tv_sub(&tv, &tv, &tcp->etime);
2679 tprintf(" <%ld.%06ld>",
2680 (long) tv.tv_sec, (long) tv.tv_usec);
2681 }
Denys Vlasenko000b6012012-01-28 01:25:03 +01002682 tprints("\n");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002683 dumpio(tcp);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002684 line_ended();
2685
Denys Vlasenko3b738812011-08-22 02:06:35 +02002686 ret:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002687 tcp->flags &= ~TCB_INSYSCALL;
2688 return 0;
2689}
2690
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002691int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002692trace_syscall(struct tcb *tcp)
2693{
2694 return exiting(tcp) ?
2695 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2696}