blob: 396a7dd7d992d5f23f05040983143d43805b8912 [file] [log] [blame]
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001/*
2 * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3 * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4 * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00005 * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6 * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7 * Linux for s390 port by D.J. Barrow
8 * <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00009 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000032 */
33
34#include "defs.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000035#include <sys/param.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000036
Dmitry V. Levin5503dd22015-02-13 02:12:14 +000037/* for struct iovec */
38#include <sys/uio.h>
Maarten ter Huurne40c174b2014-10-20 01:02:48 +020039
Dmitry V. Levin7211dbc2015-02-28 12:20:21 +000040#include "regs.h"
Dmitry V. Levinfadf3792015-02-13 00:26:38 +000041#include "ptrace.h"
Wichert Akkerman15dea971999-10-06 13:06:34 +000042
Denys Vlasenko84703742012-02-25 02:38:52 +010043#if defined(SPARC64)
Roland McGrath6d1a65c2004-07-12 07:44:08 +000044# undef PTRACE_GETREGS
45# define PTRACE_GETREGS PTRACE_GETREGS64
46# undef PTRACE_SETREGS
47# define PTRACE_SETREGS PTRACE_SETREGS64
Denys Vlasenko84703742012-02-25 02:38:52 +010048#endif
Roland McGrath6d1a65c2004-07-12 07:44:08 +000049
Dmitry V. Levin5503dd22015-02-13 02:12:14 +000050#if defined SPARC64
51# include <asm/psrcompat.h>
52#elif defined SPARC
53# include <asm/psr.h>
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010054#endif
55
Dmitry V. Levin48f08902015-03-05 23:30:02 +000056#ifdef IA64
57# include <asm/rse.h>
58#endif
59
Dmitry V. Levin4c3f2ae2015-02-13 22:53:00 +000060#ifndef NT_PRSTATUS
61# define NT_PRSTATUS 1
62#endif
63
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000064#ifndef NSIG
Denys Vlasenko523635f2012-02-25 02:44:25 +010065# warning: NSIG is not defined, using 32
66# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000067#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000068
69#include "syscall.h"
70
71/* Define these shorthand notations to simplify the syscallent files. */
Roland McGrath2fe7b132005-07-05 03:25:35 +000072#define TD TRACE_DESC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000073#define TF TRACE_FILE
74#define TI TRACE_IPC
75#define TN TRACE_NETWORK
76#define TP TRACE_PROCESS
77#define TS TRACE_SIGNAL
Namhyung Kim96792962012-10-24 11:41:57 +090078#define TM TRACE_MEMORY
Dmitry V. Levin50a218d2011-01-18 17:36:20 +000079#define NF SYSCALL_NEVER_FAILS
Denys Vlasenkoac1ce772011-08-23 13:24:17 +020080#define MA MAX_ARGS
Masatake YAMATO1d78d222014-04-16 15:33:11 +090081#define SI STACKTRACE_INVALIDATE_CACHE
82#define SE STACKTRACE_CAPTURE_ON_ENTER
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000083
Elvira Khabirova28e32df2015-07-10 22:24:54 +030084#define SEN_NAME(syscall_name) SEN_ ## syscall_name
85#define SEN(syscall_name) SEN_NAME(syscall_name), SYS_FUNC_NAME(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
92static const struct_sysent sysent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +010093# include "syscallent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000094};
Denys Vlasenko39fca622011-08-20 02:12:33 +020095#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000096
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010097#if SUPPORTED_PERSONALITIES > 2
98static const struct_sysent sysent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +010099# include "syscallent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000100};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200101#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000102
103/* Now undef them since short defines cause wicked namespace pollution. */
Elvira Khabirova140ecf82015-07-10 22:24:48 +0300104#undef SEN
Elvira Khabirova28e32df2015-07-10 22:24:54 +0300105#undef SEN_NAME
Roland McGrath2fe7b132005-07-05 03:25:35 +0000106#undef TD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000107#undef TF
108#undef TI
109#undef TN
110#undef TP
111#undef TS
Namhyung Kim96792962012-10-24 11:41:57 +0900112#undef TM
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000113#undef NF
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200114#undef MA
Masatake YAMATO1d78d222014-04-16 15:33:11 +0900115#undef SI
116#undef SE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000117
Denys Vlasenko39fca622011-08-20 02:12:33 +0200118/*
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +0000119 * `ioctlent[012].h' files are automatically generated by the auxiliary
Denys Vlasenko39fca622011-08-20 02:12:33 +0200120 * program `ioctlsort', such that the list is sorted by the `code' field.
121 * This has the side-effect of resolving the _IO.. macros into
122 * plain integers, eliminating the need to include here everything
123 * in "/usr/include".
124 */
125
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100126const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000127#include "errnoent.h"
128};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100129const char *const signalent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200130#include "signalent.h"
131};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100132const struct_ioctlent ioctlent0[] = {
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +0000133#include "ioctlent0.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200134};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000135
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100136#if SUPPORTED_PERSONALITIES > 1
Roland McGrathee36ce12004-09-04 03:53:10 +0000137static const char *const errnoent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100138# include "errnoent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000139};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200140static const char *const signalent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100141# include "signalent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200142};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100143static const struct_ioctlent ioctlent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100144# include "ioctlent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200145};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200146#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000147
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100148#if SUPPORTED_PERSONALITIES > 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000149static const char *const errnoent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100150# include "errnoent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000151};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200152static const char *const signalent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100153# include "signalent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200154};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100155static const struct_ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100156# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200157};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200158#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000159
Dmitry V. Levine6f55242013-02-26 22:02:30 +0000160enum {
161 nsyscalls0 = ARRAY_SIZE(sysent0)
162#if SUPPORTED_PERSONALITIES > 1
163 , nsyscalls1 = ARRAY_SIZE(sysent1)
164# if SUPPORTED_PERSONALITIES > 2
165 , nsyscalls2 = ARRAY_SIZE(sysent2)
166# endif
167#endif
168};
169
170enum {
171 nerrnos0 = ARRAY_SIZE(errnoent0)
172#if SUPPORTED_PERSONALITIES > 1
173 , nerrnos1 = ARRAY_SIZE(errnoent1)
174# if SUPPORTED_PERSONALITIES > 2
175 , nerrnos2 = ARRAY_SIZE(errnoent2)
176# endif
177#endif
178};
179
180enum {
181 nsignals0 = ARRAY_SIZE(signalent0)
182#if SUPPORTED_PERSONALITIES > 1
183 , nsignals1 = ARRAY_SIZE(signalent1)
184# if SUPPORTED_PERSONALITIES > 2
185 , nsignals2 = ARRAY_SIZE(signalent2)
186# endif
187#endif
188};
189
190enum {
191 nioctlents0 = ARRAY_SIZE(ioctlent0)
192#if SUPPORTED_PERSONALITIES > 1
193 , nioctlents1 = ARRAY_SIZE(ioctlent1)
194# if SUPPORTED_PERSONALITIES > 2
195 , nioctlents2 = ARRAY_SIZE(ioctlent2)
196# endif
197#endif
198};
199
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100200#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100201const struct_sysent *sysent = sysent0;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100202const char *const *errnoent = errnoent0;
203const char *const *signalent = signalent0;
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100204const struct_ioctlent *ioctlent = ioctlent0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100205#endif
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100206unsigned nsyscalls = nsyscalls0;
207unsigned nerrnos = nerrnos0;
208unsigned nsignals = nsignals0;
209unsigned nioctlents = nioctlents0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100210
211unsigned num_quals;
212qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
213
214static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
215 nsyscalls0,
216#if SUPPORTED_PERSONALITIES > 1
217 nsyscalls1,
218#endif
219#if SUPPORTED_PERSONALITIES > 2
220 nsyscalls2,
221#endif
222};
223static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
224 sysent0,
225#if SUPPORTED_PERSONALITIES > 1
226 sysent1,
227#endif
228#if SUPPORTED_PERSONALITIES > 2
229 sysent2,
230#endif
231};
232
233enum {
234 MAX_NSYSCALLS1 = (nsyscalls0
235#if SUPPORTED_PERSONALITIES > 1
236 > nsyscalls1 ? nsyscalls0 : nsyscalls1
237#endif
238 ),
239 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
240#if SUPPORTED_PERSONALITIES > 2
241 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
242#endif
243 ),
244 MAX_NSYSCALLS = MAX_NSYSCALLS2,
245 /* We are ready for arches with up to 255 signals,
246 * even though the largest known signo is on MIPS and it is 128.
247 * The number of existing syscalls on all arches is
248 * larger that 255 anyway, so it is just a pedantic matter.
249 */
250 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
251};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000252
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100253#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100254unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000255
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100256# ifndef current_wordsize
257unsigned current_wordsize;
258static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000259 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000260 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100261# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000262 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100263# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200264};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100265# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000266
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200267void
Dmitry V. Levin3abe8b22006-12-20 22:37:21 +0000268set_personality(int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000269{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100270 nsyscalls = nsyscall_vec[personality];
271 sysent = sysent_vec[personality];
272
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000273 switch (personality) {
274 case 0:
275 errnoent = errnoent0;
276 nerrnos = nerrnos0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000277 ioctlent = ioctlent0;
278 nioctlents = nioctlents0;
279 signalent = signalent0;
280 nsignals = nsignals0;
281 break;
282
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000283 case 1:
284 errnoent = errnoent1;
285 nerrnos = nerrnos1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000286 ioctlent = ioctlent1;
287 nioctlents = nioctlents1;
288 signalent = signalent1;
289 nsignals = nsignals1;
290 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000291
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100292# if SUPPORTED_PERSONALITIES > 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000293 case 2:
294 errnoent = errnoent2;
295 nerrnos = nerrnos2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000296 ioctlent = ioctlent2;
297 nioctlents = nioctlents2;
298 signalent = signalent2;
299 nsignals = nsignals2;
300 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100301# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000302 }
303
304 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100305# ifndef current_wordsize
306 current_wordsize = personality_wordsize[personality];
307# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000308}
309
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000310static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000311update_personality(struct tcb *tcp, unsigned int personality)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000312{
313 if (personality == current_personality)
314 return;
315 set_personality(personality);
316
317 if (personality == tcp->currpers)
318 return;
319 tcp->currpers = personality;
320
Dmitry V. Levin6c8ef052015-05-25 22:51:19 +0000321# undef PERSONALITY_NAMES
322# if defined POWERPC64
323# define PERSONALITY_NAMES {"64 bit", "32 bit"}
324# elif defined X86_64
325# define PERSONALITY_NAMES {"64 bit", "32 bit", "x32"}
326# elif defined X32
327# define PERSONALITY_NAMES {"x32", "32 bit"}
328# elif defined AARCH64
329# define PERSONALITY_NAMES {"32-bit", "AArch64"}
330# elif defined TILE
331# define PERSONALITY_NAMES {"64-bit", "32-bit"}
332# endif
333# ifdef PERSONALITY_NAMES
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000334 if (!qflag) {
Dmitry V. Levin6c8ef052015-05-25 22:51:19 +0000335 static const char *const names[] = PERSONALITY_NAMES;
336 error_msg("[ Process PID=%d runs in %s mode. ]",
337 tcp->pid, names[personality]);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100338 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100339# endif
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000340}
341#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000342
Denys Vlasenkoc1540fe2013-02-21 16:17:08 +0100343static int qual_syscall(), qual_signal(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000344
Roland McGrathe10e62a2004-09-04 04:20:43 +0000345static const struct qual_options {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000346 unsigned int bitflag;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000347 const char *option_name;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000348 int (*qualify)(const char *, int, int);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000349 const char *argument_name;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000350} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000351 { QUAL_TRACE, "trace", qual_syscall, "system call" },
352 { QUAL_TRACE, "t", qual_syscall, "system call" },
353 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
354 { QUAL_ABBREV, "a", qual_syscall, "system call" },
355 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
356 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
357 { QUAL_RAW, "raw", qual_syscall, "system call" },
358 { QUAL_RAW, "x", qual_syscall, "system call" },
359 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
360 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
361 { QUAL_SIGNAL, "s", qual_signal, "signal" },
Roland McGrath9797ceb2002-12-30 10:23:00 +0000362 { QUAL_READ, "read", qual_desc, "descriptor" },
363 { QUAL_READ, "reads", qual_desc, "descriptor" },
364 { QUAL_READ, "r", qual_desc, "descriptor" },
365 { QUAL_WRITE, "write", qual_desc, "descriptor" },
366 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
367 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000368 { 0, NULL, NULL, NULL },
369};
370
Roland McGrath9797ceb2002-12-30 10:23:00 +0000371static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000372reallocate_qual(const unsigned int n)
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100373{
374 unsigned p;
375 qualbits_t *qp;
376 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
Dmitry V. Levin3e9d71f2015-05-25 20:41:02 +0000377 qp = qual_vec[p] = xreallocarray(qual_vec[p], n,
378 sizeof(qualbits_t));
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100379 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
380 }
381 num_quals = n;
382}
383
384static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000385qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000386{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000387 int p;
Roland McGrath138c6a32006-01-12 09:50:49 +0000388
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100389 if (num_quals <= n)
390 reallocate_qual(n + 1);
Roland McGrath138c6a32006-01-12 09:50:49 +0000391
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100392 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
393 if (pers == p || pers < 0) {
394 if (not)
395 qual_vec[p][n] &= ~bitflag;
396 else
397 qual_vec[p][n] |= bitflag;
398 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000399 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000400}
401
402static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000403qual_syscall(const char *s, const unsigned int bitflag, const int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000404{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000405 int p;
406 unsigned int i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000407 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000408
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100409 if (*s >= '0' && *s <= '9') {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100410 i = string_to_uint(s);
Denys Vlasenkob43dacd2013-02-23 18:19:28 +0100411 if (i >= MAX_NSYSCALLS)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000412 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000413 qualify_one(i, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000414 return 0;
Roland McGrath48a035f2006-01-12 09:45:56 +0000415 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000416
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100417 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
418 for (i = 0; i < nsyscall_vec[p]; i++) {
419 if (sysent_vec[p][i].sys_name
420 && strcmp(s, sysent_vec[p][i].sys_name) == 0
421 ) {
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000422 qualify_one(i, bitflag, not, p);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100423 rc = 0;
424 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000425 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100426 }
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000427
Roland McGrathfe6b3522005-02-02 04:40:11 +0000428 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000429}
430
431static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000432qual_signal(const char *s, const unsigned int bitflag, const int not)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000433{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000434 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000435
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100436 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000437 int signo = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100438 if (signo < 0 || signo > 255)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000439 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000440 qualify_one(signo, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000441 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000442 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000443 if (strncasecmp(s, "SIG", 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000444 s += 3;
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100445 for (i = 0; i <= NSIG; i++) {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000446 if (strcasecmp(s, signame(i) + 3) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000447 qualify_one(i, bitflag, not, -1);
Roland McGrath76421df2005-02-02 03:51:18 +0000448 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000449 }
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100450 }
Roland McGrath76421df2005-02-02 03:51:18 +0000451 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000452}
453
454static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000455qual_desc(const char *s, const unsigned int bitflag, const int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000456{
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100457 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000458 int desc = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100459 if (desc < 0 || desc > 0x7fff) /* paranoia */
Roland McGrathfe6b3522005-02-02 04:40:11 +0000460 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000461 qualify_one(desc, bitflag, not, -1);
Roland McGrath2b619022003-04-10 18:58:20 +0000462 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000463 }
464 return -1;
465}
466
467static int
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000468lookup_class(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000469{
470 if (strcmp(s, "file") == 0)
471 return TRACE_FILE;
472 if (strcmp(s, "ipc") == 0)
473 return TRACE_IPC;
474 if (strcmp(s, "network") == 0)
475 return TRACE_NETWORK;
476 if (strcmp(s, "process") == 0)
477 return TRACE_PROCESS;
478 if (strcmp(s, "signal") == 0)
479 return TRACE_SIGNAL;
Roland McGrath2fe7b132005-07-05 03:25:35 +0000480 if (strcmp(s, "desc") == 0)
481 return TRACE_DESC;
Namhyung Kim96792962012-10-24 11:41:57 +0900482 if (strcmp(s, "memory") == 0)
483 return TRACE_MEMORY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000484 return -1;
485}
486
487void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000488qualify(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000489{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000490 const struct qual_options *opt;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000491 char *copy;
492 const char *p;
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000493 int not;
494 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000495
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100496 if (num_quals == 0)
497 reallocate_qual(MIN_QUALS);
498
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000499 opt = &qual_options[0];
500 for (i = 0; (p = qual_options[i].option_name); i++) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000501 unsigned int len = strlen(p);
502 if (strncmp(s, p, len) == 0 && s[len] == '=') {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000503 opt = &qual_options[i];
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000504 s += len + 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000505 break;
506 }
507 }
508 not = 0;
509 if (*s == '!') {
510 not = 1;
511 s++;
512 }
513 if (strcmp(s, "none") == 0) {
514 not = 1 - not;
515 s = "all";
516 }
517 if (strcmp(s, "all") == 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100518 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000519 qualify_one(i, opt->bitflag, not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000520 }
521 return;
522 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100523 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000524 qualify_one(i, opt->bitflag, !not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000525 }
Dmitry V. Levin3e9d71f2015-05-25 20:41:02 +0000526 copy = xstrdup(s);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000527 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000528 int n;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000529 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100530 unsigned pers;
531 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
532 for (i = 0; i < nsyscall_vec[pers]; i++)
533 if (sysent_vec[pers][i].sys_flags & n)
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000534 qualify_one(i, opt->bitflag, not, pers);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100535 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000536 continue;
537 }
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000538 if (opt->qualify(p, opt->bitflag, not)) {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100539 error_msg_and_die("invalid %s '%s'",
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000540 opt->argument_name, p);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000541 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000542 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000543 free(copy);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000544 return;
545}
546
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000547#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000548static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000549decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000550{
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000551 unsigned long addr;
Dmitry V. Levinc2155692015-03-22 15:52:40 +0000552 unsigned int n;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000553
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000554 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
555 return;
556
557 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100558 tcp->qual_flg = qual_flags[tcp->scno];
559 tcp->s_ent = &sysent[tcp->scno];
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000560 addr = tcp->u_arg[1];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100561 n = tcp->s_ent->nargs;
Dmitry V. Levinc2155692015-03-22 15:52:40 +0000562 if (sizeof(tcp->u_arg[0]) == current_wordsize) {
563 memset(tcp->u_arg, 0, n * sizeof(tcp->u_arg[0]));
564 (void) umoven(tcp, addr, n * sizeof(tcp->u_arg[0]), tcp->u_arg);
565 } else {
566 unsigned int args[n];
567 unsigned int i;
568
569 memset(args, 0, sizeof(args));
570 (void) umove(tcp, addr, &args);
571 for (i = 0; i < n; ++i)
572 tcp->u_arg[i] = args[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000573 }
574}
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000575#endif
Mike Frysinger3362e892012-03-15 01:09:19 -0400576
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000577#ifdef SYS_ipc_subcall
578static void
579decode_ipc_subcall(struct tcb *tcp)
580{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100581 unsigned int i, n;
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000582
583 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
584 return;
585
586 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100587 tcp->qual_flg = qual_flags[tcp->scno];
588 tcp->s_ent = &sysent[tcp->scno];
589 n = tcp->s_ent->nargs;
590 for (i = 0; i < n; i++)
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000591 tcp->u_arg[i] = tcp->u_arg[i + 1];
592}
593#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000594
Dmitry V. Levinf34b97f2015-04-17 09:14:19 +0000595#ifdef LINUX_MIPSO32
596static void
597decode_mips_subcall(struct tcb *tcp)
598{
599 if (!SCNO_IS_VALID(tcp->u_arg[0]))
600 return;
601 tcp->scno = tcp->u_arg[0];
602 tcp->qual_flg = qual_flags[tcp->scno];
603 tcp->s_ent = &sysent[tcp->scno];
604 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
605 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
606 /*
607 * Fetching the last arg of 7-arg syscalls (fadvise64_64
608 * and sync_file_range) would require additional code,
609 * see linux/mips/get_syscall_args.c
610 */
611}
612
613SYS_FUNC(syscall)
614{
615 return printargs(tcp);
616}
617#endif
618
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200619int
620printargs(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000621{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200622 if (entering(tcp)) {
623 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100624 int n = tcp->s_ent->nargs;
625 for (i = 0; i < n; i++)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200626 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
627 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000628 return 0;
629}
630
Denys Vlasenko72879c62012-02-27 14:18:02 +0100631int
632printargs_lu(struct tcb *tcp)
633{
634 if (entering(tcp)) {
635 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100636 int n = tcp->s_ent->nargs;
637 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100638 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
639 }
640 return 0;
641}
642
643int
644printargs_ld(struct tcb *tcp)
645{
646 if (entering(tcp)) {
647 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100648 int n = tcp->s_ent->nargs;
649 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100650 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
651 }
652 return 0;
653}
654
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000655static void
656dumpio(struct tcb *tcp)
657{
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300658 int sen;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000659
660 if (syserror(tcp))
661 return;
662 if ((unsigned long) tcp->u_arg[0] >= num_quals)
663 return;
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300664 sen = tcp->s_ent->sen;
665 if (SEN_printargs == sen)
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000666 return;
667 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300668 switch (sen) {
669 case SEN_read:
670 case SEN_pread:
671 case SEN_recv:
672 case SEN_recvfrom:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000673 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
674 return;
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300675 case SEN_readv:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000676 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
677 return;
Dmitry V. Levind93c4e82015-06-17 20:09:13 +0000678#ifdef HAVE_SENDMSG
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300679 case SEN_recvmsg:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000680 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
681 return;
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300682 case SEN_recvmmsg:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000683 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
684 return;
685#endif
686 }
687 }
688 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300689 switch (sen) {
690 case SEN_write:
691 case SEN_pwrite:
692 case SEN_send:
693 case SEN_sendto:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000694 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300695 break;
696 case SEN_writev:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000697 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300698 break;
Dmitry V. Levind93c4e82015-06-17 20:09:13 +0000699#ifdef HAVE_SENDMSG
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300700 case SEN_sendmsg:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000701 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300702 break;
703 case SEN_sendmmsg:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000704 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300705 break;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000706#endif
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300707 }
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000708 }
709}
710
711/*
712 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
713 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
714 */
715#if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
716static long
717shuffle_scno(unsigned long scno)
718{
719 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
720 return scno;
721
722 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
723 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
724 return 0x000ffff0;
725 if (scno == 0x000ffff0)
726 return ARM_FIRST_SHUFFLED_SYSCALL;
727
728#define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
729 /*
730 * Is it ARM specific syscall?
731 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
732 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
733 */
734 if (scno >= 0x000f0000 &&
735 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
736 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
737 }
738 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
739 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
740 }
741
742 return scno;
743}
744#else
745# define shuffle_scno(scno) ((long)(scno))
746#endif
747
Mike Frysingerd2eaf672015-08-18 03:24:59 -0400748const char *
749syscall_name(long scno)
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000750{
751 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
752
Mike Frysingerd2eaf672015-08-18 03:24:59 -0400753 if (SCNO_IS_VALID(scno))
754 return sysent[scno].sys_name;
755 else {
756 sprintf(buf, "syscall_%lu", shuffle_scno(scno));
757 return buf;
758 }
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000759}
760
761static long get_regs_error;
762
763void
764clear_regs(void)
765{
766 get_regs_error = -1;
767}
768
769static int get_syscall_args(struct tcb *);
770static int get_syscall_result(struct tcb *);
771
772static int
773trace_syscall_entering(struct tcb *tcp)
774{
775 int res, scno_good;
776
777 scno_good = res = get_scno(tcp);
778 if (res == 0)
779 return res;
780 if (res == 1)
781 res = get_syscall_args(tcp);
782
783 if (res != 1) {
784 printleader(tcp);
785 if (scno_good != 1)
786 tprints("????" /* anti-trigraph gap */ "(");
787 else if (tcp->qual_flg & UNDEFINED_SCNO)
Mike Frysingerd2eaf672015-08-18 03:24:59 -0400788 tprintf("%s(", syscall_name(tcp->scno));
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000789 else
790 tprintf("%s(", tcp->s_ent->sys_name);
791 /*
792 * " <unavailable>" will be added later by the code which
793 * detects ptrace errors.
794 */
795 goto ret;
796 }
797
Dmitry V. Levinf34b97f2015-04-17 09:14:19 +0000798#ifdef LINUX_MIPSO32
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300799 if (SEN_syscall == tcp->s_ent->sen)
Dmitry V. Levinf34b97f2015-04-17 09:14:19 +0000800 decode_mips_subcall(tcp);
801#endif
802
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300803 if ( SEN_execve == tcp->s_ent->sen
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000804# if defined(SPARC) || defined(SPARC64)
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300805 || SEN_execv == tcp->s_ent->sen
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000806# endif
807 ) {
808 hide_log_until_execve = 0;
809 }
810
811#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300812 switch (tcp->s_ent->sen) {
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000813# ifdef SYS_socket_subcall
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300814 case SEN_socketcall:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000815 decode_socket_subcall(tcp);
816 break;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000817# endif
818# ifdef SYS_ipc_subcall
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300819 case SEN_ipc:
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000820 decode_ipc_subcall(tcp);
821 break;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000822# endif
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000823 }
824#endif
825
826 if (!(tcp->qual_flg & QUAL_TRACE)
827 || (tracing_paths && !pathtrace_match(tcp))
828 ) {
829 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000830 tcp->sys_func_rval = 0;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000831 return 0;
832 }
833
834 tcp->flags &= ~TCB_FILTERED;
835
836 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
837 res = 0;
838 goto ret;
839 }
840
841#ifdef USE_LIBUNWIND
842 if (stack_trace_enabled) {
843 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
844 unwind_capture_stacktrace(tcp);
845 }
846#endif
847
848 printleader(tcp);
849 if (tcp->qual_flg & UNDEFINED_SCNO)
Mike Frysingerd2eaf672015-08-18 03:24:59 -0400850 tprintf("%s(", syscall_name(tcp->scno));
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000851 else
852 tprintf("%s(", tcp->s_ent->sys_name);
Elvira Khabirova483c15f2015-07-10 22:24:58 +0300853 if ((tcp->qual_flg & QUAL_RAW) && SEN_exit != tcp->s_ent->sen)
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000854 res = printargs(tcp);
855 else
856 res = tcp->s_ent->sys_func(tcp);
857
858 fflush(tcp->outf);
859 ret:
860 tcp->flags |= TCB_INSYSCALL;
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000861 tcp->sys_func_rval = res;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000862 /* Measure the entrance time as late as possible to avoid errors. */
863 if (Tflag || cflag)
864 gettimeofday(&tcp->etime, NULL);
865 return res;
866}
867
868static int
869trace_syscall_exiting(struct tcb *tcp)
870{
871 int sys_res;
872 struct timeval tv;
873 int res;
874 long u_error;
875
876 /* Measure the exit time as early as possible to avoid errors. */
877 if (Tflag || cflag)
878 gettimeofday(&tv, NULL);
879
880#ifdef USE_LIBUNWIND
881 if (stack_trace_enabled) {
882 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
883 unwind_cache_invalidate(tcp);
884 }
885#endif
886
887#if SUPPORTED_PERSONALITIES > 1
888 update_personality(tcp, tcp->currpers);
889#endif
890 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Dmitry V. Levin4b80f342015-06-29 11:57:44 +0000891 if (filtered(tcp) || hide_log_until_execve)
892 goto ret;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000893
894 if (cflag) {
895 count_syscall(tcp, &tv);
896 if (cflag == CFLAG_ONLY_STATS) {
897 goto ret;
898 }
899 }
900
901 /* If not in -ff mode, and printing_tcp != tcp,
902 * then the log currently does not end with output
903 * of _our syscall entry_, but with something else.
904 * We need to say which syscall's return is this.
905 *
906 * Forced reprinting via TCB_REPRINT is used only by
907 * "strace -ff -oLOG test/threaded_execve" corner case.
908 * It's the only case when -ff mode needs reprinting.
909 */
910 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
911 tcp->flags &= ~TCB_REPRINT;
912 printleader(tcp);
913 if (tcp->qual_flg & UNDEFINED_SCNO)
Mike Frysingerd2eaf672015-08-18 03:24:59 -0400914 tprintf("<... %s resumed> ", syscall_name(tcp->scno));
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000915 else
916 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
917 }
918 printing_tcp = tcp;
919
920 tcp->s_prev_ent = NULL;
921 if (res != 1) {
922 /* There was error in one of prior ptrace ops */
923 tprints(") ");
924 tabto();
925 tprints("= ? <unavailable>\n");
926 line_ended();
927 tcp->flags &= ~TCB_INSYSCALL;
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000928 tcp->sys_func_rval = 0;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000929 return res;
930 }
931 tcp->s_prev_ent = tcp->s_ent;
932
933 sys_res = 0;
934 if (tcp->qual_flg & QUAL_RAW) {
935 /* sys_res = printargs(tcp); - but it's nop on sysexit */
936 } else {
937 /* FIXME: not_failing_only (IOW, option -z) is broken:
938 * failure of syscall is known only after syscall return.
939 * Thus we end up with something like this on, say, ENOENT:
940 * open("doesnt_exist", O_RDONLY <unfinished ...>
941 * {next syscall decode}
942 * whereas the intended result is that open(...) line
943 * is not shown at all.
944 */
945 if (not_failing_only && tcp->u_error)
946 goto ret; /* ignore failed syscalls */
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000947 if (tcp->sys_func_rval & RVAL_DECODED)
948 sys_res = tcp->sys_func_rval;
949 else
950 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000951 }
952
953 tprints(") ");
954 tabto();
955 u_error = tcp->u_error;
956 if (tcp->qual_flg & QUAL_RAW) {
957 if (u_error)
958 tprintf("= -1 (errno %ld)", u_error);
959 else
960 tprintf("= %#lx", tcp->u_rval);
961 }
962 else if (!(sys_res & RVAL_NONE) && u_error) {
963 switch (u_error) {
964 /* Blocked signals do not interrupt any syscalls.
965 * In this case syscalls don't return ERESTARTfoo codes.
966 *
967 * Deadly signals set to SIG_DFL interrupt syscalls
968 * and kill the process regardless of which of the codes below
969 * is returned by the interrupted syscall.
970 * In some cases, kernel forces a kernel-generated deadly
971 * signal to be unblocked and set to SIG_DFL (and thus cause
972 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
973 * or SIGILL. (The alternative is to leave process spinning
974 * forever on the faulty instruction - not useful).
975 *
976 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
977 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
978 * but kernel will always restart them.
979 */
980 case ERESTARTSYS:
981 /* Most common type of signal-interrupted syscall exit code.
982 * The system call will be restarted with the same arguments
983 * if SA_RESTART is set; otherwise, it will fail with EINTR.
984 */
985 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
986 break;
987 case ERESTARTNOINTR:
988 /* Rare. For example, fork() returns this if interrupted.
989 * SA_RESTART is ignored (assumed set): the restart is unconditional.
990 */
991 tprints("= ? ERESTARTNOINTR (To be restarted)");
992 break;
993 case ERESTARTNOHAND:
994 /* pause(), rt_sigsuspend() etc use this code.
995 * SA_RESTART is ignored (assumed not set):
996 * syscall won't restart (will return EINTR instead)
997 * even after signal with SA_RESTART set. However,
998 * after SIG_IGN or SIG_DFL signal it will restart
999 * (thus the name "restart only if has no handler").
1000 */
1001 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
1002 break;
1003 case ERESTART_RESTARTBLOCK:
1004 /* Syscalls like nanosleep(), poll() which can't be
1005 * restarted with their original arguments use this
1006 * code. Kernel will execute restart_syscall() instead,
1007 * which changes arguments before restarting syscall.
1008 * SA_RESTART is ignored (assumed not set) similarly
1009 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
1010 * since restart data is saved in "restart block"
1011 * in task struct, and if signal handler uses a syscall
1012 * which in turn saves another such restart block,
1013 * old data is lost and restart becomes impossible)
1014 */
1015 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
1016 break;
1017 default:
1018 if ((unsigned long) u_error < nerrnos
1019 && errnoent[u_error])
1020 tprintf("= -1 %s (%s)", errnoent[u_error],
1021 strerror(u_error));
1022 else
1023 tprintf("= -1 ERRNO_%lu (%s)", u_error,
1024 strerror(u_error));
1025 break;
1026 }
1027 if ((sys_res & RVAL_STR) && tcp->auxstr)
1028 tprintf(" (%s)", tcp->auxstr);
1029 }
1030 else {
1031 if (sys_res & RVAL_NONE)
1032 tprints("= ?");
1033 else {
1034 switch (sys_res & RVAL_MASK) {
1035 case RVAL_HEX:
1036#if SUPPORTED_PERSONALITIES > 1
1037 if (current_wordsize < sizeof(long))
1038 tprintf("= %#x",
1039 (unsigned int) tcp->u_rval);
1040 else
1041#endif
1042 tprintf("= %#lx", tcp->u_rval);
1043 break;
1044 case RVAL_OCTAL:
1045 tprintf("= %#lo", tcp->u_rval);
1046 break;
1047 case RVAL_UDECIMAL:
1048 tprintf("= %lu", tcp->u_rval);
1049 break;
1050 case RVAL_DECIMAL:
1051 tprintf("= %ld", tcp->u_rval);
1052 break;
1053 case RVAL_FD:
1054 if (show_fd_path) {
1055 tprints("= ");
1056 printfd(tcp, tcp->u_rval);
1057 }
1058 else
1059 tprintf("= %ld", tcp->u_rval);
1060 break;
1061#if defined(LINUX_MIPSN32) || defined(X32)
1062 /*
1063 case RVAL_LHEX:
1064 tprintf("= %#llx", tcp->u_lrval);
1065 break;
1066 case RVAL_LOCTAL:
1067 tprintf("= %#llo", tcp->u_lrval);
1068 break;
1069 */
1070 case RVAL_LUDECIMAL:
1071 tprintf("= %llu", tcp->u_lrval);
1072 break;
1073 /*
1074 case RVAL_LDECIMAL:
1075 tprintf("= %lld", tcp->u_lrval);
1076 break;
1077 */
1078#endif
1079 default:
Dmitry V. Levin6c8ef052015-05-25 22:51:19 +00001080 error_msg("invalid rval format");
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001081 break;
1082 }
1083 }
1084 if ((sys_res & RVAL_STR) && tcp->auxstr)
1085 tprintf(" (%s)", tcp->auxstr);
1086 }
1087 if (Tflag) {
1088 tv_sub(&tv, &tv, &tcp->etime);
1089 tprintf(" <%ld.%06ld>",
1090 (long) tv.tv_sec, (long) tv.tv_usec);
1091 }
1092 tprints("\n");
1093 dumpio(tcp);
1094 line_ended();
1095
1096#ifdef USE_LIBUNWIND
1097 if (stack_trace_enabled)
1098 unwind_print_stacktrace(tcp);
1099#endif
1100
1101 ret:
1102 tcp->flags &= ~TCB_INSYSCALL;
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +00001103 tcp->sys_func_rval = 0;
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001104 return 0;
1105}
1106
1107int
1108trace_syscall(struct tcb *tcp)
1109{
1110 return exiting(tcp) ?
1111 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
1112}
1113
1114/*
1115 * Cannot rely on __kernel_[u]long_t being defined,
1116 * it is quite a recent feature of <asm/posix_types.h>.
1117 */
1118#ifdef __kernel_long_t
1119typedef __kernel_long_t kernel_long_t;
1120typedef __kernel_ulong_t kernel_ulong_t;
1121#else
1122# ifdef X32
1123typedef long long kernel_long_t;
1124typedef unsigned long long kernel_ulong_t;
1125# else
1126typedef long kernel_long_t;
1127typedef unsigned long kernel_ulong_t;
1128# endif
1129#endif
1130
1131/*
1132 * Check the syscall return value register value for whether it is
1133 * a negated errno code indicating an error, or a success return value.
1134 */
1135static inline bool
1136is_negated_errno(kernel_ulong_t val)
1137{
1138 /* Linux kernel defines MAX_ERRNO to 4095. */
1139 kernel_ulong_t max = -(kernel_long_t) 4095;
1140
1141#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1142 if (current_wordsize < sizeof(val)) {
1143 val = (uint32_t) val;
1144 max = (uint32_t) max;
1145 }
1146#elif defined X32
1147 /*
1148 * current_wordsize is 4 even in personality 0 (native X32)
1149 * but truncation _must not_ be done in it.
1150 * can't check current_wordsize here!
1151 */
1152 if (current_personality != 0) {
1153 val = (uint32_t) val;
1154 max = (uint32_t) max;
1155 }
1156#endif
1157
1158 return val >= max;
1159}
1160
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001161#include "arch_regs.c"
Wichert Akkermanc7926982000-04-10 22:22:31 +00001162
Dmitry V. Levin78ed3f32015-03-23 00:04:27 +00001163#ifdef HAVE_GETRVAL2
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001164# include "arch_getrval2.c"
Dmitry V. Levin48f08902015-03-05 23:30:02 +00001165#endif
1166
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001167void
Denys Vlasenko5a2483b2013-07-01 12:49:14 +02001168print_pc(struct tcb *tcp)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001169{
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001170 const char *fmt;
1171 const char *bad;
1172
Dmitry V. Levinb2d9ff22015-02-23 13:43:20 +00001173#ifdef current_wordsize
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001174# define pc_wordsize current_wordsize
Dmitry V. Levinb2d9ff22015-02-23 13:43:20 +00001175#else
1176# define pc_wordsize personality_wordsize[tcp->currpers]
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001177#endif
1178
1179 if (pc_wordsize == 4) {
1180 fmt = "[%08lx] ";
1181 bad = "[????????] ";
1182 } else {
1183 fmt = "[%016lx] ";
1184 bad = "[????????????????] ";
1185 }
1186
1187#undef pc_wordsize
1188#define PRINTBADPC tprints(bad)
1189
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001190 if (get_regs_error) {
1191 PRINTBADPC;
1192 return;
1193 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001194
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001195#include "print_pc.c"
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001196}
1197
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001198#if defined X86_64 || defined POWERPC
1199# include "getregs_old.c"
Anton Blanchard14d51a62013-06-26 14:42:37 +10001200#endif
1201
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001202#if defined ARCH_REGS_FOR_GETREGSET
1203static long
1204get_regset(pid_t pid)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001205{
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001206# ifdef ARCH_IOVEC_FOR_GETREGSET
1207 /* variable iovec */
1208 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1209 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1210 &ARCH_IOVEC_FOR_GETREGSET);
1211# else
1212 /* constant iovec */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001213 static struct iovec io = {
1214 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1215 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001216 };
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001217 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001218
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001219# endif
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001220}
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001221#endif /* ARCH_REGS_FOR_GETREGSET */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001222
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001223void
1224get_regs(pid_t pid)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001225{
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001226#ifdef ARCH_REGS_FOR_GETREGSET
1227# ifdef X86_64
1228 /* Try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS. */
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001229 static int getregset_support;
1230
1231 if (getregset_support >= 0) {
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001232 get_regs_error = get_regset(pid);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001233 if (getregset_support > 0)
1234 return;
1235 if (get_regs_error >= 0) {
1236 getregset_support = 1;
1237 return;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001238 }
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001239 if (errno == EPERM || errno == ESRCH)
1240 return;
1241 getregset_support = -1;
1242 }
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001243 getregs_old(pid);
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001244# else /* !X86_64 */
1245 /* Assume that PTRACE_GETREGSET works. */
1246 get_regs_error = get_regset(pid);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001247# endif
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001248#elif defined ARCH_REGS_FOR_GETREGS
1249# if defined SPARC || defined SPARC64
1250 /* SPARC systems have the meaning of data and addr reversed */
1251 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&ARCH_REGS_FOR_GETREGS, 0);
1252# elif defined POWERPC
1253 static bool old_kernel = 0;
1254 if (old_kernel)
1255 goto old;
1256 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1257 if (get_regs_error && errno == EIO) {
1258 old_kernel = 1;
1259 old:
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001260 get_regs_error = getregs_old(pid);
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001261 }
1262# else
1263 /* Assume that PTRACE_GETREGS works. */
1264 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1265# endif
1266
1267#else /* !ARCH_REGS_FOR_GETREGSET && !ARCH_REGS_FOR_GETREGS */
1268# warning get_regs is not implemented for this architecture yet
1269 get_regs_error = 0;
1270#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001271}
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001272
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001273/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001274 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1275 * 1: ok, continue in trace_syscall_entering().
1276 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001277 * ("????" etc) and bail out.
1278 */
Denys Vlasenko8497b622015-03-21 17:51:52 +01001279int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001280get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001281{
Dmitry V. Levin144cda22015-03-23 18:48:32 +00001282 if (get_regs_error)
1283 return -1;
1284
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001285 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001286
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001287#include "get_scno.c"
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001288
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001289 tcp->scno = scno;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001290 if (SCNO_IS_VALID(tcp->scno)) {
1291 tcp->s_ent = &sysent[scno];
1292 tcp->qual_flg = qual_flags[scno];
1293 } else {
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01001294 static const struct_sysent unknown = {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001295 .nargs = MAX_ARGS,
1296 .sys_flags = 0,
1297 .sys_func = printargs,
Denys Vlasenkocc59f142015-04-07 12:46:59 +02001298 .sys_name = "system call",
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001299 };
1300 tcp->s_ent = &unknown;
1301 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
Dmitry V. Levinea009002015-03-24 01:59:07 +00001302 if (debug_flag)
Dmitry V. Levin6c8ef052015-05-25 22:51:19 +00001303 error_msg("pid %d invalid syscall %ld", tcp->pid, scno);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001304 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001305 return 1;
1306}
1307
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001308/* Return -1 on error or 1 on success (never 0!) */
Roland McGratha4d48532005-06-08 20:45:28 +00001309static int
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001310get_syscall_args(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001311{
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001312#include "get_syscall_args.c"
Pavel Machek4dc3b142000-02-01 17:58:41 +00001313 return 1;
1314}
1315
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01001316static void
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001317get_error(struct tcb *tcp)
1318{
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001319 const bool check_errno = !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS);
1320 tcp->u_error = 0;
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00001321
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001322#include "get_error.c"
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001323}
1324
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001325/* Returns:
1326 * 1: ok, continue in trace_syscall_exiting().
1327 * -1: error, trace_syscall_exiting() should print error indicator
1328 * ("????" etc) and bail out.
1329 */
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001330static int
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001331get_syscall_result(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001332{
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001333#if defined ARCH_REGS_FOR_GETREGSET || defined ARCH_REGS_FOR_GETREGS
1334 /* already done by get_regs */
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001335#else
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001336# include "get_syscall_result.c"
Masatake YAMATOed69fc22014-04-16 15:33:35 +09001337#endif
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001338 get_error(tcp);
1339 return 1;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00001340}