blob: 34fc07213f29884ec2a6fea041483c683ddce94a [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.
32 *
33 * $Id$
34 */
35
36#include "defs.h"
37
38#include <signal.h>
39#include <time.h>
40#include <errno.h>
41#include <sys/user.h>
42#include <sys/syscall.h>
43#include <sys/param.h>
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000044
Wichert Akkerman15dea971999-10-06 13:06:34 +000045#if HAVE_ASM_REG_H
Roland McGrath6d1a65c2004-07-12 07:44:08 +000046#if defined (SPARC) || defined (SPARC64)
Wichert Akkerman00a82ee2001-03-28 20:29:17 +000047# define fpq kernel_fpq
48# define fq kernel_fq
49# define fpu kernel_fpu
50#endif
Wichert Akkerman9ce1a631999-08-29 23:15:07 +000051#include <asm/reg.h>
Roland McGrath6d1a65c2004-07-12 07:44:08 +000052#if defined (SPARC) || defined (SPARC64)
Wichert Akkerman00a82ee2001-03-28 20:29:17 +000053# undef fpq
54# undef fq
Roland McGrath761b5d72002-12-15 23:58:31 +000055# undef fpu
Wichert Akkerman00a82ee2001-03-28 20:29:17 +000056#endif
Wichert Akkerman9ce1a631999-08-29 23:15:07 +000057#endif
58
Wichert Akkerman15dea971999-10-06 13:06:34 +000059#ifdef HAVE_SYS_REG_H
60#include <sys/reg.h>
61#ifndef PTRACE_PEEKUSR
62# define PTRACE_PEEKUSR PTRACE_PEEKUSER
63#endif
Wichert Akkermanfaf72222000-02-19 23:59:03 +000064#elif defined(HAVE_LINUX_PTRACE_H)
65#undef PTRACE_SYSCALL
Roland McGrathce9f0742004-03-01 21:29:22 +000066# ifdef HAVE_STRUCT_IA64_FPREG
67# define ia64_fpreg XXX_ia64_fpreg
68# endif
69# ifdef HAVE_STRUCT_PT_ALL_USER_REGS
70# define pt_all_user_regs XXX_pt_all_user_regs
71# endif
Wichert Akkermanfaf72222000-02-19 23:59:03 +000072#include <linux/ptrace.h>
Roland McGrathce9f0742004-03-01 21:29:22 +000073# undef ia64_fpreg
74# undef pt_all_user_regs
Wichert Akkerman15dea971999-10-06 13:06:34 +000075#endif
76
Roland McGrath6d1a65c2004-07-12 07:44:08 +000077#if defined (LINUX) && defined (SPARC64)
78# define r_pc r_tpc
79# undef PTRACE_GETREGS
80# define PTRACE_GETREGS PTRACE_GETREGS64
81# undef PTRACE_SETREGS
82# define PTRACE_SETREGS PTRACE_SETREGS64
83#endif /* LINUX && SPARC64 */
84
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000085#if defined(LINUX) && defined(IA64)
86# include <asm/ptrace_offsets.h>
87# include <asm/rse.h>
88#endif
89
Pavel Machekd8ae7e32000-02-01 17:17:25 +000090#define NR_SYSCALL_BASE 0
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000091#ifdef LINUX
92#ifndef ERESTARTSYS
93#define ERESTARTSYS 512
94#endif
95#ifndef ERESTARTNOINTR
96#define ERESTARTNOINTR 513
97#endif
98#ifndef ERESTARTNOHAND
99#define ERESTARTNOHAND 514 /* restart if no handler.. */
100#endif
101#ifndef ENOIOCTLCMD
102#define ENOIOCTLCMD 515 /* No ioctl command */
103#endif
Roland McGrath9c555e72003-07-09 09:47:59 +0000104#ifndef ERESTART_RESTARTBLOCK
105#define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
106#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000107#ifndef NSIG
108#define NSIG 32
109#endif
110#ifdef ARM
111#undef NSIG
112#define NSIG 32
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000113#undef NR_SYSCALL_BASE
114#define NR_SYSCALL_BASE __NR_SYSCALL_BASE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000115#endif
116#endif /* LINUX */
117
118#include "syscall.h"
119
120/* Define these shorthand notations to simplify the syscallent files. */
121#define TF TRACE_FILE
122#define TI TRACE_IPC
123#define TN TRACE_NETWORK
124#define TP TRACE_PROCESS
125#define TS TRACE_SIGNAL
126
Roland McGrathee36ce12004-09-04 03:53:10 +0000127static const struct sysent sysent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000128#include "syscallent.h"
129};
Roland McGrathee36ce12004-09-04 03:53:10 +0000130static const int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000131
132#if SUPPORTED_PERSONALITIES >= 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000133static const struct sysent sysent1[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000134#include "syscallent1.h"
135};
Roland McGrathee36ce12004-09-04 03:53:10 +0000136static const int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000137#endif /* SUPPORTED_PERSONALITIES >= 2 */
138
139#if SUPPORTED_PERSONALITIES >= 3
Roland McGrathee36ce12004-09-04 03:53:10 +0000140static const struct sysent sysent2[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000141#include "syscallent2.h"
142};
Roland McGrathee36ce12004-09-04 03:53:10 +0000143static const int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000144#endif /* SUPPORTED_PERSONALITIES >= 3 */
145
Roland McGrathee36ce12004-09-04 03:53:10 +0000146const struct sysent *sysent;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000147int nsyscalls;
148
149/* Now undef them since short defines cause wicked namespace pollution. */
150#undef TF
151#undef TI
152#undef TN
153#undef TP
154#undef TS
155
Roland McGrathee36ce12004-09-04 03:53:10 +0000156static const char *const errnoent0[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000157#include "errnoent.h"
158};
Roland McGrathee36ce12004-09-04 03:53:10 +0000159static const int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000160
161#if SUPPORTED_PERSONALITIES >= 2
Roland McGrathee36ce12004-09-04 03:53:10 +0000162static const char *const errnoent1[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000163#include "errnoent1.h"
164};
Roland McGrathee36ce12004-09-04 03:53:10 +0000165static const int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000166#endif /* SUPPORTED_PERSONALITIES >= 2 */
167
168#if SUPPORTED_PERSONALITIES >= 3
Roland McGrathee36ce12004-09-04 03:53:10 +0000169static const char *const errnoent2[] = {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000170#include "errnoent2.h"
171};
Roland McGrathee36ce12004-09-04 03:53:10 +0000172static const int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000173#endif /* SUPPORTED_PERSONALITIES >= 3 */
174
Roland McGrathee36ce12004-09-04 03:53:10 +0000175const char *const *errnoent;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000176int nerrnos;
177
178int current_personality;
179
180int
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000181set_personality(personality)
182int personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000183{
184 switch (personality) {
185 case 0:
186 errnoent = errnoent0;
187 nerrnos = nerrnos0;
188 sysent = sysent0;
189 nsyscalls = nsyscalls0;
190 ioctlent = ioctlent0;
191 nioctlents = nioctlents0;
192 signalent = signalent0;
193 nsignals = nsignals0;
194 break;
195
196#if SUPPORTED_PERSONALITIES >= 2
197 case 1:
198 errnoent = errnoent1;
199 nerrnos = nerrnos1;
200 sysent = sysent1;
201 nsyscalls = nsyscalls1;
202 ioctlent = ioctlent1;
203 nioctlents = nioctlents1;
204 signalent = signalent1;
205 nsignals = nsignals1;
206 break;
207#endif /* SUPPORTED_PERSONALITIES >= 2 */
208
209#if SUPPORTED_PERSONALITIES >= 3
210 case 2:
211 errnoent = errnoent2;
212 nerrnos = nerrnos2;
213 sysent = sysent2;
214 nsyscalls = nsyscalls2;
215 ioctlent = ioctlent2;
216 nioctlents = nioctlents2;
217 signalent = signalent2;
218 nsignals = nsignals2;
219 break;
220#endif /* SUPPORTED_PERSONALITIES >= 3 */
221
222 default:
223 return -1;
224 }
225
226 current_personality = personality;
227 return 0;
228}
229
230int qual_flags[MAX_QUALS];
231
Roland McGrathe10e62a2004-09-04 04:20:43 +0000232
233struct call_counts {
234 struct timeval time;
235 int calls, errors;
236};
237
238static struct call_counts *counts;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000239
240static struct timeval shortest = { 1000000, 0 };
241
Roland McGrath9797ceb2002-12-30 10:23:00 +0000242static int qual_syscall(), qual_signal(), qual_fault(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000243
Roland McGrathe10e62a2004-09-04 04:20:43 +0000244static const struct qual_options {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000245 int bitflag;
246 char *option_name;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000247 int (*qualify)();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000248 char *argument_name;
249} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000250 { QUAL_TRACE, "trace", qual_syscall, "system call" },
251 { QUAL_TRACE, "t", qual_syscall, "system call" },
252 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
253 { QUAL_ABBREV, "a", qual_syscall, "system call" },
254 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
255 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
256 { QUAL_RAW, "raw", qual_syscall, "system call" },
257 { QUAL_RAW, "x", qual_syscall, "system call" },
258 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
259 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
260 { QUAL_SIGNAL, "s", qual_signal, "signal" },
261 { QUAL_FAULT, "fault", qual_fault, "fault" },
262 { QUAL_FAULT, "faults", qual_fault, "fault" },
263 { QUAL_FAULT, "m", qual_fault, "fault" },
264 { QUAL_READ, "read", qual_desc, "descriptor" },
265 { QUAL_READ, "reads", qual_desc, "descriptor" },
266 { QUAL_READ, "r", qual_desc, "descriptor" },
267 { QUAL_WRITE, "write", qual_desc, "descriptor" },
268 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
269 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000270 { 0, NULL, NULL, NULL },
271};
272
Roland McGrath9797ceb2002-12-30 10:23:00 +0000273static void
274qualify_one(n, opt, not)
275 int n;
Roland McGrathe10e62a2004-09-04 04:20:43 +0000276 const struct qual_options *opt;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000277 int not;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000278{
Roland McGrath9797ceb2002-12-30 10:23:00 +0000279 if (not)
280 qual_flags[n] &= ~opt->bitflag;
281 else
282 qual_flags[n] |= opt->bitflag;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000283}
284
285static int
Roland McGrath9797ceb2002-12-30 10:23:00 +0000286qual_syscall(s, opt, not)
287 char *s;
Roland McGrathe10e62a2004-09-04 04:20:43 +0000288 const struct qual_options *opt;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000289 int not;
290{
291 int i;
Roland McGrathfe6b3522005-02-02 04:40:11 +0000292 int rc = -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000293
294 for (i = 0; i < nsyscalls; i++) {
295 if (strcmp(s, sysent[i].sys_name) == 0) {
296 qualify_one(i, opt, not);
Roland McGrathfe6b3522005-02-02 04:40:11 +0000297 rc = 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000298 }
299 }
Roland McGrathfe6b3522005-02-02 04:40:11 +0000300 return rc;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000301}
302
303static int
304qual_signal(s, opt, not)
305 char *s;
Roland McGrathe10e62a2004-09-04 04:20:43 +0000306 const struct qual_options *opt;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000307 int not;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000308{
309 int i;
310 char buf[32];
311
Roland McGrathfe6b3522005-02-02 04:40:11 +0000312 if (s && *s && isdigit((unsigned char)*s)) {
313 int signo = atoi(s);
314 if (signo < 0 || signo >= MAX_QUALS)
315 return -1;
316 qualify_one(signo, opt, not);
317 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000318 }
Roland McGrathd62b6712004-03-02 06:52:01 +0000319 if (strlen(s) >= sizeof buf)
Roland McGrathfe6b3522005-02-02 04:40:11 +0000320 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000321 strcpy(buf, s);
322 s = buf;
323 for (i = 0; s[i]; i++)
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000324 s[i] = toupper((unsigned char)(s[i]));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000325 if (strncmp(s, "SIG", 3) == 0)
326 s += 3;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000327 for (i = 0; i <= NSIG; i++)
328 if (strcmp(s, signame(i) + 3) == 0) {
Roland McGrath76421df2005-02-02 03:51:18 +0000329 qualify_one(i, opt, not);
330 return 0;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000331 }
Roland McGrath76421df2005-02-02 03:51:18 +0000332 return -1;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000333}
334
335static int
336qual_fault(s, opt, not)
337 char *s;
Roland McGrathe10e62a2004-09-04 04:20:43 +0000338 const struct qual_options *opt;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000339 int not;
340{
341 return -1;
342}
343
344static int
345qual_desc(s, opt, not)
346 char *s;
Roland McGrathe10e62a2004-09-04 04:20:43 +0000347 const struct qual_options *opt;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000348 int not;
349{
350 if (s && *s && isdigit((unsigned char)*s)) {
Roland McGrathfe6b3522005-02-02 04:40:11 +0000351 int desc = atoi(s);
352 if (desc < 0 || desc >= MAX_QUALS)
353 return -1;
354 qualify_one(desc, opt, not);
Roland McGrath2b619022003-04-10 18:58:20 +0000355 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000356 }
357 return -1;
358}
359
360static int
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000361lookup_class(s)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000362 char *s;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000363{
364 if (strcmp(s, "file") == 0)
365 return TRACE_FILE;
366 if (strcmp(s, "ipc") == 0)
367 return TRACE_IPC;
368 if (strcmp(s, "network") == 0)
369 return TRACE_NETWORK;
370 if (strcmp(s, "process") == 0)
371 return TRACE_PROCESS;
372 if (strcmp(s, "signal") == 0)
373 return TRACE_SIGNAL;
374 return -1;
375}
376
377void
378qualify(s)
379char *s;
380{
Roland McGrathe10e62a2004-09-04 04:20:43 +0000381 const struct qual_options *opt;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000382 int not;
383 char *p;
384 int i, n;
385
386 opt = &qual_options[0];
387 for (i = 0; (p = qual_options[i].option_name); i++) {
388 n = strlen(p);
389 if (strncmp(s, p, n) == 0 && s[n] == '=') {
390 opt = &qual_options[i];
391 s += n + 1;
392 break;
393 }
394 }
395 not = 0;
396 if (*s == '!') {
397 not = 1;
398 s++;
399 }
400 if (strcmp(s, "none") == 0) {
401 not = 1 - not;
402 s = "all";
403 }
404 if (strcmp(s, "all") == 0) {
405 for (i = 0; i < MAX_QUALS; i++) {
406 if (not)
407 qual_flags[i] &= ~opt->bitflag;
408 else
409 qual_flags[i] |= opt->bitflag;
410 }
411 return;
412 }
413 for (i = 0; i < MAX_QUALS; i++) {
414 if (not)
415 qual_flags[i] |= opt->bitflag;
416 else
417 qual_flags[i] &= ~opt->bitflag;
418 }
419 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
420 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
421 for (i = 0; i < MAX_QUALS; i++) {
422 if (sysent[i].sys_flags & n) {
423 if (not)
424 qual_flags[i] &= ~opt->bitflag;
425 else
426 qual_flags[i] |= opt->bitflag;
427 }
428 }
429 continue;
430 }
Roland McGrath9797ceb2002-12-30 10:23:00 +0000431 if (opt->qualify(p, opt, not)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000432 fprintf(stderr, "strace: invalid %s `%s'\n",
433 opt->argument_name, p);
434 exit(1);
435 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000436 }
437 return;
438}
439
440static void
441dumpio(tcp)
442struct tcb *tcp;
443{
444 if (syserror(tcp))
445 return;
446 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
447 return;
Roland McGrath17352792005-06-07 23:21:26 +0000448 switch (known_scno(tcp)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000449 case SYS_read:
Roland McGrathaa510622004-08-31 07:47:45 +0000450#ifdef SYS_pread64
451 case SYS_pread64:
452#endif
453#if defined SYS_pread && SYS_pread64 != SYS_pread
454 case SYS_pread:
455#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000456#ifdef SYS_recv
457 case SYS_recv:
Roland McGrath17352792005-06-07 23:21:26 +0000458#elif defined SYS_sub_recv
459 case SYS_sub_recv:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000460#endif
461#ifdef SYS_recvfrom
462 case SYS_recvfrom:
Roland McGrath17352792005-06-07 23:21:26 +0000463#elif defined SYS_sub_recvfrom
464 case SYS_sub_recvfrom:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000465#endif
466 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
467 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
468 break;
469 case SYS_write:
Roland McGrathaa510622004-08-31 07:47:45 +0000470#ifdef SYS_pwrite64
471 case SYS_pwrite64:
472#endif
473#if defined SYS_pwrite && SYS_pwrite64 != SYS_pwrite
474 case SYS_pwrite:
475#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000476#ifdef SYS_send
477 case SYS_send:
Roland McGrath17352792005-06-07 23:21:26 +0000478#elif defined SYS_sub_send
479 case SYS_sub_send:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000480#endif
481#ifdef SYS_sendto
482 case SYS_sendto:
Roland McGrath17352792005-06-07 23:21:26 +0000483#elif defined SYS_sub_sendto
484 case SYS_sub_sendto:
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000485#endif
486 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
487 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
488 break;
John Hughes1d08dcf2001-07-10 13:48:44 +0000489#ifdef SYS_readv
490 case SYS_readv:
491 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
492 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
493 break;
494#endif
495#ifdef SYS_writev
496 case SYS_writev:
497
498 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
499 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
500 break;
501#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000502 }
503}
504
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000505#ifndef FREEBSD
Wichert Akkerman8829a551999-06-11 13:18:40 +0000506enum subcall_style { shift_style, deref_style, mask_style, door_style };
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000507#else /* FREEBSD */
508enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
509
510struct subcall {
511 int call;
512 int nsubcalls;
513 int subcalls[5];
514};
515
516const struct subcall subcalls_table[] = {
517 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
John Hughes61eeb552001-03-06 15:51:53 +0000518#ifdef SYS_semconfig
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000519 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
John Hughes61eeb552001-03-06 15:51:53 +0000520#else
521 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
522#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000523 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
524};
525#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000526
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000527#if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000528
529const int socket_map [] = {
530 /* SYS_SOCKET */ 97,
531 /* SYS_BIND */ 104,
532 /* SYS_CONNECT */ 98,
533 /* SYS_LISTEN */ 106,
534 /* SYS_ACCEPT */ 99,
535 /* SYS_GETSOCKNAME */ 150,
536 /* SYS_GETPEERNAME */ 141,
537 /* SYS_SOCKETPAIR */ 135,
538 /* SYS_SEND */ 101,
539 /* SYS_RECV */ 102,
540 /* SYS_SENDTO */ 133,
541 /* SYS_RECVFROM */ 125,
542 /* SYS_SHUTDOWN */ 134,
543 /* SYS_SETSOCKOPT */ 105,
544 /* SYS_GETSOCKOPT */ 118,
545 /* SYS_SENDMSG */ 114,
546 /* SYS_RECVMSG */ 113
547};
548
549void
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000550sparc_socket_decode (tcp)
551struct tcb *tcp;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000552{
553 volatile long addr;
554 volatile int i, n;
555
556 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
557 return;
558 }
559 tcp->scno = socket_map [tcp->u_arg [0]-1];
560 n = tcp->u_nargs = sysent [tcp->scno].nargs;
561 addr = tcp->u_arg [1];
562 for (i = 0; i < n; i++){
563 int arg;
564 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
565 arg = 0;
566 tcp->u_arg [i] = arg;
567 addr += sizeof (arg);
568 }
569}
570
Michal Ludvig0e035502002-09-23 15:41:01 +0000571void
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000572decode_subcall(tcp, subcall, nsubcalls, style)
573struct tcb *tcp;
574int subcall;
575int nsubcalls;
576enum subcall_style style;
577{
Michal Ludvig10a88d02002-10-07 14:31:00 +0000578 long addr, mask, arg;
579 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000580
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000581 switch (style) {
582 case shift_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000583 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
584 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000585 tcp->scno = subcall + tcp->u_arg[0];
586 if (sysent[tcp->scno].nargs != -1)
587 tcp->u_nargs = sysent[tcp->scno].nargs;
588 else
589 tcp->u_nargs--;
590 for (i = 0; i < tcp->u_nargs; i++)
591 tcp->u_arg[i] = tcp->u_arg[i + 1];
592 break;
593 case deref_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000594 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
595 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000596 tcp->scno = subcall + tcp->u_arg[0];
597 addr = tcp->u_arg[1];
598 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
599 if (umove(tcp, addr, &arg) < 0)
600 arg = 0;
601 tcp->u_arg[i] = arg;
602 addr += sizeof(arg);
603 }
604 tcp->u_nargs = sysent[tcp->scno].nargs;
605 break;
606 case mask_style:
607 mask = (tcp->u_arg[0] >> 8) & 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000608 for (i = 0; mask; i++)
609 mask >>= 1;
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000610 if (i >= nsubcalls)
611 return;
612 tcp->u_arg[0] &= 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000613 tcp->scno = subcall + i;
614 if (sysent[tcp->scno].nargs != -1)
615 tcp->u_nargs = sysent[tcp->scno].nargs;
616 break;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000617 case door_style:
618 /*
619 * Oh, yuck. The call code is the *sixth* argument.
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000620 * (don't you mean the *last* argument? - JH)
Wichert Akkerman8829a551999-06-11 13:18:40 +0000621 */
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000622 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
623 return;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000624 tcp->scno = subcall + tcp->u_arg[5];
625 if (sysent[tcp->scno].nargs != -1)
626 tcp->u_nargs = sysent[tcp->scno].nargs;
627 else
628 tcp->u_nargs--;
629 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000630#ifdef FREEBSD
631 case table_style:
632 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
633 if (subcalls_table[i].call == tcp->scno) break;
634 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
635 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
636 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
637 for (i = 0; i < tcp->u_nargs; i++)
638 tcp->u_arg[i] = tcp->u_arg[i + 1];
639 }
640 break;
641#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000642 }
643}
644#endif
645
646struct tcb *tcp_last = NULL;
647
648static int
649internal_syscall(tcp)
650struct tcb *tcp;
651{
652 /*
653 * We must always trace a few critical system calls in order to
654 * correctly support following forks in the presence of tracing
655 * qualifiers.
656 */
Roland McGrath17352792005-06-07 23:21:26 +0000657 switch (known_scno(tcp)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000658#ifdef SYS_fork
659 case SYS_fork:
660#endif
661#ifdef SYS_vfork
662 case SYS_vfork:
663#endif
John Hughes4e36a812001-04-18 15:11:51 +0000664#ifdef SYS_fork1
665 case SYS_fork1:
666#endif
667#ifdef SYS_forkall
668 case SYS_forkall:
669#endif
670#ifdef SYS_rfork1
671 case SYS_rfork1:
672#endif
673#ifdef SYS_rforkall
674 case SYS_rforkall:
675#endif
Roland McGrathf3a0e1b2003-02-20 02:45:22 +0000676#ifdef SYS_rfork
677 case SYS_rfork:
678#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000679 internal_fork(tcp);
680 break;
Wichert Akkerman7a0b6491999-12-23 15:08:17 +0000681#ifdef SYS_clone
682 case SYS_clone:
683 internal_clone(tcp);
684 break;
685#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000686#ifdef SYS_clone2
687 case SYS_clone2:
688 internal_clone(tcp);
689 break;
690#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000691#ifdef SYS_execv
692 case SYS_execv:
693#endif
694#ifdef SYS_execve
695 case SYS_execve:
696#endif
John Hughes4e36a812001-04-18 15:11:51 +0000697#ifdef SYS_rexecve
698 case SYS_rexecve:
699#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000700 internal_exec(tcp);
701 break;
702
703#ifdef SYS_wait
704 case SYS_wait:
705#endif
706#ifdef SYS_wait4
707 case SYS_wait4:
708#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000709#ifdef SYS32_wait4
710 case SYS32_wait4:
711#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000712#ifdef SYS_waitpid
713 case SYS_waitpid:
714#endif
715#ifdef SYS_waitsys
716 case SYS_waitsys:
717#endif
Roland McGrathc74c0b72004-09-01 19:39:46 +0000718 internal_wait(tcp, 2);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000719 break;
Roland McGrathc74c0b72004-09-01 19:39:46 +0000720#ifdef SYS_waitid
721 case SYS_waitid:
722 internal_wait(tcp, 3);
723 break;
724#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000725
726#ifdef SYS_exit
727 case SYS_exit:
728#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000729#ifdef SYS32_exit
730 case SYS32_exit:
731#endif
Roland McGrath923f7502003-01-09 06:53:27 +0000732#ifdef __NR_exit_group
733 case __NR_exit_group:
734#endif
Roland McGrath08267b82004-02-20 22:56:43 +0000735#ifdef IA64
736 case 252: /* IA-32 __NR_exit_group */
737#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000738 internal_exit(tcp);
739 break;
740 }
741 return 0;
742}
743
Wichert Akkermanc7926982000-04-10 22:22:31 +0000744
745#ifdef LINUX
746#if defined (I386)
747 static long eax;
748#elif defined (IA64)
749 long r8, r10, psr;
750 long ia32 = 0;
751#elif defined (POWERPC)
752 static long result,flags;
753#elif defined (M68K)
754 static int d0;
755#elif defined (ARM)
Roland McGrath0f87c492003-06-03 23:29:04 +0000756 static struct pt_regs regs;
Wichert Akkermanc7926982000-04-10 22:22:31 +0000757#elif defined (ALPHA)
758 static long r0;
759 static long a3;
Roland McGrath6d1a65c2004-07-12 07:44:08 +0000760#elif defined (SPARC) || defined (SPARC64)
Wichert Akkerman00a82ee2001-03-28 20:29:17 +0000761 static struct regs regs;
Wichert Akkermanc7926982000-04-10 22:22:31 +0000762 static unsigned long trap;
763#elif defined(MIPS)
764 static long a3;
765 static long r2;
Michal Ludvig10a88d02002-10-07 14:31:00 +0000766#elif defined(S390) || defined(S390X)
Wichert Akkermanc7926982000-04-10 22:22:31 +0000767 static long gpr2;
768 static long pc;
Michal Ludvig882eda82002-11-11 12:50:47 +0000769 static long syscall_mode;
Wichert Akkermanc1652e22001-03-27 12:17:16 +0000770#elif defined(HPPA)
771 static long r28;
Wichert Akkermanccef6372002-05-01 16:39:22 +0000772#elif defined(SH)
773 static long r0;
Roland McGrathf5a47772003-06-26 22:40:42 +0000774#elif defined(SH64)
Roland McGrath0f87c492003-06-03 23:29:04 +0000775 static long r9;
Michal Ludvig0e035502002-09-23 15:41:01 +0000776#elif defined(X86_64)
777 static long rax;
Roland McGrath761b5d72002-12-15 23:58:31 +0000778#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000779#endif /* LINUX */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000780#ifdef FREEBSD
781 struct reg regs;
Roland McGrath761b5d72002-12-15 23:58:31 +0000782#endif /* FREEBSD */
Wichert Akkermanc7926982000-04-10 22:22:31 +0000783
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000784int
Pavel Machek4dc3b142000-02-01 17:58:41 +0000785get_scno(tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000786struct tcb *tcp;
787{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000788 long scno = 0;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000789#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000790 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +0000791#endif /* !PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000792
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000793#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +0000794#if defined(S390) || defined(S390X)
Roland McGrath96dc5142003-01-20 10:23:04 +0000795 if (tcp->flags & TCB_WAITEXECVE) {
796 /*
797 * When the execve system call completes successfully, the
798 * new process still has -ENOSYS (old style) or __NR_execve
799 * (new style) in gpr2. We cannot recover the scno again
800 * by disassembly, because the image that executed the
801 * syscall is gone now. Fortunately, we don't want it. We
802 * leave the flag set so that syscall_fixup can fake the
803 * result.
804 */
805 if (tcp->flags & TCB_INSYSCALL)
806 return 1;
807 /*
808 * This is the SIGTRAP after execve. We cannot try to read
809 * the system call here either.
810 */
811 tcp->flags &= ~TCB_WAITEXECVE;
812 return 0;
813 }
Roland McGrath2f924ca2003-06-26 22:23:28 +0000814
815 if (upeek(pid, PT_GPR2, &syscall_mode) < 0)
816 return -1;
817
818 if (syscall_mode != -ENOSYS) {
819 /*
820 * Since kernel version 2.5.44 the scno gets passed in gpr2.
821 */
822 scno = syscall_mode;
823 } else {
Michal Ludvig882eda82002-11-11 12:50:47 +0000824 /*
825 * Old style of "passing" the scno via the SVC instruction.
826 */
827
828 long opcode, offset_reg, tmp;
829 void * svc_addr;
830 int gpr_offset[16] = {PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
831 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
832 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
833 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15};
Roland McGrath761b5d72002-12-15 23:58:31 +0000834
Michal Ludvig882eda82002-11-11 12:50:47 +0000835 if (upeek(pid, PT_PSWADDR, &pc) < 0)
836 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +0000837 errno = 0;
Michal Ludvig882eda82002-11-11 12:50:47 +0000838 opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +0000839 if (errno) {
840 perror("peektext(pc-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +0000841 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +0000842 }
Michal Ludvig882eda82002-11-11 12:50:47 +0000843
844 /*
845 * We have to check if the SVC got executed directly or via an
846 * EXECUTE instruction. In case of EXECUTE it is necessary to do
847 * instruction decoding to derive the system call number.
848 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
849 * so that this doesn't work if a SVC opcode is part of an EXECUTE
850 * opcode. Since there is no way to find out the opcode size this
851 * is the best we can do...
852 */
853
854 if ((opcode & 0xff00) == 0x0a00) {
855 /* SVC opcode */
856 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +0000857 }
Michal Ludvig882eda82002-11-11 12:50:47 +0000858 else {
859 /* SVC got executed by EXECUTE instruction */
860
861 /*
862 * Do instruction decoding of EXECUTE. If you really want to
863 * understand this, read the Principles of Operations.
864 */
865 svc_addr = (void *) (opcode & 0xfff);
866
867 tmp = 0;
868 offset_reg = (opcode & 0x000f0000) >> 16;
869 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
870 return -1;
871 svc_addr += tmp;
872
873 tmp = 0;
874 offset_reg = (opcode & 0x0000f000) >> 12;
875 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
876 return -1;
877 svc_addr += tmp;
878
879 scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0);
880 if (errno)
881 return -1;
882#if defined(S390X)
883 scno >>= 48;
884#else
885 scno >>= 16;
886#endif
887 tmp = 0;
888 offset_reg = (opcode & 0x00f00000) >> 20;
889 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
890 return -1;
891
892 scno = (scno | tmp) & 0xff;
893 }
894 }
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000895#elif defined (POWERPC)
Roland McGratheb285352003-01-14 09:59:00 +0000896 if (upeek(pid, sizeof(unsigned long)*PT_R0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000897 return -1;
898 if (!(tcp->flags & TCB_INSYSCALL)) {
899 /* Check if we return from execve. */
900 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
901 tcp->flags &= ~TCB_WAITEXECVE;
902 return 0;
903 }
904 }
905#elif defined (I386)
906 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
907 return -1;
Michal Ludvig0e035502002-09-23 15:41:01 +0000908#elif defined (X86_64)
909 if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
910 return -1;
911
Roland McGrath761b5d72002-12-15 23:58:31 +0000912 if (!(tcp->flags & TCB_INSYSCALL)) {
913 static int currpers=-1;
Michal Ludvig0e035502002-09-23 15:41:01 +0000914 long val;
915
916 /* Check CS register value. On x86-64 linux it is:
917 * 0x33 for long mode (64 bit)
918 * 0x23 for compatibility mode (32 bit)
Roland McGrath761b5d72002-12-15 23:58:31 +0000919 * It takes only one ptrace and thus doesn't need
Michal Ludvig0e035502002-09-23 15:41:01 +0000920 * to be cached.
921 */
922 if (upeek(pid, 8*CS, &val) < 0)
923 return -1;
924 switch(val)
925 {
926 case 0x23: currpers = 1; break;
927 case 0x33: currpers = 0; break;
928 default:
929 fprintf(stderr, "Unknown value CS=0x%02X while "
930 "detecting personality of process "
931 "PID=%d\n", (int)val, pid);
932 currpers = current_personality;
933 break;
934 }
935#if 0
936 /* This version analyzes the opcode of a syscall instruction.
937 * (int 0x80 on i386 vs. syscall on x86-64)
938 * It works, but is too complicated.
939 */
940 unsigned long val, rip, i;
941
942 if(upeek(pid, 8*RIP, &rip)<0)
943 perror("upeek(RIP)");
Roland McGrath761b5d72002-12-15 23:58:31 +0000944
Michal Ludvig0e035502002-09-23 15:41:01 +0000945 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
946 rip-=2;
947 errno = 0;
948
Roland McGrath761b5d72002-12-15 23:58:31 +0000949 call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
950 if (errno)
951 printf("ptrace_peektext failed: %s\n",
Michal Ludvig0e035502002-09-23 15:41:01 +0000952 strerror(errno));
953 switch (call & 0xffff)
954 {
955 /* x86-64: syscall = 0x0f 0x05 */
956 case 0x050f: currpers = 0; break;
957 /* i386: int 0x80 = 0xcd 0x80 */
958 case 0x80cd: currpers = 1; break;
959 default:
960 currpers = current_personality;
Roland McGrath761b5d72002-12-15 23:58:31 +0000961 fprintf(stderr,
Michal Ludvig0e035502002-09-23 15:41:01 +0000962 "Unknown syscall opcode (0x%04X) while "
963 "detecting personality of process "
964 "PID=%d\n", (int)call, pid);
965 break;
966 }
967#endif
968 if(currpers != current_personality)
969 {
970 char *names[]={"64 bit", "32 bit"};
971 set_personality(currpers);
Roland McGrath761b5d72002-12-15 23:58:31 +0000972 printf("[ Process PID=%d runs in %s mode. ]\n",
Michal Ludvig0e035502002-09-23 15:41:01 +0000973 pid, names[current_personality]);
974 }
Roland McGrath761b5d72002-12-15 23:58:31 +0000975 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000976#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000977# define IA64_PSR_IS ((long)1 << 34)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000978 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000979 ia32 = (psr & IA64_PSR_IS) != 0;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000980 if (!(tcp->flags & TCB_INSYSCALL)) {
981 if (ia32) {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000982 if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000983 return -1;
984 } else {
985 if (upeek (pid, PT_R15, &scno) < 0)
986 return -1;
987 }
Roland McGrathba954762003-03-05 06:29:06 +0000988 /* Check if we return from execve. */
989 if (tcp->flags & TCB_WAITEXECVE) {
990 tcp->flags &= ~TCB_WAITEXECVE;
991 return 0;
992 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000993 } else {
994 /* syscall in progress */
995 if (upeek (pid, PT_R8, &r8) < 0)
996 return -1;
997 if (upeek (pid, PT_R10, &r10) < 0)
998 return -1;
999 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001000#elif defined (ARM)
Roland McGrath0f87c492003-06-03 23:29:04 +00001001 /*
1002 * Read complete register set in one go.
1003 */
1004 if (ptrace(PTRACE_GETREGS, pid, NULL, (void *)&regs) == -1)
1005 return -1;
1006
1007 /*
1008 * We only need to grab the syscall number on syscall entry.
1009 */
1010 if (regs.ARM_ip == 0) {
1011 /*
1012 * Note: we only deal with only 32-bit CPUs here.
1013 */
1014 if (regs.ARM_cpsr & 0x20) {
1015 /*
1016 * Get the Thumb-mode system call number
1017 */
1018 scno = regs.ARM_r7;
1019 } else {
1020 /*
1021 * Get the ARM-mode system call number
1022 */
1023 errno = 0;
1024 scno = ptrace(PTRACE_PEEKTEXT, pid, (void *)(regs.ARM_pc - 4), NULL);
1025 if (errno)
1026 return -1;
1027
1028 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
1029 tcp->flags &= ~TCB_WAITEXECVE;
1030 return 0;
1031 }
1032
1033 if ((scno & 0x0ff00000) != 0x0f900000) {
1034 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
1035 scno);
1036 return -1;
1037 }
1038
1039 /*
1040 * Fixup the syscall number
1041 */
1042 scno &= 0x000fffff;
1043 }
1044
1045 if (tcp->flags & TCB_INSYSCALL) {
1046 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
1047 tcp->flags &= ~TCB_INSYSCALL;
1048 }
1049 } else {
1050 if (!(tcp->flags & TCB_INSYSCALL)) {
1051 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
1052 tcp->flags |= TCB_INSYSCALL;
1053 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001054 }
1055#elif defined (M68K)
1056 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
1057 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001058#elif defined (MIPS)
1059 if (upeek(pid, REG_A3, &a3) < 0)
1060 return -1;
1061
1062 if(!(tcp->flags & TCB_INSYSCALL)) {
1063 if (upeek(pid, REG_V0, &scno) < 0)
1064 return -1;
1065
1066 if (scno < 0 || scno > nsyscalls) {
1067 if(a3 == 0 || a3 == -1) {
1068 if(debug)
1069 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
1070 return 0;
1071 }
1072 }
1073 } else {
1074 if (upeek(pid, REG_V0, &r2) < 0)
1075 return -1;
1076 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001077#elif defined (ALPHA)
1078 if (upeek(pid, REG_A3, &a3) < 0)
1079 return -1;
1080
1081 if (!(tcp->flags & TCB_INSYSCALL)) {
1082 if (upeek(pid, REG_R0, &scno) < 0)
1083 return -1;
1084
1085 /* Check if we return from execve. */
1086 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1087 tcp->flags &= ~TCB_WAITEXECVE;
1088 return 0;
1089 }
1090
1091 /*
1092 * Do some sanity checks to figure out if it's
1093 * really a syscall entry
1094 */
1095 if (scno < 0 || scno > nsyscalls) {
1096 if (a3 == 0 || a3 == -1) {
1097 if (debug)
1098 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
1099 return 0;
1100 }
1101 }
1102 }
1103 else {
1104 if (upeek(pid, REG_R0, &r0) < 0)
1105 return -1;
1106 }
Roland McGrath6d1a65c2004-07-12 07:44:08 +00001107#elif defined (SPARC) || defined (SPARC64)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001108 /* Everything we need is in the current register set. */
1109 if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
1110 return -1;
1111
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001112 /* If we are entering, then disassemble the syscall trap. */
1113 if (!(tcp->flags & TCB_INSYSCALL)) {
1114 /* Retrieve the syscall trap instruction. */
1115 errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001116 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
Roland McGrath6d1a65c2004-07-12 07:44:08 +00001117#if defined(SPARC64)
1118 trap >>= 32;
1119#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001120 if (errno)
1121 return -1;
1122
1123 /* Disassemble the trap to see what personality to use. */
1124 switch (trap) {
1125 case 0x91d02010:
1126 /* Linux/SPARC syscall trap. */
1127 set_personality(0);
1128 break;
Wichert Akkermandacfb6e1999-06-03 14:21:07 +00001129 case 0x91d0206d:
1130 /* Linux/SPARC64 syscall trap. */
Roland McGrath6d1a65c2004-07-12 07:44:08 +00001131 set_personality(2);
1132 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001133 case 0x91d02000:
1134 /* SunOS syscall trap. (pers 1) */
1135 fprintf(stderr,"syscall: SunOS no support\n");
1136 return -1;
1137 case 0x91d02008:
1138 /* Solaris 2.x syscall trap. (per 2) */
1139 set_personality(1);
Roland McGrath761b5d72002-12-15 23:58:31 +00001140 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001141 case 0x91d02009:
1142 /* NetBSD/FreeBSD syscall trap. */
1143 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
1144 return -1;
1145 case 0x91d02027:
1146 /* Solaris 2.x gettimeofday */
1147 set_personality(1);
1148 break;
1149 default:
1150 /* Unknown syscall trap. */
1151 if(tcp->flags & TCB_WAITEXECVE) {
1152 tcp->flags &= ~TCB_WAITEXECVE;
1153 return 0;
1154 }
Roland McGrath6d1a65c2004-07-12 07:44:08 +00001155#if defined (SPARC64)
1156 fprintf(stderr,"syscall: unknown syscall trap %08lx %016lx\n", trap, regs.r_tpc);
1157#else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001158 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
Roland McGrath6d1a65c2004-07-12 07:44:08 +00001159#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001160 return -1;
1161 }
1162
1163 /* Extract the system call number from the registers. */
1164 if (trap == 0x91d02027)
1165 scno = 156;
1166 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001167 scno = regs.r_g1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001168 if (scno == 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001169 scno = regs.r_o0;
1170 memmove (&regs.r_o0, &regs.r_o1, 7*sizeof(regs.r_o0));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001171 }
1172 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001173#elif defined(HPPA)
1174 if (upeek(pid, PT_GR20, &scno) < 0)
1175 return -1;
1176 if (!(tcp->flags & TCB_INSYSCALL)) {
1177 /* Check if we return from execve. */
1178 if ((tcp->flags & TCB_WAITEXECVE)) {
1179 tcp->flags &= ~TCB_WAITEXECVE;
1180 return 0;
1181 }
1182 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001183#elif defined(SH)
1184 /*
1185 * In the new syscall ABI, the system call number is in R3.
1186 */
1187 if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
1188 return -1;
1189
1190 if (scno < 0) {
1191 /* Odd as it may seem, a glibc bug has been known to cause
1192 glibc to issue bogus negative syscall numbers. So for
1193 our purposes, make strace print what it *should* have been */
1194 long correct_scno = (scno & 0xff);
1195 if (debug)
1196 fprintf(stderr,
Michal Ludvig53b320f2002-09-23 13:30:09 +00001197 "Detected glibc bug: bogus system call number = %ld, "
1198 "correcting to %ld\n",
Wichert Akkermanccef6372002-05-01 16:39:22 +00001199 scno,
1200 correct_scno);
1201 scno = correct_scno;
1202 }
1203
1204
1205 if (!(tcp->flags & TCB_INSYSCALL)) {
1206 /* Check if we return from execve. */
1207 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1208 tcp->flags &= ~TCB_WAITEXECVE;
1209 return 0;
1210 }
1211 }
Roland McGrathf5a47772003-06-26 22:40:42 +00001212#elif defined(SH64)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001213 if (upeek(pid, REG_SYSCALL, &scno) < 0)
1214 return -1;
1215 scno &= 0xFFFF;
1216
1217 if (!(tcp->flags & TCB_INSYSCALL)) {
1218 /* Check if we return from execve. */
1219 if (tcp->flags & TCB_WAITEXECVE) {
1220 tcp->flags &= ~TCB_WAITEXECVE;
1221 return 0;
1222 }
1223 }
Roland McGrathf5a47772003-06-26 22:40:42 +00001224#endif /* SH64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001225#endif /* LINUX */
1226#ifdef SUNOS4
1227 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
1228 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001229#elif defined(SH)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001230 /* new syscall ABI returns result in R0 */
1231 if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
1232 return -1;
Roland McGrathf5a47772003-06-26 22:40:42 +00001233#elif defined(SH64)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001234 /* ABI defines result returned in r9 */
1235 if (upeek(pid, REG_GENERAL(9), (long *)&r9) < 0)
1236 return -1;
1237
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001238#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001239#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001240#ifdef HAVE_PR_SYSCALL
John Hughes25299712001-03-06 10:10:06 +00001241 scno = tcp->status.PR_SYSCALL;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001242#else /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001243#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001244 scno = tcp->status.PR_WHAT;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001245#else /* FREEBSD */
1246 if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1247 perror("pread");
1248 return -1;
1249 }
1250 switch (regs.r_eax) {
1251 case SYS_syscall:
1252 case SYS___syscall:
1253 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1254 break;
1255 default:
1256 scno = regs.r_eax;
1257 break;
1258 }
1259#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001260#endif /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001261#endif /* USE_PROCFS */
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +00001262 if (!(tcp->flags & TCB_INSYSCALL))
1263 tcp->scno = scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +00001264 return 1;
1265}
1266
Pavel Machek4dc3b142000-02-01 17:58:41 +00001267
Roland McGrath17352792005-06-07 23:21:26 +00001268long
1269known_scno(tcp)
1270struct tcb *tcp;
1271{
1272 long scno = tcp->scno;
1273 if (scno >= 0 && scno < nsyscalls && sysent[scno].native_scno != 0)
1274 scno = sysent[scno].native_scno;
1275 else
1276 scno += NR_SYSCALL_BASE;
1277 return scno;
1278}
1279
Pavel Machek4dc3b142000-02-01 17:58:41 +00001280int
1281syscall_fixup(tcp)
1282struct tcb *tcp;
1283{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001284#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001285 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +00001286#else /* USE_PROCFS */
Roland McGrath17352792005-06-07 23:21:26 +00001287 int scno = known_scno(tcp);
Pavel Machek4dc3b142000-02-01 17:58:41 +00001288
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001289 if (!(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001290 if (tcp->status.PR_WHY != PR_SYSENTRY) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001291 if (
1292 scno == SYS_fork
1293#ifdef SYS_vfork
1294 || scno == SYS_vfork
1295#endif /* SYS_vfork */
John Hughes4e36a812001-04-18 15:11:51 +00001296#ifdef SYS_fork1
1297 || scno == SYS_fork1
1298#endif /* SYS_fork1 */
1299#ifdef SYS_forkall
1300 || scno == SYS_forkall
1301#endif /* SYS_forkall */
1302#ifdef SYS_rfork1
1303 || scno == SYS_rfork1
1304#endif /* SYS_fork1 */
1305#ifdef SYS_rforkall
1306 || scno == SYS_rforkall
1307#endif /* SYS_rforkall */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001308 ) {
1309 /* We are returning in the child, fake it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001310 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001311 trace_syscall(tcp);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001312 tcp->status.PR_WHY = PR_SYSEXIT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001313 }
1314 else {
1315 fprintf(stderr, "syscall: missing entry\n");
1316 tcp->flags |= TCB_INSYSCALL;
1317 }
1318 }
1319 }
1320 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001321 if (tcp->status.PR_WHY != PR_SYSEXIT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001322 fprintf(stderr, "syscall: missing exit\n");
1323 tcp->flags &= ~TCB_INSYSCALL;
1324 }
1325 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001326#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001327#ifdef SUNOS4
1328 if (!(tcp->flags & TCB_INSYSCALL)) {
1329 if (scno == 0) {
1330 fprintf(stderr, "syscall: missing entry\n");
1331 tcp->flags |= TCB_INSYSCALL;
1332 }
1333 }
1334 else {
1335 if (scno != 0) {
1336 if (debug) {
1337 /*
1338 * This happens when a signal handler
1339 * for a signal which interrupted a
1340 * a system call makes another system call.
1341 */
1342 fprintf(stderr, "syscall: missing exit\n");
1343 }
1344 tcp->flags &= ~TCB_INSYSCALL;
1345 }
1346 }
1347#endif /* SUNOS4 */
1348#ifdef LINUX
1349#if defined (I386)
1350 if (upeek(pid, 4*EAX, &eax) < 0)
1351 return -1;
1352 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1353 if (debug)
1354 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1355 return 0;
1356 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001357#elif defined (X86_64)
1358 if (upeek(pid, 8*RAX, &rax) < 0)
1359 return -1;
Roland McGrath998fac72004-06-23 01:40:45 +00001360 if (current_personality == 1)
1361 rax = (long int)(int)rax; /* sign extend from 32 bits */
Michal Ludvig0e035502002-09-23 15:41:01 +00001362 if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1363 if (debug)
1364 fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1365 return 0;
1366 }
Michal Ludvig10a88d02002-10-07 14:31:00 +00001367#elif defined (S390) || defined (S390X)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001368 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1369 return -1;
Michal Ludvig882eda82002-11-11 12:50:47 +00001370 if (syscall_mode != -ENOSYS)
1371 syscall_mode = tcp->scno;
1372 if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001373 if (debug)
1374 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1375 return 0;
1376 }
Roland McGrath96dc5142003-01-20 10:23:04 +00001377 else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE))
1378 == (TCB_INSYSCALL|TCB_WAITEXECVE))
1379 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1380 /*
1381 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1382 * flag set for the post-execve SIGTRAP to see and reset.
1383 */
1384 gpr2 = 0;
1385 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001386#elif defined (POWERPC)
1387# define SO_MASK 0x10000000
Roland McGratheb285352003-01-14 09:59:00 +00001388 if (upeek(pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001389 return -1;
Roland McGratheb285352003-01-14 09:59:00 +00001390 if (upeek(pid, sizeof(unsigned long)*PT_R3, &result) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001391 return -1;
1392 if (flags & SO_MASK)
1393 result = -result;
1394#elif defined (M68K)
1395 if (upeek(pid, 4*PT_D0, &d0) < 0)
1396 return -1;
1397 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1398 if (debug)
1399 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1400 return 0;
1401 }
1402#elif defined (ARM)
Roland McGrath0f87c492003-06-03 23:29:04 +00001403 /*
1404 * Nothing required
1405 */
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001406#elif defined (HPPA)
1407 if (upeek(pid, PT_GR28, &r28) < 0)
1408 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001409#elif defined(IA64)
1410 if (upeek(pid, PT_R10, &r10) < 0)
1411 return -1;
1412 if (upeek(pid, PT_R8, &r8) < 0)
1413 return -1;
1414 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1415 if (debug)
1416 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1417 return 0;
1418 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001419#endif
1420#endif /* LINUX */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001421 return 1;
1422}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001423
Pavel Machek4dc3b142000-02-01 17:58:41 +00001424int
1425get_error(tcp)
1426struct tcb *tcp;
1427{
1428 int u_error = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001429#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +00001430#if defined(S390) || defined(S390X)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001431 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1432 tcp->u_rval = -1;
1433 u_error = -gpr2;
1434 }
1435 else {
1436 tcp->u_rval = gpr2;
1437 u_error = 0;
1438 }
Michal Ludvig10a88d02002-10-07 14:31:00 +00001439#else /* !S390 && !S390X */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001440#ifdef I386
1441 if (eax < 0 && -eax < nerrnos) {
1442 tcp->u_rval = -1;
1443 u_error = -eax;
1444 }
1445 else {
1446 tcp->u_rval = eax;
1447 u_error = 0;
1448 }
1449#else /* !I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001450#ifdef X86_64
1451 if (rax < 0 && -rax < nerrnos) {
1452 tcp->u_rval = -1;
1453 u_error = -rax;
1454 }
1455 else {
1456 tcp->u_rval = rax;
1457 u_error = 0;
1458 }
1459#else
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001460#ifdef IA64
1461 if (ia32) {
1462 int err;
1463
1464 err = (int)r8;
1465 if (err < 0 && -err < nerrnos) {
1466 tcp->u_rval = -1;
1467 u_error = -err;
1468 }
1469 else {
1470 tcp->u_rval = err;
1471 u_error = 0;
1472 }
1473 } else {
1474 if (r10) {
1475 tcp->u_rval = -1;
1476 u_error = r8;
1477 } else {
1478 tcp->u_rval = r8;
1479 u_error = 0;
1480 }
1481 }
1482#else /* !IA64 */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001483#ifdef MIPS
1484 if (a3) {
1485 tcp->u_rval = -1;
1486 u_error = r2;
1487 } else {
1488 tcp->u_rval = r2;
1489 u_error = 0;
1490 }
1491#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001492#ifdef POWERPC
Roland McGrath190f8dd2004-01-13 10:13:44 +00001493 if (result && (unsigned long) -result < nerrnos) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001494 tcp->u_rval = -1;
1495 u_error = -result;
1496 }
1497 else {
1498 tcp->u_rval = result;
1499 u_error = 0;
1500 }
1501#else /* !POWERPC */
1502#ifdef M68K
1503 if (d0 && (unsigned) -d0 < nerrnos) {
1504 tcp->u_rval = -1;
1505 u_error = -d0;
1506 }
1507 else {
1508 tcp->u_rval = d0;
1509 u_error = 0;
1510 }
1511#else /* !M68K */
1512#ifdef ARM
Roland McGrath0f87c492003-06-03 23:29:04 +00001513 if (regs.ARM_r0 && (unsigned) -regs.ARM_r0 < nerrnos) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001514 tcp->u_rval = -1;
Roland McGrath0f87c492003-06-03 23:29:04 +00001515 u_error = -regs.ARM_r0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001516 }
1517 else {
Roland McGrath0f87c492003-06-03 23:29:04 +00001518 tcp->u_rval = regs.ARM_r0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001519 u_error = 0;
1520 }
1521#else /* !ARM */
1522#ifdef ALPHA
1523 if (a3) {
1524 tcp->u_rval = -1;
1525 u_error = r0;
1526 }
1527 else {
1528 tcp->u_rval = r0;
1529 u_error = 0;
1530 }
1531#else /* !ALPHA */
1532#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001533 if (regs.r_psr & PSR_C) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001534 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001535 u_error = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001536 }
1537 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001538 tcp->u_rval = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001539 u_error = 0;
1540 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001541#else /* !SPARC */
Roland McGrath6d1a65c2004-07-12 07:44:08 +00001542#ifdef SPARC64
1543 if (regs.r_tstate & 0x1100000000UL) {
1544 tcp->u_rval = -1;
1545 u_error = regs.r_o0;
1546 }
1547 else {
1548 tcp->u_rval = regs.r_o0;
1549 u_error = 0;
1550 }
1551#else /* !SPARC64 */
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001552#ifdef HPPA
1553 if (r28 && (unsigned) -r28 < nerrnos) {
1554 tcp->u_rval = -1;
1555 u_error = -r28;
1556 }
1557 else {
1558 tcp->u_rval = r28;
1559 u_error = 0;
1560 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001561#else
1562#ifdef SH
1563 /* interpret R0 as return value or error number */
1564 if (r0 && (unsigned) -r0 < nerrnos) {
1565 tcp->u_rval = -1;
1566 u_error = -r0;
1567 }
1568 else {
1569 tcp->u_rval = r0;
1570 u_error = 0;
1571 }
Roland McGrathe1e584b2003-06-02 19:18:58 +00001572#else
Roland McGrathf5a47772003-06-26 22:40:42 +00001573#ifdef SH64
Roland McGrathe1e584b2003-06-02 19:18:58 +00001574 /* interpret result as return value or error number */
1575 if (r9 && (unsigned) -r9 < nerrnos) {
1576 tcp->u_rval = -1;
1577 u_error = -r9;
1578 }
1579 else {
1580 tcp->u_rval = r9;
1581 u_error = 0;
1582 }
Roland McGrathf5a47772003-06-26 22:40:42 +00001583#endif /* SH64 */
Wichert Akkermanccef6372002-05-01 16:39:22 +00001584#endif /* SH */
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001585#endif /* HPPA */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001586#endif /* SPARC */
Roland McGrath6d1a65c2004-07-12 07:44:08 +00001587#endif /* SPARC64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001588#endif /* ALPHA */
1589#endif /* ARM */
1590#endif /* M68K */
1591#endif /* POWERPC */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001592#endif /* MIPS */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001593#endif /* IA64 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001594#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001595#endif /* I386 */
Michal Ludvig10a88d02002-10-07 14:31:00 +00001596#endif /* S390 || S390X */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001597#endif /* LINUX */
1598#ifdef SUNOS4
1599 /* get error code from user struct */
1600 if (upeek(pid, uoff(u_error), &u_error) < 0)
1601 return -1;
1602 u_error >>= 24; /* u_error is a char */
1603
1604 /* get system call return value */
1605 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1606 return -1;
1607#endif /* SUNOS4 */
1608#ifdef SVR4
1609#ifdef SPARC
1610 /* Judicious guessing goes a long way. */
1611 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1612 tcp->u_rval = -1;
1613 u_error = tcp->status.pr_reg[R_O0];
1614 }
1615 else {
1616 tcp->u_rval = tcp->status.pr_reg[R_O0];
1617 u_error = 0;
1618 }
1619#endif /* SPARC */
1620#ifdef I386
1621 /* Wanna know how to kill an hour single-stepping? */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001622 if (tcp->status.PR_REG[EFL] & 0x1) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001623 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001624 u_error = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001625 }
1626 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001627 tcp->u_rval = tcp->status.PR_REG[EAX];
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001628#ifdef HAVE_LONG_LONG
1629 tcp->u_lrval =
1630 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1631 tcp->status.PR_REG[EAX];
1632#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001633 u_error = 0;
1634 }
1635#endif /* I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001636#ifdef X86_64
1637 /* Wanna know how to kill an hour single-stepping? */
1638 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1639 tcp->u_rval = -1;
1640 u_error = tcp->status.PR_REG[RAX];
1641 }
1642 else {
1643 tcp->u_rval = tcp->status.PR_REG[RAX];
1644 u_error = 0;
1645 }
1646#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001647#ifdef MIPS
1648 if (tcp->status.pr_reg[CTX_A3]) {
1649 tcp->u_rval = -1;
1650 u_error = tcp->status.pr_reg[CTX_V0];
1651 }
1652 else {
1653 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1654 u_error = 0;
1655 }
1656#endif /* MIPS */
1657#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001658#ifdef FREEBSD
1659 if (regs.r_eflags & PSL_C) {
1660 tcp->u_rval = -1;
1661 u_error = regs.r_eax;
1662 } else {
1663 tcp->u_rval = regs.r_eax;
1664 tcp->u_lrval =
1665 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1666 u_error = 0;
1667 }
Roland McGrath761b5d72002-12-15 23:58:31 +00001668#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001669 tcp->u_error = u_error;
1670 return 1;
1671}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001672
Roland McGrathb69f81b2002-12-21 23:25:18 +00001673int
1674force_result(tcp, error, rval)
1675 struct tcb *tcp;
1676 int error;
1677 long rval;
1678{
1679#ifdef LINUX
1680#if defined(S390) || defined(S390X)
1681 gpr2 = error ? -error : rval;
Roland McGrathb69f81b2002-12-21 23:25:18 +00001682 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
1683 return -1;
1684#else /* !S390 && !S390X */
1685#ifdef I386
1686 eax = error ? -error : rval;
1687 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
1688 return -1;
1689#else /* !I386 */
1690#ifdef X86_64
1691 rax = error ? -error : rval;
Roland McGrath998fac72004-06-23 01:40:45 +00001692 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 8), rax) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001693 return -1;
1694#else
1695#ifdef IA64
1696 if (ia32) {
1697 r8 = error ? -error : rval;
1698 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
1699 return -1;
1700 }
1701 else {
1702 if (error) {
1703 r8 = error;
1704 r10 = -1;
1705 }
1706 else {
1707 r8 = rval;
1708 r10 = 0;
1709 }
1710 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
1711 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
1712 return -1;
1713 }
1714#else /* !IA64 */
1715#ifdef MIPS
1716 if (error) {
1717 r2 = error;
1718 a3 = -1;
1719 }
1720 else {
1721 r2 = rval;
1722 a3 = 0;
1723 }
1724 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1725 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
1726 return -1;
1727#else
1728#ifdef POWERPC
Roland McGratheb285352003-01-14 09:59:00 +00001729 if (upeek(tcp->pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001730 return -1;
1731 if (error) {
1732 flags |= SO_MASK;
1733 result = error;
1734 }
1735 else {
1736 flags &= ~SO_MASK;
1737 result = rval;
1738 }
Roland McGratheb285352003-01-14 09:59:00 +00001739 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 ||
1740 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_R3), result) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001741 return -1;
1742#else /* !POWERPC */
1743#ifdef M68K
1744 d0 = error ? -error : rval;
1745 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
1746 return -1;
1747#else /* !M68K */
1748#ifdef ARM
Roland McGrath7c051d22003-06-26 22:29:32 +00001749 regs.ARM_r0 = error ? -error : rval;
1750 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), regs.ARM_r0) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001751 return -1;
1752#else /* !ARM */
1753#ifdef ALPHA
1754 if (error) {
1755 a3 = -1;
1756 r0 = error;
1757 }
1758 else {
1759 a3 = 0;
1760 r0 = rval;
1761 }
1762 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1763 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
1764 return -1;
1765#else /* !ALPHA */
1766#ifdef SPARC
1767 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)&regs, 0) < 0)
1768 return -1;
1769 if (error) {
1770 regs.r_psr |= PSR_C;
1771 regs.r_o0 = error;
1772 }
1773 else {
1774 regs.r_psr &= ~PSR_C;
1775 regs.r_o0 = rval;
1776 }
1777 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)&regs, 0) < 0)
1778 return -1;
1779#else /* !SPARC */
Roland McGrath6d1a65c2004-07-12 07:44:08 +00001780#ifdef SPARC64
1781 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)&regs, 0) < 0)
1782 return -1;
1783 if (error) {
1784 regs.r_tstate |= 0x1100000000UL;
1785 regs.r_o0 = error;
1786 }
1787 else {
1788 regs.r_tstate &= ~0x1100000000UL;
1789 regs.r_o0 = rval;
1790 }
1791 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)&regs, 0) < 0)
1792 return -1;
1793#else /* !SPARC64 */
Roland McGrathb69f81b2002-12-21 23:25:18 +00001794#ifdef HPPA
1795 r28 = error ? -error : rval;
1796 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
1797 return -1;
1798#else
1799#ifdef SH
1800 r0 = error ? -error : rval;
1801 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
1802 return -1;
Roland McGrathe1e584b2003-06-02 19:18:58 +00001803#else
Roland McGrathf5a47772003-06-26 22:40:42 +00001804#ifdef SH64
Roland McGrathe1e584b2003-06-02 19:18:58 +00001805 r9 = error ? -error : rval;
1806 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)REG_GENERAL(9), r9) < 0)
1807 return -1;
Roland McGrathf5a47772003-06-26 22:40:42 +00001808#endif /* SH64 */
Roland McGrathb69f81b2002-12-21 23:25:18 +00001809#endif /* SH */
1810#endif /* HPPA */
1811#endif /* SPARC */
Roland McGrath6d1a65c2004-07-12 07:44:08 +00001812#endif /* SPARC64 */
Roland McGrathb69f81b2002-12-21 23:25:18 +00001813#endif /* ALPHA */
1814#endif /* ARM */
1815#endif /* M68K */
1816#endif /* POWERPC */
1817#endif /* MIPS */
1818#endif /* IA64 */
1819#endif /* X86_64 */
1820#endif /* I386 */
1821#endif /* S390 || S390X */
1822#endif /* LINUX */
1823#ifdef SUNOS4
1824 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
1825 error << 24) < 0 ||
1826 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
1827 return -1;
1828#endif /* SUNOS4 */
1829#ifdef SVR4
1830 /* XXX no clue */
1831 return -1;
1832#endif /* SVR4 */
1833#ifdef FREEBSD
1834 if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1835 perror("pread");
1836 return -1;
1837 }
1838 if (error) {
1839 regs.r_eflags |= PSL_C;
1840 regs.r_eax = error;
1841 }
1842 else {
1843 regs.r_eflags &= ~PSL_C;
1844 regs.r_eax = rval;
1845 }
1846 if (pwrite(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1847 perror("pwrite");
1848 return -1;
1849 }
1850#endif /* FREEBSD */
1851
1852 /* All branches reach here on success (only). */
1853 tcp->u_error = error;
1854 tcp->u_rval = rval;
1855 return 0;
1856}
1857
Pavel Machek4dc3b142000-02-01 17:58:41 +00001858int syscall_enter(tcp)
1859struct tcb *tcp;
1860{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001861#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001862 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +00001863#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001864#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +00001865#if defined(S390) || defined(S390X)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001866 {
1867 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001868 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1869 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001870 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001871 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001872 for (i = 0; i < tcp->u_nargs; i++) {
Michal Ludvig10a88d02002-10-07 14:31:00 +00001873 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001874 return -1;
1875 }
1876 }
1877#elif defined (ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001878 {
1879 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001880 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1881 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001882 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001883 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001884 for (i = 0; i < tcp->u_nargs; i++) {
Wichert Akkermanb859bea1999-04-18 22:50:50 +00001885 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1886 * for scno somewhere above here!
1887 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001888 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1889 return -1;
1890 }
1891 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001892#elif defined (IA64)
1893 {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001894 if (!ia32) {
1895 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1896 /* be backwards compatible with kernel < 2.4.4... */
1897# ifndef PT_RBS_END
1898# define PT_RBS_END PT_AR_BSP
1899# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001900
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001901 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001902 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001903 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1904 return -1;
1905
1906 sof = (cfm >> 0) & 0x7f;
1907 sol = (cfm >> 7) & 0x7f;
1908 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1909
1910 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1911 && sysent[tcp->scno].nargs != -1)
1912 tcp->u_nargs = sysent[tcp->scno].nargs;
1913 else
1914 tcp->u_nargs = MAX_ARGS;
1915 for (i = 0; i < tcp->u_nargs; ++i) {
1916 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1917 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1918 return -1;
1919 }
1920 } else {
1921 int i;
1922
1923 if (/* EBX = out0 */
1924 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1925 /* ECX = out1 */
1926 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1927 /* EDX = out2 */
1928 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1929 /* ESI = out3 */
1930 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1931 /* EDI = out4 */
1932 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1933 /* EBP = out5 */
1934 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1935 return -1;
1936
1937 for (i = 0; i < 6; ++i)
1938 /* truncate away IVE sign-extension */
1939 tcp->u_arg[i] &= 0xffffffff;
1940
1941 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1942 && sysent[tcp->scno].nargs != -1)
1943 tcp->u_nargs = sysent[tcp->scno].nargs;
1944 else
1945 tcp->u_nargs = 5;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001946 }
1947 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001948#elif defined (MIPS)
1949 {
1950 long sp;
1951 int i, nargs;
1952
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001953 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1954 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001955 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001956 nargs = tcp->u_nargs = MAX_ARGS;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001957 if(nargs > 4) {
1958 if(upeek(pid, REG_SP, &sp) < 0)
1959 return -1;
1960 for(i = 0; i < 4; i++) {
1961 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1962 return -1;
1963 }
1964 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1965 (char *)(tcp->u_arg + 4));
1966 } else {
1967 for(i = 0; i < nargs; i++) {
1968 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1969 return -1;
1970 }
1971 }
1972 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001973#elif defined (POWERPC)
Roland McGrath761b5d72002-12-15 23:58:31 +00001974#ifndef PT_ORIG_R3
1975#define PT_ORIG_R3 34
1976#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001977 {
1978 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001979 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1980 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001981 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001982 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001983 for (i = 0; i < tcp->u_nargs; i++) {
Roland McGratheb285352003-01-14 09:59:00 +00001984 if (upeek(pid, (i==0) ?
1985 (sizeof(unsigned long)*PT_ORIG_R3) :
1986 ((i+PT_R3)*sizeof(unsigned long)),
1987 &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001988 return -1;
1989 }
1990 }
Roland McGrath6d1a65c2004-07-12 07:44:08 +00001991#elif defined (SPARC) || defined (SPARC64)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001992 {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001993 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001994
1995 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1996 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001997 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001998 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001999 for (i = 0; i < tcp->u_nargs; i++)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002000 tcp->u_arg[i] = *((&regs.r_o0) + i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002001 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00002002#elif defined (HPPA)
2003 {
2004 int i;
2005
2006 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2007 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00002008 else
Wichert Akkermanc1652e22001-03-27 12:17:16 +00002009 tcp->u_nargs = MAX_ARGS;
2010 for (i = 0; i < tcp->u_nargs; i++) {
2011 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
2012 return -1;
2013 }
2014 }
Roland McGrath0f87c492003-06-03 23:29:04 +00002015#elif defined(ARM)
2016 {
2017 int i;
2018
2019 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2020 tcp->u_nargs = sysent[tcp->scno].nargs;
2021 else
2022 tcp->u_nargs = MAX_ARGS;
2023 for (i = 0; i < tcp->u_nargs; i++)
2024 tcp->u_arg[i] = regs.uregs[i];
2025 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00002026#elif defined(SH)
2027 {
Roland McGrath761b5d72002-12-15 23:58:31 +00002028 int i;
Wichert Akkermanccef6372002-05-01 16:39:22 +00002029 static int syscall_regs[] = {
2030 REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
2031 REG_REG0, REG_REG0+1, REG_REG0+2
2032 };
2033
2034 tcp->u_nargs = sysent[tcp->scno].nargs;
2035 for (i = 0; i < tcp->u_nargs; i++) {
2036 if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
2037 return -1;
2038 }
2039 }
Roland McGrathf5a47772003-06-26 22:40:42 +00002040#elif defined(SH64)
Roland McGrathe1e584b2003-06-02 19:18:58 +00002041 {
2042 int i;
2043 /* Registers used by SH5 Linux system calls for parameters */
2044 static int syscall_regs[] = { 2, 3, 4, 5, 6, 7 };
2045
2046 /*
2047 * TODO: should also check that the number of arguments encoded
2048 * in the trap number matches the number strace expects.
2049 */
2050 /*
2051 assert(sysent[tcp->scno].nargs <
2052 sizeof(syscall_regs)/sizeof(syscall_regs[0]));
2053 */
2054
2055 tcp->u_nargs = sysent[tcp->scno].nargs;
2056 for (i = 0; i < tcp->u_nargs; i++) {
2057 if (upeek(pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
2058 return -1;
2059 }
2060 }
2061
Michal Ludvig0e035502002-09-23 15:41:01 +00002062#elif defined(X86_64)
2063 {
2064 int i;
2065 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
2066 {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */
Roland McGrath5a9c6ad2005-02-02 03:06:52 +00002067 {RBX,RCX,RDX,RSI,RDI,RBP} /* i386 ABI */
Michal Ludvig0e035502002-09-23 15:41:01 +00002068 };
Roland McGrath761b5d72002-12-15 23:58:31 +00002069
Michal Ludvig0e035502002-09-23 15:41:01 +00002070 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2071 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00002072 else
Michal Ludvig0e035502002-09-23 15:41:01 +00002073 tcp->u_nargs = MAX_ARGS;
2074 for (i = 0; i < tcp->u_nargs; i++) {
2075 if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
2076 return -1;
2077 }
2078 }
Wichert Akkermanfaf72222000-02-19 23:59:03 +00002079#else /* Other architecture (like i386) (32bits specific) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002080 {
2081 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002082 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2083 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00002084 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002085 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002086 for (i = 0; i < tcp->u_nargs; i++) {
2087 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
2088 return -1;
2089 }
2090 }
Roland McGrath761b5d72002-12-15 23:58:31 +00002091#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002092#endif /* LINUX */
2093#ifdef SUNOS4
2094 {
2095 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002096 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2097 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00002098 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002099 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002100 for (i = 0; i < tcp->u_nargs; i++) {
2101 struct user *u;
2102
2103 if (upeek(pid, uoff(u_arg[0]) +
2104 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
2105 return -1;
2106 }
2107 }
2108#endif /* SUNOS4 */
2109#ifdef SVR4
2110#ifdef MIPS
2111 /*
2112 * SGI is broken: even though it has pr_sysarg, it doesn't
2113 * set them on system call entry. Get a clue.
2114 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002115 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002116 tcp->u_nargs = sysent[tcp->scno].nargs;
2117 else
2118 tcp->u_nargs = tcp->status.pr_nsysarg;
2119 if (tcp->u_nargs > 4) {
2120 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2121 4*sizeof(tcp->u_arg[0]));
2122 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
2123 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
2124 }
2125 else {
2126 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2127 tcp->u_nargs*sizeof(tcp->u_arg[0]));
2128 }
John Hughes25299712001-03-06 10:10:06 +00002129#elif UNIXWARE >= 2
2130 /*
2131 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
2132 */
2133 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2134 tcp->u_nargs = sysent[tcp->scno].nargs;
2135 else
2136 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
2137 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2138 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2139#elif defined (HAVE_PR_SYSCALL)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002140 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002141 tcp->u_nargs = sysent[tcp->scno].nargs;
2142 else
2143 tcp->u_nargs = tcp->status.pr_nsysarg;
2144 {
2145 int i;
2146 for (i = 0; i < tcp->u_nargs; i++)
2147 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
2148 }
John Hughes25299712001-03-06 10:10:06 +00002149#elif defined (I386)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002150 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002151 tcp->u_nargs = sysent[tcp->scno].nargs;
2152 else
2153 tcp->u_nargs = 5;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002154 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002155 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
John Hughes25299712001-03-06 10:10:06 +00002156#else
2157 I DONT KNOW WHAT TO DO
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002158#endif /* !HAVE_PR_SYSCALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002159#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002160#ifdef FREEBSD
2161 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2162 sysent[tcp->scno].nargs > tcp->status.val)
2163 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00002164 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002165 tcp->u_nargs = tcp->status.val;
2166 if (tcp->u_nargs < 0)
2167 tcp->u_nargs = 0;
2168 if (tcp->u_nargs > MAX_ARGS)
2169 tcp->u_nargs = MAX_ARGS;
2170 switch(regs.r_eax) {
2171 case SYS___syscall:
2172 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2173 regs.r_esp + sizeof(int) + sizeof(quad_t));
2174 break;
2175 case SYS_syscall:
2176 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2177 regs.r_esp + 2 * sizeof(int));
2178 break;
2179 default:
2180 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2181 regs.r_esp + sizeof(int));
2182 break;
2183 }
2184#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00002185 return 1;
2186}
2187
2188int
2189trace_syscall(tcp)
2190struct tcb *tcp;
2191{
2192 int sys_res;
2193 struct timeval tv;
2194 int res;
2195
2196 /* Measure the exit time as early as possible to avoid errors. */
2197 if (dtime && (tcp->flags & TCB_INSYSCALL))
2198 gettimeofday(&tv, NULL);
2199
2200 res = get_scno(tcp);
2201 if (res != 1)
2202 return res;
2203
2204 res = syscall_fixup(tcp);
2205 if (res != 1)
2206 return res;
2207
2208 if (tcp->flags & TCB_INSYSCALL) {
2209 long u_error;
2210 res = get_error(tcp);
2211 if (res != 1)
2212 return res;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002213
2214 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002215 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2216 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00002217 tcp->flags &= ~TCB_INSYSCALL;
2218 return 0;
2219 }
2220
2221 if (tcp->flags & TCB_REPRINT) {
2222 printleader(tcp);
2223 tprintf("<... ");
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002224 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002225 tprintf("syscall_%lu", tcp->scno);
2226 else
2227 tprintf("%s", sysent[tcp->scno].sys_name);
2228 tprintf(" resumed> ");
2229 }
2230
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002231 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
Roland McGrathe10e62a2004-09-04 04:20:43 +00002232 if (counts == NULL) {
2233 counts = calloc(sizeof *counts, nsyscalls);
2234 if (counts == NULL) {
2235 fprintf(stderr, "\
2236strace: out of memory for call counts\n");
2237 exit(1);
2238 }
2239 }
2240
2241 counts[tcp->scno].calls++;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002242 if (tcp->u_error)
Roland McGrathe10e62a2004-09-04 04:20:43 +00002243 counts[tcp->scno].errors++;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002244 tv_sub(&tv, &tv, &tcp->etime);
2245#ifdef LINUX
2246 if (tv_cmp(&tv, &tcp->dtime) > 0) {
Roland McGrathee9c5b52003-11-01 22:11:22 +00002247 static struct timeval one_tick;
2248 if (one_tick.tv_usec == 0) {
2249 /* Initialize it. */
2250 struct itimerval it;
2251 memset(&it, 0, sizeof it);
2252 it.it_interval.tv_usec = 1;
2253 setitimer(ITIMER_REAL, &it, NULL);
2254 getitimer(ITIMER_REAL, &it);
2255 one_tick = it.it_interval;
2256 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00002257
2258 if (tv_nz(&tcp->dtime))
2259 tv = tcp->dtime;
2260 else if (tv_cmp(&tv, &one_tick) > 0) {
2261 if (tv_cmp(&shortest, &one_tick) < 0)
2262 tv = shortest;
2263 else
2264 tv = one_tick;
2265 }
2266 }
2267#endif /* LINUX */
2268 if (tv_cmp(&tv, &shortest) < 0)
2269 shortest = tv;
Roland McGrathe10e62a2004-09-04 04:20:43 +00002270 tv_add(&counts[tcp->scno].time,
2271 &counts[tcp->scno].time, &tv);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002272 tcp->flags &= ~TCB_INSYSCALL;
2273 return 0;
2274 }
2275
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002276 if (tcp->scno >= nsyscalls || tcp->scno < 0
Pavel Machek4dc3b142000-02-01 17:58:41 +00002277 || (qual_flags[tcp->scno] & QUAL_RAW))
2278 sys_res = printargs(tcp);
Michal Ludvig17f8fb32002-11-06 13:17:21 +00002279 else {
2280 if (not_failing_only && tcp->u_error)
Roland McGrath761b5d72002-12-15 23:58:31 +00002281 return 0; /* ignore failed syscalls */
Pavel Machek4dc3b142000-02-01 17:58:41 +00002282 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
Roland McGrath761b5d72002-12-15 23:58:31 +00002283 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00002284 u_error = tcp->u_error;
2285 tprintf(") ");
2286 tabto(acolumn);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002287 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2288 qual_flags[tcp->scno] & QUAL_RAW) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00002289 if (u_error)
2290 tprintf("= -1 (errno %ld)", u_error);
2291 else
2292 tprintf("= %#lx", tcp->u_rval);
2293 }
2294 else if (!(sys_res & RVAL_NONE) && u_error) {
2295 switch (u_error) {
2296#ifdef LINUX
2297 case ERESTARTSYS:
2298 tprintf("= ? ERESTARTSYS (To be restarted)");
2299 break;
2300 case ERESTARTNOINTR:
2301 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2302 break;
2303 case ERESTARTNOHAND:
2304 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2305 break;
Roland McGrath9c555e72003-07-09 09:47:59 +00002306 case ERESTART_RESTARTBLOCK:
2307 tprintf("= ? ERESTART_RESTARTBLOCK (To be restarted)");
2308 break;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002309#endif /* LINUX */
2310 default:
2311 tprintf("= -1 ");
Wichert Akkerman4527dae2002-03-31 19:03:29 +00002312 if (u_error < 0)
2313 tprintf("E??? (errno %ld)", u_error);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002314 else if (u_error < nerrnos)
Roland McGrath761b5d72002-12-15 23:58:31 +00002315 tprintf("%s (%s)", errnoent[u_error],
2316 strerror(u_error));
Pavel Machek4dc3b142000-02-01 17:58:41 +00002317 else
Roland McGrath761b5d72002-12-15 23:58:31 +00002318 tprintf("ERRNO_%ld (%s)", u_error,
2319 strerror(u_error));
Pavel Machek4dc3b142000-02-01 17:58:41 +00002320 break;
2321 }
2322 }
2323 else {
2324 if (sys_res & RVAL_NONE)
2325 tprintf("= ?");
2326 else {
2327 switch (sys_res & RVAL_MASK) {
2328 case RVAL_HEX:
2329 tprintf("= %#lx", tcp->u_rval);
2330 break;
2331 case RVAL_OCTAL:
2332 tprintf("= %#lo", tcp->u_rval);
2333 break;
2334 case RVAL_UDECIMAL:
2335 tprintf("= %lu", tcp->u_rval);
2336 break;
2337 case RVAL_DECIMAL:
2338 tprintf("= %ld", tcp->u_rval);
2339 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002340#ifdef HAVE_LONG_LONG
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002341 case RVAL_LHEX:
2342 tprintf("= %#llx", tcp->u_lrval);
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002343 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002344 case RVAL_LOCTAL:
2345 tprintf("= %#llo", tcp->u_lrval);
2346 break;
2347 case RVAL_LUDECIMAL:
2348 tprintf("= %llu", tcp->u_lrval);
2349 break;
2350 case RVAL_LDECIMAL:
2351 tprintf("= %lld", tcp->u_lrval);
2352 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002353#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00002354 default:
2355 fprintf(stderr,
2356 "invalid rval format\n");
2357 break;
2358 }
2359 }
2360 if ((sys_res & RVAL_STR) && tcp->auxstr)
2361 tprintf(" (%s)", tcp->auxstr);
2362 }
2363 if (dtime) {
2364 tv_sub(&tv, &tv, &tcp->etime);
2365 tprintf(" <%ld.%06ld>",
2366 (long) tv.tv_sec, (long) tv.tv_usec);
2367 }
2368 printtrailer(tcp);
2369
2370 dumpio(tcp);
2371 if (fflush(tcp->outf) == EOF)
2372 return -1;
2373 tcp->flags &= ~TCB_INSYSCALL;
2374 return 0;
2375 }
2376
2377 /* Entering system call */
2378 res = syscall_enter(tcp);
2379 if (res != 1)
2380 return res;
2381
Roland McGrath17352792005-06-07 23:21:26 +00002382 switch (known_scno(tcp)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002383#ifdef LINUX
Roland McGrath17352792005-06-07 23:21:26 +00002384#if !defined (ALPHA) && !defined(SPARC) && !defined(SPARC64) && !defined(MIPS) && !defined(HPPA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002385 case SYS_socketcall:
2386 decode_subcall(tcp, SYS_socket_subcall,
2387 SYS_socket_nsubcalls, deref_style);
2388 break;
2389 case SYS_ipc:
2390 decode_subcall(tcp, SYS_ipc_subcall,
2391 SYS_ipc_nsubcalls, shift_style);
2392 break;
Roland McGrath17352792005-06-07 23:21:26 +00002393#endif /* !ALPHA && !MIPS && !SPARC && !SPARC64 && !HPPA */
Roland McGrath6d1a65c2004-07-12 07:44:08 +00002394#if defined (SPARC) || defined (SPARC64)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002395 case SYS_socketcall:
2396 sparc_socket_decode (tcp);
2397 break;
2398#endif
2399#endif /* LINUX */
2400#ifdef SVR4
2401#ifdef SYS_pgrpsys_subcall
2402 case SYS_pgrpsys:
2403 decode_subcall(tcp, SYS_pgrpsys_subcall,
2404 SYS_pgrpsys_nsubcalls, shift_style);
2405 break;
2406#endif /* SYS_pgrpsys_subcall */
2407#ifdef SYS_sigcall_subcall
2408 case SYS_sigcall:
2409 decode_subcall(tcp, SYS_sigcall_subcall,
2410 SYS_sigcall_nsubcalls, mask_style);
2411 break;
2412#endif /* SYS_sigcall_subcall */
2413 case SYS_msgsys:
2414 decode_subcall(tcp, SYS_msgsys_subcall,
2415 SYS_msgsys_nsubcalls, shift_style);
2416 break;
2417 case SYS_shmsys:
2418 decode_subcall(tcp, SYS_shmsys_subcall,
2419 SYS_shmsys_nsubcalls, shift_style);
2420 break;
2421 case SYS_semsys:
2422 decode_subcall(tcp, SYS_semsys_subcall,
2423 SYS_semsys_nsubcalls, shift_style);
2424 break;
2425#if 0 /* broken */
2426 case SYS_utssys:
2427 decode_subcall(tcp, SYS_utssys_subcall,
2428 SYS_utssys_nsubcalls, shift_style);
2429 break;
2430#endif
2431 case SYS_sysfs:
2432 decode_subcall(tcp, SYS_sysfs_subcall,
2433 SYS_sysfs_nsubcalls, shift_style);
2434 break;
2435 case SYS_spcall:
2436 decode_subcall(tcp, SYS_spcall_subcall,
2437 SYS_spcall_nsubcalls, shift_style);
2438 break;
2439#ifdef SYS_context_subcall
2440 case SYS_context:
2441 decode_subcall(tcp, SYS_context_subcall,
2442 SYS_context_nsubcalls, shift_style);
2443 break;
2444#endif /* SYS_context_subcall */
Wichert Akkerman8829a551999-06-11 13:18:40 +00002445#ifdef SYS_door_subcall
2446 case SYS_door:
2447 decode_subcall(tcp, SYS_door_subcall,
2448 SYS_door_nsubcalls, door_style);
2449 break;
2450#endif /* SYS_door_subcall */
John Hughesbdf48f52001-03-06 15:08:09 +00002451#ifdef SYS_kaio_subcall
2452 case SYS_kaio:
2453 decode_subcall(tcp, SYS_kaio_subcall,
2454 SYS_kaio_nsubcalls, shift_style);
2455 break;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002456#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002457#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002458#ifdef FREEBSD
2459 case SYS_msgsys:
2460 case SYS_shmsys:
2461 case SYS_semsys:
2462 decode_subcall(tcp, 0, 0, table_style);
2463 break;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002464#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002465#ifdef SUNOS4
2466 case SYS_semsys:
2467 decode_subcall(tcp, SYS_semsys_subcall,
2468 SYS_semsys_nsubcalls, shift_style);
2469 break;
2470 case SYS_msgsys:
2471 decode_subcall(tcp, SYS_msgsys_subcall,
2472 SYS_msgsys_nsubcalls, shift_style);
2473 break;
2474 case SYS_shmsys:
2475 decode_subcall(tcp, SYS_shmsys_subcall,
2476 SYS_shmsys_nsubcalls, shift_style);
2477 break;
2478#endif
2479 }
2480
2481 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002482 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002483 tcp->flags |= TCB_INSYSCALL;
2484 return 0;
2485 }
2486
2487 if (cflag) {
2488 gettimeofday(&tcp->etime, NULL);
2489 tcp->flags |= TCB_INSYSCALL;
2490 return 0;
2491 }
2492
2493 printleader(tcp);
2494 tcp->flags &= ~TCB_REPRINT;
2495 tcp_last = tcp;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002496 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002497 tprintf("syscall_%lu(", tcp->scno);
2498 else
2499 tprintf("%s(", sysent[tcp->scno].sys_name);
Roland McGrath761b5d72002-12-15 23:58:31 +00002500 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002501 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
2502 sys_res = printargs(tcp);
2503 else
2504 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2505 if (fflush(tcp->outf) == EOF)
2506 return -1;
2507 tcp->flags |= TCB_INSYSCALL;
2508 /* Measure the entrance time as late as possible to avoid errors. */
2509 if (dtime)
2510 gettimeofday(&tcp->etime, NULL);
2511 return sys_res;
2512}
2513
2514int
2515printargs(tcp)
2516struct tcb *tcp;
2517{
2518 if (entering(tcp)) {
2519 int i;
2520
2521 for (i = 0; i < tcp->u_nargs; i++)
2522 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2523 }
2524 return 0;
2525}
2526
2527long
2528getrval2(tcp)
2529struct tcb *tcp;
2530{
2531 long val = -1;
2532
2533#ifdef LINUX
Roland McGrath6d1a65c2004-07-12 07:44:08 +00002534#if defined (SPARC) || defined (SPARC64)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002535 struct regs regs;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002536 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
2537 return -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002538 val = regs.r_o1;
Roland McGrath6b1d43e2003-03-31 01:05:01 +00002539#elif defined(SH)
2540 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
2541 return -1;
Roland McGrathb4ce1762004-03-01 20:30:48 +00002542#elif defined(IA64)
2543 if (upeek(tcp->pid, PT_R9, &val) < 0)
2544 return -1;
Roland McGrath920e6bb2005-03-15 02:15:20 +00002545#endif /* SPARC || SPARC64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002546#endif /* LINUX */
2547
2548#ifdef SUNOS4
2549 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2550 return -1;
2551#endif /* SUNOS4 */
2552
2553#ifdef SVR4
2554#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002555 val = tcp->status.PR_REG[R_O1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002556#endif /* SPARC */
2557#ifdef I386
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002558 val = tcp->status.PR_REG[EDX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002559#endif /* I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00002560#ifdef X86_64
2561 val = tcp->status.PR_REG[RDX];
2562#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002563#ifdef MIPS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002564 val = tcp->status.PR_REG[CTX_V1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002565#endif /* MIPS */
2566#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002567#ifdef FREEBSD
2568 struct reg regs;
2569 pread(tcp->pfd_reg, &regs, sizeof(regs), 0);
2570 val = regs.r_edx;
Roland McGrath761b5d72002-12-15 23:58:31 +00002571#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002572 return val;
2573}
2574
2575/*
2576 * Apparently, indirect system calls have already be converted by ptrace(2),
2577 * so if you see "indir" this program has gone astray.
2578 */
2579int
2580sys_indir(tcp)
2581struct tcb *tcp;
2582{
2583 int i, scno, nargs;
2584
2585 if (entering(tcp)) {
2586 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2587 fprintf(stderr, "Bogus syscall: %u\n", scno);
2588 return 0;
2589 }
2590 nargs = sysent[scno].nargs;
2591 tprintf("%s", sysent[scno].sys_name);
2592 for (i = 0; i < nargs; i++)
2593 tprintf(", %#lx", tcp->u_arg[i+1]);
2594 }
2595 return 0;
2596}
2597
2598static int
2599time_cmp(a, b)
2600void *a;
2601void *b;
2602{
Roland McGrathe10e62a2004-09-04 04:20:43 +00002603 return -tv_cmp(&counts[*((int *) a)].time, &counts[*((int *) b)].time);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002604}
2605
2606static int
2607syscall_cmp(a, b)
2608void *a;
2609void *b;
2610{
2611 return strcmp(sysent[*((int *) a)].sys_name,
2612 sysent[*((int *) b)].sys_name);
2613}
2614
2615static int
2616count_cmp(a, b)
2617void *a;
2618void *b;
2619{
Roland McGrathe10e62a2004-09-04 04:20:43 +00002620 int m = counts[*((int *) a)].calls, n = counts[*((int *) b)].calls;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002621
2622 return (m < n) ? 1 : (m > n) ? -1 : 0;
2623}
2624
2625static int (*sortfun)();
2626static struct timeval overhead = { -1, -1 };
2627
2628void
2629set_sortby(sortby)
2630char *sortby;
2631{
2632 if (strcmp(sortby, "time") == 0)
2633 sortfun = time_cmp;
2634 else if (strcmp(sortby, "calls") == 0)
2635 sortfun = count_cmp;
2636 else if (strcmp(sortby, "name") == 0)
2637 sortfun = syscall_cmp;
2638 else if (strcmp(sortby, "nothing") == 0)
2639 sortfun = NULL;
2640 else {
2641 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2642 exit(1);
2643 }
2644}
2645
2646void set_overhead(n)
2647int n;
2648{
2649 overhead.tv_sec = n / 1000000;
2650 overhead.tv_usec = n % 1000000;
2651}
2652
2653void
2654call_summary(outf)
2655FILE *outf;
2656{
2657 int i, j;
2658 int call_cum, error_cum;
2659 struct timeval tv_cum, dtv;
2660 double percent;
2661 char *dashes = "-------------------------";
2662 char error_str[16];
2663
Roland McGrathe10e62a2004-09-04 04:20:43 +00002664 int *sorted_count = malloc(nsyscalls * sizeof(int));
2665
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002666 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2667 if (overhead.tv_sec == -1) {
2668 tv_mul(&overhead, &shortest, 8);
2669 tv_div(&overhead, &overhead, 10);
2670 }
2671 for (i = 0; i < nsyscalls; i++) {
2672 sorted_count[i] = i;
Roland McGrathe10e62a2004-09-04 04:20:43 +00002673 if (counts == NULL || counts[i].calls == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002674 continue;
Roland McGrathe10e62a2004-09-04 04:20:43 +00002675 tv_mul(&dtv, &overhead, counts[i].calls);
2676 tv_sub(&counts[i].time, &counts[i].time, &dtv);
2677 call_cum += counts[i].calls;
2678 error_cum += counts[i].errors;
2679 tv_add(&tv_cum, &tv_cum, &counts[i].time);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002680 }
Roland McGrathb77d0932005-02-02 04:42:25 +00002681 if (counts && sortfun)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002682 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2683 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2684 "% time", "seconds", "usecs/call",
2685 "calls", "errors", "syscall");
2686 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2687 dashes, dashes, dashes, dashes, dashes, dashes);
Roland McGrathe10e62a2004-09-04 04:20:43 +00002688 if (counts) {
2689 for (i = 0; i < nsyscalls; i++) {
2690 j = sorted_count[i];
2691 if (counts[j].calls == 0)
2692 continue;
2693 tv_div(&dtv, &counts[j].time, counts[j].calls);
2694 if (counts[j].errors)
2695 sprintf(error_str, "%d", counts[j].errors);
2696 else
2697 error_str[0] = '\0';
2698 percent = (100.0 * tv_float(&counts[j].time)
2699 / tv_float(&tv_cum));
2700 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2701 percent, (long) counts[j].time.tv_sec,
2702 (long) counts[j].time.tv_usec,
2703 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2704 counts[j].calls,
2705 error_str, sysent[j].sys_name);
2706 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002707 }
Roland McGrathe10e62a2004-09-04 04:20:43 +00002708 free(sorted_count);
2709
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002710 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2711 dashes, dashes, dashes, dashes, dashes, dashes);
2712 if (error_cum)
2713 sprintf(error_str, "%d", error_cum);
2714 else
2715 error_str[0] = '\0';
2716 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2717 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2718 call_cum, error_str, "total");
Roland McGrathe10e62a2004-09-04 04:20:43 +00002719
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002720}