blob: ea74e353f8a1c46dfb9d80736f4a2114a0b72d16 [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
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +010084const struct_sysent sysent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000085#include "syscallent.h"
86};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000087
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010088#if SUPPORTED_PERSONALITIES > 1
89static const struct_sysent sysent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +010090# include "syscallent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000091};
Denys Vlasenko39fca622011-08-20 02:12:33 +020092#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000093
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +010094#if SUPPORTED_PERSONALITIES > 2
95static const struct_sysent sysent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +010096# include "syscallent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000097};
Denys Vlasenko39fca622011-08-20 02:12:33 +020098#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000099
100/* Now undef them since short defines cause wicked namespace pollution. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000101#undef TD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000102#undef TF
103#undef TI
104#undef TN
105#undef TP
106#undef TS
Namhyung Kim96792962012-10-24 11:41:57 +0900107#undef TM
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000108#undef NF
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200109#undef MA
Masatake YAMATO1d78d222014-04-16 15:33:11 +0900110#undef SI
111#undef SE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000112
Denys Vlasenko39fca622011-08-20 02:12:33 +0200113/*
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +0000114 * `ioctlent[012].h' files are automatically generated by the auxiliary
Denys Vlasenko39fca622011-08-20 02:12:33 +0200115 * program `ioctlsort', such that the list is sorted by the `code' field.
116 * This has the side-effect of resolving the _IO.. macros into
117 * plain integers, eliminating the need to include here everything
118 * in "/usr/include".
119 */
120
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100121const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000122#include "errnoent.h"
123};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100124const char *const signalent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200125#include "signalent.h"
126};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100127const struct_ioctlent ioctlent0[] = {
Dmitry V. Levindf7aa2b2015-01-19 17:02:16 +0000128#include "ioctlent0.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200129};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000130
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100131#if SUPPORTED_PERSONALITIES > 1
Roland McGrathee36ce12004-09-04 03:53:10 +0000132static const char *const errnoent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100133# include "errnoent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000134};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200135static const char *const signalent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100136# include "signalent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200137};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100138static const struct_ioctlent ioctlent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100139# include "ioctlent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200140};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200141#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000142
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100143#if SUPPORTED_PERSONALITIES > 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000144static const char *const errnoent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100145# include "errnoent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000146};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200147static const char *const signalent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100148# include "signalent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200149};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100150static const struct_ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100151# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200152};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200153#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000154
Dmitry V. Levine6f55242013-02-26 22:02:30 +0000155enum {
156 nsyscalls0 = ARRAY_SIZE(sysent0)
157#if SUPPORTED_PERSONALITIES > 1
158 , nsyscalls1 = ARRAY_SIZE(sysent1)
159# if SUPPORTED_PERSONALITIES > 2
160 , nsyscalls2 = ARRAY_SIZE(sysent2)
161# endif
162#endif
163};
164
165enum {
166 nerrnos0 = ARRAY_SIZE(errnoent0)
167#if SUPPORTED_PERSONALITIES > 1
168 , nerrnos1 = ARRAY_SIZE(errnoent1)
169# if SUPPORTED_PERSONALITIES > 2
170 , nerrnos2 = ARRAY_SIZE(errnoent2)
171# endif
172#endif
173};
174
175enum {
176 nsignals0 = ARRAY_SIZE(signalent0)
177#if SUPPORTED_PERSONALITIES > 1
178 , nsignals1 = ARRAY_SIZE(signalent1)
179# if SUPPORTED_PERSONALITIES > 2
180 , nsignals2 = ARRAY_SIZE(signalent2)
181# endif
182#endif
183};
184
185enum {
186 nioctlents0 = ARRAY_SIZE(ioctlent0)
187#if SUPPORTED_PERSONALITIES > 1
188 , nioctlents1 = ARRAY_SIZE(ioctlent1)
189# if SUPPORTED_PERSONALITIES > 2
190 , nioctlents2 = ARRAY_SIZE(ioctlent2)
191# endif
192#endif
193};
194
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100195#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100196const struct_sysent *sysent = sysent0;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100197const char *const *errnoent = errnoent0;
198const char *const *signalent = signalent0;
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100199const struct_ioctlent *ioctlent = ioctlent0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100200#endif
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100201unsigned nsyscalls = nsyscalls0;
202unsigned nerrnos = nerrnos0;
203unsigned nsignals = nsignals0;
204unsigned nioctlents = nioctlents0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100205
206unsigned num_quals;
207qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
208
209static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
210 nsyscalls0,
211#if SUPPORTED_PERSONALITIES > 1
212 nsyscalls1,
213#endif
214#if SUPPORTED_PERSONALITIES > 2
215 nsyscalls2,
216#endif
217};
218static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
219 sysent0,
220#if SUPPORTED_PERSONALITIES > 1
221 sysent1,
222#endif
223#if SUPPORTED_PERSONALITIES > 2
224 sysent2,
225#endif
226};
227
228enum {
229 MAX_NSYSCALLS1 = (nsyscalls0
230#if SUPPORTED_PERSONALITIES > 1
231 > nsyscalls1 ? nsyscalls0 : nsyscalls1
232#endif
233 ),
234 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
235#if SUPPORTED_PERSONALITIES > 2
236 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
237#endif
238 ),
239 MAX_NSYSCALLS = MAX_NSYSCALLS2,
240 /* We are ready for arches with up to 255 signals,
241 * even though the largest known signo is on MIPS and it is 128.
242 * The number of existing syscalls on all arches is
243 * larger that 255 anyway, so it is just a pedantic matter.
244 */
245 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
246};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000247
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100248#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100249unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000250
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100251# ifndef current_wordsize
252unsigned current_wordsize;
253static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000254 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000255 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100256# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000257 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100258# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200259};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100260# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000261
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200262void
Dmitry V. Levin3abe8b22006-12-20 22:37:21 +0000263set_personality(int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000264{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100265 nsyscalls = nsyscall_vec[personality];
266 sysent = sysent_vec[personality];
267
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000268 switch (personality) {
269 case 0:
270 errnoent = errnoent0;
271 nerrnos = nerrnos0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000272 ioctlent = ioctlent0;
273 nioctlents = nioctlents0;
274 signalent = signalent0;
275 nsignals = nsignals0;
276 break;
277
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000278 case 1:
279 errnoent = errnoent1;
280 nerrnos = nerrnos1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000281 ioctlent = ioctlent1;
282 nioctlents = nioctlents1;
283 signalent = signalent1;
284 nsignals = nsignals1;
285 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000286
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100287# if SUPPORTED_PERSONALITIES > 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000288 case 2:
289 errnoent = errnoent2;
290 nerrnos = nerrnos2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000291 ioctlent = ioctlent2;
292 nioctlents = nioctlents2;
293 signalent = signalent2;
294 nsignals = nsignals2;
295 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100296# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000297 }
298
299 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100300# ifndef current_wordsize
301 current_wordsize = personality_wordsize[personality];
302# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000303}
304
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000305static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000306update_personality(struct tcb *tcp, unsigned int personality)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000307{
308 if (personality == current_personality)
309 return;
310 set_personality(personality);
311
312 if (personality == tcp->currpers)
313 return;
314 tcp->currpers = personality;
315
Dmitry V. Levin6c8ef052015-05-25 22:51:19 +0000316# undef PERSONALITY_NAMES
317# if defined POWERPC64
318# define PERSONALITY_NAMES {"64 bit", "32 bit"}
319# elif defined X86_64
320# define PERSONALITY_NAMES {"64 bit", "32 bit", "x32"}
321# elif defined X32
322# define PERSONALITY_NAMES {"x32", "32 bit"}
323# elif defined AARCH64
324# define PERSONALITY_NAMES {"32-bit", "AArch64"}
325# elif defined TILE
326# define PERSONALITY_NAMES {"64-bit", "32-bit"}
327# endif
328# ifdef PERSONALITY_NAMES
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000329 if (!qflag) {
Dmitry V. Levin6c8ef052015-05-25 22:51:19 +0000330 static const char *const names[] = PERSONALITY_NAMES;
331 error_msg("[ Process PID=%d runs in %s mode. ]",
332 tcp->pid, names[personality]);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100333 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100334# endif
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000335}
336#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000337
Denys Vlasenkoc1540fe2013-02-21 16:17:08 +0100338static int qual_syscall(), qual_signal(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000339
Roland McGrathe10e62a2004-09-04 04:20:43 +0000340static const struct qual_options {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000341 unsigned int bitflag;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000342 const char *option_name;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000343 int (*qualify)(const char *, int, int);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000344 const char *argument_name;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000345} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000346 { QUAL_TRACE, "trace", qual_syscall, "system call" },
347 { QUAL_TRACE, "t", qual_syscall, "system call" },
348 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
349 { QUAL_ABBREV, "a", qual_syscall, "system call" },
350 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
351 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
352 { QUAL_RAW, "raw", qual_syscall, "system call" },
353 { QUAL_RAW, "x", qual_syscall, "system call" },
354 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
355 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
356 { QUAL_SIGNAL, "s", qual_signal, "signal" },
Roland McGrath9797ceb2002-12-30 10:23:00 +0000357 { QUAL_READ, "read", qual_desc, "descriptor" },
358 { QUAL_READ, "reads", qual_desc, "descriptor" },
359 { QUAL_READ, "r", qual_desc, "descriptor" },
360 { QUAL_WRITE, "write", qual_desc, "descriptor" },
361 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
362 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000363 { 0, NULL, NULL, NULL },
364};
365
Roland McGrath9797ceb2002-12-30 10:23:00 +0000366static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000367reallocate_qual(const unsigned int n)
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100368{
369 unsigned p;
370 qualbits_t *qp;
371 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
Dmitry V. Levin3e9d71f2015-05-25 20:41:02 +0000372 qp = qual_vec[p] = xreallocarray(qual_vec[p], n,
373 sizeof(qualbits_t));
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100374 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
375 }
376 num_quals = n;
377}
378
379static void
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000380qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000381{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000382 int p;
Roland McGrath138c6a32006-01-12 09:50:49 +0000383
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100384 if (num_quals <= n)
385 reallocate_qual(n + 1);
Roland McGrath138c6a32006-01-12 09:50:49 +0000386
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100387 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
388 if (pers == p || pers < 0) {
389 if (not)
390 qual_vec[p][n] &= ~bitflag;
391 else
392 qual_vec[p][n] |= bitflag;
393 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000394 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000395}
396
397static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000398qual_syscall(const char *s, const unsigned int bitflag, const int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000399{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000400 int p;
401 unsigned int i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000402 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000403
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100404 if (*s >= '0' && *s <= '9') {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100405 i = string_to_uint(s);
Denys Vlasenkob43dacd2013-02-23 18:19:28 +0100406 if (i >= MAX_NSYSCALLS)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000407 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000408 qualify_one(i, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000409 return 0;
Roland McGrath48a035f2006-01-12 09:45:56 +0000410 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000411
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100412 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
413 for (i = 0; i < nsyscall_vec[p]; i++) {
414 if (sysent_vec[p][i].sys_name
415 && strcmp(s, sysent_vec[p][i].sys_name) == 0
416 ) {
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000417 qualify_one(i, bitflag, not, p);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100418 rc = 0;
419 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000420 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100421 }
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000422
Roland McGrathfe6b3522005-02-02 04:40:11 +0000423 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000424}
425
426static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000427qual_signal(const char *s, const unsigned int bitflag, const int not)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000428{
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000429 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000430
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100431 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000432 int signo = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100433 if (signo < 0 || signo > 255)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000434 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000435 qualify_one(signo, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000436 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000437 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000438 if (strncasecmp(s, "SIG", 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000439 s += 3;
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100440 for (i = 0; i <= NSIG; i++) {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000441 if (strcasecmp(s, signame(i) + 3) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000442 qualify_one(i, bitflag, not, -1);
Roland McGrath76421df2005-02-02 03:51:18 +0000443 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000444 }
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100445 }
Roland McGrath76421df2005-02-02 03:51:18 +0000446 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000447}
448
449static int
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000450qual_desc(const char *s, const unsigned int bitflag, const int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000451{
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100452 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000453 int desc = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100454 if (desc < 0 || desc > 0x7fff) /* paranoia */
Roland McGrathfe6b3522005-02-02 04:40:11 +0000455 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000456 qualify_one(desc, bitflag, not, -1);
Roland McGrath2b619022003-04-10 18:58:20 +0000457 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000458 }
459 return -1;
460}
461
462static int
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000463lookup_class(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000464{
465 if (strcmp(s, "file") == 0)
466 return TRACE_FILE;
467 if (strcmp(s, "ipc") == 0)
468 return TRACE_IPC;
469 if (strcmp(s, "network") == 0)
470 return TRACE_NETWORK;
471 if (strcmp(s, "process") == 0)
472 return TRACE_PROCESS;
473 if (strcmp(s, "signal") == 0)
474 return TRACE_SIGNAL;
Roland McGrath2fe7b132005-07-05 03:25:35 +0000475 if (strcmp(s, "desc") == 0)
476 return TRACE_DESC;
Namhyung Kim96792962012-10-24 11:41:57 +0900477 if (strcmp(s, "memory") == 0)
478 return TRACE_MEMORY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000479 return -1;
480}
481
482void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000483qualify(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000484{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000485 const struct qual_options *opt;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000486 char *copy;
487 const char *p;
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000488 int not;
489 unsigned int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000490
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100491 if (num_quals == 0)
492 reallocate_qual(MIN_QUALS);
493
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000494 opt = &qual_options[0];
495 for (i = 0; (p = qual_options[i].option_name); i++) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000496 unsigned int len = strlen(p);
497 if (strncmp(s, p, len) == 0 && s[len] == '=') {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000498 opt = &qual_options[i];
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000499 s += len + 1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000500 break;
501 }
502 }
503 not = 0;
504 if (*s == '!') {
505 not = 1;
506 s++;
507 }
508 if (strcmp(s, "none") == 0) {
509 not = 1 - not;
510 s = "all";
511 }
512 if (strcmp(s, "all") == 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100513 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000514 qualify_one(i, opt->bitflag, not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000515 }
516 return;
517 }
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 }
Dmitry V. Levin3e9d71f2015-05-25 20:41:02 +0000521 copy = xstrdup(s);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000522 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
Dmitry V. Levin3ed5d022014-09-10 13:46:04 +0000523 int n;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000524 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100525 unsigned pers;
526 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
527 for (i = 0; i < nsyscall_vec[pers]; i++)
528 if (sysent_vec[pers][i].sys_flags & n)
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000529 qualify_one(i, opt->bitflag, not, pers);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100530 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000531 continue;
532 }
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000533 if (opt->qualify(p, opt->bitflag, not)) {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100534 error_msg_and_die("invalid %s '%s'",
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000535 opt->argument_name, p);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000536 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000537 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000538 free(copy);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000539 return;
540}
541
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000542#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000543static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000544decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000545{
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000546 unsigned long addr;
Dmitry V. Levinc2155692015-03-22 15:52:40 +0000547 unsigned int n;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000548
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000549 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
550 return;
551
552 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100553 tcp->qual_flg = qual_flags[tcp->scno];
554 tcp->s_ent = &sysent[tcp->scno];
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000555 addr = tcp->u_arg[1];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100556 n = tcp->s_ent->nargs;
Dmitry V. Levinc2155692015-03-22 15:52:40 +0000557 if (sizeof(tcp->u_arg[0]) == current_wordsize) {
558 memset(tcp->u_arg, 0, n * sizeof(tcp->u_arg[0]));
559 (void) umoven(tcp, addr, n * sizeof(tcp->u_arg[0]), tcp->u_arg);
560 } else {
561 unsigned int args[n];
562 unsigned int i;
563
564 memset(args, 0, sizeof(args));
565 (void) umove(tcp, addr, &args);
566 for (i = 0; i < n; ++i)
567 tcp->u_arg[i] = args[i];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000568 }
569}
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000570#endif
Mike Frysinger3362e892012-03-15 01:09:19 -0400571
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000572#ifdef SYS_ipc_subcall
573static void
574decode_ipc_subcall(struct tcb *tcp)
575{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100576 unsigned int i, n;
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000577
578 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
579 return;
580
581 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100582 tcp->qual_flg = qual_flags[tcp->scno];
583 tcp->s_ent = &sysent[tcp->scno];
584 n = tcp->s_ent->nargs;
585 for (i = 0; i < n; i++)
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000586 tcp->u_arg[i] = tcp->u_arg[i + 1];
587}
588#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000589
Dmitry V. Levinf34b97f2015-04-17 09:14:19 +0000590#ifdef LINUX_MIPSO32
591static void
592decode_mips_subcall(struct tcb *tcp)
593{
594 if (!SCNO_IS_VALID(tcp->u_arg[0]))
595 return;
596 tcp->scno = tcp->u_arg[0];
597 tcp->qual_flg = qual_flags[tcp->scno];
598 tcp->s_ent = &sysent[tcp->scno];
599 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
600 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
601 /*
602 * Fetching the last arg of 7-arg syscalls (fadvise64_64
603 * and sync_file_range) would require additional code,
604 * see linux/mips/get_syscall_args.c
605 */
606}
607
608SYS_FUNC(syscall)
609{
610 return printargs(tcp);
611}
612#endif
613
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200614int
615printargs(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000616{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200617 if (entering(tcp)) {
618 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100619 int n = tcp->s_ent->nargs;
620 for (i = 0; i < n; i++)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200621 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
622 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000623 return 0;
624}
625
Denys Vlasenko72879c62012-02-27 14:18:02 +0100626int
627printargs_lu(struct tcb *tcp)
628{
629 if (entering(tcp)) {
630 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100631 int n = tcp->s_ent->nargs;
632 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100633 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
634 }
635 return 0;
636}
637
638int
639printargs_ld(struct tcb *tcp)
640{
641 if (entering(tcp)) {
642 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100643 int n = tcp->s_ent->nargs;
644 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100645 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
646 }
647 return 0;
648}
649
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000650static void
651dumpio(struct tcb *tcp)
652{
653 int (*func)();
654
655 if (syserror(tcp))
656 return;
657 if ((unsigned long) tcp->u_arg[0] >= num_quals)
658 return;
659 func = tcp->s_ent->sys_func;
660 if (func == printargs)
661 return;
662 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
663 if (func == sys_read ||
664 func == sys_pread ||
665 func == sys_recv ||
666 func == sys_recvfrom) {
667 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
668 return;
669 } else if (func == sys_readv) {
670 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
671 return;
Dmitry V. Levind93c4e82015-06-17 20:09:13 +0000672#ifdef HAVE_SENDMSG
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000673 } else if (func == sys_recvmsg) {
674 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
675 return;
676 } else if (func == sys_recvmmsg) {
677 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
678 return;
679#endif
680 }
681 }
682 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
683 if (func == sys_write ||
684 func == sys_pwrite ||
685 func == sys_send ||
686 func == sys_sendto)
687 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
688 else if (func == sys_writev)
689 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
Dmitry V. Levind93c4e82015-06-17 20:09:13 +0000690#ifdef HAVE_SENDMSG
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000691 else if (func == sys_sendmsg)
692 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
693 else if (func == sys_sendmmsg)
694 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
695#endif
696 }
697}
698
699/*
700 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
701 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
702 */
703#if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
704static long
705shuffle_scno(unsigned long scno)
706{
707 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
708 return scno;
709
710 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
711 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
712 return 0x000ffff0;
713 if (scno == 0x000ffff0)
714 return ARM_FIRST_SHUFFLED_SYSCALL;
715
716#define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
717 /*
718 * Is it ARM specific syscall?
719 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
720 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
721 */
722 if (scno >= 0x000f0000 &&
723 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
724 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
725 }
726 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
727 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
728 }
729
730 return scno;
731}
732#else
733# define shuffle_scno(scno) ((long)(scno))
734#endif
735
736static char*
737undefined_scno_name(struct tcb *tcp)
738{
739 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
740
741 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
742 return buf;
743}
744
745static long get_regs_error;
746
747void
748clear_regs(void)
749{
750 get_regs_error = -1;
751}
752
753static int get_syscall_args(struct tcb *);
754static int get_syscall_result(struct tcb *);
755
756static int
757trace_syscall_entering(struct tcb *tcp)
758{
759 int res, scno_good;
760
761 scno_good = res = get_scno(tcp);
762 if (res == 0)
763 return res;
764 if (res == 1)
765 res = get_syscall_args(tcp);
766
767 if (res != 1) {
768 printleader(tcp);
769 if (scno_good != 1)
770 tprints("????" /* anti-trigraph gap */ "(");
771 else if (tcp->qual_flg & UNDEFINED_SCNO)
772 tprintf("%s(", undefined_scno_name(tcp));
773 else
774 tprintf("%s(", tcp->s_ent->sys_name);
775 /*
776 * " <unavailable>" will be added later by the code which
777 * detects ptrace errors.
778 */
779 goto ret;
780 }
781
Dmitry V. Levinf34b97f2015-04-17 09:14:19 +0000782#ifdef LINUX_MIPSO32
783 if (sys_syscall == tcp->s_ent->sys_func)
784 decode_mips_subcall(tcp);
785#endif
786
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000787 if ( sys_execve == tcp->s_ent->sys_func
788# if defined(SPARC) || defined(SPARC64)
789 || sys_execv == tcp->s_ent->sys_func
790# endif
791 ) {
792 hide_log_until_execve = 0;
793 }
794
795#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
796 while (1) {
797# ifdef SYS_socket_subcall
798 if (tcp->s_ent->sys_func == sys_socketcall) {
799 decode_socket_subcall(tcp);
800 break;
801 }
802# endif
803# ifdef SYS_ipc_subcall
804 if (tcp->s_ent->sys_func == sys_ipc) {
805 decode_ipc_subcall(tcp);
806 break;
807 }
808# endif
809 break;
810 }
811#endif
812
813 if (!(tcp->qual_flg & QUAL_TRACE)
814 || (tracing_paths && !pathtrace_match(tcp))
815 ) {
816 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000817 tcp->sys_func_rval = 0;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000818 return 0;
819 }
820
821 tcp->flags &= ~TCB_FILTERED;
822
823 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
824 res = 0;
825 goto ret;
826 }
827
828#ifdef USE_LIBUNWIND
829 if (stack_trace_enabled) {
830 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
831 unwind_capture_stacktrace(tcp);
832 }
833#endif
834
835 printleader(tcp);
836 if (tcp->qual_flg & UNDEFINED_SCNO)
837 tprintf("%s(", undefined_scno_name(tcp));
838 else
839 tprintf("%s(", tcp->s_ent->sys_name);
840 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
841 res = printargs(tcp);
842 else
843 res = tcp->s_ent->sys_func(tcp);
844
845 fflush(tcp->outf);
846 ret:
847 tcp->flags |= TCB_INSYSCALL;
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000848 tcp->sys_func_rval = res;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000849 /* Measure the entrance time as late as possible to avoid errors. */
850 if (Tflag || cflag)
851 gettimeofday(&tcp->etime, NULL);
852 return res;
853}
854
855static int
856trace_syscall_exiting(struct tcb *tcp)
857{
858 int sys_res;
859 struct timeval tv;
860 int res;
861 long u_error;
862
863 /* Measure the exit time as early as possible to avoid errors. */
864 if (Tflag || cflag)
865 gettimeofday(&tv, NULL);
866
867#ifdef USE_LIBUNWIND
868 if (stack_trace_enabled) {
869 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
870 unwind_cache_invalidate(tcp);
871 }
872#endif
873
874#if SUPPORTED_PERSONALITIES > 1
875 update_personality(tcp, tcp->currpers);
876#endif
877 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Dmitry V. Levin4b80f342015-06-29 11:57:44 +0000878 if (filtered(tcp) || hide_log_until_execve)
879 goto ret;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000880
881 if (cflag) {
882 count_syscall(tcp, &tv);
883 if (cflag == CFLAG_ONLY_STATS) {
884 goto ret;
885 }
886 }
887
888 /* If not in -ff mode, and printing_tcp != tcp,
889 * then the log currently does not end with output
890 * of _our syscall entry_, but with something else.
891 * We need to say which syscall's return is this.
892 *
893 * Forced reprinting via TCB_REPRINT is used only by
894 * "strace -ff -oLOG test/threaded_execve" corner case.
895 * It's the only case when -ff mode needs reprinting.
896 */
897 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
898 tcp->flags &= ~TCB_REPRINT;
899 printleader(tcp);
900 if (tcp->qual_flg & UNDEFINED_SCNO)
901 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
902 else
903 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
904 }
905 printing_tcp = tcp;
906
907 tcp->s_prev_ent = NULL;
908 if (res != 1) {
909 /* There was error in one of prior ptrace ops */
910 tprints(") ");
911 tabto();
912 tprints("= ? <unavailable>\n");
913 line_ended();
914 tcp->flags &= ~TCB_INSYSCALL;
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000915 tcp->sys_func_rval = 0;
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000916 return res;
917 }
918 tcp->s_prev_ent = tcp->s_ent;
919
920 sys_res = 0;
921 if (tcp->qual_flg & QUAL_RAW) {
922 /* sys_res = printargs(tcp); - but it's nop on sysexit */
923 } else {
924 /* FIXME: not_failing_only (IOW, option -z) is broken:
925 * failure of syscall is known only after syscall return.
926 * Thus we end up with something like this on, say, ENOENT:
927 * open("doesnt_exist", O_RDONLY <unfinished ...>
928 * {next syscall decode}
929 * whereas the intended result is that open(...) line
930 * is not shown at all.
931 */
932 if (not_failing_only && tcp->u_error)
933 goto ret; /* ignore failed syscalls */
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +0000934 if (tcp->sys_func_rval & RVAL_DECODED)
935 sys_res = tcp->sys_func_rval;
936 else
937 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin1b786072015-03-22 18:09:55 +0000938 }
939
940 tprints(") ");
941 tabto();
942 u_error = tcp->u_error;
943 if (tcp->qual_flg & QUAL_RAW) {
944 if (u_error)
945 tprintf("= -1 (errno %ld)", u_error);
946 else
947 tprintf("= %#lx", tcp->u_rval);
948 }
949 else if (!(sys_res & RVAL_NONE) && u_error) {
950 switch (u_error) {
951 /* Blocked signals do not interrupt any syscalls.
952 * In this case syscalls don't return ERESTARTfoo codes.
953 *
954 * Deadly signals set to SIG_DFL interrupt syscalls
955 * and kill the process regardless of which of the codes below
956 * is returned by the interrupted syscall.
957 * In some cases, kernel forces a kernel-generated deadly
958 * signal to be unblocked and set to SIG_DFL (and thus cause
959 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
960 * or SIGILL. (The alternative is to leave process spinning
961 * forever on the faulty instruction - not useful).
962 *
963 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
964 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
965 * but kernel will always restart them.
966 */
967 case ERESTARTSYS:
968 /* Most common type of signal-interrupted syscall exit code.
969 * The system call will be restarted with the same arguments
970 * if SA_RESTART is set; otherwise, it will fail with EINTR.
971 */
972 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
973 break;
974 case ERESTARTNOINTR:
975 /* Rare. For example, fork() returns this if interrupted.
976 * SA_RESTART is ignored (assumed set): the restart is unconditional.
977 */
978 tprints("= ? ERESTARTNOINTR (To be restarted)");
979 break;
980 case ERESTARTNOHAND:
981 /* pause(), rt_sigsuspend() etc use this code.
982 * SA_RESTART is ignored (assumed not set):
983 * syscall won't restart (will return EINTR instead)
984 * even after signal with SA_RESTART set. However,
985 * after SIG_IGN or SIG_DFL signal it will restart
986 * (thus the name "restart only if has no handler").
987 */
988 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
989 break;
990 case ERESTART_RESTARTBLOCK:
991 /* Syscalls like nanosleep(), poll() which can't be
992 * restarted with their original arguments use this
993 * code. Kernel will execute restart_syscall() instead,
994 * which changes arguments before restarting syscall.
995 * SA_RESTART is ignored (assumed not set) similarly
996 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
997 * since restart data is saved in "restart block"
998 * in task struct, and if signal handler uses a syscall
999 * which in turn saves another such restart block,
1000 * old data is lost and restart becomes impossible)
1001 */
1002 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
1003 break;
1004 default:
1005 if ((unsigned long) u_error < nerrnos
1006 && errnoent[u_error])
1007 tprintf("= -1 %s (%s)", errnoent[u_error],
1008 strerror(u_error));
1009 else
1010 tprintf("= -1 ERRNO_%lu (%s)", u_error,
1011 strerror(u_error));
1012 break;
1013 }
1014 if ((sys_res & RVAL_STR) && tcp->auxstr)
1015 tprintf(" (%s)", tcp->auxstr);
1016 }
1017 else {
1018 if (sys_res & RVAL_NONE)
1019 tprints("= ?");
1020 else {
1021 switch (sys_res & RVAL_MASK) {
1022 case RVAL_HEX:
1023#if SUPPORTED_PERSONALITIES > 1
1024 if (current_wordsize < sizeof(long))
1025 tprintf("= %#x",
1026 (unsigned int) tcp->u_rval);
1027 else
1028#endif
1029 tprintf("= %#lx", tcp->u_rval);
1030 break;
1031 case RVAL_OCTAL:
1032 tprintf("= %#lo", tcp->u_rval);
1033 break;
1034 case RVAL_UDECIMAL:
1035 tprintf("= %lu", tcp->u_rval);
1036 break;
1037 case RVAL_DECIMAL:
1038 tprintf("= %ld", tcp->u_rval);
1039 break;
1040 case RVAL_FD:
1041 if (show_fd_path) {
1042 tprints("= ");
1043 printfd(tcp, tcp->u_rval);
1044 }
1045 else
1046 tprintf("= %ld", tcp->u_rval);
1047 break;
1048#if defined(LINUX_MIPSN32) || defined(X32)
1049 /*
1050 case RVAL_LHEX:
1051 tprintf("= %#llx", tcp->u_lrval);
1052 break;
1053 case RVAL_LOCTAL:
1054 tprintf("= %#llo", tcp->u_lrval);
1055 break;
1056 */
1057 case RVAL_LUDECIMAL:
1058 tprintf("= %llu", tcp->u_lrval);
1059 break;
1060 /*
1061 case RVAL_LDECIMAL:
1062 tprintf("= %lld", tcp->u_lrval);
1063 break;
1064 */
1065#endif
1066 default:
Dmitry V. Levin6c8ef052015-05-25 22:51:19 +00001067 error_msg("invalid rval format");
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001068 break;
1069 }
1070 }
1071 if ((sys_res & RVAL_STR) && tcp->auxstr)
1072 tprintf(" (%s)", tcp->auxstr);
1073 }
1074 if (Tflag) {
1075 tv_sub(&tv, &tv, &tcp->etime);
1076 tprintf(" <%ld.%06ld>",
1077 (long) tv.tv_sec, (long) tv.tv_usec);
1078 }
1079 tprints("\n");
1080 dumpio(tcp);
1081 line_ended();
1082
1083#ifdef USE_LIBUNWIND
1084 if (stack_trace_enabled)
1085 unwind_print_stacktrace(tcp);
1086#endif
1087
1088 ret:
1089 tcp->flags &= ~TCB_INSYSCALL;
Dmitry V. Levin204c2bc2015-07-08 14:10:56 +00001090 tcp->sys_func_rval = 0;
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001091 return 0;
1092}
1093
1094int
1095trace_syscall(struct tcb *tcp)
1096{
1097 return exiting(tcp) ?
1098 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
1099}
1100
1101/*
1102 * Cannot rely on __kernel_[u]long_t being defined,
1103 * it is quite a recent feature of <asm/posix_types.h>.
1104 */
1105#ifdef __kernel_long_t
1106typedef __kernel_long_t kernel_long_t;
1107typedef __kernel_ulong_t kernel_ulong_t;
1108#else
1109# ifdef X32
1110typedef long long kernel_long_t;
1111typedef unsigned long long kernel_ulong_t;
1112# else
1113typedef long kernel_long_t;
1114typedef unsigned long kernel_ulong_t;
1115# endif
1116#endif
1117
1118/*
1119 * Check the syscall return value register value for whether it is
1120 * a negated errno code indicating an error, or a success return value.
1121 */
1122static inline bool
1123is_negated_errno(kernel_ulong_t val)
1124{
1125 /* Linux kernel defines MAX_ERRNO to 4095. */
1126 kernel_ulong_t max = -(kernel_long_t) 4095;
1127
1128#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1129 if (current_wordsize < sizeof(val)) {
1130 val = (uint32_t) val;
1131 max = (uint32_t) max;
1132 }
1133#elif defined X32
1134 /*
1135 * current_wordsize is 4 even in personality 0 (native X32)
1136 * but truncation _must not_ be done in it.
1137 * can't check current_wordsize here!
1138 */
1139 if (current_personality != 0) {
1140 val = (uint32_t) val;
1141 max = (uint32_t) max;
1142 }
1143#endif
1144
1145 return val >= max;
1146}
1147
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001148#include "arch_regs.c"
Wichert Akkermanc7926982000-04-10 22:22:31 +00001149
Dmitry V. Levin78ed3f32015-03-23 00:04:27 +00001150#ifdef HAVE_GETRVAL2
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001151# include "arch_getrval2.c"
Dmitry V. Levin48f08902015-03-05 23:30:02 +00001152#endif
1153
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001154void
Denys Vlasenko5a2483b2013-07-01 12:49:14 +02001155print_pc(struct tcb *tcp)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001156{
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001157 const char *fmt;
1158 const char *bad;
1159
Dmitry V. Levinb2d9ff22015-02-23 13:43:20 +00001160#ifdef current_wordsize
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001161# define pc_wordsize current_wordsize
Dmitry V. Levinb2d9ff22015-02-23 13:43:20 +00001162#else
1163# define pc_wordsize personality_wordsize[tcp->currpers]
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001164#endif
1165
1166 if (pc_wordsize == 4) {
1167 fmt = "[%08lx] ";
1168 bad = "[????????] ";
1169 } else {
1170 fmt = "[%016lx] ";
1171 bad = "[????????????????] ";
1172 }
1173
1174#undef pc_wordsize
1175#define PRINTBADPC tprints(bad)
1176
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001177 if (get_regs_error) {
1178 PRINTBADPC;
1179 return;
1180 }
Dmitry V. Levine96cb622015-02-15 15:52:02 +00001181
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001182#include "print_pc.c"
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001183}
1184
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001185#if defined X86_64 || defined POWERPC
1186# include "getregs_old.c"
Anton Blanchard14d51a62013-06-26 14:42:37 +10001187#endif
1188
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001189#if defined ARCH_REGS_FOR_GETREGSET
1190static long
1191get_regset(pid_t pid)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001192{
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001193# ifdef ARCH_IOVEC_FOR_GETREGSET
1194 /* variable iovec */
1195 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1196 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1197 &ARCH_IOVEC_FOR_GETREGSET);
1198# else
1199 /* constant iovec */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001200 static struct iovec io = {
1201 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1202 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001203 };
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001204 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001205
Dmitry V. Levinfaa177e2013-03-17 23:48:45 +00001206# endif
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001207}
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001208#endif /* ARCH_REGS_FOR_GETREGSET */
Dmitry V. Levinb787b102013-03-18 10:17:14 +00001209
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001210void
1211get_regs(pid_t pid)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001212{
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001213#ifdef ARCH_REGS_FOR_GETREGSET
1214# ifdef X86_64
1215 /* Try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS. */
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001216 static int getregset_support;
1217
1218 if (getregset_support >= 0) {
Dmitry V. Levind6db1db2015-02-13 23:41:04 +00001219 get_regs_error = get_regset(pid);
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001220 if (getregset_support > 0)
1221 return;
1222 if (get_regs_error >= 0) {
1223 getregset_support = 1;
1224 return;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001225 }
Dmitry V. Levin27e3ae92013-03-17 23:18:35 +00001226 if (errno == EPERM || errno == ESRCH)
1227 return;
1228 getregset_support = -1;
1229 }
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001230 getregs_old(pid);
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001231# else /* !X86_64 */
1232 /* Assume that PTRACE_GETREGSET works. */
1233 get_regs_error = get_regset(pid);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001234# endif
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001235#elif defined ARCH_REGS_FOR_GETREGS
1236# if defined SPARC || defined SPARC64
1237 /* SPARC systems have the meaning of data and addr reversed */
1238 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&ARCH_REGS_FOR_GETREGS, 0);
1239# elif defined POWERPC
1240 static bool old_kernel = 0;
1241 if (old_kernel)
1242 goto old;
1243 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1244 if (get_regs_error && errno == EIO) {
1245 old_kernel = 1;
1246 old:
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001247 get_regs_error = getregs_old(pid);
Dmitry V. Levin7abf2e82015-02-13 23:56:54 +00001248 }
1249# else
1250 /* Assume that PTRACE_GETREGS works. */
1251 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1252# endif
1253
1254#else /* !ARCH_REGS_FOR_GETREGSET && !ARCH_REGS_FOR_GETREGS */
1255# warning get_regs is not implemented for this architecture yet
1256 get_regs_error = 0;
1257#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001258}
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001259
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001260/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001261 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1262 * 1: ok, continue in trace_syscall_entering().
1263 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001264 * ("????" etc) and bail out.
1265 */
Denys Vlasenko8497b622015-03-21 17:51:52 +01001266int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001267get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001268{
Dmitry V. Levin144cda22015-03-23 18:48:32 +00001269 if (get_regs_error)
1270 return -1;
1271
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001272 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001273
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001274#include "get_scno.c"
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001275
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001276 tcp->scno = scno;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001277 if (SCNO_IS_VALID(tcp->scno)) {
1278 tcp->s_ent = &sysent[scno];
1279 tcp->qual_flg = qual_flags[scno];
1280 } else {
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01001281 static const struct_sysent unknown = {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001282 .nargs = MAX_ARGS,
1283 .sys_flags = 0,
1284 .sys_func = printargs,
Denys Vlasenkocc59f142015-04-07 12:46:59 +02001285 .sys_name = "system call",
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001286 };
1287 tcp->s_ent = &unknown;
1288 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
Dmitry V. Levinea009002015-03-24 01:59:07 +00001289 if (debug_flag)
Dmitry V. Levin6c8ef052015-05-25 22:51:19 +00001290 error_msg("pid %d invalid syscall %ld", tcp->pid, scno);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001291 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001292 return 1;
1293}
1294
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001295/* Return -1 on error or 1 on success (never 0!) */
Roland McGratha4d48532005-06-08 20:45:28 +00001296static int
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001297get_syscall_args(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001298{
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001299#include "get_syscall_args.c"
Pavel Machek4dc3b142000-02-01 17:58:41 +00001300 return 1;
1301}
1302
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01001303static void
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001304get_error(struct tcb *tcp)
1305{
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001306 const bool check_errno = !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS);
1307 tcp->u_error = 0;
Dmitry V. Levinf46ab5f2015-02-05 18:50:24 +00001308
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001309#include "get_error.c"
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001310}
1311
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001312/* Returns:
1313 * 1: ok, continue in trace_syscall_exiting().
1314 * -1: error, trace_syscall_exiting() should print error indicator
1315 * ("????" etc) and bail out.
1316 */
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001317static int
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001318get_syscall_result(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001319{
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001320#if defined ARCH_REGS_FOR_GETREGSET || defined ARCH_REGS_FOR_GETREGS
1321 /* already done by get_regs */
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001322#else
Dmitry V. Levind70d1c42015-03-22 22:13:55 +00001323# include "get_syscall_result.c"
Masatake YAMATOed69fc22014-04-16 15:33:35 +09001324#endif
Dmitry V. Levin1b786072015-03-22 18:09:55 +00001325 get_error(tcp);
1326 return 1;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00001327}