blob: fe85b8bfa5104e01b3611f5b44a3c0a4b588a303 [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>
Elliott Hughesb7556142018-02-20 17:03:16 -08009 * Copyright (c) 1999-2018 The strace developers.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000010 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000033 */
34
35#include "defs.h"
Elliott Hughes03a418e2018-06-15 13:11:40 -070036#include "mmap_notify.h"
Dmitry V. Levine2fb0bb2015-09-15 21:51:15 +000037#include "native_defs.h"
Elliott Hughesb7556142018-02-20 17:03:16 -080038#include "ptrace.h"
Elliott Hughesd35df492017-02-15 15:19:05 -080039#include "nsig.h"
Elliott Hughes77c3ff82017-09-08 17:11:00 -070040#include "number_set.h"
Elliott Hughes28e98bc2018-06-14 16:59:04 -070041#include "delay.h"
42#include "retval.h"
Elliott Hughesb7556142018-02-20 17:03:16 -080043#include <limits.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000044
Dmitry V. Levin5503dd22015-02-13 02:12:14 +000045/* for struct iovec */
46#include <sys/uio.h>
Maarten ter Huurne40c174b2014-10-20 01:02:48 +020047
Elliott Hughesd35df492017-02-15 15:19:05 -080048/* for __X32_SYSCALL_BIT */
49#include <asm/unistd.h>
50
Dmitry V. Levin7211dbc2015-02-28 12:20:21 +000051#include "regs.h"
Wichert Akkerman15dea971999-10-06 13:06:34 +000052
Denys Vlasenko84703742012-02-25 02:38:52 +010053#if defined(SPARC64)
Roland McGrath6d1a65c2004-07-12 07:44:08 +000054# undef PTRACE_GETREGS
55# define PTRACE_GETREGS PTRACE_GETREGS64
56# undef PTRACE_SETREGS
57# define PTRACE_SETREGS PTRACE_SETREGS64
Denys Vlasenko84703742012-02-25 02:38:52 +010058#endif
Roland McGrath6d1a65c2004-07-12 07:44:08 +000059
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000060#include "syscall.h"
Elliott Hughesb7556142018-02-20 17:03:16 -080061#include "xstring.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000062
63/* Define these shorthand notations to simplify the syscallent files. */
Elliott Hughes77c3ff82017-09-08 17:11:00 -070064#include "sysent_shorthand_defs.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000065
Szabolcs Nagy34683e32015-12-15 18:32:17 +000066#define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
Elvira Khabirova140ecf82015-07-10 22:24:48 +030067
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +010068const struct_sysent sysent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000069#include "syscallent.h"
70};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000071
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010072#if SUPPORTED_PERSONALITIES > 1
Elvira Khabirova09294222015-08-04 01:47:02 +030073# include PERSONALITY1_INCLUDE_FUNCS
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010074static const struct_sysent sysent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +010075# include "syscallent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000076};
Denys Vlasenko39fca622011-08-20 02:12:33 +020077#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000078
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010079#if SUPPORTED_PERSONALITIES > 2
Elvira Khabirova09294222015-08-04 01:47:02 +030080# include PERSONALITY2_INCLUDE_FUNCS
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010081static const struct_sysent sysent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +010082# include "syscallent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000083};
Denys Vlasenko39fca622011-08-20 02:12:33 +020084#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000085
86/* Now undef them since short defines cause wicked namespace pollution. */
Elliott Hughes77c3ff82017-09-08 17:11:00 -070087#include "sysent_shorthand_undefs.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000088
Elliott Hughes03a418e2018-06-15 13:11:40 -070089const char *const errnoent[] = {
90#include "errnoent.h"
91};
92const char *const signalent[] = {
93#include "signalent.h"
94
95};
Denys Vlasenko39fca622011-08-20 02:12:33 +020096/*
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +000097 * `ioctlent[012].h' files are automatically generated by the auxiliary
Denys Vlasenko39fca622011-08-20 02:12:33 +020098 * program `ioctlsort', such that the list is sorted by the `code' field.
99 * This has the side-effect of resolving the _IO.. macros into
100 * plain integers, eliminating the need to include here everything
101 * in "/usr/include".
102 */
103
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100104const struct_ioctlent ioctlent0[] = {
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +0000105#include "ioctlent0.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200106};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000107
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100108#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100109static const struct_ioctlent ioctlent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100110# include "ioctlent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200111};
Elvira Khabirova09294222015-08-04 01:47:02 +0300112# include PERSONALITY0_INCLUDE_PRINTERS_DECLS
113static const struct_printers printers0 = {
114# include PERSONALITY0_INCLUDE_PRINTERS_DEFS
115};
116# include PERSONALITY1_INCLUDE_PRINTERS_DECLS
117static const struct_printers printers1 = {
118# include PERSONALITY1_INCLUDE_PRINTERS_DEFS
119};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200120#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000121
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100122#if SUPPORTED_PERSONALITIES > 2
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100123static const struct_ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100124# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200125};
Elvira Khabirova09294222015-08-04 01:47:02 +0300126# include PERSONALITY2_INCLUDE_PRINTERS_DECLS
127static const struct_printers printers2 = {
128# include PERSONALITY2_INCLUDE_PRINTERS_DEFS
129};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200130#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000131
Dmitry V. Levine6f55242013-02-26 22:02:30 +0000132enum {
133 nsyscalls0 = ARRAY_SIZE(sysent0)
134#if SUPPORTED_PERSONALITIES > 1
135 , nsyscalls1 = ARRAY_SIZE(sysent1)
136# if SUPPORTED_PERSONALITIES > 2
137 , nsyscalls2 = ARRAY_SIZE(sysent2)
138# endif
139#endif
140};
141
142enum {
Dmitry V. Levine6f55242013-02-26 22:02:30 +0000143 nioctlents0 = ARRAY_SIZE(ioctlent0)
144#if SUPPORTED_PERSONALITIES > 1
145 , nioctlents1 = ARRAY_SIZE(ioctlent1)
146# if SUPPORTED_PERSONALITIES > 2
147 , nioctlents2 = ARRAY_SIZE(ioctlent2)
148# endif
149#endif
150};
151
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100152#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100153const struct_sysent *sysent = sysent0;
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100154const struct_ioctlent *ioctlent = ioctlent0;
Elvira Khabirova09294222015-08-04 01:47:02 +0300155const struct_printers *printers = &printers0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100156#endif
Elvira Khabirova09294222015-08-04 01:47:02 +0300157
Elliott Hughes03a418e2018-06-15 13:11:40 -0700158const unsigned int nerrnos = ARRAY_SIZE(errnoent);
159const unsigned int nsignals = ARRAY_SIZE(signalent);
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100160unsigned nsyscalls = nsyscalls0;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100161unsigned nioctlents = nioctlents0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100162
Elliott Hughesd35df492017-02-15 15:19:05 -0800163const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES] = {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100164 nsyscalls0,
165#if SUPPORTED_PERSONALITIES > 1
166 nsyscalls1,
167#endif
168#if SUPPORTED_PERSONALITIES > 2
169 nsyscalls2,
170#endif
171};
Elliott Hughesd35df492017-02-15 15:19:05 -0800172const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100173 sysent0,
174#if SUPPORTED_PERSONALITIES > 1
175 sysent1,
176#endif
177#if SUPPORTED_PERSONALITIES > 2
178 sysent2,
179#endif
180};
181
Elliott Hughesb7556142018-02-20 17:03:16 -0800182const char *const personality_names[] =
183# if defined X86_64
184 {"64 bit", "32 bit", "x32"}
185# elif defined X32
186 {"x32", "32 bit"}
187# elif SUPPORTED_PERSONALITIES == 2
188 {"64 bit", "32 bit"}
189# else
190 {STRINGIFY_VAL(__WORDSIZE) " bit"}
191# endif
192 ;
193
Elliott Hughes03a418e2018-06-15 13:11:40 -0700194const char *const personality_designators[] =
195# if defined X86_64
196 { "64", "32", "x32" }
197# elif defined X32
198 { "x32", "32" }
199# elif SUPPORTED_PERSONALITIES == 2
200 { "64", "32" }
201# else
202 { STRINGIFY_VAL(__WORDSIZE) }
203# endif
204 ;
205
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100206#if SUPPORTED_PERSONALITIES > 1
Elliott Hughesb7556142018-02-20 17:03:16 -0800207
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100208unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000209
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100210# ifndef current_wordsize
Elliott Hughesb7556142018-02-20 17:03:16 -0800211unsigned current_wordsize = PERSONALITY0_WORDSIZE;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100212static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000213 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000214 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100215# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000216 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100217# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200218};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100219# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000220
Elliott Hughesd35df492017-02-15 15:19:05 -0800221# ifndef current_klongsize
Elliott Hughesb7556142018-02-20 17:03:16 -0800222unsigned current_klongsize = PERSONALITY0_KLONGSIZE;
Elliott Hughesd35df492017-02-15 15:19:05 -0800223static const int personality_klongsize[SUPPORTED_PERSONALITIES] = {
224 PERSONALITY0_KLONGSIZE,
225 PERSONALITY1_KLONGSIZE,
226# if SUPPORTED_PERSONALITIES > 2
227 PERSONALITY2_KLONGSIZE,
228# endif
229};
230# endif
231
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200232void
Elliott Hughesb7556142018-02-20 17:03:16 -0800233set_personality(unsigned int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000234{
Elliott Hughesb7556142018-02-20 17:03:16 -0800235 if (personality == current_personality)
236 return;
237
238 if (personality >= SUPPORTED_PERSONALITIES)
239 error_msg_and_die("Requested switch to unsupported personality "
240 "%u", personality);
241
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100242 nsyscalls = nsyscall_vec[personality];
243 sysent = sysent_vec[personality];
244
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000245 switch (personality) {
246 case 0:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000247 ioctlent = ioctlent0;
248 nioctlents = nioctlents0;
Elvira Khabirova09294222015-08-04 01:47:02 +0300249 printers = &printers0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000250 break;
251
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000252 case 1:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000253 ioctlent = ioctlent1;
254 nioctlents = nioctlents1;
Elvira Khabirova09294222015-08-04 01:47:02 +0300255 printers = &printers1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000256 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000257
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100258# if SUPPORTED_PERSONALITIES > 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000259 case 2:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000260 ioctlent = ioctlent2;
261 nioctlents = nioctlents2;
Elvira Khabirova09294222015-08-04 01:47:02 +0300262 printers = &printers2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000263 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100264# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000265 }
266
267 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100268# ifndef current_wordsize
269 current_wordsize = personality_wordsize[personality];
270# endif
Elliott Hughesd35df492017-02-15 15:19:05 -0800271# ifndef current_klongsize
272 current_klongsize = personality_klongsize[personality];
273# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000274}
275
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000276static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000277update_personality(struct tcb *tcp, unsigned int personality)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000278{
Elliott Hughesb7556142018-02-20 17:03:16 -0800279 static bool need_mpers_warning[] =
280 { false, !HAVE_PERSONALITY_1_MPERS, !HAVE_PERSONALITY_2_MPERS };
281
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000282 set_personality(personality);
283
284 if (personality == tcp->currpers)
285 return;
286 tcp->currpers = personality;
287
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000288 if (!qflag) {
Dmitry V. Levin6c8ef052015-05-25 22:51:19 +0000289 error_msg("[ Process PID=%d runs in %s mode. ]",
Elliott Hughesb7556142018-02-20 17:03:16 -0800290 tcp->pid, personality_names[personality]);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100291 }
Elliott Hughesb7556142018-02-20 17:03:16 -0800292
293 if (need_mpers_warning[personality]) {
294 error_msg("WARNING: Proper structure decoding for this "
295 "personality is not supported, please consider "
296 "building strace with mpers support enabled.");
297 need_mpers_warning[personality] = false;
298 }
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000299}
300#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000301
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000302#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000303static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000304decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000305{
Dmitry V. Levine0a0d892016-07-20 01:49:25 +0000306 const int call = tcp->u_arg[0];
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000307
Dmitry V. Levine0a0d892016-07-20 01:49:25 +0000308 if (call < 1 || call >= SYS_socket_nsubcalls)
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000309 return;
310
Elliott Hughesd35df492017-02-15 15:19:05 -0800311 const kernel_ulong_t scno = SYS_socket_subcall + call;
Dmitry V. Levine0a0d892016-07-20 01:49:25 +0000312 const unsigned int nargs = sysent[scno].nargs;
313 uint64_t buf[nargs];
Dmitry V. Levinc2155692015-03-22 15:52:40 +0000314
Dmitry V. Levine0a0d892016-07-20 01:49:25 +0000315 if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
316 return;
317
318 tcp->scno = scno;
Elliott Hughesd35df492017-02-15 15:19:05 -0800319 tcp->qual_flg = qual_flags(scno);
Dmitry V. Levine0a0d892016-07-20 01:49:25 +0000320 tcp->s_ent = &sysent[scno];
321
322 unsigned int i;
323 for (i = 0; i < nargs; ++i)
324 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
Dmitry V. Levinde5b0092016-07-26 15:59:28 +0000325 ? ((uint32_t *) (void *) buf)[i] : buf[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000326}
Elliott Hughesb7556142018-02-20 17:03:16 -0800327#endif /* SYS_socket_subcall */
Mike Frysinger3362e892012-03-15 01:09:19 -0400328
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000329#ifdef SYS_ipc_subcall
330static void
331decode_ipc_subcall(struct tcb *tcp)
332{
Dmitry V. Levinb412d752016-07-22 01:17:25 +0000333 unsigned int call = tcp->u_arg[0];
334 const unsigned int version = call >> 16;
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000335
Dmitry V. Levinb412d752016-07-22 01:17:25 +0000336 if (version) {
337# if defined S390 || defined S390X
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000338 return;
Dmitry V. Levinb412d752016-07-22 01:17:25 +0000339# else
340# ifdef SPARC64
341 if (current_wordsize == 8)
342 return;
343# endif
344 set_tcb_priv_ulong(tcp, version);
345 call &= 0xffff;
346# endif
347 }
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000348
Dmitry V. Levinb412d752016-07-22 01:17:25 +0000349 switch (call) {
350 case 1: case 2: case 3: case 4:
351 case 11: case 12: case 13: case 14:
352 case 21: case 22: case 23: case 24:
353 break;
354 default:
355 return;
356 }
357
358 tcp->scno = SYS_ipc_subcall + call;
Elliott Hughesd35df492017-02-15 15:19:05 -0800359 tcp->qual_flg = qual_flags(tcp->scno);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100360 tcp->s_ent = &sysent[tcp->scno];
Dmitry V. Levinb412d752016-07-22 01:17:25 +0000361
362 const unsigned int n = tcp->s_ent->nargs;
363 unsigned int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100364 for (i = 0; i < n; i++)
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000365 tcp->u_arg[i] = tcp->u_arg[i + 1];
366}
Elliott Hughesb7556142018-02-20 17:03:16 -0800367#endif /* SYS_ipc_subcall */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000368
Elliott Hughesb7556142018-02-20 17:03:16 -0800369#ifdef SYS_syscall_subcall
Dmitry V. Levinf34b97f2015-04-17 09:14:19 +0000370static void
Elliott Hughesb7556142018-02-20 17:03:16 -0800371decode_syscall_subcall(struct tcb *tcp)
Dmitry V. Levinf34b97f2015-04-17 09:14:19 +0000372{
Elliott Hughesd35df492017-02-15 15:19:05 -0800373 if (!scno_is_valid(tcp->u_arg[0]))
Dmitry V. Levinf34b97f2015-04-17 09:14:19 +0000374 return;
375 tcp->scno = tcp->u_arg[0];
Elliott Hughesd35df492017-02-15 15:19:05 -0800376 tcp->qual_flg = qual_flags(tcp->scno);
Dmitry V. Levinf34b97f2015-04-17 09:14:19 +0000377 tcp->s_ent = &sysent[tcp->scno];
378 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
379 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
Elliott Hughesb7556142018-02-20 17:03:16 -0800380# ifdef LINUX_MIPSO32
Dmitry V. Levinf34b97f2015-04-17 09:14:19 +0000381 /*
382 * Fetching the last arg of 7-arg syscalls (fadvise64_64
Dmitry V. Levin54a4a1a2016-09-28 01:57:59 +0000383 * and sync_file_range) requires additional code,
Dmitry V. Levinf34b97f2015-04-17 09:14:19 +0000384 * see linux/mips/get_syscall_args.c
385 */
Dmitry V. Levin54a4a1a2016-09-28 01:57:59 +0000386 if (tcp->s_ent->nargs == MAX_ARGS) {
387 if (umoven(tcp,
388 mips_REG_SP + MAX_ARGS * sizeof(tcp->u_arg[0]),
389 sizeof(tcp->u_arg[0]),
390 &tcp->u_arg[MAX_ARGS - 1]) < 0)
391 tcp->u_arg[MAX_ARGS - 1] = 0;
392 }
Elliott Hughesb7556142018-02-20 17:03:16 -0800393# endif /* LINUX_MIPSO32 */
Dmitry V. Levinf34b97f2015-04-17 09:14:19 +0000394}
Elliott Hughesb7556142018-02-20 17:03:16 -0800395#endif /* SYS_syscall_subcall */
Dmitry V. Levinf34b97f2015-04-17 09:14:19 +0000396
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000397static void
398dumpio(struct tcb *tcp)
399{
Elliott Hughesd35df492017-02-15 15:19:05 -0800400 int fd = tcp->u_arg[0];
401 if (fd < 0)
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000402 return;
Elliott Hughesd35df492017-02-15 15:19:05 -0800403
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700404 if (is_number_in_set(fd, write_set)) {
405 switch (tcp->s_ent->sen) {
406 case SEN_write:
407 case SEN_pwrite:
408 case SEN_send:
409 case SEN_sendto:
410 case SEN_mq_timedsend:
411 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
412 break;
413 case SEN_writev:
414 case SEN_pwritev:
415 case SEN_pwritev2:
416 case SEN_vmsplice:
417 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], -1);
418 break;
419 case SEN_sendmsg:
420 dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1);
421 break;
422 case SEN_sendmmsg:
423 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
424 break;
425 }
426 }
427
428 if (syserror(tcp))
429 return;
430
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700431 if (is_number_in_set(fd, read_set)) {
Elliott Hughesd35df492017-02-15 15:19:05 -0800432 switch (tcp->s_ent->sen) {
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300433 case SEN_read:
434 case SEN_pread:
435 case SEN_recv:
436 case SEN_recvfrom:
Elliott Hughesd35df492017-02-15 15:19:05 -0800437 case SEN_mq_timedreceive:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000438 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
439 return;
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300440 case SEN_readv:
Dmitry V. Levin3f6ebce2016-03-31 00:01:58 +0000441 case SEN_preadv:
Dmitry V. Levina6dd0942016-05-11 00:42:10 +0000442 case SEN_preadv2:
Dmitry V. Levin05a0af62016-01-20 00:17:02 +0000443 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
444 tcp->u_rval);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000445 return;
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300446 case SEN_recvmsg:
Dmitry V. Levin93c9d1c2016-01-20 03:26:37 +0000447 dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000448 return;
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300449 case SEN_recvmmsg:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000450 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
451 return;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000452 }
453 }
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000454}
455
Eugene Syromyatnikov6fdb1042016-09-29 15:56:27 +0300456const char *
457err_name(unsigned long err)
458{
459 if ((err < nerrnos) && errnoent[err])
460 return errnoent[err];
461
462 return NULL;
463}
464
Elliott Hughes03a418e2018-06-15 13:11:40 -0700465static void
466print_err_ret(kernel_ulong_t ret, unsigned long u_error)
467{
468 const char *u_error_str = err_name(u_error);
469
470 if (u_error_str)
471 tprintf("= %" PRI_kld " %s (%s)",
472 ret, u_error_str, strerror(u_error));
473 else
474 tprintf("= %" PRI_kld " (errno %lu)", ret, u_error);
475}
476
Elliott Hughesb7556142018-02-20 17:03:16 -0800477static long get_regs(struct tcb *);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000478static int get_syscall_args(struct tcb *);
479static int get_syscall_result(struct tcb *);
Dmitry V. Levin7386ca72015-11-30 13:57:51 +0000480static int arch_get_scno(struct tcb *tcp);
Elliott Hughesd35df492017-02-15 15:19:05 -0800481static int arch_set_scno(struct tcb *, kernel_ulong_t);
Dmitry V. Levin7386ca72015-11-30 13:57:51 +0000482static void get_error(struct tcb *, const bool);
Elliott Hughesd35df492017-02-15 15:19:05 -0800483static int arch_set_error(struct tcb *);
484static int arch_set_success(struct tcb *);
485
486struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
487
488static struct inject_opts *
489tcb_inject_opts(struct tcb *tcp)
490{
491 return (scno_in_range(tcp->scno) && tcp->inject_vec[current_personality])
492 ? &tcp->inject_vec[current_personality][tcp->scno] : NULL;
493}
494
495
496static long
497tamper_with_syscall_entering(struct tcb *tcp, unsigned int *signo)
498{
499 if (!tcp->inject_vec[current_personality]) {
500 tcp->inject_vec[current_personality] =
501 xcalloc(nsyscalls, sizeof(**inject_vec));
502 memcpy(tcp->inject_vec[current_personality],
503 inject_vec[current_personality],
504 nsyscalls * sizeof(**inject_vec));
505 }
506
507 struct inject_opts *opts = tcb_inject_opts(tcp);
508
509 if (!opts || opts->first == 0)
510 return 0;
511
512 --opts->first;
513
514 if (opts->first != 0)
515 return 0;
516
517 opts->first = opts->step;
518
Elliott Hughesb7556142018-02-20 17:03:16 -0800519 if (!recovering(tcp)) {
520 if (opts->data.flags & INJECT_F_SIGNAL)
521 *signo = opts->data.signo;
Elliott Hughes03a418e2018-06-15 13:11:40 -0700522 if (opts->data.flags & (INJECT_F_ERROR | INJECT_F_RETVAL)) {
523 kernel_long_t scno =
524 (opts->data.flags & INJECT_F_SYSCALL)
525 ? (kernel_long_t) shuffle_scno(opts->data.scno)
526 : -1;
527
528 if (!arch_set_scno(tcp, scno)) {
529 tcp->flags |= TCB_TAMPERED;
530 if (scno != -1)
531 tcp->flags |= TCB_TAMPERED_NO_FAIL;
532 }
533 }
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700534 if (opts->data.flags & INJECT_F_DELAY_ENTER)
535 delay_tcb(tcp, opts->data.delay_idx, true);
536 if (opts->data.flags & INJECT_F_DELAY_EXIT)
537 tcp->flags |= TCB_INJECT_DELAY_EXIT;
Elliott Hughesb7556142018-02-20 17:03:16 -0800538 }
Elliott Hughesd35df492017-02-15 15:19:05 -0800539
540 return 0;
541}
542
543static long
544tamper_with_syscall_exiting(struct tcb *tcp)
545{
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700546 struct inject_opts *opts = tcb_inject_opts(tcp);
547 if (!opts)
548 return 0;
549
550 if (inject_delay_exit(tcp))
551 delay_tcb(tcp, opts->data.delay_idx, false);
552
553 if (!syscall_tampered(tcp))
554 return 0;
555
Elliott Hughes03a418e2018-06-15 13:11:40 -0700556 if (!syserror(tcp) ^ !!syscall_tampered_nofail(tcp)) {
557 error_msg("Failed to tamper with process %d: unexpectedly got"
558 " %serror (return value %#" PRI_klx ", error %lu)",
559 tcp->pid, syscall_tampered_nofail(tcp) ? "" : "no ",
560 tcp->u_rval, tcp->u_error);
Elliott Hughesb7556142018-02-20 17:03:16 -0800561
562 return 1;
563 }
564
Elliott Hughesb7556142018-02-20 17:03:16 -0800565 bool update_tcb = false;
Elliott Hughesd35df492017-02-15 15:19:05 -0800566
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700567 if (opts->data.flags & INJECT_F_RETVAL) {
568 kernel_long_t inject_rval =
569 retval_get(opts->data.rval_idx);
Elliott Hughesd35df492017-02-15 15:19:05 -0800570 kernel_long_t u_rval = tcp->u_rval;
571
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700572 tcp->u_rval = inject_rval;
Elliott Hughesd35df492017-02-15 15:19:05 -0800573 if (arch_set_success(tcp)) {
574 tcp->u_rval = u_rval;
575 } else {
Elliott Hughesb7556142018-02-20 17:03:16 -0800576 update_tcb = true;
Elliott Hughesd35df492017-02-15 15:19:05 -0800577 tcp->u_error = 0;
578 }
579 } else {
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700580 unsigned long new_error = retval_get(opts->data.rval_idx);
Elliott Hughesd35df492017-02-15 15:19:05 -0800581
582 if (new_error != tcp->u_error && new_error <= MAX_ERRNO_VALUE) {
583 unsigned long u_error = tcp->u_error;
584
585 tcp->u_error = new_error;
586 if (arch_set_error(tcp)) {
587 tcp->u_error = u_error;
Elliott Hughesb7556142018-02-20 17:03:16 -0800588 } else {
589 update_tcb = true;
Elliott Hughesd35df492017-02-15 15:19:05 -0800590 }
591 }
592 }
593
Elliott Hughesb7556142018-02-20 17:03:16 -0800594 if (update_tcb) {
595 tcp->u_error = 0;
596 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
597 }
598
Elliott Hughesd35df492017-02-15 15:19:05 -0800599 return 0;
600}
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000601
Elliott Hughesdc75b012017-07-05 13:54:44 -0700602/*
603 * Returns:
604 * 0: "ignore this ptrace stop", bail out silently.
605 * 1: ok, decoded; call
606 * syscall_entering_finish(tcp, syscall_entering_trace(tcp, ...)).
607 * other: error; call syscall_entering_finish(tcp, res), where res is the value
608 * returned.
609 */
610int
611syscall_entering_decode(struct tcb *tcp)
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000612{
Elliott Hughes39bac052017-05-25 16:56:11 -0700613 int res = get_scno(tcp);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000614 if (res == 0)
615 return res;
Elliott Hughes39bac052017-05-25 16:56:11 -0700616 int scno_good = res;
Elliott Hughesdc75b012017-07-05 13:54:44 -0700617 if (res != 1 || (res = get_syscall_args(tcp)) != 1) {
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000618 printleader(tcp);
Dmitry V. Levin60d7ec82016-08-09 00:07:53 +0000619 tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000620 /*
621 * " <unavailable>" will be added later by the code which
622 * detects ptrace errors.
623 */
Elliott Hughesdc75b012017-07-05 13:54:44 -0700624 return res;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000625 }
626
Elliott Hughesb7556142018-02-20 17:03:16 -0800627#if defined SYS_ipc_subcall \
628 || defined SYS_socket_subcall \
629 || defined SYS_syscall_subcall
630 for (;;) {
631 switch (tcp->s_ent->sen) {
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000632# ifdef SYS_ipc_subcall
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300633 case SEN_ipc:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000634 decode_ipc_subcall(tcp);
635 break;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000636# endif
Elliott Hughesb7556142018-02-20 17:03:16 -0800637# ifdef SYS_socket_subcall
638 case SEN_socketcall:
639 decode_socket_subcall(tcp);
640 break;
641# endif
642# ifdef SYS_syscall_subcall
643 case SEN_syscall:
644 decode_syscall_subcall(tcp);
645 if (tcp->s_ent->sen != SEN_syscall)
646 continue;
647 break;
648# endif
649 }
650 break;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000651 }
652#endif
653
Elliott Hughesdc75b012017-07-05 13:54:44 -0700654 return 1;
655}
656
657int
658syscall_entering_trace(struct tcb *tcp, unsigned int *sig)
659{
Elliott Hughesd35df492017-02-15 15:19:05 -0800660 /* Restrain from fault injection while the trace executes strace code. */
661 if (hide_log(tcp)) {
662 tcp->qual_flg &= ~QUAL_INJECT;
663 }
664
665 switch (tcp->s_ent->sen) {
666 case SEN_execve:
667 case SEN_execveat:
668#if defined SPARC || defined SPARC64
669 case SEN_execv:
670#endif
671 tcp->flags &= ~TCB_HIDE_LOG;
672 break;
673 }
674
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700675 if (!traced(tcp) || (tracing_paths && !pathtrace_match(tcp))) {
Elliott Hughesdc75b012017-07-05 13:54:44 -0700676 tcp->flags |= TCB_FILTERED;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000677 return 0;
678 }
679
680 tcp->flags &= ~TCB_FILTERED;
681
Elliott Hughesd35df492017-02-15 15:19:05 -0800682 if (hide_log(tcp)) {
Elliott Hughesdc75b012017-07-05 13:54:44 -0700683 return 0;
Elliott Hughesd35df492017-02-15 15:19:05 -0800684 }
685
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700686 if (inject(tcp))
Elliott Hughesd35df492017-02-15 15:19:05 -0800687 tamper_with_syscall_entering(tcp, sig);
688
689 if (cflag == CFLAG_ONLY_STATS) {
Elliott Hughesdc75b012017-07-05 13:54:44 -0700690 return 0;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000691 }
692
Elliott Hughes03a418e2018-06-15 13:11:40 -0700693#ifdef ENABLE_STACKTRACE
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000694 if (stack_trace_enabled) {
695 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700696 unwind_tcb_capture(tcp);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000697 }
698#endif
699
700 printleader(tcp);
Dmitry V. Levin60d7ec82016-08-09 00:07:53 +0000701 tprintf("%s(", tcp->s_ent->sys_name);
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700702 int res = raw(tcp) ? printargs(tcp) : tcp->s_ent->sys_func(tcp);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000703 fflush(tcp->outf);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700704 return res;
705}
706
707void
708syscall_entering_finish(struct tcb *tcp, int res)
709{
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000710 tcp->flags |= TCB_INSYSCALL;
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000711 tcp->sys_func_rval = res;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000712 /* Measure the entrance time as late as possible to avoid errors. */
Elliott Hughesdc75b012017-07-05 13:54:44 -0700713 if ((Tflag || cflag) && !filtered(tcp))
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700714 clock_gettime(CLOCK_MONOTONIC, &tcp->etime);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000715}
716
Elliott Hughesdc75b012017-07-05 13:54:44 -0700717/* Returns:
718 * 0: "bail out".
719 * 1: ok.
720 * -1: error in one of ptrace ops.
721 *
722 * If not 0, call syscall_exiting_trace(tcp, res), where res is the return
723 * value. Anyway, call syscall_exiting_finish(tcp) then.
724 */
725int
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700726syscall_exiting_decode(struct tcb *tcp, struct timespec *pts)
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000727{
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000728 /* Measure the exit time as early as possible to avoid errors. */
Elliott Hughes39bac052017-05-25 16:56:11 -0700729 if ((Tflag || cflag) && !(filtered(tcp) || hide_log(tcp)))
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700730 clock_gettime(CLOCK_MONOTONIC, pts);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000731
Elliott Hughes03a418e2018-06-15 13:11:40 -0700732 if (tcp->s_ent->sys_flags & MEMORY_MAPPING_CHANGE)
733 mmap_notify_report(tcp);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000734
Elliott Hughes39bac052017-05-25 16:56:11 -0700735 if (filtered(tcp) || hide_log(tcp))
Elliott Hughesdc75b012017-07-05 13:54:44 -0700736 return 0;
Elliott Hughes39bac052017-05-25 16:56:11 -0700737
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000738#if SUPPORTED_PERSONALITIES > 1
739 update_personality(tcp, tcp->currpers);
740#endif
Elliott Hughesb7556142018-02-20 17:03:16 -0800741
742 return get_syscall_result(tcp);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700743}
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000744
Elliott Hughesdc75b012017-07-05 13:54:44 -0700745int
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700746syscall_exiting_trace(struct tcb *tcp, struct timespec *ts, int res)
Elliott Hughesdc75b012017-07-05 13:54:44 -0700747{
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700748 if (syscall_tampered(tcp) || inject_delay_exit(tcp))
Elliott Hughesd35df492017-02-15 15:19:05 -0800749 tamper_with_syscall_exiting(tcp);
750
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000751 if (cflag) {
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700752 count_syscall(tcp, ts);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000753 if (cflag == CFLAG_ONLY_STATS) {
Elliott Hughesdc75b012017-07-05 13:54:44 -0700754 return 0;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000755 }
756 }
757
758 /* If not in -ff mode, and printing_tcp != tcp,
759 * then the log currently does not end with output
760 * of _our syscall entry_, but with something else.
761 * We need to say which syscall's return is this.
762 *
763 * Forced reprinting via TCB_REPRINT is used only by
764 * "strace -ff -oLOG test/threaded_execve" corner case.
765 * It's the only case when -ff mode needs reprinting.
766 */
767 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
768 tcp->flags &= ~TCB_REPRINT;
769 printleader(tcp);
Dmitry V. Levin60d7ec82016-08-09 00:07:53 +0000770 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000771 }
772 printing_tcp = tcp;
773
774 tcp->s_prev_ent = NULL;
775 if (res != 1) {
776 /* There was error in one of prior ptrace ops */
777 tprints(") ");
778 tabto();
779 tprints("= ? <unavailable>\n");
780 line_ended();
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000781 return res;
782 }
783 tcp->s_prev_ent = tcp->s_ent;
784
Elliott Hughes39bac052017-05-25 16:56:11 -0700785 int sys_res = 0;
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700786 if (raw(tcp)) {
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000787 /* sys_res = printargs(tcp); - but it's nop on sysexit */
788 } else {
789 /* FIXME: not_failing_only (IOW, option -z) is broken:
790 * failure of syscall is known only after syscall return.
791 * Thus we end up with something like this on, say, ENOENT:
Elliott Hughesdc75b012017-07-05 13:54:44 -0700792 * open("does_not_exist", O_RDONLY <unfinished ...>
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000793 * {next syscall decode}
794 * whereas the intended result is that open(...) line
795 * is not shown at all.
796 */
797 if (not_failing_only && tcp->u_error)
Elliott Hughesdc75b012017-07-05 13:54:44 -0700798 return 0; /* ignore failed syscalls */
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000799 if (tcp->sys_func_rval & RVAL_DECODED)
800 sys_res = tcp->sys_func_rval;
801 else
802 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000803 }
804
805 tprints(") ");
806 tabto();
Elliott Hughesd35df492017-02-15 15:19:05 -0800807
Elliott Hughes77c3ff82017-09-08 17:11:00 -0700808 if (raw(tcp)) {
Elliott Hughes03a418e2018-06-15 13:11:40 -0700809 if (tcp->u_error)
810 print_err_ret(tcp->u_rval, tcp->u_error);
811 else
Elliott Hughesd35df492017-02-15 15:19:05 -0800812 tprintf("= %#" PRI_klx, tcp->u_rval);
Elliott Hughes03a418e2018-06-15 13:11:40 -0700813
Elliott Hughesd35df492017-02-15 15:19:05 -0800814 if (syscall_tampered(tcp))
815 tprints(" (INJECTED)");
Elliott Hughes03a418e2018-06-15 13:11:40 -0700816 } else if (!(sys_res & RVAL_NONE) && tcp->u_error) {
817 switch (tcp->u_error) {
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000818 /* Blocked signals do not interrupt any syscalls.
819 * In this case syscalls don't return ERESTARTfoo codes.
820 *
821 * Deadly signals set to SIG_DFL interrupt syscalls
822 * and kill the process regardless of which of the codes below
823 * is returned by the interrupted syscall.
824 * In some cases, kernel forces a kernel-generated deadly
825 * signal to be unblocked and set to SIG_DFL (and thus cause
826 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
827 * or SIGILL. (The alternative is to leave process spinning
828 * forever on the faulty instruction - not useful).
829 *
830 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
831 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
832 * but kernel will always restart them.
833 */
834 case ERESTARTSYS:
835 /* Most common type of signal-interrupted syscall exit code.
836 * The system call will be restarted with the same arguments
837 * if SA_RESTART is set; otherwise, it will fail with EINTR.
838 */
839 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
840 break;
841 case ERESTARTNOINTR:
842 /* Rare. For example, fork() returns this if interrupted.
843 * SA_RESTART is ignored (assumed set): the restart is unconditional.
844 */
845 tprints("= ? ERESTARTNOINTR (To be restarted)");
846 break;
847 case ERESTARTNOHAND:
848 /* pause(), rt_sigsuspend() etc use this code.
849 * SA_RESTART is ignored (assumed not set):
850 * syscall won't restart (will return EINTR instead)
851 * even after signal with SA_RESTART set. However,
852 * after SIG_IGN or SIG_DFL signal it will restart
853 * (thus the name "restart only if has no handler").
854 */
855 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
856 break;
857 case ERESTART_RESTARTBLOCK:
858 /* Syscalls like nanosleep(), poll() which can't be
859 * restarted with their original arguments use this
860 * code. Kernel will execute restart_syscall() instead,
861 * which changes arguments before restarting syscall.
862 * SA_RESTART is ignored (assumed not set) similarly
863 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
864 * since restart data is saved in "restart block"
865 * in task struct, and if signal handler uses a syscall
866 * which in turn saves another such restart block,
867 * old data is lost and restart becomes impossible)
868 */
869 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
870 break;
871 default:
Elliott Hughes03a418e2018-06-15 13:11:40 -0700872 print_err_ret(tcp->u_rval, tcp->u_error);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000873 break;
874 }
Elliott Hughesd35df492017-02-15 15:19:05 -0800875 if (syscall_tampered(tcp))
Elliott Hughes39bac052017-05-25 16:56:11 -0700876 tprints(" (INJECTED)");
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000877 if ((sys_res & RVAL_STR) && tcp->auxstr)
878 tprintf(" (%s)", tcp->auxstr);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700879 } else {
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000880 if (sys_res & RVAL_NONE)
881 tprints("= ?");
882 else {
883 switch (sys_res & RVAL_MASK) {
884 case RVAL_HEX:
Elliott Hughesd35df492017-02-15 15:19:05 -0800885#if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
Elliott Hughesb7556142018-02-20 17:03:16 -0800886 if (current_klongsize < sizeof(tcp->u_rval)) {
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000887 tprintf("= %#x",
888 (unsigned int) tcp->u_rval);
Elliott Hughesd35df492017-02-15 15:19:05 -0800889 } else
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000890#endif
Elliott Hughesd35df492017-02-15 15:19:05 -0800891 {
892 tprintf("= %#" PRI_klx, tcp->u_rval);
893 }
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000894 break;
895 case RVAL_OCTAL:
Dmitry V. Levin8d374382016-08-03 14:05:39 +0000896 tprints("= ");
897 print_numeric_long_umask(tcp->u_rval);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000898 break;
899 case RVAL_UDECIMAL:
Elliott Hughesd35df492017-02-15 15:19:05 -0800900#if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
Elliott Hughesb7556142018-02-20 17:03:16 -0800901 if (current_klongsize < sizeof(tcp->u_rval)) {
Dmitry V. Levinbe1cb922016-01-07 14:20:17 +0000902 tprintf("= %u",
903 (unsigned int) tcp->u_rval);
Elliott Hughesd35df492017-02-15 15:19:05 -0800904 } else
Dmitry V. Levinbe1cb922016-01-07 14:20:17 +0000905#endif
Elliott Hughesd35df492017-02-15 15:19:05 -0800906 {
907 tprintf("= %" PRI_klu, tcp->u_rval);
908 }
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000909 break;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000910 case RVAL_FD:
911 if (show_fd_path) {
912 tprints("= ");
913 printfd(tcp, tcp->u_rval);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700914 } else
Elliott Hughesd35df492017-02-15 15:19:05 -0800915 tprintf("= %" PRI_kld, tcp->u_rval);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000916 break;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000917 default:
Dmitry V. Levin6c8ef052015-05-25 22:51:19 +0000918 error_msg("invalid rval format");
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000919 break;
920 }
921 }
922 if ((sys_res & RVAL_STR) && tcp->auxstr)
923 tprintf(" (%s)", tcp->auxstr);
Elliott Hughesd35df492017-02-15 15:19:05 -0800924 if (syscall_tampered(tcp))
925 tprints(" (INJECTED)");
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000926 }
927 if (Tflag) {
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700928 ts_sub(ts, ts, &tcp->etime);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000929 tprintf(" <%ld.%06ld>",
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700930 (long) ts->tv_sec, (long) ts->tv_nsec / 1000);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000931 }
932 tprints("\n");
933 dumpio(tcp);
934 line_ended();
935
Elliott Hughes03a418e2018-06-15 13:11:40 -0700936#ifdef ENABLE_STACKTRACE
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000937 if (stack_trace_enabled)
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700938 unwind_tcb_print(tcp);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000939#endif
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000940 return 0;
941}
942
Elliott Hughesdc75b012017-07-05 13:54:44 -0700943void
944syscall_exiting_finish(struct tcb *tcp)
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000945{
Elliott Hughes28e98bc2018-06-14 16:59:04 -0700946 tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED | TCB_INJECT_DELAY_EXIT);
Elliott Hughesdc75b012017-07-05 13:54:44 -0700947 tcp->sys_func_rval = 0;
948 free_tcb_priv_data(tcp);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000949}
950
Dmitry V. Levin9af94a22015-09-18 01:54:59 +0000951bool
952is_erestart(struct tcb *tcp)
953{
954 switch (tcp->u_error) {
955 case ERESTARTSYS:
956 case ERESTARTNOINTR:
957 case ERESTARTNOHAND:
958 case ERESTART_RESTARTBLOCK:
959 return true;
960 default:
961 return false;
962 }
963}
964
Dmitry V. Levinba63d8a2016-10-03 11:48:55 +0000965static unsigned long saved_u_error;
Dmitry V. Levin3858b932015-09-18 01:54:59 +0000966
967void
968temporarily_clear_syserror(struct tcb *tcp)
969{
970 saved_u_error = tcp->u_error;
971 tcp->u_error = 0;
972}
973
974void
975restore_cleared_syserror(struct tcb *tcp)
976{
977 tcp->u_error = saved_u_error;
978}
979
Elliott Hughes03a418e2018-06-15 13:11:40 -0700980#define XLAT_MACROS_ONLY
981# include "xlat/nt_descriptor_types.h"
982#undef XLAT_MACROS_ONLY
983
Dmitry V. Levind70d1c42015-03-22 22:13:55 +0000984#include "arch_regs.c"
Wichert Akkermanc7926982000-04-10 22:22:31 +0000985
Elliott Hughesb7556142018-02-20 17:03:16 -0800986#if HAVE_ARCH_GETRVAL2
Dmitry V. Levind70d1c42015-03-22 22:13:55 +0000987# include "arch_getrval2.c"
Dmitry V. Levin48f08902015-03-05 23:30:02 +0000988#endif
989
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100990void
Denys Vlasenko5a2483b2013-07-01 12:49:14 +0200991print_pc(struct tcb *tcp)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100992{
Dmitry V. Levin5105d4a2015-11-30 03:30:51 +0000993#if defined ARCH_PC_REG
994# define ARCH_GET_PC 0
995#elif defined ARCH_PC_PEEK_ADDR
Elliott Hughesd35df492017-02-15 15:19:05 -0800996 kernel_ulong_t pc;
Dmitry V. Levin5105d4a2015-11-30 03:30:51 +0000997# define ARCH_PC_REG pc
Elliott Hughesb7556142018-02-20 17:03:16 -0800998# define ARCH_GET_PC upeek(tcp, ARCH_PC_PEEK_ADDR, &pc)
Dmitry V. Levinb2d9ff22015-02-23 13:43:20 +0000999#else
Dmitry V. Levin5105d4a2015-11-30 03:30:51 +00001000# error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001001#endif
Elliott Hughesb7556142018-02-20 17:03:16 -08001002 if (get_regs(tcp) < 0 || ARCH_GET_PC)
Dmitry V. Levin5105d4a2015-11-30 03:30:51 +00001003 tprints(current_wordsize == 4 ? "[????????] "
1004 : "[????????????????] ");
1005 else
Elliott Hughesd35df492017-02-15 15:19:05 -08001006 tprintf(current_wordsize == 4
1007 ? "[%08" PRI_klx "] " : "[%016" PRI_klx "] ",
1008 (kernel_ulong_t) ARCH_PC_REG);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001009}
1010
Elliott Hughesd35df492017-02-15 15:19:05 -08001011#include "getregs_old.h"
1012
1013#undef ptrace_getregset_or_getregs
1014#undef ptrace_setregset_or_setregs
1015#ifdef ARCH_REGS_FOR_GETREGSET
1016
1017# define ptrace_getregset_or_getregs ptrace_getregset
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001018static long
Elliott Hughesd35df492017-02-15 15:19:05 -08001019ptrace_getregset(pid_t pid)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001020{
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001021# ifdef ARCH_IOVEC_FOR_GETREGSET
1022 /* variable iovec */
1023 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1024 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1025 &ARCH_IOVEC_FOR_GETREGSET);
1026# else
1027 /* constant iovec */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001028 static struct iovec io = {
1029 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1030 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001031 };
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001032 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001033
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001034# endif
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001035}
Elliott Hughesd35df492017-02-15 15:19:05 -08001036
1037# ifndef HAVE_GETREGS_OLD
1038# define ptrace_setregset_or_setregs ptrace_setregset
1039static int
1040ptrace_setregset(pid_t pid)
1041{
1042# ifdef ARCH_IOVEC_FOR_GETREGSET
1043 /* variable iovec */
1044 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1045 &ARCH_IOVEC_FOR_GETREGSET);
1046# else
1047 /* constant iovec */
1048 static struct iovec io = {
1049 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1050 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1051 };
1052 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1053# endif
1054}
1055# endif /* !HAVE_GETREGS_OLD */
1056
1057#elif defined ARCH_REGS_FOR_GETREGS
1058
1059# define ptrace_getregset_or_getregs ptrace_getregs
1060static long
1061ptrace_getregs(pid_t pid)
1062{
1063# if defined SPARC || defined SPARC64
1064 /* SPARC systems have the meaning of data and addr reversed */
1065 return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1066# else
1067 return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1068# endif
1069}
1070
1071# ifndef HAVE_GETREGS_OLD
1072# define ptrace_setregset_or_setregs ptrace_setregs
1073static int
1074ptrace_setregs(pid_t pid)
1075{
1076# if defined SPARC || defined SPARC64
1077 /* SPARC systems have the meaning of data and addr reversed */
1078 return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1079# else
1080 return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1081# endif
1082}
1083# endif /* !HAVE_GETREGS_OLD */
1084
1085#endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001086
Elliott Hughesb7556142018-02-20 17:03:16 -08001087#ifdef ptrace_getregset_or_getregs
1088static long get_regs_error;
1089#endif
1090
1091void
1092clear_regs(struct tcb *tcp)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001093{
Elliott Hughesb7556142018-02-20 17:03:16 -08001094#ifdef ptrace_getregset_or_getregs
1095 get_regs_error = -1;
1096#endif
1097}
1098
1099static long
1100get_regs(struct tcb *const tcp)
1101{
Elliott Hughesd35df492017-02-15 15:19:05 -08001102#ifdef ptrace_getregset_or_getregs
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001103
Elliott Hughes39bac052017-05-25 16:56:11 -07001104 if (get_regs_error != -1)
Elliott Hughesb7556142018-02-20 17:03:16 -08001105 return get_regs_error;
Elliott Hughes39bac052017-05-25 16:56:11 -07001106
Elliott Hughesd35df492017-02-15 15:19:05 -08001107# ifdef HAVE_GETREGS_OLD
1108 /*
1109 * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1110 * fallback to getregs_old.
1111 */
1112 static int use_getregs_old;
1113 if (use_getregs_old < 0) {
Elliott Hughesb7556142018-02-20 17:03:16 -08001114 return get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
Elliott Hughesd35df492017-02-15 15:19:05 -08001115 } else if (use_getregs_old == 0) {
Elliott Hughesb7556142018-02-20 17:03:16 -08001116 get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001117 if (get_regs_error >= 0) {
Elliott Hughesd35df492017-02-15 15:19:05 -08001118 use_getregs_old = -1;
Elliott Hughesb7556142018-02-20 17:03:16 -08001119 return get_regs_error;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001120 }
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001121 if (errno == EPERM || errno == ESRCH)
Elliott Hughesb7556142018-02-20 17:03:16 -08001122 return get_regs_error;
Elliott Hughesd35df492017-02-15 15:19:05 -08001123 use_getregs_old = 1;
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001124 }
Elliott Hughesb7556142018-02-20 17:03:16 -08001125 return get_regs_error = getregs_old(tcp);
Elliott Hughesd35df492017-02-15 15:19:05 -08001126# else /* !HAVE_GETREGS_OLD */
1127 /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
Elliott Hughesb7556142018-02-20 17:03:16 -08001128 get_regs_error = ptrace_getregset_or_getregs(tcp->pid);
1129
1130# if defined ARCH_PERSONALITY_0_IOV_SIZE
1131 if (get_regs_error)
1132 return get_regs_error;
1133
1134 switch (ARCH_IOVEC_FOR_GETREGSET.iov_len) {
1135 case ARCH_PERSONALITY_0_IOV_SIZE:
1136 update_personality(tcp, 0);
1137 break;
1138 case ARCH_PERSONALITY_1_IOV_SIZE:
1139 update_personality(tcp, 1);
1140 break;
1141 default: {
1142 static bool printed = false;
1143
1144 if (!printed) {
1145 error_msg("Unsupported regset size returned by "
1146 "PTRACE_GETREGSET: %zu",
1147 ARCH_IOVEC_FOR_GETREGSET.iov_len);
1148
1149 printed = true;
1150 }
1151
1152 update_personality(tcp, 0);
1153 }
1154 }
1155# endif /* ARCH_PERSONALITY_0_IOV_SIZE */
1156
1157 return get_regs_error;
1158
Elliott Hughesd35df492017-02-15 15:19:05 -08001159# endif /* !HAVE_GETREGS_OLD */
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001160
Elliott Hughesd35df492017-02-15 15:19:05 -08001161#else /* !ptrace_getregset_or_getregs */
1162
Dmitry V. Levinf51aec62015-11-30 00:01:01 +00001163# warning get_regs is not implemented for this architecture yet
Elliott Hughesb7556142018-02-20 17:03:16 -08001164 return 0;
Elliott Hughesd35df492017-02-15 15:19:05 -08001165
1166#endif /* !ptrace_getregset_or_getregs */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001167}
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001168
Elliott Hughesd35df492017-02-15 15:19:05 -08001169#ifdef ptrace_setregset_or_setregs
1170static int
1171set_regs(pid_t pid)
1172{
1173 return ptrace_setregset_or_setregs(pid);
1174}
1175#endif /* ptrace_setregset_or_setregs */
1176
Dmitry V. Levine3dfc592016-08-09 10:28:22 +00001177struct sysent_buf {
1178 struct tcb *tcp;
1179 struct_sysent ent;
Elliott Hughesb7556142018-02-20 17:03:16 -08001180 char buf[sizeof("syscall_0x") + sizeof(kernel_ulong_t) * 2];
Dmitry V. Levine3dfc592016-08-09 10:28:22 +00001181};
1182
1183static void
1184free_sysent_buf(void *ptr)
1185{
1186 struct sysent_buf *s = ptr;
1187 s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1188 free(ptr);
1189}
1190
Dmitry V. Levin16510512015-11-30 01:46:52 +00001191/*
1192 * Returns:
Elliott Hughesdc75b012017-07-05 13:54:44 -07001193 * 0: "ignore this ptrace stop", syscall_entering_decode() should return a "bail
1194 * out silently" code.
1195 * 1: ok, continue in syscall_entering_decode().
1196 * other: error, syscall_entering_decode() should print error indicator
1197 * ("????" etc) and return an appropriate code.
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001198 */
Denys Vlasenko8497b622015-03-21 17:51:52 +01001199int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001200get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001201{
Elliott Hughesb7556142018-02-20 17:03:16 -08001202 if (get_regs(tcp) < 0)
Dmitry V. Levin144cda22015-03-23 18:48:32 +00001203 return -1;
1204
Dmitry V. Levin16510512015-11-30 01:46:52 +00001205 int rc = arch_get_scno(tcp);
1206 if (rc != 1)
1207 return rc;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001208
Elliott Hughesb7556142018-02-20 17:03:16 -08001209 tcp->scno = shuffle_scno(tcp->scno);
1210
Elliott Hughesd35df492017-02-15 15:19:05 -08001211 if (scno_is_valid(tcp->scno)) {
Dmitry V. Levin16510512015-11-30 01:46:52 +00001212 tcp->s_ent = &sysent[tcp->scno];
Elliott Hughesd35df492017-02-15 15:19:05 -08001213 tcp->qual_flg = qual_flags(tcp->scno);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001214 } else {
Dmitry V. Levine3dfc592016-08-09 10:28:22 +00001215 struct sysent_buf *s = xcalloc(1, sizeof(*s));
Dmitry V. Levin60d7ec82016-08-09 00:07:53 +00001216
Dmitry V. Levine3dfc592016-08-09 10:28:22 +00001217 s->tcp = tcp;
Dmitry V. Levin60d7ec82016-08-09 00:07:53 +00001218 s->ent.nargs = MAX_ARGS;
1219 s->ent.sen = SEN_printargs;
1220 s->ent.sys_func = printargs;
1221 s->ent.sys_name = s->buf;
Elliott Hughesb7556142018-02-20 17:03:16 -08001222 xsprintf(s->buf, "syscall_%#" PRI_klx, shuffle_scno(tcp->scno));
Dmitry V. Levin60d7ec82016-08-09 00:07:53 +00001223
1224 tcp->s_ent = &s->ent;
Elvira Khabirovaed37c8d2016-06-12 19:11:43 +03001225 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
Dmitry V. Levine3dfc592016-08-09 10:28:22 +00001226
1227 set_tcb_priv_data(tcp, s, free_sysent_buf);
1228
Elliott Hughesb7556142018-02-20 17:03:16 -08001229 debug_msg("pid %d invalid syscall %#" PRI_klx,
1230 tcp->pid, shuffle_scno(tcp->scno));
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001231 }
Elliott Hughesb7556142018-02-20 17:03:16 -08001232
1233 /*
1234 * We refrain from argument decoding during recovering
1235 * as tracee memory mappings has changed and the registers
1236 * are very likely pointing to garbage already.
1237 */
1238 if (recovering(tcp))
1239 tcp->qual_flg |= QUAL_RAW;
1240
Pavel Machek4dc3b142000-02-01 17:58:41 +00001241 return 1;
1242}
1243
Elliott Hughesb7556142018-02-20 17:03:16 -08001244#ifdef ptrace_getregset_or_getregs
1245# define get_syscall_result_regs get_regs
1246#else
Dmitry V. Levin7386ca72015-11-30 13:57:51 +00001247static int get_syscall_result_regs(struct tcb *);
Dmitry V. Levinf51aec62015-11-30 00:01:01 +00001248#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001249
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001250/* Returns:
Elliott Hughesdc75b012017-07-05 13:54:44 -07001251 * 1: ok, continue in syscall_exiting_trace().
1252 * -1: error, syscall_exiting_trace() should print error indicator
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001253 * ("????" etc) and bail out.
1254 */
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001255static int
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001256get_syscall_result(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001257{
Elliott Hughesb7556142018-02-20 17:03:16 -08001258 if (get_syscall_result_regs(tcp) < 0)
Dmitry V. Levinf51aec62015-11-30 00:01:01 +00001259 return -1;
Dmitry V. Levin0c8c5c92015-11-29 00:06:45 +00001260 tcp->u_error = 0;
Elliott Hughesb7556142018-02-20 17:03:16 -08001261 get_error(tcp,
Elliott Hughes03a418e2018-06-15 13:11:40 -07001262 (!(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS)
1263 || syscall_tampered(tcp))
1264 && !syscall_tampered_nofail(tcp));
Dmitry V. Levin0c8c5c92015-11-29 00:06:45 +00001265
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001266 return 1;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00001267}
Dmitry V. Levin7386ca72015-11-30 13:57:51 +00001268
1269#include "get_scno.c"
Elliott Hughesd35df492017-02-15 15:19:05 -08001270#include "set_scno.c"
Dmitry V. Levin7386ca72015-11-30 13:57:51 +00001271#include "get_syscall_args.c"
Elliott Hughesb7556142018-02-20 17:03:16 -08001272#ifndef ptrace_getregset_or_getregs
Dmitry V. Levin7386ca72015-11-30 13:57:51 +00001273# include "get_syscall_result.c"
1274#endif
1275#include "get_error.c"
Elliott Hughesd35df492017-02-15 15:19:05 -08001276#include "set_error.c"
1277#ifdef HAVE_GETREGS_OLD
Dmitry V. Levin7386ca72015-11-30 13:57:51 +00001278# include "getregs_old.c"
1279#endif
Elliott Hughesb7556142018-02-20 17:03:16 -08001280#include "shuffle_scno.c"
Elliott Hughesd35df492017-02-15 15:19:05 -08001281
1282const char *
1283syscall_name(kernel_ulong_t scno)
1284{
Elliott Hughesdc75b012017-07-05 13:54:44 -07001285 return scno_is_valid(scno) ? sysent[scno].sys_name : NULL;
Elliott Hughesd35df492017-02-15 15:19:05 -08001286}