blob: 67af9fd811e0d5207f1aa501fa966cffef8d0fc4 [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/param.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000036
Dmitry V. Levin5503dd22015-02-13 02:12:14 +000037/* for struct iovec */
38#include <sys/uio.h>
Maarten ter Huurne40c174b2014-10-20 01:02:48 +020039
Dmitry V. Levin7211dbc2015-02-28 12:20:21 +000040#include "regs.h"
Dmitry V. Levinfadf3792015-02-13 00:26:38 +000041#include "ptrace.h"
Wichert Akkerman15dea971999-10-06 13:06:34 +000042
Denys Vlasenko84703742012-02-25 02:38:52 +010043#if defined(SPARC64)
Roland McGrath6d1a65c2004-07-12 07:44:08 +000044# undef PTRACE_GETREGS
45# define PTRACE_GETREGS PTRACE_GETREGS64
46# undef PTRACE_SETREGS
47# define PTRACE_SETREGS PTRACE_SETREGS64
Denys Vlasenko84703742012-02-25 02:38:52 +010048#endif
Roland McGrath6d1a65c2004-07-12 07:44:08 +000049
Dmitry V. Levin5503dd22015-02-13 02:12:14 +000050#if defined SPARC64
51# include <asm/psrcompat.h>
52#elif defined SPARC
53# include <asm/psr.h>
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010054#endif
55
Dmitry V. Levin48f08902015-03-05 23:30:02 +000056#ifdef IA64
57# include <asm/rse.h>
58#endif
59
Dmitry V. Levin4c3f2ae2015-02-13 22:53:00 +000060#ifndef NT_PRSTATUS
61# define NT_PRSTATUS 1
62#endif
63
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000064#ifndef NSIG
Denys Vlasenko523635f2012-02-25 02:44:25 +010065# warning: NSIG is not defined, using 32
66# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000067#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000068
69#include "syscall.h"
70
71/* Define these shorthand notations to simplify the syscallent files. */
Roland McGrath2fe7b132005-07-05 03:25:35 +000072#define TD TRACE_DESC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000073#define TF TRACE_FILE
74#define TI TRACE_IPC
75#define TN TRACE_NETWORK
76#define TP TRACE_PROCESS
77#define TS TRACE_SIGNAL
Namhyung Kim96792962012-10-24 11:41:57 +090078#define TM TRACE_MEMORY
Dmitry V. Levin50a218d2011-01-18 17:36:20 +000079#define NF SYSCALL_NEVER_FAILS
Denys Vlasenkoac1ce772011-08-23 13:24:17 +020080#define MA MAX_ARGS
Masatake YAMATO1d78d222014-04-16 15:33:11 +090081#define SI STACKTRACE_INVALIDATE_CACHE
82#define SE STACKTRACE_CAPTURE_ON_ENTER
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000083
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +010084const struct_sysent sysent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000085#include "syscallent.h"
86};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000087
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010088#if SUPPORTED_PERSONALITIES > 1
89static const struct_sysent sysent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +010090# include "syscallent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000091};
Denys Vlasenko39fca622011-08-20 02:12:33 +020092#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000093
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010094#if SUPPORTED_PERSONALITIES > 2
95static const struct_sysent sysent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +010096# include "syscallent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000097};
Denys Vlasenko39fca622011-08-20 02:12:33 +020098#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000099
100/* Now undef them since short defines cause wicked namespace pollution. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000101#undef TD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000102#undef TF
103#undef TI
104#undef TN
105#undef TP
106#undef TS
Namhyung Kim96792962012-10-24 11:41:57 +0900107#undef TM
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000108#undef NF
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200109#undef MA
Masatake YAMATO1d78d222014-04-16 15:33:11 +0900110#undef SI
111#undef SE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000112
Denys Vlasenko39fca622011-08-20 02:12:33 +0200113/*
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +0000114 * `ioctlent[012].h' files are automatically generated by the auxiliary
Denys Vlasenko39fca622011-08-20 02:12:33 +0200115 * program `ioctlsort', such that the list is sorted by the `code' field.
116 * This has the side-effect of resolving the _IO.. macros into
117 * plain integers, eliminating the need to include here everything
118 * in "/usr/include".
119 */
120
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100121const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000122#include "errnoent.h"
123};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100124const char *const signalent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200125#include "signalent.h"
126};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100127const struct_ioctlent ioctlent0[] = {
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +0000128#include "ioctlent0.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200129};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000130
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100131#if SUPPORTED_PERSONALITIES > 1
Roland McGrathee36ce12004-09-04 03:53:10 +0000132static const char *const errnoent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100133# include "errnoent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000134};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200135static const char *const signalent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100136# include "signalent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200137};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100138static const struct_ioctlent ioctlent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100139# include "ioctlent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200140};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200141#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000142
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100143#if SUPPORTED_PERSONALITIES > 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000144static const char *const errnoent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100145# include "errnoent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000146};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200147static const char *const signalent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100148# include "signalent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200149};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100150static const struct_ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100151# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200152};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200153#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000154
Dmitry V. Levine6f55242013-02-26 22:02:30 +0000155enum {
156 nsyscalls0 = ARRAY_SIZE(sysent0)
157#if SUPPORTED_PERSONALITIES > 1
158 , nsyscalls1 = ARRAY_SIZE(sysent1)
159# if SUPPORTED_PERSONALITIES > 2
160 , nsyscalls2 = ARRAY_SIZE(sysent2)
161# endif
162#endif
163};
164
165enum {
166 nerrnos0 = ARRAY_SIZE(errnoent0)
167#if SUPPORTED_PERSONALITIES > 1
168 , nerrnos1 = ARRAY_SIZE(errnoent1)
169# if SUPPORTED_PERSONALITIES > 2
170 , nerrnos2 = ARRAY_SIZE(errnoent2)
171# endif
172#endif
173};
174
175enum {
176 nsignals0 = ARRAY_SIZE(signalent0)
177#if SUPPORTED_PERSONALITIES > 1
178 , nsignals1 = ARRAY_SIZE(signalent1)
179# if SUPPORTED_PERSONALITIES > 2
180 , nsignals2 = ARRAY_SIZE(signalent2)
181# endif
182#endif
183};
184
185enum {
186 nioctlents0 = ARRAY_SIZE(ioctlent0)
187#if SUPPORTED_PERSONALITIES > 1
188 , nioctlents1 = ARRAY_SIZE(ioctlent1)
189# if SUPPORTED_PERSONALITIES > 2
190 , nioctlents2 = ARRAY_SIZE(ioctlent2)
191# endif
192#endif
193};
194
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100195#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100196const struct_sysent *sysent = sysent0;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100197const char *const *errnoent = errnoent0;
198const char *const *signalent = signalent0;
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100199const struct_ioctlent *ioctlent = ioctlent0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100200#endif
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100201unsigned nsyscalls = nsyscalls0;
202unsigned nerrnos = nerrnos0;
203unsigned nsignals = nsignals0;
204unsigned nioctlents = nioctlents0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100205
206unsigned num_quals;
207qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
208
209static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
210 nsyscalls0,
211#if SUPPORTED_PERSONALITIES > 1
212 nsyscalls1,
213#endif
214#if SUPPORTED_PERSONALITIES > 2
215 nsyscalls2,
216#endif
217};
218static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
219 sysent0,
220#if SUPPORTED_PERSONALITIES > 1
221 sysent1,
222#endif
223#if SUPPORTED_PERSONALITIES > 2
224 sysent2,
225#endif
226};
227
228enum {
229 MAX_NSYSCALLS1 = (nsyscalls0
230#if SUPPORTED_PERSONALITIES > 1
231 > nsyscalls1 ? nsyscalls0 : nsyscalls1
232#endif
233 ),
234 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
235#if SUPPORTED_PERSONALITIES > 2
236 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
237#endif
238 ),
239 MAX_NSYSCALLS = MAX_NSYSCALLS2,
240 /* We are ready for arches with up to 255 signals,
241 * even though the largest known signo is on MIPS and it is 128.
242 * The number of existing syscalls on all arches is
243 * larger that 255 anyway, so it is just a pedantic matter.
244 */
245 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
246};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000247
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100248#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100249unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000250
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100251# ifndef current_wordsize
252unsigned current_wordsize;
253static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000254 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000255 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100256# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000257 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100258# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200259};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100260# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000261
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200262void
Dmitry V. Levin3abe8b22006-12-20 22:37:21 +0000263set_personality(int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000264{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100265 nsyscalls = nsyscall_vec[personality];
266 sysent = sysent_vec[personality];
267
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000268 switch (personality) {
269 case 0:
270 errnoent = errnoent0;
271 nerrnos = nerrnos0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000272 ioctlent = ioctlent0;
273 nioctlents = nioctlents0;
274 signalent = signalent0;
275 nsignals = nsignals0;
276 break;
277
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000278 case 1:
279 errnoent = errnoent1;
280 nerrnos = nerrnos1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000281 ioctlent = ioctlent1;
282 nioctlents = nioctlents1;
283 signalent = signalent1;
284 nsignals = nsignals1;
285 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000286
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100287# if SUPPORTED_PERSONALITIES > 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000288 case 2:
289 errnoent = errnoent2;
290 nerrnos = nerrnos2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000291 ioctlent = ioctlent2;
292 nioctlents = nioctlents2;
293 signalent = signalent2;
294 nsignals = nsignals2;
295 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100296# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000297 }
298
299 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100300# ifndef current_wordsize
301 current_wordsize = personality_wordsize[personality];
302# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000303}
304
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000305static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000306update_personality(struct tcb *tcp, unsigned int personality)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000307{
308 if (personality == current_personality)
309 return;
310 set_personality(personality);
311
312 if (personality == tcp->currpers)
313 return;
314 tcp->currpers = personality;
315
H.J. Lu35be5812012-04-16 13:00:01 +0200316# if defined(POWERPC64)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000317 if (!qflag) {
318 static const char *const names[] = {"64 bit", "32 bit"};
319 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
320 tcp->pid, names[personality]);
321 }
H.J. Lu35be5812012-04-16 13:00:01 +0200322# elif defined(X86_64)
323 if (!qflag) {
324 static const char *const names[] = {"64 bit", "32 bit", "x32"};
325 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
326 tcp->pid, names[personality]);
327 }
H.J. Lu085e4282012-04-17 11:05:04 -0700328# elif defined(X32)
329 if (!qflag) {
330 static const char *const names[] = {"x32", "32 bit"};
331 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
332 tcp->pid, names[personality]);
333 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000334# elif defined(AARCH64)
335 if (!qflag) {
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100336 static const char *const names[] = {"32-bit", "AArch64"};
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000337 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
338 tcp->pid, names[personality]);
339 }
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100340# elif defined(TILE)
341 if (!qflag) {
342 static const char *const names[] = {"64-bit", "32-bit"};
343 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
344 tcp->pid, names[personality]);
345 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100346# endif
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000347}
348#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000349
Denys Vlasenkoc1540fe2013-02-21 16:17:08 +0100350static int qual_syscall(), qual_signal(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000351
Roland McGrathe10e62a2004-09-04 04:20:43 +0000352static const struct qual_options {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000353 unsigned int bitflag;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000354 const char *option_name;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000355 int (*qualify)(const char *, int, int);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000356 const char *argument_name;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000357} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000358 { QUAL_TRACE, "trace", qual_syscall, "system call" },
359 { QUAL_TRACE, "t", qual_syscall, "system call" },
360 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
361 { QUAL_ABBREV, "a", qual_syscall, "system call" },
362 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
363 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
364 { QUAL_RAW, "raw", qual_syscall, "system call" },
365 { QUAL_RAW, "x", qual_syscall, "system call" },
366 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
367 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
368 { QUAL_SIGNAL, "s", qual_signal, "signal" },
Roland McGrath9797ceb2002-12-30 10:23:00 +0000369 { QUAL_READ, "read", qual_desc, "descriptor" },
370 { QUAL_READ, "reads", qual_desc, "descriptor" },
371 { QUAL_READ, "r", qual_desc, "descriptor" },
372 { QUAL_WRITE, "write", qual_desc, "descriptor" },
373 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
374 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000375 { 0, NULL, NULL, NULL },
376};
377
Roland McGrath9797ceb2002-12-30 10:23:00 +0000378static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000379reallocate_qual(const unsigned int n)
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100380{
381 unsigned p;
382 qualbits_t *qp;
383 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
384 qp = qual_vec[p] = realloc(qual_vec[p], n * sizeof(qualbits_t));
385 if (!qp)
386 die_out_of_memory();
387 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
388 }
389 num_quals = n;
390}
391
392static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000393qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000394{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000395 int p;
Roland McGrath138c6a32006-01-12 09:50:49 +0000396
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100397 if (num_quals <= n)
398 reallocate_qual(n + 1);
Roland McGrath138c6a32006-01-12 09:50:49 +0000399
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100400 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
401 if (pers == p || pers < 0) {
402 if (not)
403 qual_vec[p][n] &= ~bitflag;
404 else
405 qual_vec[p][n] |= bitflag;
406 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000407 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000408}
409
410static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000411qual_syscall(const char *s, const unsigned int bitflag, const int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000412{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000413 int p;
414 unsigned int i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000415 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000416
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100417 if (*s >= '0' && *s <= '9') {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100418 i = string_to_uint(s);
Denys Vlasenkob43dacd2013-02-23 18:19:28 +0100419 if (i >= MAX_NSYSCALLS)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000420 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000421 qualify_one(i, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000422 return 0;
Roland McGrath48a035f2006-01-12 09:45:56 +0000423 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000424
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100425 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
426 for (i = 0; i < nsyscall_vec[p]; i++) {
427 if (sysent_vec[p][i].sys_name
428 && strcmp(s, sysent_vec[p][i].sys_name) == 0
429 ) {
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000430 qualify_one(i, bitflag, not, p);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100431 rc = 0;
432 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000433 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100434 }
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000435
Roland McGrathfe6b3522005-02-02 04:40:11 +0000436 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000437}
438
439static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000440qual_signal(const char *s, const unsigned int bitflag, const int not)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000441{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000442 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000443
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100444 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000445 int signo = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100446 if (signo < 0 || signo > 255)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000447 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000448 qualify_one(signo, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000449 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000450 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000451 if (strncasecmp(s, "SIG", 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000452 s += 3;
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100453 for (i = 0; i <= NSIG; i++) {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000454 if (strcasecmp(s, signame(i) + 3) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000455 qualify_one(i, bitflag, not, -1);
Roland McGrath76421df2005-02-02 03:51:18 +0000456 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000457 }
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100458 }
Roland McGrath76421df2005-02-02 03:51:18 +0000459 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000460}
461
462static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000463qual_desc(const char *s, const unsigned int bitflag, const int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000464{
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100465 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000466 int desc = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100467 if (desc < 0 || desc > 0x7fff) /* paranoia */
Roland McGrathfe6b3522005-02-02 04:40:11 +0000468 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000469 qualify_one(desc, bitflag, not, -1);
Roland McGrath2b619022003-04-10 18:58:20 +0000470 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000471 }
472 return -1;
473}
474
475static int
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000476lookup_class(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000477{
478 if (strcmp(s, "file") == 0)
479 return TRACE_FILE;
480 if (strcmp(s, "ipc") == 0)
481 return TRACE_IPC;
482 if (strcmp(s, "network") == 0)
483 return TRACE_NETWORK;
484 if (strcmp(s, "process") == 0)
485 return TRACE_PROCESS;
486 if (strcmp(s, "signal") == 0)
487 return TRACE_SIGNAL;
Roland McGrath2fe7b132005-07-05 03:25:35 +0000488 if (strcmp(s, "desc") == 0)
489 return TRACE_DESC;
Namhyung Kim96792962012-10-24 11:41:57 +0900490 if (strcmp(s, "memory") == 0)
491 return TRACE_MEMORY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000492 return -1;
493}
494
495void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000496qualify(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000497{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000498 const struct qual_options *opt;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000499 char *copy;
500 const char *p;
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000501 int not;
502 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000503
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100504 if (num_quals == 0)
505 reallocate_qual(MIN_QUALS);
506
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000507 opt = &qual_options[0];
508 for (i = 0; (p = qual_options[i].option_name); i++) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000509 unsigned int len = strlen(p);
510 if (strncmp(s, p, len) == 0 && s[len] == '=') {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000511 opt = &qual_options[i];
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000512 s += len + 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000513 break;
514 }
515 }
516 not = 0;
517 if (*s == '!') {
518 not = 1;
519 s++;
520 }
521 if (strcmp(s, "none") == 0) {
522 not = 1 - not;
523 s = "all";
524 }
525 if (strcmp(s, "all") == 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100526 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000527 qualify_one(i, opt->bitflag, not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000528 }
529 return;
530 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100531 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000532 qualify_one(i, opt->bitflag, !not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000533 }
Denys Vlasenko5d645812011-08-20 12:48:18 +0200534 copy = strdup(s);
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200535 if (!copy)
536 die_out_of_memory();
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000537 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000538 int n;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000539 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100540 unsigned pers;
541 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
542 for (i = 0; i < nsyscall_vec[pers]; i++)
543 if (sysent_vec[pers][i].sys_flags & n)
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000544 qualify_one(i, opt->bitflag, not, pers);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100545 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000546 continue;
547 }
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000548 if (opt->qualify(p, opt->bitflag, not)) {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100549 error_msg_and_die("invalid %s '%s'",
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000550 opt->argument_name, p);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000551 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000552 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000553 free(copy);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000554 return;
555}
556
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000557#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000558static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000559decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000560{
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000561 unsigned long addr;
Dmitry V. Levinc2155692015-03-22 15:52:40 +0000562 unsigned int n;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000563
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000564 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
565 return;
566
567 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100568 tcp->qual_flg = qual_flags[tcp->scno];
569 tcp->s_ent = &sysent[tcp->scno];
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000570 addr = tcp->u_arg[1];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100571 n = tcp->s_ent->nargs;
Dmitry V. Levinc2155692015-03-22 15:52:40 +0000572 if (sizeof(tcp->u_arg[0]) == current_wordsize) {
573 memset(tcp->u_arg, 0, n * sizeof(tcp->u_arg[0]));
574 (void) umoven(tcp, addr, n * sizeof(tcp->u_arg[0]), tcp->u_arg);
575 } else {
576 unsigned int args[n];
577 unsigned int i;
578
579 memset(args, 0, sizeof(args));
580 (void) umove(tcp, addr, &args);
581 for (i = 0; i < n; ++i)
582 tcp->u_arg[i] = args[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000583 }
584}
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000585#endif
Mike Frysinger3362e892012-03-15 01:09:19 -0400586
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000587#ifdef SYS_ipc_subcall
588static void
589decode_ipc_subcall(struct tcb *tcp)
590{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100591 unsigned int i, n;
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000592
593 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
594 return;
595
596 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100597 tcp->qual_flg = qual_flags[tcp->scno];
598 tcp->s_ent = &sysent[tcp->scno];
599 n = tcp->s_ent->nargs;
600 for (i = 0; i < n; i++)
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000601 tcp->u_arg[i] = tcp->u_arg[i + 1];
602}
603#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000604
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200605int
606printargs(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000607{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200608 if (entering(tcp)) {
609 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100610 int n = tcp->s_ent->nargs;
611 for (i = 0; i < n; i++)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200612 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
613 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000614 return 0;
615}
616
Denys Vlasenko72879c62012-02-27 14:18:02 +0100617int
618printargs_lu(struct tcb *tcp)
619{
620 if (entering(tcp)) {
621 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100622 int n = tcp->s_ent->nargs;
623 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100624 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
625 }
626 return 0;
627}
628
629int
630printargs_ld(struct tcb *tcp)
631{
632 if (entering(tcp)) {
633 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100634 int n = tcp->s_ent->nargs;
635 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100636 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
637 }
638 return 0;
639}
640
Denys Vlasenko523635f2012-02-25 02:44:25 +0100641#if defined(I386)
Denys Vlasenkob51f3642013-07-16 12:06:25 +0200642static struct user_regs_struct i386_regs;
Dmitry V. Levinf97a4772015-02-15 00:08:11 +0000643long *const i386_esp_ptr = &i386_regs.esp;
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +0000644# define ARCH_REGS_FOR_GETREGS i386_regs
H.J. Lu35be5812012-04-16 13:00:01 +0200645#elif defined(X86_64) || defined(X32)
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100646/*
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100647 * On i386, pt_regs and user_regs_struct are the same,
648 * but on 64 bit x86, user_regs_struct has six more fields:
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100649 * fs_base, gs_base, ds, es, fs, gs.
650 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
651 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100652struct i386_user_regs_struct {
653 uint32_t ebx;
654 uint32_t ecx;
655 uint32_t edx;
656 uint32_t esi;
657 uint32_t edi;
658 uint32_t ebp;
659 uint32_t eax;
660 uint32_t xds;
661 uint32_t xes;
662 uint32_t xfs;
663 uint32_t xgs;
664 uint32_t orig_eax;
665 uint32_t eip;
666 uint32_t xcs;
667 uint32_t eflags;
668 uint32_t esp;
669 uint32_t xss;
670};
671static union {
672 struct user_regs_struct x86_64_r;
673 struct i386_user_regs_struct i386_r;
674} x86_regs_union;
675# define x86_64_regs x86_regs_union.x86_64_r
676# define i386_regs x86_regs_union.i386_r
Denys Vlasenkob51f3642013-07-16 12:06:25 +0200677uint32_t *const i386_esp_ptr = &i386_regs.esp;
Dmitry V. Levinccb4fda2015-03-04 12:19:55 +0000678uint64_t *const x86_64_rsp_ptr = (uint64_t *) &x86_64_regs.rsp;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100679static struct iovec x86_io = {
680 .iov_base = &x86_regs_union
681};
Dmitry V. Levind6db1db2015-02-13 23:41:04 +0000682# define ARCH_REGS_FOR_GETREGSET x86_regs_union
683# define ARCH_IOVEC_FOR_GETREGSET x86_io
Denys Vlasenko523635f2012-02-25 02:44:25 +0100684#elif defined(IA64)
Dmitry V. Levin48f08902015-03-05 23:30:02 +0000685static struct pt_all_user_regs ia64_regs;
686unsigned long *const ia64_frame_ptr = &ia64_regs.gr[12];
687# define IA64_PSR_IS ((long)1 << 34)
688# define ia64_ia32mode (ia64_regs.cr_ipsr & IA64_PSR_IS)
689# define ARCH_REGS_FOR_GETREGS ia64_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +0100690#elif defined(POWERPC)
Dmitry V. Levin23ce9e42015-02-08 13:05:53 +0000691struct pt_regs ppc_regs; /* not static */
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +0000692# define ARCH_REGS_FOR_GETREGS ppc_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +0100693#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100694static long m68k_d0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100695#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100696static long bfin_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100697#elif defined(ARM)
Dmitry V. Levin5b9b7e12015-03-04 23:05:53 +0000698static struct pt_regs arm_regs;
699long *const arm_sp_ptr = &arm_regs.ARM_sp;
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +0000700# define ARCH_REGS_FOR_GETREGS arm_regs
Steve McIntyred8d3bd32012-10-24 17:58:16 +0100701#elif defined(AARCH64)
Dmitry V. Levin5503dd22015-02-13 02:12:14 +0000702struct arm_pt_regs {
703 int uregs[18];
704};
705# define ARM_cpsr uregs[16]
706# define ARM_pc uregs[15]
707# define ARM_lr uregs[14]
708# define ARM_sp uregs[13]
709# define ARM_ip uregs[12]
710# define ARM_fp uregs[11]
711# define ARM_r10 uregs[10]
712# define ARM_r9 uregs[9]
713# define ARM_r8 uregs[8]
714# define ARM_r7 uregs[7]
715# define ARM_r6 uregs[6]
716# define ARM_r5 uregs[5]
717# define ARM_r4 uregs[4]
718# define ARM_r3 uregs[3]
719# define ARM_r2 uregs[2]
720# define ARM_r1 uregs[1]
721# define ARM_r0 uregs[0]
722# define ARM_ORIG_r0 uregs[17]
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100723static union {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100724 struct user_pt_regs aarch64_r;
725 struct arm_pt_regs arm_r;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100726} arm_regs_union;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100727# define aarch64_regs arm_regs_union.aarch64_r
728# define arm_regs arm_regs_union.arm_r
Dmitry V. Levin47e289b2015-03-05 04:10:52 +0000729uint64_t *const aarch64_sp_ptr = (uint64_t *) &aarch64_regs.sp;
730uint32_t *const arm_sp_ptr = (uint32_t *) &arm_regs.ARM_sp;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100731static struct iovec aarch64_io = {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100732 .iov_base = &arm_regs_union
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100733};
Dmitry V. Levind6db1db2015-02-13 23:41:04 +0000734# define ARCH_REGS_FOR_GETREGSET arm_regs_union
735# define ARCH_IOVEC_FOR_GETREGSET aarch64_io
Denys Vlasenko523635f2012-02-25 02:44:25 +0100736#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100737static long alpha_r0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100738static long alpha_a3;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100739#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100740static struct pt_regs avr32_regs;
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +0000741# define ARCH_REGS_FOR_GETREGS avr32_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +0100742#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100743struct pt_regs sparc_regs; /* not static */
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +0000744# define ARCH_REGS_FOR_GETREGS sparc_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +0100745#elif defined(MIPS)
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +0000746struct mips_regs mips_regs; /* not static */
747/* PTRACE_GETREGS on MIPS is available since linux v2.6.15. */
748# define ARCH_REGS_FOR_GETREGS mips_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +0100749#elif defined(S390) || defined(S390X)
Dmitry V. Levin20eca8a2015-03-05 22:10:15 +0000750/* PTRACE_GETREGSET on S390 is available since linux v2.6.27. */
751static struct user_regs_struct s390_regset;
752unsigned long *const s390_frame_ptr = &s390_regset.gprs[15];
753# define ARCH_REGS_FOR_GETREGSET s390_regset
Denys Vlasenko523635f2012-02-25 02:44:25 +0100754#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100755static long hppa_r28;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100756#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100757static long sh_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100758#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100759static long sh64_r9;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100760#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100761static long cris_r10;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100762#elif defined(TILE)
Dmitry V. Levin23ce9e42015-02-08 13:05:53 +0000763struct pt_regs tile_regs; /* not static */
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +0000764# define ARCH_REGS_FOR_GETREGS tile_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +0100765#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100766static long microblaze_r3;
Christian Svensson492f81f2013-02-14 13:26:27 +0100767#elif defined(OR1K)
768static struct user_regs_struct or1k_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000769# define ARCH_REGS_FOR_GETREGSET or1k_regs
James Hogan5f999a82013-02-22 14:44:10 +0000770#elif defined(METAG)
771static struct user_gp_regs metag_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000772# define ARCH_REGS_FOR_GETREGSET metag_regs
Chris Zankel8f636ed2013-03-25 10:22:07 -0700773#elif defined(XTENSA)
774static long xtensa_a2;
Vineet Gupta7daacbb2013-08-16 12:47:06 +0530775# elif defined(ARC)
776static struct user_regs_struct arc_regs;
777# define ARCH_REGS_FOR_GETREGSET arc_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +0100778#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000779
Dmitry V. Levin9a176c92015-02-13 21:56:50 +0000780static long get_regs_error;
781
Dmitry V. Levin48f08902015-03-05 23:30:02 +0000782#if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
783long
784getrval2(struct tcb *tcp)
785{
786 long val;
787
788# if defined(SPARC) || defined(SPARC64)
789 val = sparc_regs.u_regs[U_REG_O1];
790# elif defined(SH)
791 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
792 return -1;
793# elif defined(IA64)
794 val = ia64_regs.gr[9];
795# endif
796
797 return val;
798}
799#endif
800
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100801void
Denys Vlasenko5a2483b2013-07-01 12:49:14 +0200802print_pc(struct tcb *tcp)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100803{
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000804 const char *fmt;
805 const char *bad;
806
Dmitry V. Levinb2d9ff22015-02-23 13:43:20 +0000807#ifdef current_wordsize
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000808# define pc_wordsize current_wordsize
Dmitry V. Levinb2d9ff22015-02-23 13:43:20 +0000809#else
810# define pc_wordsize personality_wordsize[tcp->currpers]
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000811#endif
812
813 if (pc_wordsize == 4) {
814 fmt = "[%08lx] ";
815 bad = "[????????] ";
816 } else {
817 fmt = "[%016lx] ";
818 bad = "[????????????????] ";
819 }
820
821#undef pc_wordsize
822#define PRINTBADPC tprints(bad)
823
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100824 if (get_regs_error) {
825 PRINTBADPC;
826 return;
827 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000828
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100829#if defined(I386)
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000830 tprintf(fmt, i386_regs.eip);
831#elif defined(X86_64) || defined(X32)
832 if (x86_io.iov_len == sizeof(i386_regs))
833 tprintf(fmt, (unsigned long) i386_regs.eip);
834 else
835 tprintf(fmt, (unsigned long) x86_64_regs.rip);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100836#elif defined(S390) || defined(S390X)
Dmitry V. Levin20eca8a2015-03-05 22:10:15 +0000837 tprintf(fmt, s390_regset.psw.addr);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100838#elif defined(IA64)
Dmitry V. Levin48f08902015-03-05 23:30:02 +0000839 tprintf(fmt, ia64_regs.br[0]);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100840#elif defined(POWERPC)
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000841 tprintf(fmt, ppc_regs.nip);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100842#elif defined(M68K)
843 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200844 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000845 PRINTBADPC;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100846 return;
847 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000848 tprintf(fmt, pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100849#elif defined(ALPHA)
850 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200851 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000852 PRINTBADPC;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100853 return;
854 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000855 tprintf(fmt, pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100856#elif defined(SPARC)
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000857 tprintf(fmt, sparc_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100858#elif defined(SPARC64)
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000859 tprintf(fmt, sparc_regs.tpc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100860#elif defined(HPPA)
861 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200862 if (upeek(tcp->pid, PT_IAOQ0, &pc) < 0) {
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000863 PRINTBADPC;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100864 return;
865 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000866 tprintf(fmt, pc);
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +0000867#elif defined MIPS
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000868 tprintf(fmt, (unsigned long) mips_REG_EPC);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100869#elif defined(SH)
870 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200871 if (upeek(tcp->pid, 4*REG_PC, &pc) < 0) {
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000872 PRINTBADPC;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100873 return;
874 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000875 tprintf(fmt, pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100876#elif defined(SH64)
877 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200878 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000879 PRINTBADPC;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100880 return;
881 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000882 tprintf(fmt, pc);
Dmitry V. Levind819fe82015-03-02 03:38:27 +0000883#elif defined(AARCH64)
884 if (aarch64_io.iov_len == sizeof(arm_regs))
885 tprintf(fmt, (unsigned long) arm_regs.ARM_pc);
886 else
887 tprintf(fmt, (unsigned long) aarch64_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100888#elif defined(ARM)
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000889 tprintf(fmt, arm_regs.ARM_pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100890#elif defined(AVR32)
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000891 tprintf(fmt, avr32_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100892#elif defined(BFIN)
893 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200894 if (upeek(tcp->pid, PT_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100895 PRINTBADPC;
896 return;
897 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000898 tprintf(fmt, pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100899#elif defined(CRISV10)
900 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200901 if (upeek(tcp->pid, 4*PT_IRP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100902 PRINTBADPC;
903 return;
904 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000905 tprintf(fmt, pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100906#elif defined(CRISV32)
907 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200908 if (upeek(tcp->pid, 4*PT_ERP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100909 PRINTBADPC;
910 return;
911 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000912 tprintf(fmt, pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100913#elif defined(TILE)
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000914 tprintf(fmt, (unsigned long) tile_regs.pc);
Christian Svensson492f81f2013-02-14 13:26:27 +0100915#elif defined(OR1K)
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000916 tprintf(fmt, or1k_regs.pc);
James Hogan5f999a82013-02-22 14:44:10 +0000917#elif defined(METAG)
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000918 tprintf(fmt, metag_regs.pc);
Chris Zankel8f636ed2013-03-25 10:22:07 -0700919#elif defined(XTENSA)
920 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200921 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Chris Zankel8f636ed2013-03-25 10:22:07 -0700922 PRINTBADPC;
923 return;
924 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000925 tprintf(fmt, pc);
Vineet Gupta7daacbb2013-08-16 12:47:06 +0530926#elif defined(ARC)
Dmitry V. Levine96cb622015-02-15 15:52:02 +0000927 tprintf(fmt, arc_regs.efa);
928#else
929# warning print_pc is not implemented for this architecture
930 PRINTBADPC;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100931#endif /* architecture */
932}
933
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000934/*
935 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
936 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
Denys Vlasenko7270de52013-02-21 15:46:34 +0100937 */
Elliott Hughes7a28f7f2014-03-03 23:45:26 +0000938#if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
Denys Vlasenko7270de52013-02-21 15:46:34 +0100939static long
940shuffle_scno(unsigned long scno)
941{
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000942 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
Denys Vlasenko7270de52013-02-21 15:46:34 +0100943 return scno;
944
945 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000946 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
Denys Vlasenko7270de52013-02-21 15:46:34 +0100947 return 0x000ffff0;
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000948 if (scno == 0x000ffff0)
949 return ARM_FIRST_SHUFFLED_SYSCALL;
Denys Vlasenko7270de52013-02-21 15:46:34 +0100950
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000951#define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
952 /*
953 * Is it ARM specific syscall?
954 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
955 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
Denys Vlasenko7270de52013-02-21 15:46:34 +0100956 */
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000957 if (scno >= 0x000f0000 &&
958 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
959 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
Denys Vlasenko7270de52013-02-21 15:46:34 +0100960 }
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000961 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
962 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
Denys Vlasenko7270de52013-02-21 15:46:34 +0100963 }
964
965 return scno;
966}
967#else
968# define shuffle_scno(scno) ((long)(scno))
969#endif
970
971static char*
972undefined_scno_name(struct tcb *tcp)
973{
974 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
975
976 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
977 return buf;
978}
979
Anton Blanchard14d51a62013-06-26 14:42:37 +1000980#ifdef POWERPC
981/*
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200982 * PTRACE_GETREGS was added to the PowerPC kernel in v2.6.23,
983 * we provide a slow fallback for old kernels.
Anton Blanchard14d51a62013-06-26 14:42:37 +1000984 */
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200985static int powerpc_getregs_old(pid_t pid)
Anton Blanchard14d51a62013-06-26 14:42:37 +1000986{
987 int i;
988 long r;
989
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200990 if (iflag) {
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200991 r = upeek(pid, sizeof(long) * PT_NIP, (long *)&ppc_regs.nip);
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200992 if (r)
993 goto out;
994 }
995#ifdef POWERPC64 /* else we never use it */
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200996 r = upeek(pid, sizeof(long) * PT_MSR, (long *)&ppc_regs.msr);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000997 if (r)
998 goto out;
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200999#endif
Anton Blanchard9459dfb2013-07-12 12:24:02 +02001000 r = upeek(pid, sizeof(long) * PT_CCR, (long *)&ppc_regs.ccr);
Anton Blanchard14d51a62013-06-26 14:42:37 +10001001 if (r)
1002 goto out;
Anton Blanchard9459dfb2013-07-12 12:24:02 +02001003 r = upeek(pid, sizeof(long) * PT_ORIG_R3, (long *)&ppc_regs.orig_gpr3);
Anton Blanchard14d51a62013-06-26 14:42:37 +10001004 if (r)
1005 goto out;
Anton Blanchard14d51a62013-06-26 14:42:37 +10001006 for (i = 0; i <= 8; i++) {
Anton Blanchard9459dfb2013-07-12 12:24:02 +02001007 r = upeek(pid, sizeof(long) * (PT_R0 + i),
1008 (long *)&ppc_regs.gpr[i]);
Anton Blanchard14d51a62013-06-26 14:42:37 +10001009 if (r)
1010 goto out;
1011 }
Denys Vlasenko7f5a1322013-06-28 14:36:39 +02001012 out:
Anton Blanchard14d51a62013-06-26 14:42:37 +10001013 return r;
1014}
1015#endif
1016
Dmitry V. Levin9a176c92015-02-13 21:56:50 +00001017void
1018clear_regs(void)
1019{
1020 get_regs_error = -1;
1021}
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001022
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001023#if defined ARCH_REGS_FOR_GETREGSET
1024static long
1025get_regset(pid_t pid)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001026{
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001027# ifdef ARCH_IOVEC_FOR_GETREGSET
1028 /* variable iovec */
1029 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1030 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1031 &ARCH_IOVEC_FOR_GETREGSET);
1032# else
1033 /* constant iovec */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001034 static struct iovec io = {
1035 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1036 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001037 };
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001038 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001039
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001040# endif
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001041}
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001042#endif /* ARCH_REGS_FOR_GETREGSET */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001043
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001044void
1045get_regs(pid_t pid)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001046{
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001047#ifdef ARCH_REGS_FOR_GETREGSET
1048# ifdef X86_64
1049 /* Try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS. */
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001050 static int getregset_support;
1051
1052 if (getregset_support >= 0) {
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001053 get_regs_error = get_regset(pid);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001054 if (getregset_support > 0)
1055 return;
1056 if (get_regs_error >= 0) {
1057 getregset_support = 1;
1058 return;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001059 }
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001060 if (errno == EPERM || errno == ESRCH)
1061 return;
1062 getregset_support = -1;
1063 }
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001064 /* Use old method, with unreliable heuristical detection of 32-bitness. */
1065 x86_io.iov_len = sizeof(x86_64_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001066 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &x86_64_regs);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001067 if (!get_regs_error && x86_64_regs.cs == 0x23) {
1068 x86_io.iov_len = sizeof(i386_regs);
1069 /*
1070 * The order is important: i386_regs and x86_64_regs
1071 * are overlaid in memory!
1072 */
1073 i386_regs.ebx = x86_64_regs.rbx;
1074 i386_regs.ecx = x86_64_regs.rcx;
1075 i386_regs.edx = x86_64_regs.rdx;
1076 i386_regs.esi = x86_64_regs.rsi;
1077 i386_regs.edi = x86_64_regs.rdi;
1078 i386_regs.ebp = x86_64_regs.rbp;
1079 i386_regs.eax = x86_64_regs.rax;
1080 /* i386_regs.xds = x86_64_regs.ds; unused by strace */
1081 /* i386_regs.xes = x86_64_regs.es; ditto... */
1082 /* i386_regs.xfs = x86_64_regs.fs; */
1083 /* i386_regs.xgs = x86_64_regs.gs; */
1084 i386_regs.orig_eax = x86_64_regs.orig_rax;
1085 i386_regs.eip = x86_64_regs.rip;
1086 /* i386_regs.xcs = x86_64_regs.cs; */
1087 /* i386_regs.eflags = x86_64_regs.eflags; */
1088 i386_regs.esp = x86_64_regs.rsp;
1089 /* i386_regs.xss = x86_64_regs.ss; */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001090 }
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001091# else /* !X86_64 */
1092 /* Assume that PTRACE_GETREGSET works. */
1093 get_regs_error = get_regset(pid);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001094# endif
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001095#elif defined ARCH_REGS_FOR_GETREGS
1096# if defined SPARC || defined SPARC64
1097 /* SPARC systems have the meaning of data and addr reversed */
1098 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&ARCH_REGS_FOR_GETREGS, 0);
1099# elif defined POWERPC
1100 static bool old_kernel = 0;
1101 if (old_kernel)
1102 goto old;
1103 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1104 if (get_regs_error && errno == EIO) {
1105 old_kernel = 1;
1106 old:
1107 get_regs_error = powerpc_getregs_old(pid);
1108 }
1109# else
1110 /* Assume that PTRACE_GETREGS works. */
1111 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1112# endif
1113
1114#else /* !ARCH_REGS_FOR_GETREGSET && !ARCH_REGS_FOR_GETREGS */
1115# warning get_regs is not implemented for this architecture yet
1116 get_regs_error = 0;
1117#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001118}
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001119
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001120/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001121 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1122 * 1: ok, continue in trace_syscall_entering().
1123 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001124 * ("????" etc) and bail out.
1125 */
Denys Vlasenko8497b622015-03-21 17:51:52 +01001126int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001127get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001128{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001129 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001130
Denys Vlasenko523635f2012-02-25 02:44:25 +01001131#if defined(S390) || defined(S390X)
Dmitry V. Levin20eca8a2015-03-05 22:10:15 +00001132 scno = s390_regset.gprs[2];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001133#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001134 scno = ppc_regs.gpr[0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001135# ifdef POWERPC64
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001136 unsigned int currpers;
Andreas Schwabd69fa492010-07-12 21:39:57 +02001137
James Yanga78d7772014-02-18 15:32:43 -06001138 /*
1139 * Check for 64/32 bit mode.
1140 * Embedded implementations covered by Book E extension of PPC use
1141 * bit 0 (CM) of 32-bit Machine state register (MSR).
1142 * Other implementations use bit 0 (SF) of 64-bit MSR.
1143 */
1144 currpers = (ppc_regs.msr & 0x8000000080000000) ? 0 : 1;
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001145 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001146# endif
1147#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001148 scno = avr32_regs.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001149#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001150 if (upeek(tcp->pid, PT_ORIG_P0, &scno))
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001151 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001152#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001153 scno = i386_regs.orig_eax;
H.J. Lu35be5812012-04-16 13:00:01 +02001154#elif defined(X86_64) || defined(X32)
1155# ifndef __X32_SYSCALL_BIT
1156# define __X32_SYSCALL_BIT 0x40000000
1157# endif
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001158 unsigned int currpers;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001159# if 1
1160 /* GETREGSET of NT_PRSTATUS tells us regset size,
1161 * which unambiguously detects i386.
1162 *
1163 * Linux kernel distinguishes x86-64 and x32 processes
1164 * solely by looking at __X32_SYSCALL_BIT:
1165 * arch/x86/include/asm/compat.h::is_x32_task():
1166 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1167 * return true;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001168 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001169 if (x86_io.iov_len == sizeof(i386_regs)) {
1170 scno = i386_regs.orig_eax;
1171 currpers = 1;
1172 } else {
1173 scno = x86_64_regs.orig_rax;
1174 currpers = 0;
1175 if (scno & __X32_SYSCALL_BIT) {
Dmitry V. Levinc3948322015-02-05 16:15:46 +00001176 /*
1177 * Syscall number -1 requires special treatment:
1178 * it might be a side effect of SECCOMP_RET_ERRNO
1179 * filtering that sets orig_rax to -1
1180 * in some versions of linux kernel.
1181 * If that is the case, then
1182 * __X32_SYSCALL_BIT logic does not apply.
1183 */
1184 if ((long long) x86_64_regs.orig_rax != -1) {
1185 scno -= __X32_SYSCALL_BIT;
1186 currpers = 2;
1187 } else {
1188# ifdef X32
1189 currpers = 2;
1190# endif
1191 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001192 }
1193 }
1194# elif 0
1195 /* cs = 0x33 for long mode (native 64 bit and x32)
1196 * cs = 0x23 for compatibility mode (32 bit)
1197 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1198 */
1199 scno = x86_64_regs.orig_rax;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001200 switch (x86_64_regs.cs) {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001201 case 0x23: currpers = 1; break;
H.J. Lu35be5812012-04-16 13:00:01 +02001202 case 0x33:
1203 if (x86_64_regs.ds == 0x2b) {
1204 currpers = 2;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001205 scno &= ~__X32_SYSCALL_BIT;
H.J. Lu35be5812012-04-16 13:00:01 +02001206 } else
1207 currpers = 0;
1208 break;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001209 default:
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001210 fprintf(stderr, "Unknown value CS=0x%08X while "
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001211 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001212 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001213 currpers = current_personality;
1214 break;
1215 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001216# elif 0
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001217 /* This version analyzes the opcode of a syscall instruction.
1218 * (int 0x80 on i386 vs. syscall on x86-64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001219 * It works, but is too complicated, and strictly speaking, unreliable.
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001220 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001221 unsigned long call, rip = x86_64_regs.rip;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001222 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1223 rip -= 2;
1224 errno = 0;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001225 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001226 if (errno)
1227 fprintf(stderr, "ptrace_peektext failed: %s\n",
1228 strerror(errno));
1229 switch (call & 0xffff) {
1230 /* x86-64: syscall = 0x0f 0x05 */
1231 case 0x050f: currpers = 0; break;
1232 /* i386: int 0x80 = 0xcd 0x80 */
1233 case 0x80cd: currpers = 1; break;
1234 default:
1235 currpers = current_personality;
1236 fprintf(stderr,
1237 "Unknown syscall opcode (0x%04X) while "
1238 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001239 "PID=%d\n", (int)call, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001240 break;
1241 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001242# endif
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001243
H.J. Lu35be5812012-04-16 13:00:01 +02001244# ifdef X32
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001245 /* If we are built for a x32 system, then personality 0 is x32
1246 * (not x86_64), and stracing of x86_64 apps is not supported.
1247 * Stracing of i386 apps is still supported.
H.J. Lu085e4282012-04-17 11:05:04 -07001248 */
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001249 if (currpers == 0) {
1250 fprintf(stderr, "syscall_%lu(...) in unsupported "
1251 "64-bit mode of process PID=%d\n",
1252 scno, tcp->pid);
1253 return 0;
H.J. Lu35be5812012-04-16 13:00:01 +02001254 }
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001255 currpers &= ~2; /* map 2,1 to 0,1 */
H.J. Lu35be5812012-04-16 13:00:01 +02001256# endif
H.J. Lu085e4282012-04-17 11:05:04 -07001257 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001258#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001259 if (ia64_ia32mode) {
Dmitry V. Levin48f08902015-03-05 23:30:02 +00001260 scno = ia64_regs.gr[0];
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001261 } else {
Dmitry V. Levin48f08902015-03-05 23:30:02 +00001262 scno = ia64_regs.gr[15];
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001263 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001264#elif defined(AARCH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001265 switch (aarch64_io.iov_len) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001266 case sizeof(aarch64_regs):
1267 /* We are in 64-bit mode */
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001268 scno = aarch64_regs.regs[8];
1269 update_personality(tcp, 1);
1270 break;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001271 case sizeof(arm_regs):
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001272 /* We are in 32-bit mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001273 /* Note: we don't support OABI, unlike 32-bit ARM build */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001274 scno = arm_regs.ARM_r7;
Elliott Hughes7a28f7f2014-03-03 23:45:26 +00001275 scno = shuffle_scno(scno);
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001276 update_personality(tcp, 0);
1277 break;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001278 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001279#elif defined(ARM)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001280 if (arm_regs.ARM_ip != 0) {
1281 /* It is not a syscall entry */
1282 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001283 tcp->flags |= TCB_INSYSCALL;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001284 return 0;
1285 }
1286 /* Note: we support only 32-bit CPUs, not 26-bit */
1287
Dmitry V. Levin0160e162014-03-18 23:37:43 +00001288# if !defined(__ARM_EABI__) || ENABLE_ARM_OABI
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001289 if (arm_regs.ARM_cpsr & 0x20)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001290 /* Thumb mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001291 goto scno_in_r7;
1292 /* ARM mode */
1293 /* Check EABI/OABI by examining SVC insn's low 24 bits */
1294 errno = 0;
1295 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1296 if (errno)
1297 return -1;
1298 /* EABI syscall convention? */
Dmitry V. Levin03b44542015-01-14 07:21:36 +00001299 if ((unsigned long) scno != 0xef000000) {
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001300 /* No, it's OABI */
1301 if ((scno & 0x0ff00000) != 0x0f900000) {
1302 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1303 tcp->pid, scno);
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001304 return -1;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001305 }
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001306 /* Fixup the syscall number */
1307 scno &= 0x000fffff;
1308 } else {
1309 scno_in_r7:
1310 scno = arm_regs.ARM_r7;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001311 }
Dmitry V. Levin0160e162014-03-18 23:37:43 +00001312# else /* __ARM_EABI__ || !ENABLE_ARM_OABI */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001313 scno = arm_regs.ARM_r7;
1314# endif
Denys Vlasenko7270de52013-02-21 15:46:34 +01001315 scno = shuffle_scno(scno);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001316#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001317 if (upeek(tcp->pid, 4*PT_ORIG_D0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001318 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001319#elif defined(MIPS)
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +00001320 scno = mips_REG_V0;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001321
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001322 if (!SCNO_IN_RANGE(scno)) {
Dmitry V. Levinf6777152015-03-02 15:06:51 +00001323 if (mips_REG_A3 == 0 || mips_REG_A3 == (uint64_t) -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001324 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001325 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001326 return 0;
1327 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001328 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001329#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001330 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001331 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001332 if (upeek(tcp->pid, REG_R0, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001333 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001334
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001335 /*
1336 * Do some sanity checks to figure out if it's
1337 * really a syscall entry
1338 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001339 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001340 if (alpha_a3 == 0 || alpha_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001341 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001342 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001343 return 0;
1344 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001345 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001346#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001347 /* Disassemble the syscall trap. */
1348 /* Retrieve the syscall trap instruction. */
Denys Vlasenko46455822013-02-05 17:02:59 +01001349 unsigned long trap;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001350 errno = 0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001351# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001352 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001353 trap >>= 32;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001354# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001355 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001356# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001357 if (errno)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001358 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001359
1360 /* Disassemble the trap to see what personality to use. */
1361 switch (trap) {
1362 case 0x91d02010:
1363 /* Linux/SPARC syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001364 update_personality(tcp, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001365 break;
1366 case 0x91d0206d:
1367 /* Linux/SPARC64 syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001368 update_personality(tcp, 2);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001369 break;
1370 case 0x91d02000:
1371 /* SunOS syscall trap. (pers 1) */
1372 fprintf(stderr, "syscall: SunOS no support\n");
1373 return -1;
1374 case 0x91d02008:
1375 /* Solaris 2.x syscall trap. (per 2) */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001376 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001377 break;
1378 case 0x91d02009:
1379 /* NetBSD/FreeBSD syscall trap. */
1380 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1381 return -1;
1382 case 0x91d02027:
1383 /* Solaris 2.x gettimeofday */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001384 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001385 break;
1386 default:
Denys Vlasenko523635f2012-02-25 02:44:25 +01001387# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001388 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001389# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001390 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001391# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001392 return -1;
1393 }
1394
1395 /* Extract the system call number from the registers. */
1396 if (trap == 0x91d02027)
1397 scno = 156;
1398 else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001399 scno = sparc_regs.u_regs[U_REG_G1];
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001400 if (scno == 0) {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001401 scno = sparc_regs.u_regs[U_REG_O0];
1402 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 +02001403 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001404#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001405 if (upeek(tcp->pid, PT_GR20, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001406 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001407#elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001408 /*
1409 * In the new syscall ABI, the system call number is in R3.
1410 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001411 if (upeek(tcp->pid, 4*(REG_REG0+3), &scno) < 0)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001412 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001413
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001414 if (scno < 0) {
1415 /* Odd as it may seem, a glibc bug has been known to cause
1416 glibc to issue bogus negative syscall numbers. So for
1417 our purposes, make strace print what it *should* have been */
1418 long correct_scno = (scno & 0xff);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001419 if (debug_flag)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001420 fprintf(stderr,
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001421 "Detected glibc bug: bogus system call"
1422 " number = %ld, correcting to %ld\n",
1423 scno,
1424 correct_scno);
1425 scno = correct_scno;
1426 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001427#elif defined(SH64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001428 if (upeek(tcp->pid, REG_SYSCALL, &scno) < 0)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001429 return -1;
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001430 scno &= 0xFFFF;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001431#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001432 if (upeek(tcp->pid, 4*PT_R9, &scno) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001433 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001434#elif defined(TILE)
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001435 unsigned int currpers;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001436 scno = tile_regs.regs[10];
1437# ifdef __tilepro__
1438 currpers = 1;
1439# else
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001440# ifndef PT_FLAGS_COMPAT
1441# define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1442# endif
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001443 if (tile_regs.flags & PT_FLAGS_COMPAT)
1444 currpers = 1;
1445 else
1446 currpers = 0;
1447# endif
1448 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001449#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001450 if (upeek(tcp->pid, 0, &scno) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001451 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001452#elif defined(OR1K)
1453 scno = or1k_regs.gpr[11];
James Hogan5f999a82013-02-22 14:44:10 +00001454#elif defined(METAG)
1455 scno = metag_regs.dx[0][1]; /* syscall number in D1Re0 (D1.0) */
Chris Zankel8f636ed2013-03-25 10:22:07 -07001456#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001457 if (upeek(tcp->pid, SYSCALL_NR, &scno) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001458 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301459# elif defined(ARC)
1460 scno = arc_regs.scratch.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001461#endif
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001462
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001463 tcp->scno = scno;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001464 if (SCNO_IS_VALID(tcp->scno)) {
1465 tcp->s_ent = &sysent[scno];
1466 tcp->qual_flg = qual_flags[scno];
1467 } else {
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01001468 static const struct_sysent unknown = {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001469 .nargs = MAX_ARGS,
1470 .sys_flags = 0,
1471 .sys_func = printargs,
1472 .sys_name = "unknown", /* not used */
1473 };
1474 tcp->s_ent = &unknown;
1475 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1476 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001477 return 1;
1478}
1479
Dmitry V. Levind70c41d2015-02-05 23:43:06 +00001480/*
1481 * Cannot rely on __kernel_[u]long_t being defined,
1482 * it is quite a recent feature of <asm/posix_types.h>.
1483 */
1484#ifdef __kernel_long_t
1485typedef __kernel_long_t kernel_long_t;
1486typedef __kernel_ulong_t kernel_ulong_t;
1487#else
1488# ifdef X32
1489typedef long long kernel_long_t;
1490typedef unsigned long long kernel_ulong_t;
1491# else
1492typedef long kernel_long_t;
1493typedef unsigned long kernel_ulong_t;
1494# endif
1495#endif
1496
1497/*
1498 * Check the syscall return value register value for whether it is
1499 * a negated errno code indicating an error, or a success return value.
1500 */
1501static inline bool
1502is_negated_errno(kernel_ulong_t val)
1503{
Dmitry V. Levinfb585db2015-02-19 16:58:52 +00001504 /* Linux kernel defines MAX_ERRNO to 4095. */
1505 kernel_ulong_t max = -(kernel_long_t) 4095;
Dmitry V. Levind70c41d2015-02-05 23:43:06 +00001506
1507#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1508 if (current_wordsize < sizeof(val)) {
1509 val = (uint32_t) val;
1510 max = (uint32_t) max;
1511 }
1512#elif defined X32
1513 /*
1514 * current_wordsize is 4 even in personality 0 (native X32)
1515 * but truncation _must not_ be done in it.
1516 * can't check current_wordsize here!
1517 */
1518 if (current_personality != 0) {
1519 val = (uint32_t) val;
1520 max = (uint32_t) max;
1521 }
1522#endif
1523
Dmitry V. Levinfb585db2015-02-19 16:58:52 +00001524 return val >= max;
Dmitry V. Levind70c41d2015-02-05 23:43:06 +00001525}
1526
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001527/* Return -1 on error or 1 on success (never 0!) */
Roland McGratha4d48532005-06-08 20:45:28 +00001528static int
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001529get_syscall_args(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001530{
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001531 int i, nargs;
1532
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001533 nargs = tcp->s_ent->nargs;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001534
Denys Vlasenko523635f2012-02-25 02:44:25 +01001535#if defined(S390) || defined(S390X)
Dmitry V. Levin20eca8a2015-03-05 22:10:15 +00001536 (void)i;
1537 (void)nargs;
1538 tcp->u_arg[0] = s390_regset.orig_gpr2;
1539 tcp->u_arg[1] = s390_regset.gprs[3];
1540 tcp->u_arg[2] = s390_regset.gprs[4];
1541 tcp->u_arg[3] = s390_regset.gprs[5];
1542 tcp->u_arg[4] = s390_regset.gprs[6];
1543 tcp->u_arg[5] = s390_regset.gprs[7];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001544#elif defined(ALPHA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001545 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001546 if (upeek(tcp->pid, REG_A0+i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001547 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001548#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001549 if (!ia64_ia32mode) {
Dmitry V. Levin48f08902015-03-05 23:30:02 +00001550 unsigned long *rbs_end =
1551 (unsigned long *) ia64_regs.ar[PT_AUR_BSP];
1552 unsigned long sof = (ia64_regs.cfm >> 0) & 0x7f;
1553 unsigned long sol = (ia64_regs.cfm >> 7) & 0x7f;
1554 unsigned long *out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001555
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001556 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001557 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
Denys Vlasenko7e69ed92015-03-21 19:50:53 +01001558 sizeof(long), &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001559 return -1;
1560 }
1561 } else {
Dmitry V. Levin48f08902015-03-05 23:30:02 +00001562 (void)i;
1563 (void)nargs;
1564 /* truncate away IVE sign-extension */
1565 tcp->u_arg[0] = 0xffffffff & ia64_regs.gr[11]; /* EBX */
1566 tcp->u_arg[1] = 0xffffffff & ia64_regs.gr[ 9]; /* ECX */
1567 tcp->u_arg[2] = 0xffffffff & ia64_regs.gr[10]; /* EDX */
1568 tcp->u_arg[3] = 0xffffffff & ia64_regs.gr[14]; /* ESI */
1569 tcp->u_arg[4] = 0xffffffff & ia64_regs.gr[15]; /* EDI */
1570 tcp->u_arg[5] = 0xffffffff & ia64_regs.gr[13]; /* EBP */
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001571 }
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +00001572#elif defined LINUX_MIPSN64
1573 (void)i;
1574 (void)nargs;
1575 tcp->u_arg[0] = mips_REG_A0;
1576 tcp->u_arg[1] = mips_REG_A1;
1577 tcp->u_arg[2] = mips_REG_A2;
1578 tcp->u_arg[3] = mips_REG_A3;
1579 tcp->u_arg[4] = mips_REG_A4;
1580 tcp->u_arg[5] = mips_REG_A5;
1581#elif defined LINUX_MIPSN32
1582 (void)i;
1583 (void)nargs;
1584 tcp->u_arg[0] = tcp->ext_arg[0] = mips_REG_A0;
1585 tcp->u_arg[1] = tcp->ext_arg[1] = mips_REG_A1;
1586 tcp->u_arg[2] = tcp->ext_arg[2] = mips_REG_A2;
1587 tcp->u_arg[3] = tcp->ext_arg[3] = mips_REG_A3;
1588 tcp->u_arg[4] = tcp->ext_arg[4] = mips_REG_A4;
1589 tcp->u_arg[5] = tcp->ext_arg[5] = mips_REG_A5;
1590#elif defined LINUX_MIPSO32
1591 (void)i;
1592 (void)nargs;
1593 tcp->u_arg[0] = mips_REG_A0;
1594 tcp->u_arg[1] = mips_REG_A1;
1595 tcp->u_arg[2] = mips_REG_A2;
1596 tcp->u_arg[3] = mips_REG_A3;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001597 if (nargs > 4) {
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +00001598 umoven(tcp, mips_REG_SP + 4 * 4,
1599 (nargs - 4) * sizeof(tcp->u_arg[0]),
Denys Vlasenko7e69ed92015-03-21 19:50:53 +01001600 &tcp->u_arg[4]);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001601 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001602#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001603 (void)i;
1604 (void)nargs;
1605 tcp->u_arg[0] = ppc_regs.orig_gpr3;
1606 tcp->u_arg[1] = ppc_regs.gpr[4];
1607 tcp->u_arg[2] = ppc_regs.gpr[5];
1608 tcp->u_arg[3] = ppc_regs.gpr[6];
1609 tcp->u_arg[4] = ppc_regs.gpr[7];
1610 tcp->u_arg[5] = ppc_regs.gpr[8];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001611#elif defined(SPARC) || defined(SPARC64)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001612 for (i = 0; i < nargs; ++i)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001613 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001614#elif defined(HPPA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001615 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001616 if (upeek(tcp->pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001617 return -1;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001618#elif defined(ARM) || defined(AARCH64)
1619# if defined(AARCH64)
1620 if (tcp->currpers == 1)
1621 for (i = 0; i < nargs; ++i)
1622 tcp->u_arg[i] = aarch64_regs.regs[i];
1623 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001624# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001625 for (i = 0; i < nargs; ++i)
Denys Vlasenko401374e2013-02-06 18:24:39 +01001626 tcp->u_arg[i] = arm_regs.uregs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001627#elif defined(AVR32)
Denys Vlasenkob5b25892011-08-30 19:04:54 +02001628 (void)i;
1629 (void)nargs;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001630 tcp->u_arg[0] = avr32_regs.r12;
1631 tcp->u_arg[1] = avr32_regs.r11;
1632 tcp->u_arg[2] = avr32_regs.r10;
1633 tcp->u_arg[3] = avr32_regs.r9;
1634 tcp->u_arg[4] = avr32_regs.r5;
1635 tcp->u_arg[5] = avr32_regs.r3;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001636#elif defined(BFIN)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001637 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 +02001638
Denys Vlasenko4b887a52011-08-23 13:32:38 +02001639 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001640 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001641 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001642#elif defined(SH)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001643 static const int syscall_regs[MAX_ARGS] = {
1644 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1645 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001646 };
1647
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001648 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001649 if (upeek(tcp->pid, syscall_regs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001650 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001651#elif defined(SH64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001652 int i;
1653 /* Registers used by SH5 Linux system calls for parameters */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001654 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001655
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001656 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001657 if (upeek(tcp->pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001658 return -1;
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001659#elif defined(I386)
1660 (void)i;
1661 (void)nargs;
1662 tcp->u_arg[0] = i386_regs.ebx;
1663 tcp->u_arg[1] = i386_regs.ecx;
1664 tcp->u_arg[2] = i386_regs.edx;
1665 tcp->u_arg[3] = i386_regs.esi;
1666 tcp->u_arg[4] = i386_regs.edi;
1667 tcp->u_arg[5] = i386_regs.ebp;
H.J. Lu35be5812012-04-16 13:00:01 +02001668#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001669 (void)i;
1670 (void)nargs;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001671 if (x86_io.iov_len != sizeof(i386_regs)) {
1672 /* x86-64 or x32 ABI */
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001673 tcp->u_arg[0] = x86_64_regs.rdi;
1674 tcp->u_arg[1] = x86_64_regs.rsi;
1675 tcp->u_arg[2] = x86_64_regs.rdx;
1676 tcp->u_arg[3] = x86_64_regs.r10;
1677 tcp->u_arg[4] = x86_64_regs.r8;
1678 tcp->u_arg[5] = x86_64_regs.r9;
H.J. Lu35be5812012-04-16 13:00:01 +02001679# ifdef X32
1680 tcp->ext_arg[0] = x86_64_regs.rdi;
1681 tcp->ext_arg[1] = x86_64_regs.rsi;
1682 tcp->ext_arg[2] = x86_64_regs.rdx;
1683 tcp->ext_arg[3] = x86_64_regs.r10;
1684 tcp->ext_arg[4] = x86_64_regs.r8;
1685 tcp->ext_arg[5] = x86_64_regs.r9;
1686# endif
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001687 } else {
1688 /* i386 ABI */
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001689 /* Zero-extend from 32 bits */
1690 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1691 * if you need to use *sign-extended* parameter.
1692 */
1693 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1694 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1695 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1696 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1697 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1698 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001699 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001700#elif defined(MICROBLAZE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001701 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001702 if (upeek(tcp->pid, (5 + i) * 4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001703 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001704#elif defined(CRISV10) || defined(CRISV32)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001705 static const int crisregs[MAX_ARGS] = {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001706 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001707 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001708 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001709
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001710 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001711 if (upeek(tcp->pid, crisregs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001712 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001713#elif defined(TILE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001714 for (i = 0; i < nargs; ++i)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001715 tcp->u_arg[i] = tile_regs.regs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001716#elif defined(M68K)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001717 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001718 if (upeek(tcp->pid, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001719 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001720#elif defined(OR1K)
1721 (void)nargs;
1722 for (i = 0; i < 6; ++i)
1723 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
James Hogan5f999a82013-02-22 14:44:10 +00001724#elif defined(METAG)
1725 for (i = 0; i < nargs; i++)
1726 /* arguments go backwards from D1Ar1 (D1.3) */
1727 tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
Chris Zankel8f636ed2013-03-25 10:22:07 -07001728#elif defined(XTENSA)
1729 /* arg0: a6, arg1: a3, arg2: a4, arg3: a5, arg4: a8, arg5: a9 */
1730 static const int xtensaregs[MAX_ARGS] = { 6, 3, 4, 5, 8, 9 };
1731 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001732 if (upeek(tcp->pid, REG_A_BASE + xtensaregs[i], &tcp->u_arg[i]) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001733 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301734# elif defined(ARC)
1735 long *arc_args = &arc_regs.scratch.r0;
1736 for (i = 0; i < nargs; ++i)
1737 tcp->u_arg[i] = *arc_args--;
1738
Denys Vlasenko523635f2012-02-25 02:44:25 +01001739#else /* Other architecture (32bits specific) */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001740 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001741 if (upeek(tcp->pid, i*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001742 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001743#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001744 return 1;
1745}
1746
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00001747static int
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001748trace_syscall_entering(struct tcb *tcp)
1749{
1750 int res, scno_good;
1751
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001752 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001753 if (res == 0)
1754 return res;
Dmitry V. Levin23ce9e42015-02-08 13:05:53 +00001755 if (res == 1)
1756 res = get_syscall_args(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001757
1758 if (res != 1) {
1759 printleader(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001760 if (scno_good != 1)
Denys Vlasenkob7a6dae2012-03-20 16:48:35 +01001761 tprints("????" /* anti-trigraph gap */ "(");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001762 else if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01001763 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001764 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001765 tprintf("%s(", tcp->s_ent->sys_name);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001766 /*
1767 * " <unavailable>" will be added later by the code which
1768 * detects ptrace errors.
1769 */
1770 goto ret;
1771 }
1772
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02001773 if ( sys_execve == tcp->s_ent->sys_func
1774# if defined(SPARC) || defined(SPARC64)
1775 || sys_execv == tcp->s_ent->sys_func
1776# endif
1777 ) {
1778 hide_log_until_execve = 0;
1779 }
1780
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001781#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001782 while (1) {
Denys Vlasenko523635f2012-02-25 02:44:25 +01001783# ifdef SYS_socket_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001784 if (tcp->s_ent->sys_func == sys_socketcall) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00001785 decode_socket_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001786 break;
1787 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001788# endif
1789# ifdef SYS_ipc_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001790 if (tcp->s_ent->sys_func == sys_ipc) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00001791 decode_ipc_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001792 break;
1793 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001794# endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001795 break;
1796 }
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001797#endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001798
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001799 if (!(tcp->qual_flg & QUAL_TRACE)
1800 || (tracing_paths && !pathtrace_match(tcp))
1801 ) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001802 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
1803 return 0;
1804 }
1805
1806 tcp->flags &= ~TCB_FILTERED;
1807
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02001808 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001809 res = 0;
1810 goto ret;
1811 }
1812
Masatake YAMATOed69fc22014-04-16 15:33:35 +09001813#ifdef USE_LIBUNWIND
1814 if (stack_trace_enabled) {
1815 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
1816 unwind_capture_stacktrace(tcp);
1817 }
1818#endif
1819
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001820 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001821 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01001822 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001823 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001824 tprintf("%s(", tcp->s_ent->sys_name);
1825 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001826 res = printargs(tcp);
1827 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001828 res = tcp->s_ent->sys_func(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001829
Dmitry V. Levinb742d8c2012-09-17 22:40:12 +00001830 fflush(tcp->outf);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001831 ret:
1832 tcp->flags |= TCB_INSYSCALL;
1833 /* Measure the entrance time as late as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001834 if (Tflag || cflag)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001835 gettimeofday(&tcp->etime, NULL);
1836 return res;
1837}
1838
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001839/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001840 * 1: ok, continue in trace_syscall_exiting().
1841 * -1: error, trace_syscall_exiting() should print error indicator
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001842 * ("????" etc) and bail out.
1843 */
1844static int
1845get_syscall_result(struct tcb *tcp)
1846{
Dmitry V. Levin5d092242015-02-13 23:06:36 +00001847#if defined ARCH_REGS_FOR_GETREGSET || defined ARCH_REGS_FOR_GETREGS
1848 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001849#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001850 if (upeek(tcp->pid, PT_R0, &bfin_r0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001851 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001852#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001853 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001854 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001855#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001856 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001857 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001858 if (upeek(tcp->pid, REG_R0, &alpha_r0) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001859 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001860#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001861 if (upeek(tcp->pid, PT_GR28, &hppa_r28) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001862 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001863#elif defined(SH)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001864 /* new syscall ABI returns result in R0 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001865 if (upeek(tcp->pid, 4*REG_REG0, (long *)&sh_r0) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001866 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001867#elif defined(SH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001868 /* ABI defines result returned in r9 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001869 if (upeek(tcp->pid, REG_GENERAL(9), (long *)&sh64_r9) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001870 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001871#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001872 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001873 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001874#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001875 if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001876 return -1;
Chris Zankel8f636ed2013-03-25 10:22:07 -07001877#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001878 if (upeek(tcp->pid, REG_A_BASE + 2, &xtensa_a2) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001879 return -1;
Dmitry V. Levin5d092242015-02-13 23:06:36 +00001880#else
1881# error get_syscall_result is not implemented for this architecture
Denys Vlasenko523635f2012-02-25 02:44:25 +01001882#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001883 return 1;
1884}
1885
Denys Vlasenko907735a2012-03-21 00:23:16 +01001886/* Returns:
1887 * 1: ok, continue in trace_syscall_exiting().
1888 * -1: error, trace_syscall_exiting() should print error indicator
1889 * ("????" etc) and bail out.
1890 */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01001891static void
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001892get_error(struct tcb *tcp)
1893{
1894 int u_error = 0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001895 int check_errno = 1;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001896 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001897 check_errno = 0;
1898 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001899#if defined(S390) || defined(S390X)
Dmitry V. Levin20eca8a2015-03-05 22:10:15 +00001900 if (check_errno && is_negated_errno(s390_regset.gprs[2])) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001901 tcp->u_rval = -1;
Dmitry V. Levin20eca8a2015-03-05 22:10:15 +00001902 u_error = -s390_regset.gprs[2];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001903 }
1904 else {
Dmitry V. Levin20eca8a2015-03-05 22:10:15 +00001905 tcp->u_rval = s390_regset.gprs[2];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001906 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001907#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001908 if (check_errno && is_negated_errno(i386_regs.eax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001909 tcp->u_rval = -1;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001910 u_error = -i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001911 }
1912 else {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001913 tcp->u_rval = i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001914 }
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00001915#elif defined(X86_64) || defined(X32)
1916 /*
1917 * In X32, return value is 64-bit (llseek uses one).
1918 * Using merely "long rax" would not work.
1919 */
1920 kernel_long_t rax;
1921
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001922 if (x86_io.iov_len == sizeof(i386_regs)) {
1923 /* Sign extend from 32 bits */
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00001924 rax = (int32_t) i386_regs.eax;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001925 } else {
1926 rax = x86_64_regs.rax;
1927 }
1928 if (check_errno && is_negated_errno(rax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001929 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001930 u_error = -rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001931 }
1932 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001933 tcp->u_rval = rax;
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00001934# ifdef X32
1935 /* tcp->u_rval contains a truncated value */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001936 tcp->u_lrval = rax;
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00001937# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001938 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001939#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001940 if (ia64_ia32mode) {
Dmitry V. Levin48f08902015-03-05 23:30:02 +00001941 int err = ia64_regs.gr[8];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001942 if (check_errno && is_negated_errno(err)) {
1943 tcp->u_rval = -1;
1944 u_error = -err;
1945 }
1946 else {
1947 tcp->u_rval = err;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001948 }
1949 } else {
Dmitry V. Levin48f08902015-03-05 23:30:02 +00001950 if (check_errno && ia64_regs.gr[10]) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001951 tcp->u_rval = -1;
Dmitry V. Levin48f08902015-03-05 23:30:02 +00001952 u_error = ia64_regs.gr[8];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001953 } else {
Dmitry V. Levin48f08902015-03-05 23:30:02 +00001954 tcp->u_rval = ia64_regs.gr[8];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001955 }
1956 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001957#elif defined(MIPS)
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +00001958 if (check_errno && mips_REG_A3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001959 tcp->u_rval = -1;
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +00001960 u_error = mips_REG_V0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001961 } else {
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +00001962# if defined LINUX_MIPSN32
1963 tcp->u_lrval = mips_REG_V0;
H.J. Ludd0130b2012-04-16 12:16:45 +02001964# endif
Dmitry V. Levin4f2d1ae2015-02-15 03:27:13 +00001965 tcp->u_rval = mips_REG_V0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001966 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001967#elif defined(POWERPC)
Denys Vlasenko44a6d042013-06-28 16:47:38 +02001968 if (check_errno && (ppc_regs.ccr & 0x10000000)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001969 tcp->u_rval = -1;
Denys Vlasenko44a6d042013-06-28 16:47:38 +02001970 u_error = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001971 }
1972 else {
Denys Vlasenko44a6d042013-06-28 16:47:38 +02001973 tcp->u_rval = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001974 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001975#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001976 if (check_errno && is_negated_errno(m68k_d0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001977 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001978 u_error = -m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001979 }
1980 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001981 tcp->u_rval = m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001982 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001983#elif defined(ARM) || defined(AARCH64)
1984# if defined(AARCH64)
1985 if (tcp->currpers == 1) {
1986 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
1987 tcp->u_rval = -1;
1988 u_error = -aarch64_regs.regs[0];
1989 }
1990 else {
1991 tcp->u_rval = aarch64_regs.regs[0];
1992 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001993 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001994 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001995# endif
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001996 {
Denys Vlasenko401374e2013-02-06 18:24:39 +01001997 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001998 tcp->u_rval = -1;
Denys Vlasenko401374e2013-02-06 18:24:39 +01001999 u_error = -arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002000 }
2001 else {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002002 tcp->u_rval = arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002003 }
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002004 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002005#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002006 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002007 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002008 u_error = -avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002009 }
2010 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002011 tcp->u_rval = avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002012 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002013#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002014 if (check_errno && is_negated_errno(bfin_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002015 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002016 u_error = -bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002017 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002018 tcp->u_rval = bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002019 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002020#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002021 if (check_errno && alpha_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002022 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002023 u_error = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002024 }
2025 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002026 tcp->u_rval = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002027 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002028#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002029 if (check_errno && sparc_regs.psr & PSR_C) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002030 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002031 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002032 }
2033 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002034 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002035 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002036#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002037 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002038 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002039 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002040 }
2041 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002042 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002043 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002044#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002045 if (check_errno && is_negated_errno(hppa_r28)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002046 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002047 u_error = -hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002048 }
2049 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002050 tcp->u_rval = hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002051 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002052#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002053 if (check_errno && is_negated_errno(sh_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002054 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002055 u_error = -sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002056 }
2057 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002058 tcp->u_rval = sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002059 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002060#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002061 if (check_errno && is_negated_errno(sh64_r9)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002062 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002063 u_error = -sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002064 }
2065 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002066 tcp->u_rval = sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002067 }
James Hogan5f999a82013-02-22 14:44:10 +00002068#elif defined(METAG)
2069 /* result pointer in D0Re0 (D0.0) */
2070 if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2071 tcp->u_rval = -1;
2072 u_error = -metag_regs.dx[0][0];
2073 }
2074 else {
2075 tcp->u_rval = metag_regs.dx[0][0];
2076 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002077#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002078 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002079 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002080 u_error = -cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002081 }
2082 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002083 tcp->u_rval = cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002084 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002085#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002086 /*
2087 * The standard tile calling convention returns the value (or negative
2088 * errno) in r0, and zero (or positive errno) in r1.
2089 * Until at least kernel 3.8, however, the r1 value is not reflected
2090 * in ptregs at this point, so we use r0 here.
2091 */
2092 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002093 tcp->u_rval = -1;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002094 u_error = -tile_regs.regs[0];
2095 } else {
2096 tcp->u_rval = tile_regs.regs[0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002097 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002098#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002099 if (check_errno && is_negated_errno(microblaze_r3)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002100 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002101 u_error = -microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002102 }
2103 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002104 tcp->u_rval = microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002105 }
Christian Svensson492f81f2013-02-14 13:26:27 +01002106#elif defined(OR1K)
2107 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2108 tcp->u_rval = -1;
2109 u_error = -or1k_regs.gpr[11];
2110 }
2111 else {
2112 tcp->u_rval = or1k_regs.gpr[11];
2113 }
Chris Zankel8f636ed2013-03-25 10:22:07 -07002114#elif defined(XTENSA)
2115 if (check_errno && is_negated_errno(xtensa_a2)) {
2116 tcp->u_rval = -1;
2117 u_error = -xtensa_a2;
2118 }
2119 else {
2120 tcp->u_rval = xtensa_a2;
2121 }
Vineet Gupta7daacbb2013-08-16 12:47:06 +05302122#elif defined(ARC)
2123 if (check_errno && is_negated_errno(arc_regs.scratch.r0)) {
2124 tcp->u_rval = -1;
2125 u_error = -arc_regs.scratch.r0;
2126 }
2127 else {
2128 tcp->u_rval = arc_regs.scratch.r0;
2129 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002130#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002131 tcp->u_error = u_error;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002132}
2133
2134static void
2135dumpio(struct tcb *tcp)
2136{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002137 int (*func)();
2138
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002139 if (syserror(tcp))
2140 return;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +01002141 if ((unsigned long) tcp->u_arg[0] >= num_quals)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002142 return;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002143 func = tcp->s_ent->sys_func;
2144 if (func == printargs)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002145 return;
2146 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002147 if (func == sys_read ||
2148 func == sys_pread ||
2149 func == sys_recv ||
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002150 func == sys_recvfrom) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002151 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002152 return;
2153 } else if (func == sys_readv) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002154 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002155 return;
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002156#if HAVE_SENDMSG
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002157 } else if (func == sys_recvmsg) {
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002158 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002159 return;
2160 } else if (func == sys_recvmmsg) {
Masatake YAMATOa807dce2014-11-07 01:23:26 +09002161 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002162 return;
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002163#endif
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002164 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002165 }
2166 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002167 if (func == sys_write ||
2168 func == sys_pwrite ||
2169 func == sys_send ||
2170 func == sys_sendto)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002171 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002172 else if (func == sys_writev)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002173 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002174#if HAVE_SENDMSG
2175 else if (func == sys_sendmsg)
2176 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
Masatake YAMATOa807dce2014-11-07 01:23:26 +09002177 else if (func == sys_sendmmsg)
2178 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002179#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002180 }
2181}
2182
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002183static int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002184trace_syscall_exiting(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002185{
2186 int sys_res;
2187 struct timeval tv;
Denys Vlasenko1a5b5a72011-08-25 00:29:56 +02002188 int res;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002189 long u_error;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002190
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002191 /* Measure the exit time as early as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002192 if (Tflag || cflag)
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002193 gettimeofday(&tv, NULL);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002194
Masatake YAMATOed69fc22014-04-16 15:33:35 +09002195#ifdef USE_LIBUNWIND
2196 if (stack_trace_enabled) {
2197 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
2198 unwind_cache_invalidate(tcp);
2199 }
2200#endif
2201
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00002202#if SUPPORTED_PERSONALITIES > 1
2203 update_personality(tcp, tcp->currpers);
2204#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002205 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002206 if (res == 1) {
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002207 get_error(tcp); /* never fails */
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002208 if (filtered(tcp) || hide_log_until_execve)
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002209 goto ret;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002210 }
2211
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002212 if (cflag) {
Dmitry V. Levinac5133d2014-05-29 18:10:00 +00002213 count_syscall(tcp, &tv);
Denys Vlasenko7b609d52011-06-22 14:32:43 +02002214 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002215 goto ret;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002216 }
2217 }
2218
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002219 /* If not in -ff mode, and printing_tcp != tcp,
2220 * then the log currently does not end with output
2221 * of _our syscall entry_, but with something else.
2222 * We need to say which syscall's return is this.
2223 *
2224 * Forced reprinting via TCB_REPRINT is used only by
2225 * "strace -ff -oLOG test/threaded_execve" corner case.
2226 * It's the only case when -ff mode needs reprinting.
2227 */
2228 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2229 tcp->flags &= ~TCB_REPRINT;
2230 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002231 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002232 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002233 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002234 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002235 }
2236 printing_tcp = tcp;
2237
Denys Vlasenko8497b622015-03-21 17:51:52 +01002238 tcp->s_prev_ent = NULL;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002239 if (res != 1) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002240 /* There was error in one of prior ptrace ops */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002241 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002242 tabto();
Denys Vlasenko000b6012012-01-28 01:25:03 +01002243 tprints("= ? <unavailable>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002244 line_ended();
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002245 tcp->flags &= ~TCB_INSYSCALL;
2246 return res;
2247 }
Denys Vlasenko8497b622015-03-21 17:51:52 +01002248 tcp->s_prev_ent = tcp->s_ent;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002249
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002250 sys_res = 0;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002251 if (tcp->qual_flg & QUAL_RAW) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002252 /* sys_res = printargs(tcp); - but it's nop on sysexit */
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002253 } else {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002254 /* FIXME: not_failing_only (IOW, option -z) is broken:
2255 * failure of syscall is known only after syscall return.
2256 * Thus we end up with something like this on, say, ENOENT:
2257 * open("doesnt_exist", O_RDONLY <unfinished ...>
2258 * {next syscall decode}
2259 * whereas the intended result is that open(...) line
2260 * is not shown at all.
2261 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002262 if (not_failing_only && tcp->u_error)
Denys Vlasenko3b738812011-08-22 02:06:35 +02002263 goto ret; /* ignore failed syscalls */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002264 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002265 }
2266
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002267 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002268 tabto();
Denys Vlasenko3b738812011-08-22 02:06:35 +02002269 u_error = tcp->u_error;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002270 if (tcp->qual_flg & QUAL_RAW) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002271 if (u_error)
2272 tprintf("= -1 (errno %ld)", u_error);
2273 else
2274 tprintf("= %#lx", tcp->u_rval);
2275 }
2276 else if (!(sys_res & RVAL_NONE) && u_error) {
2277 switch (u_error) {
Denys Vlasenkofe585652012-01-12 11:26:34 +01002278 /* Blocked signals do not interrupt any syscalls.
2279 * In this case syscalls don't return ERESTARTfoo codes.
2280 *
2281 * Deadly signals set to SIG_DFL interrupt syscalls
2282 * and kill the process regardless of which of the codes below
2283 * is returned by the interrupted syscall.
2284 * In some cases, kernel forces a kernel-generated deadly
2285 * signal to be unblocked and set to SIG_DFL (and thus cause
2286 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2287 * or SIGILL. (The alternative is to leave process spinning
2288 * forever on the faulty instruction - not useful).
2289 *
2290 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2291 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2292 * but kernel will always restart them.
2293 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002294 case ERESTARTSYS:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002295 /* Most common type of signal-interrupted syscall exit code.
2296 * The system call will be restarted with the same arguments
2297 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2298 */
2299 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002300 break;
2301 case ERESTARTNOINTR:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002302 /* Rare. For example, fork() returns this if interrupted.
2303 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2304 */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002305 tprints("= ? ERESTARTNOINTR (To be restarted)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002306 break;
2307 case ERESTARTNOHAND:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002308 /* pause(), rt_sigsuspend() etc use this code.
2309 * SA_RESTART is ignored (assumed not set):
2310 * syscall won't restart (will return EINTR instead)
Denys Vlasenko30c03232013-02-19 16:59:26 +01002311 * even after signal with SA_RESTART set. However,
2312 * after SIG_IGN or SIG_DFL signal it will restart
2313 * (thus the name "restart only if has no handler").
Denys Vlasenkofe585652012-01-12 11:26:34 +01002314 */
Denys Vlasenkoaba62922013-03-05 16:56:35 +01002315 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002316 break;
2317 case ERESTART_RESTARTBLOCK:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002318 /* Syscalls like nanosleep(), poll() which can't be
2319 * restarted with their original arguments use this
2320 * code. Kernel will execute restart_syscall() instead,
2321 * which changes arguments before restarting syscall.
2322 * SA_RESTART is ignored (assumed not set) similarly
2323 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2324 * since restart data is saved in "restart block"
2325 * in task struct, and if signal handler uses a syscall
2326 * which in turn saves another such restart block,
2327 * old data is lost and restart becomes impossible)
2328 */
2329 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002330 break;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002331 default:
Dmitry V. Levinb2f8c772015-02-23 03:10:25 +00002332 if ((unsigned long) u_error < nerrnos
2333 && errnoent[u_error])
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002334 tprintf("= -1 %s (%s)", errnoent[u_error],
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002335 strerror(u_error));
2336 else
Dmitry V. Levinb2f8c772015-02-23 03:10:25 +00002337 tprintf("= -1 ERRNO_%lu (%s)", u_error,
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002338 strerror(u_error));
2339 break;
2340 }
2341 if ((sys_res & RVAL_STR) && tcp->auxstr)
2342 tprintf(" (%s)", tcp->auxstr);
2343 }
2344 else {
2345 if (sys_res & RVAL_NONE)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002346 tprints("= ?");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002347 else {
2348 switch (sys_res & RVAL_MASK) {
2349 case RVAL_HEX:
Dmitry V. Levind93d9f82015-02-28 23:41:11 +00002350#if SUPPORTED_PERSONALITIES > 1
2351 if (current_wordsize < sizeof(long))
2352 tprintf("= %#x",
2353 (unsigned int) tcp->u_rval);
2354 else
2355#endif
2356 tprintf("= %#lx", tcp->u_rval);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002357 break;
2358 case RVAL_OCTAL:
2359 tprintf("= %#lo", tcp->u_rval);
2360 break;
2361 case RVAL_UDECIMAL:
2362 tprintf("= %lu", tcp->u_rval);
2363 break;
2364 case RVAL_DECIMAL:
2365 tprintf("= %ld", tcp->u_rval);
2366 break;
Zubin Mithra64aa1b12014-06-04 08:30:41 +05302367 case RVAL_FD:
2368 if (show_fd_path) {
2369 tprints("= ");
2370 printfd(tcp, tcp->u_rval);
2371 }
2372 else
2373 tprintf("= %ld", tcp->u_rval);
2374 break;
H.J. Ludd0130b2012-04-16 12:16:45 +02002375#if defined(LINUX_MIPSN32) || defined(X32)
2376 /*
2377 case RVAL_LHEX:
2378 tprintf("= %#llx", tcp->u_lrval);
2379 break;
2380 case RVAL_LOCTAL:
2381 tprintf("= %#llo", tcp->u_lrval);
2382 break;
2383 */
2384 case RVAL_LUDECIMAL:
2385 tprintf("= %llu", tcp->u_lrval);
2386 break;
2387 /*
2388 case RVAL_LDECIMAL:
2389 tprintf("= %lld", tcp->u_lrval);
2390 break;
2391 */
2392#endif
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002393 default:
2394 fprintf(stderr,
2395 "invalid rval format\n");
2396 break;
2397 }
2398 }
2399 if ((sys_res & RVAL_STR) && tcp->auxstr)
2400 tprintf(" (%s)", tcp->auxstr);
2401 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002402 if (Tflag) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002403 tv_sub(&tv, &tv, &tcp->etime);
2404 tprintf(" <%ld.%06ld>",
2405 (long) tv.tv_sec, (long) tv.tv_usec);
2406 }
Denys Vlasenko000b6012012-01-28 01:25:03 +01002407 tprints("\n");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002408 dumpio(tcp);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002409 line_ended();
2410
Luca Clementi327064b2013-07-23 00:11:35 -07002411#ifdef USE_LIBUNWIND
2412 if (stack_trace_enabled)
Masatake YAMATO61413922014-04-16 15:33:02 +09002413 unwind_print_stacktrace(tcp);
Luca Clementi327064b2013-07-23 00:11:35 -07002414#endif
2415
Denys Vlasenko3b738812011-08-22 02:06:35 +02002416 ret:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002417 tcp->flags &= ~TCB_INSYSCALL;
2418 return 0;
2419}
2420
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002421int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002422trace_syscall(struct tcb *tcp)
2423{
2424 return exiting(tcp) ?
2425 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2426}