blob: 3566a013f01a85d1bfde8e50886b93e790129be2 [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/user.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000036#include <sys/param.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000037
Wichert Akkerman15dea971999-10-06 13:06:34 +000038#ifdef HAVE_SYS_REG_H
Denys Vlasenko523635f2012-02-25 02:44:25 +010039# include <sys/reg.h>
40# ifndef PTRACE_PEEKUSR
41# define PTRACE_PEEKUSR PTRACE_PEEKUSER
42# endif
Wichert Akkermanfaf72222000-02-19 23:59:03 +000043#elif defined(HAVE_LINUX_PTRACE_H)
Denys Vlasenko523635f2012-02-25 02:44:25 +010044# undef PTRACE_SYSCALL
Roland McGrathce9f0742004-03-01 21:29:22 +000045# ifdef HAVE_STRUCT_IA64_FPREG
46# define ia64_fpreg XXX_ia64_fpreg
47# endif
48# ifdef HAVE_STRUCT_PT_ALL_USER_REGS
49# define pt_all_user_regs XXX_pt_all_user_regs
50# endif
Denys Vlasenko523635f2012-02-25 02:44:25 +010051# include <linux/ptrace.h>
Roland McGrathce9f0742004-03-01 21:29:22 +000052# undef ia64_fpreg
53# undef pt_all_user_regs
Wichert Akkerman15dea971999-10-06 13:06:34 +000054#endif
55
Denys Vlasenko84703742012-02-25 02:38:52 +010056#if defined(SPARC64)
Roland McGrath6d1a65c2004-07-12 07:44:08 +000057# undef PTRACE_GETREGS
58# define PTRACE_GETREGS PTRACE_GETREGS64
59# undef PTRACE_SETREGS
60# define PTRACE_SETREGS PTRACE_SETREGS64
Denys Vlasenko84703742012-02-25 02:38:52 +010061#endif
Roland McGrath6d1a65c2004-07-12 07:44:08 +000062
Denys Vlasenko84703742012-02-25 02:38:52 +010063#if defined(IA64)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000064# include <asm/ptrace_offsets.h>
65# include <asm/rse.h>
66#endif
67
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010068#if defined(X86_64) || defined(X32)
69# include <linux/ptrace.h>
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +010070# include <sys/uio.h>
71# include <elf.h>
72#endif
73
Steve McIntyred8d3bd32012-10-24 17:58:16 +010074#if defined(AARCH64)
75# include <asm/ptrace.h>
76# include <sys/uio.h>
77# include <elf.h>
78#endif
79
Christian Svensson492f81f2013-02-14 13:26:27 +010080#if defined(OR1K)
81# include <sys/uio.h>
82# include <elf.h>
83#endif
84
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000085#ifndef ERESTARTSYS
Denys Vlasenko523635f2012-02-25 02:44:25 +010086# define ERESTARTSYS 512
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000087#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010088#ifndef ERESTARTNOINTR
Denys Vlasenko523635f2012-02-25 02:44:25 +010089# define ERESTARTNOINTR 513
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000090#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010091#ifndef ERESTARTNOHAND
92# define ERESTARTNOHAND 514 /* restart if no handler */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000093#endif
Denys Vlasenko3da96932012-03-17 03:17:15 +010094#ifndef ERESTART_RESTARTBLOCK
Denys Vlasenko523635f2012-02-25 02:44:25 +010095# define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
Roland McGrath9c555e72003-07-09 09:47:59 +000096#endif
Denys Vlasenko523635f2012-02-25 02:44:25 +010097
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000098#ifndef NSIG
Denys Vlasenko523635f2012-02-25 02:44:25 +010099# warning: NSIG is not defined, using 32
100# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000101#endif
102#ifdef ARM
Denys Vlasenko041b3ee2011-08-18 12:48:56 +0200103/* Ugh. Is this really correct? ARM has no RT signals?! */
Denys Vlasenko523635f2012-02-25 02:44:25 +0100104# undef NSIG
105# define NSIG 32
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000106#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000107
108#include "syscall.h"
109
110/* Define these shorthand notations to simplify the syscallent files. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000111#define TD TRACE_DESC
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000112#define TF TRACE_FILE
113#define TI TRACE_IPC
114#define TN TRACE_NETWORK
115#define TP TRACE_PROCESS
116#define TS TRACE_SIGNAL
Namhyung Kim96792962012-10-24 11:41:57 +0900117#define TM TRACE_MEMORY
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000118#define NF SYSCALL_NEVER_FAILS
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200119#define MA MAX_ARGS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000120
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100121const struct_sysent sysent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000122#include "syscallent.h"
123};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000124
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100125#if SUPPORTED_PERSONALITIES > 1
126static const struct_sysent sysent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100127# include "syscallent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000128};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200129#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000130
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100131#if SUPPORTED_PERSONALITIES > 2
132static const struct_sysent sysent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100133# include "syscallent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000134};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200135#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000136
137/* Now undef them since short defines cause wicked namespace pollution. */
Roland McGrath2fe7b132005-07-05 03:25:35 +0000138#undef TD
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000139#undef TF
140#undef TI
141#undef TN
142#undef TP
143#undef TS
Namhyung Kim96792962012-10-24 11:41:57 +0900144#undef TM
Dmitry V. Levin50a218d2011-01-18 17:36:20 +0000145#undef NF
Denys Vlasenkoac1ce772011-08-23 13:24:17 +0200146#undef MA
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000147
Denys Vlasenko39fca622011-08-20 02:12:33 +0200148/*
149 * `ioctlent.h' may be generated from `ioctlent.raw' by the auxiliary
150 * program `ioctlsort', such that the list is sorted by the `code' field.
151 * This has the side-effect of resolving the _IO.. macros into
152 * plain integers, eliminating the need to include here everything
153 * in "/usr/include".
154 */
155
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100156const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000157#include "errnoent.h"
158};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100159const char *const signalent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200160#include "signalent.h"
161};
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100162const struct_ioctlent ioctlent0[] = {
Denys Vlasenko39fca622011-08-20 02:12:33 +0200163#include "ioctlent.h"
164};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000165
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100166#if SUPPORTED_PERSONALITIES > 1
Roland McGrathee36ce12004-09-04 03:53:10 +0000167static const char *const errnoent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100168# include "errnoent1.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000169};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200170static const char *const signalent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100171# include "signalent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200172};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100173static const struct_ioctlent ioctlent1[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100174# include "ioctlent1.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200175};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200176#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000177
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100178#if SUPPORTED_PERSONALITIES > 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000179static const char *const errnoent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100180# include "errnoent2.h"
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000181};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200182static const char *const signalent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100183# include "signalent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200184};
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100185static const struct_ioctlent ioctlent2[] = {
Denys Vlasenko523635f2012-02-25 02:44:25 +0100186# include "ioctlent2.h"
Denys Vlasenko39fca622011-08-20 02:12:33 +0200187};
Denys Vlasenko39fca622011-08-20 02:12:33 +0200188#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000189
Dmitry V. Levine6f55242013-02-26 22:02:30 +0000190enum {
191 nsyscalls0 = ARRAY_SIZE(sysent0)
192#if SUPPORTED_PERSONALITIES > 1
193 , nsyscalls1 = ARRAY_SIZE(sysent1)
194# if SUPPORTED_PERSONALITIES > 2
195 , nsyscalls2 = ARRAY_SIZE(sysent2)
196# endif
197#endif
198};
199
200enum {
201 nerrnos0 = ARRAY_SIZE(errnoent0)
202#if SUPPORTED_PERSONALITIES > 1
203 , nerrnos1 = ARRAY_SIZE(errnoent1)
204# if SUPPORTED_PERSONALITIES > 2
205 , nerrnos2 = ARRAY_SIZE(errnoent2)
206# endif
207#endif
208};
209
210enum {
211 nsignals0 = ARRAY_SIZE(signalent0)
212#if SUPPORTED_PERSONALITIES > 1
213 , nsignals1 = ARRAY_SIZE(signalent1)
214# if SUPPORTED_PERSONALITIES > 2
215 , nsignals2 = ARRAY_SIZE(signalent2)
216# endif
217#endif
218};
219
220enum {
221 nioctlents0 = ARRAY_SIZE(ioctlent0)
222#if SUPPORTED_PERSONALITIES > 1
223 , nioctlents1 = ARRAY_SIZE(ioctlent1)
224# if SUPPORTED_PERSONALITIES > 2
225 , nioctlents2 = ARRAY_SIZE(ioctlent2)
226# endif
227#endif
228};
229
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100230#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100231const struct_sysent *sysent = sysent0;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100232const char *const *errnoent = errnoent0;
233const char *const *signalent = signalent0;
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100234const struct_ioctlent *ioctlent = ioctlent0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100235#endif
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100236unsigned nsyscalls = nsyscalls0;
237unsigned nerrnos = nerrnos0;
238unsigned nsignals = nsignals0;
239unsigned nioctlents = nioctlents0;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100240
241unsigned num_quals;
242qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
243
244static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
245 nsyscalls0,
246#if SUPPORTED_PERSONALITIES > 1
247 nsyscalls1,
248#endif
249#if SUPPORTED_PERSONALITIES > 2
250 nsyscalls2,
251#endif
252};
253static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
254 sysent0,
255#if SUPPORTED_PERSONALITIES > 1
256 sysent1,
257#endif
258#if SUPPORTED_PERSONALITIES > 2
259 sysent2,
260#endif
261};
262
263enum {
264 MAX_NSYSCALLS1 = (nsyscalls0
265#if SUPPORTED_PERSONALITIES > 1
266 > nsyscalls1 ? nsyscalls0 : nsyscalls1
267#endif
268 ),
269 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
270#if SUPPORTED_PERSONALITIES > 2
271 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
272#endif
273 ),
274 MAX_NSYSCALLS = MAX_NSYSCALLS2,
275 /* We are ready for arches with up to 255 signals,
276 * even though the largest known signo is on MIPS and it is 128.
277 * The number of existing syscalls on all arches is
278 * larger that 255 anyway, so it is just a pedantic matter.
279 */
280 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
281};
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000282
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100283#if SUPPORTED_PERSONALITIES > 1
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100284unsigned current_personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000285
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100286# ifndef current_wordsize
287unsigned current_wordsize;
288static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000289 PERSONALITY0_WORDSIZE,
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000290 PERSONALITY1_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100291# if SUPPORTED_PERSONALITIES > 2
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000292 PERSONALITY2_WORDSIZE,
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100293# endif
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200294};
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100295# endif
Roland McGrath4b2dcca2006-01-12 10:18:53 +0000296
Denys Vlasenko5c774b22011-08-20 01:50:09 +0200297void
Dmitry V. Levin3abe8b22006-12-20 22:37:21 +0000298set_personality(int personality)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000299{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100300 nsyscalls = nsyscall_vec[personality];
301 sysent = sysent_vec[personality];
302
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000303 switch (personality) {
304 case 0:
305 errnoent = errnoent0;
306 nerrnos = nerrnos0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000307 ioctlent = ioctlent0;
308 nioctlents = nioctlents0;
309 signalent = signalent0;
310 nsignals = nsignals0;
311 break;
312
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000313 case 1:
314 errnoent = errnoent1;
315 nerrnos = nerrnos1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000316 ioctlent = ioctlent1;
317 nioctlents = nioctlents1;
318 signalent = signalent1;
319 nsignals = nsignals1;
320 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000321
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +0100322# if SUPPORTED_PERSONALITIES > 2
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000323 case 2:
324 errnoent = errnoent2;
325 nerrnos = nerrnos2;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000326 ioctlent = ioctlent2;
327 nioctlents = nioctlents2;
328 signalent = signalent2;
329 nsignals = nsignals2;
330 break;
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100331# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000332 }
333
334 current_personality = personality;
Denys Vlasenkoae8643e2013-02-15 14:55:14 +0100335# ifndef current_wordsize
336 current_wordsize = personality_wordsize[personality];
337# endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000338}
339
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000340static void
341update_personality(struct tcb *tcp, int personality)
342{
343 if (personality == current_personality)
344 return;
345 set_personality(personality);
346
347 if (personality == tcp->currpers)
348 return;
349 tcp->currpers = personality;
350
H.J. Lu35be5812012-04-16 13:00:01 +0200351# if defined(POWERPC64)
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000352 if (!qflag) {
353 static const char *const names[] = {"64 bit", "32 bit"};
354 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
355 tcp->pid, names[personality]);
356 }
H.J. Lu35be5812012-04-16 13:00:01 +0200357# elif defined(X86_64)
358 if (!qflag) {
359 static const char *const names[] = {"64 bit", "32 bit", "x32"};
360 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
361 tcp->pid, names[personality]);
362 }
H.J. Lu085e4282012-04-17 11:05:04 -0700363# elif defined(X32)
364 if (!qflag) {
365 static const char *const names[] = {"x32", "32 bit"};
366 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
367 tcp->pid, names[personality]);
368 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000369# elif defined(AARCH64)
370 if (!qflag) {
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100371 static const char *const names[] = {"32-bit", "AArch64"};
Steve McIntyre890a5ca2012-11-10 11:24:48 +0000372 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
373 tcp->pid, names[personality]);
374 }
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100375# elif defined(TILE)
376 if (!qflag) {
377 static const char *const names[] = {"64-bit", "32-bit"};
378 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
379 tcp->pid, names[personality]);
380 }
Denys Vlasenko523635f2012-02-25 02:44:25 +0100381# endif
Dmitry V. Levina5a839a2011-12-23 00:50:49 +0000382}
383#endif
Roland McGrathe10e62a2004-09-04 04:20:43 +0000384
Denys Vlasenkoc1540fe2013-02-21 16:17:08 +0100385static int qual_syscall(), qual_signal(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000386
Roland McGrathe10e62a2004-09-04 04:20:43 +0000387static const struct qual_options {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000388 int bitflag;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000389 const char *option_name;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000390 int (*qualify)(const char *, int, int);
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000391 const char *argument_name;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000392} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000393 { QUAL_TRACE, "trace", qual_syscall, "system call" },
394 { QUAL_TRACE, "t", qual_syscall, "system call" },
395 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
396 { QUAL_ABBREV, "a", qual_syscall, "system call" },
397 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
398 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
399 { QUAL_RAW, "raw", qual_syscall, "system call" },
400 { QUAL_RAW, "x", qual_syscall, "system call" },
401 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
402 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
403 { QUAL_SIGNAL, "s", qual_signal, "signal" },
Roland McGrath9797ceb2002-12-30 10:23:00 +0000404 { QUAL_READ, "read", qual_desc, "descriptor" },
405 { QUAL_READ, "reads", qual_desc, "descriptor" },
406 { QUAL_READ, "r", qual_desc, "descriptor" },
407 { QUAL_WRITE, "write", qual_desc, "descriptor" },
408 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
409 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000410 { 0, NULL, NULL, NULL },
411};
412
Roland McGrath9797ceb2002-12-30 10:23:00 +0000413static void
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100414reallocate_qual(int n)
415{
416 unsigned p;
417 qualbits_t *qp;
418 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
419 qp = qual_vec[p] = realloc(qual_vec[p], n * sizeof(qualbits_t));
420 if (!qp)
421 die_out_of_memory();
422 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
423 }
424 num_quals = n;
425}
426
427static void
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000428qualify_one(int n, int bitflag, int not, int pers)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000429{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100430 unsigned p;
Roland McGrath138c6a32006-01-12 09:50:49 +0000431
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100432 if (num_quals <= n)
433 reallocate_qual(n + 1);
Roland McGrath138c6a32006-01-12 09:50:49 +0000434
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100435 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
436 if (pers == p || pers < 0) {
437 if (not)
438 qual_vec[p][n] &= ~bitflag;
439 else
440 qual_vec[p][n] |= bitflag;
441 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000442 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000443}
444
445static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000446qual_syscall(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000447{
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100448 unsigned p;
449 unsigned i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000450 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000451
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100452 if (*s >= '0' && *s <= '9') {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100453 i = string_to_uint(s);
Denys Vlasenkob43dacd2013-02-23 18:19:28 +0100454 if (i >= MAX_NSYSCALLS)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000455 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000456 qualify_one(i, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000457 return 0;
Roland McGrath48a035f2006-01-12 09:45:56 +0000458 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000459
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100460 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
461 for (i = 0; i < nsyscall_vec[p]; i++) {
462 if (sysent_vec[p][i].sys_name
463 && strcmp(s, sysent_vec[p][i].sys_name) == 0
464 ) {
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000465 qualify_one(i, bitflag, not, p);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100466 rc = 0;
467 }
Roland McGrath138c6a32006-01-12 09:50:49 +0000468 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100469 }
Dmitry V. Levinc18c7032007-08-22 21:43:30 +0000470
Roland McGrathfe6b3522005-02-02 04:40:11 +0000471 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000472}
473
474static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000475qual_signal(const char *s, int bitflag, int not)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000476{
477 int i;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000478
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100479 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000480 int signo = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100481 if (signo < 0 || signo > 255)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000482 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000483 qualify_one(signo, bitflag, not, -1);
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +0000484 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000485 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000486 if (strncasecmp(s, "SIG", 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000487 s += 3;
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100488 for (i = 0; i <= NSIG; i++) {
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000489 if (strcasecmp(s, signame(i) + 3) == 0) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000490 qualify_one(i, bitflag, not, -1);
Roland McGrath76421df2005-02-02 03:51:18 +0000491 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000492 }
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100493 }
Roland McGrath76421df2005-02-02 03:51:18 +0000494 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000495}
496
497static int
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000498qual_desc(const char *s, int bitflag, int not)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000499{
Denys Vlasenkoe4cc7c52012-03-23 11:29:01 +0100500 if (*s >= '0' && *s <= '9') {
Dmitry V. Levinccee1692012-03-25 21:49:48 +0000501 int desc = string_to_uint(s);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100502 if (desc < 0 || desc > 0x7fff) /* paranoia */
Roland McGrathfe6b3522005-02-02 04:40:11 +0000503 return -1;
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000504 qualify_one(desc, bitflag, not, -1);
Roland McGrath2b619022003-04-10 18:58:20 +0000505 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000506 }
507 return -1;
508}
509
510static int
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000511lookup_class(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000512{
513 if (strcmp(s, "file") == 0)
514 return TRACE_FILE;
515 if (strcmp(s, "ipc") == 0)
516 return TRACE_IPC;
517 if (strcmp(s, "network") == 0)
518 return TRACE_NETWORK;
519 if (strcmp(s, "process") == 0)
520 return TRACE_PROCESS;
521 if (strcmp(s, "signal") == 0)
522 return TRACE_SIGNAL;
Roland McGrath2fe7b132005-07-05 03:25:35 +0000523 if (strcmp(s, "desc") == 0)
524 return TRACE_DESC;
Namhyung Kim96792962012-10-24 11:41:57 +0900525 if (strcmp(s, "memory") == 0)
526 return TRACE_MEMORY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000527 return -1;
528}
529
530void
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000531qualify(const char *s)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000532{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000533 const struct qual_options *opt;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000534 int not;
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000535 char *copy;
536 const char *p;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000537 int i, n;
538
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100539 if (num_quals == 0)
540 reallocate_qual(MIN_QUALS);
541
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000542 opt = &qual_options[0];
543 for (i = 0; (p = qual_options[i].option_name); i++) {
544 n = strlen(p);
545 if (strncmp(s, p, n) == 0 && s[n] == '=') {
546 opt = &qual_options[i];
547 s += n + 1;
548 break;
549 }
550 }
551 not = 0;
552 if (*s == '!') {
553 not = 1;
554 s++;
555 }
556 if (strcmp(s, "none") == 0) {
557 not = 1 - not;
558 s = "all";
559 }
560 if (strcmp(s, "all") == 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100561 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000562 qualify_one(i, opt->bitflag, not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000563 }
564 return;
565 }
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100566 for (i = 0; i < num_quals; i++) {
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000567 qualify_one(i, opt->bitflag, !not, -1);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000568 }
Denys Vlasenko5d645812011-08-20 12:48:18 +0200569 copy = strdup(s);
Denys Vlasenko1d46ba52011-08-31 14:00:02 +0200570 if (!copy)
571 die_out_of_memory();
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000572 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000573 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100574 unsigned pers;
575 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
576 for (i = 0; i < nsyscall_vec[pers]; i++)
577 if (sysent_vec[pers][i].sys_flags & n)
Dmitry V. Levin7b9e45e2013-03-01 15:50:22 +0000578 qualify_one(i, opt->bitflag, not, pers);
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +0100579 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000580 continue;
581 }
Dmitry V. Levin35d05722010-09-15 16:18:20 +0000582 if (opt->qualify(p, opt->bitflag, not)) {
Denys Vlasenko4c65c442012-03-08 11:54:10 +0100583 error_msg_and_die("invalid %s '%s'",
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000584 opt->argument_name, p);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000585 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000586 }
Dmitry V. Levin30145dd2010-09-06 22:08:24 +0000587 free(copy);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000588 return;
589}
590
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000591#ifdef SYS_socket_subcall
Roland McGratha4d48532005-06-08 20:45:28 +0000592static void
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000593decode_socket_subcall(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000594{
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000595 unsigned long addr;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100596 unsigned int i, n, size;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000597
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000598 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
599 return;
600
601 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100602 tcp->qual_flg = qual_flags[tcp->scno];
603 tcp->s_ent = &sysent[tcp->scno];
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000604 addr = tcp->u_arg[1];
Denys Vlasenko9fd4f962012-03-19 09:36:42 +0100605 size = current_wordsize;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100606 n = tcp->s_ent->nargs;
607 for (i = 0; i < n; ++i) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000608 if (size == sizeof(int)) {
609 unsigned int arg;
610 if (umove(tcp, addr, &arg) < 0)
611 arg = 0;
612 tcp->u_arg[i] = arg;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000613 }
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000614 else {
615 unsigned long arg;
616 if (umove(tcp, addr, &arg) < 0)
617 arg = 0;
618 tcp->u_arg[i] = arg;
619 }
620 addr += size;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000621 }
622}
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000623#endif
Mike Frysinger3362e892012-03-15 01:09:19 -0400624
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000625#ifdef SYS_ipc_subcall
626static void
627decode_ipc_subcall(struct tcb *tcp)
628{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100629 unsigned int i, n;
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000630
631 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
632 return;
633
634 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100635 tcp->qual_flg = qual_flags[tcp->scno];
636 tcp->s_ent = &sysent[tcp->scno];
637 n = tcp->s_ent->nargs;
638 for (i = 0; i < n; i++)
Dmitry V. Levin648c22c2012-03-15 22:08:55 +0000639 tcp->u_arg[i] = tcp->u_arg[i + 1];
640}
641#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000642
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200643int
644printargs(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000645{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200646 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 Vlasenkoa6146922011-08-24 18:07:22 +0200650 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
651 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000652 return 0;
653}
654
Denys Vlasenko72879c62012-02-27 14:18:02 +0100655int
656printargs_lu(struct tcb *tcp)
657{
658 if (entering(tcp)) {
659 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100660 int n = tcp->s_ent->nargs;
661 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100662 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
663 }
664 return 0;
665}
666
667int
668printargs_ld(struct tcb *tcp)
669{
670 if (entering(tcp)) {
671 int i;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +0100672 int n = tcp->s_ent->nargs;
673 for (i = 0; i < n; i++)
Denys Vlasenko72879c62012-02-27 14:18:02 +0100674 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
675 }
676 return 0;
677}
678
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100679#if defined(SPARC) || defined(SPARC64) || defined(IA64) || defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200680long
681getrval2(struct tcb *tcp)
682{
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100683 long val;
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200684
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100685# if defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100686 val = sparc_regs.u_regs[U_REG_O1];
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100687# elif defined(SH)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200688 if (upeek(tcp, 4*(REG_REG0+1), &val) < 0)
689 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100690# elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200691 if (upeek(tcp, PT_R9, &val) < 0)
692 return -1;
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100693# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200694
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200695 return val;
696}
Denys Vlasenko1ebe08d2013-02-05 16:55:23 +0100697#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200698
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200699int
700is_restart_error(struct tcb *tcp)
701{
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200702 switch (tcp->u_error) {
703 case ERESTARTSYS:
704 case ERESTARTNOINTR:
705 case ERESTARTNOHAND:
706 case ERESTART_RESTARTBLOCK:
707 return 1;
708 default:
709 break;
710 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +0200711 return 0;
712}
713
Denys Vlasenko523635f2012-02-25 02:44:25 +0100714#if defined(I386)
Denys Vlasenko2550d482013-02-15 21:04:28 +0100715struct user_regs_struct i386_regs;
H.J. Lu35be5812012-04-16 13:00:01 +0200716#elif defined(X86_64) || defined(X32)
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100717/*
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100718 * On i386, pt_regs and user_regs_struct are the same,
719 * but on 64 bit x86, user_regs_struct has six more fields:
Denys Vlasenkoe73a89d2012-01-18 11:07:24 +0100720 * fs_base, gs_base, ds, es, fs, gs.
721 * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
722 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100723struct i386_user_regs_struct {
724 uint32_t ebx;
725 uint32_t ecx;
726 uint32_t edx;
727 uint32_t esi;
728 uint32_t edi;
729 uint32_t ebp;
730 uint32_t eax;
731 uint32_t xds;
732 uint32_t xes;
733 uint32_t xfs;
734 uint32_t xgs;
735 uint32_t orig_eax;
736 uint32_t eip;
737 uint32_t xcs;
738 uint32_t eflags;
739 uint32_t esp;
740 uint32_t xss;
741};
742static union {
743 struct user_regs_struct x86_64_r;
744 struct i386_user_regs_struct i386_r;
745} x86_regs_union;
746# define x86_64_regs x86_regs_union.x86_64_r
747# define i386_regs x86_regs_union.i386_r
748static struct iovec x86_io = {
749 .iov_base = &x86_regs_union
750};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100751#elif defined(IA64)
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200752long ia32 = 0; /* not static */
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100753static long ia64_r8, ia64_r10;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100754#elif defined(POWERPC)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +0100755static long ppc_result;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100756#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100757static long m68k_d0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100758#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100759static long bfin_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100760#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100761struct pt_regs arm_regs; /* not static */
Steve McIntyred8d3bd32012-10-24 17:58:16 +0100762#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100763static union {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100764 struct user_pt_regs aarch64_r;
765 struct arm_pt_regs arm_r;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100766} arm_regs_union;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100767# define aarch64_regs arm_regs_union.aarch64_r
768# define arm_regs arm_regs_union.arm_r
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100769static struct iovec aarch64_io = {
Denys Vlasenko59aea0a2013-02-11 12:29:36 +0100770 .iov_base = &arm_regs_union
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100771};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100772#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100773static long alpha_r0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100774static long alpha_a3;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100775#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100776static struct pt_regs avr32_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100777#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100778struct pt_regs sparc_regs; /* not static */
Denys Vlasenko523635f2012-02-25 02:44:25 +0100779#elif defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100780static long long mips_a3;
781static long long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100782#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100783static long mips_a3;
784static long mips_r2;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100785#elif defined(S390) || defined(S390X)
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200786static long gpr2;
Denys Vlasenkob63256e2011-06-07 12:13:24 +0200787static long syscall_mode;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100788#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100789static long hppa_r28;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100790#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +0100791static long sh_r0;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100792#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100793static long sh64_r9;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100794#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100795static long cris_r10;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100796#elif defined(TILE)
797struct pt_regs tile_regs;
Denys Vlasenko523635f2012-02-25 02:44:25 +0100798#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +0100799static long microblaze_r3;
Christian Svensson492f81f2013-02-14 13:26:27 +0100800#elif defined(OR1K)
801static struct user_regs_struct or1k_regs;
802static struct iovec or1k_io = {
803 .iov_base = &or1k_regs
804};
Denys Vlasenko523635f2012-02-25 02:44:25 +0100805#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000806
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100807void
808printcall(struct tcb *tcp)
809{
810#define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
811 sizeof(long) == 8 ? "[????????????????] " : \
812 NULL /* crash */)
813 if (get_regs_error) {
814 PRINTBADPC;
815 return;
816 }
817#if defined(I386)
818 tprintf("[%08lx] ", i386_regs.eip);
819#elif defined(S390) || defined(S390X)
820 long psw;
821 if (upeek(tcp, PT_PSWADDR, &psw) < 0) {
822 PRINTBADPC;
823 return;
824 }
825# ifdef S390
826 tprintf("[%08lx] ", psw);
827# elif S390X
Dmitry V. Levinddba73e2013-02-05 19:01:58 +0000828 tprintf("[%016lx] ", psw);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100829# endif
830#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +0100831 if (x86_io.iov_len == sizeof(i386_regs)) {
832 tprintf("[%08x] ", (unsigned) i386_regs.eip);
833 } else {
834# if defined(X86_64)
835 tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
836# elif defined(X32)
837 /* Note: this truncates 64-bit rip to 32 bits */
838 tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip);
839# endif
840 }
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100841#elif defined(IA64)
842 long ip;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100843 if (upeek(tcp, PT_B0, &ip) < 0) {
844 PRINTBADPC;
845 return;
846 }
847 tprintf("[%08lx] ", ip);
848#elif defined(POWERPC)
849 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100850 if (upeek(tcp, sizeof(unsigned long)*PT_NIP, &pc) < 0) {
851 PRINTBADPC;
852 return;
853 }
854# ifdef POWERPC64
855 tprintf("[%016lx] ", pc);
856# else
857 tprintf("[%08lx] ", pc);
858# endif
859#elif defined(M68K)
860 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100861 if (upeek(tcp, 4*PT_PC, &pc) < 0) {
862 tprints("[????????] ");
863 return;
864 }
865 tprintf("[%08lx] ", pc);
866#elif defined(ALPHA)
867 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100868 if (upeek(tcp, REG_PC, &pc) < 0) {
869 tprints("[????????????????] ");
870 return;
871 }
872 tprintf("[%08lx] ", pc);
873#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100874 tprintf("[%08lx] ", sparc_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100875#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100876 tprintf("[%08lx] ", sparc_regs.tpc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100877#elif defined(HPPA)
878 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100879 if (upeek(tcp, PT_IAOQ0, &pc) < 0) {
880 tprints("[????????] ");
881 return;
882 }
883 tprintf("[%08lx] ", pc);
884#elif defined(MIPS)
885 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100886 if (upeek(tcp, REG_EPC, &pc) < 0) {
887 tprints("[????????] ");
888 return;
889 }
890 tprintf("[%08lx] ", pc);
891#elif defined(SH)
892 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100893 if (upeek(tcp, 4*REG_PC, &pc) < 0) {
894 tprints("[????????] ");
895 return;
896 }
897 tprintf("[%08lx] ", pc);
898#elif defined(SH64)
899 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100900 if (upeek(tcp, REG_PC, &pc) < 0) {
901 tprints("[????????????????] ");
902 return;
903 }
904 tprintf("[%08lx] ", pc);
905#elif defined(ARM)
Denys Vlasenko401374e2013-02-06 18:24:39 +0100906 tprintf("[%08lx] ", arm_regs.ARM_pc);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +0100907#elif defined(AARCH64)
908 /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100909#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100910 tprintf("[%08lx] ", avr32_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100911#elif defined(BFIN)
912 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100913 if (upeek(tcp, PT_PC, &pc) < 0) {
914 PRINTBADPC;
915 return;
916 }
917 tprintf("[%08lx] ", pc);
918#elif defined(CRISV10)
919 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100920 if (upeek(tcp, 4*PT_IRP, &pc) < 0) {
921 PRINTBADPC;
922 return;
923 }
924 tprintf("[%08lx] ", pc);
925#elif defined(CRISV32)
926 long pc;
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100927 if (upeek(tcp, 4*PT_ERP, &pc) < 0) {
928 PRINTBADPC;
929 return;
930 }
931 tprintf("[%08lx] ", pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100932#elif defined(TILE)
933# ifdef _LP64
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500934 tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100935# else
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -0500936 tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +0100937# endif
Christian Svensson492f81f2013-02-14 13:26:27 +0100938#elif defined(OR1K)
939 tprintf("[%08lx] ", or1k_regs.pc);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100940#endif /* architecture */
941}
942
Denys Vlasenko7270de52013-02-21 15:46:34 +0100943/* Shuffle syscall numbers so that we don't have huge gaps in syscall table.
944 * The shuffling should be reversible: shuffle_scno(shuffle_scno(n)) == n.
945 */
946#if defined(ARM) /* So far only ARM needs this */
947static long
948shuffle_scno(unsigned long scno)
949{
950 if (scno <= ARM_LAST_ORDINARY_SYSCALL)
951 return scno;
952
953 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
954 if (scno == 0x000ffff0)
955 return ARM_LAST_ORDINARY_SYSCALL+1;
956 if (scno == ARM_LAST_ORDINARY_SYSCALL+1)
957 return 0x000ffff0;
958
959 /* Is it ARM specific syscall?
960 * Swap with [LAST_ORDINARY+2, LAST_ORDINARY+2 + LAST_SPECIAL] range.
961 */
962 if (scno >= 0x000f0000
963 && scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL
964 ) {
965 return scno - 0x000f0000 + (ARM_LAST_ORDINARY_SYSCALL+2);
966 }
967 if (/* scno >= ARM_LAST_ORDINARY_SYSCALL+2 - always true */ 1
968 && scno <= (ARM_LAST_ORDINARY_SYSCALL+2) + ARM_LAST_SPECIAL_SYSCALL
969 ) {
970 return scno + 0x000f0000 - (ARM_LAST_ORDINARY_SYSCALL+2);
971 }
972
973 return scno;
974}
975#else
976# define shuffle_scno(scno) ((long)(scno))
977#endif
978
979static char*
980undefined_scno_name(struct tcb *tcp)
981{
982 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
983
984 sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
985 return buf;
986}
987
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100988#ifndef get_regs
989long get_regs_error;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100990void
991get_regs(pid_t pid)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100992{
993# if defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +0100994 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +0100995# elif defined(I386)
996 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &i386_regs);
997# elif defined(X86_64) || defined(X32)
Denys Vlasenkoe3b248d2013-02-15 00:24:19 +0100998 /*
999 * PTRACE_GETREGSET was introduced in 2.6.33.
1000 * Let's be paranoid and require a bit later kernel.
1001 */
1002 if (os_release >= KERNEL_VERSION(2,6,35)) {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001003 /*x86_io.iov_base = &x86_regs_union; - already is */
1004 x86_io.iov_len = sizeof(x86_regs_union);
1005 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (long) &x86_io);
1006 } else {
1007 /* Use old method, with heuristical detection of 32-bitness */
1008 x86_io.iov_len = sizeof(x86_64_regs);
1009 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &x86_64_regs);
1010 if (!get_regs_error && x86_64_regs.cs == 0x23) {
1011 x86_io.iov_len = sizeof(i386_regs);
1012 /*
1013 * The order is important: i386_regs and x86_64_regs
1014 * are overlaid in memory!
1015 */
1016 i386_regs.ebx = x86_64_regs.rbx;
1017 i386_regs.ecx = x86_64_regs.rcx;
1018 i386_regs.edx = x86_64_regs.rdx;
1019 i386_regs.esi = x86_64_regs.rsi;
1020 i386_regs.edi = x86_64_regs.rdi;
1021 i386_regs.ebp = x86_64_regs.rbp;
1022 i386_regs.eax = x86_64_regs.rax;
1023 /*i386_regs.xds = x86_64_regs.ds; unused by strace */
1024 /*i386_regs.xes = x86_64_regs.es; ditto... */
1025 /*i386_regs.xfs = x86_64_regs.fs;*/
1026 /*i386_regs.xgs = x86_64_regs.gs;*/
1027 i386_regs.orig_eax = x86_64_regs.orig_rax;
1028 i386_regs.eip = x86_64_regs.rip;
1029 /*i386_regs.xcs = x86_64_regs.cs;*/
1030 /*i386_regs.eflags = x86_64_regs.eflags;*/
1031 i386_regs.esp = x86_64_regs.rsp;
1032 /*i386_regs.xss = x86_64_regs.ss;*/
1033 }
1034 }
Denys Vlasenko401374e2013-02-06 18:24:39 +01001035# elif defined(ARM)
1036 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (void *)&arm_regs);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001037# elif defined(AARCH64)
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001038 /*aarch64_io.iov_base = &arm_regs_union; - already is */
1039 aarch64_io.iov_len = sizeof(arm_regs_union);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001040 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, (void *)&aarch64_io);
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001041# if 0
1042 /* Paranoia checks */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001043 if (get_regs_error)
1044 return;
1045 switch (aarch64_io.iov_len) {
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001046 case sizeof(aarch64_regs):
1047 /* We are in 64-bit mode */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001048 break;
1049 case sizeof(arm_regs):
1050 /* We are in 32-bit mode */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001051 break;
1052 default:
1053 get_regs_error = -1;
1054 break;
1055 }
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001056# endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001057# elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001058 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001059# elif defined(TILE)
Chris Metcalfaf8dc6b2013-02-05 13:02:42 -05001060 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &tile_regs);
Christian Svensson492f81f2013-02-14 13:26:27 +01001061# elif defined(OR1K)
1062 or1k_io.iov_len = sizeof(or1k_regs);
1063 get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &or1k_io);
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001064# endif
1065}
1066#endif
1067
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001068/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001069 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1070 * 1: ok, continue in trace_syscall_entering().
1071 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001072 * ("????" etc) and bail out.
1073 */
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01001074static int
Denys Vlasenko06602d92011-08-24 17:53:52 +02001075get_scno(struct tcb *tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001076{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001077 long scno = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001078
Denys Vlasenko523635f2012-02-25 02:44:25 +01001079#if defined(S390) || defined(S390X)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001080 if (upeek(tcp, PT_GPR2, &syscall_mode) < 0)
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001081 return -1;
Roland McGrath2f924ca2003-06-26 22:23:28 +00001082
1083 if (syscall_mode != -ENOSYS) {
1084 /*
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001085 * Since kernel version 2.5.44 the scno gets passed in gpr2.
Roland McGrath2f924ca2003-06-26 22:23:28 +00001086 */
1087 scno = syscall_mode;
1088 } else {
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001089 /*
Michal Ludvig882eda82002-11-11 12:50:47 +00001090 * Old style of "passing" the scno via the SVC instruction.
1091 */
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001092 long psw;
Michal Ludvig882eda82002-11-11 12:50:47 +00001093 long opcode, offset_reg, tmp;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001094 void *svc_addr;
Denys Vlasenko7c9ba8b2011-08-19 19:46:32 +02001095 static const int gpr_offset[16] = {
1096 PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
1097 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
1098 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
1099 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15
1100 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001101
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001102 if (upeek(tcp, PT_PSWADDR, &psw) < 0)
Michal Ludvig882eda82002-11-11 12:50:47 +00001103 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001104 errno = 0;
Denys Vlasenko7ba8e722013-02-08 15:50:05 +01001105 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +00001106 if (errno) {
Denys Vlasenko905e8e02013-02-26 12:30:09 +01001107 perror_msg("peektext(psw-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +00001108 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +00001109 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001110
1111 /*
1112 * We have to check if the SVC got executed directly or via an
1113 * EXECUTE instruction. In case of EXECUTE it is necessary to do
1114 * instruction decoding to derive the system call number.
1115 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1116 * so that this doesn't work if a SVC opcode is part of an EXECUTE
1117 * opcode. Since there is no way to find out the opcode size this
1118 * is the best we can do...
1119 */
Michal Ludvig882eda82002-11-11 12:50:47 +00001120 if ((opcode & 0xff00) == 0x0a00) {
1121 /* SVC opcode */
1122 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +00001123 }
Michal Ludvig882eda82002-11-11 12:50:47 +00001124 else {
1125 /* SVC got executed by EXECUTE instruction */
1126
1127 /*
1128 * Do instruction decoding of EXECUTE. If you really want to
1129 * understand this, read the Principles of Operations.
1130 */
1131 svc_addr = (void *) (opcode & 0xfff);
1132
1133 tmp = 0;
1134 offset_reg = (opcode & 0x000f0000) >> 16;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001135 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001136 return -1;
1137 svc_addr += tmp;
1138
1139 tmp = 0;
1140 offset_reg = (opcode & 0x0000f000) >> 12;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001141 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001142 return -1;
1143 svc_addr += tmp;
1144
Denys Vlasenkofb036672009-01-23 16:30:26 +00001145 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
Michal Ludvig882eda82002-11-11 12:50:47 +00001146 if (errno)
1147 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001148# if defined(S390X)
Michal Ludvig882eda82002-11-11 12:50:47 +00001149 scno >>= 48;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001150# else
Michal Ludvig882eda82002-11-11 12:50:47 +00001151 scno >>= 16;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001152# endif
Michal Ludvig882eda82002-11-11 12:50:47 +00001153 tmp = 0;
1154 offset_reg = (opcode & 0x00f00000) >> 20;
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001155 if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
Michal Ludvig882eda82002-11-11 12:50:47 +00001156 return -1;
1157
1158 scno = (scno | tmp) & 0xff;
1159 }
1160 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001161#elif defined(POWERPC)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001162 if (upeek(tcp, sizeof(unsigned long)*PT_R0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001163 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001164# ifdef POWERPC64
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001165 /* TODO: speed up strace by not doing this at every syscall.
1166 * We only need to do it after execve.
1167 */
1168 int currpers;
1169 long val;
Andreas Schwabd69fa492010-07-12 21:39:57 +02001170
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001171 /* Check for 64/32 bit mode. */
1172 if (upeek(tcp, sizeof(unsigned long)*PT_MSR, &val) < 0)
1173 return -1;
1174 /* SF is bit 0 of MSR */
1175 if (val < 0)
1176 currpers = 0;
1177 else
1178 currpers = 1;
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001179 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001180# endif
1181#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001182 scno = avr32_regs.r8;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001183#elif defined(BFIN)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001184 if (upeek(tcp, PT_ORIG_P0, &scno))
Dmitry V. Levin87ea1f42008-11-10 22:21:41 +00001185 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001186#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001187 scno = i386_regs.orig_eax;
H.J. Lu35be5812012-04-16 13:00:01 +02001188#elif defined(X86_64) || defined(X32)
1189# ifndef __X32_SYSCALL_BIT
1190# define __X32_SYSCALL_BIT 0x40000000
1191# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001192 int currpers;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001193# if 1
1194 /* GETREGSET of NT_PRSTATUS tells us regset size,
1195 * which unambiguously detects i386.
1196 *
1197 * Linux kernel distinguishes x86-64 and x32 processes
1198 * solely by looking at __X32_SYSCALL_BIT:
1199 * arch/x86/include/asm/compat.h::is_x32_task():
1200 * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1201 * return true;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001202 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001203 if (x86_io.iov_len == sizeof(i386_regs)) {
1204 scno = i386_regs.orig_eax;
1205 currpers = 1;
1206 } else {
1207 scno = x86_64_regs.orig_rax;
1208 currpers = 0;
1209 if (scno & __X32_SYSCALL_BIT) {
1210 scno -= __X32_SYSCALL_BIT;
1211 currpers = 2;
1212 }
1213 }
1214# elif 0
1215 /* cs = 0x33 for long mode (native 64 bit and x32)
1216 * cs = 0x23 for compatibility mode (32 bit)
1217 * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1218 */
1219 scno = x86_64_regs.orig_rax;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001220 switch (x86_64_regs.cs) {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001221 case 0x23: currpers = 1; break;
H.J. Lu35be5812012-04-16 13:00:01 +02001222 case 0x33:
1223 if (x86_64_regs.ds == 0x2b) {
1224 currpers = 2;
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001225 scno &= ~__X32_SYSCALL_BIT;
H.J. Lu35be5812012-04-16 13:00:01 +02001226 } else
1227 currpers = 0;
1228 break;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001229 default:
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001230 fprintf(stderr, "Unknown value CS=0x%08X while "
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001231 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001232 "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001233 currpers = current_personality;
1234 break;
1235 }
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001236# elif 0
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001237 /* This version analyzes the opcode of a syscall instruction.
1238 * (int 0x80 on i386 vs. syscall on x86-64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001239 * It works, but is too complicated, and strictly speaking, unreliable.
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001240 */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001241 unsigned long call, rip = x86_64_regs.rip;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001242 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1243 rip -= 2;
1244 errno = 0;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001245 call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001246 if (errno)
1247 fprintf(stderr, "ptrace_peektext failed: %s\n",
1248 strerror(errno));
1249 switch (call & 0xffff) {
1250 /* x86-64: syscall = 0x0f 0x05 */
1251 case 0x050f: currpers = 0; break;
1252 /* i386: int 0x80 = 0xcd 0x80 */
1253 case 0x80cd: currpers = 1; break;
1254 default:
1255 currpers = current_personality;
1256 fprintf(stderr,
1257 "Unknown syscall opcode (0x%04X) while "
1258 "detecting personality of process "
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001259 "PID=%d\n", (int)call, tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001260 break;
1261 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001262# endif
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001263
H.J. Lu35be5812012-04-16 13:00:01 +02001264# ifdef X32
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001265 /* If we are built for a x32 system, then personality 0 is x32
1266 * (not x86_64), and stracing of x86_64 apps is not supported.
1267 * Stracing of i386 apps is still supported.
H.J. Lu085e4282012-04-17 11:05:04 -07001268 */
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001269 if (currpers == 0) {
1270 fprintf(stderr, "syscall_%lu(...) in unsupported "
1271 "64-bit mode of process PID=%d\n",
1272 scno, tcp->pid);
1273 return 0;
H.J. Lu35be5812012-04-16 13:00:01 +02001274 }
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001275 currpers &= ~2; /* map 2,1 to 0,1 */
H.J. Lu35be5812012-04-16 13:00:01 +02001276# endif
H.J. Lu085e4282012-04-17 11:05:04 -07001277 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001278#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001279# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01001280 long psr;
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001281 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001282 ia32 = (psr & IA64_PSR_IS) != 0;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001283 if (ia32) {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001284 if (upeek(tcp, PT_R1, &scno) < 0)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001285 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001286 } else {
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001287 if (upeek(tcp, PT_R15, &scno) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001288 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001289 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001290#elif defined(AARCH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001291 switch (aarch64_io.iov_len) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001292 case sizeof(aarch64_regs):
1293 /* We are in 64-bit mode */
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001294 scno = aarch64_regs.regs[8];
1295 update_personality(tcp, 1);
1296 break;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001297 case sizeof(arm_regs):
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001298 /* We are in 32-bit mode */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001299 scno = arm_regs.ARM_r7;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001300 update_personality(tcp, 0);
1301 break;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001302 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001303#elif defined(ARM)
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001304 if (arm_regs.ARM_ip != 0) {
1305 /* It is not a syscall entry */
1306 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001307 tcp->flags |= TCB_INSYSCALL;
Denys Vlasenkoe7030e52013-02-20 18:08:25 +01001308 return 0;
1309 }
1310 /* Note: we support only 32-bit CPUs, not 26-bit */
1311
1312 if (arm_regs.ARM_cpsr & 0x20) {
1313 /* Thumb mode */
1314 scno = arm_regs.ARM_r7;
1315 } else {
1316 /* ARM mode */
1317 errno = 0;
1318 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1319 if (errno)
1320 return -1;
1321
1322 /* EABI syscall convention? */
1323 if (scno == 0xef000000) {
1324 scno = arm_regs.ARM_r7; /* yes */
1325 } else {
1326 if ((scno & 0x0ff00000) != 0x0f900000) {
1327 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1328 tcp->pid, scno);
1329 return -1;
1330 }
1331 /* Fixup the syscall number */
1332 scno &= 0x000fffff;
1333 }
1334 }
Denys Vlasenko7270de52013-02-21 15:46:34 +01001335
1336 scno = shuffle_scno(scno);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001337#elif defined(M68K)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001338 if (upeek(tcp, 4*PT_ORIG_D0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001339 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001340#elif defined(LINUX_MIPSN32)
Roland McGrath542c2c62008-05-20 01:11:56 +00001341 unsigned long long regs[38];
1342
Denys Vlasenkob63256e2011-06-07 12:13:24 +02001343 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001344 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001345 mips_a3 = regs[REG_A3];
1346 mips_r2 = regs[REG_V0];
Roland McGrath542c2c62008-05-20 01:11:56 +00001347
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001348 scno = mips_r2;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001349 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001350 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001351 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001352 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001353 return 0;
1354 }
Roland McGrath542c2c62008-05-20 01:11:56 +00001355 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001356#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001357 if (upeek(tcp, REG_A3, &mips_a3) < 0)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001358 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001359 if (upeek(tcp, REG_V0, &scno) < 0)
1360 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001361
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001362 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01001363 if (mips_a3 == 0 || mips_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001364 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001365 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
Roland McGrath542c2c62008-05-20 01:11:56 +00001366 return 0;
1367 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001368 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001369#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001370 if (upeek(tcp, REG_A3, &alpha_a3) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001371 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001372 if (upeek(tcp, REG_R0, &scno) < 0)
1373 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001374
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001375 /*
1376 * Do some sanity checks to figure out if it's
1377 * really a syscall entry
1378 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001379 if (!SCNO_IN_RANGE(scno)) {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001380 if (alpha_a3 == 0 || alpha_a3 == -1) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001381 if (debug_flag)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001382 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001383 return 0;
1384 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001385 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001386#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001387 /* Disassemble the syscall trap. */
1388 /* Retrieve the syscall trap instruction. */
Denys Vlasenko46455822013-02-05 17:02:59 +01001389 unsigned long trap;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001390 errno = 0;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001391# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001392 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001393 trap >>= 32;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001394# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001395 trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001396# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001397 if (errno)
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001398 return -1;
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001399
1400 /* Disassemble the trap to see what personality to use. */
1401 switch (trap) {
1402 case 0x91d02010:
1403 /* Linux/SPARC syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001404 update_personality(tcp, 0);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001405 break;
1406 case 0x91d0206d:
1407 /* Linux/SPARC64 syscall trap. */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001408 update_personality(tcp, 2);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001409 break;
1410 case 0x91d02000:
1411 /* SunOS syscall trap. (pers 1) */
1412 fprintf(stderr, "syscall: SunOS no support\n");
1413 return -1;
1414 case 0x91d02008:
1415 /* Solaris 2.x syscall trap. (per 2) */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001416 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001417 break;
1418 case 0x91d02009:
1419 /* NetBSD/FreeBSD syscall trap. */
1420 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1421 return -1;
1422 case 0x91d02027:
1423 /* Solaris 2.x gettimeofday */
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00001424 update_personality(tcp, 1);
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001425 break;
1426 default:
Denys Vlasenko523635f2012-02-25 02:44:25 +01001427# if defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001428 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001429# else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001430 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001431# endif
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001432 return -1;
1433 }
1434
1435 /* Extract the system call number from the registers. */
1436 if (trap == 0x91d02027)
1437 scno = 156;
1438 else
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001439 scno = sparc_regs.u_regs[U_REG_G1];
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001440 if (scno == 0) {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001441 scno = sparc_regs.u_regs[U_REG_O0];
1442 memmove(&sparc_regs.u_regs[U_REG_O0], &sparc_regs.u_regs[U_REG_O1], 7*sizeof(sparc_regs.u_regs[0]));
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001443 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001444#elif defined(HPPA)
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001445 if (upeek(tcp, PT_GR20, &scno) < 0)
1446 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001447#elif defined(SH)
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001448 /*
1449 * In the new syscall ABI, the system call number is in R3.
1450 */
1451 if (upeek(tcp, 4*(REG_REG0+3), &scno) < 0)
1452 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001453
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001454 if (scno < 0) {
1455 /* Odd as it may seem, a glibc bug has been known to cause
1456 glibc to issue bogus negative syscall numbers. So for
1457 our purposes, make strace print what it *should* have been */
1458 long correct_scno = (scno & 0xff);
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001459 if (debug_flag)
Denys Vlasenko5ae2b7c2009-02-27 20:32:52 +00001460 fprintf(stderr,
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001461 "Detected glibc bug: bogus system call"
1462 " number = %ld, correcting to %ld\n",
1463 scno,
1464 correct_scno);
1465 scno = correct_scno;
1466 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001467#elif defined(SH64)
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001468 if (upeek(tcp, REG_SYSCALL, &scno) < 0)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001469 return -1;
Denys Vlasenkoadedb512008-12-30 18:47:55 +00001470 scno &= 0xFFFF;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001471#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001472 if (upeek(tcp, 4*PT_R9, &scno) < 0)
1473 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001474#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001475 int currpers;
1476 scno = tile_regs.regs[10];
1477# ifdef __tilepro__
1478 currpers = 1;
1479# else
Denys Vlasenko59aea0a2013-02-11 12:29:36 +01001480# ifndef PT_FLAGS_COMPAT
1481# define PT_FLAGS_COMPAT 0x10000 /* from Linux 3.8 on */
1482# endif
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001483 if (tile_regs.flags & PT_FLAGS_COMPAT)
1484 currpers = 1;
1485 else
1486 currpers = 0;
1487# endif
1488 update_personality(tcp, currpers);
Denys Vlasenko523635f2012-02-25 02:44:25 +01001489#elif defined(MICROBLAZE)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001490 if (upeek(tcp, 0, &scno) < 0)
1491 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001492#elif defined(OR1K)
1493 scno = or1k_regs.gpr[11];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001494#endif
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001495
Denys Vlasenko8cd1acd2011-08-24 16:52:57 +02001496 tcp->scno = scno;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001497 if (SCNO_IS_VALID(tcp->scno)) {
1498 tcp->s_ent = &sysent[scno];
1499 tcp->qual_flg = qual_flags[scno];
1500 } else {
Denys Vlasenkoa9fe13c2013-02-22 13:26:10 +01001501 static const struct_sysent unknown = {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001502 .nargs = MAX_ARGS,
1503 .sys_flags = 0,
1504 .sys_func = printargs,
1505 .sys_name = "unknown", /* not used */
1506 };
1507 tcp->s_ent = &unknown;
1508 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1509 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00001510 return 1;
1511}
1512
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02001513/* Called at each syscall entry.
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001514 * Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001515 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1516 * 1: ok, continue in trace_syscall_entering().
1517 * other: error, trace_syscall_entering() should print error indicator
Denys Vlasenkobc161ec2009-01-02 18:02:45 +00001518 * ("????" etc) and bail out.
1519 */
Roland McGratha4d48532005-06-08 20:45:28 +00001520static int
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001521syscall_fixup_on_sysenter(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001522{
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001523 /* A common case of "not a syscall entry" is post-execve SIGTRAP */
Denys Vlasenko523635f2012-02-25 02:44:25 +01001524#if defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001525 if (i386_regs.eax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001526 if (debug_flag)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001527 fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1528 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001529 }
H.J. Lu35be5812012-04-16 13:00:01 +02001530#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001531 {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001532 long rax;
1533 if (x86_io.iov_len == sizeof(i386_regs)) {
1534 /* Sign extend from 32 bits */
1535 rax = (int32_t)i386_regs.eax;
1536 } else {
1537 /* Note: in X32 build, this truncates 64 to 32 bits */
1538 rax = x86_64_regs.rax;
1539 }
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001540 if (rax != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001541 if (debug_flag)
Denys Vlasenko18beb982011-08-24 16:59:23 +02001542 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1543 return 0;
1544 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001545 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001546#elif defined(S390) || defined(S390X)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001547 /* TODO: we already fetched PT_GPR2 in get_scno
1548 * and stored it in syscall_mode, reuse it here
1549 * instead of re-fetching?
1550 */
Denys Vlasenko932fc7d2008-12-16 18:18:40 +00001551 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001552 return -1;
Michal Ludvig882eda82002-11-11 12:50:47 +00001553 if (syscall_mode != -ENOSYS)
1554 syscall_mode = tcp->scno;
Denys Vlasenkoece98792011-08-25 10:25:35 +02001555 if (gpr2 != syscall_mode) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001556 if (debug_flag)
Denys Vlasenkob88f9612011-08-21 18:03:23 +02001557 fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2);
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001558 return 0;
1559 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001560#elif defined(M68K)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02001561 /* TODO? Eliminate upeek's in arches below like we did in x86 */
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001562 if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001563 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001564 if (m68k_d0 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001565 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001566 fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001567 return 0;
1568 }
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001569#elif defined(IA64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001570 if (upeek(tcp, PT_R10, &ia64_r10) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001571 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001572 if (upeek(tcp, PT_R8, &ia64_r8) < 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001573 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001574 if (ia32 && ia64_r8 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001575 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001576 fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001577 return 0;
1578 }
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001579#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001580 if (upeek(tcp, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001581 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001582 if (cris_r10 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001583 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001584 fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
Denys Vlasenkoea0e6e82009-02-25 17:08:40 +00001585 return 0;
1586 }
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001587#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001588 if (upeek(tcp, 3 * 4, &microblaze_r3) < 0)
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001589 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001590 if (microblaze_r3 != -ENOSYS) {
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001591 if (debug_flag)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01001592 fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
Edgar E. Iglesias939caba2010-07-06 14:21:07 +02001593 return 0;
1594 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001595#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001596 return 1;
1597}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001598
Denys Vlasenko146b9442012-03-18 22:10:48 +01001599static void
1600internal_fork(struct tcb *tcp)
1601{
1602#if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1603# define ARG_FLAGS 1
1604#else
1605# define ARG_FLAGS 0
1606#endif
1607#ifndef CLONE_UNTRACED
1608# define CLONE_UNTRACED 0x00800000
1609#endif
1610 if ((ptrace_setoptions
1611 & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1612 == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1613 return;
1614
1615 if (!followfork)
1616 return;
1617
1618 if (entering(tcp)) {
1619 /*
1620 * We won't see the new child if clone is called with
1621 * CLONE_UNTRACED, so we keep the same logic with that option
1622 * and don't trace it.
1623 */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001624 if ((tcp->s_ent->sys_func == sys_clone)
1625 && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
1626 )
Denys Vlasenko146b9442012-03-18 22:10:48 +01001627 return;
1628 setbpt(tcp);
1629 } else {
1630 if (tcp->flags & TCB_BPTSET)
1631 clearbpt(tcp);
1632 }
1633}
1634
1635#if defined(TCB_WAITEXECVE)
1636static void
1637internal_exec(struct tcb *tcp)
1638{
1639 /* Maybe we have post-execve SIGTRAP suppressed? */
1640 if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1641 return; /* yes, no need to do anything */
1642
1643 if (exiting(tcp) && syserror(tcp))
1644 /* Error in execve, no post-execve SIGTRAP expected */
1645 tcp->flags &= ~TCB_WAITEXECVE;
1646 else
1647 tcp->flags |= TCB_WAITEXECVE;
1648}
1649#endif
1650
1651static void
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001652syscall_fixup_for_fork_exec(struct tcb *tcp)
Roland McGrathc1e45922008-05-27 23:18:29 +00001653{
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001654 /*
1655 * We must always trace a few critical system calls in order to
1656 * correctly support following forks in the presence of tracing
1657 * qualifiers.
1658 */
1659 int (*func)();
1660
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001661 func = tcp->s_ent->sys_func;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001662
1663 if ( sys_fork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001664 || sys_vfork == func
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001665 || sys_clone == func
Denys Vlasenko146b9442012-03-18 22:10:48 +01001666 ) {
1667 internal_fork(tcp);
1668 return;
1669 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001670
Denys Vlasenko84703742012-02-25 02:38:52 +01001671#if defined(TCB_WAITEXECVE)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001672 if ( sys_execve == func
Denys Vlasenko84703742012-02-25 02:38:52 +01001673# if defined(SPARC) || defined(SPARC64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001674 || sys_execv == func
Denys Vlasenkoa7949742011-08-21 17:26:55 +02001675# endif
Denys Vlasenko146b9442012-03-18 22:10:48 +01001676 ) {
1677 internal_exec(tcp);
1678 return;
1679 }
Roland McGrathc1e45922008-05-27 23:18:29 +00001680#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001681}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001682
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001683/* Return -1 on error or 1 on success (never 0!) */
Roland McGratha4d48532005-06-08 20:45:28 +00001684static int
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01001685get_syscall_args(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001686{
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001687 int i, nargs;
1688
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001689 nargs = tcp->s_ent->nargs;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001690
Denys Vlasenko523635f2012-02-25 02:44:25 +01001691#if defined(S390) || defined(S390X)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001692 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001693 if (upeek(tcp, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
1694 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001695#elif defined(ALPHA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001696 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001697 if (upeek(tcp, REG_A0+i, &tcp->u_arg[i]) < 0)
1698 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001699#elif defined(IA64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001700 if (!ia32) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001701 unsigned long *out0, cfm, sof, sol;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001702 long rbs_end;
1703 /* be backwards compatible with kernel < 2.4.4... */
1704# ifndef PT_RBS_END
1705# define PT_RBS_END PT_AR_BSP
1706# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001707
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001708 if (upeek(tcp, PT_RBS_END, &rbs_end) < 0)
1709 return -1;
1710 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
Roland McGrath542c2c62008-05-20 01:11:56 +00001711 return -1;
1712
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001713 sof = (cfm >> 0) & 0x7f;
1714 sol = (cfm >> 7) & 0x7f;
1715 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1716
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001717 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001718 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1719 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1720 return -1;
1721 }
1722 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001723 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1724 PT_R9 /* ECX = out1 */,
1725 PT_R10 /* EDX = out2 */,
1726 PT_R14 /* ESI = out3 */,
1727 PT_R15 /* EDI = out4 */,
1728 PT_R13 /* EBP = out5 */};
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001729
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001730 for (i = 0; i < nargs; ++i) {
1731 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1732 return -1;
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001733 /* truncate away IVE sign-extension */
1734 tcp->u_arg[i] &= 0xffffffff;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001735 }
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001736 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001737#elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001738 /* N32 and N64 both use up to six registers. */
1739 unsigned long long regs[38];
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001740
1741 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1742 return -1;
1743
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001744 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001745 tcp->u_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001746# if defined(LINUX_MIPSN32)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001747 tcp->ext_arg[i] = regs[REG_A0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001748# endif
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001749 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001750#elif defined(MIPS)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001751 if (nargs > 4) {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001752 long sp;
1753
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001754 if (upeek(tcp, REG_SP, &sp) < 0)
1755 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001756 for (i = 0; i < 4; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001757 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1758 return -1;
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001759 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001760 (char *)(tcp->u_arg + 4));
1761 } else {
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001762 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001763 if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001764 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001765 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001766#elif defined(POWERPC)
1767# ifndef PT_ORIG_R3
1768# define PT_ORIG_R3 34
1769# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001770 for (i = 0; i < nargs; ++i) {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001771 if (upeek(tcp, (i==0) ?
1772 (sizeof(unsigned long) * PT_ORIG_R3) :
1773 ((i+PT_R3) * sizeof(unsigned long)),
1774 &tcp->u_arg[i]) < 0)
1775 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001776 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001777#elif defined(SPARC) || defined(SPARC64)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001778 for (i = 0; i < nargs; ++i)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001779 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001780#elif defined(HPPA)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001781 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001782 if (upeek(tcp, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1783 return -1;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00001784#elif defined(ARM) || defined(AARCH64)
1785# if defined(AARCH64)
1786 if (tcp->currpers == 1)
1787 for (i = 0; i < nargs; ++i)
1788 tcp->u_arg[i] = aarch64_regs.regs[i];
1789 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01001790# endif
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001791 for (i = 0; i < nargs; ++i)
Denys Vlasenko401374e2013-02-06 18:24:39 +01001792 tcp->u_arg[i] = arm_regs.uregs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001793#elif defined(AVR32)
Denys Vlasenkob5b25892011-08-30 19:04:54 +02001794 (void)i;
1795 (void)nargs;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01001796 tcp->u_arg[0] = avr32_regs.r12;
1797 tcp->u_arg[1] = avr32_regs.r11;
1798 tcp->u_arg[2] = avr32_regs.r10;
1799 tcp->u_arg[3] = avr32_regs.r9;
1800 tcp->u_arg[4] = avr32_regs.r5;
1801 tcp->u_arg[5] = avr32_regs.r3;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001802#elif defined(BFIN)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001803 static const int argreg[MAX_ARGS] = { PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5 };
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001804
Denys Vlasenko4b887a52011-08-23 13:32:38 +02001805 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001806 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1807 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001808#elif defined(SH)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001809 static const int syscall_regs[MAX_ARGS] = {
1810 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1811 4 * (REG_REG0+7), 4 * (REG_REG0 ), 4 * (REG_REG0+1)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001812 };
1813
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001814 for (i = 0; i < nargs; ++i)
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001815 if (upeek(tcp, syscall_regs[i], &tcp->u_arg[i]) < 0)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001816 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001817#elif defined(SH64)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001818 int i;
1819 /* Registers used by SH5 Linux system calls for parameters */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001820 static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001821
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001822 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001823 if (upeek(tcp, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1824 return -1;
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001825#elif defined(I386)
1826 (void)i;
1827 (void)nargs;
1828 tcp->u_arg[0] = i386_regs.ebx;
1829 tcp->u_arg[1] = i386_regs.ecx;
1830 tcp->u_arg[2] = i386_regs.edx;
1831 tcp->u_arg[3] = i386_regs.esi;
1832 tcp->u_arg[4] = i386_regs.edi;
1833 tcp->u_arg[5] = i386_regs.ebp;
H.J. Lu35be5812012-04-16 13:00:01 +02001834#elif defined(X86_64) || defined(X32)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001835 (void)i;
1836 (void)nargs;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001837 if (x86_io.iov_len != sizeof(i386_regs)) {
1838 /* x86-64 or x32 ABI */
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001839 tcp->u_arg[0] = x86_64_regs.rdi;
1840 tcp->u_arg[1] = x86_64_regs.rsi;
1841 tcp->u_arg[2] = x86_64_regs.rdx;
1842 tcp->u_arg[3] = x86_64_regs.r10;
1843 tcp->u_arg[4] = x86_64_regs.r8;
1844 tcp->u_arg[5] = x86_64_regs.r9;
H.J. Lu35be5812012-04-16 13:00:01 +02001845# ifdef X32
1846 tcp->ext_arg[0] = x86_64_regs.rdi;
1847 tcp->ext_arg[1] = x86_64_regs.rsi;
1848 tcp->ext_arg[2] = x86_64_regs.rdx;
1849 tcp->ext_arg[3] = x86_64_regs.r10;
1850 tcp->ext_arg[4] = x86_64_regs.r8;
1851 tcp->ext_arg[5] = x86_64_regs.r9;
1852# endif
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01001853 } else {
1854 /* i386 ABI */
Denys Vlasenko6cf36052013-02-15 15:01:38 +01001855 /* Zero-extend from 32 bits */
1856 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1857 * if you need to use *sign-extended* parameter.
1858 */
1859 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1860 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1861 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1862 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1863 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1864 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02001865 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001866#elif defined(MICROBLAZE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001867 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001868 if (upeek(tcp, (5 + i) * 4, &tcp->u_arg[i]) < 0)
1869 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001870#elif defined(CRISV10) || defined(CRISV32)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001871 static const int crisregs[MAX_ARGS] = {
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001872 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
Denys Vlasenko0b6c73c2011-06-23 22:22:34 +02001873 4*PT_R13 , 4*PT_MOF, 4*PT_SRP
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001874 };
Roland McGrathe1e584b2003-06-02 19:18:58 +00001875
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001876 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001877 if (upeek(tcp, crisregs[i], &tcp->u_arg[i]) < 0)
1878 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001879#elif defined(TILE)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001880 for (i = 0; i < nargs; ++i)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01001881 tcp->u_arg[i] = tile_regs.regs[i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001882#elif defined(M68K)
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001883 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001884 if (upeek(tcp, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
1885 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01001886#elif defined(OR1K)
1887 (void)nargs;
1888 for (i = 0; i < 6; ++i)
1889 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
Denys Vlasenko523635f2012-02-25 02:44:25 +01001890#else /* Other architecture (32bits specific) */
Dmitry V. Levin5f731c42011-08-23 16:24:20 +00001891 for (i = 0; i < nargs; ++i)
Denys Vlasenkof5d099c2011-06-23 22:10:54 +02001892 if (upeek(tcp, i*4, &tcp->u_arg[i]) < 0)
1893 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01001894#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001895 return 1;
1896}
1897
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00001898static int
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001899trace_syscall_entering(struct tcb *tcp)
1900{
1901 int res, scno_good;
1902
Denys Vlasenko2ce12ed2011-08-24 17:25:32 +02001903#if defined TCB_WAITEXECVE
1904 if (tcp->flags & TCB_WAITEXECVE) {
1905 /* This is the post-execve SIGTRAP. */
1906 tcp->flags &= ~TCB_WAITEXECVE;
1907 return 0;
1908 }
1909#endif
1910
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01001911 scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001912 if (res == 0)
1913 return res;
Denys Vlasenko907735a2012-03-21 00:23:16 +01001914 if (res == 1) {
Denys Vlasenko8b4454c2011-08-25 10:40:14 +02001915 res = syscall_fixup_on_sysenter(tcp);
Denys Vlasenko907735a2012-03-21 00:23:16 +01001916 if (res == 0)
1917 return res;
1918 if (res == 1)
1919 res = get_syscall_args(tcp);
1920 }
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001921
1922 if (res != 1) {
1923 printleader(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001924 if (scno_good != 1)
Denys Vlasenkob7a6dae2012-03-20 16:48:35 +01001925 tprints("????" /* anti-trigraph gap */ "(");
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001926 else if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01001927 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001928 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001929 tprintf("%s(", tcp->s_ent->sys_name);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001930 /*
1931 * " <unavailable>" will be added later by the code which
1932 * detects ptrace errors.
1933 */
1934 goto ret;
1935 }
1936
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001937#if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001938 while (1) {
Denys Vlasenko523635f2012-02-25 02:44:25 +01001939# ifdef SYS_socket_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001940 if (tcp->s_ent->sys_func == sys_socketcall) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00001941 decode_socket_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001942 break;
1943 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001944# endif
1945# ifdef SYS_ipc_subcall
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001946 if (tcp->s_ent->sys_func == sys_ipc) {
Dmitry V. Levin648c22c2012-03-15 22:08:55 +00001947 decode_ipc_subcall(tcp);
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001948 break;
1949 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01001950# endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001951 break;
1952 }
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001953#endif
Dmitry V. Levinb5e88d42012-02-20 17:02:38 +00001954
Denys Vlasenko8d4ca0c2013-02-06 13:18:42 +01001955 if (need_fork_exec_workarounds)
1956 syscall_fixup_for_fork_exec(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001957
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001958 if (!(tcp->qual_flg & QUAL_TRACE)
1959 || (tracing_paths && !pathtrace_match(tcp))
1960 ) {
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001961 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
1962 return 0;
1963 }
1964
1965 tcp->flags &= ~TCB_FILTERED;
1966
1967 if (cflag == CFLAG_ONLY_STATS) {
1968 res = 0;
1969 goto ret;
1970 }
1971
1972 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001973 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01001974 tprintf("%s(", undefined_scno_name(tcp));
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001975 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001976 tprintf("%s(", tcp->s_ent->sys_name);
1977 if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001978 res = printargs(tcp);
1979 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01001980 res = tcp->s_ent->sys_func(tcp);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001981
Dmitry V. Levinb742d8c2012-09-17 22:40:12 +00001982 fflush(tcp->outf);
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001983 ret:
1984 tcp->flags |= TCB_INSYSCALL;
1985 /* Measure the entrance time as late as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01001986 if (Tflag || cflag)
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02001987 gettimeofday(&tcp->etime, NULL);
1988 return res;
1989}
1990
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001991/* Returns:
Denys Vlasenko907735a2012-03-21 00:23:16 +01001992 * 1: ok, continue in trace_syscall_exiting().
1993 * -1: error, trace_syscall_exiting() should print error indicator
Denys Vlasenkoa6146922011-08-24 18:07:22 +02001994 * ("????" etc) and bail out.
1995 */
1996static int
1997get_syscall_result(struct tcb *tcp)
1998{
Denys Vlasenko523635f2012-02-25 02:44:25 +01001999#if defined(S390) || defined(S390X)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002000 if (upeek(tcp, PT_GPR2, &gpr2) < 0)
2001 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002002#elif defined(POWERPC)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002003# define SO_MASK 0x10000000
2004 {
2005 long flags;
2006 if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0)
2007 return -1;
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002008 if (upeek(tcp, sizeof(unsigned long)*PT_R3, &ppc_result) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002009 return -1;
2010 if (flags & SO_MASK)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002011 ppc_result = -ppc_result;
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002012 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002013#elif defined(AVR32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002014 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002015#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002016 if (upeek(tcp, PT_R0, &bfin_r0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002017 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002018#elif defined(I386)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002019 /* already done by get_regs */
H.J. Lu35be5812012-04-16 13:00:01 +02002020#elif defined(X86_64) || defined(X32)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002021 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002022#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002023# define IA64_PSR_IS ((long)1 << 34)
Denys Vlasenko4bdb6bb2013-02-06 18:09:31 +01002024 long psr;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002025 if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
2026 ia32 = (psr & IA64_PSR_IS) != 0;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002027 if (upeek(tcp, PT_R8, &ia64_r8) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002028 return -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002029 if (upeek(tcp, PT_R10, &ia64_r10) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002030 return -1;
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002031#elif defined(ARM)
2032 /* already done by get_regs */
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002033#elif defined(AARCH64)
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002034 /* register reading already done by get_regs */
2035
2036 /* Used to do this, but we did it on syscall entry already: */
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002037 /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
2038 * else it's personality 0.
2039 */
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002040 /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
Denys Vlasenko523635f2012-02-25 02:44:25 +01002041#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002042 if (upeek(tcp, 4*PT_D0, &m68k_d0) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002043 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002044#elif defined(LINUX_MIPSN32)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002045 unsigned long long regs[38];
2046
2047 if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
2048 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002049 mips_a3 = regs[REG_A3];
2050 mips_r2 = regs[REG_V0];
Denys Vlasenko523635f2012-02-25 02:44:25 +01002051#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002052 if (upeek(tcp, REG_A3, &mips_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002053 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002054 if (upeek(tcp, REG_V0, &mips_r2) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002055 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002056#elif defined(ALPHA)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002057 if (upeek(tcp, REG_A3, &alpha_a3) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002058 return -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002059 if (upeek(tcp, REG_R0, &alpha_r0) < 0)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002060 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002061#elif defined(SPARC) || defined(SPARC64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002062 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002063#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002064 if (upeek(tcp, PT_GR28, &hppa_r28) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002065 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002066#elif defined(SH)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002067 /* new syscall ABI returns result in R0 */
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002068 if (upeek(tcp, 4*REG_REG0, (long *)&sh_r0) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002069 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002070#elif defined(SH64)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002071 /* ABI defines result returned in r9 */
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002072 if (upeek(tcp, REG_GENERAL(9), (long *)&sh64_r9) < 0)
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002073 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002074#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002075 if (upeek(tcp, 4*PT_R10, &cris_r10) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002076 return -1;
Denys Vlasenko523635f2012-02-25 02:44:25 +01002077#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002078 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002079#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002080 if (upeek(tcp, 3 * 4, &microblaze_r3) < 0)
Denys Vlasenkof20bff62011-08-25 10:31:24 +02002081 return -1;
Christian Svensson492f81f2013-02-14 13:26:27 +01002082#elif defined(OR1K)
2083 /* already done by get_regs */
Denys Vlasenko523635f2012-02-25 02:44:25 +01002084#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002085 return 1;
2086}
2087
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002088/* Called at each syscall exit */
2089static void
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002090syscall_fixup_on_sysexit(struct tcb *tcp)
2091{
Denys Vlasenko523635f2012-02-25 02:44:25 +01002092#if defined(S390) || defined(S390X)
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002093 if (syscall_mode != -ENOSYS)
2094 syscall_mode = tcp->scno;
Denys Vlasenkoece98792011-08-25 10:25:35 +02002095 if ((tcp->flags & TCB_WAITEXECVE)
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002096 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
2097 /*
2098 * Return from execve.
2099 * Fake a return value of zero. We leave the TCB_WAITEXECVE
2100 * flag set for the post-execve SIGTRAP to see and reset.
2101 */
2102 gpr2 = 0;
2103 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002104#endif
Denys Vlasenko20c41fd2011-08-25 10:23:00 +02002105}
2106
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002107/*
2108 * Check the syscall return value register value for whether it is
2109 * a negated errno code indicating an error, or a success return value.
2110 */
2111static inline int
2112is_negated_errno(unsigned long int val)
2113{
2114 unsigned long int max = -(long int) nerrnos;
Denys Vlasenko2544f982013-02-19 17:39:56 +01002115#if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
Denys Vlasenko9fd4f962012-03-19 09:36:42 +01002116 if (current_wordsize < sizeof(val)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002117 val = (unsigned int) val;
2118 max = (unsigned int) max;
2119 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002120#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002121 return val > max;
2122}
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002123
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002124#if defined(X32)
2125static inline int
2126is_negated_errno_x32(unsigned long long val)
2127{
2128 unsigned long long max = -(long long) nerrnos;
2129 /*
2130 * current_wordsize is 4 even in personality 0 (native X32)
2131 * but truncation _must not_ be done in it.
2132 * can't check current_wordsize here!
2133 */
2134 if (current_personality != 0) {
2135 val = (uint32_t) val;
2136 max = (uint32_t) max;
2137 }
2138 return val > max;
2139}
2140#endif
2141
Denys Vlasenko907735a2012-03-21 00:23:16 +01002142/* Returns:
2143 * 1: ok, continue in trace_syscall_exiting().
2144 * -1: error, trace_syscall_exiting() should print error indicator
2145 * ("????" etc) and bail out.
2146 */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002147static void
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002148get_error(struct tcb *tcp)
2149{
2150 int u_error = 0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002151 int check_errno = 1;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002152 if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002153 check_errno = 0;
2154 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002155#if defined(S390) || defined(S390X)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002156 if (check_errno && is_negated_errno(gpr2)) {
2157 tcp->u_rval = -1;
2158 u_error = -gpr2;
2159 }
2160 else {
2161 tcp->u_rval = gpr2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002162 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002163#elif defined(I386)
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002164 if (check_errno && is_negated_errno(i386_regs.eax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002165 tcp->u_rval = -1;
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002166 u_error = -i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002167 }
2168 else {
Denys Vlasenkoeb0e3e82011-08-30 18:53:49 +02002169 tcp->u_rval = i386_regs.eax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002170 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002171#elif defined(X86_64)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002172 long rax;
2173 if (x86_io.iov_len == sizeof(i386_regs)) {
2174 /* Sign extend from 32 bits */
2175 rax = (int32_t)i386_regs.eax;
2176 } else {
2177 rax = x86_64_regs.rax;
2178 }
2179 if (check_errno && is_negated_errno(rax)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002180 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002181 u_error = -rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002182 }
2183 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002184 tcp->u_rval = rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002185 }
2186#elif defined(X32)
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002187 /* In X32, return value is 64-bit (llseek uses one).
2188 * Using merely "long rax" would not work.
2189 */
2190 long long rax;
2191 if (x86_io.iov_len == sizeof(i386_regs)) {
2192 /* Sign extend from 32 bits */
2193 rax = (int32_t)i386_regs.eax;
2194 } else {
2195 rax = x86_64_regs.rax;
2196 }
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002197 /* Careful: is_negated_errno() works only on longs */
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002198 if (check_errno && is_negated_errno_x32(rax)) {
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002199 tcp->u_rval = -1;
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002200 u_error = -rax;
Denys Vlasenkoafea7dd2013-02-12 11:52:35 +01002201 }
2202 else {
Denys Vlasenkoeec8d5d2013-02-14 03:29:48 +01002203 tcp->u_rval = rax; /* truncating */
2204 tcp->u_lrval = rax;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002205 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002206#elif defined(IA64)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002207 if (ia32) {
2208 int err;
2209
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002210 err = (int)ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002211 if (check_errno && is_negated_errno(err)) {
2212 tcp->u_rval = -1;
2213 u_error = -err;
2214 }
2215 else {
2216 tcp->u_rval = err;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002217 }
2218 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002219 if (check_errno && ia64_r10) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002220 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002221 u_error = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002222 } else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002223 tcp->u_rval = ia64_r8;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002224 }
2225 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002226#elif defined(MIPS)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002227 if (check_errno && mips_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002228 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002229 u_error = mips_r2;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002230 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002231 tcp->u_rval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002232# if defined(LINUX_MIPSN32)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002233 tcp->u_lrval = mips_r2;
H.J. Ludd0130b2012-04-16 12:16:45 +02002234# endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002235 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002236#elif defined(POWERPC)
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002237 if (check_errno && is_negated_errno(ppc_result)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002238 tcp->u_rval = -1;
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002239 u_error = -ppc_result;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002240 }
2241 else {
Denys Vlasenko46dc8b22012-03-21 00:07:25 +01002242 tcp->u_rval = ppc_result;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002243 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002244#elif defined(M68K)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002245 if (check_errno && is_negated_errno(m68k_d0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002246 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002247 u_error = -m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002248 }
2249 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002250 tcp->u_rval = m68k_d0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002251 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002252#elif defined(ARM) || defined(AARCH64)
2253# if defined(AARCH64)
2254 if (tcp->currpers == 1) {
2255 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2256 tcp->u_rval = -1;
2257 u_error = -aarch64_regs.regs[0];
2258 }
2259 else {
2260 tcp->u_rval = aarch64_regs.regs[0];
2261 }
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002262 }
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002263 else
Denys Vlasenko28ac68f2013-02-08 12:38:51 +01002264# endif
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002265 {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002266 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002267 tcp->u_rval = -1;
Denys Vlasenko401374e2013-02-06 18:24:39 +01002268 u_error = -arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002269 }
2270 else {
Denys Vlasenko401374e2013-02-06 18:24:39 +01002271 tcp->u_rval = arm_regs.ARM_r0;
Steve McIntyre890a5ca2012-11-10 11:24:48 +00002272 }
Steve McIntyred8d3bd32012-10-24 17:58:16 +01002273 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002274#elif defined(AVR32)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002275 if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002276 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002277 u_error = -avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002278 }
2279 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002280 tcp->u_rval = avr32_regs.r12;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002281 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002282#elif defined(BFIN)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002283 if (check_errno && is_negated_errno(bfin_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002284 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002285 u_error = -bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002286 } else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002287 tcp->u_rval = bfin_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002288 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002289#elif defined(ALPHA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002290 if (check_errno && alpha_a3) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002291 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002292 u_error = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002293 }
2294 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002295 tcp->u_rval = alpha_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002296 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002297#elif defined(SPARC)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002298 if (check_errno && sparc_regs.psr & PSR_C) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002299 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002300 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002301 }
2302 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002303 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002304 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002305#elif defined(SPARC64)
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002306 if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002307 tcp->u_rval = -1;
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002308 u_error = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002309 }
2310 else {
Denys Vlasenko48e4c1b2013-02-16 08:23:40 +01002311 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002312 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002313#elif defined(HPPA)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002314 if (check_errno && is_negated_errno(hppa_r28)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002315 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002316 u_error = -hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002317 }
2318 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002319 tcp->u_rval = hppa_r28;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002320 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002321#elif defined(SH)
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002322 if (check_errno && is_negated_errno(sh_r0)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002323 tcp->u_rval = -1;
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002324 u_error = -sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002325 }
2326 else {
Denys Vlasenkod22213a2013-02-13 17:52:31 +01002327 tcp->u_rval = sh_r0;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002328 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002329#elif defined(SH64)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002330 if (check_errno && is_negated_errno(sh64_r9)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002331 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002332 u_error = -sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002333 }
2334 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002335 tcp->u_rval = sh64_r9;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002336 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002337#elif defined(CRISV10) || defined(CRISV32)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002338 if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002339 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002340 u_error = -cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002341 }
2342 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002343 tcp->u_rval = cris_r10;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002344 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002345#elif defined(TILE)
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002346 /*
2347 * The standard tile calling convention returns the value (or negative
2348 * errno) in r0, and zero (or positive errno) in r1.
2349 * Until at least kernel 3.8, however, the r1 value is not reflected
2350 * in ptregs at this point, so we use r0 here.
2351 */
2352 if (check_errno && is_negated_errno(tile_regs.regs[0])) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002353 tcp->u_rval = -1;
Chris Metcalf0b99a8a2013-02-05 17:48:33 +01002354 u_error = -tile_regs.regs[0];
2355 } else {
2356 tcp->u_rval = tile_regs.regs[0];
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002357 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002358#elif defined(MICROBLAZE)
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002359 if (check_errno && is_negated_errno(microblaze_r3)) {
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002360 tcp->u_rval = -1;
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002361 u_error = -microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002362 }
2363 else {
Denys Vlasenko89804ec2013-02-07 13:14:48 +01002364 tcp->u_rval = microblaze_r3;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002365 }
Christian Svensson492f81f2013-02-14 13:26:27 +01002366#elif defined(OR1K)
2367 if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2368 tcp->u_rval = -1;
2369 u_error = -or1k_regs.gpr[11];
2370 }
2371 else {
2372 tcp->u_rval = or1k_regs.gpr[11];
2373 }
Denys Vlasenko523635f2012-02-25 02:44:25 +01002374#endif
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002375 tcp->u_error = u_error;
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002376}
2377
2378static void
2379dumpio(struct tcb *tcp)
2380{
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002381 int (*func)();
2382
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002383 if (syserror(tcp))
2384 return;
Denys Vlasenko9cbc15b2013-02-22 13:37:36 +01002385 if ((unsigned long) tcp->u_arg[0] >= num_quals)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002386 return;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002387 func = tcp->s_ent->sys_func;
2388 if (func == printargs)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002389 return;
2390 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002391 if (func == sys_read ||
2392 func == sys_pread ||
2393 func == sys_recv ||
2394 func == sys_recvfrom)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002395 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002396 else if (func == sys_readv)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002397 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2398 return;
2399 }
2400 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002401 if (func == sys_write ||
2402 func == sys_pwrite ||
2403 func == sys_send ||
2404 func == sys_sendto)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002405 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002406 else if (func == sys_writev)
Denys Vlasenkoa6146922011-08-24 18:07:22 +02002407 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2408 return;
2409 }
2410}
2411
Denys Vlasenkoed4f4f02011-08-22 11:54:06 +02002412static int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002413trace_syscall_exiting(struct tcb *tcp)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002414{
2415 int sys_res;
2416 struct timeval tv;
Denys Vlasenko1a5b5a72011-08-25 00:29:56 +02002417 int res;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002418 long u_error;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002419
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002420 /* Measure the exit time as early as possible to avoid errors. */
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002421 if (Tflag || cflag)
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002422 gettimeofday(&tv, NULL);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002423
Dmitry V. Levina5a839a2011-12-23 00:50:49 +00002424#if SUPPORTED_PERSONALITIES > 1
2425 update_personality(tcp, tcp->currpers);
2426#endif
Denys Vlasenkoce7d9532013-02-05 16:36:13 +01002427 res = (get_regs_error ? -1 : get_syscall_result(tcp));
Denys Vlasenkobb6bb5c2012-03-20 17:10:35 +01002428 if (res == 1) {
2429 syscall_fixup_on_sysexit(tcp); /* never fails */
Denys Vlasenkoc956ef02013-02-16 14:25:56 +01002430 get_error(tcp); /* never fails */
2431 if (need_fork_exec_workarounds)
2432 syscall_fixup_for_fork_exec(tcp);
2433 if (filtered(tcp))
2434 goto ret;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002435 }
2436
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002437 if (cflag) {
2438 struct timeval t = tv;
Denys Vlasenkoc95a88f2011-08-21 17:47:40 +02002439 count_syscall(tcp, &t);
Denys Vlasenko7b609d52011-06-22 14:32:43 +02002440 if (cflag == CFLAG_ONLY_STATS) {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002441 goto ret;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002442 }
2443 }
2444
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002445 /* If not in -ff mode, and printing_tcp != tcp,
2446 * then the log currently does not end with output
2447 * of _our syscall entry_, but with something else.
2448 * We need to say which syscall's return is this.
2449 *
2450 * Forced reprinting via TCB_REPRINT is used only by
2451 * "strace -ff -oLOG test/threaded_execve" corner case.
2452 * It's the only case when -ff mode needs reprinting.
2453 */
2454 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2455 tcp->flags &= ~TCB_REPRINT;
2456 printleader(tcp);
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002457 if (tcp->qual_flg & UNDEFINED_SCNO)
Denys Vlasenko7270de52013-02-21 15:46:34 +01002458 tprintf("<... %s resumed> ", undefined_scno_name(tcp));
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002459 else
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002460 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002461 }
2462 printing_tcp = tcp;
2463
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002464 if (res != 1) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002465 /* There was error in one of prior ptrace ops */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002466 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002467 tabto();
Denys Vlasenko000b6012012-01-28 01:25:03 +01002468 tprints("= ? <unavailable>\n");
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002469 line_ended();
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002470 tcp->flags &= ~TCB_INSYSCALL;
2471 return res;
2472 }
2473
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002474 sys_res = 0;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002475 if (tcp->qual_flg & QUAL_RAW) {
Denys Vlasenkoa44f9692012-03-21 11:06:20 +01002476 /* sys_res = printargs(tcp); - but it's nop on sysexit */
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002477 } else {
Denys Vlasenko3b738812011-08-22 02:06:35 +02002478 /* FIXME: not_failing_only (IOW, option -z) is broken:
2479 * failure of syscall is known only after syscall return.
2480 * Thus we end up with something like this on, say, ENOENT:
2481 * open("doesnt_exist", O_RDONLY <unfinished ...>
2482 * {next syscall decode}
2483 * whereas the intended result is that open(...) line
2484 * is not shown at all.
2485 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002486 if (not_failing_only && tcp->u_error)
Denys Vlasenko3b738812011-08-22 02:06:35 +02002487 goto ret; /* ignore failed syscalls */
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002488 sys_res = tcp->s_ent->sys_func(tcp);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002489 }
2490
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002491 tprints(") ");
Denys Vlasenko102ec492011-08-25 01:27:59 +02002492 tabto();
Denys Vlasenko3b738812011-08-22 02:06:35 +02002493 u_error = tcp->u_error;
Denys Vlasenko74ec14f2013-02-21 16:13:47 +01002494 if (tcp->qual_flg & QUAL_RAW) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002495 if (u_error)
2496 tprintf("= -1 (errno %ld)", u_error);
2497 else
2498 tprintf("= %#lx", tcp->u_rval);
2499 }
2500 else if (!(sys_res & RVAL_NONE) && u_error) {
2501 switch (u_error) {
Denys Vlasenkofe585652012-01-12 11:26:34 +01002502 /* Blocked signals do not interrupt any syscalls.
2503 * In this case syscalls don't return ERESTARTfoo codes.
2504 *
2505 * Deadly signals set to SIG_DFL interrupt syscalls
2506 * and kill the process regardless of which of the codes below
2507 * is returned by the interrupted syscall.
2508 * In some cases, kernel forces a kernel-generated deadly
2509 * signal to be unblocked and set to SIG_DFL (and thus cause
2510 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2511 * or SIGILL. (The alternative is to leave process spinning
2512 * forever on the faulty instruction - not useful).
2513 *
2514 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2515 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2516 * but kernel will always restart them.
2517 */
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002518 case ERESTARTSYS:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002519 /* Most common type of signal-interrupted syscall exit code.
2520 * The system call will be restarted with the same arguments
2521 * if SA_RESTART is set; otherwise, it will fail with EINTR.
2522 */
2523 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002524 break;
2525 case ERESTARTNOINTR:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002526 /* Rare. For example, fork() returns this if interrupted.
2527 * SA_RESTART is ignored (assumed set): the restart is unconditional.
2528 */
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002529 tprints("= ? ERESTARTNOINTR (To be restarted)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002530 break;
2531 case ERESTARTNOHAND:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002532 /* pause(), rt_sigsuspend() etc use this code.
2533 * SA_RESTART is ignored (assumed not set):
2534 * syscall won't restart (will return EINTR instead)
Denys Vlasenko30c03232013-02-19 16:59:26 +01002535 * even after signal with SA_RESTART set. However,
2536 * after SIG_IGN or SIG_DFL signal it will restart
2537 * (thus the name "restart only if has no handler").
Denys Vlasenkofe585652012-01-12 11:26:34 +01002538 */
2539 tprints("= ? ERESTARTNOHAND (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002540 break;
2541 case ERESTART_RESTARTBLOCK:
Denys Vlasenkofe585652012-01-12 11:26:34 +01002542 /* Syscalls like nanosleep(), poll() which can't be
2543 * restarted with their original arguments use this
2544 * code. Kernel will execute restart_syscall() instead,
2545 * which changes arguments before restarting syscall.
2546 * SA_RESTART is ignored (assumed not set) similarly
2547 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2548 * since restart data is saved in "restart block"
2549 * in task struct, and if signal handler uses a syscall
2550 * which in turn saves another such restart block,
2551 * old data is lost and restart becomes impossible)
2552 */
2553 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002554 break;
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002555 default:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002556 if (u_error < 0)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002557 tprintf("= -1 E??? (errno %ld)", u_error);
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002558 else if (u_error < nerrnos)
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002559 tprintf("= -1 %s (%s)", errnoent[u_error],
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002560 strerror(u_error));
2561 else
Denys Vlasenkoa7949742011-08-21 17:26:55 +02002562 tprintf("= -1 ERRNO_%ld (%s)", u_error,
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002563 strerror(u_error));
2564 break;
2565 }
2566 if ((sys_res & RVAL_STR) && tcp->auxstr)
2567 tprintf(" (%s)", tcp->auxstr);
2568 }
2569 else {
2570 if (sys_res & RVAL_NONE)
Denys Vlasenko60fe8c12011-09-01 10:00:28 +02002571 tprints("= ?");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002572 else {
2573 switch (sys_res & RVAL_MASK) {
2574 case RVAL_HEX:
2575 tprintf("= %#lx", tcp->u_rval);
2576 break;
2577 case RVAL_OCTAL:
2578 tprintf("= %#lo", tcp->u_rval);
2579 break;
2580 case RVAL_UDECIMAL:
2581 tprintf("= %lu", tcp->u_rval);
2582 break;
2583 case RVAL_DECIMAL:
2584 tprintf("= %ld", tcp->u_rval);
2585 break;
H.J. Ludd0130b2012-04-16 12:16:45 +02002586#if defined(LINUX_MIPSN32) || defined(X32)
2587 /*
2588 case RVAL_LHEX:
2589 tprintf("= %#llx", tcp->u_lrval);
2590 break;
2591 case RVAL_LOCTAL:
2592 tprintf("= %#llo", tcp->u_lrval);
2593 break;
2594 */
2595 case RVAL_LUDECIMAL:
2596 tprintf("= %llu", tcp->u_lrval);
2597 break;
2598 /*
2599 case RVAL_LDECIMAL:
2600 tprintf("= %lld", tcp->u_lrval);
2601 break;
2602 */
2603#endif
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002604 default:
2605 fprintf(stderr,
2606 "invalid rval format\n");
2607 break;
2608 }
2609 }
2610 if ((sys_res & RVAL_STR) && tcp->auxstr)
2611 tprintf(" (%s)", tcp->auxstr);
2612 }
Denys Vlasenkoa50d2a82012-03-15 12:49:52 +01002613 if (Tflag) {
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002614 tv_sub(&tv, &tv, &tcp->etime);
2615 tprintf(" <%ld.%06ld>",
2616 (long) tv.tv_sec, (long) tv.tv_usec);
2617 }
Denys Vlasenko000b6012012-01-28 01:25:03 +01002618 tprints("\n");
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002619 dumpio(tcp);
Denys Vlasenko7de265d2012-03-13 11:44:31 +01002620 line_ended();
2621
Denys Vlasenko3b738812011-08-22 02:06:35 +02002622 ret:
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002623 tcp->flags &= ~TCB_INSYSCALL;
2624 return 0;
2625}
2626
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002627int
Dmitry V. Levin7d7c9632010-03-29 17:51:02 +00002628trace_syscall(struct tcb *tcp)
2629{
2630 return exiting(tcp) ?
2631 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2632}