blob: 6c5119f37a08956101a5d34575510577bb513a10 [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"
Dmitry V. Levine2fb0bb2015-09-15 21:51:15 +000035#include "native_defs.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000036#include <sys/param.h>
Dmitry V. Levin2a6ac942016-01-12 00:04:30 +000037#include <signal.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000038
Dmitry V. Levin5503dd22015-02-13 02:12:14 +000039/* for struct iovec */
40#include <sys/uio.h>
Maarten ter Huurne40c174b2014-10-20 01:02:48 +020041
Dmitry V. Levin7211dbc2015-02-28 12:20:21 +000042#include "regs.h"
Dmitry V. Levinfadf3792015-02-13 00:26:38 +000043#include "ptrace.h"
Wichert Akkerman15dea971999-10-06 13:06:34 +000044
Denys Vlasenko84703742012-02-25 02:38:52 +010045#if defined(SPARC64)
Roland McGrath6d1a65c2004-07-12 07:44:08 +000046# undef PTRACE_GETREGS
47# define PTRACE_GETREGS PTRACE_GETREGS64
48# undef PTRACE_SETREGS
49# define PTRACE_SETREGS PTRACE_SETREGS64
Denys Vlasenko84703742012-02-25 02:38:52 +010050#endif
Roland McGrath6d1a65c2004-07-12 07:44:08 +000051
Dmitry V. Levin5503dd22015-02-13 02:12:14 +000052#if defined SPARC64
53# include <asm/psrcompat.h>
54#elif defined SPARC
55# include <asm/psr.h>
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010056#endif
57
Dmitry V. Levin48f08902015-03-05 23:30:02 +000058#ifdef IA64
59# include <asm/rse.h>
60#endif
61
Dmitry V. Levin4c3f2ae2015-02-13 22:53:00 +000062#ifndef NT_PRSTATUS
63# define NT_PRSTATUS 1
64#endif
65
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000066#ifndef NSIG
Denys Vlasenko523635f2012-02-25 02:44:25 +010067# warning: NSIG is not defined, using 32
68# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000069#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000070
71#include "syscall.h"
72
73/* Define these shorthand notations to simplify the syscallent files. */
Roland McGrath2fe7b132005-07-05 03:25:35 +000074#define TD TRACE_DESC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000075#define TF TRACE_FILE
76#define TI TRACE_IPC
77#define TN TRACE_NETWORK
78#define TP TRACE_PROCESS
79#define TS TRACE_SIGNAL
Namhyung Kim96792962012-10-24 11:41:57 +090080#define TM TRACE_MEMORY
Dmitry V. Levin50a218d2011-01-18 17:36:20 +000081#define NF SYSCALL_NEVER_FAILS
Denys Vlasenkoac1ce772011-08-23 13:24:17 +020082#define MA MAX_ARGS
Masatake YAMATO1d78d222014-04-16 15:33:11 +090083#define SI STACKTRACE_INVALIDATE_CACHE
84#define SE STACKTRACE_CAPTURE_ON_ENTER
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000085
Szabolcs Nagy34683e32015-12-15 18:32:17 +000086#define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
Elvira Khabirova140ecf82015-07-10 22:24:48 +030087
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +010088const struct_sysent sysent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000089#include "syscallent.h"
90};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000091
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010092#if SUPPORTED_PERSONALITIES > 1
Elvira Khabirova09294222015-08-04 01:47:02 +030093# include PERSONALITY1_INCLUDE_FUNCS
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010094static const struct_sysent sysent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +010095# include "syscallent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000096};
Denys Vlasenko39fca622011-08-20 02:12:33 +020097#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000098
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010099#if SUPPORTED_PERSONALITIES > 2
Elvira Khabirova09294222015-08-04 01:47:02 +0300100# include PERSONALITY2_INCLUDE_FUNCS
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100101static const struct_sysent sysent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100102# include "syscallent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000103};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200104#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000105
106/* Now undef them since short defines cause wicked namespace pollution. */
Elvira Khabirova140ecf82015-07-10 22:24:48 +0300107#undef SEN
Roland McGrath2fe7b132005-07-05 03:25:35 +0000108#undef TD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000109#undef TF
110#undef TI
111#undef TN
112#undef TP
113#undef TS
Namhyung Kim96792962012-10-24 11:41:57 +0900114#undef TM
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000115#undef NF
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200116#undef MA
Masatake YAMATO1d78d222014-04-16 15:33:11 +0900117#undef SI
118#undef SE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000119
Denys Vlasenko39fca622011-08-20 02:12:33 +0200120/*
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +0000121 * `ioctlent[012].h' files are automatically generated by the auxiliary
Denys Vlasenko39fca622011-08-20 02:12:33 +0200122 * program `ioctlsort', such that the list is sorted by the `code' field.
123 * This has the side-effect of resolving the _IO.. macros into
124 * plain integers, eliminating the need to include here everything
125 * in "/usr/include".
126 */
127
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100128const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000129#include "errnoent.h"
130};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100131const char *const signalent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200132#include "signalent.h"
133};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100134const struct_ioctlent ioctlent0[] = {
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +0000135#include "ioctlent0.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200136};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000137
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100138#if SUPPORTED_PERSONALITIES > 1
Roland McGrathee36ce12004-09-04 03:53:10 +0000139static const char *const errnoent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100140# include "errnoent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000141};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200142static const char *const signalent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100143# include "signalent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200144};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100145static const struct_ioctlent ioctlent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100146# include "ioctlent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200147};
Elvira Khabirova09294222015-08-04 01:47:02 +0300148# include PERSONALITY0_INCLUDE_PRINTERS_DECLS
149static const struct_printers printers0 = {
150# include PERSONALITY0_INCLUDE_PRINTERS_DEFS
151};
152# include PERSONALITY1_INCLUDE_PRINTERS_DECLS
153static const struct_printers printers1 = {
154# include PERSONALITY1_INCLUDE_PRINTERS_DEFS
155};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200156#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000157
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100158#if SUPPORTED_PERSONALITIES > 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000159static const char *const errnoent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100160# include "errnoent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000161};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200162static const char *const signalent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100163# include "signalent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200164};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100165static const struct_ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100166# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200167};
Elvira Khabirova09294222015-08-04 01:47:02 +0300168# include PERSONALITY2_INCLUDE_PRINTERS_DECLS
169static const struct_printers printers2 = {
170# include PERSONALITY2_INCLUDE_PRINTERS_DEFS
171};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200172#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000173
Dmitry V. Levine6f55242013-02-26 22:02:30 +0000174enum {
175 nsyscalls0 = ARRAY_SIZE(sysent0)
176#if SUPPORTED_PERSONALITIES > 1
177 , nsyscalls1 = ARRAY_SIZE(sysent1)
178# if SUPPORTED_PERSONALITIES > 2
179 , nsyscalls2 = ARRAY_SIZE(sysent2)
180# endif
181#endif
182};
183
184enum {
185 nerrnos0 = ARRAY_SIZE(errnoent0)
186#if SUPPORTED_PERSONALITIES > 1
187 , nerrnos1 = ARRAY_SIZE(errnoent1)
188# if SUPPORTED_PERSONALITIES > 2
189 , nerrnos2 = ARRAY_SIZE(errnoent2)
190# endif
191#endif
192};
193
194enum {
195 nsignals0 = ARRAY_SIZE(signalent0)
196#if SUPPORTED_PERSONALITIES > 1
197 , nsignals1 = ARRAY_SIZE(signalent1)
198# if SUPPORTED_PERSONALITIES > 2
199 , nsignals2 = ARRAY_SIZE(signalent2)
200# endif
201#endif
202};
203
204enum {
205 nioctlents0 = ARRAY_SIZE(ioctlent0)
206#if SUPPORTED_PERSONALITIES > 1
207 , nioctlents1 = ARRAY_SIZE(ioctlent1)
208# if SUPPORTED_PERSONALITIES > 2
209 , nioctlents2 = ARRAY_SIZE(ioctlent2)
210# endif
211#endif
212};
213
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100214#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100215const struct_sysent *sysent = sysent0;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100216const char *const *errnoent = errnoent0;
217const char *const *signalent = signalent0;
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100218const struct_ioctlent *ioctlent = ioctlent0;
Elvira Khabirova09294222015-08-04 01:47:02 +0300219const struct_printers *printers = &printers0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100220#endif
Elvira Khabirova09294222015-08-04 01:47:02 +0300221
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100222unsigned nsyscalls = nsyscalls0;
223unsigned nerrnos = nerrnos0;
224unsigned nsignals = nsignals0;
225unsigned nioctlents = nioctlents0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100226
227unsigned num_quals;
228qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
229
230static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
231 nsyscalls0,
232#if SUPPORTED_PERSONALITIES > 1
233 nsyscalls1,
234#endif
235#if SUPPORTED_PERSONALITIES > 2
236 nsyscalls2,
237#endif
238};
239static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
240 sysent0,
241#if SUPPORTED_PERSONALITIES > 1
242 sysent1,
243#endif
244#if SUPPORTED_PERSONALITIES > 2
245 sysent2,
246#endif
247};
248
249enum {
250 MAX_NSYSCALLS1 = (nsyscalls0
251#if SUPPORTED_PERSONALITIES > 1
252 > nsyscalls1 ? nsyscalls0 : nsyscalls1
253#endif
254 ),
255 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
256#if SUPPORTED_PERSONALITIES > 2
257 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
258#endif
259 ),
260 MAX_NSYSCALLS = MAX_NSYSCALLS2,
261 /* We are ready for arches with up to 255 signals,
262 * even though the largest known signo is on MIPS and it is 128.
263 * The number of existing syscalls on all arches is
264 * larger that 255 anyway, so it is just a pedantic matter.
265 */
266 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
267};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000268
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100269#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100270unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000271
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100272# ifndef current_wordsize
273unsigned current_wordsize;
274static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000275 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000276 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100277# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000278 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100279# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200280};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100281# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000282
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200283void
Dmitry V. Levin3abe8b22006-12-20 22:37:21 +0000284set_personality(int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000285{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100286 nsyscalls = nsyscall_vec[personality];
287 sysent = sysent_vec[personality];
288
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000289 switch (personality) {
290 case 0:
291 errnoent = errnoent0;
292 nerrnos = nerrnos0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000293 ioctlent = ioctlent0;
294 nioctlents = nioctlents0;
295 signalent = signalent0;
296 nsignals = nsignals0;
Elvira Khabirova09294222015-08-04 01:47:02 +0300297 printers = &printers0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000298 break;
299
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000300 case 1:
301 errnoent = errnoent1;
302 nerrnos = nerrnos1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000303 ioctlent = ioctlent1;
304 nioctlents = nioctlents1;
305 signalent = signalent1;
306 nsignals = nsignals1;
Elvira Khabirova09294222015-08-04 01:47:02 +0300307 printers = &printers1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000308 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000309
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100310# if SUPPORTED_PERSONALITIES > 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000311 case 2:
312 errnoent = errnoent2;
313 nerrnos = nerrnos2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000314 ioctlent = ioctlent2;
315 nioctlents = nioctlents2;
316 signalent = signalent2;
317 nsignals = nsignals2;
Elvira Khabirova09294222015-08-04 01:47:02 +0300318 printers = &printers2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000319 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100320# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000321 }
322
323 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100324# ifndef current_wordsize
325 current_wordsize = personality_wordsize[personality];
326# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000327}
328
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000329static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000330update_personality(struct tcb *tcp, unsigned int personality)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000331{
332 if (personality == current_personality)
333 return;
334 set_personality(personality);
335
336 if (personality == tcp->currpers)
337 return;
338 tcp->currpers = personality;
339
Dmitry V. Levin6c8ef052015-05-25 22:51:19 +0000340# undef PERSONALITY_NAMES
341# if defined POWERPC64
342# define PERSONALITY_NAMES {"64 bit", "32 bit"}
343# elif defined X86_64
344# define PERSONALITY_NAMES {"64 bit", "32 bit", "x32"}
345# elif defined X32
346# define PERSONALITY_NAMES {"x32", "32 bit"}
347# elif defined AARCH64
Dmitry V. Levinc37173f2015-12-09 00:43:39 +0000348# define PERSONALITY_NAMES {"64 bit", "32 bit"}
Dmitry V. Levin6c8ef052015-05-25 22:51:19 +0000349# elif defined TILE
350# define PERSONALITY_NAMES {"64-bit", "32-bit"}
351# endif
352# ifdef PERSONALITY_NAMES
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000353 if (!qflag) {
Dmitry V. Levin6c8ef052015-05-25 22:51:19 +0000354 static const char *const names[] = PERSONALITY_NAMES;
355 error_msg("[ Process PID=%d runs in %s mode. ]",
356 tcp->pid, names[personality]);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100357 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100358# endif
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000359}
360#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000361
Denys Vlasenkoc1540fe2013-02-21 16:17:08 +0100362static int qual_syscall(), qual_signal(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000363
Roland McGrathe10e62a2004-09-04 04:20:43 +0000364static const struct qual_options {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000365 unsigned int bitflag;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000366 const char *option_name;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000367 int (*qualify)(const char *, int, int);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000368 const char *argument_name;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000369} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000370 { QUAL_TRACE, "trace", qual_syscall, "system call" },
371 { QUAL_TRACE, "t", qual_syscall, "system call" },
372 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
373 { QUAL_ABBREV, "a", qual_syscall, "system call" },
374 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
375 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
376 { QUAL_RAW, "raw", qual_syscall, "system call" },
377 { QUAL_RAW, "x", qual_syscall, "system call" },
378 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
379 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
380 { QUAL_SIGNAL, "s", qual_signal, "signal" },
Roland McGrath9797ceb2002-12-30 10:23:00 +0000381 { QUAL_READ, "read", qual_desc, "descriptor" },
382 { QUAL_READ, "reads", qual_desc, "descriptor" },
383 { QUAL_READ, "r", qual_desc, "descriptor" },
384 { QUAL_WRITE, "write", qual_desc, "descriptor" },
385 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
386 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000387 { 0, NULL, NULL, NULL },
388};
389
Roland McGrath9797ceb2002-12-30 10:23:00 +0000390static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000391reallocate_qual(const unsigned int n)
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100392{
393 unsigned p;
394 qualbits_t *qp;
395 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
Dmitry V. Levin3e9d71f2015-05-25 20:41:02 +0000396 qp = qual_vec[p] = xreallocarray(qual_vec[p], n,
397 sizeof(qualbits_t));
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100398 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
399 }
400 num_quals = n;
401}
402
403static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000404qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000405{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000406 int p;
Roland McGrath138c6a32006-01-12 09:50:49 +0000407
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100408 if (num_quals <= n)
409 reallocate_qual(n + 1);
Roland McGrath138c6a32006-01-12 09:50:49 +0000410
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100411 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
412 if (pers == p || pers < 0) {
413 if (not)
414 qual_vec[p][n] &= ~bitflag;
415 else
416 qual_vec[p][n] |= bitflag;
417 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000418 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000419}
420
421static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000422qual_syscall(const char *s, const unsigned int bitflag, const int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000423{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000424 int p;
425 unsigned int i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000426 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000427
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100428 if (*s >= '0' && *s <= '9') {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100429 i = string_to_uint(s);
Denys Vlasenkob43dacd2013-02-23 18:19:28 +0100430 if (i >= MAX_NSYSCALLS)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000431 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000432 qualify_one(i, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000433 return 0;
Roland McGrath48a035f2006-01-12 09:45:56 +0000434 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000435
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100436 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
437 for (i = 0; i < nsyscall_vec[p]; i++) {
438 if (sysent_vec[p][i].sys_name
439 && strcmp(s, sysent_vec[p][i].sys_name) == 0
440 ) {
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000441 qualify_one(i, bitflag, not, p);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100442 rc = 0;
443 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000444 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100445 }
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000446
Roland McGrathfe6b3522005-02-02 04:40:11 +0000447 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000448}
449
450static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000451qual_signal(const char *s, const unsigned int bitflag, const int not)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000452{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000453 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000454
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100455 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000456 int signo = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100457 if (signo < 0 || signo > 255)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000458 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000459 qualify_one(signo, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000460 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000461 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000462 if (strncasecmp(s, "SIG", 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000463 s += 3;
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100464 for (i = 0; i <= NSIG; i++) {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000465 if (strcasecmp(s, signame(i) + 3) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000466 qualify_one(i, bitflag, not, -1);
Roland McGrath76421df2005-02-02 03:51:18 +0000467 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000468 }
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100469 }
Roland McGrath76421df2005-02-02 03:51:18 +0000470 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000471}
472
473static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000474qual_desc(const char *s, const unsigned int bitflag, const int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000475{
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100476 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000477 int desc = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100478 if (desc < 0 || desc > 0x7fff) /* paranoia */
Roland McGrathfe6b3522005-02-02 04:40:11 +0000479 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000480 qualify_one(desc, bitflag, not, -1);
Roland McGrath2b619022003-04-10 18:58:20 +0000481 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000482 }
483 return -1;
484}
485
486static int
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000487lookup_class(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000488{
489 if (strcmp(s, "file") == 0)
490 return TRACE_FILE;
491 if (strcmp(s, "ipc") == 0)
492 return TRACE_IPC;
493 if (strcmp(s, "network") == 0)
494 return TRACE_NETWORK;
495 if (strcmp(s, "process") == 0)
496 return TRACE_PROCESS;
497 if (strcmp(s, "signal") == 0)
498 return TRACE_SIGNAL;
Roland McGrath2fe7b132005-07-05 03:25:35 +0000499 if (strcmp(s, "desc") == 0)
500 return TRACE_DESC;
Namhyung Kim96792962012-10-24 11:41:57 +0900501 if (strcmp(s, "memory") == 0)
502 return TRACE_MEMORY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000503 return -1;
504}
505
506void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000507qualify(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000508{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000509 const struct qual_options *opt;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000510 char *copy;
511 const char *p;
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000512 int not;
513 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000514
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100515 if (num_quals == 0)
516 reallocate_qual(MIN_QUALS);
517
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000518 opt = &qual_options[0];
519 for (i = 0; (p = qual_options[i].option_name); i++) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000520 unsigned int len = strlen(p);
521 if (strncmp(s, p, len) == 0 && s[len] == '=') {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000522 opt = &qual_options[i];
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000523 s += len + 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000524 break;
525 }
526 }
527 not = 0;
528 if (*s == '!') {
529 not = 1;
530 s++;
531 }
532 if (strcmp(s, "none") == 0) {
533 not = 1 - not;
534 s = "all";
535 }
536 if (strcmp(s, "all") == 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100537 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000538 qualify_one(i, opt->bitflag, not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000539 }
540 return;
541 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100542 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000543 qualify_one(i, opt->bitflag, !not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000544 }
Dmitry V. Levin3e9d71f2015-05-25 20:41:02 +0000545 copy = xstrdup(s);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000546 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000547 int n;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000548 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100549 unsigned pers;
550 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
551 for (i = 0; i < nsyscall_vec[pers]; i++)
552 if (sysent_vec[pers][i].sys_flags & n)
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000553 qualify_one(i, opt->bitflag, not, pers);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100554 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000555 continue;
556 }
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000557 if (opt->qualify(p, opt->bitflag, not)) {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100558 error_msg_and_die("invalid %s '%s'",
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000559 opt->argument_name, p);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000560 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000561 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000562 free(copy);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000563 return;
564}
565
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000566#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000567static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000568decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000569{
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000570 unsigned long addr;
Dmitry V. Levinc2155692015-03-22 15:52:40 +0000571 unsigned int n;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000572
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000573 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
574 return;
575
576 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100577 tcp->qual_flg = qual_flags[tcp->scno];
578 tcp->s_ent = &sysent[tcp->scno];
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000579 addr = tcp->u_arg[1];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100580 n = tcp->s_ent->nargs;
Dmitry V. Levinc2155692015-03-22 15:52:40 +0000581 if (sizeof(tcp->u_arg[0]) == current_wordsize) {
582 memset(tcp->u_arg, 0, n * sizeof(tcp->u_arg[0]));
583 (void) umoven(tcp, addr, n * sizeof(tcp->u_arg[0]), tcp->u_arg);
584 } else {
585 unsigned int args[n];
586 unsigned int i;
587
588 memset(args, 0, sizeof(args));
589 (void) umove(tcp, addr, &args);
590 for (i = 0; i < n; ++i)
591 tcp->u_arg[i] = args[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000592 }
593}
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000594#endif
Mike Frysinger3362e892012-03-15 01:09:19 -0400595
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000596#ifdef SYS_ipc_subcall
597static void
598decode_ipc_subcall(struct tcb *tcp)
599{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100600 unsigned int i, n;
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000601
602 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
603 return;
604
605 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100606 tcp->qual_flg = qual_flags[tcp->scno];
607 tcp->s_ent = &sysent[tcp->scno];
608 n = tcp->s_ent->nargs;
609 for (i = 0; i < n; i++)
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000610 tcp->u_arg[i] = tcp->u_arg[i + 1];
611}
612#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000613
Dmitry V. Levinf34b97f2015-04-17 09:14:19 +0000614#ifdef LINUX_MIPSO32
615static void
616decode_mips_subcall(struct tcb *tcp)
617{
618 if (!SCNO_IS_VALID(tcp->u_arg[0]))
619 return;
620 tcp->scno = tcp->u_arg[0];
621 tcp->qual_flg = qual_flags[tcp->scno];
622 tcp->s_ent = &sysent[tcp->scno];
623 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
624 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
625 /*
626 * Fetching the last arg of 7-arg syscalls (fadvise64_64
627 * and sync_file_range) would require additional code,
628 * see linux/mips/get_syscall_args.c
629 */
630}
Dmitry V. Levinf34b97f2015-04-17 09:14:19 +0000631#endif
632
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200633int
634printargs(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000635{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200636 if (entering(tcp)) {
637 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100638 int n = tcp->s_ent->nargs;
639 for (i = 0; i < n; i++)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200640 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
641 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000642 return 0;
643}
644
Denys Vlasenko72879c62012-02-27 14:18:02 +0100645int
Dmitry V. Levin1ab6e252016-02-14 16:26:37 +0000646printargs_u(struct tcb *tcp)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100647{
Dmitry V. Levin835127d2016-02-14 15:53:05 +0000648 const int n = tcp->s_ent->nargs;
649 int i;
650 for (i = 0; i < n; ++i)
Dmitry V. Levin1ab6e252016-02-14 16:26:37 +0000651 tprintf("%s%u", i ? ", " : "",
652 (unsigned int) tcp->u_arg[i]);
Dmitry V. Levin835127d2016-02-14 15:53:05 +0000653 return RVAL_DECODED;
Denys Vlasenko72879c62012-02-27 14:18:02 +0100654}
655
656int
Dmitry V. Levin1ab6e252016-02-14 16:26:37 +0000657printargs_d(struct tcb *tcp)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100658{
Dmitry V. Levin835127d2016-02-14 15:53:05 +0000659 const int n = tcp->s_ent->nargs;
660 int i;
661 for (i = 0; i < n; ++i)
Dmitry V. Levin1ab6e252016-02-14 16:26:37 +0000662 tprintf("%s%d", i ? ", " : "",
663 (int) tcp->u_arg[i]);
Dmitry V. Levin835127d2016-02-14 15:53:05 +0000664 return RVAL_DECODED;
Denys Vlasenko72879c62012-02-27 14:18:02 +0100665}
666
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000667static void
668dumpio(struct tcb *tcp)
669{
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300670 int sen;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000671
672 if (syserror(tcp))
673 return;
674 if ((unsigned long) tcp->u_arg[0] >= num_quals)
675 return;
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300676 sen = tcp->s_ent->sen;
677 if (SEN_printargs == sen)
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000678 return;
679 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300680 switch (sen) {
681 case SEN_read:
682 case SEN_pread:
683 case SEN_recv:
684 case SEN_recvfrom:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000685 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
686 return;
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300687 case SEN_readv:
Dmitry V. Levin3f6ebce2016-03-31 00:01:58 +0000688 case SEN_preadv:
Dmitry V. Levin05a0af62016-01-20 00:17:02 +0000689 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
690 tcp->u_rval);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000691 return;
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300692 case SEN_recvmsg:
Dmitry V. Levin93c9d1c2016-01-20 03:26:37 +0000693 dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000694 return;
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300695 case SEN_recvmmsg:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000696 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
697 return;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000698 }
699 }
700 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300701 switch (sen) {
702 case SEN_write:
703 case SEN_pwrite:
704 case SEN_send:
705 case SEN_sendto:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000706 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300707 break;
708 case SEN_writev:
Dmitry V. Levin3f6ebce2016-03-31 00:01:58 +0000709 case SEN_pwritev:
Dmitry V. Levin13870c52016-02-13 23:18:15 +0000710 case SEN_vmsplice:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000711 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300712 break;
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300713 case SEN_sendmsg:
Dmitry V. Levin93c9d1c2016-01-20 03:26:37 +0000714 dumpiov_in_msghdr(tcp, tcp->u_arg[1],
715 (unsigned long) -1L);
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300716 break;
717 case SEN_sendmmsg:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000718 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300719 break;
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300720 }
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000721 }
722}
723
724/*
725 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
726 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
727 */
728#if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
729static long
730shuffle_scno(unsigned long scno)
731{
732 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
733 return scno;
734
735 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
736 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
737 return 0x000ffff0;
738 if (scno == 0x000ffff0)
739 return ARM_FIRST_SHUFFLED_SYSCALL;
740
741#define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
742 /*
743 * Is it ARM specific syscall?
744 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
745 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
746 */
747 if (scno >= 0x000f0000 &&
748 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
749 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
750 }
751 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
752 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
753 }
754
755 return scno;
756}
757#else
758# define shuffle_scno(scno) ((long)(scno))
759#endif
760
Mike Frysingerd2eaf672015-08-18 03:24:59 -0400761const char *
762syscall_name(long scno)
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000763{
764 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
765
Mike Frysingerd2eaf672015-08-18 03:24:59 -0400766 if (SCNO_IS_VALID(scno))
767 return sysent[scno].sys_name;
768 else {
769 sprintf(buf, "syscall_%lu", shuffle_scno(scno));
770 return buf;
771 }
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000772}
773
774static long get_regs_error;
775
776void
777clear_regs(void)
778{
779 get_regs_error = -1;
780}
781
782static int get_syscall_args(struct tcb *);
783static int get_syscall_result(struct tcb *);
Dmitry V. Levin7386ca72015-11-30 13:57:51 +0000784static int arch_get_scno(struct tcb *tcp);
785static void get_error(struct tcb *, const bool);
786#if defined X86_64 || defined POWERPC
787static int getregs_old(pid_t);
788#endif
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000789
790static int
791trace_syscall_entering(struct tcb *tcp)
792{
793 int res, scno_good;
794
795 scno_good = res = get_scno(tcp);
796 if (res == 0)
797 return res;
798 if (res == 1)
799 res = get_syscall_args(tcp);
800
801 if (res != 1) {
802 printleader(tcp);
803 if (scno_good != 1)
804 tprints("????" /* anti-trigraph gap */ "(");
805 else if (tcp->qual_flg & UNDEFINED_SCNO)
Mike Frysingerd2eaf672015-08-18 03:24:59 -0400806 tprintf("%s(", syscall_name(tcp->scno));
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000807 else
808 tprintf("%s(", tcp->s_ent->sys_name);
809 /*
810 * " <unavailable>" will be added later by the code which
811 * detects ptrace errors.
812 */
813 goto ret;
814 }
815
Dmitry V. Levinf34b97f2015-04-17 09:14:19 +0000816#ifdef LINUX_MIPSO32
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300817 if (SEN_syscall == tcp->s_ent->sen)
Dmitry V. Levinf34b97f2015-04-17 09:14:19 +0000818 decode_mips_subcall(tcp);
819#endif
820
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300821 if ( SEN_execve == tcp->s_ent->sen
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000822# if defined(SPARC) || defined(SPARC64)
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300823 || SEN_execv == tcp->s_ent->sen
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000824# endif
825 ) {
826 hide_log_until_execve = 0;
827 }
828
829#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300830 switch (tcp->s_ent->sen) {
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000831# ifdef SYS_socket_subcall
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300832 case SEN_socketcall:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000833 decode_socket_subcall(tcp);
834 break;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000835# endif
836# ifdef SYS_ipc_subcall
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300837 case SEN_ipc:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000838 decode_ipc_subcall(tcp);
839 break;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000840# endif
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000841 }
842#endif
843
844 if (!(tcp->qual_flg & QUAL_TRACE)
845 || (tracing_paths && !pathtrace_match(tcp))
846 ) {
847 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000848 tcp->sys_func_rval = 0;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000849 return 0;
850 }
851
852 tcp->flags &= ~TCB_FILTERED;
853
854 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
855 res = 0;
856 goto ret;
857 }
858
859#ifdef USE_LIBUNWIND
860 if (stack_trace_enabled) {
861 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
862 unwind_capture_stacktrace(tcp);
863 }
864#endif
865
866 printleader(tcp);
867 if (tcp->qual_flg & UNDEFINED_SCNO)
Mike Frysingerd2eaf672015-08-18 03:24:59 -0400868 tprintf("%s(", syscall_name(tcp->scno));
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000869 else
870 tprintf("%s(", tcp->s_ent->sys_name);
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300871 if ((tcp->qual_flg & QUAL_RAW) && SEN_exit != tcp->s_ent->sen)
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000872 res = printargs(tcp);
873 else
874 res = tcp->s_ent->sys_func(tcp);
875
876 fflush(tcp->outf);
877 ret:
878 tcp->flags |= TCB_INSYSCALL;
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000879 tcp->sys_func_rval = res;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000880 /* Measure the entrance time as late as possible to avoid errors. */
881 if (Tflag || cflag)
882 gettimeofday(&tcp->etime, NULL);
883 return res;
884}
885
886static int
887trace_syscall_exiting(struct tcb *tcp)
888{
889 int sys_res;
890 struct timeval tv;
891 int res;
892 long u_error;
893
894 /* Measure the exit time as early as possible to avoid errors. */
895 if (Tflag || cflag)
896 gettimeofday(&tv, NULL);
897
898#ifdef USE_LIBUNWIND
899 if (stack_trace_enabled) {
900 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
901 unwind_cache_invalidate(tcp);
902 }
903#endif
904
905#if SUPPORTED_PERSONALITIES > 1
906 update_personality(tcp, tcp->currpers);
907#endif
908 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Dmitry V. Levin4b80f342015-06-29 11:57:44 +0000909 if (filtered(tcp) || hide_log_until_execve)
910 goto ret;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000911
912 if (cflag) {
913 count_syscall(tcp, &tv);
914 if (cflag == CFLAG_ONLY_STATS) {
915 goto ret;
916 }
917 }
918
919 /* If not in -ff mode, and printing_tcp != tcp,
920 * then the log currently does not end with output
921 * of _our syscall entry_, but with something else.
922 * We need to say which syscall's return is this.
923 *
924 * Forced reprinting via TCB_REPRINT is used only by
925 * "strace -ff -oLOG test/threaded_execve" corner case.
926 * It's the only case when -ff mode needs reprinting.
927 */
928 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
929 tcp->flags &= ~TCB_REPRINT;
930 printleader(tcp);
931 if (tcp->qual_flg & UNDEFINED_SCNO)
Mike Frysingerd2eaf672015-08-18 03:24:59 -0400932 tprintf("<... %s resumed> ", syscall_name(tcp->scno));
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000933 else
934 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
935 }
936 printing_tcp = tcp;
937
938 tcp->s_prev_ent = NULL;
939 if (res != 1) {
940 /* There was error in one of prior ptrace ops */
941 tprints(") ");
942 tabto();
943 tprints("= ? <unavailable>\n");
944 line_ended();
945 tcp->flags &= ~TCB_INSYSCALL;
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000946 tcp->sys_func_rval = 0;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000947 return res;
948 }
949 tcp->s_prev_ent = tcp->s_ent;
950
951 sys_res = 0;
952 if (tcp->qual_flg & QUAL_RAW) {
953 /* sys_res = printargs(tcp); - but it's nop on sysexit */
954 } else {
955 /* FIXME: not_failing_only (IOW, option -z) is broken:
956 * failure of syscall is known only after syscall return.
957 * Thus we end up with something like this on, say, ENOENT:
958 * open("doesnt_exist", O_RDONLY <unfinished ...>
959 * {next syscall decode}
960 * whereas the intended result is that open(...) line
961 * is not shown at all.
962 */
963 if (not_failing_only && tcp->u_error)
964 goto ret; /* ignore failed syscalls */
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000965 if (tcp->sys_func_rval & RVAL_DECODED)
966 sys_res = tcp->sys_func_rval;
967 else
968 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000969 }
970
971 tprints(") ");
972 tabto();
973 u_error = tcp->u_error;
974 if (tcp->qual_flg & QUAL_RAW) {
975 if (u_error)
976 tprintf("= -1 (errno %ld)", u_error);
977 else
978 tprintf("= %#lx", tcp->u_rval);
979 }
980 else if (!(sys_res & RVAL_NONE) && u_error) {
981 switch (u_error) {
982 /* Blocked signals do not interrupt any syscalls.
983 * In this case syscalls don't return ERESTARTfoo codes.
984 *
985 * Deadly signals set to SIG_DFL interrupt syscalls
986 * and kill the process regardless of which of the codes below
987 * is returned by the interrupted syscall.
988 * In some cases, kernel forces a kernel-generated deadly
989 * signal to be unblocked and set to SIG_DFL (and thus cause
990 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
991 * or SIGILL. (The alternative is to leave process spinning
992 * forever on the faulty instruction - not useful).
993 *
994 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
995 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
996 * but kernel will always restart them.
997 */
998 case ERESTARTSYS:
999 /* Most common type of signal-interrupted syscall exit code.
1000 * The system call will be restarted with the same arguments
1001 * if SA_RESTART is set; otherwise, it will fail with EINTR.
1002 */
1003 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
1004 break;
1005 case ERESTARTNOINTR:
1006 /* Rare. For example, fork() returns this if interrupted.
1007 * SA_RESTART is ignored (assumed set): the restart is unconditional.
1008 */
1009 tprints("= ? ERESTARTNOINTR (To be restarted)");
1010 break;
1011 case ERESTARTNOHAND:
1012 /* pause(), rt_sigsuspend() etc use this code.
1013 * SA_RESTART is ignored (assumed not set):
1014 * syscall won't restart (will return EINTR instead)
1015 * even after signal with SA_RESTART set. However,
1016 * after SIG_IGN or SIG_DFL signal it will restart
1017 * (thus the name "restart only if has no handler").
1018 */
1019 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
1020 break;
1021 case ERESTART_RESTARTBLOCK:
1022 /* Syscalls like nanosleep(), poll() which can't be
1023 * restarted with their original arguments use this
1024 * code. Kernel will execute restart_syscall() instead,
1025 * which changes arguments before restarting syscall.
1026 * SA_RESTART is ignored (assumed not set) similarly
1027 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
1028 * since restart data is saved in "restart block"
1029 * in task struct, and if signal handler uses a syscall
1030 * which in turn saves another such restart block,
1031 * old data is lost and restart becomes impossible)
1032 */
1033 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
1034 break;
1035 default:
1036 if ((unsigned long) u_error < nerrnos
1037 && errnoent[u_error])
1038 tprintf("= -1 %s (%s)", errnoent[u_error],
1039 strerror(u_error));
1040 else
1041 tprintf("= -1 ERRNO_%lu (%s)", u_error,
1042 strerror(u_error));
1043 break;
1044 }
1045 if ((sys_res & RVAL_STR) && tcp->auxstr)
1046 tprintf(" (%s)", tcp->auxstr);
1047 }
1048 else {
1049 if (sys_res & RVAL_NONE)
1050 tprints("= ?");
1051 else {
1052 switch (sys_res & RVAL_MASK) {
1053 case RVAL_HEX:
1054#if SUPPORTED_PERSONALITIES > 1
1055 if (current_wordsize < sizeof(long))
1056 tprintf("= %#x",
1057 (unsigned int) tcp->u_rval);
1058 else
1059#endif
1060 tprintf("= %#lx", tcp->u_rval);
1061 break;
1062 case RVAL_OCTAL:
1063 tprintf("= %#lo", tcp->u_rval);
1064 break;
1065 case RVAL_UDECIMAL:
Dmitry V. Levinbe1cb922016-01-07 14:20:17 +00001066#if SUPPORTED_PERSONALITIES > 1
1067 if (current_wordsize < sizeof(long))
1068 tprintf("= %u",
1069 (unsigned int) tcp->u_rval);
1070 else
1071#endif
1072 tprintf("= %lu", tcp->u_rval);
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001073 break;
1074 case RVAL_DECIMAL:
1075 tprintf("= %ld", tcp->u_rval);
1076 break;
1077 case RVAL_FD:
1078 if (show_fd_path) {
1079 tprints("= ");
1080 printfd(tcp, tcp->u_rval);
1081 }
1082 else
1083 tprintf("= %ld", tcp->u_rval);
1084 break;
1085#if defined(LINUX_MIPSN32) || defined(X32)
1086 /*
1087 case RVAL_LHEX:
1088 tprintf("= %#llx", tcp->u_lrval);
1089 break;
1090 case RVAL_LOCTAL:
1091 tprintf("= %#llo", tcp->u_lrval);
1092 break;
1093 */
1094 case RVAL_LUDECIMAL:
1095 tprintf("= %llu", tcp->u_lrval);
1096 break;
1097 /*
1098 case RVAL_LDECIMAL:
1099 tprintf("= %lld", tcp->u_lrval);
1100 break;
1101 */
1102#endif
1103 default:
Dmitry V. Levin6c8ef052015-05-25 22:51:19 +00001104 error_msg("invalid rval format");
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001105 break;
1106 }
1107 }
1108 if ((sys_res & RVAL_STR) && tcp->auxstr)
1109 tprintf(" (%s)", tcp->auxstr);
1110 }
1111 if (Tflag) {
1112 tv_sub(&tv, &tv, &tcp->etime);
1113 tprintf(" <%ld.%06ld>",
1114 (long) tv.tv_sec, (long) tv.tv_usec);
1115 }
1116 tprints("\n");
1117 dumpio(tcp);
1118 line_ended();
1119
1120#ifdef USE_LIBUNWIND
1121 if (stack_trace_enabled)
1122 unwind_print_stacktrace(tcp);
1123#endif
1124
1125 ret:
1126 tcp->flags &= ~TCB_INSYSCALL;
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +00001127 tcp->sys_func_rval = 0;
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001128 return 0;
1129}
1130
1131int
1132trace_syscall(struct tcb *tcp)
1133{
1134 return exiting(tcp) ?
1135 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
1136}
1137
Dmitry V. Levin9af94a22015-09-18 01:54:59 +00001138bool
1139is_erestart(struct tcb *tcp)
1140{
1141 switch (tcp->u_error) {
1142 case ERESTARTSYS:
1143 case ERESTARTNOINTR:
1144 case ERESTARTNOHAND:
1145 case ERESTART_RESTARTBLOCK:
1146 return true;
1147 default:
1148 return false;
1149 }
1150}
1151
Dmitry V. Levin3858b932015-09-18 01:54:59 +00001152static int saved_u_error;
1153
1154void
1155temporarily_clear_syserror(struct tcb *tcp)
1156{
1157 saved_u_error = tcp->u_error;
1158 tcp->u_error = 0;
1159}
1160
1161void
1162restore_cleared_syserror(struct tcb *tcp)
1163{
1164 tcp->u_error = saved_u_error;
1165}
1166
Dmitry V. Levinbdb07e32015-11-27 01:51:22 +00001167#include "kernel_types.h"
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001168
1169/*
1170 * Check the syscall return value register value for whether it is
1171 * a negated errno code indicating an error, or a success return value.
1172 */
1173static inline bool
1174is_negated_errno(kernel_ulong_t val)
1175{
1176 /* Linux kernel defines MAX_ERRNO to 4095. */
1177 kernel_ulong_t max = -(kernel_long_t) 4095;
1178
1179#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1180 if (current_wordsize < sizeof(val)) {
1181 val = (uint32_t) val;
1182 max = (uint32_t) max;
1183 }
1184#elif defined X32
1185 /*
1186 * current_wordsize is 4 even in personality 0 (native X32)
1187 * but truncation _must not_ be done in it.
1188 * can't check current_wordsize here!
1189 */
1190 if (current_personality != 0) {
1191 val = (uint32_t) val;
1192 max = (uint32_t) max;
1193 }
1194#endif
1195
1196 return val >= max;
1197}
1198
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001199#include "arch_regs.c"
Wichert Akkermanc7926982000-04-10 22:22:31 +00001200
Dmitry V. Levin78ed3f32015-03-23 00:04:27 +00001201#ifdef HAVE_GETRVAL2
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001202# include "arch_getrval2.c"
Dmitry V. Levin48f08902015-03-05 23:30:02 +00001203#endif
1204
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001205void
Denys Vlasenko5a2483b2013-07-01 12:49:14 +02001206print_pc(struct tcb *tcp)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001207{
Dmitry V. Levin5105d4a2015-11-30 03:30:51 +00001208#if defined ARCH_PC_REG
1209# define ARCH_GET_PC 0
1210#elif defined ARCH_PC_PEEK_ADDR
1211 long pc;
1212# define ARCH_PC_REG pc
1213# define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
Dmitry V. Levinb2d9ff22015-02-23 13:43:20 +00001214#else
Dmitry V. Levin5105d4a2015-11-30 03:30:51 +00001215# error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001216#endif
Dmitry V. Levin5105d4a2015-11-30 03:30:51 +00001217 if (get_regs_error || ARCH_GET_PC)
1218 tprints(current_wordsize == 4 ? "[????????] "
1219 : "[????????????????] ");
1220 else
1221 tprintf(current_wordsize == 4 ? "[%08lx] " : "[%016lx] ",
1222 (unsigned long) ARCH_PC_REG);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001223}
1224
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001225#if defined ARCH_REGS_FOR_GETREGSET
1226static long
1227get_regset(pid_t pid)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001228{
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001229# ifdef ARCH_IOVEC_FOR_GETREGSET
1230 /* variable iovec */
1231 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1232 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1233 &ARCH_IOVEC_FOR_GETREGSET);
1234# else
1235 /* constant iovec */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001236 static struct iovec io = {
1237 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1238 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001239 };
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001240 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001241
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001242# endif
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001243}
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001244#endif /* ARCH_REGS_FOR_GETREGSET */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001245
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001246void
1247get_regs(pid_t pid)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001248{
Dmitry V. Levinf51aec62015-11-30 00:01:01 +00001249#undef USE_GET_SYSCALL_RESULT_REGS
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001250#ifdef ARCH_REGS_FOR_GETREGSET
1251# ifdef X86_64
1252 /* Try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS. */
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001253 static int getregset_support;
1254
1255 if (getregset_support >= 0) {
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001256 get_regs_error = get_regset(pid);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001257 if (getregset_support > 0)
1258 return;
1259 if (get_regs_error >= 0) {
1260 getregset_support = 1;
1261 return;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001262 }
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001263 if (errno == EPERM || errno == ESRCH)
1264 return;
1265 getregset_support = -1;
1266 }
Dmitry V. Levinf3e9e612015-11-30 15:30:05 +00001267 get_regs_error = getregs_old(pid);
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001268# else /* !X86_64 */
1269 /* Assume that PTRACE_GETREGSET works. */
1270 get_regs_error = get_regset(pid);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001271# endif
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001272#elif defined ARCH_REGS_FOR_GETREGS
1273# if defined SPARC || defined SPARC64
1274 /* SPARC systems have the meaning of data and addr reversed */
1275 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&ARCH_REGS_FOR_GETREGS, 0);
1276# elif defined POWERPC
1277 static bool old_kernel = 0;
1278 if (old_kernel)
1279 goto old;
1280 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1281 if (get_regs_error && errno == EIO) {
1282 old_kernel = 1;
1283 old:
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001284 get_regs_error = getregs_old(pid);
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001285 }
1286# else
1287 /* Assume that PTRACE_GETREGS works. */
1288 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1289# endif
1290
1291#else /* !ARCH_REGS_FOR_GETREGSET && !ARCH_REGS_FOR_GETREGS */
Dmitry V. Levinf51aec62015-11-30 00:01:01 +00001292# define USE_GET_SYSCALL_RESULT_REGS 1
1293# warning get_regs is not implemented for this architecture yet
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001294 get_regs_error = 0;
1295#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001296}
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001297
Dmitry V. Levin16510512015-11-30 01:46:52 +00001298/*
1299 * Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001300 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1301 * 1: ok, continue in trace_syscall_entering().
1302 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001303 * ("????" etc) and bail out.
1304 */
Denys Vlasenko8497b622015-03-21 17:51:52 +01001305int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001306get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001307{
Dmitry V. Levin144cda22015-03-23 18:48:32 +00001308 if (get_regs_error)
1309 return -1;
1310
Dmitry V. Levin16510512015-11-30 01:46:52 +00001311 int rc = arch_get_scno(tcp);
1312 if (rc != 1)
1313 return rc;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001314
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001315 if (SCNO_IS_VALID(tcp->scno)) {
Dmitry V. Levin16510512015-11-30 01:46:52 +00001316 tcp->s_ent = &sysent[tcp->scno];
1317 tcp->qual_flg = qual_flags[tcp->scno];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001318 } else {
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01001319 static const struct_sysent unknown = {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001320 .nargs = MAX_ARGS,
1321 .sys_flags = 0,
1322 .sys_func = printargs,
Denys Vlasenkocc59f142015-04-07 12:46:59 +02001323 .sys_name = "system call",
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001324 };
1325 tcp->s_ent = &unknown;
1326 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
Dmitry V. Levinea009002015-03-24 01:59:07 +00001327 if (debug_flag)
Dmitry V. Levin16510512015-11-30 01:46:52 +00001328 error_msg("pid %d invalid syscall %ld", tcp->pid, tcp->scno);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001329 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001330 return 1;
1331}
1332
Dmitry V. Levinf51aec62015-11-30 00:01:01 +00001333#ifdef USE_GET_SYSCALL_RESULT_REGS
Dmitry V. Levin7386ca72015-11-30 13:57:51 +00001334static int get_syscall_result_regs(struct tcb *);
Dmitry V. Levinf51aec62015-11-30 00:01:01 +00001335#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001336
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001337/* Returns:
1338 * 1: ok, continue in trace_syscall_exiting().
1339 * -1: error, trace_syscall_exiting() should print error indicator
1340 * ("????" etc) and bail out.
1341 */
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001342static int
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001343get_syscall_result(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001344{
Dmitry V. Levinf51aec62015-11-30 00:01:01 +00001345#ifdef USE_GET_SYSCALL_RESULT_REGS
1346 if (get_syscall_result_regs(tcp))
1347 return -1;
Masatake YAMATOed69fc22014-04-16 15:33:35 +09001348#endif
Dmitry V. Levin0c8c5c92015-11-29 00:06:45 +00001349 tcp->u_error = 0;
1350 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1351
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001352 return 1;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00001353}
Dmitry V. Levin7386ca72015-11-30 13:57:51 +00001354
1355#include "get_scno.c"
1356#include "get_syscall_args.c"
1357#ifdef USE_GET_SYSCALL_RESULT_REGS
1358# include "get_syscall_result.c"
1359#endif
1360#include "get_error.c"
1361#if defined X86_64 || defined POWERPC
1362# include "getregs_old.c"
1363#endif