blob: a0cc31279e189542b34d01046064b905d8dd53b3 [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>
Wichert Akkermanfaf72222000-02-19 23:59:03 +000040#elif defined(HAVE_LINUX_PTRACE_H)
Denys Vlasenko523635f2012-02-25 02:44:25 +010041# undef PTRACE_SYSCALL
Roland McGrathce9f0742004-03-01 21:29:22 +000042# ifdef HAVE_STRUCT_IA64_FPREG
43# define ia64_fpreg XXX_ia64_fpreg
44# endif
45# ifdef HAVE_STRUCT_PT_ALL_USER_REGS
46# define pt_all_user_regs XXX_pt_all_user_regs
47# endif
Ali Polatel0b4060f2013-09-24 20:04:32 +030048# ifdef HAVE_STRUCT_PTRACE_PEEKSIGINFO_ARGS
49# define ptrace_peeksiginfo_args XXX_ptrace_peeksiginfo_args
50# endif
Denys Vlasenko523635f2012-02-25 02:44:25 +010051# include <linux/ptrace.h>
Ali Polatel0b4060f2013-09-24 20:04:32 +030052# undef ptrace_peeksiginfo_args
Roland McGrathce9f0742004-03-01 21:29:22 +000053# undef ia64_fpreg
54# undef pt_all_user_regs
Wichert Akkerman15dea971999-10-06 13:06:34 +000055#endif
56
Denys Vlasenko84703742012-02-25 02:38:52 +010057#if defined(SPARC64)
Roland McGrath6d1a65c2004-07-12 07:44:08 +000058# undef PTRACE_GETREGS
59# define PTRACE_GETREGS PTRACE_GETREGS64
60# undef PTRACE_SETREGS
61# define PTRACE_SETREGS PTRACE_SETREGS64
Denys Vlasenko84703742012-02-25 02:38:52 +010062#endif
Roland McGrath6d1a65c2004-07-12 07:44:08 +000063
Denys Vlasenko84703742012-02-25 02:38:52 +010064#if defined(IA64)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000065# include <asm/ptrace_offsets.h>
66# include <asm/rse.h>
67#endif
68
Dmitry V. Levin32c049a2013-03-18 00:59:27 +000069/* for struct iovec */
70#include <sys/uio.h>
71/* for NT_PRSTATUS */
72#ifdef HAVE_ELF_H
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010073# include <elf.h>
74#endif
75
Steve McIntyred8d3bd32012-10-24 17:58:16 +010076#if defined(AARCH64)
77# include <asm/ptrace.h>
James Hogan5f999a82013-02-22 14:44:10 +000078#endif
79
Chris Zankel8f636ed2013-03-25 10:22:07 -070080#if defined(XTENSA)
81# include <asm/ptrace.h>
82#endif
83
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000084#ifndef NSIG
Denys Vlasenko523635f2012-02-25 02:44:25 +010085# warning: NSIG is not defined, using 32
86# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000087#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000088
89#include "syscall.h"
90
91/* Define these shorthand notations to simplify the syscallent files. */
Roland McGrath2fe7b132005-07-05 03:25:35 +000092#define TD TRACE_DESC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000093#define TF TRACE_FILE
94#define TI TRACE_IPC
95#define TN TRACE_NETWORK
96#define TP TRACE_PROCESS
97#define TS TRACE_SIGNAL
Namhyung Kim96792962012-10-24 11:41:57 +090098#define TM TRACE_MEMORY
Dmitry V. Levin50a218d2011-01-18 17:36:20 +000099#define NF SYSCALL_NEVER_FAILS
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200100#define MA MAX_ARGS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000101
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100102const struct_sysent sysent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000103#include "syscallent.h"
104};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000105
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100106#if SUPPORTED_PERSONALITIES > 1
107static const struct_sysent sysent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100108# include "syscallent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000109};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200110#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000111
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100112#if SUPPORTED_PERSONALITIES > 2
113static const struct_sysent sysent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100114# include "syscallent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000115};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200116#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000117
118/* Now undef them since short defines cause wicked namespace pollution. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000119#undef TD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000120#undef TF
121#undef TI
122#undef TN
123#undef TP
124#undef TS
Namhyung Kim96792962012-10-24 11:41:57 +0900125#undef TM
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000126#undef NF
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200127#undef MA
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000128
Denys Vlasenko39fca622011-08-20 02:12:33 +0200129/*
130 * `ioctlent.h' may be generated from `ioctlent.raw' by the auxiliary
131 * program `ioctlsort', such that the list is sorted by the `code' field.
132 * This has the side-effect of resolving the _IO.. macros into
133 * plain integers, eliminating the need to include here everything
134 * in "/usr/include".
135 */
136
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100137const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000138#include "errnoent.h"
139};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100140const char *const signalent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200141#include "signalent.h"
142};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100143const struct_ioctlent ioctlent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200144#include "ioctlent.h"
145};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000146
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100147#if SUPPORTED_PERSONALITIES > 1
Roland McGrathee36ce12004-09-04 03:53:10 +0000148static const char *const errnoent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100149# include "errnoent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000150};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200151static const char *const signalent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100152# include "signalent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200153};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100154static const struct_ioctlent ioctlent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100155# include "ioctlent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200156};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200157#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000158
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100159#if SUPPORTED_PERSONALITIES > 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000160static const char *const errnoent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100161# include "errnoent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000162};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200163static const char *const signalent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100164# include "signalent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200165};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100166static const struct_ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100167# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200168};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200169#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000170
Dmitry V. Levine6f55242013-02-26 22:02:30 +0000171enum {
172 nsyscalls0 = ARRAY_SIZE(sysent0)
173#if SUPPORTED_PERSONALITIES > 1
174 , nsyscalls1 = ARRAY_SIZE(sysent1)
175# if SUPPORTED_PERSONALITIES > 2
176 , nsyscalls2 = ARRAY_SIZE(sysent2)
177# endif
178#endif
179};
180
181enum {
182 nerrnos0 = ARRAY_SIZE(errnoent0)
183#if SUPPORTED_PERSONALITIES > 1
184 , nerrnos1 = ARRAY_SIZE(errnoent1)
185# if SUPPORTED_PERSONALITIES > 2
186 , nerrnos2 = ARRAY_SIZE(errnoent2)
187# endif
188#endif
189};
190
191enum {
192 nsignals0 = ARRAY_SIZE(signalent0)
193#if SUPPORTED_PERSONALITIES > 1
194 , nsignals1 = ARRAY_SIZE(signalent1)
195# if SUPPORTED_PERSONALITIES > 2
196 , nsignals2 = ARRAY_SIZE(signalent2)
197# endif
198#endif
199};
200
201enum {
202 nioctlents0 = ARRAY_SIZE(ioctlent0)
203#if SUPPORTED_PERSONALITIES > 1
204 , nioctlents1 = ARRAY_SIZE(ioctlent1)
205# if SUPPORTED_PERSONALITIES > 2
206 , nioctlents2 = ARRAY_SIZE(ioctlent2)
207# endif
208#endif
209};
210
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100211#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100212const struct_sysent *sysent = sysent0;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100213const char *const *errnoent = errnoent0;
214const char *const *signalent = signalent0;
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100215const struct_ioctlent *ioctlent = ioctlent0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100216#endif
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100217unsigned nsyscalls = nsyscalls0;
218unsigned nerrnos = nerrnos0;
219unsigned nsignals = nsignals0;
220unsigned nioctlents = nioctlents0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100221
222unsigned num_quals;
223qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
224
225static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
226 nsyscalls0,
227#if SUPPORTED_PERSONALITIES > 1
228 nsyscalls1,
229#endif
230#if SUPPORTED_PERSONALITIES > 2
231 nsyscalls2,
232#endif
233};
234static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
235 sysent0,
236#if SUPPORTED_PERSONALITIES > 1
237 sysent1,
238#endif
239#if SUPPORTED_PERSONALITIES > 2
240 sysent2,
241#endif
242};
243
244enum {
245 MAX_NSYSCALLS1 = (nsyscalls0
246#if SUPPORTED_PERSONALITIES > 1
247 > nsyscalls1 ? nsyscalls0 : nsyscalls1
248#endif
249 ),
250 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
251#if SUPPORTED_PERSONALITIES > 2
252 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
253#endif
254 ),
255 MAX_NSYSCALLS = MAX_NSYSCALLS2,
256 /* We are ready for arches with up to 255 signals,
257 * even though the largest known signo is on MIPS and it is 128.
258 * The number of existing syscalls on all arches is
259 * larger that 255 anyway, so it is just a pedantic matter.
260 */
261 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
262};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000263
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100264#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100265unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000266
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100267# ifndef current_wordsize
268unsigned current_wordsize;
269static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000270 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000271 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100272# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000273 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100274# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200275};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100276# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000277
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200278void
Dmitry V. Levin3abe8b22006-12-20 22:37:21 +0000279set_personality(int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000280{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100281 nsyscalls = nsyscall_vec[personality];
282 sysent = sysent_vec[personality];
283
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000284 switch (personality) {
285 case 0:
286 errnoent = errnoent0;
287 nerrnos = nerrnos0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000288 ioctlent = ioctlent0;
289 nioctlents = nioctlents0;
290 signalent = signalent0;
291 nsignals = nsignals0;
292 break;
293
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000294 case 1:
295 errnoent = errnoent1;
296 nerrnos = nerrnos1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000297 ioctlent = ioctlent1;
298 nioctlents = nioctlents1;
299 signalent = signalent1;
300 nsignals = nsignals1;
301 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000302
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100303# if SUPPORTED_PERSONALITIES > 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000304 case 2:
305 errnoent = errnoent2;
306 nerrnos = nerrnos2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000307 ioctlent = ioctlent2;
308 nioctlents = nioctlents2;
309 signalent = signalent2;
310 nsignals = nsignals2;
311 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100312# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000313 }
314
315 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100316# ifndef current_wordsize
317 current_wordsize = personality_wordsize[personality];
318# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000319}
320
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000321static void
322update_personality(struct tcb *tcp, int personality)
323{
324 if (personality == current_personality)
325 return;
326 set_personality(personality);
327
328 if (personality == tcp->currpers)
329 return;
330 tcp->currpers = personality;
331
H.J. Lu35be5812012-04-16 13:00:01 +0200332# if defined(POWERPC64)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000333 if (!qflag) {
334 static const char *const names[] = {"64 bit", "32 bit"};
335 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
336 tcp->pid, names[personality]);
337 }
H.J. Lu35be5812012-04-16 13:00:01 +0200338# elif defined(X86_64)
339 if (!qflag) {
340 static const char *const names[] = {"64 bit", "32 bit", "x32"};
341 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
342 tcp->pid, names[personality]);
343 }
H.J. Lu085e4282012-04-17 11:05:04 -0700344# elif defined(X32)
345 if (!qflag) {
346 static const char *const names[] = {"x32", "32 bit"};
347 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
348 tcp->pid, names[personality]);
349 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000350# elif defined(AARCH64)
351 if (!qflag) {
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100352 static const char *const names[] = {"32-bit", "AArch64"};
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000353 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
354 tcp->pid, names[personality]);
355 }
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100356# elif defined(TILE)
357 if (!qflag) {
358 static const char *const names[] = {"64-bit", "32-bit"};
359 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
360 tcp->pid, names[personality]);
361 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100362# endif
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000363}
364#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000365
Denys Vlasenkoc1540fe2013-02-21 16:17:08 +0100366static int qual_syscall(), qual_signal(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000367
Roland McGrathe10e62a2004-09-04 04:20:43 +0000368static const struct qual_options {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000369 int bitflag;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000370 const char *option_name;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000371 int (*qualify)(const char *, int, int);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000372 const char *argument_name;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000373} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000374 { QUAL_TRACE, "trace", qual_syscall, "system call" },
375 { QUAL_TRACE, "t", qual_syscall, "system call" },
376 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
377 { QUAL_ABBREV, "a", qual_syscall, "system call" },
378 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
379 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
380 { QUAL_RAW, "raw", qual_syscall, "system call" },
381 { QUAL_RAW, "x", qual_syscall, "system call" },
382 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
383 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
384 { QUAL_SIGNAL, "s", qual_signal, "signal" },
Roland McGrath9797ceb2002-12-30 10:23:00 +0000385 { QUAL_READ, "read", qual_desc, "descriptor" },
386 { QUAL_READ, "reads", qual_desc, "descriptor" },
387 { QUAL_READ, "r", qual_desc, "descriptor" },
388 { QUAL_WRITE, "write", qual_desc, "descriptor" },
389 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
390 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000391 { 0, NULL, NULL, NULL },
392};
393
Roland McGrath9797ceb2002-12-30 10:23:00 +0000394static void
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100395reallocate_qual(int n)
396{
397 unsigned p;
398 qualbits_t *qp;
399 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
400 qp = qual_vec[p] = realloc(qual_vec[p], n * sizeof(qualbits_t));
401 if (!qp)
402 die_out_of_memory();
403 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
404 }
405 num_quals = n;
406}
407
408static void
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000409qualify_one(int n, int bitflag, int not, int pers)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000410{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100411 unsigned p;
Roland McGrath138c6a32006-01-12 09:50:49 +0000412
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100413 if (num_quals <= n)
414 reallocate_qual(n + 1);
Roland McGrath138c6a32006-01-12 09:50:49 +0000415
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100416 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
417 if (pers == p || pers < 0) {
418 if (not)
419 qual_vec[p][n] &= ~bitflag;
420 else
421 qual_vec[p][n] |= bitflag;
422 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000423 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000424}
425
426static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000427qual_syscall(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000428{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100429 unsigned p;
430 unsigned i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000431 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000432
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100433 if (*s >= '0' && *s <= '9') {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100434 i = string_to_uint(s);
Denys Vlasenkob43dacd2013-02-23 18:19:28 +0100435 if (i >= MAX_NSYSCALLS)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000436 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000437 qualify_one(i, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000438 return 0;
Roland McGrath48a035f2006-01-12 09:45:56 +0000439 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000440
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100441 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
442 for (i = 0; i < nsyscall_vec[p]; i++) {
443 if (sysent_vec[p][i].sys_name
444 && strcmp(s, sysent_vec[p][i].sys_name) == 0
445 ) {
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000446 qualify_one(i, bitflag, not, p);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100447 rc = 0;
448 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000449 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100450 }
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000451
Roland McGrathfe6b3522005-02-02 04:40:11 +0000452 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000453}
454
455static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000456qual_signal(const char *s, int bitflag, int not)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000457{
458 int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000459
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100460 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000461 int signo = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100462 if (signo < 0 || signo > 255)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000463 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000464 qualify_one(signo, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000465 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000466 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000467 if (strncasecmp(s, "SIG", 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000468 s += 3;
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100469 for (i = 0; i <= NSIG; i++) {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000470 if (strcasecmp(s, signame(i) + 3) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000471 qualify_one(i, bitflag, not, -1);
Roland McGrath76421df2005-02-02 03:51:18 +0000472 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000473 }
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100474 }
Roland McGrath76421df2005-02-02 03:51:18 +0000475 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000476}
477
478static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000479qual_desc(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000480{
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100481 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000482 int desc = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100483 if (desc < 0 || desc > 0x7fff) /* paranoia */
Roland McGrathfe6b3522005-02-02 04:40:11 +0000484 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000485 qualify_one(desc, bitflag, not, -1);
Roland McGrath2b619022003-04-10 18:58:20 +0000486 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000487 }
488 return -1;
489}
490
491static int
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000492lookup_class(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000493{
494 if (strcmp(s, "file") == 0)
495 return TRACE_FILE;
496 if (strcmp(s, "ipc") == 0)
497 return TRACE_IPC;
498 if (strcmp(s, "network") == 0)
499 return TRACE_NETWORK;
500 if (strcmp(s, "process") == 0)
501 return TRACE_PROCESS;
502 if (strcmp(s, "signal") == 0)
503 return TRACE_SIGNAL;
Roland McGrath2fe7b132005-07-05 03:25:35 +0000504 if (strcmp(s, "desc") == 0)
505 return TRACE_DESC;
Namhyung Kim96792962012-10-24 11:41:57 +0900506 if (strcmp(s, "memory") == 0)
507 return TRACE_MEMORY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000508 return -1;
509}
510
511void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000512qualify(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000513{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000514 const struct qual_options *opt;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000515 int not;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000516 char *copy;
517 const char *p;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000518 int i, n;
519
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100520 if (num_quals == 0)
521 reallocate_qual(MIN_QUALS);
522
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000523 opt = &qual_options[0];
524 for (i = 0; (p = qual_options[i].option_name); i++) {
525 n = strlen(p);
526 if (strncmp(s, p, n) == 0 && s[n] == '=') {
527 opt = &qual_options[i];
528 s += n + 1;
529 break;
530 }
531 }
532 not = 0;
533 if (*s == '!') {
534 not = 1;
535 s++;
536 }
537 if (strcmp(s, "none") == 0) {
538 not = 1 - not;
539 s = "all";
540 }
541 if (strcmp(s, "all") == 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100542 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000543 qualify_one(i, opt->bitflag, not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000544 }
545 return;
546 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100547 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000548 qualify_one(i, opt->bitflag, !not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000549 }
Denys Vlasenko5d645812011-08-20 12:48:18 +0200550 copy = strdup(s);
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200551 if (!copy)
552 die_out_of_memory();
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000553 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000554 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100555 unsigned pers;
556 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
557 for (i = 0; i < nsyscall_vec[pers]; i++)
558 if (sysent_vec[pers][i].sys_flags & n)
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000559 qualify_one(i, opt->bitflag, not, pers);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100560 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000561 continue;
562 }
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000563 if (opt->qualify(p, opt->bitflag, not)) {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100564 error_msg_and_die("invalid %s '%s'",
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000565 opt->argument_name, p);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000566 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000567 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000568 free(copy);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000569 return;
570}
571
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000572#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000573static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000574decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000575{
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000576 unsigned long addr;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100577 unsigned int i, n, size;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000578
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000579 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
580 return;
581
582 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100583 tcp->qual_flg = qual_flags[tcp->scno];
584 tcp->s_ent = &sysent[tcp->scno];
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000585 addr = tcp->u_arg[1];
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100586 size = current_wordsize;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100587 n = tcp->s_ent->nargs;
588 for (i = 0; i < n; ++i) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000589 if (size == sizeof(int)) {
590 unsigned int arg;
591 if (umove(tcp, addr, &arg) < 0)
592 arg = 0;
593 tcp->u_arg[i] = arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000594 }
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000595 else {
596 unsigned long arg;
597 if (umove(tcp, addr, &arg) < 0)
598 arg = 0;
599 tcp->u_arg[i] = arg;
600 }
601 addr += size;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000602 }
603}
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000604#endif
Mike Frysinger3362e892012-03-15 01:09:19 -0400605
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000606#ifdef SYS_ipc_subcall
607static void
608decode_ipc_subcall(struct tcb *tcp)
609{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100610 unsigned int i, n;
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000611
612 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
613 return;
614
615 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100616 tcp->qual_flg = qual_flags[tcp->scno];
617 tcp->s_ent = &sysent[tcp->scno];
618 n = tcp->s_ent->nargs;
619 for (i = 0; i < n; i++)
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000620 tcp->u_arg[i] = tcp->u_arg[i + 1];
621}
622#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000623
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200624int
625printargs(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000626{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200627 if (entering(tcp)) {
628 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100629 int n = tcp->s_ent->nargs;
630 for (i = 0; i < n; i++)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200631 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
632 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000633 return 0;
634}
635
Denys Vlasenko72879c62012-02-27 14:18:02 +0100636int
637printargs_lu(struct tcb *tcp)
638{
639 if (entering(tcp)) {
640 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100641 int n = tcp->s_ent->nargs;
642 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100643 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
644 }
645 return 0;
646}
647
648int
649printargs_ld(struct tcb *tcp)
650{
651 if (entering(tcp)) {
652 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100653 int n = tcp->s_ent->nargs;
654 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100655 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
656 }
657 return 0;
658}
659
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100660#if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200661long
662getrval2(struct tcb *tcp)
663{
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100664 long val;
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200665
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100666# if defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100667 val = sparc_regs.u_regs[U_REG_O1];
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100668# elif defined(SH)
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200669 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200670 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100671# elif defined(IA64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200672 if (upeek(tcp->pid, PT_R9, &val) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200673 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100674# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200675
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200676 return val;
677}
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100678#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200679
Denys Vlasenko523635f2012-02-25 02:44:25 +0100680#if defined(I386)
Denys Vlasenkob51f3642013-07-16 12:06:25 +0200681static struct user_regs_struct i386_regs;
Denys Vlasenkoe600ac62013-07-18 17:40:45 +0200682/* Cast suppresses signedness warning (.esp is long, not unsigned long) */
683uint32_t *const i386_esp_ptr = (uint32_t*)&i386_regs.esp;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000684# define ARCH_REGS_FOR_GETREGSET i386_regs
H.J. Lu35be5812012-04-16 13:00:01 +0200685#elif defined(X86_64) || defined(X32)
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100686/*
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100687 * On i386, pt_regs and user_regs_struct are the same,
688 * but on 64 bit x86, user_regs_struct has six more fields:
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100689 * fs_base, gs_base, ds, es, fs, gs.
690 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
691 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100692struct i386_user_regs_struct {
693 uint32_t ebx;
694 uint32_t ecx;
695 uint32_t edx;
696 uint32_t esi;
697 uint32_t edi;
698 uint32_t ebp;
699 uint32_t eax;
700 uint32_t xds;
701 uint32_t xes;
702 uint32_t xfs;
703 uint32_t xgs;
704 uint32_t orig_eax;
705 uint32_t eip;
706 uint32_t xcs;
707 uint32_t eflags;
708 uint32_t esp;
709 uint32_t xss;
710};
711static union {
712 struct user_regs_struct x86_64_r;
713 struct i386_user_regs_struct i386_r;
714} x86_regs_union;
715# define x86_64_regs x86_regs_union.x86_64_r
716# define i386_regs x86_regs_union.i386_r
Denys Vlasenkob51f3642013-07-16 12:06:25 +0200717uint32_t *const i386_esp_ptr = &i386_regs.esp;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100718static struct iovec x86_io = {
719 .iov_base = &x86_regs_union
720};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100721#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +0200722bool ia64_ia32mode = 0; /* not static */
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100723static long ia64_r8, ia64_r10;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100724#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +0200725struct pt_regs ppc_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100726#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100727static long m68k_d0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100728#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100729static long bfin_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100730#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100731struct pt_regs arm_regs; /* not static */
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000732# define ARCH_REGS_FOR_GETREGSET arm_regs
Steve McIntyred8d3bd32012-10-24 17:58:16 +0100733#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100734static union {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100735 struct user_pt_regs aarch64_r;
736 struct arm_pt_regs arm_r;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100737} arm_regs_union;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100738# define aarch64_regs arm_regs_union.aarch64_r
739# define arm_regs arm_regs_union.arm_r
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100740static struct iovec aarch64_io = {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100741 .iov_base = &arm_regs_union
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100742};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100743#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100744static long alpha_r0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100745static long alpha_a3;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100746#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100747static struct pt_regs avr32_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100748#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100749struct pt_regs sparc_regs; /* not static */
Denys Vlasenko523635f2012-02-25 02:44:25 +0100750#elif defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100751static long long mips_a3;
752static long long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100753#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100754static long mips_a3;
755static long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100756#elif defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +0200757static long s390_gpr2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100758#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100759static long hppa_r28;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100760#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100761static long sh_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100762#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100763static long sh64_r9;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100764#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100765static long cris_r10;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100766#elif defined(TILE)
767struct pt_regs tile_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100768#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100769static long microblaze_r3;
Christian Svensson492f81f2013-02-14 13:26:27 +0100770#elif defined(OR1K)
771static struct user_regs_struct or1k_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000772# define ARCH_REGS_FOR_GETREGSET or1k_regs
James Hogan5f999a82013-02-22 14:44:10 +0000773#elif defined(METAG)
774static struct user_gp_regs metag_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000775# define ARCH_REGS_FOR_GETREGSET metag_regs
Chris Zankel8f636ed2013-03-25 10:22:07 -0700776#elif defined(XTENSA)
777static long xtensa_a2;
Vineet Gupta7daacbb2013-08-16 12:47:06 +0530778# elif defined(ARC)
779static struct user_regs_struct arc_regs;
780# define ARCH_REGS_FOR_GETREGSET arc_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +0100781#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000782
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100783void
Denys Vlasenko5a2483b2013-07-01 12:49:14 +0200784print_pc(struct tcb *tcp)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100785{
786#define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
787 sizeof(long) == 8 ? "[????????????????] " : \
788 NULL /* crash */)
789 if (get_regs_error) {
790 PRINTBADPC;
791 return;
792 }
793#if defined(I386)
794 tprintf("[%08lx] ", i386_regs.eip);
795#elif defined(S390) || defined(S390X)
796 long psw;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200797 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100798 PRINTBADPC;
799 return;
800 }
801# ifdef S390
802 tprintf("[%08lx] ", psw);
803# elif S390X
Dmitry V. Levinddba73e2013-02-05 19:01:58 +0000804 tprintf("[%016lx] ", psw);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100805# endif
806#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100807 if (x86_io.iov_len == sizeof(i386_regs)) {
808 tprintf("[%08x] ", (unsigned) i386_regs.eip);
809 } else {
810# if defined(X86_64)
811 tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
812# elif defined(X32)
813 /* Note: this truncates 64-bit rip to 32 bits */
814 tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip);
815# endif
816 }
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100817#elif defined(IA64)
818 long ip;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200819 if (upeek(tcp->pid, PT_B0, &ip) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100820 PRINTBADPC;
821 return;
822 }
823 tprintf("[%08lx] ", ip);
824#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +0200825 long pc = ppc_regs.nip;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100826# ifdef POWERPC64
827 tprintf("[%016lx] ", pc);
828# else
829 tprintf("[%08lx] ", pc);
830# endif
831#elif defined(M68K)
832 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200833 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100834 tprints("[????????] ");
835 return;
836 }
837 tprintf("[%08lx] ", pc);
838#elif defined(ALPHA)
839 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200840 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100841 tprints("[????????????????] ");
842 return;
843 }
844 tprintf("[%08lx] ", pc);
845#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100846 tprintf("[%08lx] ", sparc_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100847#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100848 tprintf("[%08lx] ", sparc_regs.tpc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100849#elif defined(HPPA)
850 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200851 if (upeek(tcp->pid, PT_IAOQ0, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100852 tprints("[????????] ");
853 return;
854 }
855 tprintf("[%08lx] ", pc);
856#elif defined(MIPS)
857 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200858 if (upeek(tcp->pid, REG_EPC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100859 tprints("[????????] ");
860 return;
861 }
862 tprintf("[%08lx] ", pc);
863#elif defined(SH)
864 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200865 if (upeek(tcp->pid, 4*REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100866 tprints("[????????] ");
867 return;
868 }
869 tprintf("[%08lx] ", pc);
870#elif defined(SH64)
871 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200872 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100873 tprints("[????????????????] ");
874 return;
875 }
876 tprintf("[%08lx] ", pc);
877#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100878 tprintf("[%08lx] ", arm_regs.ARM_pc);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100879#elif defined(AARCH64)
880 /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100881#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100882 tprintf("[%08lx] ", avr32_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100883#elif defined(BFIN)
884 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200885 if (upeek(tcp->pid, PT_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100886 PRINTBADPC;
887 return;
888 }
889 tprintf("[%08lx] ", pc);
890#elif defined(CRISV10)
891 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200892 if (upeek(tcp->pid, 4*PT_IRP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100893 PRINTBADPC;
894 return;
895 }
896 tprintf("[%08lx] ", pc);
897#elif defined(CRISV32)
898 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200899 if (upeek(tcp->pid, 4*PT_ERP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100900 PRINTBADPC;
901 return;
902 }
903 tprintf("[%08lx] ", pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100904#elif defined(TILE)
905# ifdef _LP64
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500906 tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100907# else
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500908 tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100909# endif
Christian Svensson492f81f2013-02-14 13:26:27 +0100910#elif defined(OR1K)
911 tprintf("[%08lx] ", or1k_regs.pc);
James Hogan5f999a82013-02-22 14:44:10 +0000912#elif defined(METAG)
913 tprintf("[%08lx] ", metag_regs.pc);
Chris Zankel8f636ed2013-03-25 10:22:07 -0700914#elif defined(XTENSA)
915 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200916 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Chris Zankel8f636ed2013-03-25 10:22:07 -0700917 PRINTBADPC;
918 return;
919 }
920 tprintf("[%08lx] ", pc);
Vineet Gupta7daacbb2013-08-16 12:47:06 +0530921#elif defined(ARC)
922 tprintf("[%08lx] ", arc_regs.efa);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100923#endif /* architecture */
924}
925
Denys Vlasenko7270de52013-02-21 15:46:34 +0100926/* Shuffle syscall numbers so that we don't have huge gaps in syscall table.
927 * The shuffling should be reversible: shuffle_scno(shuffle_scno(n)) == n.
928 */
929#if defined(ARM) /* So far only ARM needs this */
930static long
931shuffle_scno(unsigned long scno)
932{
933 if (scno <= ARM_LAST_ORDINARY_SYSCALL)
934 return scno;
935
936 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
937 if (scno == 0x000ffff0)
938 return ARM_LAST_ORDINARY_SYSCALL+1;
939 if (scno == ARM_LAST_ORDINARY_SYSCALL+1)
940 return 0x000ffff0;
941
942 /* Is it ARM specific syscall?
943 * Swap with [LAST_ORDINARY+2, LAST_ORDINARY+2 + LAST_SPECIAL] range.
944 */
945 if (scno >= 0x000f0000
946 && scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL
947 ) {
948 return scno - 0x000f0000 + (ARM_LAST_ORDINARY_SYSCALL+2);
949 }
950 if (/* scno >= ARM_LAST_ORDINARY_SYSCALL+2 - always true */ 1
951 && scno <= (ARM_LAST_ORDINARY_SYSCALL+2) + ARM_LAST_SPECIAL_SYSCALL
952 ) {
953 return scno + 0x000f0000 - (ARM_LAST_ORDINARY_SYSCALL+2);
954 }
955
956 return scno;
957}
958#else
959# define shuffle_scno(scno) ((long)(scno))
960#endif
961
962static char*
963undefined_scno_name(struct tcb *tcp)
964{
965 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
966
967 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
968 return buf;
969}
970
Anton Blanchard14d51a62013-06-26 14:42:37 +1000971#ifdef POWERPC
972/*
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200973 * PTRACE_GETREGS was added to the PowerPC kernel in v2.6.23,
974 * we provide a slow fallback for old kernels.
Anton Blanchard14d51a62013-06-26 14:42:37 +1000975 */
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200976static int powerpc_getregs_old(pid_t pid)
Anton Blanchard14d51a62013-06-26 14:42:37 +1000977{
978 int i;
979 long r;
980
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200981 if (iflag) {
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200982 r = upeek(pid, sizeof(long) * PT_NIP, (long *)&ppc_regs.nip);
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200983 if (r)
984 goto out;
985 }
986#ifdef POWERPC64 /* else we never use it */
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200987 r = upeek(pid, sizeof(long) * PT_MSR, (long *)&ppc_regs.msr);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000988 if (r)
989 goto out;
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200990#endif
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200991 r = upeek(pid, sizeof(long) * PT_CCR, (long *)&ppc_regs.ccr);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000992 if (r)
993 goto out;
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200994 r = upeek(pid, sizeof(long) * PT_ORIG_R3, (long *)&ppc_regs.orig_gpr3);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000995 if (r)
996 goto out;
Anton Blanchard14d51a62013-06-26 14:42:37 +1000997 for (i = 0; i <= 8; i++) {
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200998 r = upeek(pid, sizeof(long) * (PT_R0 + i),
999 (long *)&ppc_regs.gpr[i]);
Anton Blanchard14d51a62013-06-26 14:42:37 +10001000 if (r)
1001 goto out;
1002 }
Denys Vlasenko7f5a1322013-06-28 14:36:39 +02001003 out:
Anton Blanchard14d51a62013-06-26 14:42:37 +10001004 return r;
1005}
1006#endif
1007
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001008#ifndef get_regs
1009long get_regs_error;
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001010
1011#if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001012static void get_regset(pid_t pid)
1013{
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001014/* constant iovec */
1015# if defined(ARM) \
1016 || defined(I386) \
1017 || defined(METAG) \
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301018 || defined(OR1K) \
1019 || defined(ARC)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001020 static struct iovec io = {
1021 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1022 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001023 };
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001024 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1025
1026/* variable iovec */
1027# elif defined(X86_64) || defined(X32)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001028 /* x86_io.iov_base = &x86_regs_union; - already is */
1029 x86_io.iov_len = sizeof(x86_regs_union);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001030 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &x86_io);
1031# elif defined(AARCH64)
1032 /* aarch64_io.iov_base = &arm_regs_union; - already is */
1033 aarch64_io.iov_len = sizeof(arm_regs_union);
1034 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &aarch64_io);
1035# else
1036# warning both PTRACE_GETREGSET and NT_PRSTATUS are available but not yet used
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001037# endif
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001038}
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001039#endif /* PTRACE_GETREGSET && NT_PRSTATUS */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001040
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001041void
1042get_regs(pid_t pid)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001043{
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001044/* PTRACE_GETREGSET only */
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301045# if defined(METAG) || defined(OR1K) || defined(X32) || defined(AARCH64) || defined(ARC)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001046 get_regset(pid);
1047
1048/* PTRACE_GETREGS only */
1049# elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001050 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001051# elif defined(TILE)
1052 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &tile_regs);
1053# elif defined(SPARC) || defined(SPARC64)
1054 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001055# elif defined(POWERPC)
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001056 static bool old_kernel = 0;
1057 if (old_kernel)
1058 goto old;
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001059 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &ppc_regs);
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001060 if (get_regs_error && errno == EIO) {
1061 old_kernel = 1;
1062 old:
Anton Blanchard14d51a62013-06-26 14:42:37 +10001063 get_regs_error = powerpc_getregs_old(pid);
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001064 }
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001065
1066/* try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS */
1067# else
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001068# if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
1069 static int getregset_support;
1070
1071 if (getregset_support >= 0) {
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001072 get_regset(pid);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001073 if (getregset_support > 0)
1074 return;
1075 if (get_regs_error >= 0) {
1076 getregset_support = 1;
1077 return;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001078 }
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001079 if (errno == EPERM || errno == ESRCH)
1080 return;
1081 getregset_support = -1;
1082 }
1083# endif /* PTRACE_GETREGSET && NT_PRSTATUS */
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001084# if defined(ARM)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001085 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &arm_regs);
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001086# elif defined(I386)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001087 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &i386_regs);
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001088# elif defined(X86_64)
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001089 /* Use old method, with unreliable heuristical detection of 32-bitness. */
1090 x86_io.iov_len = sizeof(x86_64_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001091 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &x86_64_regs);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001092 if (!get_regs_error && x86_64_regs.cs == 0x23) {
1093 x86_io.iov_len = sizeof(i386_regs);
1094 /*
1095 * The order is important: i386_regs and x86_64_regs
1096 * are overlaid in memory!
1097 */
1098 i386_regs.ebx = x86_64_regs.rbx;
1099 i386_regs.ecx = x86_64_regs.rcx;
1100 i386_regs.edx = x86_64_regs.rdx;
1101 i386_regs.esi = x86_64_regs.rsi;
1102 i386_regs.edi = x86_64_regs.rdi;
1103 i386_regs.ebp = x86_64_regs.rbp;
1104 i386_regs.eax = x86_64_regs.rax;
1105 /* i386_regs.xds = x86_64_regs.ds; unused by strace */
1106 /* i386_regs.xes = x86_64_regs.es; ditto... */
1107 /* i386_regs.xfs = x86_64_regs.fs; */
1108 /* i386_regs.xgs = x86_64_regs.gs; */
1109 i386_regs.orig_eax = x86_64_regs.orig_rax;
1110 i386_regs.eip = x86_64_regs.rip;
1111 /* i386_regs.xcs = x86_64_regs.cs; */
1112 /* i386_regs.eflags = x86_64_regs.eflags; */
1113 i386_regs.esp = x86_64_regs.rsp;
1114 /* i386_regs.xss = x86_64_regs.ss; */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001115 }
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001116# else
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001117# error unhandled architecture
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001118# endif /* ARM || I386 || X86_64 */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001119# endif
1120}
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001121#endif /* !get_regs */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001122
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001123/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001124 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1125 * 1: ok, continue in trace_syscall_entering().
1126 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001127 * ("????" etc) and bail out.
1128 */
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01001129static int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001130get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001131{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001132 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001133
Denys Vlasenko523635f2012-02-25 02:44:25 +01001134#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001135 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001136 return -1;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001137
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001138 if (s390_gpr2 != -ENOSYS) {
Roland McGrath2f924ca2003-06-26 22:23:28 +00001139 /*
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001140 * Since kernel version 2.5.44 the scno gets passed in gpr2.
Roland McGrath2f924ca2003-06-26 22:23:28 +00001141 */
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001142 scno = s390_gpr2;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001143 } else {
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001144 /*
Michal Ludvig882eda82002-11-11 12:50:47 +00001145 * Old style of "passing" the scno via the SVC instruction.
1146 */
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001147 long psw;
Michal Ludvig882eda82002-11-11 12:50:47 +00001148 long opcode, offset_reg, tmp;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001149 void *svc_addr;
Denys Vlasenko7c9ba8b2011-08-19 19:46:32 +02001150 static const int gpr_offset[16] = {
1151 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
1152 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
1153 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
1154 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
1155 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001156
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001157 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0)
Michal Ludvig882eda82002-11-11 12:50:47 +00001158 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001159 errno = 0;
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001160 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +00001161 if (errno) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001162 perror_msg("peektext(psw-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +00001163 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001164 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001165
1166 /*
1167 * We have to check if the SVC got executed directly or via an
1168 * EXECUTE instruction. In case of EXECUTE it is necessary to do
1169 * instruction decoding to derive the system call number.
1170 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1171 * so that this doesn't work if a SVC opcode is part of an EXECUTE
1172 * opcode. Since there is no way to find out the opcode size this
1173 * is the best we can do...
1174 */
Michal Ludvig882eda82002-11-11 12:50:47 +00001175 if ((opcode & 0xff00) == 0x0a00) {
1176 /* SVC opcode */
1177 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +00001178 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001179 else {
1180 /* SVC got executed by EXECUTE instruction */
1181
1182 /*
1183 * Do instruction decoding of EXECUTE. If you really want to
1184 * understand this, read the Principles of Operations.
1185 */
1186 svc_addr = (void *) (opcode & 0xfff);
1187
1188 tmp = 0;
1189 offset_reg = (opcode & 0x000f0000) >> 16;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001190 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001191 return -1;
1192 svc_addr += tmp;
1193
1194 tmp = 0;
1195 offset_reg = (opcode & 0x0000f000) >> 12;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001196 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001197 return -1;
1198 svc_addr += tmp;
1199
Denys Vlasenkofb036672009-01-23 16:30:26 +00001200 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
Michal Ludvig882eda82002-11-11 12:50:47 +00001201 if (errno)
1202 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001203# if defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +00001204 scno >>= 48;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001205# else
Michal Ludvig882eda82002-11-11 12:50:47 +00001206 scno >>= 16;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001207# endif
Michal Ludvig882eda82002-11-11 12:50:47 +00001208 tmp = 0;
1209 offset_reg = (opcode & 0x00f00000) >> 20;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001210 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001211 return -1;
1212
1213 scno = (scno | tmp) & 0xff;
1214 }
1215 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001216#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001217 scno = ppc_regs.gpr[0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001218# ifdef POWERPC64
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001219 int currpers;
Andreas Schwabd69fa492010-07-12 21:39:57 +02001220
James Yanga78d7772014-02-18 15:32:43 -06001221 /*
1222 * Check for 64/32 bit mode.
1223 * Embedded implementations covered by Book E extension of PPC use
1224 * bit 0 (CM) of 32-bit Machine state register (MSR).
1225 * Other implementations use bit 0 (SF) of 64-bit MSR.
1226 */
1227 currpers = (ppc_regs.msr & 0x8000000080000000) ? 0 : 1;
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001228 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001229# endif
1230#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001231 scno = avr32_regs.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001232#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001233 if (upeek(tcp->pid, PT_ORIG_P0, &scno))
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001234 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001235#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001236 scno = i386_regs.orig_eax;
H.J. Lu35be5812012-04-16 13:00:01 +02001237#elif defined(X86_64) || defined(X32)
1238# ifndef __X32_SYSCALL_BIT
1239# define __X32_SYSCALL_BIT 0x40000000
1240# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001241 int currpers;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001242# if 1
1243 /* GETREGSET of NT_PRSTATUS tells us regset size,
1244 * which unambiguously detects i386.
1245 *
1246 * Linux kernel distinguishes x86-64 and x32 processes
1247 * solely by looking at __X32_SYSCALL_BIT:
1248 * arch/x86/include/asm/compat.h::is_x32_task():
1249 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1250 * return true;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001251 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001252 if (x86_io.iov_len == sizeof(i386_regs)) {
1253 scno = i386_regs.orig_eax;
1254 currpers = 1;
1255 } else {
1256 scno = x86_64_regs.orig_rax;
1257 currpers = 0;
1258 if (scno & __X32_SYSCALL_BIT) {
1259 scno -= __X32_SYSCALL_BIT;
1260 currpers = 2;
1261 }
1262 }
1263# elif 0
1264 /* cs = 0x33 for long mode (native 64 bit and x32)
1265 * cs = 0x23 for compatibility mode (32 bit)
1266 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1267 */
1268 scno = x86_64_regs.orig_rax;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001269 switch (x86_64_regs.cs) {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001270 case 0x23: currpers = 1; break;
H.J. Lu35be5812012-04-16 13:00:01 +02001271 case 0x33:
1272 if (x86_64_regs.ds == 0x2b) {
1273 currpers = 2;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001274 scno &= ~__X32_SYSCALL_BIT;
H.J. Lu35be5812012-04-16 13:00:01 +02001275 } else
1276 currpers = 0;
1277 break;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001278 default:
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001279 fprintf(stderr, "Unknown value CS=0x%08X while "
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001280 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001281 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001282 currpers = current_personality;
1283 break;
1284 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001285# elif 0
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001286 /* This version analyzes the opcode of a syscall instruction.
1287 * (int 0x80 on i386 vs. syscall on x86-64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001288 * It works, but is too complicated, and strictly speaking, unreliable.
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001289 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001290 unsigned long call, rip = x86_64_regs.rip;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001291 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1292 rip -= 2;
1293 errno = 0;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001294 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001295 if (errno)
1296 fprintf(stderr, "ptrace_peektext failed: %s\n",
1297 strerror(errno));
1298 switch (call & 0xffff) {
1299 /* x86-64: syscall = 0x0f 0x05 */
1300 case 0x050f: currpers = 0; break;
1301 /* i386: int 0x80 = 0xcd 0x80 */
1302 case 0x80cd: currpers = 1; break;
1303 default:
1304 currpers = current_personality;
1305 fprintf(stderr,
1306 "Unknown syscall opcode (0x%04X) while "
1307 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001308 "PID=%d\n", (int)call, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001309 break;
1310 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001311# endif
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001312
H.J. Lu35be5812012-04-16 13:00:01 +02001313# ifdef X32
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001314 /* If we are built for a x32 system, then personality 0 is x32
1315 * (not x86_64), and stracing of x86_64 apps is not supported.
1316 * Stracing of i386 apps is still supported.
H.J. Lu085e4282012-04-17 11:05:04 -07001317 */
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001318 if (currpers == 0) {
1319 fprintf(stderr, "syscall_%lu(...) in unsupported "
1320 "64-bit mode of process PID=%d\n",
1321 scno, tcp->pid);
1322 return 0;
H.J. Lu35be5812012-04-16 13:00:01 +02001323 }
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001324 currpers &= ~2; /* map 2,1 to 0,1 */
H.J. Lu35be5812012-04-16 13:00:01 +02001325# endif
H.J. Lu085e4282012-04-17 11:05:04 -07001326 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001327#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001328# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01001329 long psr;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001330 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001331 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
1332 if (ia64_ia32mode) {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001333 if (upeek(tcp->pid, PT_R1, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001334 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001335 } else {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001336 if (upeek(tcp->pid, PT_R15, &scno) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001337 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001338 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001339#elif defined(AARCH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001340 switch (aarch64_io.iov_len) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001341 case sizeof(aarch64_regs):
1342 /* We are in 64-bit mode */
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001343 scno = aarch64_regs.regs[8];
1344 update_personality(tcp, 1);
1345 break;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001346 case sizeof(arm_regs):
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001347 /* We are in 32-bit mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001348 /* Note: we don't support OABI, unlike 32-bit ARM build */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001349 scno = arm_regs.ARM_r7;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001350 update_personality(tcp, 0);
1351 break;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001352 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001353#elif defined(ARM)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001354 if (arm_regs.ARM_ip != 0) {
1355 /* It is not a syscall entry */
1356 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001357 tcp->flags |= TCB_INSYSCALL;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001358 return 0;
1359 }
1360 /* Note: we support only 32-bit CPUs, not 26-bit */
1361
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001362# ifndef STRACE_KNOWS_ONLY_EABI
1363# warning STRACE_KNOWS_ONLY_EABI not set, will PTRACE_PEEKTEXT on every syscall (slower tracing)
1364 if (arm_regs.ARM_cpsr & 0x20)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001365 /* Thumb mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001366 goto scno_in_r7;
1367 /* ARM mode */
1368 /* Check EABI/OABI by examining SVC insn's low 24 bits */
1369 errno = 0;
1370 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1371 if (errno)
1372 return -1;
1373 /* EABI syscall convention? */
1374 if (scno != 0xef000000) {
1375 /* No, it's OABI */
1376 if ((scno & 0x0ff00000) != 0x0f900000) {
1377 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1378 tcp->pid, scno);
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001379 return -1;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001380 }
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001381 /* Fixup the syscall number */
1382 scno &= 0x000fffff;
1383 } else {
1384 scno_in_r7:
1385 scno = arm_regs.ARM_r7;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001386 }
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001387# else
1388 scno = arm_regs.ARM_r7;
1389# endif
Denys Vlasenko7270de52013-02-21 15:46:34 +01001390 scno = shuffle_scno(scno);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001391#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001392 if (upeek(tcp->pid, 4*PT_ORIG_D0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001393 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001394#elif defined(LINUX_MIPSN32)
Roland McGrath542c2c62008-05-20 01:11:56 +00001395 unsigned long long regs[38];
1396
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001397 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001398 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001399 mips_a3 = regs[REG_A3];
1400 mips_r2 = regs[REG_V0];
Roland McGrath542c2c62008-05-20 01:11:56 +00001401
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001402 scno = mips_r2;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001403 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001404 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001405 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001406 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001407 return 0;
1408 }
Roland McGrath542c2c62008-05-20 01:11:56 +00001409 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001410#elif defined(MIPS)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001411 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001412 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001413 if (upeek(tcp->pid, REG_V0, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001414 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001415
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001416 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001417 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001418 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001419 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001420 return 0;
1421 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001422 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001423#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001424 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001425 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001426 if (upeek(tcp->pid, REG_R0, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001427 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001428
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001429 /*
1430 * Do some sanity checks to figure out if it's
1431 * really a syscall entry
1432 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001433 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001434 if (alpha_a3 == 0 || alpha_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001435 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001436 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001437 return 0;
1438 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001439 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001440#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001441 /* Disassemble the syscall trap. */
1442 /* Retrieve the syscall trap instruction. */
Denys Vlasenko46455822013-02-05 17:02:59 +01001443 unsigned long trap;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001444 errno = 0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001445# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001446 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001447 trap >>= 32;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001448# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001449 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001450# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001451 if (errno)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001452 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001453
1454 /* Disassemble the trap to see what personality to use. */
1455 switch (trap) {
1456 case 0x91d02010:
1457 /* Linux/SPARC syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001458 update_personality(tcp, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001459 break;
1460 case 0x91d0206d:
1461 /* Linux/SPARC64 syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001462 update_personality(tcp, 2);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001463 break;
1464 case 0x91d02000:
1465 /* SunOS syscall trap. (pers 1) */
1466 fprintf(stderr, "syscall: SunOS no support\n");
1467 return -1;
1468 case 0x91d02008:
1469 /* Solaris 2.x syscall trap. (per 2) */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001470 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001471 break;
1472 case 0x91d02009:
1473 /* NetBSD/FreeBSD syscall trap. */
1474 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1475 return -1;
1476 case 0x91d02027:
1477 /* Solaris 2.x gettimeofday */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001478 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001479 break;
1480 default:
Denys Vlasenko523635f2012-02-25 02:44:25 +01001481# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001482 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001483# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001484 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001485# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001486 return -1;
1487 }
1488
1489 /* Extract the system call number from the registers. */
1490 if (trap == 0x91d02027)
1491 scno = 156;
1492 else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001493 scno = sparc_regs.u_regs[U_REG_G1];
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001494 if (scno == 0) {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001495 scno = sparc_regs.u_regs[U_REG_O0];
1496 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 +02001497 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001498#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001499 if (upeek(tcp->pid, PT_GR20, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001500 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001501#elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001502 /*
1503 * In the new syscall ABI, the system call number is in R3.
1504 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001505 if (upeek(tcp->pid, 4*(REG_REG0+3), &scno) < 0)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001506 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001507
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001508 if (scno < 0) {
1509 /* Odd as it may seem, a glibc bug has been known to cause
1510 glibc to issue bogus negative syscall numbers. So for
1511 our purposes, make strace print what it *should* have been */
1512 long correct_scno = (scno & 0xff);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001513 if (debug_flag)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001514 fprintf(stderr,
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001515 "Detected glibc bug: bogus system call"
1516 " number = %ld, correcting to %ld\n",
1517 scno,
1518 correct_scno);
1519 scno = correct_scno;
1520 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001521#elif defined(SH64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001522 if (upeek(tcp->pid, REG_SYSCALL, &scno) < 0)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001523 return -1;
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001524 scno &= 0xFFFF;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001525#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001526 if (upeek(tcp->pid, 4*PT_R9, &scno) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001527 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001528#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001529 int currpers;
1530 scno = tile_regs.regs[10];
1531# ifdef __tilepro__
1532 currpers = 1;
1533# else
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001534# ifndef PT_FLAGS_COMPAT
1535# define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1536# endif
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001537 if (tile_regs.flags & PT_FLAGS_COMPAT)
1538 currpers = 1;
1539 else
1540 currpers = 0;
1541# endif
1542 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001543#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001544 if (upeek(tcp->pid, 0, &scno) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001545 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001546#elif defined(OR1K)
1547 scno = or1k_regs.gpr[11];
James Hogan5f999a82013-02-22 14:44:10 +00001548#elif defined(METAG)
1549 scno = metag_regs.dx[0][1]; /* syscall number in D1Re0 (D1.0) */
Chris Zankel8f636ed2013-03-25 10:22:07 -07001550#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001551 if (upeek(tcp->pid, SYSCALL_NR, &scno) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001552 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301553# elif defined(ARC)
1554 scno = arc_regs.scratch.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001555#endif
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001556
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001557 tcp->scno = scno;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001558 if (SCNO_IS_VALID(tcp->scno)) {
1559 tcp->s_ent = &sysent[scno];
1560 tcp->qual_flg = qual_flags[scno];
1561 } else {
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01001562 static const struct_sysent unknown = {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001563 .nargs = MAX_ARGS,
1564 .sys_flags = 0,
1565 .sys_func = printargs,
1566 .sys_name = "unknown", /* not used */
1567 };
1568 tcp->s_ent = &unknown;
1569 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1570 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001571 return 1;
1572}
1573
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02001574/* Called at each syscall entry.
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001575 * Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001576 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1577 * 1: ok, continue in trace_syscall_entering().
1578 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001579 * ("????" etc) and bail out.
1580 */
Roland McGratha4d48532005-06-08 20:45:28 +00001581static int
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001582syscall_fixup_on_sysenter(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001583{
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001584 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001585#if defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001586 if (i386_regs.eax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001587 if (debug_flag)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001588 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1589 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001590 }
H.J. Lu35be5812012-04-16 13:00:01 +02001591#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001592 {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001593 long rax;
1594 if (x86_io.iov_len == sizeof(i386_regs)) {
1595 /* Sign extend from 32 bits */
1596 rax = (int32_t)i386_regs.eax;
1597 } else {
1598 /* Note: in X32 build, this truncates 64 to 32 bits */
1599 rax = x86_64_regs.rax;
1600 }
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001601 if (rax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001602 if (debug_flag)
Denys Vlasenko18beb982011-08-24 16:59:23 +02001603 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1604 return 0;
1605 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001606 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001607#elif defined(M68K)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001608 /* TODO? Eliminate upeek's in arches below like we did in x86 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001609 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001610 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001611 if (m68k_d0 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001612 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001613 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001614 return 0;
1615 }
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001616#elif defined(IA64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001617 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001618 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001619 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001620 return -1;
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001621 if (ia64_ia32mode && ia64_r8 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001622 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001623 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001624 return 0;
1625 }
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001626#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001627 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001628 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001629 if (cris_r10 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001630 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001631 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001632 return 0;
1633 }
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001634#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001635 if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001636 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001637 if (microblaze_r3 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001638 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001639 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001640 return 0;
1641 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001642#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001643 return 1;
1644}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001645
Denys Vlasenko146b9442012-03-18 22:10:48 +01001646static void
1647internal_fork(struct tcb *tcp)
1648{
1649#if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1650# define ARG_FLAGS 1
1651#else
1652# define ARG_FLAGS 0
1653#endif
1654#ifndef CLONE_UNTRACED
1655# define CLONE_UNTRACED 0x00800000
1656#endif
1657 if ((ptrace_setoptions
1658 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1659 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1660 return;
1661
1662 if (!followfork)
1663 return;
1664
1665 if (entering(tcp)) {
1666 /*
1667 * We won't see the new child if clone is called with
1668 * CLONE_UNTRACED, so we keep the same logic with that option
1669 * and don't trace it.
1670 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001671 if ((tcp->s_ent->sys_func == sys_clone)
1672 && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
1673 )
Denys Vlasenko146b9442012-03-18 22:10:48 +01001674 return;
1675 setbpt(tcp);
1676 } else {
1677 if (tcp->flags & TCB_BPTSET)
1678 clearbpt(tcp);
1679 }
1680}
1681
1682#if defined(TCB_WAITEXECVE)
1683static void
1684internal_exec(struct tcb *tcp)
1685{
1686 /* Maybe we have post-execve SIGTRAP suppressed? */
1687 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1688 return; /* yes, no need to do anything */
1689
1690 if (exiting(tcp) && syserror(tcp))
1691 /* Error in execve, no post-execve SIGTRAP expected */
1692 tcp->flags &= ~TCB_WAITEXECVE;
1693 else
1694 tcp->flags |= TCB_WAITEXECVE;
1695}
1696#endif
1697
1698static void
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001699syscall_fixup_for_fork_exec(struct tcb *tcp)
Roland McGrathc1e45922008-05-27 23:18:29 +00001700{
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001701 /*
1702 * We must always trace a few critical system calls in order to
1703 * correctly support following forks in the presence of tracing
1704 * qualifiers.
1705 */
1706 int (*func)();
1707
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001708 func = tcp->s_ent->sys_func;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001709
1710 if ( sys_fork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001711 || sys_vfork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001712 || sys_clone == func
Denys Vlasenko146b9442012-03-18 22:10:48 +01001713 ) {
1714 internal_fork(tcp);
1715 return;
1716 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001717
Denys Vlasenko84703742012-02-25 02:38:52 +01001718#if defined(TCB_WAITEXECVE)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001719 if ( sys_execve == func
Denys Vlasenko84703742012-02-25 02:38:52 +01001720# if defined(SPARC) || defined(SPARC64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001721 || sys_execv == func
Denys Vlasenkoa7949742011-08-21 17:26:55 +02001722# endif
Denys Vlasenko146b9442012-03-18 22:10:48 +01001723 ) {
1724 internal_exec(tcp);
1725 return;
1726 }
Roland McGrathc1e45922008-05-27 23:18:29 +00001727#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001728}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001729
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001730/* Return -1 on error or 1 on success (never 0!) */
Roland McGratha4d48532005-06-08 20:45:28 +00001731static int
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001732get_syscall_args(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001733{
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001734 int i, nargs;
1735
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001736 nargs = tcp->s_ent->nargs;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001737
Denys Vlasenko523635f2012-02-25 02:44:25 +01001738#if defined(S390) || defined(S390X)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001739 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001740 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 +02001741 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001742#elif defined(ALPHA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001743 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001744 if (upeek(tcp->pid, REG_A0+i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001745 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001746#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001747 if (!ia64_ia32mode) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001748 unsigned long *out0, cfm, sof, sol;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001749 long rbs_end;
1750 /* be backwards compatible with kernel < 2.4.4... */
1751# ifndef PT_RBS_END
1752# define PT_RBS_END PT_AR_BSP
1753# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001754
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001755 if (upeek(tcp->pid, PT_RBS_END, &rbs_end) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001756 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001757 if (upeek(tcp->pid, PT_CFM, (long *) &cfm) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001758 return -1;
1759
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001760 sof = (cfm >> 0) & 0x7f;
1761 sol = (cfm >> 7) & 0x7f;
1762 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1763
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001764 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001765 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1766 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1767 return -1;
1768 }
1769 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001770 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1771 PT_R9 /* ECX = out1 */,
1772 PT_R10 /* EDX = out2 */,
1773 PT_R14 /* ESI = out3 */,
1774 PT_R15 /* EDI = out4 */,
1775 PT_R13 /* EBP = out5 */};
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001776
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001777 for (i = 0; i < nargs; ++i) {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001778 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001779 return -1;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001780 /* truncate away IVE sign-extension */
1781 tcp->u_arg[i] &= 0xffffffff;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001782 }
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001783 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001784#elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001785 /* N32 and N64 both use up to six registers. */
1786 unsigned long long regs[38];
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001787
1788 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1789 return -1;
1790
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001791 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001792 tcp->u_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001793# if defined(LINUX_MIPSN32)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001794 tcp->ext_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001795# endif
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001796 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001797#elif defined(MIPS)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001798 if (nargs > 4) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001799 long sp;
1800
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001801 if (upeek(tcp->pid, REG_SP, &sp) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001802 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001803 for (i = 0; i < 4; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001804 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001805 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001806 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001807 (char *)(tcp->u_arg + 4));
1808 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001809 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001810 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001811 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001812 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001813#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001814 (void)i;
1815 (void)nargs;
1816 tcp->u_arg[0] = ppc_regs.orig_gpr3;
1817 tcp->u_arg[1] = ppc_regs.gpr[4];
1818 tcp->u_arg[2] = ppc_regs.gpr[5];
1819 tcp->u_arg[3] = ppc_regs.gpr[6];
1820 tcp->u_arg[4] = ppc_regs.gpr[7];
1821 tcp->u_arg[5] = ppc_regs.gpr[8];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001822#elif defined(SPARC) || defined(SPARC64)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001823 for (i = 0; i < nargs; ++i)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001824 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001825#elif defined(HPPA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001826 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001827 if (upeek(tcp->pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001828 return -1;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001829#elif defined(ARM) || defined(AARCH64)
1830# if defined(AARCH64)
1831 if (tcp->currpers == 1)
1832 for (i = 0; i < nargs; ++i)
1833 tcp->u_arg[i] = aarch64_regs.regs[i];
1834 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001835# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001836 for (i = 0; i < nargs; ++i)
Denys Vlasenko401374e2013-02-06 18:24:39 +01001837 tcp->u_arg[i] = arm_regs.uregs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001838#elif defined(AVR32)
Denys Vlasenkob5b25892011-08-30 19:04:54 +02001839 (void)i;
1840 (void)nargs;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001841 tcp->u_arg[0] = avr32_regs.r12;
1842 tcp->u_arg[1] = avr32_regs.r11;
1843 tcp->u_arg[2] = avr32_regs.r10;
1844 tcp->u_arg[3] = avr32_regs.r9;
1845 tcp->u_arg[4] = avr32_regs.r5;
1846 tcp->u_arg[5] = avr32_regs.r3;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001847#elif defined(BFIN)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001848 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 +02001849
Denys Vlasenko4b887a52011-08-23 13:32:38 +02001850 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001851 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001852 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001853#elif defined(SH)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001854 static const int syscall_regs[MAX_ARGS] = {
1855 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1856 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001857 };
1858
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001859 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001860 if (upeek(tcp->pid, syscall_regs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001861 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001862#elif defined(SH64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001863 int i;
1864 /* Registers used by SH5 Linux system calls for parameters */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001865 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001866
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001867 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001868 if (upeek(tcp->pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001869 return -1;
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001870#elif defined(I386)
1871 (void)i;
1872 (void)nargs;
1873 tcp->u_arg[0] = i386_regs.ebx;
1874 tcp->u_arg[1] = i386_regs.ecx;
1875 tcp->u_arg[2] = i386_regs.edx;
1876 tcp->u_arg[3] = i386_regs.esi;
1877 tcp->u_arg[4] = i386_regs.edi;
1878 tcp->u_arg[5] = i386_regs.ebp;
H.J. Lu35be5812012-04-16 13:00:01 +02001879#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001880 (void)i;
1881 (void)nargs;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001882 if (x86_io.iov_len != sizeof(i386_regs)) {
1883 /* x86-64 or x32 ABI */
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001884 tcp->u_arg[0] = x86_64_regs.rdi;
1885 tcp->u_arg[1] = x86_64_regs.rsi;
1886 tcp->u_arg[2] = x86_64_regs.rdx;
1887 tcp->u_arg[3] = x86_64_regs.r10;
1888 tcp->u_arg[4] = x86_64_regs.r8;
1889 tcp->u_arg[5] = x86_64_regs.r9;
H.J. Lu35be5812012-04-16 13:00:01 +02001890# ifdef X32
1891 tcp->ext_arg[0] = x86_64_regs.rdi;
1892 tcp->ext_arg[1] = x86_64_regs.rsi;
1893 tcp->ext_arg[2] = x86_64_regs.rdx;
1894 tcp->ext_arg[3] = x86_64_regs.r10;
1895 tcp->ext_arg[4] = x86_64_regs.r8;
1896 tcp->ext_arg[5] = x86_64_regs.r9;
1897# endif
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001898 } else {
1899 /* i386 ABI */
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001900 /* Zero-extend from 32 bits */
1901 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1902 * if you need to use *sign-extended* parameter.
1903 */
1904 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1905 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1906 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1907 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1908 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1909 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001910 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001911#elif defined(MICROBLAZE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001912 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001913 if (upeek(tcp->pid, (5 + i) * 4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001914 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001915#elif defined(CRISV10) || defined(CRISV32)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001916 static const int crisregs[MAX_ARGS] = {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001917 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001918 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001919 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001920
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001921 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001922 if (upeek(tcp->pid, crisregs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001923 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001924#elif defined(TILE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001925 for (i = 0; i < nargs; ++i)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001926 tcp->u_arg[i] = tile_regs.regs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001927#elif defined(M68K)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001928 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001929 if (upeek(tcp->pid, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001930 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001931#elif defined(OR1K)
1932 (void)nargs;
1933 for (i = 0; i < 6; ++i)
1934 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
James Hogan5f999a82013-02-22 14:44:10 +00001935#elif defined(METAG)
1936 for (i = 0; i < nargs; i++)
1937 /* arguments go backwards from D1Ar1 (D1.3) */
1938 tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
Chris Zankel8f636ed2013-03-25 10:22:07 -07001939#elif defined(XTENSA)
1940 /* arg0: a6, arg1: a3, arg2: a4, arg3: a5, arg4: a8, arg5: a9 */
1941 static const int xtensaregs[MAX_ARGS] = { 6, 3, 4, 5, 8, 9 };
1942 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001943 if (upeek(tcp->pid, REG_A_BASE + xtensaregs[i], &tcp->u_arg[i]) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001944 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301945# elif defined(ARC)
1946 long *arc_args = &arc_regs.scratch.r0;
1947 for (i = 0; i < nargs; ++i)
1948 tcp->u_arg[i] = *arc_args--;
1949
Denys Vlasenko523635f2012-02-25 02:44:25 +01001950#else /* Other architecture (32bits specific) */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001951 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001952 if (upeek(tcp->pid, i*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001953 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001954#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001955 return 1;
1956}
1957
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00001958static int
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001959trace_syscall_entering(struct tcb *tcp)
1960{
1961 int res, scno_good;
1962
Denys Vlasenko2ce12ed2011-08-24 17:25:32 +02001963#if defined TCB_WAITEXECVE
1964 if (tcp->flags & TCB_WAITEXECVE) {
1965 /* This is the post-execve SIGTRAP. */
1966 tcp->flags &= ~TCB_WAITEXECVE;
1967 return 0;
1968 }
1969#endif
1970
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001971 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001972 if (res == 0)
1973 return res;
Denys Vlasenko907735a2012-03-21 00:23:16 +01001974 if (res == 1) {
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001975 res = syscall_fixup_on_sysenter(tcp);
Denys Vlasenko907735a2012-03-21 00:23:16 +01001976 if (res == 0)
1977 return res;
1978 if (res == 1)
1979 res = get_syscall_args(tcp);
1980 }
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001981
1982 if (res != 1) {
1983 printleader(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001984 if (scno_good != 1)
Denys Vlasenkob7a6dae2012-03-20 16:48:35 +01001985 tprints("????" /* anti-trigraph gap */ "(");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001986 else if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01001987 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001988 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001989 tprintf("%s(", tcp->s_ent->sys_name);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001990 /*
1991 * " <unavailable>" will be added later by the code which
1992 * detects ptrace errors.
1993 */
1994 goto ret;
1995 }
1996
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02001997 if ( sys_execve == tcp->s_ent->sys_func
1998# if defined(SPARC) || defined(SPARC64)
1999 || sys_execv == tcp->s_ent->sys_func
2000# endif
2001 ) {
2002 hide_log_until_execve = 0;
2003 }
2004
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002005#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002006 while (1) {
Denys Vlasenko523635f2012-02-25 02:44:25 +01002007# ifdef SYS_socket_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002008 if (tcp->s_ent->sys_func == sys_socketcall) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00002009 decode_socket_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002010 break;
2011 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002012# endif
2013# ifdef SYS_ipc_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002014 if (tcp->s_ent->sys_func == sys_ipc) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00002015 decode_ipc_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002016 break;
2017 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002018# endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002019 break;
2020 }
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002021#endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002022
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01002023 if (need_fork_exec_workarounds)
2024 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002025
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002026 if (!(tcp->qual_flg & QUAL_TRACE)
2027 || (tracing_paths && !pathtrace_match(tcp))
2028 ) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002029 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
2030 return 0;
2031 }
2032
2033 tcp->flags &= ~TCB_FILTERED;
2034
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002035 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002036 res = 0;
2037 goto ret;
2038 }
2039
2040 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002041 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002042 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002043 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002044 tprintf("%s(", tcp->s_ent->sys_name);
2045 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002046 res = printargs(tcp);
2047 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002048 res = tcp->s_ent->sys_func(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002049
Dmitry V. Levinb742d8c2012-09-17 22:40:12 +00002050 fflush(tcp->outf);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002051 ret:
2052 tcp->flags |= TCB_INSYSCALL;
2053 /* Measure the entrance time as late as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002054 if (Tflag || cflag)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002055 gettimeofday(&tcp->etime, NULL);
2056 return res;
2057}
2058
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002059/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01002060 * 1: ok, continue in trace_syscall_exiting().
2061 * -1: error, trace_syscall_exiting() should print error indicator
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002062 * ("????" etc) and bail out.
2063 */
2064static int
2065get_syscall_result(struct tcb *tcp)
2066{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002067#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002068 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002069 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002070#elif defined(POWERPC)
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002071 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002072#elif defined(AVR32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002073 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002074#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002075 if (upeek(tcp->pid, PT_R0, &bfin_r0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002076 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002077#elif defined(I386)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002078 /* already done by get_regs */
H.J. Lu35be5812012-04-16 13:00:01 +02002079#elif defined(X86_64) || defined(X32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002080 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002081#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002082# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01002083 long psr;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002084 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02002085 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002086 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002087 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002088 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002089 return -1;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002090#elif defined(ARM)
2091 /* already done by get_regs */
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002092#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002093 /* register reading already done by get_regs */
2094
2095 /* Used to do this, but we did it on syscall entry already: */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002096 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
2097 * else it's personality 0.
2098 */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002099 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
Denys Vlasenko523635f2012-02-25 02:44:25 +01002100#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002101 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002102 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002103#elif defined(LINUX_MIPSN32)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002104 unsigned long long regs[38];
2105
2106 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
2107 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002108 mips_a3 = regs[REG_A3];
2109 mips_r2 = regs[REG_V0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01002110#elif defined(MIPS)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002111 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002112 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002113 if (upeek(tcp->pid, REG_V0, &mips_r2) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002114 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002115#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002116 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002117 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002118 if (upeek(tcp->pid, REG_R0, &alpha_r0) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002119 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002120#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002121 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002122#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002123 if (upeek(tcp->pid, PT_GR28, &hppa_r28) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002124 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002125#elif defined(SH)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002126 /* new syscall ABI returns result in R0 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002127 if (upeek(tcp->pid, 4*REG_REG0, (long *)&sh_r0) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002128 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002129#elif defined(SH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002130 /* ABI defines result returned in r9 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002131 if (upeek(tcp->pid, REG_GENERAL(9), (long *)&sh64_r9) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002132 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002133#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002134 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002135 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002136#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002137 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002138#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002139 if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002140 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01002141#elif defined(OR1K)
2142 /* already done by get_regs */
James Hogan5f999a82013-02-22 14:44:10 +00002143#elif defined(METAG)
2144 /* already done by get_regs */
Chris Zankel8f636ed2013-03-25 10:22:07 -07002145#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002146 if (upeek(tcp->pid, REG_A_BASE + 2, &xtensa_a2) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07002147 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05302148#elif defined(ARC)
2149 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002150#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002151 return 1;
2152}
2153
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002154/* Called at each syscall exit */
2155static void
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002156syscall_fixup_on_sysexit(struct tcb *tcp)
2157{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002158#if defined(S390) || defined(S390X)
Denys Vlasenkoece98792011-08-25 10:25:35 +02002159 if ((tcp->flags & TCB_WAITEXECVE)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002160 && (s390_gpr2 == -ENOSYS || s390_gpr2 == tcp->scno)) {
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002161 /*
2162 * Return from execve.
2163 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2164 * flag set for the post-execve SIGTRAP to see and reset.
2165 */
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002166 s390_gpr2 = 0;
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002167 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002168#endif
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002169}
2170
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002171/*
2172 * Check the syscall return value register value for whether it is
2173 * a negated errno code indicating an error, or a success return value.
2174 */
2175static inline int
2176is_negated_errno(unsigned long int val)
2177{
2178 unsigned long int max = -(long int) nerrnos;
Denys Vlasenko2544f982013-02-19 17:39:56 +01002179#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01002180 if (current_wordsize < sizeof(val)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002181 val = (unsigned int) val;
2182 max = (unsigned int) max;
2183 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002184#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002185 return val > max;
2186}
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002187
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002188#if defined(X32)
2189static inline int
2190is_negated_errno_x32(unsigned long long val)
2191{
2192 unsigned long long max = -(long long) nerrnos;
2193 /*
2194 * current_wordsize is 4 even in personality 0 (native X32)
2195 * but truncation _must not_ be done in it.
2196 * can't check current_wordsize here!
2197 */
2198 if (current_personality != 0) {
2199 val = (uint32_t) val;
2200 max = (uint32_t) max;
2201 }
2202 return val > max;
2203}
2204#endif
2205
Denys Vlasenko907735a2012-03-21 00:23:16 +01002206/* Returns:
2207 * 1: ok, continue in trace_syscall_exiting().
2208 * -1: error, trace_syscall_exiting() should print error indicator
2209 * ("????" etc) and bail out.
2210 */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002211static void
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002212get_error(struct tcb *tcp)
2213{
2214 int u_error = 0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002215 int check_errno = 1;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002216 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002217 check_errno = 0;
2218 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002219#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002220 if (check_errno && is_negated_errno(s390_gpr2)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002221 tcp->u_rval = -1;
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002222 u_error = -s390_gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002223 }
2224 else {
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002225 tcp->u_rval = s390_gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002226 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002227#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002228 if (check_errno && is_negated_errno(i386_regs.eax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002229 tcp->u_rval = -1;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002230 u_error = -i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002231 }
2232 else {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002233 tcp->u_rval = i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002234 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002235#elif defined(X86_64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002236 long rax;
2237 if (x86_io.iov_len == sizeof(i386_regs)) {
2238 /* Sign extend from 32 bits */
2239 rax = (int32_t)i386_regs.eax;
2240 } else {
2241 rax = x86_64_regs.rax;
2242 }
2243 if (check_errno && is_negated_errno(rax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002244 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002245 u_error = -rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002246 }
2247 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002248 tcp->u_rval = rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002249 }
2250#elif defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002251 /* In X32, return value is 64-bit (llseek uses one).
2252 * Using merely "long rax" would not work.
2253 */
2254 long long rax;
2255 if (x86_io.iov_len == sizeof(i386_regs)) {
2256 /* Sign extend from 32 bits */
2257 rax = (int32_t)i386_regs.eax;
2258 } else {
2259 rax = x86_64_regs.rax;
2260 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002261 /* Careful: is_negated_errno() works only on longs */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002262 if (check_errno && is_negated_errno_x32(rax)) {
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002263 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002264 u_error = -rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002265 }
2266 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002267 tcp->u_rval = rax; /* truncating */
2268 tcp->u_lrval = rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002269 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002270#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02002271 if (ia64_ia32mode) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002272 int err;
2273
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002274 err = (int)ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002275 if (check_errno && is_negated_errno(err)) {
2276 tcp->u_rval = -1;
2277 u_error = -err;
2278 }
2279 else {
2280 tcp->u_rval = err;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002281 }
2282 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002283 if (check_errno && ia64_r10) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002284 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002285 u_error = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002286 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002287 tcp->u_rval = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002288 }
2289 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002290#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002291 if (check_errno && mips_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002292 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002293 u_error = mips_r2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002294 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002295 tcp->u_rval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002296# if defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002297 tcp->u_lrval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002298# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002299 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002300#elif defined(POWERPC)
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002301 if (check_errno && (ppc_regs.ccr & 0x10000000)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002302 tcp->u_rval = -1;
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002303 u_error = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002304 }
2305 else {
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002306 tcp->u_rval = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002307 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002308#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002309 if (check_errno && is_negated_errno(m68k_d0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002310 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002311 u_error = -m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002312 }
2313 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002314 tcp->u_rval = m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002315 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002316#elif defined(ARM) || defined(AARCH64)
2317# if defined(AARCH64)
2318 if (tcp->currpers == 1) {
2319 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2320 tcp->u_rval = -1;
2321 u_error = -aarch64_regs.regs[0];
2322 }
2323 else {
2324 tcp->u_rval = aarch64_regs.regs[0];
2325 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002326 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002327 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002328# endif
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002329 {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002330 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002331 tcp->u_rval = -1;
Denys Vlasenko401374e2013-02-06 18:24:39 +01002332 u_error = -arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002333 }
2334 else {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002335 tcp->u_rval = arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002336 }
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002337 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002338#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002339 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002340 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002341 u_error = -avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002342 }
2343 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002344 tcp->u_rval = avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002345 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002346#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002347 if (check_errno && is_negated_errno(bfin_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002348 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002349 u_error = -bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002350 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002351 tcp->u_rval = bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002352 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002353#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002354 if (check_errno && alpha_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002355 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002356 u_error = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002357 }
2358 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002359 tcp->u_rval = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002360 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002361#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002362 if (check_errno && sparc_regs.psr & PSR_C) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002363 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002364 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002365 }
2366 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002367 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002368 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002369#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002370 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002371 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002372 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002373 }
2374 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002375 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002376 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002377#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002378 if (check_errno && is_negated_errno(hppa_r28)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002379 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002380 u_error = -hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002381 }
2382 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002383 tcp->u_rval = hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002384 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002385#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002386 if (check_errno && is_negated_errno(sh_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002387 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002388 u_error = -sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002389 }
2390 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002391 tcp->u_rval = sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002392 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002393#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002394 if (check_errno && is_negated_errno(sh64_r9)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002395 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002396 u_error = -sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002397 }
2398 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002399 tcp->u_rval = sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002400 }
James Hogan5f999a82013-02-22 14:44:10 +00002401#elif defined(METAG)
2402 /* result pointer in D0Re0 (D0.0) */
2403 if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2404 tcp->u_rval = -1;
2405 u_error = -metag_regs.dx[0][0];
2406 }
2407 else {
2408 tcp->u_rval = metag_regs.dx[0][0];
2409 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002410#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002411 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002412 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002413 u_error = -cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002414 }
2415 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002416 tcp->u_rval = cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002417 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002418#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002419 /*
2420 * The standard tile calling convention returns the value (or negative
2421 * errno) in r0, and zero (or positive errno) in r1.
2422 * Until at least kernel 3.8, however, the r1 value is not reflected
2423 * in ptregs at this point, so we use r0 here.
2424 */
2425 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002426 tcp->u_rval = -1;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002427 u_error = -tile_regs.regs[0];
2428 } else {
2429 tcp->u_rval = tile_regs.regs[0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002430 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002431#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002432 if (check_errno && is_negated_errno(microblaze_r3)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002433 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002434 u_error = -microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002435 }
2436 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002437 tcp->u_rval = microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002438 }
Christian Svensson492f81f2013-02-14 13:26:27 +01002439#elif defined(OR1K)
2440 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2441 tcp->u_rval = -1;
2442 u_error = -or1k_regs.gpr[11];
2443 }
2444 else {
2445 tcp->u_rval = or1k_regs.gpr[11];
2446 }
Chris Zankel8f636ed2013-03-25 10:22:07 -07002447#elif defined(XTENSA)
2448 if (check_errno && is_negated_errno(xtensa_a2)) {
2449 tcp->u_rval = -1;
2450 u_error = -xtensa_a2;
2451 }
2452 else {
2453 tcp->u_rval = xtensa_a2;
2454 }
Vineet Gupta7daacbb2013-08-16 12:47:06 +05302455#elif defined(ARC)
2456 if (check_errno && is_negated_errno(arc_regs.scratch.r0)) {
2457 tcp->u_rval = -1;
2458 u_error = -arc_regs.scratch.r0;
2459 }
2460 else {
2461 tcp->u_rval = arc_regs.scratch.r0;
2462 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002463#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002464 tcp->u_error = u_error;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002465}
2466
2467static void
2468dumpio(struct tcb *tcp)
2469{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002470 int (*func)();
2471
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002472 if (syserror(tcp))
2473 return;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +01002474 if ((unsigned long) tcp->u_arg[0] >= num_quals)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002475 return;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002476 func = tcp->s_ent->sys_func;
2477 if (func == printargs)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002478 return;
2479 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002480 if (func == sys_read ||
2481 func == sys_pread ||
2482 func == sys_recv ||
2483 func == sys_recvfrom)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002484 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002485 else if (func == sys_readv)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002486 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2487 return;
2488 }
2489 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002490 if (func == sys_write ||
2491 func == sys_pwrite ||
2492 func == sys_send ||
2493 func == sys_sendto)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002494 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002495 else if (func == sys_writev)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002496 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2497 return;
2498 }
2499}
2500
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002501static int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002502trace_syscall_exiting(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002503{
2504 int sys_res;
2505 struct timeval tv;
Denys Vlasenko1a5b5a72011-08-25 00:29:56 +02002506 int res;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002507 long u_error;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002508
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002509 /* Measure the exit time as early as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002510 if (Tflag || cflag)
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002511 gettimeofday(&tv, NULL);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002512
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00002513#if SUPPORTED_PERSONALITIES > 1
2514 update_personality(tcp, tcp->currpers);
2515#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002516 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002517 if (res == 1) {
2518 syscall_fixup_on_sysexit(tcp); /* never fails */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002519 get_error(tcp); /* never fails */
2520 if (need_fork_exec_workarounds)
2521 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002522 if (filtered(tcp) || hide_log_until_execve)
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002523 goto ret;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002524 }
2525
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002526 if (cflag) {
2527 struct timeval t = tv;
Denys Vlasenkoc95a88f2011-08-21 17:47:40 +02002528 count_syscall(tcp, &t);
Denys Vlasenko7b609d52011-06-22 14:32:43 +02002529 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002530 goto ret;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002531 }
2532 }
2533
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002534 /* If not in -ff mode, and printing_tcp != tcp,
2535 * then the log currently does not end with output
2536 * of _our syscall entry_, but with something else.
2537 * We need to say which syscall's return is this.
2538 *
2539 * Forced reprinting via TCB_REPRINT is used only by
2540 * "strace -ff -oLOG test/threaded_execve" corner case.
2541 * It's the only case when -ff mode needs reprinting.
2542 */
2543 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2544 tcp->flags &= ~TCB_REPRINT;
2545 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002546 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002547 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002548 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002549 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002550 }
2551 printing_tcp = tcp;
2552
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002553 if (res != 1) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002554 /* There was error in one of prior ptrace ops */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002555 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002556 tabto();
Denys Vlasenko000b6012012-01-28 01:25:03 +01002557 tprints("= ? <unavailable>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002558 line_ended();
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002559 tcp->flags &= ~TCB_INSYSCALL;
2560 return res;
2561 }
2562
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002563 sys_res = 0;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002564 if (tcp->qual_flg & QUAL_RAW) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002565 /* sys_res = printargs(tcp); - but it's nop on sysexit */
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002566 } else {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002567 /* FIXME: not_failing_only (IOW, option -z) is broken:
2568 * failure of syscall is known only after syscall return.
2569 * Thus we end up with something like this on, say, ENOENT:
2570 * open("doesnt_exist", O_RDONLY <unfinished ...>
2571 * {next syscall decode}
2572 * whereas the intended result is that open(...) line
2573 * is not shown at all.
2574 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002575 if (not_failing_only && tcp->u_error)
Denys Vlasenko3b738812011-08-22 02:06:35 +02002576 goto ret; /* ignore failed syscalls */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002577 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002578 }
2579
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002580 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002581 tabto();
Denys Vlasenko3b738812011-08-22 02:06:35 +02002582 u_error = tcp->u_error;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002583 if (tcp->qual_flg & QUAL_RAW) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002584 if (u_error)
2585 tprintf("= -1 (errno %ld)", u_error);
2586 else
2587 tprintf("= %#lx", tcp->u_rval);
2588 }
2589 else if (!(sys_res & RVAL_NONE) && u_error) {
2590 switch (u_error) {
Denys Vlasenkofe585652012-01-12 11:26:34 +01002591 /* Blocked signals do not interrupt any syscalls.
2592 * In this case syscalls don't return ERESTARTfoo codes.
2593 *
2594 * Deadly signals set to SIG_DFL interrupt syscalls
2595 * and kill the process regardless of which of the codes below
2596 * is returned by the interrupted syscall.
2597 * In some cases, kernel forces a kernel-generated deadly
2598 * signal to be unblocked and set to SIG_DFL (and thus cause
2599 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2600 * or SIGILL. (The alternative is to leave process spinning
2601 * forever on the faulty instruction - not useful).
2602 *
2603 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2604 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2605 * but kernel will always restart them.
2606 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002607 case ERESTARTSYS:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002608 /* Most common type of signal-interrupted syscall exit code.
2609 * The system call will be restarted with the same arguments
2610 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2611 */
2612 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002613 break;
2614 case ERESTARTNOINTR:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002615 /* Rare. For example, fork() returns this if interrupted.
2616 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2617 */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002618 tprints("= ? ERESTARTNOINTR (To be restarted)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002619 break;
2620 case ERESTARTNOHAND:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002621 /* pause(), rt_sigsuspend() etc use this code.
2622 * SA_RESTART is ignored (assumed not set):
2623 * syscall won't restart (will return EINTR instead)
Denys Vlasenko30c03232013-02-19 16:59:26 +01002624 * even after signal with SA_RESTART set. However,
2625 * after SIG_IGN or SIG_DFL signal it will restart
2626 * (thus the name "restart only if has no handler").
Denys Vlasenkofe585652012-01-12 11:26:34 +01002627 */
Denys Vlasenkoaba62922013-03-05 16:56:35 +01002628 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002629 break;
2630 case ERESTART_RESTARTBLOCK:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002631 /* Syscalls like nanosleep(), poll() which can't be
2632 * restarted with their original arguments use this
2633 * code. Kernel will execute restart_syscall() instead,
2634 * which changes arguments before restarting syscall.
2635 * SA_RESTART is ignored (assumed not set) similarly
2636 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2637 * since restart data is saved in "restart block"
2638 * in task struct, and if signal handler uses a syscall
2639 * which in turn saves another such restart block,
2640 * old data is lost and restart becomes impossible)
2641 */
2642 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002643 break;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002644 default:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002645 if (u_error < 0)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002646 tprintf("= -1 E??? (errno %ld)", u_error);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002647 else if (u_error < nerrnos)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002648 tprintf("= -1 %s (%s)", errnoent[u_error],
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002649 strerror(u_error));
2650 else
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002651 tprintf("= -1 ERRNO_%ld (%s)", u_error,
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002652 strerror(u_error));
2653 break;
2654 }
2655 if ((sys_res & RVAL_STR) && tcp->auxstr)
2656 tprintf(" (%s)", tcp->auxstr);
2657 }
2658 else {
2659 if (sys_res & RVAL_NONE)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002660 tprints("= ?");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002661 else {
2662 switch (sys_res & RVAL_MASK) {
2663 case RVAL_HEX:
2664 tprintf("= %#lx", tcp->u_rval);
2665 break;
2666 case RVAL_OCTAL:
2667 tprintf("= %#lo", tcp->u_rval);
2668 break;
2669 case RVAL_UDECIMAL:
2670 tprintf("= %lu", tcp->u_rval);
2671 break;
2672 case RVAL_DECIMAL:
2673 tprintf("= %ld", tcp->u_rval);
2674 break;
H.J. Ludd0130b2012-04-16 12:16:45 +02002675#if defined(LINUX_MIPSN32) || defined(X32)
2676 /*
2677 case RVAL_LHEX:
2678 tprintf("= %#llx", tcp->u_lrval);
2679 break;
2680 case RVAL_LOCTAL:
2681 tprintf("= %#llo", tcp->u_lrval);
2682 break;
2683 */
2684 case RVAL_LUDECIMAL:
2685 tprintf("= %llu", tcp->u_lrval);
2686 break;
2687 /*
2688 case RVAL_LDECIMAL:
2689 tprintf("= %lld", tcp->u_lrval);
2690 break;
2691 */
2692#endif
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002693 default:
2694 fprintf(stderr,
2695 "invalid rval format\n");
2696 break;
2697 }
2698 }
2699 if ((sys_res & RVAL_STR) && tcp->auxstr)
2700 tprintf(" (%s)", tcp->auxstr);
2701 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002702 if (Tflag) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002703 tv_sub(&tv, &tv, &tcp->etime);
2704 tprintf(" <%ld.%06ld>",
2705 (long) tv.tv_sec, (long) tv.tv_usec);
2706 }
Denys Vlasenko000b6012012-01-28 01:25:03 +01002707 tprints("\n");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002708 dumpio(tcp);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002709 line_ended();
2710
Denys Vlasenko3b738812011-08-22 02:06:35 +02002711 ret:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002712 tcp->flags &= ~TCB_INSYSCALL;
2713 return 0;
2714}
2715
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002716int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002717trace_syscall(struct tcb *tcp)
2718{
2719 return exiting(tcp) ?
2720 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2721}