blob: b5ce90e2a910c1aa27f546e96532bd513d1853aa [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. Levinfadf3792015-02-13 00:26:38 +000040#include "ptrace.h"
Wichert Akkerman15dea971999-10-06 13:06:34 +000041
Denys Vlasenko84703742012-02-25 02:38:52 +010042#if defined(SPARC64)
Roland McGrath6d1a65c2004-07-12 07:44:08 +000043# undef PTRACE_GETREGS
44# define PTRACE_GETREGS PTRACE_GETREGS64
45# undef PTRACE_SETREGS
46# define PTRACE_SETREGS PTRACE_SETREGS64
Denys Vlasenko84703742012-02-25 02:38:52 +010047#endif
Roland McGrath6d1a65c2004-07-12 07:44:08 +000048
Dmitry V. Levin5503dd22015-02-13 02:12:14 +000049#include "regs.h"
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000050
Dmitry V. Levin5503dd22015-02-13 02:12:14 +000051#if defined SPARC64
52# include <asm/psrcompat.h>
53#elif defined SPARC
54# include <asm/psr.h>
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010055#endif
56
Dmitry V. Levin4c3f2ae2015-02-13 22:53:00 +000057#ifndef NT_PRSTATUS
58# define NT_PRSTATUS 1
59#endif
60
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000061#ifndef NSIG
Denys Vlasenko523635f2012-02-25 02:44:25 +010062# warning: NSIG is not defined, using 32
63# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000064#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000065
66#include "syscall.h"
67
68/* Define these shorthand notations to simplify the syscallent files. */
Roland McGrath2fe7b132005-07-05 03:25:35 +000069#define TD TRACE_DESC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000070#define TF TRACE_FILE
71#define TI TRACE_IPC
72#define TN TRACE_NETWORK
73#define TP TRACE_PROCESS
74#define TS TRACE_SIGNAL
Namhyung Kim96792962012-10-24 11:41:57 +090075#define TM TRACE_MEMORY
Dmitry V. Levin50a218d2011-01-18 17:36:20 +000076#define NF SYSCALL_NEVER_FAILS
Denys Vlasenkoac1ce772011-08-23 13:24:17 +020077#define MA MAX_ARGS
Masatake YAMATO1d78d222014-04-16 15:33:11 +090078#define SI STACKTRACE_INVALIDATE_CACHE
79#define SE STACKTRACE_CAPTURE_ON_ENTER
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000080
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +010081const struct_sysent sysent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000082#include "syscallent.h"
83};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000084
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010085#if SUPPORTED_PERSONALITIES > 1
86static const struct_sysent sysent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +010087# include "syscallent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000088};
Denys Vlasenko39fca622011-08-20 02:12:33 +020089#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000090
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010091#if SUPPORTED_PERSONALITIES > 2
92static const struct_sysent sysent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +010093# include "syscallent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000094};
Denys Vlasenko39fca622011-08-20 02:12:33 +020095#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000096
97/* Now undef them since short defines cause wicked namespace pollution. */
Roland McGrath2fe7b132005-07-05 03:25:35 +000098#undef TD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000099#undef TF
100#undef TI
101#undef TN
102#undef TP
103#undef TS
Namhyung Kim96792962012-10-24 11:41:57 +0900104#undef TM
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000105#undef NF
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200106#undef MA
Masatake YAMATO1d78d222014-04-16 15:33:11 +0900107#undef SI
108#undef SE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000109
Denys Vlasenko39fca622011-08-20 02:12:33 +0200110/*
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +0000111 * `ioctlent[012].h' files are automatically generated by the auxiliary
Denys Vlasenko39fca622011-08-20 02:12:33 +0200112 * program `ioctlsort', such that the list is sorted by the `code' field.
113 * This has the side-effect of resolving the _IO.. macros into
114 * plain integers, eliminating the need to include here everything
115 * in "/usr/include".
116 */
117
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100118const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000119#include "errnoent.h"
120};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100121const char *const signalent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200122#include "signalent.h"
123};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100124const struct_ioctlent ioctlent0[] = {
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +0000125#include "ioctlent0.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200126};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000127
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100128#if SUPPORTED_PERSONALITIES > 1
Roland McGrathee36ce12004-09-04 03:53:10 +0000129static const char *const errnoent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100130# include "errnoent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000131};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200132static const char *const signalent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100133# include "signalent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200134};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100135static const struct_ioctlent ioctlent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100136# include "ioctlent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200137};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200138#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000139
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100140#if SUPPORTED_PERSONALITIES > 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000141static const char *const errnoent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100142# include "errnoent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000143};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200144static const char *const signalent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100145# include "signalent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200146};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100147static const struct_ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100148# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200149};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200150#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000151
Dmitry V. Levine6f55242013-02-26 22:02:30 +0000152enum {
153 nsyscalls0 = ARRAY_SIZE(sysent0)
154#if SUPPORTED_PERSONALITIES > 1
155 , nsyscalls1 = ARRAY_SIZE(sysent1)
156# if SUPPORTED_PERSONALITIES > 2
157 , nsyscalls2 = ARRAY_SIZE(sysent2)
158# endif
159#endif
160};
161
162enum {
163 nerrnos0 = ARRAY_SIZE(errnoent0)
164#if SUPPORTED_PERSONALITIES > 1
165 , nerrnos1 = ARRAY_SIZE(errnoent1)
166# if SUPPORTED_PERSONALITIES > 2
167 , nerrnos2 = ARRAY_SIZE(errnoent2)
168# endif
169#endif
170};
171
172enum {
173 nsignals0 = ARRAY_SIZE(signalent0)
174#if SUPPORTED_PERSONALITIES > 1
175 , nsignals1 = ARRAY_SIZE(signalent1)
176# if SUPPORTED_PERSONALITIES > 2
177 , nsignals2 = ARRAY_SIZE(signalent2)
178# endif
179#endif
180};
181
182enum {
183 nioctlents0 = ARRAY_SIZE(ioctlent0)
184#if SUPPORTED_PERSONALITIES > 1
185 , nioctlents1 = ARRAY_SIZE(ioctlent1)
186# if SUPPORTED_PERSONALITIES > 2
187 , nioctlents2 = ARRAY_SIZE(ioctlent2)
188# endif
189#endif
190};
191
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100192#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100193const struct_sysent *sysent = sysent0;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100194const char *const *errnoent = errnoent0;
195const char *const *signalent = signalent0;
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100196const struct_ioctlent *ioctlent = ioctlent0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100197#endif
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100198unsigned nsyscalls = nsyscalls0;
199unsigned nerrnos = nerrnos0;
200unsigned nsignals = nsignals0;
201unsigned nioctlents = nioctlents0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100202
203unsigned num_quals;
204qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
205
206static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
207 nsyscalls0,
208#if SUPPORTED_PERSONALITIES > 1
209 nsyscalls1,
210#endif
211#if SUPPORTED_PERSONALITIES > 2
212 nsyscalls2,
213#endif
214};
215static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
216 sysent0,
217#if SUPPORTED_PERSONALITIES > 1
218 sysent1,
219#endif
220#if SUPPORTED_PERSONALITIES > 2
221 sysent2,
222#endif
223};
224
225enum {
226 MAX_NSYSCALLS1 = (nsyscalls0
227#if SUPPORTED_PERSONALITIES > 1
228 > nsyscalls1 ? nsyscalls0 : nsyscalls1
229#endif
230 ),
231 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
232#if SUPPORTED_PERSONALITIES > 2
233 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
234#endif
235 ),
236 MAX_NSYSCALLS = MAX_NSYSCALLS2,
237 /* We are ready for arches with up to 255 signals,
238 * even though the largest known signo is on MIPS and it is 128.
239 * The number of existing syscalls on all arches is
240 * larger that 255 anyway, so it is just a pedantic matter.
241 */
242 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
243};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000244
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100245#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100246unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000247
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100248# ifndef current_wordsize
249unsigned current_wordsize;
250static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000251 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000252 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100253# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000254 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100255# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200256};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100257# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000258
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200259void
Dmitry V. Levin3abe8b22006-12-20 22:37:21 +0000260set_personality(int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000261{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100262 nsyscalls = nsyscall_vec[personality];
263 sysent = sysent_vec[personality];
264
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000265 switch (personality) {
266 case 0:
267 errnoent = errnoent0;
268 nerrnos = nerrnos0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000269 ioctlent = ioctlent0;
270 nioctlents = nioctlents0;
271 signalent = signalent0;
272 nsignals = nsignals0;
273 break;
274
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000275 case 1:
276 errnoent = errnoent1;
277 nerrnos = nerrnos1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000278 ioctlent = ioctlent1;
279 nioctlents = nioctlents1;
280 signalent = signalent1;
281 nsignals = nsignals1;
282 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000283
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100284# if SUPPORTED_PERSONALITIES > 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000285 case 2:
286 errnoent = errnoent2;
287 nerrnos = nerrnos2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000288 ioctlent = ioctlent2;
289 nioctlents = nioctlents2;
290 signalent = signalent2;
291 nsignals = nsignals2;
292 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100293# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000294 }
295
296 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100297# ifndef current_wordsize
298 current_wordsize = personality_wordsize[personality];
299# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000300}
301
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000302static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000303update_personality(struct tcb *tcp, unsigned int personality)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000304{
305 if (personality == current_personality)
306 return;
307 set_personality(personality);
308
309 if (personality == tcp->currpers)
310 return;
311 tcp->currpers = personality;
312
H.J. Lu35be5812012-04-16 13:00:01 +0200313# if defined(POWERPC64)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000314 if (!qflag) {
315 static const char *const names[] = {"64 bit", "32 bit"};
316 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
317 tcp->pid, names[personality]);
318 }
H.J. Lu35be5812012-04-16 13:00:01 +0200319# elif defined(X86_64)
320 if (!qflag) {
321 static const char *const names[] = {"64 bit", "32 bit", "x32"};
322 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
323 tcp->pid, names[personality]);
324 }
H.J. Lu085e4282012-04-17 11:05:04 -0700325# elif defined(X32)
326 if (!qflag) {
327 static const char *const names[] = {"x32", "32 bit"};
328 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
329 tcp->pid, names[personality]);
330 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000331# elif defined(AARCH64)
332 if (!qflag) {
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100333 static const char *const names[] = {"32-bit", "AArch64"};
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000334 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
335 tcp->pid, names[personality]);
336 }
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100337# elif defined(TILE)
338 if (!qflag) {
339 static const char *const names[] = {"64-bit", "32-bit"};
340 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
341 tcp->pid, names[personality]);
342 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100343# endif
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000344}
345#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000346
Denys Vlasenkoc1540fe2013-02-21 16:17:08 +0100347static int qual_syscall(), qual_signal(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000348
Roland McGrathe10e62a2004-09-04 04:20:43 +0000349static const struct qual_options {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000350 unsigned int bitflag;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000351 const char *option_name;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000352 int (*qualify)(const char *, int, int);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000353 const char *argument_name;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000354} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000355 { QUAL_TRACE, "trace", qual_syscall, "system call" },
356 { QUAL_TRACE, "t", qual_syscall, "system call" },
357 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
358 { QUAL_ABBREV, "a", qual_syscall, "system call" },
359 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
360 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
361 { QUAL_RAW, "raw", qual_syscall, "system call" },
362 { QUAL_RAW, "x", qual_syscall, "system call" },
363 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
364 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
365 { QUAL_SIGNAL, "s", qual_signal, "signal" },
Roland McGrath9797ceb2002-12-30 10:23:00 +0000366 { QUAL_READ, "read", qual_desc, "descriptor" },
367 { QUAL_READ, "reads", qual_desc, "descriptor" },
368 { QUAL_READ, "r", qual_desc, "descriptor" },
369 { QUAL_WRITE, "write", qual_desc, "descriptor" },
370 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
371 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000372 { 0, NULL, NULL, NULL },
373};
374
Roland McGrath9797ceb2002-12-30 10:23:00 +0000375static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000376reallocate_qual(const unsigned int n)
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100377{
378 unsigned p;
379 qualbits_t *qp;
380 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
381 qp = qual_vec[p] = realloc(qual_vec[p], n * sizeof(qualbits_t));
382 if (!qp)
383 die_out_of_memory();
384 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
385 }
386 num_quals = n;
387}
388
389static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000390qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000391{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000392 int p;
Roland McGrath138c6a32006-01-12 09:50:49 +0000393
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100394 if (num_quals <= n)
395 reallocate_qual(n + 1);
Roland McGrath138c6a32006-01-12 09:50:49 +0000396
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100397 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
398 if (pers == p || pers < 0) {
399 if (not)
400 qual_vec[p][n] &= ~bitflag;
401 else
402 qual_vec[p][n] |= bitflag;
403 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000404 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000405}
406
407static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000408qual_syscall(const char *s, const unsigned int bitflag, const int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000409{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000410 int p;
411 unsigned int i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000412 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000413
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100414 if (*s >= '0' && *s <= '9') {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100415 i = string_to_uint(s);
Denys Vlasenkob43dacd2013-02-23 18:19:28 +0100416 if (i >= MAX_NSYSCALLS)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000417 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000418 qualify_one(i, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000419 return 0;
Roland McGrath48a035f2006-01-12 09:45:56 +0000420 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000421
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100422 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
423 for (i = 0; i < nsyscall_vec[p]; i++) {
424 if (sysent_vec[p][i].sys_name
425 && strcmp(s, sysent_vec[p][i].sys_name) == 0
426 ) {
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000427 qualify_one(i, bitflag, not, p);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100428 rc = 0;
429 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000430 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100431 }
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000432
Roland McGrathfe6b3522005-02-02 04:40:11 +0000433 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000434}
435
436static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000437qual_signal(const char *s, const unsigned int bitflag, const int not)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000438{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000439 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000440
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100441 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000442 int signo = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100443 if (signo < 0 || signo > 255)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000444 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000445 qualify_one(signo, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000446 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000447 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000448 if (strncasecmp(s, "SIG", 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000449 s += 3;
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100450 for (i = 0; i <= NSIG; i++) {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000451 if (strcasecmp(s, signame(i) + 3) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000452 qualify_one(i, bitflag, not, -1);
Roland McGrath76421df2005-02-02 03:51:18 +0000453 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000454 }
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100455 }
Roland McGrath76421df2005-02-02 03:51:18 +0000456 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000457}
458
459static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000460qual_desc(const char *s, const unsigned int bitflag, const int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000461{
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100462 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000463 int desc = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100464 if (desc < 0 || desc > 0x7fff) /* paranoia */
Roland McGrathfe6b3522005-02-02 04:40:11 +0000465 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000466 qualify_one(desc, bitflag, not, -1);
Roland McGrath2b619022003-04-10 18:58:20 +0000467 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000468 }
469 return -1;
470}
471
472static int
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000473lookup_class(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000474{
475 if (strcmp(s, "file") == 0)
476 return TRACE_FILE;
477 if (strcmp(s, "ipc") == 0)
478 return TRACE_IPC;
479 if (strcmp(s, "network") == 0)
480 return TRACE_NETWORK;
481 if (strcmp(s, "process") == 0)
482 return TRACE_PROCESS;
483 if (strcmp(s, "signal") == 0)
484 return TRACE_SIGNAL;
Roland McGrath2fe7b132005-07-05 03:25:35 +0000485 if (strcmp(s, "desc") == 0)
486 return TRACE_DESC;
Namhyung Kim96792962012-10-24 11:41:57 +0900487 if (strcmp(s, "memory") == 0)
488 return TRACE_MEMORY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000489 return -1;
490}
491
492void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000493qualify(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000494{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000495 const struct qual_options *opt;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000496 char *copy;
497 const char *p;
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000498 int not;
499 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000500
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100501 if (num_quals == 0)
502 reallocate_qual(MIN_QUALS);
503
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000504 opt = &qual_options[0];
505 for (i = 0; (p = qual_options[i].option_name); i++) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000506 unsigned int len = strlen(p);
507 if (strncmp(s, p, len) == 0 && s[len] == '=') {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000508 opt = &qual_options[i];
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000509 s += len + 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000510 break;
511 }
512 }
513 not = 0;
514 if (*s == '!') {
515 not = 1;
516 s++;
517 }
518 if (strcmp(s, "none") == 0) {
519 not = 1 - not;
520 s = "all";
521 }
522 if (strcmp(s, "all") == 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100523 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000524 qualify_one(i, opt->bitflag, not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000525 }
526 return;
527 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100528 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000529 qualify_one(i, opt->bitflag, !not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000530 }
Denys Vlasenko5d645812011-08-20 12:48:18 +0200531 copy = strdup(s);
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200532 if (!copy)
533 die_out_of_memory();
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000534 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000535 int n;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000536 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100537 unsigned pers;
538 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
539 for (i = 0; i < nsyscall_vec[pers]; i++)
540 if (sysent_vec[pers][i].sys_flags & n)
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000541 qualify_one(i, opt->bitflag, not, pers);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100542 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000543 continue;
544 }
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000545 if (opt->qualify(p, opt->bitflag, not)) {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100546 error_msg_and_die("invalid %s '%s'",
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000547 opt->argument_name, p);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000548 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000549 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000550 free(copy);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000551 return;
552}
553
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000554#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000555static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000556decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000557{
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000558 unsigned long addr;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100559 unsigned int i, n, size;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000560
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000561 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
562 return;
563
564 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100565 tcp->qual_flg = qual_flags[tcp->scno];
566 tcp->s_ent = &sysent[tcp->scno];
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000567 addr = tcp->u_arg[1];
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100568 size = current_wordsize;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100569 n = tcp->s_ent->nargs;
570 for (i = 0; i < n; ++i) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000571 if (size == sizeof(int)) {
572 unsigned int arg;
573 if (umove(tcp, addr, &arg) < 0)
574 arg = 0;
575 tcp->u_arg[i] = arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000576 }
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000577 else {
578 unsigned long arg;
579 if (umove(tcp, addr, &arg) < 0)
580 arg = 0;
581 tcp->u_arg[i] = arg;
582 }
583 addr += size;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000584 }
585}
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000586#endif
Mike Frysinger3362e892012-03-15 01:09:19 -0400587
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000588#ifdef SYS_ipc_subcall
589static void
590decode_ipc_subcall(struct tcb *tcp)
591{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100592 unsigned int i, n;
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000593
594 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
595 return;
596
597 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100598 tcp->qual_flg = qual_flags[tcp->scno];
599 tcp->s_ent = &sysent[tcp->scno];
600 n = tcp->s_ent->nargs;
601 for (i = 0; i < n; i++)
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000602 tcp->u_arg[i] = tcp->u_arg[i + 1];
603}
604#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000605
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200606int
607printargs(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000608{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200609 if (entering(tcp)) {
610 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100611 int n = tcp->s_ent->nargs;
612 for (i = 0; i < n; i++)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200613 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
614 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000615 return 0;
616}
617
Denys Vlasenko72879c62012-02-27 14:18:02 +0100618int
619printargs_lu(struct tcb *tcp)
620{
621 if (entering(tcp)) {
622 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100623 int n = tcp->s_ent->nargs;
624 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100625 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
626 }
627 return 0;
628}
629
630int
631printargs_ld(struct tcb *tcp)
632{
633 if (entering(tcp)) {
634 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100635 int n = tcp->s_ent->nargs;
636 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100637 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
638 }
639 return 0;
640}
641
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100642#if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200643long
644getrval2(struct tcb *tcp)
645{
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100646 long val;
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200647
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100648# if defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100649 val = sparc_regs.u_regs[U_REG_O1];
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100650# elif defined(SH)
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200651 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200652 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100653# elif defined(IA64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200654 if (upeek(tcp->pid, PT_R9, &val) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200655 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100656# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200657
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200658 return val;
659}
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100660#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200661
Denys Vlasenko523635f2012-02-25 02:44:25 +0100662#if defined(I386)
Denys Vlasenkob51f3642013-07-16 12:06:25 +0200663static struct user_regs_struct i386_regs;
Denys Vlasenkoe600ac62013-07-18 17:40:45 +0200664/* Cast suppresses signedness warning (.esp is long, not unsigned long) */
665uint32_t *const i386_esp_ptr = (uint32_t*)&i386_regs.esp;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000666# define ARCH_REGS_FOR_GETREGSET i386_regs
H.J. Lu35be5812012-04-16 13:00:01 +0200667#elif defined(X86_64) || defined(X32)
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100668/*
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100669 * On i386, pt_regs and user_regs_struct are the same,
670 * but on 64 bit x86, user_regs_struct has six more fields:
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100671 * fs_base, gs_base, ds, es, fs, gs.
672 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
673 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100674struct i386_user_regs_struct {
675 uint32_t ebx;
676 uint32_t ecx;
677 uint32_t edx;
678 uint32_t esi;
679 uint32_t edi;
680 uint32_t ebp;
681 uint32_t eax;
682 uint32_t xds;
683 uint32_t xes;
684 uint32_t xfs;
685 uint32_t xgs;
686 uint32_t orig_eax;
687 uint32_t eip;
688 uint32_t xcs;
689 uint32_t eflags;
690 uint32_t esp;
691 uint32_t xss;
692};
693static union {
694 struct user_regs_struct x86_64_r;
695 struct i386_user_regs_struct i386_r;
696} x86_regs_union;
697# define x86_64_regs x86_regs_union.x86_64_r
698# define i386_regs x86_regs_union.i386_r
Denys Vlasenkob51f3642013-07-16 12:06:25 +0200699uint32_t *const i386_esp_ptr = &i386_regs.esp;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100700static struct iovec x86_io = {
701 .iov_base = &x86_regs_union
702};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100703#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +0200704bool ia64_ia32mode = 0; /* not static */
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100705static long ia64_r8, ia64_r10;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100706#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +0200707struct pt_regs ppc_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100708#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100709static long m68k_d0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100710#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100711static long bfin_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100712#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100713struct pt_regs arm_regs; /* not static */
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000714# define ARCH_REGS_FOR_GETREGSET arm_regs
Steve McIntyred8d3bd32012-10-24 17:58:16 +0100715#elif defined(AARCH64)
Dmitry V. Levin5503dd22015-02-13 02:12:14 +0000716struct arm_pt_regs {
717 int uregs[18];
718};
719# define ARM_cpsr uregs[16]
720# define ARM_pc uregs[15]
721# define ARM_lr uregs[14]
722# define ARM_sp uregs[13]
723# define ARM_ip uregs[12]
724# define ARM_fp uregs[11]
725# define ARM_r10 uregs[10]
726# define ARM_r9 uregs[9]
727# define ARM_r8 uregs[8]
728# define ARM_r7 uregs[7]
729# define ARM_r6 uregs[6]
730# define ARM_r5 uregs[5]
731# define ARM_r4 uregs[4]
732# define ARM_r3 uregs[3]
733# define ARM_r2 uregs[2]
734# define ARM_r1 uregs[1]
735# define ARM_r0 uregs[0]
736# define ARM_ORIG_r0 uregs[17]
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100737static union {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100738 struct user_pt_regs aarch64_r;
739 struct arm_pt_regs arm_r;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100740} arm_regs_union;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100741# define aarch64_regs arm_regs_union.aarch64_r
742# define arm_regs arm_regs_union.arm_r
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100743static struct iovec aarch64_io = {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100744 .iov_base = &arm_regs_union
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100745};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100746#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100747static long alpha_r0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100748static long alpha_a3;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100749#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100750static struct pt_regs avr32_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100751#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100752struct pt_regs sparc_regs; /* not static */
Denys Vlasenko523635f2012-02-25 02:44:25 +0100753#elif defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100754static long long mips_a3;
755static long long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100756#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100757static long mips_a3;
758static long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100759#elif defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +0200760static long s390_gpr2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100761#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100762static long hppa_r28;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100763#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100764static long sh_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100765#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100766static long sh64_r9;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100767#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100768static long cris_r10;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100769#elif defined(TILE)
770struct pt_regs tile_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100771#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100772static long microblaze_r3;
Christian Svensson492f81f2013-02-14 13:26:27 +0100773#elif defined(OR1K)
774static struct user_regs_struct or1k_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000775# define ARCH_REGS_FOR_GETREGSET or1k_regs
James Hogan5f999a82013-02-22 14:44:10 +0000776#elif defined(METAG)
777static struct user_gp_regs metag_regs;
Dmitry V. Levinb787b102013-03-18 10:17:14 +0000778# define ARCH_REGS_FOR_GETREGSET metag_regs
Chris Zankel8f636ed2013-03-25 10:22:07 -0700779#elif defined(XTENSA)
780static long xtensa_a2;
Vineet Gupta7daacbb2013-08-16 12:47:06 +0530781# elif defined(ARC)
782static struct user_regs_struct arc_regs;
783# define ARCH_REGS_FOR_GETREGSET arc_regs
Denys Vlasenko523635f2012-02-25 02:44:25 +0100784#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000785
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100786void
Denys Vlasenko5a2483b2013-07-01 12:49:14 +0200787print_pc(struct tcb *tcp)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100788{
789#define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
790 sizeof(long) == 8 ? "[????????????????] " : \
791 NULL /* crash */)
792 if (get_regs_error) {
793 PRINTBADPC;
794 return;
795 }
796#if defined(I386)
797 tprintf("[%08lx] ", i386_regs.eip);
798#elif defined(S390) || defined(S390X)
799 long psw;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200800 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100801 PRINTBADPC;
802 return;
803 }
804# ifdef S390
805 tprintf("[%08lx] ", psw);
806# elif S390X
Dmitry V. Levinddba73e2013-02-05 19:01:58 +0000807 tprintf("[%016lx] ", psw);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100808# endif
809#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100810 if (x86_io.iov_len == sizeof(i386_regs)) {
811 tprintf("[%08x] ", (unsigned) i386_regs.eip);
812 } else {
813# if defined(X86_64)
814 tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
815# elif defined(X32)
816 /* Note: this truncates 64-bit rip to 32 bits */
817 tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip);
818# endif
819 }
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100820#elif defined(IA64)
821 long ip;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200822 if (upeek(tcp->pid, PT_B0, &ip) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100823 PRINTBADPC;
824 return;
825 }
826 tprintf("[%08lx] ", ip);
827#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +0200828 long pc = ppc_regs.nip;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100829# ifdef POWERPC64
830 tprintf("[%016lx] ", pc);
831# else
832 tprintf("[%08lx] ", pc);
833# endif
834#elif defined(M68K)
835 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200836 if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100837 tprints("[????????] ");
838 return;
839 }
840 tprintf("[%08lx] ", pc);
841#elif defined(ALPHA)
842 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200843 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100844 tprints("[????????????????] ");
845 return;
846 }
847 tprintf("[%08lx] ", pc);
848#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100849 tprintf("[%08lx] ", sparc_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100850#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100851 tprintf("[%08lx] ", sparc_regs.tpc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100852#elif defined(HPPA)
853 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200854 if (upeek(tcp->pid, PT_IAOQ0, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100855 tprints("[????????] ");
856 return;
857 }
858 tprintf("[%08lx] ", pc);
859#elif defined(MIPS)
860 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200861 if (upeek(tcp->pid, REG_EPC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100862 tprints("[????????] ");
863 return;
864 }
865 tprintf("[%08lx] ", pc);
866#elif defined(SH)
867 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200868 if (upeek(tcp->pid, 4*REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100869 tprints("[????????] ");
870 return;
871 }
872 tprintf("[%08lx] ", pc);
873#elif defined(SH64)
874 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200875 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100876 tprints("[????????????????] ");
877 return;
878 }
879 tprintf("[%08lx] ", pc);
880#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100881 tprintf("[%08lx] ", arm_regs.ARM_pc);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100882#elif defined(AARCH64)
883 /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100884#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100885 tprintf("[%08lx] ", avr32_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100886#elif defined(BFIN)
887 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200888 if (upeek(tcp->pid, PT_PC, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100889 PRINTBADPC;
890 return;
891 }
892 tprintf("[%08lx] ", pc);
893#elif defined(CRISV10)
894 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200895 if (upeek(tcp->pid, 4*PT_IRP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100896 PRINTBADPC;
897 return;
898 }
899 tprintf("[%08lx] ", pc);
900#elif defined(CRISV32)
901 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200902 if (upeek(tcp->pid, 4*PT_ERP, &pc) < 0) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100903 PRINTBADPC;
904 return;
905 }
906 tprintf("[%08lx] ", pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100907#elif defined(TILE)
908# ifdef _LP64
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500909 tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100910# else
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500911 tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100912# endif
Christian Svensson492f81f2013-02-14 13:26:27 +0100913#elif defined(OR1K)
914 tprintf("[%08lx] ", or1k_regs.pc);
James Hogan5f999a82013-02-22 14:44:10 +0000915#elif defined(METAG)
916 tprintf("[%08lx] ", metag_regs.pc);
Chris Zankel8f636ed2013-03-25 10:22:07 -0700917#elif defined(XTENSA)
918 long pc;
Denys Vlasenko752e5a02013-06-28 14:35:47 +0200919 if (upeek(tcp->pid, REG_PC, &pc) < 0) {
Chris Zankel8f636ed2013-03-25 10:22:07 -0700920 PRINTBADPC;
921 return;
922 }
923 tprintf("[%08lx] ", pc);
Vineet Gupta7daacbb2013-08-16 12:47:06 +0530924#elif defined(ARC)
925 tprintf("[%08lx] ", arc_regs.efa);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100926#endif /* architecture */
927}
928
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000929/*
930 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
931 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
Denys Vlasenko7270de52013-02-21 15:46:34 +0100932 */
Elliott Hughes7a28f7f2014-03-03 23:45:26 +0000933#if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
Denys Vlasenko7270de52013-02-21 15:46:34 +0100934static long
935shuffle_scno(unsigned long scno)
936{
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000937 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
Denys Vlasenko7270de52013-02-21 15:46:34 +0100938 return scno;
939
940 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000941 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
Denys Vlasenko7270de52013-02-21 15:46:34 +0100942 return 0x000ffff0;
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000943 if (scno == 0x000ffff0)
944 return ARM_FIRST_SHUFFLED_SYSCALL;
Denys Vlasenko7270de52013-02-21 15:46:34 +0100945
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000946#define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
947 /*
948 * Is it ARM specific syscall?
949 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
950 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
Denys Vlasenko7270de52013-02-21 15:46:34 +0100951 */
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000952 if (scno >= 0x000f0000 &&
953 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
954 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
Denys Vlasenko7270de52013-02-21 15:46:34 +0100955 }
Dmitry V. Levincf7248d2015-01-11 17:26:29 +0000956 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
957 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
Denys Vlasenko7270de52013-02-21 15:46:34 +0100958 }
959
960 return scno;
961}
962#else
963# define shuffle_scno(scno) ((long)(scno))
964#endif
965
966static char*
967undefined_scno_name(struct tcb *tcp)
968{
969 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
970
971 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
972 return buf;
973}
974
Anton Blanchard14d51a62013-06-26 14:42:37 +1000975#ifdef POWERPC
976/*
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200977 * PTRACE_GETREGS was added to the PowerPC kernel in v2.6.23,
978 * we provide a slow fallback for old kernels.
Anton Blanchard14d51a62013-06-26 14:42:37 +1000979 */
Denys Vlasenko7f5a1322013-06-28 14:36:39 +0200980static int powerpc_getregs_old(pid_t pid)
Anton Blanchard14d51a62013-06-26 14:42:37 +1000981{
982 int i;
983 long r;
984
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200985 if (iflag) {
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200986 r = upeek(pid, sizeof(long) * PT_NIP, (long *)&ppc_regs.nip);
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200987 if (r)
988 goto out;
989 }
990#ifdef POWERPC64 /* else we never use it */
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200991 r = upeek(pid, sizeof(long) * PT_MSR, (long *)&ppc_regs.msr);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000992 if (r)
993 goto out;
Denys Vlasenko6b3016e2013-06-28 14:51:50 +0200994#endif
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200995 r = upeek(pid, sizeof(long) * PT_CCR, (long *)&ppc_regs.ccr);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000996 if (r)
997 goto out;
Anton Blanchard9459dfb2013-07-12 12:24:02 +0200998 r = upeek(pid, sizeof(long) * PT_ORIG_R3, (long *)&ppc_regs.orig_gpr3);
Anton Blanchard14d51a62013-06-26 14:42:37 +1000999 if (r)
1000 goto out;
Anton Blanchard14d51a62013-06-26 14:42:37 +10001001 for (i = 0; i <= 8; i++) {
Anton Blanchard9459dfb2013-07-12 12:24:02 +02001002 r = upeek(pid, sizeof(long) * (PT_R0 + i),
1003 (long *)&ppc_regs.gpr[i]);
Anton Blanchard14d51a62013-06-26 14:42:37 +10001004 if (r)
1005 goto out;
1006 }
Denys Vlasenko7f5a1322013-06-28 14:36:39 +02001007 out:
Anton Blanchard14d51a62013-06-26 14:42:37 +10001008 return r;
1009}
1010#endif
1011
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001012#ifndef get_regs
1013long get_regs_error;
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001014
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001015static void get_regset(pid_t pid)
1016{
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001017/* constant iovec */
1018# if defined(ARM) \
1019 || defined(I386) \
1020 || defined(METAG) \
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301021 || defined(OR1K) \
1022 || defined(ARC)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001023 static struct iovec io = {
1024 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1025 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001026 };
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001027 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1028
1029/* variable iovec */
1030# elif defined(X86_64) || defined(X32)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001031 /* x86_io.iov_base = &x86_regs_union; - already is */
1032 x86_io.iov_len = sizeof(x86_regs_union);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001033 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &x86_io);
1034# elif defined(AARCH64)
1035 /* aarch64_io.iov_base = &arm_regs_union; - already is */
1036 aarch64_io.iov_len = sizeof(arm_regs_union);
1037 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &aarch64_io);
1038# else
1039# warning both PTRACE_GETREGSET and NT_PRSTATUS are available but not yet used
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001040# endif
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001041}
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001042
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001043void
1044get_regs(pid_t pid)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001045{
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001046/* PTRACE_GETREGSET only */
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301047# if defined(METAG) || defined(OR1K) || defined(X32) || defined(AARCH64) || defined(ARC)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001048 get_regset(pid);
1049
1050/* PTRACE_GETREGS only */
1051# elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001052 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001053# elif defined(TILE)
1054 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &tile_regs);
1055# elif defined(SPARC) || defined(SPARC64)
1056 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001057# elif defined(POWERPC)
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001058 static bool old_kernel = 0;
1059 if (old_kernel)
1060 goto old;
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001061 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &ppc_regs);
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001062 if (get_regs_error && errno == EIO) {
1063 old_kernel = 1;
1064 old:
Anton Blanchard14d51a62013-06-26 14:42:37 +10001065 get_regs_error = powerpc_getregs_old(pid);
Denys Vlasenko7eb89322013-06-28 14:41:30 +02001066 }
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001067
1068/* try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS */
1069# else
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001070 static int getregset_support;
1071
1072 if (getregset_support >= 0) {
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001073 get_regset(pid);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001074 if (getregset_support > 0)
1075 return;
1076 if (get_regs_error >= 0) {
1077 getregset_support = 1;
1078 return;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001079 }
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001080 if (errno == EPERM || errno == ESRCH)
1081 return;
1082 getregset_support = -1;
1083 }
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001084# if defined(ARM)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001085 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &arm_regs);
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001086# elif defined(I386)
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001087 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &i386_regs);
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001088# elif defined(X86_64)
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001089 /* Use old method, with unreliable heuristical detection of 32-bitness. */
1090 x86_io.iov_len = sizeof(x86_64_regs);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001091 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &x86_64_regs);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001092 if (!get_regs_error && x86_64_regs.cs == 0x23) {
1093 x86_io.iov_len = sizeof(i386_regs);
1094 /*
1095 * The order is important: i386_regs and x86_64_regs
1096 * are overlaid in memory!
1097 */
1098 i386_regs.ebx = x86_64_regs.rbx;
1099 i386_regs.ecx = x86_64_regs.rcx;
1100 i386_regs.edx = x86_64_regs.rdx;
1101 i386_regs.esi = x86_64_regs.rsi;
1102 i386_regs.edi = x86_64_regs.rdi;
1103 i386_regs.ebp = x86_64_regs.rbp;
1104 i386_regs.eax = x86_64_regs.rax;
1105 /* i386_regs.xds = x86_64_regs.ds; unused by strace */
1106 /* i386_regs.xes = x86_64_regs.es; ditto... */
1107 /* i386_regs.xfs = x86_64_regs.fs; */
1108 /* i386_regs.xgs = x86_64_regs.gs; */
1109 i386_regs.orig_eax = x86_64_regs.orig_rax;
1110 i386_regs.eip = x86_64_regs.rip;
1111 /* i386_regs.xcs = x86_64_regs.cs; */
1112 /* i386_regs.eflags = x86_64_regs.eflags; */
1113 i386_regs.esp = x86_64_regs.rsp;
1114 /* i386_regs.xss = x86_64_regs.ss; */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001115 }
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001116# else
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001117# error unhandled architecture
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001118# endif /* ARM || I386 || X86_64 */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001119# endif
1120}
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001121#endif /* !get_regs */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001122
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001123/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001124 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1125 * 1: ok, continue in trace_syscall_entering().
1126 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001127 * ("????" etc) and bail out.
1128 */
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01001129static int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001130get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001131{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001132 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001133
Denys Vlasenko523635f2012-02-25 02:44:25 +01001134#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001135 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001136 return -1;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001137
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001138 if (s390_gpr2 != -ENOSYS) {
Roland McGrath2f924ca2003-06-26 22:23:28 +00001139 /*
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001140 * Since kernel version 2.5.44 the scno gets passed in gpr2.
Roland McGrath2f924ca2003-06-26 22:23:28 +00001141 */
Denys Vlasenkof5730e92013-07-07 12:47:39 +02001142 scno = s390_gpr2;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001143 } else {
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001144 /*
Michal Ludvig882eda82002-11-11 12:50:47 +00001145 * Old style of "passing" the scno via the SVC instruction.
1146 */
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001147 long psw;
Michal Ludvig882eda82002-11-11 12:50:47 +00001148 long opcode, offset_reg, tmp;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001149 void *svc_addr;
Denys Vlasenko7c9ba8b2011-08-19 19:46:32 +02001150 static const int gpr_offset[16] = {
1151 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
1152 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
1153 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
1154 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
1155 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001156
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001157 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0)
Michal Ludvig882eda82002-11-11 12:50:47 +00001158 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001159 errno = 0;
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001160 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +00001161 if (errno) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001162 perror_msg("peektext(psw-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +00001163 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001164 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001165
1166 /*
1167 * We have to check if the SVC got executed directly or via an
1168 * EXECUTE instruction. In case of EXECUTE it is necessary to do
1169 * instruction decoding to derive the system call number.
1170 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1171 * so that this doesn't work if a SVC opcode is part of an EXECUTE
1172 * opcode. Since there is no way to find out the opcode size this
1173 * is the best we can do...
1174 */
Michal Ludvig882eda82002-11-11 12:50:47 +00001175 if ((opcode & 0xff00) == 0x0a00) {
1176 /* SVC opcode */
1177 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +00001178 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001179 else {
1180 /* SVC got executed by EXECUTE instruction */
1181
1182 /*
1183 * Do instruction decoding of EXECUTE. If you really want to
1184 * understand this, read the Principles of Operations.
1185 */
1186 svc_addr = (void *) (opcode & 0xfff);
1187
1188 tmp = 0;
1189 offset_reg = (opcode & 0x000f0000) >> 16;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001190 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001191 return -1;
1192 svc_addr += tmp;
1193
1194 tmp = 0;
1195 offset_reg = (opcode & 0x0000f000) >> 12;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001196 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001197 return -1;
1198 svc_addr += tmp;
1199
Denys Vlasenkofb036672009-01-23 16:30:26 +00001200 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
Michal Ludvig882eda82002-11-11 12:50:47 +00001201 if (errno)
1202 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001203# if defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +00001204 scno >>= 48;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001205# else
Michal Ludvig882eda82002-11-11 12:50:47 +00001206 scno >>= 16;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001207# endif
Michal Ludvig882eda82002-11-11 12:50:47 +00001208 tmp = 0;
1209 offset_reg = (opcode & 0x00f00000) >> 20;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001210 if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001211 return -1;
1212
1213 scno = (scno | tmp) & 0xff;
1214 }
1215 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001216#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001217 scno = ppc_regs.gpr[0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001218# ifdef POWERPC64
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001219 unsigned int currpers;
Andreas Schwabd69fa492010-07-12 21:39:57 +02001220
James Yanga78d7772014-02-18 15:32:43 -06001221 /*
1222 * Check for 64/32 bit mode.
1223 * Embedded implementations covered by Book E extension of PPC use
1224 * bit 0 (CM) of 32-bit Machine state register (MSR).
1225 * Other implementations use bit 0 (SF) of 64-bit MSR.
1226 */
1227 currpers = (ppc_regs.msr & 0x8000000080000000) ? 0 : 1;
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001228 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001229# endif
1230#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001231 scno = avr32_regs.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001232#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001233 if (upeek(tcp->pid, PT_ORIG_P0, &scno))
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001234 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001235#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001236 scno = i386_regs.orig_eax;
H.J. Lu35be5812012-04-16 13:00:01 +02001237#elif defined(X86_64) || defined(X32)
1238# ifndef __X32_SYSCALL_BIT
1239# define __X32_SYSCALL_BIT 0x40000000
1240# endif
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001241 unsigned int currpers;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001242# if 1
1243 /* GETREGSET of NT_PRSTATUS tells us regset size,
1244 * which unambiguously detects i386.
1245 *
1246 * Linux kernel distinguishes x86-64 and x32 processes
1247 * solely by looking at __X32_SYSCALL_BIT:
1248 * arch/x86/include/asm/compat.h::is_x32_task():
1249 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1250 * return true;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001251 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001252 if (x86_io.iov_len == sizeof(i386_regs)) {
1253 scno = i386_regs.orig_eax;
1254 currpers = 1;
1255 } else {
1256 scno = x86_64_regs.orig_rax;
1257 currpers = 0;
1258 if (scno & __X32_SYSCALL_BIT) {
Dmitry V. Levinc3948322015-02-05 16:15:46 +00001259 /*
1260 * Syscall number -1 requires special treatment:
1261 * it might be a side effect of SECCOMP_RET_ERRNO
1262 * filtering that sets orig_rax to -1
1263 * in some versions of linux kernel.
1264 * If that is the case, then
1265 * __X32_SYSCALL_BIT logic does not apply.
1266 */
1267 if ((long long) x86_64_regs.orig_rax != -1) {
1268 scno -= __X32_SYSCALL_BIT;
1269 currpers = 2;
1270 } else {
1271# ifdef X32
1272 currpers = 2;
1273# endif
1274 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001275 }
1276 }
1277# elif 0
1278 /* cs = 0x33 for long mode (native 64 bit and x32)
1279 * cs = 0x23 for compatibility mode (32 bit)
1280 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1281 */
1282 scno = x86_64_regs.orig_rax;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001283 switch (x86_64_regs.cs) {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001284 case 0x23: currpers = 1; break;
H.J. Lu35be5812012-04-16 13:00:01 +02001285 case 0x33:
1286 if (x86_64_regs.ds == 0x2b) {
1287 currpers = 2;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001288 scno &= ~__X32_SYSCALL_BIT;
H.J. Lu35be5812012-04-16 13:00:01 +02001289 } else
1290 currpers = 0;
1291 break;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001292 default:
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001293 fprintf(stderr, "Unknown value CS=0x%08X while "
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001294 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001295 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001296 currpers = current_personality;
1297 break;
1298 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001299# elif 0
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001300 /* This version analyzes the opcode of a syscall instruction.
1301 * (int 0x80 on i386 vs. syscall on x86-64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001302 * It works, but is too complicated, and strictly speaking, unreliable.
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001303 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001304 unsigned long call, rip = x86_64_regs.rip;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001305 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1306 rip -= 2;
1307 errno = 0;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001308 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001309 if (errno)
1310 fprintf(stderr, "ptrace_peektext failed: %s\n",
1311 strerror(errno));
1312 switch (call & 0xffff) {
1313 /* x86-64: syscall = 0x0f 0x05 */
1314 case 0x050f: currpers = 0; break;
1315 /* i386: int 0x80 = 0xcd 0x80 */
1316 case 0x80cd: currpers = 1; break;
1317 default:
1318 currpers = current_personality;
1319 fprintf(stderr,
1320 "Unknown syscall opcode (0x%04X) while "
1321 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001322 "PID=%d\n", (int)call, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001323 break;
1324 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001325# endif
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001326
H.J. Lu35be5812012-04-16 13:00:01 +02001327# ifdef X32
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001328 /* If we are built for a x32 system, then personality 0 is x32
1329 * (not x86_64), and stracing of x86_64 apps is not supported.
1330 * Stracing of i386 apps is still supported.
H.J. Lu085e4282012-04-17 11:05:04 -07001331 */
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001332 if (currpers == 0) {
1333 fprintf(stderr, "syscall_%lu(...) in unsupported "
1334 "64-bit mode of process PID=%d\n",
1335 scno, tcp->pid);
1336 return 0;
H.J. Lu35be5812012-04-16 13:00:01 +02001337 }
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001338 currpers &= ~2; /* map 2,1 to 0,1 */
H.J. Lu35be5812012-04-16 13:00:01 +02001339# endif
H.J. Lu085e4282012-04-17 11:05:04 -07001340 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001341#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001342# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01001343 long psr;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001344 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001345 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
1346 if (ia64_ia32mode) {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001347 if (upeek(tcp->pid, PT_R1, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001348 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001349 } else {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001350 if (upeek(tcp->pid, PT_R15, &scno) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001351 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001352 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001353#elif defined(AARCH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001354 switch (aarch64_io.iov_len) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001355 case sizeof(aarch64_regs):
1356 /* We are in 64-bit mode */
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001357 scno = aarch64_regs.regs[8];
1358 update_personality(tcp, 1);
1359 break;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001360 case sizeof(arm_regs):
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001361 /* We are in 32-bit mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001362 /* Note: we don't support OABI, unlike 32-bit ARM build */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001363 scno = arm_regs.ARM_r7;
Elliott Hughes7a28f7f2014-03-03 23:45:26 +00001364 scno = shuffle_scno(scno);
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001365 update_personality(tcp, 0);
1366 break;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001367 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001368#elif defined(ARM)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001369 if (arm_regs.ARM_ip != 0) {
1370 /* It is not a syscall entry */
1371 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001372 tcp->flags |= TCB_INSYSCALL;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001373 return 0;
1374 }
1375 /* Note: we support only 32-bit CPUs, not 26-bit */
1376
Dmitry V. Levin0160e162014-03-18 23:37:43 +00001377# if !defined(__ARM_EABI__) || ENABLE_ARM_OABI
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001378 if (arm_regs.ARM_cpsr & 0x20)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001379 /* Thumb mode */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001380 goto scno_in_r7;
1381 /* ARM mode */
1382 /* Check EABI/OABI by examining SVC insn's low 24 bits */
1383 errno = 0;
1384 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1385 if (errno)
1386 return -1;
1387 /* EABI syscall convention? */
Dmitry V. Levin03b44542015-01-14 07:21:36 +00001388 if ((unsigned long) scno != 0xef000000) {
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001389 /* No, it's OABI */
1390 if ((scno & 0x0ff00000) != 0x0f900000) {
1391 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1392 tcp->pid, scno);
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001393 return -1;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001394 }
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001395 /* Fixup the syscall number */
1396 scno &= 0x000fffff;
1397 } else {
1398 scno_in_r7:
1399 scno = arm_regs.ARM_r7;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001400 }
Dmitry V. Levin0160e162014-03-18 23:37:43 +00001401# else /* __ARM_EABI__ || !ENABLE_ARM_OABI */
Denys Vlasenko8b7aa2b2013-07-04 09:54:19 +02001402 scno = arm_regs.ARM_r7;
1403# endif
Denys Vlasenko7270de52013-02-21 15:46:34 +01001404 scno = shuffle_scno(scno);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001405#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001406 if (upeek(tcp->pid, 4*PT_ORIG_D0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001407 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001408#elif defined(LINUX_MIPSN32)
Roland McGrath542c2c62008-05-20 01:11:56 +00001409 unsigned long long regs[38];
1410
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001411 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001412 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001413 mips_a3 = regs[REG_A3];
1414 mips_r2 = regs[REG_V0];
Roland McGrath542c2c62008-05-20 01:11:56 +00001415
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001416 scno = mips_r2;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001417 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001418 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001419 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001420 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001421 return 0;
1422 }
Roland McGrath542c2c62008-05-20 01:11:56 +00001423 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001424#elif defined(MIPS)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001425 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001426 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001427 if (upeek(tcp->pid, REG_V0, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001428 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001429
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001430 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001431 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001432 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001433 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001434 return 0;
1435 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001436 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001437#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001438 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001439 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001440 if (upeek(tcp->pid, REG_R0, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001441 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001442
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001443 /*
1444 * Do some sanity checks to figure out if it's
1445 * really a syscall entry
1446 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001447 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001448 if (alpha_a3 == 0 || alpha_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001449 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001450 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001451 return 0;
1452 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001453 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001454#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001455 /* Disassemble the syscall trap. */
1456 /* Retrieve the syscall trap instruction. */
Denys Vlasenko46455822013-02-05 17:02:59 +01001457 unsigned long trap;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001458 errno = 0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001459# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001460 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001461 trap >>= 32;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001462# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001463 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001464# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001465 if (errno)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001466 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001467
1468 /* Disassemble the trap to see what personality to use. */
1469 switch (trap) {
1470 case 0x91d02010:
1471 /* Linux/SPARC syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001472 update_personality(tcp, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001473 break;
1474 case 0x91d0206d:
1475 /* Linux/SPARC64 syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001476 update_personality(tcp, 2);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001477 break;
1478 case 0x91d02000:
1479 /* SunOS syscall trap. (pers 1) */
1480 fprintf(stderr, "syscall: SunOS no support\n");
1481 return -1;
1482 case 0x91d02008:
1483 /* Solaris 2.x syscall trap. (per 2) */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001484 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001485 break;
1486 case 0x91d02009:
1487 /* NetBSD/FreeBSD syscall trap. */
1488 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1489 return -1;
1490 case 0x91d02027:
1491 /* Solaris 2.x gettimeofday */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001492 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001493 break;
1494 default:
Denys Vlasenko523635f2012-02-25 02:44:25 +01001495# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001496 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001497# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001498 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001499# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001500 return -1;
1501 }
1502
1503 /* Extract the system call number from the registers. */
1504 if (trap == 0x91d02027)
1505 scno = 156;
1506 else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001507 scno = sparc_regs.u_regs[U_REG_G1];
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001508 if (scno == 0) {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001509 scno = sparc_regs.u_regs[U_REG_O0];
1510 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 +02001511 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001512#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001513 if (upeek(tcp->pid, PT_GR20, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001514 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001515#elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001516 /*
1517 * In the new syscall ABI, the system call number is in R3.
1518 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001519 if (upeek(tcp->pid, 4*(REG_REG0+3), &scno) < 0)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001520 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001521
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001522 if (scno < 0) {
1523 /* Odd as it may seem, a glibc bug has been known to cause
1524 glibc to issue bogus negative syscall numbers. So for
1525 our purposes, make strace print what it *should* have been */
1526 long correct_scno = (scno & 0xff);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001527 if (debug_flag)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001528 fprintf(stderr,
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001529 "Detected glibc bug: bogus system call"
1530 " number = %ld, correcting to %ld\n",
1531 scno,
1532 correct_scno);
1533 scno = correct_scno;
1534 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001535#elif defined(SH64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001536 if (upeek(tcp->pid, REG_SYSCALL, &scno) < 0)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001537 return -1;
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001538 scno &= 0xFFFF;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001539#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001540 if (upeek(tcp->pid, 4*PT_R9, &scno) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001541 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001542#elif defined(TILE)
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00001543 unsigned int currpers;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001544 scno = tile_regs.regs[10];
1545# ifdef __tilepro__
1546 currpers = 1;
1547# else
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001548# ifndef PT_FLAGS_COMPAT
1549# define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1550# endif
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001551 if (tile_regs.flags & PT_FLAGS_COMPAT)
1552 currpers = 1;
1553 else
1554 currpers = 0;
1555# endif
1556 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001557#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001558 if (upeek(tcp->pid, 0, &scno) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001559 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001560#elif defined(OR1K)
1561 scno = or1k_regs.gpr[11];
James Hogan5f999a82013-02-22 14:44:10 +00001562#elif defined(METAG)
1563 scno = metag_regs.dx[0][1]; /* syscall number in D1Re0 (D1.0) */
Chris Zankel8f636ed2013-03-25 10:22:07 -07001564#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001565 if (upeek(tcp->pid, SYSCALL_NR, &scno) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07001566 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05301567# elif defined(ARC)
1568 scno = arc_regs.scratch.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001569#endif
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001570
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001571 tcp->scno = scno;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001572 if (SCNO_IS_VALID(tcp->scno)) {
1573 tcp->s_ent = &sysent[scno];
1574 tcp->qual_flg = qual_flags[scno];
1575 } else {
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01001576 static const struct_sysent unknown = {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001577 .nargs = MAX_ARGS,
1578 .sys_flags = 0,
1579 .sys_func = printargs,
1580 .sys_name = "unknown", /* not used */
1581 };
1582 tcp->s_ent = &unknown;
1583 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1584 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001585 return 1;
1586}
1587
Dmitry V. Levind70c41d2015-02-05 23:43:06 +00001588/*
1589 * Cannot rely on __kernel_[u]long_t being defined,
1590 * it is quite a recent feature of <asm/posix_types.h>.
1591 */
1592#ifdef __kernel_long_t
1593typedef __kernel_long_t kernel_long_t;
1594typedef __kernel_ulong_t kernel_ulong_t;
1595#else
1596# ifdef X32
1597typedef long long kernel_long_t;
1598typedef unsigned long long kernel_ulong_t;
1599# else
1600typedef long kernel_long_t;
1601typedef unsigned long kernel_ulong_t;
1602# endif
1603#endif
1604
1605/*
1606 * Check the syscall return value register value for whether it is
1607 * a negated errno code indicating an error, or a success return value.
1608 */
1609static inline bool
1610is_negated_errno(kernel_ulong_t val)
1611{
Dmitry V. Levin09460522015-02-05 23:47:36 +00001612 /*
1613 * Thanks to SECCOMP_RET_DATA == 0xffff, abnormally large errno
1614 * values could be easily seen when a seccomp filter is used, e.g.
1615 * BPF_STMT(BPF_RET, SECCOMP_RET_ERRNO | SECCOMP_RET_DATA)
1616 */
1617 kernel_ulong_t max = -(kernel_long_t) 0x10000; /* SECCOMP_RET_DATA + 1 */
Dmitry V. Levind70c41d2015-02-05 23:43:06 +00001618
1619#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1620 if (current_wordsize < sizeof(val)) {
1621 val = (uint32_t) val;
1622 max = (uint32_t) max;
1623 }
1624#elif defined X32
1625 /*
1626 * current_wordsize is 4 even in personality 0 (native X32)
1627 * but truncation _must not_ be done in it.
1628 * can't check current_wordsize here!
1629 */
1630 if (current_personality != 0) {
1631 val = (uint32_t) val;
1632 max = (uint32_t) max;
1633 }
1634#endif
1635
1636 return val > max;
1637}
1638
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02001639/* Called at each syscall entry.
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001640 * Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001641 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1642 * 1: ok, continue in trace_syscall_entering().
1643 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001644 * ("????" etc) and bail out.
1645 */
Roland McGratha4d48532005-06-08 20:45:28 +00001646static int
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001647syscall_fixup_on_sysenter(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001648{
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001649 /* Do we have post-execve SIGTRAP suppressed? */
1650 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1651 return 1;
1652
Dmitry V. Levin8e398b62015-02-05 19:31:13 +00001653 /*
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001654 * No, unfortunately. Apply -ENOSYS heuristics.
1655 * We don't have to workaround SECCOMP_RET_ERRNO side effects
1656 * because any kernel with SECCOMP_RET_ERRNO support surely
1657 * implements PTRACE_O_TRACEEXEC.
Dmitry V. Levin8e398b62015-02-05 19:31:13 +00001658 */
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001659#if defined(I386)
1660 if (i386_regs.eax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001661 if (debug_flag)
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001662 fprintf(stderr, "not a syscall entry (eax = %ld)\n",
1663 i386_regs.eax);
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001664 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001665 }
H.J. Lu35be5812012-04-16 13:00:01 +02001666#elif defined(X86_64) || defined(X32)
Dmitry V. Levin8e398b62015-02-05 19:31:13 +00001667 if (x86_io.iov_len == sizeof(i386_regs)) {
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001668 if ((int) i386_regs.eax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001669 if (debug_flag)
Dmitry V. Levin8e398b62015-02-05 19:31:13 +00001670 fprintf(stderr,
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001671 "not a syscall entry (eax = %d)\n",
1672 (int) i386_regs.eax);
Dmitry V. Levin8e398b62015-02-05 19:31:13 +00001673 return 0;
1674 }
1675 } else {
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001676 if ((long long) x86_64_regs.rax != -ENOSYS) {
Dmitry V. Levin8e398b62015-02-05 19:31:13 +00001677 if (debug_flag)
1678 fprintf(stderr,
Dmitry V. Levin85dc4a12015-02-07 23:26:44 +00001679 "not a syscall entry (rax = %lld)\n",
1680 (long long) x86_64_regs.rax);
Denys Vlasenko18beb982011-08-24 16:59:23 +02001681 return 0;
1682 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001683 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001684#elif defined(M68K)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001685 /* TODO? Eliminate upeek's in arches below like we did in x86 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001686 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001687 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001688 if (m68k_d0 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001689 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001690 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001691 return 0;
1692 }
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001693#elif defined(IA64)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001694 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001695 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001696 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001697 return -1;
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001698 if (ia64_ia32mode && ia64_r8 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001699 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001700 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001701 return 0;
1702 }
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001703#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001704 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001705 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001706 if (cris_r10 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001707 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001708 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001709 return 0;
1710 }
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001711#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001712 if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001713 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001714 if (microblaze_r3 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001715 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001716 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001717 return 0;
1718 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001719#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001720 return 1;
1721}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001722
Denys Vlasenko146b9442012-03-18 22:10:48 +01001723static void
1724internal_fork(struct tcb *tcp)
1725{
1726#if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1727# define ARG_FLAGS 1
1728#else
1729# define ARG_FLAGS 0
1730#endif
1731#ifndef CLONE_UNTRACED
1732# define CLONE_UNTRACED 0x00800000
1733#endif
1734 if ((ptrace_setoptions
1735 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1736 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1737 return;
1738
1739 if (!followfork)
1740 return;
1741
1742 if (entering(tcp)) {
1743 /*
1744 * We won't see the new child if clone is called with
1745 * CLONE_UNTRACED, so we keep the same logic with that option
1746 * and don't trace it.
1747 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001748 if ((tcp->s_ent->sys_func == sys_clone)
1749 && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
1750 )
Denys Vlasenko146b9442012-03-18 22:10:48 +01001751 return;
1752 setbpt(tcp);
1753 } else {
1754 if (tcp->flags & TCB_BPTSET)
1755 clearbpt(tcp);
1756 }
1757}
1758
1759#if defined(TCB_WAITEXECVE)
1760static void
1761internal_exec(struct tcb *tcp)
1762{
1763 /* Maybe we have post-execve SIGTRAP suppressed? */
1764 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1765 return; /* yes, no need to do anything */
1766
1767 if (exiting(tcp) && syserror(tcp))
1768 /* Error in execve, no post-execve SIGTRAP expected */
1769 tcp->flags &= ~TCB_WAITEXECVE;
1770 else
1771 tcp->flags |= TCB_WAITEXECVE;
1772}
1773#endif
1774
1775static void
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001776syscall_fixup_for_fork_exec(struct tcb *tcp)
Roland McGrathc1e45922008-05-27 23:18:29 +00001777{
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001778 /*
1779 * We must always trace a few critical system calls in order to
1780 * correctly support following forks in the presence of tracing
1781 * qualifiers.
1782 */
1783 int (*func)();
1784
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001785 func = tcp->s_ent->sys_func;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001786
1787 if ( sys_fork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001788 || sys_clone == func
Denys Vlasenko146b9442012-03-18 22:10:48 +01001789 ) {
1790 internal_fork(tcp);
1791 return;
1792 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001793
Denys Vlasenko84703742012-02-25 02:38:52 +01001794#if defined(TCB_WAITEXECVE)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001795 if ( sys_execve == func
Denys Vlasenko84703742012-02-25 02:38:52 +01001796# if defined(SPARC) || defined(SPARC64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001797 || sys_execv == func
Denys Vlasenkoa7949742011-08-21 17:26:55 +02001798# endif
Denys Vlasenko146b9442012-03-18 22:10:48 +01001799 ) {
1800 internal_exec(tcp);
1801 return;
1802 }
Roland McGrathc1e45922008-05-27 23:18:29 +00001803#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001804}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001805
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001806/* Return -1 on error or 1 on success (never 0!) */
Roland McGratha4d48532005-06-08 20:45:28 +00001807static int
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001808get_syscall_args(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001809{
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001810 int i, nargs;
1811
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001812 nargs = tcp->s_ent->nargs;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001813
Denys Vlasenko523635f2012-02-25 02:44:25 +01001814#if defined(S390) || defined(S390X)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001815 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001816 if (upeek(tcp->pid, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001817 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001818#elif defined(ALPHA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001819 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001820 if (upeek(tcp->pid, REG_A0+i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001821 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001822#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02001823 if (!ia64_ia32mode) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001824 unsigned long *out0, cfm, sof, sol;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001825 long rbs_end;
1826 /* be backwards compatible with kernel < 2.4.4... */
1827# ifndef PT_RBS_END
1828# define PT_RBS_END PT_AR_BSP
1829# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001830
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001831 if (upeek(tcp->pid, PT_RBS_END, &rbs_end) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001832 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001833 if (upeek(tcp->pid, PT_CFM, (long *) &cfm) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001834 return -1;
1835
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001836 sof = (cfm >> 0) & 0x7f;
1837 sol = (cfm >> 7) & 0x7f;
1838 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1839
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001840 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001841 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1842 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1843 return -1;
1844 }
1845 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001846 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1847 PT_R9 /* ECX = out1 */,
1848 PT_R10 /* EDX = out2 */,
1849 PT_R14 /* ESI = out3 */,
1850 PT_R15 /* EDI = out4 */,
1851 PT_R13 /* EBP = out5 */};
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001852
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001853 for (i = 0; i < nargs; ++i) {
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001854 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001855 return -1;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001856 /* truncate away IVE sign-extension */
1857 tcp->u_arg[i] &= 0xffffffff;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001858 }
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001859 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001860#elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001861 /* N32 and N64 both use up to six registers. */
1862 unsigned long long regs[38];
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001863
1864 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1865 return -1;
1866
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001867 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001868 tcp->u_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001869# if defined(LINUX_MIPSN32)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001870 tcp->ext_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001871# endif
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001872 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001873#elif defined(MIPS)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001874 if (nargs > 4) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001875 long sp;
1876
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001877 if (upeek(tcp->pid, REG_SP, &sp) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001878 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001879 for (i = 0; i < 4; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001880 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001881 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001882 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001883 (char *)(tcp->u_arg + 4));
1884 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001885 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001886 if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001887 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001888 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001889#elif defined(POWERPC)
Anton Blanchardce6e33b2013-06-26 15:53:33 +02001890 (void)i;
1891 (void)nargs;
1892 tcp->u_arg[0] = ppc_regs.orig_gpr3;
1893 tcp->u_arg[1] = ppc_regs.gpr[4];
1894 tcp->u_arg[2] = ppc_regs.gpr[5];
1895 tcp->u_arg[3] = ppc_regs.gpr[6];
1896 tcp->u_arg[4] = ppc_regs.gpr[7];
1897 tcp->u_arg[5] = ppc_regs.gpr[8];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001898#elif defined(SPARC) || defined(SPARC64)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001899 for (i = 0; i < nargs; ++i)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001900 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001901#elif defined(HPPA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001902 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001903 if (upeek(tcp->pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001904 return -1;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001905#elif defined(ARM) || defined(AARCH64)
1906# if defined(AARCH64)
1907 if (tcp->currpers == 1)
1908 for (i = 0; i < nargs; ++i)
1909 tcp->u_arg[i] = aarch64_regs.regs[i];
1910 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001911# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001912 for (i = 0; i < nargs; ++i)
Denys Vlasenko401374e2013-02-06 18:24:39 +01001913 tcp->u_arg[i] = arm_regs.uregs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001914#elif defined(AVR32)
Denys Vlasenkob5b25892011-08-30 19:04:54 +02001915 (void)i;
1916 (void)nargs;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001917 tcp->u_arg[0] = avr32_regs.r12;
1918 tcp->u_arg[1] = avr32_regs.r11;
1919 tcp->u_arg[2] = avr32_regs.r10;
1920 tcp->u_arg[3] = avr32_regs.r9;
1921 tcp->u_arg[4] = avr32_regs.r5;
1922 tcp->u_arg[5] = avr32_regs.r3;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001923#elif defined(BFIN)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001924 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 +02001925
Denys Vlasenko4b887a52011-08-23 13:32:38 +02001926 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001927 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001928 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001929#elif defined(SH)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001930 static const int syscall_regs[MAX_ARGS] = {
1931 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1932 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001933 };
1934
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001935 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001936 if (upeek(tcp->pid, syscall_regs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001937 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001938#elif defined(SH64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001939 int i;
1940 /* Registers used by SH5 Linux system calls for parameters */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001941 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001942
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001943 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001944 if (upeek(tcp->pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001945 return -1;
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001946#elif defined(I386)
1947 (void)i;
1948 (void)nargs;
1949 tcp->u_arg[0] = i386_regs.ebx;
1950 tcp->u_arg[1] = i386_regs.ecx;
1951 tcp->u_arg[2] = i386_regs.edx;
1952 tcp->u_arg[3] = i386_regs.esi;
1953 tcp->u_arg[4] = i386_regs.edi;
1954 tcp->u_arg[5] = i386_regs.ebp;
H.J. Lu35be5812012-04-16 13:00:01 +02001955#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001956 (void)i;
1957 (void)nargs;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001958 if (x86_io.iov_len != sizeof(i386_regs)) {
1959 /* x86-64 or x32 ABI */
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001960 tcp->u_arg[0] = x86_64_regs.rdi;
1961 tcp->u_arg[1] = x86_64_regs.rsi;
1962 tcp->u_arg[2] = x86_64_regs.rdx;
1963 tcp->u_arg[3] = x86_64_regs.r10;
1964 tcp->u_arg[4] = x86_64_regs.r8;
1965 tcp->u_arg[5] = x86_64_regs.r9;
H.J. Lu35be5812012-04-16 13:00:01 +02001966# ifdef X32
1967 tcp->ext_arg[0] = x86_64_regs.rdi;
1968 tcp->ext_arg[1] = x86_64_regs.rsi;
1969 tcp->ext_arg[2] = x86_64_regs.rdx;
1970 tcp->ext_arg[3] = x86_64_regs.r10;
1971 tcp->ext_arg[4] = x86_64_regs.r8;
1972 tcp->ext_arg[5] = x86_64_regs.r9;
1973# endif
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001974 } else {
1975 /* i386 ABI */
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001976 /* Zero-extend from 32 bits */
1977 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1978 * if you need to use *sign-extended* parameter.
1979 */
1980 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1981 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1982 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1983 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1984 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1985 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001986 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001987#elif defined(MICROBLAZE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001988 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001989 if (upeek(tcp->pid, (5 + i) * 4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001990 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001991#elif defined(CRISV10) || defined(CRISV32)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001992 static const int crisregs[MAX_ARGS] = {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001993 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001994 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001995 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001996
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001997 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02001998 if (upeek(tcp->pid, crisregs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001999 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002000#elif defined(TILE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00002001 for (i = 0; i < nargs; ++i)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002002 tcp->u_arg[i] = tile_regs.regs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01002003#elif defined(M68K)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00002004 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002005 if (upeek(tcp->pid, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02002006 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01002007#elif defined(OR1K)
2008 (void)nargs;
2009 for (i = 0; i < 6; ++i)
2010 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
James Hogan5f999a82013-02-22 14:44:10 +00002011#elif defined(METAG)
2012 for (i = 0; i < nargs; i++)
2013 /* arguments go backwards from D1Ar1 (D1.3) */
2014 tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
Chris Zankel8f636ed2013-03-25 10:22:07 -07002015#elif defined(XTENSA)
2016 /* arg0: a6, arg1: a3, arg2: a4, arg3: a5, arg4: a8, arg5: a9 */
2017 static const int xtensaregs[MAX_ARGS] = { 6, 3, 4, 5, 8, 9 };
2018 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002019 if (upeek(tcp->pid, REG_A_BASE + xtensaregs[i], &tcp->u_arg[i]) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07002020 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05302021# elif defined(ARC)
2022 long *arc_args = &arc_regs.scratch.r0;
2023 for (i = 0; i < nargs; ++i)
2024 tcp->u_arg[i] = *arc_args--;
2025
Denys Vlasenko523635f2012-02-25 02:44:25 +01002026#else /* Other architecture (32bits specific) */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00002027 for (i = 0; i < nargs; ++i)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002028 if (upeek(tcp->pid, i*4, &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02002029 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002030#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00002031 return 1;
2032}
2033
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002034static int
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002035trace_syscall_entering(struct tcb *tcp)
2036{
2037 int res, scno_good;
2038
Denys Vlasenko2ce12ed2011-08-24 17:25:32 +02002039#if defined TCB_WAITEXECVE
2040 if (tcp->flags & TCB_WAITEXECVE) {
2041 /* This is the post-execve SIGTRAP. */
2042 tcp->flags &= ~TCB_WAITEXECVE;
2043 return 0;
2044 }
2045#endif
2046
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002047 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002048 if (res == 0)
2049 return res;
Denys Vlasenko907735a2012-03-21 00:23:16 +01002050 if (res == 1) {
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02002051 res = syscall_fixup_on_sysenter(tcp);
Denys Vlasenko907735a2012-03-21 00:23:16 +01002052 if (res == 0)
2053 return res;
2054 if (res == 1)
2055 res = get_syscall_args(tcp);
2056 }
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002057
2058 if (res != 1) {
2059 printleader(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002060 if (scno_good != 1)
Denys Vlasenkob7a6dae2012-03-20 16:48:35 +01002061 tprints("????" /* anti-trigraph gap */ "(");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002062 else if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002063 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002064 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002065 tprintf("%s(", tcp->s_ent->sys_name);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002066 /*
2067 * " <unavailable>" will be added later by the code which
2068 * detects ptrace errors.
2069 */
2070 goto ret;
2071 }
2072
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002073 if ( sys_execve == tcp->s_ent->sys_func
2074# if defined(SPARC) || defined(SPARC64)
2075 || sys_execv == tcp->s_ent->sys_func
2076# endif
2077 ) {
2078 hide_log_until_execve = 0;
2079 }
2080
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002081#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002082 while (1) {
Denys Vlasenko523635f2012-02-25 02:44:25 +01002083# ifdef SYS_socket_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002084 if (tcp->s_ent->sys_func == sys_socketcall) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00002085 decode_socket_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002086 break;
2087 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002088# endif
2089# ifdef SYS_ipc_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002090 if (tcp->s_ent->sys_func == sys_ipc) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00002091 decode_ipc_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002092 break;
2093 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002094# endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002095 break;
2096 }
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002097#endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00002098
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01002099 if (need_fork_exec_workarounds)
2100 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002101
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002102 if (!(tcp->qual_flg & QUAL_TRACE)
2103 || (tracing_paths && !pathtrace_match(tcp))
2104 ) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002105 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
2106 return 0;
2107 }
2108
2109 tcp->flags &= ~TCB_FILTERED;
2110
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002111 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002112 res = 0;
2113 goto ret;
2114 }
2115
Masatake YAMATOed69fc22014-04-16 15:33:35 +09002116#ifdef USE_LIBUNWIND
2117 if (stack_trace_enabled) {
2118 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
2119 unwind_capture_stacktrace(tcp);
2120 }
2121#endif
2122
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002123 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002124 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002125 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002126 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002127 tprintf("%s(", tcp->s_ent->sys_name);
2128 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002129 res = printargs(tcp);
2130 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002131 res = tcp->s_ent->sys_func(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002132
Dmitry V. Levinb742d8c2012-09-17 22:40:12 +00002133 fflush(tcp->outf);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002134 ret:
2135 tcp->flags |= TCB_INSYSCALL;
2136 /* Measure the entrance time as late as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002137 if (Tflag || cflag)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002138 gettimeofday(&tcp->etime, NULL);
2139 return res;
2140}
2141
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002142/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01002143 * 1: ok, continue in trace_syscall_exiting().
2144 * -1: error, trace_syscall_exiting() should print error indicator
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002145 * ("????" etc) and bail out.
2146 */
2147static int
2148get_syscall_result(struct tcb *tcp)
2149{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002150#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002151 if (upeek(tcp->pid, PT_GPR2, &s390_gpr2) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002152 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002153#elif defined(POWERPC)
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002154 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002155#elif defined(AVR32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002156 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002157#elif defined(BFIN)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002158 if (upeek(tcp->pid, PT_R0, &bfin_r0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002159 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002160#elif defined(I386)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002161 /* already done by get_regs */
H.J. Lu35be5812012-04-16 13:00:01 +02002162#elif defined(X86_64) || defined(X32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002163 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002164#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002165# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01002166 long psr;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002167 if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02002168 ia64_ia32mode = ((psr & IA64_PSR_IS) != 0);
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002169 if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002170 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002171 if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002172 return -1;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002173#elif defined(ARM)
2174 /* already done by get_regs */
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002175#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002176 /* register reading already done by get_regs */
2177
2178 /* Used to do this, but we did it on syscall entry already: */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002179 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
2180 * else it's personality 0.
2181 */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002182 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
Denys Vlasenko523635f2012-02-25 02:44:25 +01002183#elif defined(M68K)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002184 if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002185 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002186#elif defined(LINUX_MIPSN32)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002187 unsigned long long regs[38];
2188
2189 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
2190 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002191 mips_a3 = regs[REG_A3];
2192 mips_r2 = regs[REG_V0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01002193#elif defined(MIPS)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002194 if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002195 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002196 if (upeek(tcp->pid, REG_V0, &mips_r2) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002197 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002198#elif defined(ALPHA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002199 if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002200 return -1;
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002201 if (upeek(tcp->pid, REG_R0, &alpha_r0) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002202 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002203#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002204 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002205#elif defined(HPPA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002206 if (upeek(tcp->pid, PT_GR28, &hppa_r28) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002207 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002208#elif defined(SH)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002209 /* new syscall ABI returns result in R0 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002210 if (upeek(tcp->pid, 4*REG_REG0, (long *)&sh_r0) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002211 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002212#elif defined(SH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002213 /* ABI defines result returned in r9 */
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002214 if (upeek(tcp->pid, REG_GENERAL(9), (long *)&sh64_r9) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002215 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002216#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002217 if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002218 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002219#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002220 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002221#elif defined(MICROBLAZE)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002222 if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002223 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01002224#elif defined(OR1K)
2225 /* already done by get_regs */
James Hogan5f999a82013-02-22 14:44:10 +00002226#elif defined(METAG)
2227 /* already done by get_regs */
Chris Zankel8f636ed2013-03-25 10:22:07 -07002228#elif defined(XTENSA)
Denys Vlasenko752e5a02013-06-28 14:35:47 +02002229 if (upeek(tcp->pid, REG_A_BASE + 2, &xtensa_a2) < 0)
Chris Zankel8f636ed2013-03-25 10:22:07 -07002230 return -1;
Vineet Gupta7daacbb2013-08-16 12:47:06 +05302231#elif defined(ARC)
2232 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002233#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002234 return 1;
2235}
2236
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002237/* Called at each syscall exit */
2238static void
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002239syscall_fixup_on_sysexit(struct tcb *tcp)
2240{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002241#if defined(S390) || defined(S390X)
Denys Vlasenkoece98792011-08-25 10:25:35 +02002242 if ((tcp->flags & TCB_WAITEXECVE)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002243 && (s390_gpr2 == -ENOSYS || s390_gpr2 == tcp->scno)) {
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002244 /*
2245 * Return from execve.
2246 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2247 * flag set for the post-execve SIGTRAP to see and reset.
2248 */
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002249 s390_gpr2 = 0;
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002250 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002251#endif
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002252}
2253
Denys Vlasenko907735a2012-03-21 00:23:16 +01002254/* Returns:
2255 * 1: ok, continue in trace_syscall_exiting().
2256 * -1: error, trace_syscall_exiting() should print error indicator
2257 * ("????" etc) and bail out.
2258 */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002259static void
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002260get_error(struct tcb *tcp)
2261{
2262 int u_error = 0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002263 int check_errno = 1;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002264 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002265 check_errno = 0;
2266 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002267#if defined(S390) || defined(S390X)
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002268 if (check_errno && is_negated_errno(s390_gpr2)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002269 tcp->u_rval = -1;
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002270 u_error = -s390_gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002271 }
2272 else {
Denys Vlasenkof5730e92013-07-07 12:47:39 +02002273 tcp->u_rval = s390_gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002274 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002275#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002276 if (check_errno && is_negated_errno(i386_regs.eax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002277 tcp->u_rval = -1;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002278 u_error = -i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002279 }
2280 else {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002281 tcp->u_rval = i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002282 }
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00002283#elif defined(X86_64) || defined(X32)
2284 /*
2285 * In X32, return value is 64-bit (llseek uses one).
2286 * Using merely "long rax" would not work.
2287 */
2288 kernel_long_t rax;
2289
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002290 if (x86_io.iov_len == sizeof(i386_regs)) {
2291 /* Sign extend from 32 bits */
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00002292 rax = (int32_t) i386_regs.eax;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002293 } else {
2294 rax = x86_64_regs.rax;
2295 }
2296 if (check_errno && is_negated_errno(rax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002297 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002298 u_error = -rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002299 }
2300 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002301 tcp->u_rval = rax;
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00002302# ifdef X32
2303 /* tcp->u_rval contains a truncated value */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002304 tcp->u_lrval = rax;
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00002305# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002306 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002307#elif defined(IA64)
Denys Vlasenkoc09646a2013-07-01 12:28:17 +02002308 if (ia64_ia32mode) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002309 int err;
2310
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002311 err = (int)ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002312 if (check_errno && is_negated_errno(err)) {
2313 tcp->u_rval = -1;
2314 u_error = -err;
2315 }
2316 else {
2317 tcp->u_rval = err;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002318 }
2319 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002320 if (check_errno && ia64_r10) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002321 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002322 u_error = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002323 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002324 tcp->u_rval = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002325 }
2326 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002327#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002328 if (check_errno && mips_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002329 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002330 u_error = mips_r2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002331 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002332 tcp->u_rval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002333# if defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002334 tcp->u_lrval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002335# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002336 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002337#elif defined(POWERPC)
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002338 if (check_errno && (ppc_regs.ccr & 0x10000000)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002339 tcp->u_rval = -1;
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002340 u_error = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002341 }
2342 else {
Denys Vlasenko44a6d042013-06-28 16:47:38 +02002343 tcp->u_rval = ppc_regs.gpr[3];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002344 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002345#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002346 if (check_errno && is_negated_errno(m68k_d0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002347 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002348 u_error = -m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002349 }
2350 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002351 tcp->u_rval = m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002352 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002353#elif defined(ARM) || defined(AARCH64)
2354# if defined(AARCH64)
2355 if (tcp->currpers == 1) {
2356 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2357 tcp->u_rval = -1;
2358 u_error = -aarch64_regs.regs[0];
2359 }
2360 else {
2361 tcp->u_rval = aarch64_regs.regs[0];
2362 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002363 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002364 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002365# endif
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002366 {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002367 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002368 tcp->u_rval = -1;
Denys Vlasenko401374e2013-02-06 18:24:39 +01002369 u_error = -arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002370 }
2371 else {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002372 tcp->u_rval = arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002373 }
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002374 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002375#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002376 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002377 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002378 u_error = -avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002379 }
2380 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002381 tcp->u_rval = avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002382 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002383#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002384 if (check_errno && is_negated_errno(bfin_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002385 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002386 u_error = -bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002387 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002388 tcp->u_rval = bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002389 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002390#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002391 if (check_errno && alpha_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002392 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002393 u_error = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002394 }
2395 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002396 tcp->u_rval = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002397 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002398#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002399 if (check_errno && sparc_regs.psr & PSR_C) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002400 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002401 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002402 }
2403 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002404 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002405 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002406#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002407 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002408 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002409 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002410 }
2411 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002412 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002413 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002414#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002415 if (check_errno && is_negated_errno(hppa_r28)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002416 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002417 u_error = -hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002418 }
2419 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002420 tcp->u_rval = hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002421 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002422#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002423 if (check_errno && is_negated_errno(sh_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002424 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002425 u_error = -sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002426 }
2427 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002428 tcp->u_rval = sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002429 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002430#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002431 if (check_errno && is_negated_errno(sh64_r9)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002432 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002433 u_error = -sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002434 }
2435 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002436 tcp->u_rval = sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002437 }
James Hogan5f999a82013-02-22 14:44:10 +00002438#elif defined(METAG)
2439 /* result pointer in D0Re0 (D0.0) */
2440 if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2441 tcp->u_rval = -1;
2442 u_error = -metag_regs.dx[0][0];
2443 }
2444 else {
2445 tcp->u_rval = metag_regs.dx[0][0];
2446 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002447#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002448 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002449 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002450 u_error = -cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002451 }
2452 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002453 tcp->u_rval = cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002454 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002455#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002456 /*
2457 * The standard tile calling convention returns the value (or negative
2458 * errno) in r0, and zero (or positive errno) in r1.
2459 * Until at least kernel 3.8, however, the r1 value is not reflected
2460 * in ptregs at this point, so we use r0 here.
2461 */
2462 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002463 tcp->u_rval = -1;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002464 u_error = -tile_regs.regs[0];
2465 } else {
2466 tcp->u_rval = tile_regs.regs[0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002467 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002468#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002469 if (check_errno && is_negated_errno(microblaze_r3)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002470 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002471 u_error = -microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002472 }
2473 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002474 tcp->u_rval = microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002475 }
Christian Svensson492f81f2013-02-14 13:26:27 +01002476#elif defined(OR1K)
2477 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2478 tcp->u_rval = -1;
2479 u_error = -or1k_regs.gpr[11];
2480 }
2481 else {
2482 tcp->u_rval = or1k_regs.gpr[11];
2483 }
Chris Zankel8f636ed2013-03-25 10:22:07 -07002484#elif defined(XTENSA)
2485 if (check_errno && is_negated_errno(xtensa_a2)) {
2486 tcp->u_rval = -1;
2487 u_error = -xtensa_a2;
2488 }
2489 else {
2490 tcp->u_rval = xtensa_a2;
2491 }
Vineet Gupta7daacbb2013-08-16 12:47:06 +05302492#elif defined(ARC)
2493 if (check_errno && is_negated_errno(arc_regs.scratch.r0)) {
2494 tcp->u_rval = -1;
2495 u_error = -arc_regs.scratch.r0;
2496 }
2497 else {
2498 tcp->u_rval = arc_regs.scratch.r0;
2499 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002500#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002501 tcp->u_error = u_error;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002502}
2503
2504static void
2505dumpio(struct tcb *tcp)
2506{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002507 int (*func)();
2508
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002509 if (syserror(tcp))
2510 return;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +01002511 if ((unsigned long) tcp->u_arg[0] >= num_quals)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002512 return;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002513 func = tcp->s_ent->sys_func;
2514 if (func == printargs)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002515 return;
2516 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002517 if (func == sys_read ||
2518 func == sys_pread ||
2519 func == sys_recv ||
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002520 func == sys_recvfrom) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002521 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002522 return;
2523 } else if (func == sys_readv) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002524 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002525 return;
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002526#if HAVE_SENDMSG
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002527 } else if (func == sys_recvmsg) {
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002528 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002529 return;
2530 } else if (func == sys_recvmmsg) {
Masatake YAMATOa807dce2014-11-07 01:23:26 +09002531 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002532 return;
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002533#endif
Aurelien Jacobsdc521212015-01-31 23:57:00 +01002534 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002535 }
2536 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002537 if (func == sys_write ||
2538 func == sys_pwrite ||
2539 func == sys_send ||
2540 func == sys_sendto)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002541 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002542 else if (func == sys_writev)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002543 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002544#if HAVE_SENDMSG
2545 else if (func == sys_sendmsg)
2546 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
Masatake YAMATOa807dce2014-11-07 01:23:26 +09002547 else if (func == sys_sendmmsg)
2548 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
Masatake YAMATO02f9f6b2014-10-15 22:11:43 +09002549#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002550 }
2551}
2552
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002553static int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002554trace_syscall_exiting(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002555{
2556 int sys_res;
2557 struct timeval tv;
Denys Vlasenko1a5b5a72011-08-25 00:29:56 +02002558 int res;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002559 long u_error;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002560
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002561 /* Measure the exit time as early as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002562 if (Tflag || cflag)
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002563 gettimeofday(&tv, NULL);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002564
Masatake YAMATOed69fc22014-04-16 15:33:35 +09002565#ifdef USE_LIBUNWIND
2566 if (stack_trace_enabled) {
2567 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
2568 unwind_cache_invalidate(tcp);
2569 }
2570#endif
2571
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00002572#if SUPPORTED_PERSONALITIES > 1
2573 update_personality(tcp, tcp->currpers);
2574#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002575 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002576 if (res == 1) {
2577 syscall_fixup_on_sysexit(tcp); /* never fails */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002578 get_error(tcp); /* never fails */
2579 if (need_fork_exec_workarounds)
2580 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenko2a3d2752013-05-14 16:07:46 +02002581 if (filtered(tcp) || hide_log_until_execve)
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002582 goto ret;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002583 }
2584
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002585 if (cflag) {
Dmitry V. Levinac5133d2014-05-29 18:10:00 +00002586 count_syscall(tcp, &tv);
Denys Vlasenko7b609d52011-06-22 14:32:43 +02002587 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002588 goto ret;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002589 }
2590 }
2591
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002592 /* If not in -ff mode, and printing_tcp != tcp,
2593 * then the log currently does not end with output
2594 * of _our syscall entry_, but with something else.
2595 * We need to say which syscall's return is this.
2596 *
2597 * Forced reprinting via TCB_REPRINT is used only by
2598 * "strace -ff -oLOG test/threaded_execve" corner case.
2599 * It's the only case when -ff mode needs reprinting.
2600 */
2601 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2602 tcp->flags &= ~TCB_REPRINT;
2603 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002604 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002605 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002606 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002607 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002608 }
2609 printing_tcp = tcp;
2610
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002611 if (res != 1) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002612 /* There was error in one of prior ptrace ops */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002613 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002614 tabto();
Denys Vlasenko000b6012012-01-28 01:25:03 +01002615 tprints("= ? <unavailable>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002616 line_ended();
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002617 tcp->flags &= ~TCB_INSYSCALL;
2618 return res;
2619 }
2620
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002621 sys_res = 0;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002622 if (tcp->qual_flg & QUAL_RAW) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002623 /* sys_res = printargs(tcp); - but it's nop on sysexit */
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002624 } else {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002625 /* FIXME: not_failing_only (IOW, option -z) is broken:
2626 * failure of syscall is known only after syscall return.
2627 * Thus we end up with something like this on, say, ENOENT:
2628 * open("doesnt_exist", O_RDONLY <unfinished ...>
2629 * {next syscall decode}
2630 * whereas the intended result is that open(...) line
2631 * is not shown at all.
2632 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002633 if (not_failing_only && tcp->u_error)
Denys Vlasenko3b738812011-08-22 02:06:35 +02002634 goto ret; /* ignore failed syscalls */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002635 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002636 }
2637
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002638 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002639 tabto();
Denys Vlasenko3b738812011-08-22 02:06:35 +02002640 u_error = tcp->u_error;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002641 if (tcp->qual_flg & QUAL_RAW) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002642 if (u_error)
2643 tprintf("= -1 (errno %ld)", u_error);
2644 else
2645 tprintf("= %#lx", tcp->u_rval);
2646 }
2647 else if (!(sys_res & RVAL_NONE) && u_error) {
2648 switch (u_error) {
Denys Vlasenkofe585652012-01-12 11:26:34 +01002649 /* Blocked signals do not interrupt any syscalls.
2650 * In this case syscalls don't return ERESTARTfoo codes.
2651 *
2652 * Deadly signals set to SIG_DFL interrupt syscalls
2653 * and kill the process regardless of which of the codes below
2654 * is returned by the interrupted syscall.
2655 * In some cases, kernel forces a kernel-generated deadly
2656 * signal to be unblocked and set to SIG_DFL (and thus cause
2657 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2658 * or SIGILL. (The alternative is to leave process spinning
2659 * forever on the faulty instruction - not useful).
2660 *
2661 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2662 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2663 * but kernel will always restart them.
2664 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002665 case ERESTARTSYS:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002666 /* Most common type of signal-interrupted syscall exit code.
2667 * The system call will be restarted with the same arguments
2668 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2669 */
2670 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002671 break;
2672 case ERESTARTNOINTR:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002673 /* Rare. For example, fork() returns this if interrupted.
2674 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2675 */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002676 tprints("= ? ERESTARTNOINTR (To be restarted)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002677 break;
2678 case ERESTARTNOHAND:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002679 /* pause(), rt_sigsuspend() etc use this code.
2680 * SA_RESTART is ignored (assumed not set):
2681 * syscall won't restart (will return EINTR instead)
Denys Vlasenko30c03232013-02-19 16:59:26 +01002682 * even after signal with SA_RESTART set. However,
2683 * after SIG_IGN or SIG_DFL signal it will restart
2684 * (thus the name "restart only if has no handler").
Denys Vlasenkofe585652012-01-12 11:26:34 +01002685 */
Denys Vlasenkoaba62922013-03-05 16:56:35 +01002686 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002687 break;
2688 case ERESTART_RESTARTBLOCK:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002689 /* Syscalls like nanosleep(), poll() which can't be
2690 * restarted with their original arguments use this
2691 * code. Kernel will execute restart_syscall() instead,
2692 * which changes arguments before restarting syscall.
2693 * SA_RESTART is ignored (assumed not set) similarly
2694 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2695 * since restart data is saved in "restart block"
2696 * in task struct, and if signal handler uses a syscall
2697 * which in turn saves another such restart block,
2698 * old data is lost and restart becomes impossible)
2699 */
2700 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002701 break;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002702 default:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002703 if (u_error < 0)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002704 tprintf("= -1 E??? (errno %ld)", u_error);
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +00002705 else if ((unsigned long) u_error < nerrnos)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002706 tprintf("= -1 %s (%s)", errnoent[u_error],
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002707 strerror(u_error));
2708 else
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002709 tprintf("= -1 ERRNO_%ld (%s)", u_error,
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002710 strerror(u_error));
2711 break;
2712 }
2713 if ((sys_res & RVAL_STR) && tcp->auxstr)
2714 tprintf(" (%s)", tcp->auxstr);
2715 }
2716 else {
2717 if (sys_res & RVAL_NONE)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002718 tprints("= ?");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002719 else {
2720 switch (sys_res & RVAL_MASK) {
2721 case RVAL_HEX:
2722 tprintf("= %#lx", tcp->u_rval);
2723 break;
2724 case RVAL_OCTAL:
2725 tprintf("= %#lo", tcp->u_rval);
2726 break;
2727 case RVAL_UDECIMAL:
2728 tprintf("= %lu", tcp->u_rval);
2729 break;
2730 case RVAL_DECIMAL:
2731 tprintf("= %ld", tcp->u_rval);
2732 break;
Zubin Mithra64aa1b12014-06-04 08:30:41 +05302733 case RVAL_FD:
2734 if (show_fd_path) {
2735 tprints("= ");
2736 printfd(tcp, tcp->u_rval);
2737 }
2738 else
2739 tprintf("= %ld", tcp->u_rval);
2740 break;
H.J. Ludd0130b2012-04-16 12:16:45 +02002741#if defined(LINUX_MIPSN32) || defined(X32)
2742 /*
2743 case RVAL_LHEX:
2744 tprintf("= %#llx", tcp->u_lrval);
2745 break;
2746 case RVAL_LOCTAL:
2747 tprintf("= %#llo", tcp->u_lrval);
2748 break;
2749 */
2750 case RVAL_LUDECIMAL:
2751 tprintf("= %llu", tcp->u_lrval);
2752 break;
2753 /*
2754 case RVAL_LDECIMAL:
2755 tprintf("= %lld", tcp->u_lrval);
2756 break;
2757 */
2758#endif
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002759 default:
2760 fprintf(stderr,
2761 "invalid rval format\n");
2762 break;
2763 }
2764 }
2765 if ((sys_res & RVAL_STR) && tcp->auxstr)
2766 tprintf(" (%s)", tcp->auxstr);
2767 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002768 if (Tflag) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002769 tv_sub(&tv, &tv, &tcp->etime);
2770 tprintf(" <%ld.%06ld>",
2771 (long) tv.tv_sec, (long) tv.tv_usec);
2772 }
Denys Vlasenko000b6012012-01-28 01:25:03 +01002773 tprints("\n");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002774 dumpio(tcp);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002775 line_ended();
2776
Luca Clementi327064b2013-07-23 00:11:35 -07002777#ifdef USE_LIBUNWIND
2778 if (stack_trace_enabled)
Masatake YAMATO61413922014-04-16 15:33:02 +09002779 unwind_print_stacktrace(tcp);
Luca Clementi327064b2013-07-23 00:11:35 -07002780#endif
2781
Denys Vlasenko3b738812011-08-22 02:06:35 +02002782 ret:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002783 tcp->flags &= ~TCB_INSYSCALL;
2784 return 0;
2785}
2786
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002787int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002788trace_syscall(struct tcb *tcp)
2789{
2790 return exiting(tcp) ?
2791 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2792}