blob: 83a95bded3bd3cf2d6426732d3a62ee10dedcbb0 [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;
Vineet Gupta7daacbb2013-08-16 12:47:06 +0530777# elif defined(ARC)
778static struct user_regs_struct arc_regs;
779# define ARCH_REGS_FOR_GETREGSET arc_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +0100780#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000781
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100782void
Denys Vlasenko5a2483b2013-07-01 12:49:14 +0200783print_pc(struct tcb *tcp)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100784{
785#define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
786 sizeof(long) == 8 ? "[????????????????] " : \
787 NULL /* crash */)
788 if (get_regs_error) {
789 PRINTBADPC;
790 return;
791 }
792#if defined(I386)
793 tprintf("[%08lx] ", i386_regs.eip);
794#elif defined(S390) || defined(S390X)
795 long psw;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200796 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100797 PRINTBADPC;
798 return;
799 }
800# ifdef S390
801 tprintf("[%08lx] ", psw);
802# elif S390X
Dmitry V. Levinddba73e2013-02-05 19:01:58 +0000803 tprintf("[%016lx] ", psw);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100804# endif
805#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100806 if (x86_io.iov_len == sizeof(i386_regs)) {
807 tprintf("[%08x] ", (unsigned) i386_regs.eip);
808 } else {
809# if defined(X86_64)
810 tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
811# elif defined(X32)
812 /* Note: this truncates 64-bit rip to 32 bits */
813 tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip);
814# endif
815 }
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100816#elif defined(IA64)
817 long ip;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200818 if (upeek(tcp->pid, PT_B0, &ip) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100819 PRINTBADPC;
820 return;
821 }
822 tprintf("[%08lx] ", ip);
823#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +0200824 long pc = ppc_regs.nip;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100825# ifdef POWERPC64
826 tprintf("[%016lx] ", pc);
827# else
828 tprintf("[%08lx] ", pc);
829# endif
830#elif defined(M68K)
831 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200832 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100833 tprints("[????????] ");
834 return;
835 }
836 tprintf("[%08lx] ", pc);
837#elif defined(ALPHA)
838 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200839 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100840 tprints("[????????????????] ");
841 return;
842 }
843 tprintf("[%08lx] ", pc);
844#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100845 tprintf("[%08lx] ", sparc_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100846#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100847 tprintf("[%08lx] ", sparc_regs.tpc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100848#elif defined(HPPA)
849 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200850 if (upeek(tcp->pid, PT_IAOQ0, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100851 tprints("[????????] ");
852 return;
853 }
854 tprintf("[%08lx] ", pc);
855#elif defined(MIPS)
856 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200857 if (upeek(tcp->pid, REG_EPC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100858 tprints("[????????] ");
859 return;
860 }
861 tprintf("[%08lx] ", pc);
862#elif defined(SH)
863 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200864 if (upeek(tcp->pid, 4*REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100865 tprints("[????????] ");
866 return;
867 }
868 tprintf("[%08lx] ", pc);
869#elif defined(SH64)
870 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200871 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100872 tprints("[????????????????] ");
873 return;
874 }
875 tprintf("[%08lx] ", pc);
876#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100877 tprintf("[%08lx] ", arm_regs.ARM_pc);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100878#elif defined(AARCH64)
879 /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100880#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100881 tprintf("[%08lx] ", avr32_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100882#elif defined(BFIN)
883 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200884 if (upeek(tcp->pid, PT_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100885 PRINTBADPC;
886 return;
887 }
888 tprintf("[%08lx] ", pc);
889#elif defined(CRISV10)
890 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200891 if (upeek(tcp->pid, 4*PT_IRP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100892 PRINTBADPC;
893 return;
894 }
895 tprintf("[%08lx] ", pc);
896#elif defined(CRISV32)
897 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200898 if (upeek(tcp->pid, 4*PT_ERP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100899 PRINTBADPC;
900 return;
901 }
902 tprintf("[%08lx] ", pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100903#elif defined(TILE)
904# ifdef _LP64
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500905 tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100906# else
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500907 tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100908# endif
Christian Svensson492f81f2013-02-14 13:26:27 +0100909#elif defined(OR1K)
910 tprintf("[%08lx] ", or1k_regs.pc);
James Hogan5f999a82013-02-22 14:44:10 +0000911#elif defined(METAG)
912 tprintf("[%08lx] ", metag_regs.pc);
Chris Zankel8f636ed2013-03-25 10:22:07 -0700913#elif defined(XTENSA)
914 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200915 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Chris Zankel8f636ed2013-03-25 10:22:07 -0700916 PRINTBADPC;
917 return;
918 }
919 tprintf("[%08lx] ", pc);
Vineet Gupta7daacbb2013-08-16 12:47:06 +0530920#elif defined(ARC)
921 tprintf("[%08lx] ", arc_regs.efa);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100922#endif /* architecture */
923}
924
Denys Vlasenko7270de52013-02-21 15:46:34 +0100925/* Shuffle syscall numbers so that we don't have huge gaps in syscall table.
926 * The shuffling should be reversible: shuffle_scno(shuffle_scno(n)) == n.
927 */
928#if defined(ARM) /* So far only ARM needs this */
929static long
930shuffle_scno(unsigned long scno)
931{
932 if (scno <= ARM_LAST_ORDINARY_SYSCALL)
933 return scno;
934
935 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
936 if (scno == 0x000ffff0)
937 return ARM_LAST_ORDINARY_SYSCALL+1;
938 if (scno == ARM_LAST_ORDINARY_SYSCALL+1)
939 return 0x000ffff0;
940
941 /* Is it ARM specific syscall?
942 * Swap with [LAST_ORDINARY+2, LAST_ORDINARY+2 + LAST_SPECIAL] range.
943 */
944 if (scno >= 0x000f0000
945 && scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL
946 ) {
947 return scno - 0x000f0000 + (ARM_LAST_ORDINARY_SYSCALL+2);
948 }
949 if (/* scno >= ARM_LAST_ORDINARY_SYSCALL+2 - always true */ 1
950 && scno <= (ARM_LAST_ORDINARY_SYSCALL+2) + ARM_LAST_SPECIAL_SYSCALL
951 ) {
952 return scno + 0x000f0000 - (ARM_LAST_ORDINARY_SYSCALL+2);
953 }
954
955 return scno;
956}
957#else
958# define shuffle_scno(scno) ((long)(scno))
959#endif
960
961static char*
962undefined_scno_name(struct tcb *tcp)
963{
964 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
965
966 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
967 return buf;
968}
969
Anton Blanchard14d51a62013-06-26 14:42:37 +1000970#ifdef POWERPC
971/*
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200972 * PTRACE_GETREGS was added to the PowerPC kernel in v2.6.23,
973 * we provide a slow fallback for old kernels.
Anton Blanchard14d51a62013-06-26 14:42:37 +1000974 */
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200975static int powerpc_getregs_old(pid_t pid)
Anton Blanchard14d51a62013-06-26 14:42:37 +1000976{
977 int i;
978 long r;
979
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200980 if (iflag) {
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200981 r = upeek(pid, sizeof(long) * PT_NIP, (long *)&ppc_regs.nip);
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200982 if (r)
983 goto out;
984 }
985#ifdef POWERPC64 /* else we never use it */
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200986 r = upeek(pid, sizeof(long) * PT_MSR, (long *)&ppc_regs.msr);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000987 if (r)
988 goto out;
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200989#endif
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200990 r = upeek(pid, sizeof(long) * PT_CCR, (long *)&ppc_regs.ccr);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000991 if (r)
992 goto out;
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200993 r = upeek(pid, sizeof(long) * PT_ORIG_R3, (long *)&ppc_regs.orig_gpr3);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000994 if (r)
995 goto out;
Anton Blanchard14d51a62013-06-26 14:42:37 +1000996 for (i = 0; i <= 8; i++) {
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200997 r = upeek(pid, sizeof(long) * (PT_R0 + i),
998 (long *)&ppc_regs.gpr[i]);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000999 if (r)
1000 goto out;
1001 }
Denys Vlasenko7f5a1322013-06-28 14:36:39 +02001002 out:
Anton Blanchard14d51a62013-06-26 14:42:37 +10001003 return r;
1004}
1005#endif
1006
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001007#ifndef get_regs
1008long get_regs_error;
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001009
1010#if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001011static void get_regset(pid_t pid)
1012{
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001013/* constant iovec */
1014# if defined(ARM) \
1015 || defined(I386) \
1016 || defined(METAG) \
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301017 || defined(OR1K) \
1018 || defined(ARC)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001019 static struct iovec io = {
1020 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1021 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001022 };
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001023 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1024
1025/* variable iovec */
1026# elif defined(X86_64) || defined(X32)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001027 /* x86_io.iov_base = &x86_regs_union; - already is */
1028 x86_io.iov_len = sizeof(x86_regs_union);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001029 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &x86_io);
1030# elif defined(AARCH64)
1031 /* aarch64_io.iov_base = &arm_regs_union; - already is */
1032 aarch64_io.iov_len = sizeof(arm_regs_union);
1033 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &aarch64_io);
1034# else
1035# warning both PTRACE_GETREGSET and NT_PRSTATUS are available but not yet used
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001036# endif
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001037}
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001038#endif /* PTRACE_GETREGSET && NT_PRSTATUS */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001039
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001040void
1041get_regs(pid_t pid)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001042{
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001043/* PTRACE_GETREGSET only */
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301044# if defined(METAG) || defined(OR1K) || defined(X32) || defined(AARCH64) || defined(ARC)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001045 get_regset(pid);
1046
1047/* PTRACE_GETREGS only */
1048# elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001049 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001050# elif defined(TILE)
1051 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &tile_regs);
1052# elif defined(SPARC) || defined(SPARC64)
1053 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001054# elif defined(POWERPC)
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001055 static bool old_kernel = 0;
1056 if (old_kernel)
1057 goto old;
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001058 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &ppc_regs);
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001059 if (get_regs_error && errno == EIO) {
1060 old_kernel = 1;
1061 old:
Anton Blanchard14d51a62013-06-26 14:42:37 +10001062 get_regs_error = powerpc_getregs_old(pid);
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001063 }
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001064
1065/* try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS */
1066# else
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001067# if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
1068 static int getregset_support;
1069
1070 if (getregset_support >= 0) {
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001071 get_regset(pid);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001072 if (getregset_support > 0)
1073 return;
1074 if (get_regs_error >= 0) {
1075 getregset_support = 1;
1076 return;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001077 }
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001078 if (errno == EPERM || errno == ESRCH)
1079 return;
1080 getregset_support = -1;
1081 }
1082# endif /* PTRACE_GETREGSET && NT_PRSTATUS */
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001083# if defined(ARM)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001084 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &arm_regs);
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001085# elif defined(I386)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001086 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &i386_regs);
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001087# elif defined(X86_64)
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001088 /* Use old method, with unreliable heuristical detection of 32-bitness. */
1089 x86_io.iov_len = sizeof(x86_64_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001090 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &x86_64_regs);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001091 if (!get_regs_error && x86_64_regs.cs == 0x23) {
1092 x86_io.iov_len = sizeof(i386_regs);
1093 /*
1094 * The order is important: i386_regs and x86_64_regs
1095 * are overlaid in memory!
1096 */
1097 i386_regs.ebx = x86_64_regs.rbx;
1098 i386_regs.ecx = x86_64_regs.rcx;
1099 i386_regs.edx = x86_64_regs.rdx;
1100 i386_regs.esi = x86_64_regs.rsi;
1101 i386_regs.edi = x86_64_regs.rdi;
1102 i386_regs.ebp = x86_64_regs.rbp;
1103 i386_regs.eax = x86_64_regs.rax;
1104 /* i386_regs.xds = x86_64_regs.ds; unused by strace */
1105 /* i386_regs.xes = x86_64_regs.es; ditto... */
1106 /* i386_regs.xfs = x86_64_regs.fs; */
1107 /* i386_regs.xgs = x86_64_regs.gs; */
1108 i386_regs.orig_eax = x86_64_regs.orig_rax;
1109 i386_regs.eip = x86_64_regs.rip;
1110 /* i386_regs.xcs = x86_64_regs.cs; */
1111 /* i386_regs.eflags = x86_64_regs.eflags; */
1112 i386_regs.esp = x86_64_regs.rsp;
1113 /* i386_regs.xss = x86_64_regs.ss; */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001114 }
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001115# else
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001116# error unhandled architecture
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001117# endif /* ARM || I386 || X86_64 */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001118# endif
1119}
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001120#endif /* !get_regs */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001121
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001122/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001123 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1124 * 1: ok, continue in trace_syscall_entering().
1125 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001126 * ("????" etc) and bail out.
1127 */
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01001128static int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001129get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001130{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001131 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001132
Denys Vlasenko523635f2012-02-25 02:44:25 +01001133#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001134 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001135 return -1;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001136
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001137 if (s390_gpr2 != -ENOSYS) {
Roland McGrath2f924ca2003-06-26 22:23:28 +00001138 /*
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001139 * Since kernel version 2.5.44 the scno gets passed in gpr2.
Roland McGrath2f924ca2003-06-26 22:23:28 +00001140 */
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001141 scno = s390_gpr2;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001142 } else {
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001143 /*
Michal Ludvig882eda82002-11-11 12:50:47 +00001144 * Old style of "passing" the scno via the SVC instruction.
1145 */
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001146 long psw;
Michal Ludvig882eda82002-11-11 12:50:47 +00001147 long opcode, offset_reg, tmp;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001148 void *svc_addr;
Denys Vlasenko7c9ba8b2011-08-19 19:46:32 +02001149 static const int gpr_offset[16] = {
1150 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
1151 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
1152 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
1153 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
1154 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001155
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001156 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0)
Michal Ludvig882eda82002-11-11 12:50:47 +00001157 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001158 errno = 0;
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001159 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +00001160 if (errno) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001161 perror_msg("peektext(psw-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +00001162 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001163 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001164
1165 /*
1166 * We have to check if the SVC got executed directly or via an
1167 * EXECUTE instruction. In case of EXECUTE it is necessary to do
1168 * instruction decoding to derive the system call number.
1169 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1170 * so that this doesn't work if a SVC opcode is part of an EXECUTE
1171 * opcode. Since there is no way to find out the opcode size this
1172 * is the best we can do...
1173 */
Michal Ludvig882eda82002-11-11 12:50:47 +00001174 if ((opcode & 0xff00) == 0x0a00) {
1175 /* SVC opcode */
1176 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +00001177 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001178 else {
1179 /* SVC got executed by EXECUTE instruction */
1180
1181 /*
1182 * Do instruction decoding of EXECUTE. If you really want to
1183 * understand this, read the Principles of Operations.
1184 */
1185 svc_addr = (void *) (opcode & 0xfff);
1186
1187 tmp = 0;
1188 offset_reg = (opcode & 0x000f0000) >> 16;
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
1193 tmp = 0;
1194 offset_reg = (opcode & 0x0000f000) >> 12;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001195 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001196 return -1;
1197 svc_addr += tmp;
1198
Denys Vlasenkofb036672009-01-23 16:30:26 +00001199 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
Michal Ludvig882eda82002-11-11 12:50:47 +00001200 if (errno)
1201 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001202# if defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +00001203 scno >>= 48;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001204# else
Michal Ludvig882eda82002-11-11 12:50:47 +00001205 scno >>= 16;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001206# endif
Michal Ludvig882eda82002-11-11 12:50:47 +00001207 tmp = 0;
1208 offset_reg = (opcode & 0x00f00000) >> 20;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001209 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001210 return -1;
1211
1212 scno = (scno | tmp) & 0xff;
1213 }
1214 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001215#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001216 scno = ppc_regs.gpr[0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001217# ifdef POWERPC64
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001218 int currpers;
Andreas Schwabd69fa492010-07-12 21:39:57 +02001219
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001220 /* Check for 64/32 bit mode. */
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001221 /* SF is bit 0 of MSR */
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001222 if ((ppc_regs.msr >> 63) & 1)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001223 currpers = 0;
1224 else
1225 currpers = 1;
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001226 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001227# endif
1228#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001229 scno = avr32_regs.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001230#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001231 if (upeek(tcp->pid, PT_ORIG_P0, &scno))
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001232 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001233#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001234 scno = i386_regs.orig_eax;
H.J. Lu35be5812012-04-16 13:00:01 +02001235#elif defined(X86_64) || defined(X32)
1236# ifndef __X32_SYSCALL_BIT
1237# define __X32_SYSCALL_BIT 0x40000000
1238# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001239 int currpers;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001240# if 1
1241 /* GETREGSET of NT_PRSTATUS tells us regset size,
1242 * which unambiguously detects i386.
1243 *
1244 * Linux kernel distinguishes x86-64 and x32 processes
1245 * solely by looking at __X32_SYSCALL_BIT:
1246 * arch/x86/include/asm/compat.h::is_x32_task():
1247 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1248 * return true;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001249 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001250 if (x86_io.iov_len == sizeof(i386_regs)) {
1251 scno = i386_regs.orig_eax;
1252 currpers = 1;
1253 } else {
1254 scno = x86_64_regs.orig_rax;
1255 currpers = 0;
1256 if (scno & __X32_SYSCALL_BIT) {
1257 scno -= __X32_SYSCALL_BIT;
1258 currpers = 2;
1259 }
1260 }
1261# elif 0
1262 /* cs = 0x33 for long mode (native 64 bit and x32)
1263 * cs = 0x23 for compatibility mode (32 bit)
1264 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1265 */
1266 scno = x86_64_regs.orig_rax;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001267 switch (x86_64_regs.cs) {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001268 case 0x23: currpers = 1; break;
H.J. Lu35be5812012-04-16 13:00:01 +02001269 case 0x33:
1270 if (x86_64_regs.ds == 0x2b) {
1271 currpers = 2;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001272 scno &= ~__X32_SYSCALL_BIT;
H.J. Lu35be5812012-04-16 13:00:01 +02001273 } else
1274 currpers = 0;
1275 break;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001276 default:
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001277 fprintf(stderr, "Unknown value CS=0x%08X while "
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001278 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001279 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001280 currpers = current_personality;
1281 break;
1282 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001283# elif 0
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001284 /* This version analyzes the opcode of a syscall instruction.
1285 * (int 0x80 on i386 vs. syscall on x86-64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001286 * It works, but is too complicated, and strictly speaking, unreliable.
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001287 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001288 unsigned long call, rip = x86_64_regs.rip;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001289 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1290 rip -= 2;
1291 errno = 0;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001292 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001293 if (errno)
1294 fprintf(stderr, "ptrace_peektext failed: %s\n",
1295 strerror(errno));
1296 switch (call & 0xffff) {
1297 /* x86-64: syscall = 0x0f 0x05 */
1298 case 0x050f: currpers = 0; break;
1299 /* i386: int 0x80 = 0xcd 0x80 */
1300 case 0x80cd: currpers = 1; break;
1301 default:
1302 currpers = current_personality;
1303 fprintf(stderr,
1304 "Unknown syscall opcode (0x%04X) while "
1305 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001306 "PID=%d\n", (int)call, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001307 break;
1308 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001309# endif
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001310
H.J. Lu35be5812012-04-16 13:00:01 +02001311# ifdef X32
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001312 /* If we are built for a x32 system, then personality 0 is x32
1313 * (not x86_64), and stracing of x86_64 apps is not supported.
1314 * Stracing of i386 apps is still supported.
H.J. Lu085e4282012-04-17 11:05:04 -07001315 */
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001316 if (currpers == 0) {
1317 fprintf(stderr, "syscall_%lu(...) in unsupported "
1318 "64-bit mode of process PID=%d\n",
1319 scno, tcp->pid);
1320 return 0;
H.J. Lu35be5812012-04-16 13:00:01 +02001321 }
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001322 currpers &= ~2; /* map 2,1 to 0,1 */
H.J. Lu35be5812012-04-16 13:00:01 +02001323# endif
H.J. Lu085e4282012-04-17 11:05:04 -07001324 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001325#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001326# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01001327 long psr;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001328 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001329 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
1330 if (ia64_ia32mode) {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001331 if (upeek(tcp->pid, PT_R1, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001332 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001333 } else {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001334 if (upeek(tcp->pid, PT_R15, &scno) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001335 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001336 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001337#elif defined(AARCH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001338 switch (aarch64_io.iov_len) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001339 case sizeof(aarch64_regs):
1340 /* We are in 64-bit mode */
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001341 scno = aarch64_regs.regs[8];
1342 update_personality(tcp, 1);
1343 break;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001344 case sizeof(arm_regs):
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001345 /* We are in 32-bit mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001346 /* Note: we don't support OABI, unlike 32-bit ARM build */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001347 scno = arm_regs.ARM_r7;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001348 update_personality(tcp, 0);
1349 break;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001350 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001351#elif defined(ARM)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001352 if (arm_regs.ARM_ip != 0) {
1353 /* It is not a syscall entry */
1354 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001355 tcp->flags |= TCB_INSYSCALL;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001356 return 0;
1357 }
1358 /* Note: we support only 32-bit CPUs, not 26-bit */
1359
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001360# ifndef STRACE_KNOWS_ONLY_EABI
1361# warning STRACE_KNOWS_ONLY_EABI not set, will PTRACE_PEEKTEXT on every syscall (slower tracing)
1362 if (arm_regs.ARM_cpsr & 0x20)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001363 /* Thumb mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001364 goto scno_in_r7;
1365 /* ARM mode */
1366 /* Check EABI/OABI by examining SVC insn's low 24 bits */
1367 errno = 0;
1368 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1369 if (errno)
1370 return -1;
1371 /* EABI syscall convention? */
1372 if (scno != 0xef000000) {
1373 /* No, it's OABI */
1374 if ((scno & 0x0ff00000) != 0x0f900000) {
1375 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1376 tcp->pid, scno);
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001377 return -1;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001378 }
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001379 /* Fixup the syscall number */
1380 scno &= 0x000fffff;
1381 } else {
1382 scno_in_r7:
1383 scno = arm_regs.ARM_r7;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001384 }
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001385# else
1386 scno = arm_regs.ARM_r7;
1387# endif
Denys Vlasenko7270de52013-02-21 15:46:34 +01001388 scno = shuffle_scno(scno);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001389#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001390 if (upeek(tcp->pid, 4*PT_ORIG_D0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001391 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001392#elif defined(LINUX_MIPSN32)
Roland McGrath542c2c62008-05-20 01:11:56 +00001393 unsigned long long regs[38];
1394
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001395 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001396 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001397 mips_a3 = regs[REG_A3];
1398 mips_r2 = regs[REG_V0];
Roland McGrath542c2c62008-05-20 01:11:56 +00001399
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001400 scno = mips_r2;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001401 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001402 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001403 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001404 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001405 return 0;
1406 }
Roland McGrath542c2c62008-05-20 01:11:56 +00001407 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001408#elif defined(MIPS)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001409 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001410 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001411 if (upeek(tcp->pid, REG_V0, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001412 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001413
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001414 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001415 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001416 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001417 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001418 return 0;
1419 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001420 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001421#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001422 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001423 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001424 if (upeek(tcp->pid, REG_R0, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001425 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001426
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001427 /*
1428 * Do some sanity checks to figure out if it's
1429 * really a syscall entry
1430 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001431 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001432 if (alpha_a3 == 0 || alpha_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001433 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001434 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001435 return 0;
1436 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001437 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001438#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001439 /* Disassemble the syscall trap. */
1440 /* Retrieve the syscall trap instruction. */
Denys Vlasenko46455822013-02-05 17:02:59 +01001441 unsigned long trap;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001442 errno = 0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001443# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001444 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001445 trap >>= 32;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001446# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001447 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001448# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001449 if (errno)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001450 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001451
1452 /* Disassemble the trap to see what personality to use. */
1453 switch (trap) {
1454 case 0x91d02010:
1455 /* Linux/SPARC syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001456 update_personality(tcp, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001457 break;
1458 case 0x91d0206d:
1459 /* Linux/SPARC64 syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001460 update_personality(tcp, 2);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001461 break;
1462 case 0x91d02000:
1463 /* SunOS syscall trap. (pers 1) */
1464 fprintf(stderr, "syscall: SunOS no support\n");
1465 return -1;
1466 case 0x91d02008:
1467 /* Solaris 2.x syscall trap. (per 2) */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001468 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001469 break;
1470 case 0x91d02009:
1471 /* NetBSD/FreeBSD syscall trap. */
1472 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1473 return -1;
1474 case 0x91d02027:
1475 /* Solaris 2.x gettimeofday */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001476 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001477 break;
1478 default:
Denys Vlasenko523635f2012-02-25 02:44:25 +01001479# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001480 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001481# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001482 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001483# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001484 return -1;
1485 }
1486
1487 /* Extract the system call number from the registers. */
1488 if (trap == 0x91d02027)
1489 scno = 156;
1490 else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001491 scno = sparc_regs.u_regs[U_REG_G1];
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001492 if (scno == 0) {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001493 scno = sparc_regs.u_regs[U_REG_O0];
1494 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 +02001495 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001496#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001497 if (upeek(tcp->pid, PT_GR20, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001498 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001499#elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001500 /*
1501 * In the new syscall ABI, the system call number is in R3.
1502 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001503 if (upeek(tcp->pid, 4*(REG_REG0+3), &scno) < 0)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001504 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001505
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001506 if (scno < 0) {
1507 /* Odd as it may seem, a glibc bug has been known to cause
1508 glibc to issue bogus negative syscall numbers. So for
1509 our purposes, make strace print what it *should* have been */
1510 long correct_scno = (scno & 0xff);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001511 if (debug_flag)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001512 fprintf(stderr,
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001513 "Detected glibc bug: bogus system call"
1514 " number = %ld, correcting to %ld\n",
1515 scno,
1516 correct_scno);
1517 scno = correct_scno;
1518 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001519#elif defined(SH64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001520 if (upeek(tcp->pid, REG_SYSCALL, &scno) < 0)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001521 return -1;
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001522 scno &= 0xFFFF;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001523#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001524 if (upeek(tcp->pid, 4*PT_R9, &scno) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001525 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001526#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001527 int currpers;
1528 scno = tile_regs.regs[10];
1529# ifdef __tilepro__
1530 currpers = 1;
1531# else
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001532# ifndef PT_FLAGS_COMPAT
1533# define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1534# endif
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001535 if (tile_regs.flags & PT_FLAGS_COMPAT)
1536 currpers = 1;
1537 else
1538 currpers = 0;
1539# endif
1540 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001541#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001542 if (upeek(tcp->pid, 0, &scno) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001543 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001544#elif defined(OR1K)
1545 scno = or1k_regs.gpr[11];
James Hogan5f999a82013-02-22 14:44:10 +00001546#elif defined(METAG)
1547 scno = metag_regs.dx[0][1]; /* syscall number in D1Re0 (D1.0) */
Chris Zankel8f636ed2013-03-25 10:22:07 -07001548#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001549 if (upeek(tcp->pid, SYSCALL_NR, &scno) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001550 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301551# elif defined(ARC)
1552 scno = arc_regs.scratch.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001553#endif
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001554
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001555 tcp->scno = scno;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001556 if (SCNO_IS_VALID(tcp->scno)) {
1557 tcp->s_ent = &sysent[scno];
1558 tcp->qual_flg = qual_flags[scno];
1559 } else {
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01001560 static const struct_sysent unknown = {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001561 .nargs = MAX_ARGS,
1562 .sys_flags = 0,
1563 .sys_func = printargs,
1564 .sys_name = "unknown", /* not used */
1565 };
1566 tcp->s_ent = &unknown;
1567 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1568 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001569 return 1;
1570}
1571
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02001572/* Called at each syscall entry.
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001573 * Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001574 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1575 * 1: ok, continue in trace_syscall_entering().
1576 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001577 * ("????" etc) and bail out.
1578 */
Roland McGratha4d48532005-06-08 20:45:28 +00001579static int
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001580syscall_fixup_on_sysenter(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001581{
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001582 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001583#if defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001584 if (i386_regs.eax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001585 if (debug_flag)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001586 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1587 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001588 }
H.J. Lu35be5812012-04-16 13:00:01 +02001589#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001590 {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001591 long rax;
1592 if (x86_io.iov_len == sizeof(i386_regs)) {
1593 /* Sign extend from 32 bits */
1594 rax = (int32_t)i386_regs.eax;
1595 } else {
1596 /* Note: in X32 build, this truncates 64 to 32 bits */
1597 rax = x86_64_regs.rax;
1598 }
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001599 if (rax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001600 if (debug_flag)
Denys Vlasenko18beb982011-08-24 16:59:23 +02001601 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1602 return 0;
1603 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001604 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001605#elif defined(M68K)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001606 /* TODO? Eliminate upeek's in arches below like we did in x86 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001607 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001608 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001609 if (m68k_d0 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001610 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001611 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001612 return 0;
1613 }
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001614#elif defined(IA64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001615 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001616 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001617 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001618 return -1;
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001619 if (ia64_ia32mode && ia64_r8 != -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 (r8 = %ld)\n", ia64_r8);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001622 return 0;
1623 }
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001624#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001625 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001626 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001627 if (cris_r10 != -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 (r10 = %ld)\n", cris_r10);
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001630 return 0;
1631 }
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001632#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001633 if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001634 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001635 if (microblaze_r3 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001636 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001637 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001638 return 0;
1639 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001640#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001641 return 1;
1642}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001643
Denys Vlasenko146b9442012-03-18 22:10:48 +01001644static void
1645internal_fork(struct tcb *tcp)
1646{
1647#if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1648# define ARG_FLAGS 1
1649#else
1650# define ARG_FLAGS 0
1651#endif
1652#ifndef CLONE_UNTRACED
1653# define CLONE_UNTRACED 0x00800000
1654#endif
1655 if ((ptrace_setoptions
1656 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1657 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1658 return;
1659
1660 if (!followfork)
1661 return;
1662
1663 if (entering(tcp)) {
1664 /*
1665 * We won't see the new child if clone is called with
1666 * CLONE_UNTRACED, so we keep the same logic with that option
1667 * and don't trace it.
1668 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001669 if ((tcp->s_ent->sys_func == sys_clone)
1670 && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
1671 )
Denys Vlasenko146b9442012-03-18 22:10:48 +01001672 return;
1673 setbpt(tcp);
1674 } else {
1675 if (tcp->flags & TCB_BPTSET)
1676 clearbpt(tcp);
1677 }
1678}
1679
1680#if defined(TCB_WAITEXECVE)
1681static void
1682internal_exec(struct tcb *tcp)
1683{
1684 /* Maybe we have post-execve SIGTRAP suppressed? */
1685 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1686 return; /* yes, no need to do anything */
1687
1688 if (exiting(tcp) && syserror(tcp))
1689 /* Error in execve, no post-execve SIGTRAP expected */
1690 tcp->flags &= ~TCB_WAITEXECVE;
1691 else
1692 tcp->flags |= TCB_WAITEXECVE;
1693}
1694#endif
1695
1696static void
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001697syscall_fixup_for_fork_exec(struct tcb *tcp)
Roland McGrathc1e45922008-05-27 23:18:29 +00001698{
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001699 /*
1700 * We must always trace a few critical system calls in order to
1701 * correctly support following forks in the presence of tracing
1702 * qualifiers.
1703 */
1704 int (*func)();
1705
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001706 func = tcp->s_ent->sys_func;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001707
1708 if ( sys_fork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001709 || sys_vfork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001710 || sys_clone == func
Denys Vlasenko146b9442012-03-18 22:10:48 +01001711 ) {
1712 internal_fork(tcp);
1713 return;
1714 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001715
Denys Vlasenko84703742012-02-25 02:38:52 +01001716#if defined(TCB_WAITEXECVE)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001717 if ( sys_execve == func
Denys Vlasenko84703742012-02-25 02:38:52 +01001718# if defined(SPARC) || defined(SPARC64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001719 || sys_execv == func
Denys Vlasenkoa7949742011-08-21 17:26:55 +02001720# endif
Denys Vlasenko146b9442012-03-18 22:10:48 +01001721 ) {
1722 internal_exec(tcp);
1723 return;
1724 }
Roland McGrathc1e45922008-05-27 23:18:29 +00001725#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001726}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001727
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001728/* Return -1 on error or 1 on success (never 0!) */
Roland McGratha4d48532005-06-08 20:45:28 +00001729static int
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001730get_syscall_args(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001731{
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001732 int i, nargs;
1733
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001734 nargs = tcp->s_ent->nargs;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001735
Denys Vlasenko523635f2012-02-25 02:44:25 +01001736#if defined(S390) || defined(S390X)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001737 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001738 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 +02001739 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001740#elif defined(ALPHA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001741 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001742 if (upeek(tcp->pid, REG_A0+i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001743 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001744#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001745 if (!ia64_ia32mode) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001746 unsigned long *out0, cfm, sof, sol;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001747 long rbs_end;
1748 /* be backwards compatible with kernel < 2.4.4... */
1749# ifndef PT_RBS_END
1750# define PT_RBS_END PT_AR_BSP
1751# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001752
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001753 if (upeek(tcp->pid, PT_RBS_END, &rbs_end) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001754 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001755 if (upeek(tcp->pid, PT_CFM, (long *) &cfm) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001756 return -1;
1757
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001758 sof = (cfm >> 0) & 0x7f;
1759 sol = (cfm >> 7) & 0x7f;
1760 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1761
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001762 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001763 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1764 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1765 return -1;
1766 }
1767 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001768 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1769 PT_R9 /* ECX = out1 */,
1770 PT_R10 /* EDX = out2 */,
1771 PT_R14 /* ESI = out3 */,
1772 PT_R15 /* EDI = out4 */,
1773 PT_R13 /* EBP = out5 */};
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001774
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001775 for (i = 0; i < nargs; ++i) {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001776 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001777 return -1;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001778 /* truncate away IVE sign-extension */
1779 tcp->u_arg[i] &= 0xffffffff;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001780 }
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001781 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001782#elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001783 /* N32 and N64 both use up to six registers. */
1784 unsigned long long regs[38];
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001785
1786 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1787 return -1;
1788
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001789 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001790 tcp->u_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001791# if defined(LINUX_MIPSN32)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001792 tcp->ext_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001793# endif
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001794 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001795#elif defined(MIPS)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001796 if (nargs > 4) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001797 long sp;
1798
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001799 if (upeek(tcp->pid, REG_SP, &sp) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001800 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001801 for (i = 0; i < 4; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001802 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001803 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001804 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001805 (char *)(tcp->u_arg + 4));
1806 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001807 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001808 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001809 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001810 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001811#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001812 (void)i;
1813 (void)nargs;
1814 tcp->u_arg[0] = ppc_regs.orig_gpr3;
1815 tcp->u_arg[1] = ppc_regs.gpr[4];
1816 tcp->u_arg[2] = ppc_regs.gpr[5];
1817 tcp->u_arg[3] = ppc_regs.gpr[6];
1818 tcp->u_arg[4] = ppc_regs.gpr[7];
1819 tcp->u_arg[5] = ppc_regs.gpr[8];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001820#elif defined(SPARC) || defined(SPARC64)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001821 for (i = 0; i < nargs; ++i)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001822 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001823#elif defined(HPPA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001824 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001825 if (upeek(tcp->pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001826 return -1;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001827#elif defined(ARM) || defined(AARCH64)
1828# if defined(AARCH64)
1829 if (tcp->currpers == 1)
1830 for (i = 0; i < nargs; ++i)
1831 tcp->u_arg[i] = aarch64_regs.regs[i];
1832 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001833# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001834 for (i = 0; i < nargs; ++i)
Denys Vlasenko401374e2013-02-06 18:24:39 +01001835 tcp->u_arg[i] = arm_regs.uregs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001836#elif defined(AVR32)
Denys Vlasenkob5b25892011-08-30 19:04:54 +02001837 (void)i;
1838 (void)nargs;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001839 tcp->u_arg[0] = avr32_regs.r12;
1840 tcp->u_arg[1] = avr32_regs.r11;
1841 tcp->u_arg[2] = avr32_regs.r10;
1842 tcp->u_arg[3] = avr32_regs.r9;
1843 tcp->u_arg[4] = avr32_regs.r5;
1844 tcp->u_arg[5] = avr32_regs.r3;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001845#elif defined(BFIN)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001846 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 +02001847
Denys Vlasenko4b887a52011-08-23 13:32:38 +02001848 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001849 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001850 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001851#elif defined(SH)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001852 static const int syscall_regs[MAX_ARGS] = {
1853 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1854 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001855 };
1856
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, syscall_regs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001859 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001860#elif defined(SH64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001861 int i;
1862 /* Registers used by SH5 Linux system calls for parameters */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001863 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001864
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001865 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001866 if (upeek(tcp->pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001867 return -1;
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001868#elif defined(I386)
1869 (void)i;
1870 (void)nargs;
1871 tcp->u_arg[0] = i386_regs.ebx;
1872 tcp->u_arg[1] = i386_regs.ecx;
1873 tcp->u_arg[2] = i386_regs.edx;
1874 tcp->u_arg[3] = i386_regs.esi;
1875 tcp->u_arg[4] = i386_regs.edi;
1876 tcp->u_arg[5] = i386_regs.ebp;
H.J. Lu35be5812012-04-16 13:00:01 +02001877#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001878 (void)i;
1879 (void)nargs;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001880 if (x86_io.iov_len != sizeof(i386_regs)) {
1881 /* x86-64 or x32 ABI */
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001882 tcp->u_arg[0] = x86_64_regs.rdi;
1883 tcp->u_arg[1] = x86_64_regs.rsi;
1884 tcp->u_arg[2] = x86_64_regs.rdx;
1885 tcp->u_arg[3] = x86_64_regs.r10;
1886 tcp->u_arg[4] = x86_64_regs.r8;
1887 tcp->u_arg[5] = x86_64_regs.r9;
H.J. Lu35be5812012-04-16 13:00:01 +02001888# ifdef X32
1889 tcp->ext_arg[0] = x86_64_regs.rdi;
1890 tcp->ext_arg[1] = x86_64_regs.rsi;
1891 tcp->ext_arg[2] = x86_64_regs.rdx;
1892 tcp->ext_arg[3] = x86_64_regs.r10;
1893 tcp->ext_arg[4] = x86_64_regs.r8;
1894 tcp->ext_arg[5] = x86_64_regs.r9;
1895# endif
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001896 } else {
1897 /* i386 ABI */
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001898 /* Zero-extend from 32 bits */
1899 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1900 * if you need to use *sign-extended* parameter.
1901 */
1902 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1903 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1904 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1905 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1906 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1907 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001908 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001909#elif defined(MICROBLAZE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001910 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001911 if (upeek(tcp->pid, (5 + i) * 4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001912 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001913#elif defined(CRISV10) || defined(CRISV32)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001914 static const int crisregs[MAX_ARGS] = {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001915 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001916 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001917 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001918
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001919 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001920 if (upeek(tcp->pid, crisregs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001921 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001922#elif defined(TILE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001923 for (i = 0; i < nargs; ++i)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001924 tcp->u_arg[i] = tile_regs.regs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001925#elif defined(M68K)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001926 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001927 if (upeek(tcp->pid, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001928 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001929#elif defined(OR1K)
1930 (void)nargs;
1931 for (i = 0; i < 6; ++i)
1932 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
James Hogan5f999a82013-02-22 14:44:10 +00001933#elif defined(METAG)
1934 for (i = 0; i < nargs; i++)
1935 /* arguments go backwards from D1Ar1 (D1.3) */
1936 tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
Chris Zankel8f636ed2013-03-25 10:22:07 -07001937#elif defined(XTENSA)
1938 /* arg0: a6, arg1: a3, arg2: a4, arg3: a5, arg4: a8, arg5: a9 */
1939 static const int xtensaregs[MAX_ARGS] = { 6, 3, 4, 5, 8, 9 };
1940 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001941 if (upeek(tcp->pid, REG_A_BASE + xtensaregs[i], &tcp->u_arg[i]) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001942 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301943# elif defined(ARC)
1944 long *arc_args = &arc_regs.scratch.r0;
1945 for (i = 0; i < nargs; ++i)
1946 tcp->u_arg[i] = *arc_args--;
1947
Denys Vlasenko523635f2012-02-25 02:44:25 +01001948#else /* Other architecture (32bits specific) */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001949 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001950 if (upeek(tcp->pid, i*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001951 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001952#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001953 return 1;
1954}
1955
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00001956static int
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001957trace_syscall_entering(struct tcb *tcp)
1958{
1959 int res, scno_good;
1960
Denys Vlasenko2ce12ed2011-08-24 17:25:32 +02001961#if defined TCB_WAITEXECVE
1962 if (tcp->flags & TCB_WAITEXECVE) {
1963 /* This is the post-execve SIGTRAP. */
1964 tcp->flags &= ~TCB_WAITEXECVE;
1965 return 0;
1966 }
1967#endif
1968
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001969 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001970 if (res == 0)
1971 return res;
Denys Vlasenko907735a2012-03-21 00:23:16 +01001972 if (res == 1) {
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001973 res = syscall_fixup_on_sysenter(tcp);
Denys Vlasenko907735a2012-03-21 00:23:16 +01001974 if (res == 0)
1975 return res;
1976 if (res == 1)
1977 res = get_syscall_args(tcp);
1978 }
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001979
1980 if (res != 1) {
1981 printleader(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001982 if (scno_good != 1)
Denys Vlasenkob7a6dae2012-03-20 16:48:35 +01001983 tprints("????" /* anti-trigraph gap */ "(");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001984 else if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01001985 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001986 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001987 tprintf("%s(", tcp->s_ent->sys_name);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001988 /*
1989 * " <unavailable>" will be added later by the code which
1990 * detects ptrace errors.
1991 */
1992 goto ret;
1993 }
1994
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02001995 if ( sys_execve == tcp->s_ent->sys_func
1996# if defined(SPARC) || defined(SPARC64)
1997 || sys_execv == tcp->s_ent->sys_func
1998# endif
1999 ) {
2000 hide_log_until_execve = 0;
2001 }
2002
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002003#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002004 while (1) {
Denys Vlasenko523635f2012-02-25 02:44:25 +01002005# ifdef SYS_socket_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002006 if (tcp->s_ent->sys_func == sys_socketcall) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00002007 decode_socket_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002008 break;
2009 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002010# endif
2011# ifdef SYS_ipc_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002012 if (tcp->s_ent->sys_func == sys_ipc) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00002013 decode_ipc_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002014 break;
2015 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002016# endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002017 break;
2018 }
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002019#endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002020
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01002021 if (need_fork_exec_workarounds)
2022 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002023
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002024 if (!(tcp->qual_flg & QUAL_TRACE)
2025 || (tracing_paths && !pathtrace_match(tcp))
2026 ) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002027 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
2028 return 0;
2029 }
2030
2031 tcp->flags &= ~TCB_FILTERED;
2032
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002033 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002034 res = 0;
2035 goto ret;
2036 }
2037
2038 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002039 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002040 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002041 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002042 tprintf("%s(", tcp->s_ent->sys_name);
2043 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002044 res = printargs(tcp);
2045 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002046 res = tcp->s_ent->sys_func(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002047
Dmitry V. Levinb742d8c2012-09-17 22:40:12 +00002048 fflush(tcp->outf);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002049 ret:
2050 tcp->flags |= TCB_INSYSCALL;
2051 /* Measure the entrance time as late as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002052 if (Tflag || cflag)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002053 gettimeofday(&tcp->etime, NULL);
2054 return res;
2055}
2056
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002057/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01002058 * 1: ok, continue in trace_syscall_exiting().
2059 * -1: error, trace_syscall_exiting() should print error indicator
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002060 * ("????" etc) and bail out.
2061 */
2062static int
2063get_syscall_result(struct tcb *tcp)
2064{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002065#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002066 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002067 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002068#elif defined(POWERPC)
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002069 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002070#elif defined(AVR32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002071 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002072#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002073 if (upeek(tcp->pid, PT_R0, &bfin_r0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002074 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002075#elif defined(I386)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002076 /* already done by get_regs */
H.J. Lu35be5812012-04-16 13:00:01 +02002077#elif defined(X86_64) || defined(X32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002078 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002079#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002080# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01002081 long psr;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002082 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02002083 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002084 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002085 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002086 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002087 return -1;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002088#elif defined(ARM)
2089 /* already done by get_regs */
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002090#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002091 /* register reading already done by get_regs */
2092
2093 /* Used to do this, but we did it on syscall entry already: */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002094 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
2095 * else it's personality 0.
2096 */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002097 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
Denys Vlasenko523635f2012-02-25 02:44:25 +01002098#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002099 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002100 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002101#elif defined(LINUX_MIPSN32)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002102 unsigned long long regs[38];
2103
2104 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
2105 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002106 mips_a3 = regs[REG_A3];
2107 mips_r2 = regs[REG_V0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01002108#elif defined(MIPS)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002109 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002110 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002111 if (upeek(tcp->pid, REG_V0, &mips_r2) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002112 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002113#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002114 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002115 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002116 if (upeek(tcp->pid, REG_R0, &alpha_r0) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002117 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002118#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002119 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002120#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002121 if (upeek(tcp->pid, PT_GR28, &hppa_r28) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002122 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002123#elif defined(SH)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002124 /* new syscall ABI returns result in R0 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002125 if (upeek(tcp->pid, 4*REG_REG0, (long *)&sh_r0) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002126 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002127#elif defined(SH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002128 /* ABI defines result returned in r9 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002129 if (upeek(tcp->pid, REG_GENERAL(9), (long *)&sh64_r9) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002130 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002131#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002132 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002133 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002134#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002135 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002136#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002137 if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002138 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01002139#elif defined(OR1K)
2140 /* already done by get_regs */
James Hogan5f999a82013-02-22 14:44:10 +00002141#elif defined(METAG)
2142 /* already done by get_regs */
Chris Zankel8f636ed2013-03-25 10:22:07 -07002143#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002144 if (upeek(tcp->pid, REG_A_BASE + 2, &xtensa_a2) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07002145 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05302146#elif defined(ARC)
2147 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002148#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002149 return 1;
2150}
2151
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002152/* Called at each syscall exit */
2153static void
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002154syscall_fixup_on_sysexit(struct tcb *tcp)
2155{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002156#if defined(S390) || defined(S390X)
Denys Vlasenkoece98792011-08-25 10:25:35 +02002157 if ((tcp->flags & TCB_WAITEXECVE)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002158 && (s390_gpr2 == -ENOSYS || s390_gpr2 == tcp->scno)) {
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002159 /*
2160 * Return from execve.
2161 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2162 * flag set for the post-execve SIGTRAP to see and reset.
2163 */
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002164 s390_gpr2 = 0;
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002165 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002166#endif
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002167}
2168
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002169/*
2170 * Check the syscall return value register value for whether it is
2171 * a negated errno code indicating an error, or a success return value.
2172 */
2173static inline int
2174is_negated_errno(unsigned long int val)
2175{
2176 unsigned long int max = -(long int) nerrnos;
Denys Vlasenko2544f982013-02-19 17:39:56 +01002177#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01002178 if (current_wordsize < sizeof(val)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002179 val = (unsigned int) val;
2180 max = (unsigned int) max;
2181 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002182#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002183 return val > max;
2184}
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002185
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002186#if defined(X32)
2187static inline int
2188is_negated_errno_x32(unsigned long long val)
2189{
2190 unsigned long long max = -(long long) nerrnos;
2191 /*
2192 * current_wordsize is 4 even in personality 0 (native X32)
2193 * but truncation _must not_ be done in it.
2194 * can't check current_wordsize here!
2195 */
2196 if (current_personality != 0) {
2197 val = (uint32_t) val;
2198 max = (uint32_t) max;
2199 }
2200 return val > max;
2201}
2202#endif
2203
Denys Vlasenko907735a2012-03-21 00:23:16 +01002204/* Returns:
2205 * 1: ok, continue in trace_syscall_exiting().
2206 * -1: error, trace_syscall_exiting() should print error indicator
2207 * ("????" etc) and bail out.
2208 */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002209static void
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002210get_error(struct tcb *tcp)
2211{
2212 int u_error = 0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002213 int check_errno = 1;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002214 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002215 check_errno = 0;
2216 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002217#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002218 if (check_errno && is_negated_errno(s390_gpr2)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002219 tcp->u_rval = -1;
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002220 u_error = -s390_gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002221 }
2222 else {
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002223 tcp->u_rval = s390_gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002224 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002225#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002226 if (check_errno && is_negated_errno(i386_regs.eax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002227 tcp->u_rval = -1;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002228 u_error = -i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002229 }
2230 else {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002231 tcp->u_rval = i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002232 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002233#elif defined(X86_64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002234 long rax;
2235 if (x86_io.iov_len == sizeof(i386_regs)) {
2236 /* Sign extend from 32 bits */
2237 rax = (int32_t)i386_regs.eax;
2238 } else {
2239 rax = x86_64_regs.rax;
2240 }
2241 if (check_errno && is_negated_errno(rax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002242 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002243 u_error = -rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002244 }
2245 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002246 tcp->u_rval = rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002247 }
2248#elif defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002249 /* In X32, return value is 64-bit (llseek uses one).
2250 * Using merely "long rax" would not work.
2251 */
2252 long long rax;
2253 if (x86_io.iov_len == sizeof(i386_regs)) {
2254 /* Sign extend from 32 bits */
2255 rax = (int32_t)i386_regs.eax;
2256 } else {
2257 rax = x86_64_regs.rax;
2258 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002259 /* Careful: is_negated_errno() works only on longs */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002260 if (check_errno && is_negated_errno_x32(rax)) {
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002261 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002262 u_error = -rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002263 }
2264 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002265 tcp->u_rval = rax; /* truncating */
2266 tcp->u_lrval = rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002267 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002268#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02002269 if (ia64_ia32mode) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002270 int err;
2271
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002272 err = (int)ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002273 if (check_errno && is_negated_errno(err)) {
2274 tcp->u_rval = -1;
2275 u_error = -err;
2276 }
2277 else {
2278 tcp->u_rval = err;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002279 }
2280 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002281 if (check_errno && ia64_r10) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002282 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002283 u_error = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002284 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002285 tcp->u_rval = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002286 }
2287 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002288#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002289 if (check_errno && mips_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002290 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002291 u_error = mips_r2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002292 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002293 tcp->u_rval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002294# if defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002295 tcp->u_lrval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002296# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002297 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002298#elif defined(POWERPC)
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002299 if (check_errno && (ppc_regs.ccr & 0x10000000)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002300 tcp->u_rval = -1;
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002301 u_error = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002302 }
2303 else {
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002304 tcp->u_rval = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002305 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002306#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002307 if (check_errno && is_negated_errno(m68k_d0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002308 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002309 u_error = -m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002310 }
2311 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002312 tcp->u_rval = m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002313 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002314#elif defined(ARM) || defined(AARCH64)
2315# if defined(AARCH64)
2316 if (tcp->currpers == 1) {
2317 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2318 tcp->u_rval = -1;
2319 u_error = -aarch64_regs.regs[0];
2320 }
2321 else {
2322 tcp->u_rval = aarch64_regs.regs[0];
2323 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002324 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002325 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002326# endif
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002327 {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002328 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002329 tcp->u_rval = -1;
Denys Vlasenko401374e2013-02-06 18:24:39 +01002330 u_error = -arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002331 }
2332 else {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002333 tcp->u_rval = arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002334 }
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002335 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002336#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002337 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002338 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002339 u_error = -avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002340 }
2341 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002342 tcp->u_rval = avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002343 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002344#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002345 if (check_errno && is_negated_errno(bfin_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002346 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002347 u_error = -bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002348 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002349 tcp->u_rval = bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002350 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002351#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002352 if (check_errno && alpha_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002353 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002354 u_error = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002355 }
2356 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002357 tcp->u_rval = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002358 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002359#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002360 if (check_errno && sparc_regs.psr & PSR_C) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002361 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002362 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002363 }
2364 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002365 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002366 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002367#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002368 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002369 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002370 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002371 }
2372 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002373 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002374 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002375#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002376 if (check_errno && is_negated_errno(hppa_r28)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002377 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002378 u_error = -hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002379 }
2380 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002381 tcp->u_rval = hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002382 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002383#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002384 if (check_errno && is_negated_errno(sh_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002385 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002386 u_error = -sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002387 }
2388 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002389 tcp->u_rval = sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002390 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002391#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002392 if (check_errno && is_negated_errno(sh64_r9)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002393 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002394 u_error = -sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002395 }
2396 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002397 tcp->u_rval = sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002398 }
James Hogan5f999a82013-02-22 14:44:10 +00002399#elif defined(METAG)
2400 /* result pointer in D0Re0 (D0.0) */
2401 if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2402 tcp->u_rval = -1;
2403 u_error = -metag_regs.dx[0][0];
2404 }
2405 else {
2406 tcp->u_rval = metag_regs.dx[0][0];
2407 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002408#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002409 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002410 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002411 u_error = -cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002412 }
2413 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002414 tcp->u_rval = cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002415 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002416#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002417 /*
2418 * The standard tile calling convention returns the value (or negative
2419 * errno) in r0, and zero (or positive errno) in r1.
2420 * Until at least kernel 3.8, however, the r1 value is not reflected
2421 * in ptregs at this point, so we use r0 here.
2422 */
2423 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002424 tcp->u_rval = -1;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002425 u_error = -tile_regs.regs[0];
2426 } else {
2427 tcp->u_rval = tile_regs.regs[0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002428 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002429#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002430 if (check_errno && is_negated_errno(microblaze_r3)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002431 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002432 u_error = -microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002433 }
2434 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002435 tcp->u_rval = microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002436 }
Christian Svensson492f81f2013-02-14 13:26:27 +01002437#elif defined(OR1K)
2438 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2439 tcp->u_rval = -1;
2440 u_error = -or1k_regs.gpr[11];
2441 }
2442 else {
2443 tcp->u_rval = or1k_regs.gpr[11];
2444 }
Chris Zankel8f636ed2013-03-25 10:22:07 -07002445#elif defined(XTENSA)
2446 if (check_errno && is_negated_errno(xtensa_a2)) {
2447 tcp->u_rval = -1;
2448 u_error = -xtensa_a2;
2449 }
2450 else {
2451 tcp->u_rval = xtensa_a2;
2452 }
Vineet Gupta7daacbb2013-08-16 12:47:06 +05302453#elif defined(ARC)
2454 if (check_errno && is_negated_errno(arc_regs.scratch.r0)) {
2455 tcp->u_rval = -1;
2456 u_error = -arc_regs.scratch.r0;
2457 }
2458 else {
2459 tcp->u_rval = arc_regs.scratch.r0;
2460 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002461#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002462 tcp->u_error = u_error;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002463}
2464
2465static void
2466dumpio(struct tcb *tcp)
2467{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002468 int (*func)();
2469
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002470 if (syserror(tcp))
2471 return;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +01002472 if ((unsigned long) tcp->u_arg[0] >= num_quals)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002473 return;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002474 func = tcp->s_ent->sys_func;
2475 if (func == printargs)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002476 return;
2477 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002478 if (func == sys_read ||
2479 func == sys_pread ||
2480 func == sys_recv ||
2481 func == sys_recvfrom)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002482 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002483 else if (func == sys_readv)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002484 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2485 return;
2486 }
2487 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002488 if (func == sys_write ||
2489 func == sys_pwrite ||
2490 func == sys_send ||
2491 func == sys_sendto)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002492 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002493 else if (func == sys_writev)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002494 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2495 return;
2496 }
2497}
2498
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002499static int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002500trace_syscall_exiting(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002501{
2502 int sys_res;
2503 struct timeval tv;
Denys Vlasenko1a5b5a72011-08-25 00:29:56 +02002504 int res;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002505 long u_error;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002506
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002507 /* Measure the exit time as early as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002508 if (Tflag || cflag)
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002509 gettimeofday(&tv, NULL);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002510
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00002511#if SUPPORTED_PERSONALITIES > 1
2512 update_personality(tcp, tcp->currpers);
2513#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002514 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002515 if (res == 1) {
2516 syscall_fixup_on_sysexit(tcp); /* never fails */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002517 get_error(tcp); /* never fails */
2518 if (need_fork_exec_workarounds)
2519 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002520 if (filtered(tcp) || hide_log_until_execve)
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002521 goto ret;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002522 }
2523
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002524 if (cflag) {
2525 struct timeval t = tv;
Denys Vlasenkoc95a88f2011-08-21 17:47:40 +02002526 count_syscall(tcp, &t);
Denys Vlasenko7b609d52011-06-22 14:32:43 +02002527 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002528 goto ret;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002529 }
2530 }
2531
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002532 /* If not in -ff mode, and printing_tcp != tcp,
2533 * then the log currently does not end with output
2534 * of _our syscall entry_, but with something else.
2535 * We need to say which syscall's return is this.
2536 *
2537 * Forced reprinting via TCB_REPRINT is used only by
2538 * "strace -ff -oLOG test/threaded_execve" corner case.
2539 * It's the only case when -ff mode needs reprinting.
2540 */
2541 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2542 tcp->flags &= ~TCB_REPRINT;
2543 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002544 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002545 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002546 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002547 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002548 }
2549 printing_tcp = tcp;
2550
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002551 if (res != 1) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002552 /* There was error in one of prior ptrace ops */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002553 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002554 tabto();
Denys Vlasenko000b6012012-01-28 01:25:03 +01002555 tprints("= ? <unavailable>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002556 line_ended();
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002557 tcp->flags &= ~TCB_INSYSCALL;
2558 return res;
2559 }
2560
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002561 sys_res = 0;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002562 if (tcp->qual_flg & QUAL_RAW) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002563 /* sys_res = printargs(tcp); - but it's nop on sysexit */
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002564 } else {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002565 /* FIXME: not_failing_only (IOW, option -z) is broken:
2566 * failure of syscall is known only after syscall return.
2567 * Thus we end up with something like this on, say, ENOENT:
2568 * open("doesnt_exist", O_RDONLY <unfinished ...>
2569 * {next syscall decode}
2570 * whereas the intended result is that open(...) line
2571 * is not shown at all.
2572 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002573 if (not_failing_only && tcp->u_error)
Denys Vlasenko3b738812011-08-22 02:06:35 +02002574 goto ret; /* ignore failed syscalls */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002575 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002576 }
2577
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002578 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002579 tabto();
Denys Vlasenko3b738812011-08-22 02:06:35 +02002580 u_error = tcp->u_error;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002581 if (tcp->qual_flg & QUAL_RAW) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002582 if (u_error)
2583 tprintf("= -1 (errno %ld)", u_error);
2584 else
2585 tprintf("= %#lx", tcp->u_rval);
2586 }
2587 else if (!(sys_res & RVAL_NONE) && u_error) {
2588 switch (u_error) {
Denys Vlasenkofe585652012-01-12 11:26:34 +01002589 /* Blocked signals do not interrupt any syscalls.
2590 * In this case syscalls don't return ERESTARTfoo codes.
2591 *
2592 * Deadly signals set to SIG_DFL interrupt syscalls
2593 * and kill the process regardless of which of the codes below
2594 * is returned by the interrupted syscall.
2595 * In some cases, kernel forces a kernel-generated deadly
2596 * signal to be unblocked and set to SIG_DFL (and thus cause
2597 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2598 * or SIGILL. (The alternative is to leave process spinning
2599 * forever on the faulty instruction - not useful).
2600 *
2601 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2602 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2603 * but kernel will always restart them.
2604 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002605 case ERESTARTSYS:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002606 /* Most common type of signal-interrupted syscall exit code.
2607 * The system call will be restarted with the same arguments
2608 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2609 */
2610 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002611 break;
2612 case ERESTARTNOINTR:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002613 /* Rare. For example, fork() returns this if interrupted.
2614 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2615 */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002616 tprints("= ? ERESTARTNOINTR (To be restarted)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002617 break;
2618 case ERESTARTNOHAND:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002619 /* pause(), rt_sigsuspend() etc use this code.
2620 * SA_RESTART is ignored (assumed not set):
2621 * syscall won't restart (will return EINTR instead)
Denys Vlasenko30c03232013-02-19 16:59:26 +01002622 * even after signal with SA_RESTART set. However,
2623 * after SIG_IGN or SIG_DFL signal it will restart
2624 * (thus the name "restart only if has no handler").
Denys Vlasenkofe585652012-01-12 11:26:34 +01002625 */
Denys Vlasenkoaba62922013-03-05 16:56:35 +01002626 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002627 break;
2628 case ERESTART_RESTARTBLOCK:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002629 /* Syscalls like nanosleep(), poll() which can't be
2630 * restarted with their original arguments use this
2631 * code. Kernel will execute restart_syscall() instead,
2632 * which changes arguments before restarting syscall.
2633 * SA_RESTART is ignored (assumed not set) similarly
2634 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2635 * since restart data is saved in "restart block"
2636 * in task struct, and if signal handler uses a syscall
2637 * which in turn saves another such restart block,
2638 * old data is lost and restart becomes impossible)
2639 */
2640 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002641 break;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002642 default:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002643 if (u_error < 0)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002644 tprintf("= -1 E??? (errno %ld)", u_error);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002645 else if (u_error < nerrnos)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002646 tprintf("= -1 %s (%s)", errnoent[u_error],
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002647 strerror(u_error));
2648 else
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002649 tprintf("= -1 ERRNO_%ld (%s)", u_error,
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002650 strerror(u_error));
2651 break;
2652 }
2653 if ((sys_res & RVAL_STR) && tcp->auxstr)
2654 tprintf(" (%s)", tcp->auxstr);
2655 }
2656 else {
2657 if (sys_res & RVAL_NONE)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002658 tprints("= ?");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002659 else {
2660 switch (sys_res & RVAL_MASK) {
2661 case RVAL_HEX:
2662 tprintf("= %#lx", tcp->u_rval);
2663 break;
2664 case RVAL_OCTAL:
2665 tprintf("= %#lo", tcp->u_rval);
2666 break;
2667 case RVAL_UDECIMAL:
2668 tprintf("= %lu", tcp->u_rval);
2669 break;
2670 case RVAL_DECIMAL:
2671 tprintf("= %ld", tcp->u_rval);
2672 break;
H.J. Ludd0130b2012-04-16 12:16:45 +02002673#if defined(LINUX_MIPSN32) || defined(X32)
2674 /*
2675 case RVAL_LHEX:
2676 tprintf("= %#llx", tcp->u_lrval);
2677 break;
2678 case RVAL_LOCTAL:
2679 tprintf("= %#llo", tcp->u_lrval);
2680 break;
2681 */
2682 case RVAL_LUDECIMAL:
2683 tprintf("= %llu", tcp->u_lrval);
2684 break;
2685 /*
2686 case RVAL_LDECIMAL:
2687 tprintf("= %lld", tcp->u_lrval);
2688 break;
2689 */
2690#endif
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002691 default:
2692 fprintf(stderr,
2693 "invalid rval format\n");
2694 break;
2695 }
2696 }
2697 if ((sys_res & RVAL_STR) && tcp->auxstr)
2698 tprintf(" (%s)", tcp->auxstr);
2699 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002700 if (Tflag) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002701 tv_sub(&tv, &tv, &tcp->etime);
2702 tprintf(" <%ld.%06ld>",
2703 (long) tv.tv_sec, (long) tv.tv_usec);
2704 }
Denys Vlasenko000b6012012-01-28 01:25:03 +01002705 tprints("\n");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002706 dumpio(tcp);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002707 line_ended();
2708
Denys Vlasenko3b738812011-08-22 02:06:35 +02002709 ret:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002710 tcp->flags &= ~TCB_INSYSCALL;
2711 return 0;
2712}
2713
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002714int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002715trace_syscall(struct tcb *tcp)
2716{
2717 return exiting(tcp) ?
2718 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2719}