blob: b0e20eeda971f5cb929fca548980534da2cddedf [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>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000037
Dmitry V. Levin5503dd22015-02-13 02:12:14 +000038/* for struct iovec */
39#include <sys/uio.h>
Maarten ter Huurne40c174b2014-10-20 01:02:48 +020040
Dmitry V. Levin7211dbc2015-02-28 12:20:21 +000041#include "regs.h"
Dmitry V. Levinfadf3792015-02-13 00:26:38 +000042#include "ptrace.h"
Wichert Akkerman15dea971999-10-06 13:06:34 +000043
Denys Vlasenko84703742012-02-25 02:38:52 +010044#if defined(SPARC64)
Roland McGrath6d1a65c2004-07-12 07:44:08 +000045# undef PTRACE_GETREGS
46# define PTRACE_GETREGS PTRACE_GETREGS64
47# undef PTRACE_SETREGS
48# define PTRACE_SETREGS PTRACE_SETREGS64
Denys Vlasenko84703742012-02-25 02:38:52 +010049#endif
Roland McGrath6d1a65c2004-07-12 07:44:08 +000050
Dmitry V. Levin5503dd22015-02-13 02:12:14 +000051#if defined SPARC64
52# include <asm/psrcompat.h>
53#elif defined SPARC
54# include <asm/psr.h>
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010055#endif
56
Dmitry V. Levin48f08902015-03-05 23:30:02 +000057#ifdef IA64
58# include <asm/rse.h>
59#endif
60
Dmitry V. Levin4c3f2ae2015-02-13 22:53:00 +000061#ifndef NT_PRSTATUS
62# define NT_PRSTATUS 1
63#endif
64
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000065#ifndef NSIG
Denys Vlasenko523635f2012-02-25 02:44:25 +010066# warning: NSIG is not defined, using 32
67# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000068#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000069
70#include "syscall.h"
71
72/* Define these shorthand notations to simplify the syscallent files. */
Roland McGrath2fe7b132005-07-05 03:25:35 +000073#define TD TRACE_DESC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000074#define TF TRACE_FILE
75#define TI TRACE_IPC
76#define TN TRACE_NETWORK
77#define TP TRACE_PROCESS
78#define TS TRACE_SIGNAL
Namhyung Kim96792962012-10-24 11:41:57 +090079#define TM TRACE_MEMORY
Dmitry V. Levin50a218d2011-01-18 17:36:20 +000080#define NF SYSCALL_NEVER_FAILS
Denys Vlasenkoac1ce772011-08-23 13:24:17 +020081#define MA MAX_ARGS
Masatake YAMATO1d78d222014-04-16 15:33:11 +090082#define SI STACKTRACE_INVALIDATE_CACHE
83#define SE STACKTRACE_CAPTURE_ON_ENTER
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000084
Szabolcs Nagy34683e32015-12-15 18:32:17 +000085#define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
Elvira Khabirova140ecf82015-07-10 22:24:48 +030086
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +010087const struct_sysent sysent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000088#include "syscallent.h"
89};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000090
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010091#if SUPPORTED_PERSONALITIES > 1
Elvira Khabirova09294222015-08-04 01:47:02 +030092# include PERSONALITY1_INCLUDE_FUNCS
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010093static const struct_sysent sysent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +010094# include "syscallent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000095};
Denys Vlasenko39fca622011-08-20 02:12:33 +020096#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000097
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010098#if SUPPORTED_PERSONALITIES > 2
Elvira Khabirova09294222015-08-04 01:47:02 +030099# include PERSONALITY2_INCLUDE_FUNCS
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100100static const struct_sysent sysent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100101# include "syscallent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000102};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200103#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000104
105/* Now undef them since short defines cause wicked namespace pollution. */
Elvira Khabirova140ecf82015-07-10 22:24:48 +0300106#undef SEN
Roland McGrath2fe7b132005-07-05 03:25:35 +0000107#undef TD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000108#undef TF
109#undef TI
110#undef TN
111#undef TP
112#undef TS
Namhyung Kim96792962012-10-24 11:41:57 +0900113#undef TM
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000114#undef NF
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200115#undef MA
Masatake YAMATO1d78d222014-04-16 15:33:11 +0900116#undef SI
117#undef SE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000118
Denys Vlasenko39fca622011-08-20 02:12:33 +0200119/*
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +0000120 * `ioctlent[012].h' files are automatically generated by the auxiliary
Denys Vlasenko39fca622011-08-20 02:12:33 +0200121 * program `ioctlsort', such that the list is sorted by the `code' field.
122 * This has the side-effect of resolving the _IO.. macros into
123 * plain integers, eliminating the need to include here everything
124 * in "/usr/include".
125 */
126
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100127const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000128#include "errnoent.h"
129};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100130const char *const signalent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200131#include "signalent.h"
132};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100133const struct_ioctlent ioctlent0[] = {
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +0000134#include "ioctlent0.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200135};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000136
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100137#if SUPPORTED_PERSONALITIES > 1
Roland McGrathee36ce12004-09-04 03:53:10 +0000138static const char *const errnoent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100139# include "errnoent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000140};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200141static const char *const signalent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100142# include "signalent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200143};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100144static const struct_ioctlent ioctlent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100145# include "ioctlent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200146};
Elvira Khabirova09294222015-08-04 01:47:02 +0300147# include PERSONALITY0_INCLUDE_PRINTERS_DECLS
148static const struct_printers printers0 = {
149# include PERSONALITY0_INCLUDE_PRINTERS_DEFS
150};
151# include PERSONALITY1_INCLUDE_PRINTERS_DECLS
152static const struct_printers printers1 = {
153# include PERSONALITY1_INCLUDE_PRINTERS_DEFS
154};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200155#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000156
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100157#if SUPPORTED_PERSONALITIES > 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000158static const char *const errnoent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100159# include "errnoent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000160};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200161static const char *const signalent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100162# include "signalent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200163};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100164static const struct_ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100165# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200166};
Elvira Khabirova09294222015-08-04 01:47:02 +0300167# include PERSONALITY2_INCLUDE_PRINTERS_DECLS
168static const struct_printers printers2 = {
169# include PERSONALITY2_INCLUDE_PRINTERS_DEFS
170};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200171#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000172
Dmitry V. Levine6f55242013-02-26 22:02:30 +0000173enum {
174 nsyscalls0 = ARRAY_SIZE(sysent0)
175#if SUPPORTED_PERSONALITIES > 1
176 , nsyscalls1 = ARRAY_SIZE(sysent1)
177# if SUPPORTED_PERSONALITIES > 2
178 , nsyscalls2 = ARRAY_SIZE(sysent2)
179# endif
180#endif
181};
182
183enum {
184 nerrnos0 = ARRAY_SIZE(errnoent0)
185#if SUPPORTED_PERSONALITIES > 1
186 , nerrnos1 = ARRAY_SIZE(errnoent1)
187# if SUPPORTED_PERSONALITIES > 2
188 , nerrnos2 = ARRAY_SIZE(errnoent2)
189# endif
190#endif
191};
192
193enum {
194 nsignals0 = ARRAY_SIZE(signalent0)
195#if SUPPORTED_PERSONALITIES > 1
196 , nsignals1 = ARRAY_SIZE(signalent1)
197# if SUPPORTED_PERSONALITIES > 2
198 , nsignals2 = ARRAY_SIZE(signalent2)
199# endif
200#endif
201};
202
203enum {
204 nioctlents0 = ARRAY_SIZE(ioctlent0)
205#if SUPPORTED_PERSONALITIES > 1
206 , nioctlents1 = ARRAY_SIZE(ioctlent1)
207# if SUPPORTED_PERSONALITIES > 2
208 , nioctlents2 = ARRAY_SIZE(ioctlent2)
209# endif
210#endif
211};
212
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100213#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100214const struct_sysent *sysent = sysent0;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100215const char *const *errnoent = errnoent0;
216const char *const *signalent = signalent0;
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100217const struct_ioctlent *ioctlent = ioctlent0;
Elvira Khabirova09294222015-08-04 01:47:02 +0300218const struct_printers *printers = &printers0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100219#endif
Elvira Khabirova09294222015-08-04 01:47:02 +0300220
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100221unsigned nsyscalls = nsyscalls0;
222unsigned nerrnos = nerrnos0;
223unsigned nsignals = nsignals0;
224unsigned nioctlents = nioctlents0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100225
226unsigned num_quals;
227qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
228
229static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
230 nsyscalls0,
231#if SUPPORTED_PERSONALITIES > 1
232 nsyscalls1,
233#endif
234#if SUPPORTED_PERSONALITIES > 2
235 nsyscalls2,
236#endif
237};
238static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
239 sysent0,
240#if SUPPORTED_PERSONALITIES > 1
241 sysent1,
242#endif
243#if SUPPORTED_PERSONALITIES > 2
244 sysent2,
245#endif
246};
247
248enum {
249 MAX_NSYSCALLS1 = (nsyscalls0
250#if SUPPORTED_PERSONALITIES > 1
251 > nsyscalls1 ? nsyscalls0 : nsyscalls1
252#endif
253 ),
254 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
255#if SUPPORTED_PERSONALITIES > 2
256 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
257#endif
258 ),
259 MAX_NSYSCALLS = MAX_NSYSCALLS2,
260 /* We are ready for arches with up to 255 signals,
261 * even though the largest known signo is on MIPS and it is 128.
262 * The number of existing syscalls on all arches is
263 * larger that 255 anyway, so it is just a pedantic matter.
264 */
265 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
266};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000267
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100268#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100269unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000270
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100271# ifndef current_wordsize
272unsigned current_wordsize;
273static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000274 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000275 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100276# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000277 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100278# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200279};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100280# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000281
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200282void
Dmitry V. Levin3abe8b22006-12-20 22:37:21 +0000283set_personality(int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000284{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100285 nsyscalls = nsyscall_vec[personality];
286 sysent = sysent_vec[personality];
287
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000288 switch (personality) {
289 case 0:
290 errnoent = errnoent0;
291 nerrnos = nerrnos0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000292 ioctlent = ioctlent0;
293 nioctlents = nioctlents0;
294 signalent = signalent0;
295 nsignals = nsignals0;
Elvira Khabirova09294222015-08-04 01:47:02 +0300296 printers = &printers0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000297 break;
298
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000299 case 1:
300 errnoent = errnoent1;
301 nerrnos = nerrnos1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000302 ioctlent = ioctlent1;
303 nioctlents = nioctlents1;
304 signalent = signalent1;
305 nsignals = nsignals1;
Elvira Khabirova09294222015-08-04 01:47:02 +0300306 printers = &printers1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000307 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000308
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100309# if SUPPORTED_PERSONALITIES > 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000310 case 2:
311 errnoent = errnoent2;
312 nerrnos = nerrnos2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000313 ioctlent = ioctlent2;
314 nioctlents = nioctlents2;
315 signalent = signalent2;
316 nsignals = nsignals2;
Elvira Khabirova09294222015-08-04 01:47:02 +0300317 printers = &printers2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000318 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100319# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000320 }
321
322 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100323# ifndef current_wordsize
324 current_wordsize = personality_wordsize[personality];
325# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000326}
327
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000328static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000329update_personality(struct tcb *tcp, unsigned int personality)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000330{
331 if (personality == current_personality)
332 return;
333 set_personality(personality);
334
335 if (personality == tcp->currpers)
336 return;
337 tcp->currpers = personality;
338
Dmitry V. Levin6c8ef052015-05-25 22:51:19 +0000339# undef PERSONALITY_NAMES
340# if defined POWERPC64
341# define PERSONALITY_NAMES {"64 bit", "32 bit"}
342# elif defined X86_64
343# define PERSONALITY_NAMES {"64 bit", "32 bit", "x32"}
344# elif defined X32
345# define PERSONALITY_NAMES {"x32", "32 bit"}
346# elif defined AARCH64
Dmitry V. Levinc37173f2015-12-09 00:43:39 +0000347# define PERSONALITY_NAMES {"64 bit", "32 bit"}
Dmitry V. Levin6c8ef052015-05-25 22:51:19 +0000348# elif defined TILE
349# define PERSONALITY_NAMES {"64-bit", "32-bit"}
350# endif
351# ifdef PERSONALITY_NAMES
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000352 if (!qflag) {
Dmitry V. Levin6c8ef052015-05-25 22:51:19 +0000353 static const char *const names[] = PERSONALITY_NAMES;
354 error_msg("[ Process PID=%d runs in %s mode. ]",
355 tcp->pid, names[personality]);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100356 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100357# endif
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000358}
359#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000360
Denys Vlasenkoc1540fe2013-02-21 16:17:08 +0100361static int qual_syscall(), qual_signal(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000362
Roland McGrathe10e62a2004-09-04 04:20:43 +0000363static const struct qual_options {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000364 unsigned int bitflag;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000365 const char *option_name;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000366 int (*qualify)(const char *, int, int);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000367 const char *argument_name;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000368} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000369 { QUAL_TRACE, "trace", qual_syscall, "system call" },
370 { QUAL_TRACE, "t", qual_syscall, "system call" },
371 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
372 { QUAL_ABBREV, "a", qual_syscall, "system call" },
373 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
374 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
375 { QUAL_RAW, "raw", qual_syscall, "system call" },
376 { QUAL_RAW, "x", qual_syscall, "system call" },
377 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
378 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
379 { QUAL_SIGNAL, "s", qual_signal, "signal" },
Roland McGrath9797ceb2002-12-30 10:23:00 +0000380 { QUAL_READ, "read", qual_desc, "descriptor" },
381 { QUAL_READ, "reads", qual_desc, "descriptor" },
382 { QUAL_READ, "r", qual_desc, "descriptor" },
383 { QUAL_WRITE, "write", qual_desc, "descriptor" },
384 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
385 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000386 { 0, NULL, NULL, NULL },
387};
388
Roland McGrath9797ceb2002-12-30 10:23:00 +0000389static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000390reallocate_qual(const unsigned int n)
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100391{
392 unsigned p;
393 qualbits_t *qp;
394 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
Dmitry V. Levin3e9d71f2015-05-25 20:41:02 +0000395 qp = qual_vec[p] = xreallocarray(qual_vec[p], n,
396 sizeof(qualbits_t));
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100397 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
398 }
399 num_quals = n;
400}
401
402static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000403qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000404{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000405 int p;
Roland McGrath138c6a32006-01-12 09:50:49 +0000406
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100407 if (num_quals <= n)
408 reallocate_qual(n + 1);
Roland McGrath138c6a32006-01-12 09:50:49 +0000409
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100410 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
411 if (pers == p || pers < 0) {
412 if (not)
413 qual_vec[p][n] &= ~bitflag;
414 else
415 qual_vec[p][n] |= bitflag;
416 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000417 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000418}
419
420static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000421qual_syscall(const char *s, const unsigned int bitflag, const int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000422{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000423 int p;
424 unsigned int i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000425 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000426
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100427 if (*s >= '0' && *s <= '9') {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100428 i = string_to_uint(s);
Denys Vlasenkob43dacd2013-02-23 18:19:28 +0100429 if (i >= MAX_NSYSCALLS)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000430 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000431 qualify_one(i, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000432 return 0;
Roland McGrath48a035f2006-01-12 09:45:56 +0000433 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000434
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100435 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
436 for (i = 0; i < nsyscall_vec[p]; i++) {
437 if (sysent_vec[p][i].sys_name
438 && strcmp(s, sysent_vec[p][i].sys_name) == 0
439 ) {
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000440 qualify_one(i, bitflag, not, p);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100441 rc = 0;
442 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000443 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100444 }
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000445
Roland McGrathfe6b3522005-02-02 04:40:11 +0000446 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000447}
448
449static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000450qual_signal(const char *s, const unsigned int bitflag, const int not)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000451{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000452 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000453
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100454 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000455 int signo = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100456 if (signo < 0 || signo > 255)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000457 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000458 qualify_one(signo, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000459 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000460 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000461 if (strncasecmp(s, "SIG", 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000462 s += 3;
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100463 for (i = 0; i <= NSIG; i++) {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000464 if (strcasecmp(s, signame(i) + 3) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000465 qualify_one(i, bitflag, not, -1);
Roland McGrath76421df2005-02-02 03:51:18 +0000466 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000467 }
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100468 }
Roland McGrath76421df2005-02-02 03:51:18 +0000469 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000470}
471
472static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000473qual_desc(const char *s, const unsigned int bitflag, const int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000474{
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100475 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000476 int desc = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100477 if (desc < 0 || desc > 0x7fff) /* paranoia */
Roland McGrathfe6b3522005-02-02 04:40:11 +0000478 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000479 qualify_one(desc, bitflag, not, -1);
Roland McGrath2b619022003-04-10 18:58:20 +0000480 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000481 }
482 return -1;
483}
484
485static int
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000486lookup_class(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000487{
488 if (strcmp(s, "file") == 0)
489 return TRACE_FILE;
490 if (strcmp(s, "ipc") == 0)
491 return TRACE_IPC;
492 if (strcmp(s, "network") == 0)
493 return TRACE_NETWORK;
494 if (strcmp(s, "process") == 0)
495 return TRACE_PROCESS;
496 if (strcmp(s, "signal") == 0)
497 return TRACE_SIGNAL;
Roland McGrath2fe7b132005-07-05 03:25:35 +0000498 if (strcmp(s, "desc") == 0)
499 return TRACE_DESC;
Namhyung Kim96792962012-10-24 11:41:57 +0900500 if (strcmp(s, "memory") == 0)
501 return TRACE_MEMORY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000502 return -1;
503}
504
505void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000506qualify(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000507{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000508 const struct qual_options *opt;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000509 char *copy;
510 const char *p;
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000511 int not;
512 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000513
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100514 if (num_quals == 0)
515 reallocate_qual(MIN_QUALS);
516
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000517 opt = &qual_options[0];
518 for (i = 0; (p = qual_options[i].option_name); i++) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000519 unsigned int len = strlen(p);
520 if (strncmp(s, p, len) == 0 && s[len] == '=') {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000521 opt = &qual_options[i];
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000522 s += len + 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000523 break;
524 }
525 }
526 not = 0;
527 if (*s == '!') {
528 not = 1;
529 s++;
530 }
531 if (strcmp(s, "none") == 0) {
532 not = 1 - not;
533 s = "all";
534 }
535 if (strcmp(s, "all") == 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100536 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000537 qualify_one(i, opt->bitflag, not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000538 }
539 return;
540 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100541 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000542 qualify_one(i, opt->bitflag, !not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000543 }
Dmitry V. Levin3e9d71f2015-05-25 20:41:02 +0000544 copy = xstrdup(s);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000545 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000546 int n;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000547 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100548 unsigned pers;
549 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
550 for (i = 0; i < nsyscall_vec[pers]; i++)
551 if (sysent_vec[pers][i].sys_flags & n)
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000552 qualify_one(i, opt->bitflag, not, pers);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100553 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000554 continue;
555 }
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000556 if (opt->qualify(p, opt->bitflag, not)) {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100557 error_msg_and_die("invalid %s '%s'",
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000558 opt->argument_name, p);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000559 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000560 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000561 free(copy);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000562 return;
563}
564
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000565#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000566static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000567decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000568{
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000569 unsigned long addr;
Dmitry V. Levinc2155692015-03-22 15:52:40 +0000570 unsigned int n;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000571
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000572 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
573 return;
574
575 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100576 tcp->qual_flg = qual_flags[tcp->scno];
577 tcp->s_ent = &sysent[tcp->scno];
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000578 addr = tcp->u_arg[1];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100579 n = tcp->s_ent->nargs;
Dmitry V. Levinc2155692015-03-22 15:52:40 +0000580 if (sizeof(tcp->u_arg[0]) == current_wordsize) {
581 memset(tcp->u_arg, 0, n * sizeof(tcp->u_arg[0]));
582 (void) umoven(tcp, addr, n * sizeof(tcp->u_arg[0]), tcp->u_arg);
583 } else {
584 unsigned int args[n];
585 unsigned int i;
586
587 memset(args, 0, sizeof(args));
588 (void) umove(tcp, addr, &args);
589 for (i = 0; i < n; ++i)
590 tcp->u_arg[i] = args[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000591 }
592}
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000593#endif
Mike Frysinger3362e892012-03-15 01:09:19 -0400594
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000595#ifdef SYS_ipc_subcall
596static void
597decode_ipc_subcall(struct tcb *tcp)
598{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100599 unsigned int i, n;
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000600
601 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
602 return;
603
604 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100605 tcp->qual_flg = qual_flags[tcp->scno];
606 tcp->s_ent = &sysent[tcp->scno];
607 n = tcp->s_ent->nargs;
608 for (i = 0; i < n; i++)
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000609 tcp->u_arg[i] = tcp->u_arg[i + 1];
610}
611#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000612
Dmitry V. Levinf34b97f2015-04-17 09:14:19 +0000613#ifdef LINUX_MIPSO32
614static void
615decode_mips_subcall(struct tcb *tcp)
616{
617 if (!SCNO_IS_VALID(tcp->u_arg[0]))
618 return;
619 tcp->scno = tcp->u_arg[0];
620 tcp->qual_flg = qual_flags[tcp->scno];
621 tcp->s_ent = &sysent[tcp->scno];
622 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
623 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
624 /*
625 * Fetching the last arg of 7-arg syscalls (fadvise64_64
626 * and sync_file_range) would require additional code,
627 * see linux/mips/get_syscall_args.c
628 */
629}
630
631SYS_FUNC(syscall)
632{
633 return printargs(tcp);
634}
635#endif
636
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200637int
638printargs(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000639{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200640 if (entering(tcp)) {
641 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100642 int n = tcp->s_ent->nargs;
643 for (i = 0; i < n; i++)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200644 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
645 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000646 return 0;
647}
648
Denys Vlasenko72879c62012-02-27 14:18:02 +0100649int
650printargs_lu(struct tcb *tcp)
651{
652 if (entering(tcp)) {
653 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100654 int n = tcp->s_ent->nargs;
655 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100656 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
657 }
658 return 0;
659}
660
661int
662printargs_ld(struct tcb *tcp)
663{
664 if (entering(tcp)) {
665 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100666 int n = tcp->s_ent->nargs;
667 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100668 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
669 }
670 return 0;
671}
672
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000673static void
674dumpio(struct tcb *tcp)
675{
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300676 int sen;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000677
678 if (syserror(tcp))
679 return;
680 if ((unsigned long) tcp->u_arg[0] >= num_quals)
681 return;
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300682 sen = tcp->s_ent->sen;
683 if (SEN_printargs == sen)
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000684 return;
685 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300686 switch (sen) {
687 case SEN_read:
688 case SEN_pread:
689 case SEN_recv:
690 case SEN_recvfrom:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000691 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
692 return;
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300693 case SEN_readv:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000694 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
695 return;
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300696 case SEN_recvmsg:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000697 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
698 return;
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300699 case SEN_recvmmsg:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000700 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
701 return;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000702 }
703 }
704 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300705 switch (sen) {
706 case SEN_write:
707 case SEN_pwrite:
708 case SEN_send:
709 case SEN_sendto:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000710 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300711 break;
712 case SEN_writev:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000713 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300714 break;
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300715 case SEN_sendmsg:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000716 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300717 break;
718 case SEN_sendmmsg:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000719 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300720 break;
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300721 }
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000722 }
723}
724
725/*
726 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
727 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
728 */
729#if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
730static long
731shuffle_scno(unsigned long scno)
732{
733 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
734 return scno;
735
736 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
737 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
738 return 0x000ffff0;
739 if (scno == 0x000ffff0)
740 return ARM_FIRST_SHUFFLED_SYSCALL;
741
742#define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
743 /*
744 * Is it ARM specific syscall?
745 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
746 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
747 */
748 if (scno >= 0x000f0000 &&
749 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
750 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
751 }
752 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
753 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
754 }
755
756 return scno;
757}
758#else
759# define shuffle_scno(scno) ((long)(scno))
760#endif
761
Mike Frysingerd2eaf672015-08-18 03:24:59 -0400762const char *
763syscall_name(long scno)
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000764{
765 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
766
Mike Frysingerd2eaf672015-08-18 03:24:59 -0400767 if (SCNO_IS_VALID(scno))
768 return sysent[scno].sys_name;
769 else {
770 sprintf(buf, "syscall_%lu", shuffle_scno(scno));
771 return buf;
772 }
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000773}
774
775static long get_regs_error;
776
777void
778clear_regs(void)
779{
780 get_regs_error = -1;
781}
782
783static int get_syscall_args(struct tcb *);
784static int get_syscall_result(struct tcb *);
Dmitry V. Levin7386ca72015-11-30 13:57:51 +0000785static int arch_get_scno(struct tcb *tcp);
786static void get_error(struct tcb *, const bool);
787#if defined X86_64 || defined POWERPC
788static int getregs_old(pid_t);
789#endif
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000790
791static int
792trace_syscall_entering(struct tcb *tcp)
793{
794 int res, scno_good;
795
796 scno_good = res = get_scno(tcp);
797 if (res == 0)
798 return res;
799 if (res == 1)
800 res = get_syscall_args(tcp);
801
802 if (res != 1) {
803 printleader(tcp);
804 if (scno_good != 1)
805 tprints("????" /* anti-trigraph gap */ "(");
806 else if (tcp->qual_flg & UNDEFINED_SCNO)
Mike Frysingerd2eaf672015-08-18 03:24:59 -0400807 tprintf("%s(", syscall_name(tcp->scno));
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000808 else
809 tprintf("%s(", tcp->s_ent->sys_name);
810 /*
811 * " <unavailable>" will be added later by the code which
812 * detects ptrace errors.
813 */
814 goto ret;
815 }
816
Dmitry V. Levinf34b97f2015-04-17 09:14:19 +0000817#ifdef LINUX_MIPSO32
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300818 if (SEN_syscall == tcp->s_ent->sen)
Dmitry V. Levinf34b97f2015-04-17 09:14:19 +0000819 decode_mips_subcall(tcp);
820#endif
821
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300822 if ( SEN_execve == tcp->s_ent->sen
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000823# if defined(SPARC) || defined(SPARC64)
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300824 || SEN_execv == tcp->s_ent->sen
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000825# endif
826 ) {
827 hide_log_until_execve = 0;
828 }
829
830#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300831 switch (tcp->s_ent->sen) {
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000832# ifdef SYS_socket_subcall
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300833 case SEN_socketcall:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000834 decode_socket_subcall(tcp);
835 break;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000836# endif
837# ifdef SYS_ipc_subcall
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300838 case SEN_ipc:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000839 decode_ipc_subcall(tcp);
840 break;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000841# endif
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000842 }
843#endif
844
845 if (!(tcp->qual_flg & QUAL_TRACE)
846 || (tracing_paths && !pathtrace_match(tcp))
847 ) {
848 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000849 tcp->sys_func_rval = 0;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000850 return 0;
851 }
852
853 tcp->flags &= ~TCB_FILTERED;
854
855 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
856 res = 0;
857 goto ret;
858 }
859
860#ifdef USE_LIBUNWIND
861 if (stack_trace_enabled) {
862 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
863 unwind_capture_stacktrace(tcp);
864 }
865#endif
866
867 printleader(tcp);
868 if (tcp->qual_flg & UNDEFINED_SCNO)
Mike Frysingerd2eaf672015-08-18 03:24:59 -0400869 tprintf("%s(", syscall_name(tcp->scno));
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000870 else
871 tprintf("%s(", tcp->s_ent->sys_name);
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300872 if ((tcp->qual_flg & QUAL_RAW) && SEN_exit != tcp->s_ent->sen)
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000873 res = printargs(tcp);
874 else
875 res = tcp->s_ent->sys_func(tcp);
876
877 fflush(tcp->outf);
878 ret:
879 tcp->flags |= TCB_INSYSCALL;
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000880 tcp->sys_func_rval = res;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000881 /* Measure the entrance time as late as possible to avoid errors. */
882 if (Tflag || cflag)
883 gettimeofday(&tcp->etime, NULL);
884 return res;
885}
886
887static int
888trace_syscall_exiting(struct tcb *tcp)
889{
890 int sys_res;
891 struct timeval tv;
892 int res;
893 long u_error;
894
895 /* Measure the exit time as early as possible to avoid errors. */
896 if (Tflag || cflag)
897 gettimeofday(&tv, NULL);
898
899#ifdef USE_LIBUNWIND
900 if (stack_trace_enabled) {
901 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
902 unwind_cache_invalidate(tcp);
903 }
904#endif
905
906#if SUPPORTED_PERSONALITIES > 1
907 update_personality(tcp, tcp->currpers);
908#endif
909 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Dmitry V. Levin4b80f342015-06-29 11:57:44 +0000910 if (filtered(tcp) || hide_log_until_execve)
911 goto ret;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000912
913 if (cflag) {
914 count_syscall(tcp, &tv);
915 if (cflag == CFLAG_ONLY_STATS) {
916 goto ret;
917 }
918 }
919
920 /* If not in -ff mode, and printing_tcp != tcp,
921 * then the log currently does not end with output
922 * of _our syscall entry_, but with something else.
923 * We need to say which syscall's return is this.
924 *
925 * Forced reprinting via TCB_REPRINT is used only by
926 * "strace -ff -oLOG test/threaded_execve" corner case.
927 * It's the only case when -ff mode needs reprinting.
928 */
929 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
930 tcp->flags &= ~TCB_REPRINT;
931 printleader(tcp);
932 if (tcp->qual_flg & UNDEFINED_SCNO)
Mike Frysingerd2eaf672015-08-18 03:24:59 -0400933 tprintf("<... %s resumed> ", syscall_name(tcp->scno));
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000934 else
935 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
936 }
937 printing_tcp = tcp;
938
939 tcp->s_prev_ent = NULL;
940 if (res != 1) {
941 /* There was error in one of prior ptrace ops */
942 tprints(") ");
943 tabto();
944 tprints("= ? <unavailable>\n");
945 line_ended();
946 tcp->flags &= ~TCB_INSYSCALL;
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000947 tcp->sys_func_rval = 0;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000948 return res;
949 }
950 tcp->s_prev_ent = tcp->s_ent;
951
952 sys_res = 0;
953 if (tcp->qual_flg & QUAL_RAW) {
954 /* sys_res = printargs(tcp); - but it's nop on sysexit */
955 } else {
956 /* FIXME: not_failing_only (IOW, option -z) is broken:
957 * failure of syscall is known only after syscall return.
958 * Thus we end up with something like this on, say, ENOENT:
959 * open("doesnt_exist", O_RDONLY <unfinished ...>
960 * {next syscall decode}
961 * whereas the intended result is that open(...) line
962 * is not shown at all.
963 */
964 if (not_failing_only && tcp->u_error)
965 goto ret; /* ignore failed syscalls */
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000966 if (tcp->sys_func_rval & RVAL_DECODED)
967 sys_res = tcp->sys_func_rval;
968 else
969 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000970 }
971
972 tprints(") ");
973 tabto();
974 u_error = tcp->u_error;
975 if (tcp->qual_flg & QUAL_RAW) {
976 if (u_error)
977 tprintf("= -1 (errno %ld)", u_error);
978 else
979 tprintf("= %#lx", tcp->u_rval);
980 }
981 else if (!(sys_res & RVAL_NONE) && u_error) {
982 switch (u_error) {
983 /* Blocked signals do not interrupt any syscalls.
984 * In this case syscalls don't return ERESTARTfoo codes.
985 *
986 * Deadly signals set to SIG_DFL interrupt syscalls
987 * and kill the process regardless of which of the codes below
988 * is returned by the interrupted syscall.
989 * In some cases, kernel forces a kernel-generated deadly
990 * signal to be unblocked and set to SIG_DFL (and thus cause
991 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
992 * or SIGILL. (The alternative is to leave process spinning
993 * forever on the faulty instruction - not useful).
994 *
995 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
996 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
997 * but kernel will always restart them.
998 */
999 case ERESTARTSYS:
1000 /* Most common type of signal-interrupted syscall exit code.
1001 * The system call will be restarted with the same arguments
1002 * if SA_RESTART is set; otherwise, it will fail with EINTR.
1003 */
1004 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
1005 break;
1006 case ERESTARTNOINTR:
1007 /* Rare. For example, fork() returns this if interrupted.
1008 * SA_RESTART is ignored (assumed set): the restart is unconditional.
1009 */
1010 tprints("= ? ERESTARTNOINTR (To be restarted)");
1011 break;
1012 case ERESTARTNOHAND:
1013 /* pause(), rt_sigsuspend() etc use this code.
1014 * SA_RESTART is ignored (assumed not set):
1015 * syscall won't restart (will return EINTR instead)
1016 * even after signal with SA_RESTART set. However,
1017 * after SIG_IGN or SIG_DFL signal it will restart
1018 * (thus the name "restart only if has no handler").
1019 */
1020 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
1021 break;
1022 case ERESTART_RESTARTBLOCK:
1023 /* Syscalls like nanosleep(), poll() which can't be
1024 * restarted with their original arguments use this
1025 * code. Kernel will execute restart_syscall() instead,
1026 * which changes arguments before restarting syscall.
1027 * SA_RESTART is ignored (assumed not set) similarly
1028 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
1029 * since restart data is saved in "restart block"
1030 * in task struct, and if signal handler uses a syscall
1031 * which in turn saves another such restart block,
1032 * old data is lost and restart becomes impossible)
1033 */
1034 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
1035 break;
1036 default:
1037 if ((unsigned long) u_error < nerrnos
1038 && errnoent[u_error])
1039 tprintf("= -1 %s (%s)", errnoent[u_error],
1040 strerror(u_error));
1041 else
1042 tprintf("= -1 ERRNO_%lu (%s)", u_error,
1043 strerror(u_error));
1044 break;
1045 }
1046 if ((sys_res & RVAL_STR) && tcp->auxstr)
1047 tprintf(" (%s)", tcp->auxstr);
1048 }
1049 else {
1050 if (sys_res & RVAL_NONE)
1051 tprints("= ?");
1052 else {
1053 switch (sys_res & RVAL_MASK) {
1054 case RVAL_HEX:
1055#if SUPPORTED_PERSONALITIES > 1
1056 if (current_wordsize < sizeof(long))
1057 tprintf("= %#x",
1058 (unsigned int) tcp->u_rval);
1059 else
1060#endif
1061 tprintf("= %#lx", tcp->u_rval);
1062 break;
1063 case RVAL_OCTAL:
1064 tprintf("= %#lo", tcp->u_rval);
1065 break;
1066 case RVAL_UDECIMAL:
Dmitry V. Levinbe1cb922016-01-07 14:20:17 +00001067#if SUPPORTED_PERSONALITIES > 1
1068 if (current_wordsize < sizeof(long))
1069 tprintf("= %u",
1070 (unsigned int) tcp->u_rval);
1071 else
1072#endif
1073 tprintf("= %lu", tcp->u_rval);
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001074 break;
1075 case RVAL_DECIMAL:
1076 tprintf("= %ld", tcp->u_rval);
1077 break;
1078 case RVAL_FD:
1079 if (show_fd_path) {
1080 tprints("= ");
1081 printfd(tcp, tcp->u_rval);
1082 }
1083 else
1084 tprintf("= %ld", tcp->u_rval);
1085 break;
1086#if defined(LINUX_MIPSN32) || defined(X32)
1087 /*
1088 case RVAL_LHEX:
1089 tprintf("= %#llx", tcp->u_lrval);
1090 break;
1091 case RVAL_LOCTAL:
1092 tprintf("= %#llo", tcp->u_lrval);
1093 break;
1094 */
1095 case RVAL_LUDECIMAL:
1096 tprintf("= %llu", tcp->u_lrval);
1097 break;
1098 /*
1099 case RVAL_LDECIMAL:
1100 tprintf("= %lld", tcp->u_lrval);
1101 break;
1102 */
1103#endif
1104 default:
Dmitry V. Levin6c8ef052015-05-25 22:51:19 +00001105 error_msg("invalid rval format");
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001106 break;
1107 }
1108 }
1109 if ((sys_res & RVAL_STR) && tcp->auxstr)
1110 tprintf(" (%s)", tcp->auxstr);
1111 }
1112 if (Tflag) {
1113 tv_sub(&tv, &tv, &tcp->etime);
1114 tprintf(" <%ld.%06ld>",
1115 (long) tv.tv_sec, (long) tv.tv_usec);
1116 }
1117 tprints("\n");
1118 dumpio(tcp);
1119 line_ended();
1120
1121#ifdef USE_LIBUNWIND
1122 if (stack_trace_enabled)
1123 unwind_print_stacktrace(tcp);
1124#endif
1125
1126 ret:
1127 tcp->flags &= ~TCB_INSYSCALL;
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +00001128 tcp->sys_func_rval = 0;
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001129 return 0;
1130}
1131
1132int
1133trace_syscall(struct tcb *tcp)
1134{
1135 return exiting(tcp) ?
1136 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
1137}
1138
Dmitry V. Levin9af94a22015-09-18 01:54:59 +00001139bool
1140is_erestart(struct tcb *tcp)
1141{
1142 switch (tcp->u_error) {
1143 case ERESTARTSYS:
1144 case ERESTARTNOINTR:
1145 case ERESTARTNOHAND:
1146 case ERESTART_RESTARTBLOCK:
1147 return true;
1148 default:
1149 return false;
1150 }
1151}
1152
Dmitry V. Levin3858b932015-09-18 01:54:59 +00001153static int saved_u_error;
1154
1155void
1156temporarily_clear_syserror(struct tcb *tcp)
1157{
1158 saved_u_error = tcp->u_error;
1159 tcp->u_error = 0;
1160}
1161
1162void
1163restore_cleared_syserror(struct tcb *tcp)
1164{
1165 tcp->u_error = saved_u_error;
1166}
1167
Dmitry V. Levinbdb07e32015-11-27 01:51:22 +00001168#include "kernel_types.h"
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001169
1170/*
1171 * Check the syscall return value register value for whether it is
1172 * a negated errno code indicating an error, or a success return value.
1173 */
1174static inline bool
1175is_negated_errno(kernel_ulong_t val)
1176{
1177 /* Linux kernel defines MAX_ERRNO to 4095. */
1178 kernel_ulong_t max = -(kernel_long_t) 4095;
1179
1180#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1181 if (current_wordsize < sizeof(val)) {
1182 val = (uint32_t) val;
1183 max = (uint32_t) max;
1184 }
1185#elif defined X32
1186 /*
1187 * current_wordsize is 4 even in personality 0 (native X32)
1188 * but truncation _must not_ be done in it.
1189 * can't check current_wordsize here!
1190 */
1191 if (current_personality != 0) {
1192 val = (uint32_t) val;
1193 max = (uint32_t) max;
1194 }
1195#endif
1196
1197 return val >= max;
1198}
1199
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001200#include "arch_regs.c"
Wichert Akkermanc7926982000-04-10 22:22:31 +00001201
Dmitry V. Levin78ed3f32015-03-23 00:04:27 +00001202#ifdef HAVE_GETRVAL2
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001203# include "arch_getrval2.c"
Dmitry V. Levin48f08902015-03-05 23:30:02 +00001204#endif
1205
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001206void
Denys Vlasenko5a2483b2013-07-01 12:49:14 +02001207print_pc(struct tcb *tcp)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001208{
Dmitry V. Levin5105d4a2015-11-30 03:30:51 +00001209#if defined ARCH_PC_REG
1210# define ARCH_GET_PC 0
1211#elif defined ARCH_PC_PEEK_ADDR
1212 long pc;
1213# define ARCH_PC_REG pc
1214# define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
Dmitry V. Levinb2d9ff22015-02-23 13:43:20 +00001215#else
Dmitry V. Levin5105d4a2015-11-30 03:30:51 +00001216# error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001217#endif
Dmitry V. Levin5105d4a2015-11-30 03:30:51 +00001218 if (get_regs_error || ARCH_GET_PC)
1219 tprints(current_wordsize == 4 ? "[????????] "
1220 : "[????????????????] ");
1221 else
1222 tprintf(current_wordsize == 4 ? "[%08lx] " : "[%016lx] ",
1223 (unsigned long) ARCH_PC_REG);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001224}
1225
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001226#if defined ARCH_REGS_FOR_GETREGSET
1227static long
1228get_regset(pid_t pid)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001229{
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001230# ifdef ARCH_IOVEC_FOR_GETREGSET
1231 /* variable iovec */
1232 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1233 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1234 &ARCH_IOVEC_FOR_GETREGSET);
1235# else
1236 /* constant iovec */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001237 static struct iovec io = {
1238 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1239 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001240 };
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001241 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001242
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001243# endif
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001244}
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001245#endif /* ARCH_REGS_FOR_GETREGSET */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001246
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001247void
1248get_regs(pid_t pid)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001249{
Dmitry V. Levinf51aec62015-11-30 00:01:01 +00001250#undef USE_GET_SYSCALL_RESULT_REGS
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001251#ifdef ARCH_REGS_FOR_GETREGSET
1252# ifdef X86_64
1253 /* Try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS. */
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001254 static int getregset_support;
1255
1256 if (getregset_support >= 0) {
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001257 get_regs_error = get_regset(pid);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001258 if (getregset_support > 0)
1259 return;
1260 if (get_regs_error >= 0) {
1261 getregset_support = 1;
1262 return;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001263 }
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001264 if (errno == EPERM || errno == ESRCH)
1265 return;
1266 getregset_support = -1;
1267 }
Dmitry V. Levinf3e9e612015-11-30 15:30:05 +00001268 get_regs_error = getregs_old(pid);
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001269# else /* !X86_64 */
1270 /* Assume that PTRACE_GETREGSET works. */
1271 get_regs_error = get_regset(pid);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001272# endif
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001273#elif defined ARCH_REGS_FOR_GETREGS
1274# if defined SPARC || defined SPARC64
1275 /* SPARC systems have the meaning of data and addr reversed */
1276 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&ARCH_REGS_FOR_GETREGS, 0);
1277# elif defined POWERPC
1278 static bool old_kernel = 0;
1279 if (old_kernel)
1280 goto old;
1281 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1282 if (get_regs_error && errno == EIO) {
1283 old_kernel = 1;
1284 old:
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001285 get_regs_error = getregs_old(pid);
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001286 }
1287# else
1288 /* Assume that PTRACE_GETREGS works. */
1289 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1290# endif
1291
1292#else /* !ARCH_REGS_FOR_GETREGSET && !ARCH_REGS_FOR_GETREGS */
Dmitry V. Levinf51aec62015-11-30 00:01:01 +00001293# define USE_GET_SYSCALL_RESULT_REGS 1
1294# warning get_regs is not implemented for this architecture yet
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001295 get_regs_error = 0;
1296#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001297}
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001298
Dmitry V. Levin16510512015-11-30 01:46:52 +00001299/*
1300 * Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001301 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1302 * 1: ok, continue in trace_syscall_entering().
1303 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001304 * ("????" etc) and bail out.
1305 */
Denys Vlasenko8497b622015-03-21 17:51:52 +01001306int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001307get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001308{
Dmitry V. Levin144cda22015-03-23 18:48:32 +00001309 if (get_regs_error)
1310 return -1;
1311
Dmitry V. Levin16510512015-11-30 01:46:52 +00001312 int rc = arch_get_scno(tcp);
1313 if (rc != 1)
1314 return rc;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001315
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001316 if (SCNO_IS_VALID(tcp->scno)) {
Dmitry V. Levin16510512015-11-30 01:46:52 +00001317 tcp->s_ent = &sysent[tcp->scno];
1318 tcp->qual_flg = qual_flags[tcp->scno];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001319 } else {
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01001320 static const struct_sysent unknown = {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001321 .nargs = MAX_ARGS,
1322 .sys_flags = 0,
1323 .sys_func = printargs,
Denys Vlasenkocc59f142015-04-07 12:46:59 +02001324 .sys_name = "system call",
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001325 };
1326 tcp->s_ent = &unknown;
1327 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
Dmitry V. Levinea009002015-03-24 01:59:07 +00001328 if (debug_flag)
Dmitry V. Levin16510512015-11-30 01:46:52 +00001329 error_msg("pid %d invalid syscall %ld", tcp->pid, tcp->scno);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001330 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001331 return 1;
1332}
1333
Dmitry V. Levinf51aec62015-11-30 00:01:01 +00001334#ifdef USE_GET_SYSCALL_RESULT_REGS
Dmitry V. Levin7386ca72015-11-30 13:57:51 +00001335static int get_syscall_result_regs(struct tcb *);
Dmitry V. Levinf51aec62015-11-30 00:01:01 +00001336#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001337
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001338/* Returns:
1339 * 1: ok, continue in trace_syscall_exiting().
1340 * -1: error, trace_syscall_exiting() should print error indicator
1341 * ("????" etc) and bail out.
1342 */
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001343static int
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001344get_syscall_result(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001345{
Dmitry V. Levinf51aec62015-11-30 00:01:01 +00001346#ifdef USE_GET_SYSCALL_RESULT_REGS
1347 if (get_syscall_result_regs(tcp))
1348 return -1;
Masatake YAMATOed69fc22014-04-16 15:33:35 +09001349#endif
Dmitry V. Levin0c8c5c92015-11-29 00:06:45 +00001350 tcp->u_error = 0;
1351 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1352
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001353 return 1;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00001354}
Dmitry V. Levin7386ca72015-11-30 13:57:51 +00001355
1356#include "get_scno.c"
1357#include "get_syscall_args.c"
1358#ifdef USE_GET_SYSCALL_RESULT_REGS
1359# include "get_syscall_result.c"
1360#endif
1361#include "get_error.c"
1362#if defined X86_64 || defined POWERPC
1363# include "getregs_old.c"
1364#endif