blob: 4264a80d82a39443c11250b6adf5e12ebb181eb9 [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
Wichert Akkerman00a82ee2001-03-28 20:29:17 +000046#ifdef SPARC
47# 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>
Wichert Akkerman00a82ee2001-03-28 20:29:17 +000052#ifdef SPARC
53# 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
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000077#if defined(LINUX) && defined(IA64)
78# include <asm/ptrace_offsets.h>
79# include <asm/rse.h>
80#endif
81
Pavel Machekd8ae7e32000-02-01 17:17:25 +000082#define NR_SYSCALL_BASE 0
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000083#ifdef LINUX
84#ifndef ERESTARTSYS
85#define ERESTARTSYS 512
86#endif
87#ifndef ERESTARTNOINTR
88#define ERESTARTNOINTR 513
89#endif
90#ifndef ERESTARTNOHAND
91#define ERESTARTNOHAND 514 /* restart if no handler.. */
92#endif
93#ifndef ENOIOCTLCMD
94#define ENOIOCTLCMD 515 /* No ioctl command */
95#endif
Roland McGrath9c555e72003-07-09 09:47:59 +000096#ifndef ERESTART_RESTARTBLOCK
97#define ERESTART_RESTARTBLOCK 516 /* restart by calling sys_restart_syscall */
98#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000099#ifndef NSIG
100#define NSIG 32
101#endif
102#ifdef ARM
103#undef NSIG
104#define NSIG 32
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000105#undef NR_SYSCALL_BASE
106#define NR_SYSCALL_BASE __NR_SYSCALL_BASE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000107#endif
108#endif /* LINUX */
109
110#include "syscall.h"
111
112/* Define these shorthand notations to simplify the syscallent files. */
113#define TF TRACE_FILE
114#define TI TRACE_IPC
115#define TN TRACE_NETWORK
116#define TP TRACE_PROCESS
117#define TS TRACE_SIGNAL
118
119struct sysent sysent0[] = {
120#include "syscallent.h"
121};
122int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
123
124#if SUPPORTED_PERSONALITIES >= 2
125struct sysent sysent1[] = {
126#include "syscallent1.h"
127};
128int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
129#endif /* SUPPORTED_PERSONALITIES >= 2 */
130
131#if SUPPORTED_PERSONALITIES >= 3
132struct sysent sysent2[] = {
133#include "syscallent2.h"
134};
135int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
136#endif /* SUPPORTED_PERSONALITIES >= 3 */
137
138struct sysent *sysent;
139int nsyscalls;
140
141/* Now undef them since short defines cause wicked namespace pollution. */
142#undef TF
143#undef TI
144#undef TN
145#undef TP
146#undef TS
147
148char *errnoent0[] = {
149#include "errnoent.h"
150};
151int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
152
153#if SUPPORTED_PERSONALITIES >= 2
154char *errnoent1[] = {
155#include "errnoent1.h"
156};
157int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
158#endif /* SUPPORTED_PERSONALITIES >= 2 */
159
160#if SUPPORTED_PERSONALITIES >= 3
161char *errnoent2[] = {
162#include "errnoent2.h"
163};
164int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
165#endif /* SUPPORTED_PERSONALITIES >= 3 */
166
167char **errnoent;
168int nerrnos;
169
170int current_personality;
171
172int
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000173set_personality(personality)
174int personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000175{
176 switch (personality) {
177 case 0:
178 errnoent = errnoent0;
179 nerrnos = nerrnos0;
180 sysent = sysent0;
181 nsyscalls = nsyscalls0;
182 ioctlent = ioctlent0;
183 nioctlents = nioctlents0;
184 signalent = signalent0;
185 nsignals = nsignals0;
186 break;
187
188#if SUPPORTED_PERSONALITIES >= 2
189 case 1:
190 errnoent = errnoent1;
191 nerrnos = nerrnos1;
192 sysent = sysent1;
193 nsyscalls = nsyscalls1;
194 ioctlent = ioctlent1;
195 nioctlents = nioctlents1;
196 signalent = signalent1;
197 nsignals = nsignals1;
198 break;
199#endif /* SUPPORTED_PERSONALITIES >= 2 */
200
201#if SUPPORTED_PERSONALITIES >= 3
202 case 2:
203 errnoent = errnoent2;
204 nerrnos = nerrnos2;
205 sysent = sysent2;
206 nsyscalls = nsyscalls2;
207 ioctlent = ioctlent2;
208 nioctlents = nioctlents2;
209 signalent = signalent2;
210 nsignals = nsignals2;
211 break;
212#endif /* SUPPORTED_PERSONALITIES >= 3 */
213
214 default:
215 return -1;
216 }
217
218 current_personality = personality;
219 return 0;
220}
221
222int qual_flags[MAX_QUALS];
223
224static int call_count[MAX_QUALS];
225static int error_count[MAX_QUALS];
226static struct timeval tv_count[MAX_QUALS];
227static int sorted_count[MAX_QUALS];
228
229static struct timeval shortest = { 1000000, 0 };
230
Roland McGrath9797ceb2002-12-30 10:23:00 +0000231static int qual_syscall(), qual_signal(), qual_fault(), qual_desc();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000232
233static struct qual_options {
234 int bitflag;
235 char *option_name;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000236 int (*qualify)();
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000237 char *argument_name;
238} qual_options[] = {
Roland McGrath9797ceb2002-12-30 10:23:00 +0000239 { QUAL_TRACE, "trace", qual_syscall, "system call" },
240 { QUAL_TRACE, "t", qual_syscall, "system call" },
241 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
242 { QUAL_ABBREV, "a", qual_syscall, "system call" },
243 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
244 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
245 { QUAL_RAW, "raw", qual_syscall, "system call" },
246 { QUAL_RAW, "x", qual_syscall, "system call" },
247 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
248 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
249 { QUAL_SIGNAL, "s", qual_signal, "signal" },
250 { QUAL_FAULT, "fault", qual_fault, "fault" },
251 { QUAL_FAULT, "faults", qual_fault, "fault" },
252 { QUAL_FAULT, "m", qual_fault, "fault" },
253 { QUAL_READ, "read", qual_desc, "descriptor" },
254 { QUAL_READ, "reads", qual_desc, "descriptor" },
255 { QUAL_READ, "r", qual_desc, "descriptor" },
256 { QUAL_WRITE, "write", qual_desc, "descriptor" },
257 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
258 { QUAL_WRITE, "w", qual_desc, "descriptor" },
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000259 { 0, NULL, NULL, NULL },
260};
261
Roland McGrath9797ceb2002-12-30 10:23:00 +0000262static void
263qualify_one(n, opt, not)
264 int n;
265 struct qual_options *opt;
266 int not;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000267{
Roland McGrath9797ceb2002-12-30 10:23:00 +0000268 if (not)
269 qual_flags[n] &= ~opt->bitflag;
270 else
271 qual_flags[n] |= opt->bitflag;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000272}
273
274static int
Roland McGrath9797ceb2002-12-30 10:23:00 +0000275qual_syscall(s, opt, not)
276 char *s;
277 struct qual_options *opt;
278 int not;
279{
280 int i;
281 int any = 0;
282
283 for (i = 0; i < nsyscalls; i++) {
284 if (strcmp(s, sysent[i].sys_name) == 0) {
285 qualify_one(i, opt, not);
286 any = 1;
287 }
288 }
289 return !any;
290}
291
292static int
293qual_signal(s, opt, not)
294 char *s;
295 struct qual_options *opt;
296 int not;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000297{
298 int i;
299 char buf[32];
300
Roland McGrath9797ceb2002-12-30 10:23:00 +0000301 if (s && *s && isdigit((unsigned char)*s)) {
302 qualify_one(atoi(s), opt, not);
303 return 1;
304 }
Roland McGrathd62b6712004-03-02 06:52:01 +0000305 if (strlen(s) >= sizeof buf)
306 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000307 strcpy(buf, s);
308 s = buf;
309 for (i = 0; s[i]; i++)
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000310 s[i] = toupper((unsigned char)(s[i]));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000311 if (strncmp(s, "SIG", 3) == 0)
312 s += 3;
Roland McGrath9797ceb2002-12-30 10:23:00 +0000313 for (i = 0; i <= NSIG; i++)
314 if (strcmp(s, signame(i) + 3) == 0) {
315 qualify_one(atoi(s), opt, not);
316 return 1;
317 }
318 return 0;
319}
320
321static int
322qual_fault(s, opt, not)
323 char *s;
324 struct qual_options *opt;
325 int not;
326{
327 return -1;
328}
329
330static int
331qual_desc(s, opt, not)
332 char *s;
333 struct qual_options *opt;
334 int not;
335{
336 if (s && *s && isdigit((unsigned char)*s)) {
337 qualify_one(atoi(s), opt, not);
Roland McGrath2b619022003-04-10 18:58:20 +0000338 return 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000339 }
340 return -1;
341}
342
343static int
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000344lookup_class(s)
Roland McGrath9797ceb2002-12-30 10:23:00 +0000345 char *s;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000346{
347 if (strcmp(s, "file") == 0)
348 return TRACE_FILE;
349 if (strcmp(s, "ipc") == 0)
350 return TRACE_IPC;
351 if (strcmp(s, "network") == 0)
352 return TRACE_NETWORK;
353 if (strcmp(s, "process") == 0)
354 return TRACE_PROCESS;
355 if (strcmp(s, "signal") == 0)
356 return TRACE_SIGNAL;
357 return -1;
358}
359
360void
361qualify(s)
362char *s;
363{
364 struct qual_options *opt;
365 int not;
366 char *p;
367 int i, n;
368
369 opt = &qual_options[0];
370 for (i = 0; (p = qual_options[i].option_name); i++) {
371 n = strlen(p);
372 if (strncmp(s, p, n) == 0 && s[n] == '=') {
373 opt = &qual_options[i];
374 s += n + 1;
375 break;
376 }
377 }
378 not = 0;
379 if (*s == '!') {
380 not = 1;
381 s++;
382 }
383 if (strcmp(s, "none") == 0) {
384 not = 1 - not;
385 s = "all";
386 }
387 if (strcmp(s, "all") == 0) {
388 for (i = 0; i < MAX_QUALS; i++) {
389 if (not)
390 qual_flags[i] &= ~opt->bitflag;
391 else
392 qual_flags[i] |= opt->bitflag;
393 }
394 return;
395 }
396 for (i = 0; i < MAX_QUALS; i++) {
397 if (not)
398 qual_flags[i] |= opt->bitflag;
399 else
400 qual_flags[i] &= ~opt->bitflag;
401 }
402 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
403 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
404 for (i = 0; i < MAX_QUALS; i++) {
405 if (sysent[i].sys_flags & n) {
406 if (not)
407 qual_flags[i] &= ~opt->bitflag;
408 else
409 qual_flags[i] |= opt->bitflag;
410 }
411 }
412 continue;
413 }
Roland McGrath9797ceb2002-12-30 10:23:00 +0000414 if (opt->qualify(p, opt, not)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000415 fprintf(stderr, "strace: invalid %s `%s'\n",
416 opt->argument_name, p);
417 exit(1);
418 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000419 }
420 return;
421}
422
423static void
424dumpio(tcp)
425struct tcb *tcp;
426{
427 if (syserror(tcp))
428 return;
429 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
430 return;
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000431 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000432 case SYS_read:
433#ifdef SYS_recv
434 case SYS_recv:
435#endif
436#ifdef SYS_recvfrom
437 case SYS_recvfrom:
438#endif
439 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
440 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
441 break;
442 case SYS_write:
443#ifdef SYS_send
444 case SYS_send:
445#endif
446#ifdef SYS_sendto
447 case SYS_sendto:
448#endif
449 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
450 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
451 break;
John Hughes1d08dcf2001-07-10 13:48:44 +0000452#ifdef SYS_readv
453 case SYS_readv:
454 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
455 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
456 break;
457#endif
458#ifdef SYS_writev
459 case SYS_writev:
460
461 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
462 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
463 break;
464#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000465 }
466}
467
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000468#ifndef FREEBSD
Wichert Akkerman8829a551999-06-11 13:18:40 +0000469enum subcall_style { shift_style, deref_style, mask_style, door_style };
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000470#else /* FREEBSD */
471enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
472
473struct subcall {
474 int call;
475 int nsubcalls;
476 int subcalls[5];
477};
478
479const struct subcall subcalls_table[] = {
480 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
John Hughes61eeb552001-03-06 15:51:53 +0000481#ifdef SYS_semconfig
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000482 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
John Hughes61eeb552001-03-06 15:51:53 +0000483#else
484 { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
485#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000486 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
487};
488#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000489
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000490#if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000491
492const int socket_map [] = {
493 /* SYS_SOCKET */ 97,
494 /* SYS_BIND */ 104,
495 /* SYS_CONNECT */ 98,
496 /* SYS_LISTEN */ 106,
497 /* SYS_ACCEPT */ 99,
498 /* SYS_GETSOCKNAME */ 150,
499 /* SYS_GETPEERNAME */ 141,
500 /* SYS_SOCKETPAIR */ 135,
501 /* SYS_SEND */ 101,
502 /* SYS_RECV */ 102,
503 /* SYS_SENDTO */ 133,
504 /* SYS_RECVFROM */ 125,
505 /* SYS_SHUTDOWN */ 134,
506 /* SYS_SETSOCKOPT */ 105,
507 /* SYS_GETSOCKOPT */ 118,
508 /* SYS_SENDMSG */ 114,
509 /* SYS_RECVMSG */ 113
510};
511
512void
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000513sparc_socket_decode (tcp)
514struct tcb *tcp;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000515{
516 volatile long addr;
517 volatile int i, n;
518
519 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
520 return;
521 }
522 tcp->scno = socket_map [tcp->u_arg [0]-1];
523 n = tcp->u_nargs = sysent [tcp->scno].nargs;
524 addr = tcp->u_arg [1];
525 for (i = 0; i < n; i++){
526 int arg;
527 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
528 arg = 0;
529 tcp->u_arg [i] = arg;
530 addr += sizeof (arg);
531 }
532}
533
Michal Ludvig0e035502002-09-23 15:41:01 +0000534void
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000535decode_subcall(tcp, subcall, nsubcalls, style)
536struct tcb *tcp;
537int subcall;
538int nsubcalls;
539enum subcall_style style;
540{
Michal Ludvig10a88d02002-10-07 14:31:00 +0000541 long addr, mask, arg;
542 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000543
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000544 switch (style) {
545 case shift_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000546 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
547 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000548 tcp->scno = subcall + tcp->u_arg[0];
549 if (sysent[tcp->scno].nargs != -1)
550 tcp->u_nargs = sysent[tcp->scno].nargs;
551 else
552 tcp->u_nargs--;
553 for (i = 0; i < tcp->u_nargs; i++)
554 tcp->u_arg[i] = tcp->u_arg[i + 1];
555 break;
556 case deref_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000557 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
558 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000559 tcp->scno = subcall + tcp->u_arg[0];
560 addr = tcp->u_arg[1];
561 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
562 if (umove(tcp, addr, &arg) < 0)
563 arg = 0;
564 tcp->u_arg[i] = arg;
565 addr += sizeof(arg);
566 }
567 tcp->u_nargs = sysent[tcp->scno].nargs;
568 break;
569 case mask_style:
570 mask = (tcp->u_arg[0] >> 8) & 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000571 for (i = 0; mask; i++)
572 mask >>= 1;
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000573 if (i >= nsubcalls)
574 return;
575 tcp->u_arg[0] &= 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000576 tcp->scno = subcall + i;
577 if (sysent[tcp->scno].nargs != -1)
578 tcp->u_nargs = sysent[tcp->scno].nargs;
579 break;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000580 case door_style:
581 /*
582 * Oh, yuck. The call code is the *sixth* argument.
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000583 * (don't you mean the *last* argument? - JH)
Wichert Akkerman8829a551999-06-11 13:18:40 +0000584 */
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000585 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
586 return;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000587 tcp->scno = subcall + tcp->u_arg[5];
588 if (sysent[tcp->scno].nargs != -1)
589 tcp->u_nargs = sysent[tcp->scno].nargs;
590 else
591 tcp->u_nargs--;
592 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000593#ifdef FREEBSD
594 case table_style:
595 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
596 if (subcalls_table[i].call == tcp->scno) break;
597 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
598 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
599 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
600 for (i = 0; i < tcp->u_nargs; i++)
601 tcp->u_arg[i] = tcp->u_arg[i + 1];
602 }
603 break;
604#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000605 }
606}
607#endif
608
609struct tcb *tcp_last = NULL;
610
611static int
612internal_syscall(tcp)
613struct tcb *tcp;
614{
615 /*
616 * We must always trace a few critical system calls in order to
617 * correctly support following forks in the presence of tracing
618 * qualifiers.
619 */
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000620 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000621#ifdef SYS_fork
622 case SYS_fork:
623#endif
624#ifdef SYS_vfork
625 case SYS_vfork:
626#endif
John Hughes4e36a812001-04-18 15:11:51 +0000627#ifdef SYS_fork1
628 case SYS_fork1:
629#endif
630#ifdef SYS_forkall
631 case SYS_forkall:
632#endif
633#ifdef SYS_rfork1
634 case SYS_rfork1:
635#endif
636#ifdef SYS_rforkall
637 case SYS_rforkall:
638#endif
Roland McGrathf3a0e1b2003-02-20 02:45:22 +0000639#ifdef SYS_rfork
640 case SYS_rfork:
641#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000642 internal_fork(tcp);
643 break;
Wichert Akkerman7a0b6491999-12-23 15:08:17 +0000644#ifdef SYS_clone
645 case SYS_clone:
646 internal_clone(tcp);
647 break;
648#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000649#ifdef SYS_clone2
650 case SYS_clone2:
651 internal_clone(tcp);
652 break;
653#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000654#ifdef SYS_execv
655 case SYS_execv:
656#endif
657#ifdef SYS_execve
658 case SYS_execve:
659#endif
John Hughes4e36a812001-04-18 15:11:51 +0000660#ifdef SYS_rexecve
661 case SYS_rexecve:
662#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000663 internal_exec(tcp);
664 break;
665
666#ifdef SYS_wait
667 case SYS_wait:
668#endif
669#ifdef SYS_wait4
670 case SYS_wait4:
671#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000672#ifdef SYS32_wait4
673 case SYS32_wait4:
674#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000675#ifdef SYS_waitpid
676 case SYS_waitpid:
677#endif
678#ifdef SYS_waitsys
679 case SYS_waitsys:
680#endif
681 internal_wait(tcp);
682 break;
683
684#ifdef SYS_exit
685 case SYS_exit:
686#endif
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000687#ifdef SYS32_exit
688 case SYS32_exit:
689#endif
Roland McGrath923f7502003-01-09 06:53:27 +0000690#ifdef __NR_exit_group
691 case __NR_exit_group:
692#endif
Roland McGrath08267b82004-02-20 22:56:43 +0000693#ifdef IA64
694 case 252: /* IA-32 __NR_exit_group */
695#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000696 internal_exit(tcp);
697 break;
698 }
699 return 0;
700}
701
Wichert Akkermanc7926982000-04-10 22:22:31 +0000702
703#ifdef LINUX
704#if defined (I386)
705 static long eax;
706#elif defined (IA64)
707 long r8, r10, psr;
708 long ia32 = 0;
709#elif defined (POWERPC)
710 static long result,flags;
711#elif defined (M68K)
712 static int d0;
713#elif defined (ARM)
Roland McGrath0f87c492003-06-03 23:29:04 +0000714 static struct pt_regs regs;
Wichert Akkermanc7926982000-04-10 22:22:31 +0000715#elif defined (ALPHA)
716 static long r0;
717 static long a3;
718#elif defined (SPARC)
Wichert Akkerman00a82ee2001-03-28 20:29:17 +0000719 static struct regs regs;
Wichert Akkermanc7926982000-04-10 22:22:31 +0000720 static unsigned long trap;
721#elif defined(MIPS)
722 static long a3;
723 static long r2;
Michal Ludvig10a88d02002-10-07 14:31:00 +0000724#elif defined(S390) || defined(S390X)
Wichert Akkermanc7926982000-04-10 22:22:31 +0000725 static long gpr2;
726 static long pc;
Michal Ludvig882eda82002-11-11 12:50:47 +0000727 static long syscall_mode;
Wichert Akkermanc1652e22001-03-27 12:17:16 +0000728#elif defined(HPPA)
729 static long r28;
Wichert Akkermanccef6372002-05-01 16:39:22 +0000730#elif defined(SH)
731 static long r0;
Roland McGrathf5a47772003-06-26 22:40:42 +0000732#elif defined(SH64)
Roland McGrath0f87c492003-06-03 23:29:04 +0000733 static long r9;
Michal Ludvig0e035502002-09-23 15:41:01 +0000734#elif defined(X86_64)
735 static long rax;
Roland McGrath761b5d72002-12-15 23:58:31 +0000736#endif
Wichert Akkermanc7926982000-04-10 22:22:31 +0000737#endif /* LINUX */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000738#ifdef FREEBSD
739 struct reg regs;
Roland McGrath761b5d72002-12-15 23:58:31 +0000740#endif /* FREEBSD */
Wichert Akkermanc7926982000-04-10 22:22:31 +0000741
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000742int
Pavel Machek4dc3b142000-02-01 17:58:41 +0000743get_scno(tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000744struct tcb *tcp;
745{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000746 long scno = 0;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000747#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000748 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +0000749#endif /* !PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000750
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000751#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +0000752#if defined(S390) || defined(S390X)
Roland McGrath96dc5142003-01-20 10:23:04 +0000753 if (tcp->flags & TCB_WAITEXECVE) {
754 /*
755 * When the execve system call completes successfully, the
756 * new process still has -ENOSYS (old style) or __NR_execve
757 * (new style) in gpr2. We cannot recover the scno again
758 * by disassembly, because the image that executed the
759 * syscall is gone now. Fortunately, we don't want it. We
760 * leave the flag set so that syscall_fixup can fake the
761 * result.
762 */
763 if (tcp->flags & TCB_INSYSCALL)
764 return 1;
765 /*
766 * This is the SIGTRAP after execve. We cannot try to read
767 * the system call here either.
768 */
769 tcp->flags &= ~TCB_WAITEXECVE;
770 return 0;
771 }
Roland McGrath2f924ca2003-06-26 22:23:28 +0000772
773 if (upeek(pid, PT_GPR2, &syscall_mode) < 0)
774 return -1;
775
776 if (syscall_mode != -ENOSYS) {
777 /*
778 * Since kernel version 2.5.44 the scno gets passed in gpr2.
779 */
780 scno = syscall_mode;
781 } else {
Michal Ludvig882eda82002-11-11 12:50:47 +0000782 /*
783 * Old style of "passing" the scno via the SVC instruction.
784 */
785
786 long opcode, offset_reg, tmp;
787 void * svc_addr;
788 int gpr_offset[16] = {PT_GPR0, PT_GPR1, PT_ORIGGPR2, PT_GPR3,
789 PT_GPR4, PT_GPR5, PT_GPR6, PT_GPR7,
790 PT_GPR8, PT_GPR9, PT_GPR10, PT_GPR11,
791 PT_GPR12, PT_GPR13, PT_GPR14, PT_GPR15};
Roland McGrath761b5d72002-12-15 23:58:31 +0000792
Michal Ludvig882eda82002-11-11 12:50:47 +0000793 if (upeek(pid, PT_PSWADDR, &pc) < 0)
794 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +0000795 errno = 0;
Michal Ludvig882eda82002-11-11 12:50:47 +0000796 opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0);
Roland McGrath96dc5142003-01-20 10:23:04 +0000797 if (errno) {
798 perror("peektext(pc-oneword)");
Michal Ludvig882eda82002-11-11 12:50:47 +0000799 return -1;
Roland McGrath96dc5142003-01-20 10:23:04 +0000800 }
Michal Ludvig882eda82002-11-11 12:50:47 +0000801
802 /*
803 * We have to check if the SVC got executed directly or via an
804 * EXECUTE instruction. In case of EXECUTE it is necessary to do
805 * instruction decoding to derive the system call number.
806 * Unfortunately the opcode sizes of EXECUTE and SVC are differently,
807 * so that this doesn't work if a SVC opcode is part of an EXECUTE
808 * opcode. Since there is no way to find out the opcode size this
809 * is the best we can do...
810 */
811
812 if ((opcode & 0xff00) == 0x0a00) {
813 /* SVC opcode */
814 scno = opcode & 0xff;
Roland McGrath761b5d72002-12-15 23:58:31 +0000815 }
Michal Ludvig882eda82002-11-11 12:50:47 +0000816 else {
817 /* SVC got executed by EXECUTE instruction */
818
819 /*
820 * Do instruction decoding of EXECUTE. If you really want to
821 * understand this, read the Principles of Operations.
822 */
823 svc_addr = (void *) (opcode & 0xfff);
824
825 tmp = 0;
826 offset_reg = (opcode & 0x000f0000) >> 16;
827 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
828 return -1;
829 svc_addr += tmp;
830
831 tmp = 0;
832 offset_reg = (opcode & 0x0000f000) >> 12;
833 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
834 return -1;
835 svc_addr += tmp;
836
837 scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0);
838 if (errno)
839 return -1;
840#if defined(S390X)
841 scno >>= 48;
842#else
843 scno >>= 16;
844#endif
845 tmp = 0;
846 offset_reg = (opcode & 0x00f00000) >> 20;
847 if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
848 return -1;
849
850 scno = (scno | tmp) & 0xff;
851 }
852 }
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000853#elif defined (POWERPC)
Roland McGratheb285352003-01-14 09:59:00 +0000854 if (upeek(pid, sizeof(unsigned long)*PT_R0, &scno) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000855 return -1;
856 if (!(tcp->flags & TCB_INSYSCALL)) {
857 /* Check if we return from execve. */
858 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
859 tcp->flags &= ~TCB_WAITEXECVE;
860 return 0;
861 }
862 }
863#elif defined (I386)
864 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
865 return -1;
Michal Ludvig0e035502002-09-23 15:41:01 +0000866#elif defined (X86_64)
867 if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
868 return -1;
869
Roland McGrath761b5d72002-12-15 23:58:31 +0000870 if (!(tcp->flags & TCB_INSYSCALL)) {
871 static int currpers=-1;
Michal Ludvig0e035502002-09-23 15:41:01 +0000872 long val;
873
874 /* Check CS register value. On x86-64 linux it is:
875 * 0x33 for long mode (64 bit)
876 * 0x23 for compatibility mode (32 bit)
Roland McGrath761b5d72002-12-15 23:58:31 +0000877 * It takes only one ptrace and thus doesn't need
Michal Ludvig0e035502002-09-23 15:41:01 +0000878 * to be cached.
879 */
880 if (upeek(pid, 8*CS, &val) < 0)
881 return -1;
882 switch(val)
883 {
884 case 0x23: currpers = 1; break;
885 case 0x33: currpers = 0; break;
886 default:
887 fprintf(stderr, "Unknown value CS=0x%02X while "
888 "detecting personality of process "
889 "PID=%d\n", (int)val, pid);
890 currpers = current_personality;
891 break;
892 }
893#if 0
894 /* This version analyzes the opcode of a syscall instruction.
895 * (int 0x80 on i386 vs. syscall on x86-64)
896 * It works, but is too complicated.
897 */
898 unsigned long val, rip, i;
899
900 if(upeek(pid, 8*RIP, &rip)<0)
901 perror("upeek(RIP)");
Roland McGrath761b5d72002-12-15 23:58:31 +0000902
Michal Ludvig0e035502002-09-23 15:41:01 +0000903 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
904 rip-=2;
905 errno = 0;
906
Roland McGrath761b5d72002-12-15 23:58:31 +0000907 call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
908 if (errno)
909 printf("ptrace_peektext failed: %s\n",
Michal Ludvig0e035502002-09-23 15:41:01 +0000910 strerror(errno));
911 switch (call & 0xffff)
912 {
913 /* x86-64: syscall = 0x0f 0x05 */
914 case 0x050f: currpers = 0; break;
915 /* i386: int 0x80 = 0xcd 0x80 */
916 case 0x80cd: currpers = 1; break;
917 default:
918 currpers = current_personality;
Roland McGrath761b5d72002-12-15 23:58:31 +0000919 fprintf(stderr,
Michal Ludvig0e035502002-09-23 15:41:01 +0000920 "Unknown syscall opcode (0x%04X) while "
921 "detecting personality of process "
922 "PID=%d\n", (int)call, pid);
923 break;
924 }
925#endif
926 if(currpers != current_personality)
927 {
928 char *names[]={"64 bit", "32 bit"};
929 set_personality(currpers);
Roland McGrath761b5d72002-12-15 23:58:31 +0000930 printf("[ Process PID=%d runs in %s mode. ]\n",
Michal Ludvig0e035502002-09-23 15:41:01 +0000931 pid, names[current_personality]);
932 }
Roland McGrath761b5d72002-12-15 23:58:31 +0000933 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000934#elif defined(IA64)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000935# define IA64_PSR_IS ((long)1 << 34)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000936 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000937 ia32 = (psr & IA64_PSR_IS) != 0;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000938 if (!(tcp->flags & TCB_INSYSCALL)) {
939 if (ia32) {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +0000940 if (upeek(pid, PT_R1, &scno) < 0) /* orig eax */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000941 return -1;
942 } else {
943 if (upeek (pid, PT_R15, &scno) < 0)
944 return -1;
945 }
Roland McGrathba954762003-03-05 06:29:06 +0000946 /* Check if we return from execve. */
947 if (tcp->flags & TCB_WAITEXECVE) {
948 tcp->flags &= ~TCB_WAITEXECVE;
949 return 0;
950 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000951 } else {
952 /* syscall in progress */
953 if (upeek (pid, PT_R8, &r8) < 0)
954 return -1;
955 if (upeek (pid, PT_R10, &r10) < 0)
956 return -1;
957 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000958#elif defined (ARM)
Roland McGrath0f87c492003-06-03 23:29:04 +0000959 /*
960 * Read complete register set in one go.
961 */
962 if (ptrace(PTRACE_GETREGS, pid, NULL, (void *)&regs) == -1)
963 return -1;
964
965 /*
966 * We only need to grab the syscall number on syscall entry.
967 */
968 if (regs.ARM_ip == 0) {
969 /*
970 * Note: we only deal with only 32-bit CPUs here.
971 */
972 if (regs.ARM_cpsr & 0x20) {
973 /*
974 * Get the Thumb-mode system call number
975 */
976 scno = regs.ARM_r7;
977 } else {
978 /*
979 * Get the ARM-mode system call number
980 */
981 errno = 0;
982 scno = ptrace(PTRACE_PEEKTEXT, pid, (void *)(regs.ARM_pc - 4), NULL);
983 if (errno)
984 return -1;
985
986 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
987 tcp->flags &= ~TCB_WAITEXECVE;
988 return 0;
989 }
990
991 if ((scno & 0x0ff00000) != 0x0f900000) {
992 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
993 scno);
994 return -1;
995 }
996
997 /*
998 * Fixup the syscall number
999 */
1000 scno &= 0x000fffff;
1001 }
1002
1003 if (tcp->flags & TCB_INSYSCALL) {
1004 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
1005 tcp->flags &= ~TCB_INSYSCALL;
1006 }
1007 } else {
1008 if (!(tcp->flags & TCB_INSYSCALL)) {
1009 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
1010 tcp->flags |= TCB_INSYSCALL;
1011 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001012 }
1013#elif defined (M68K)
1014 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
1015 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001016#elif defined (MIPS)
1017 if (upeek(pid, REG_A3, &a3) < 0)
1018 return -1;
1019
1020 if(!(tcp->flags & TCB_INSYSCALL)) {
1021 if (upeek(pid, REG_V0, &scno) < 0)
1022 return -1;
1023
1024 if (scno < 0 || scno > nsyscalls) {
1025 if(a3 == 0 || a3 == -1) {
1026 if(debug)
1027 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
1028 return 0;
1029 }
1030 }
1031 } else {
1032 if (upeek(pid, REG_V0, &r2) < 0)
1033 return -1;
1034 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001035#elif defined (ALPHA)
1036 if (upeek(pid, REG_A3, &a3) < 0)
1037 return -1;
1038
1039 if (!(tcp->flags & TCB_INSYSCALL)) {
1040 if (upeek(pid, REG_R0, &scno) < 0)
1041 return -1;
1042
1043 /* Check if we return from execve. */
1044 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1045 tcp->flags &= ~TCB_WAITEXECVE;
1046 return 0;
1047 }
1048
1049 /*
1050 * Do some sanity checks to figure out if it's
1051 * really a syscall entry
1052 */
1053 if (scno < 0 || scno > nsyscalls) {
1054 if (a3 == 0 || a3 == -1) {
1055 if (debug)
1056 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
1057 return 0;
1058 }
1059 }
1060 }
1061 else {
1062 if (upeek(pid, REG_R0, &r0) < 0)
1063 return -1;
1064 }
1065#elif defined (SPARC)
1066 /* Everything we need is in the current register set. */
1067 if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
1068 return -1;
1069
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001070 /* If we are entering, then disassemble the syscall trap. */
1071 if (!(tcp->flags & TCB_INSYSCALL)) {
1072 /* Retrieve the syscall trap instruction. */
1073 errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001074 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001075 if (errno)
1076 return -1;
1077
1078 /* Disassemble the trap to see what personality to use. */
1079 switch (trap) {
1080 case 0x91d02010:
1081 /* Linux/SPARC syscall trap. */
1082 set_personality(0);
1083 break;
Wichert Akkermandacfb6e1999-06-03 14:21:07 +00001084 case 0x91d0206d:
1085 /* Linux/SPARC64 syscall trap. */
1086 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
1087 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001088 case 0x91d02000:
1089 /* SunOS syscall trap. (pers 1) */
1090 fprintf(stderr,"syscall: SunOS no support\n");
1091 return -1;
1092 case 0x91d02008:
1093 /* Solaris 2.x syscall trap. (per 2) */
1094 set_personality(1);
Roland McGrath761b5d72002-12-15 23:58:31 +00001095 break;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001096 case 0x91d02009:
1097 /* NetBSD/FreeBSD syscall trap. */
1098 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
1099 return -1;
1100 case 0x91d02027:
1101 /* Solaris 2.x gettimeofday */
1102 set_personality(1);
1103 break;
1104 default:
1105 /* Unknown syscall trap. */
1106 if(tcp->flags & TCB_WAITEXECVE) {
1107 tcp->flags &= ~TCB_WAITEXECVE;
1108 return 0;
1109 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001110 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001111 return -1;
1112 }
1113
1114 /* Extract the system call number from the registers. */
1115 if (trap == 0x91d02027)
1116 scno = 156;
1117 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001118 scno = regs.r_g1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001119 if (scno == 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001120 scno = regs.r_o0;
1121 memmove (&regs.r_o0, &regs.r_o1, 7*sizeof(regs.r_o0));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001122 }
1123 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001124#elif defined(HPPA)
1125 if (upeek(pid, PT_GR20, &scno) < 0)
1126 return -1;
1127 if (!(tcp->flags & TCB_INSYSCALL)) {
1128 /* Check if we return from execve. */
1129 if ((tcp->flags & TCB_WAITEXECVE)) {
1130 tcp->flags &= ~TCB_WAITEXECVE;
1131 return 0;
1132 }
1133 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001134#elif defined(SH)
1135 /*
1136 * In the new syscall ABI, the system call number is in R3.
1137 */
1138 if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
1139 return -1;
1140
1141 if (scno < 0) {
1142 /* Odd as it may seem, a glibc bug has been known to cause
1143 glibc to issue bogus negative syscall numbers. So for
1144 our purposes, make strace print what it *should* have been */
1145 long correct_scno = (scno & 0xff);
1146 if (debug)
1147 fprintf(stderr,
Michal Ludvig53b320f2002-09-23 13:30:09 +00001148 "Detected glibc bug: bogus system call number = %ld, "
1149 "correcting to %ld\n",
Wichert Akkermanccef6372002-05-01 16:39:22 +00001150 scno,
1151 correct_scno);
1152 scno = correct_scno;
1153 }
1154
1155
1156 if (!(tcp->flags & TCB_INSYSCALL)) {
1157 /* Check if we return from execve. */
1158 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1159 tcp->flags &= ~TCB_WAITEXECVE;
1160 return 0;
1161 }
1162 }
Roland McGrathf5a47772003-06-26 22:40:42 +00001163#elif defined(SH64)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001164 if (upeek(pid, REG_SYSCALL, &scno) < 0)
1165 return -1;
1166 scno &= 0xFFFF;
1167
1168 if (!(tcp->flags & TCB_INSYSCALL)) {
1169 /* Check if we return from execve. */
1170 if (tcp->flags & TCB_WAITEXECVE) {
1171 tcp->flags &= ~TCB_WAITEXECVE;
1172 return 0;
1173 }
1174 }
Roland McGrathf5a47772003-06-26 22:40:42 +00001175#endif /* SH64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001176#endif /* LINUX */
1177#ifdef SUNOS4
1178 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
1179 return -1;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001180#elif defined(SH)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001181 /* new syscall ABI returns result in R0 */
1182 if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
1183 return -1;
Roland McGrathf5a47772003-06-26 22:40:42 +00001184#elif defined(SH64)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001185 /* ABI defines result returned in r9 */
1186 if (upeek(pid, REG_GENERAL(9), (long *)&r9) < 0)
1187 return -1;
1188
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001189#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001190#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001191#ifdef HAVE_PR_SYSCALL
John Hughes25299712001-03-06 10:10:06 +00001192 scno = tcp->status.PR_SYSCALL;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001193#else /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001194#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001195 scno = tcp->status.PR_WHAT;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001196#else /* FREEBSD */
1197 if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1198 perror("pread");
1199 return -1;
1200 }
1201 switch (regs.r_eax) {
1202 case SYS_syscall:
1203 case SYS___syscall:
1204 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1205 break;
1206 default:
1207 scno = regs.r_eax;
1208 break;
1209 }
1210#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001211#endif /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001212#endif /* USE_PROCFS */
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +00001213 if (!(tcp->flags & TCB_INSYSCALL))
1214 tcp->scno = scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +00001215 return 1;
1216}
1217
Pavel Machek4dc3b142000-02-01 17:58:41 +00001218
1219int
1220syscall_fixup(tcp)
1221struct tcb *tcp;
1222{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001223#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001224 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +00001225#else /* USE_PROCFS */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001226 int scno = tcp->scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +00001227
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001228 if (!(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001229 if (tcp->status.PR_WHY != PR_SYSENTRY) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001230 if (
1231 scno == SYS_fork
1232#ifdef SYS_vfork
1233 || scno == SYS_vfork
1234#endif /* SYS_vfork */
John Hughes4e36a812001-04-18 15:11:51 +00001235#ifdef SYS_fork1
1236 || scno == SYS_fork1
1237#endif /* SYS_fork1 */
1238#ifdef SYS_forkall
1239 || scno == SYS_forkall
1240#endif /* SYS_forkall */
1241#ifdef SYS_rfork1
1242 || scno == SYS_rfork1
1243#endif /* SYS_fork1 */
1244#ifdef SYS_rforkall
1245 || scno == SYS_rforkall
1246#endif /* SYS_rforkall */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001247 ) {
1248 /* We are returning in the child, fake it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001249 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001250 trace_syscall(tcp);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001251 tcp->status.PR_WHY = PR_SYSEXIT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001252 }
1253 else {
1254 fprintf(stderr, "syscall: missing entry\n");
1255 tcp->flags |= TCB_INSYSCALL;
1256 }
1257 }
1258 }
1259 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001260 if (tcp->status.PR_WHY != PR_SYSEXIT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001261 fprintf(stderr, "syscall: missing exit\n");
1262 tcp->flags &= ~TCB_INSYSCALL;
1263 }
1264 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001265#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001266#ifdef SUNOS4
1267 if (!(tcp->flags & TCB_INSYSCALL)) {
1268 if (scno == 0) {
1269 fprintf(stderr, "syscall: missing entry\n");
1270 tcp->flags |= TCB_INSYSCALL;
1271 }
1272 }
1273 else {
1274 if (scno != 0) {
1275 if (debug) {
1276 /*
1277 * This happens when a signal handler
1278 * for a signal which interrupted a
1279 * a system call makes another system call.
1280 */
1281 fprintf(stderr, "syscall: missing exit\n");
1282 }
1283 tcp->flags &= ~TCB_INSYSCALL;
1284 }
1285 }
1286#endif /* SUNOS4 */
1287#ifdef LINUX
1288#if defined (I386)
1289 if (upeek(pid, 4*EAX, &eax) < 0)
1290 return -1;
1291 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1292 if (debug)
1293 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1294 return 0;
1295 }
Michal Ludvig0e035502002-09-23 15:41:01 +00001296#elif defined (X86_64)
1297 if (upeek(pid, 8*RAX, &rax) < 0)
1298 return -1;
Roland McGrath998fac72004-06-23 01:40:45 +00001299 if (current_personality == 1)
1300 rax = (long int)(int)rax; /* sign extend from 32 bits */
Michal Ludvig0e035502002-09-23 15:41:01 +00001301 if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1302 if (debug)
1303 fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1304 return 0;
1305 }
Michal Ludvig10a88d02002-10-07 14:31:00 +00001306#elif defined (S390) || defined (S390X)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001307 if (upeek(pid, PT_GPR2, &gpr2) < 0)
1308 return -1;
Michal Ludvig882eda82002-11-11 12:50:47 +00001309 if (syscall_mode != -ENOSYS)
1310 syscall_mode = tcp->scno;
1311 if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001312 if (debug)
1313 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1314 return 0;
1315 }
Roland McGrath96dc5142003-01-20 10:23:04 +00001316 else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE))
1317 == (TCB_INSYSCALL|TCB_WAITEXECVE))
1318 && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1319 /*
1320 * Fake a return value of zero. We leave the TCB_WAITEXECVE
1321 * flag set for the post-execve SIGTRAP to see and reset.
1322 */
1323 gpr2 = 0;
1324 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001325#elif defined (POWERPC)
1326# define SO_MASK 0x10000000
Roland McGratheb285352003-01-14 09:59:00 +00001327 if (upeek(pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001328 return -1;
Roland McGratheb285352003-01-14 09:59:00 +00001329 if (upeek(pid, sizeof(unsigned long)*PT_R3, &result) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001330 return -1;
1331 if (flags & SO_MASK)
1332 result = -result;
1333#elif defined (M68K)
1334 if (upeek(pid, 4*PT_D0, &d0) < 0)
1335 return -1;
1336 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1337 if (debug)
1338 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1339 return 0;
1340 }
1341#elif defined (ARM)
Roland McGrath0f87c492003-06-03 23:29:04 +00001342 /*
1343 * Nothing required
1344 */
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001345#elif defined (HPPA)
1346 if (upeek(pid, PT_GR28, &r28) < 0)
1347 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001348#elif defined(IA64)
1349 if (upeek(pid, PT_R10, &r10) < 0)
1350 return -1;
1351 if (upeek(pid, PT_R8, &r8) < 0)
1352 return -1;
1353 if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1354 if (debug)
1355 fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1356 return 0;
1357 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001358#endif
1359#endif /* LINUX */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001360 return 1;
1361}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001362
Pavel Machek4dc3b142000-02-01 17:58:41 +00001363int
1364get_error(tcp)
1365struct tcb *tcp;
1366{
1367 int u_error = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001368#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +00001369#if defined(S390) || defined(S390X)
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001370 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1371 tcp->u_rval = -1;
1372 u_error = -gpr2;
1373 }
1374 else {
1375 tcp->u_rval = gpr2;
1376 u_error = 0;
1377 }
Michal Ludvig10a88d02002-10-07 14:31:00 +00001378#else /* !S390 && !S390X */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001379#ifdef I386
1380 if (eax < 0 && -eax < nerrnos) {
1381 tcp->u_rval = -1;
1382 u_error = -eax;
1383 }
1384 else {
1385 tcp->u_rval = eax;
1386 u_error = 0;
1387 }
1388#else /* !I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001389#ifdef X86_64
1390 if (rax < 0 && -rax < nerrnos) {
1391 tcp->u_rval = -1;
1392 u_error = -rax;
1393 }
1394 else {
1395 tcp->u_rval = rax;
1396 u_error = 0;
1397 }
1398#else
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001399#ifdef IA64
1400 if (ia32) {
1401 int err;
1402
1403 err = (int)r8;
1404 if (err < 0 && -err < nerrnos) {
1405 tcp->u_rval = -1;
1406 u_error = -err;
1407 }
1408 else {
1409 tcp->u_rval = err;
1410 u_error = 0;
1411 }
1412 } else {
1413 if (r10) {
1414 tcp->u_rval = -1;
1415 u_error = r8;
1416 } else {
1417 tcp->u_rval = r8;
1418 u_error = 0;
1419 }
1420 }
1421#else /* !IA64 */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001422#ifdef MIPS
1423 if (a3) {
1424 tcp->u_rval = -1;
1425 u_error = r2;
1426 } else {
1427 tcp->u_rval = r2;
1428 u_error = 0;
1429 }
1430#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001431#ifdef POWERPC
Roland McGrath190f8dd2004-01-13 10:13:44 +00001432 if (result && (unsigned long) -result < nerrnos) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001433 tcp->u_rval = -1;
1434 u_error = -result;
1435 }
1436 else {
1437 tcp->u_rval = result;
1438 u_error = 0;
1439 }
1440#else /* !POWERPC */
1441#ifdef M68K
1442 if (d0 && (unsigned) -d0 < nerrnos) {
1443 tcp->u_rval = -1;
1444 u_error = -d0;
1445 }
1446 else {
1447 tcp->u_rval = d0;
1448 u_error = 0;
1449 }
1450#else /* !M68K */
1451#ifdef ARM
Roland McGrath0f87c492003-06-03 23:29:04 +00001452 if (regs.ARM_r0 && (unsigned) -regs.ARM_r0 < nerrnos) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001453 tcp->u_rval = -1;
Roland McGrath0f87c492003-06-03 23:29:04 +00001454 u_error = -regs.ARM_r0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001455 }
1456 else {
Roland McGrath0f87c492003-06-03 23:29:04 +00001457 tcp->u_rval = regs.ARM_r0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001458 u_error = 0;
1459 }
1460#else /* !ARM */
1461#ifdef ALPHA
1462 if (a3) {
1463 tcp->u_rval = -1;
1464 u_error = r0;
1465 }
1466 else {
1467 tcp->u_rval = r0;
1468 u_error = 0;
1469 }
1470#else /* !ALPHA */
1471#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001472 if (regs.r_psr & PSR_C) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001473 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001474 u_error = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001475 }
1476 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001477 tcp->u_rval = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001478 u_error = 0;
1479 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001480#else /* !SPARC */
1481#ifdef HPPA
1482 if (r28 && (unsigned) -r28 < nerrnos) {
1483 tcp->u_rval = -1;
1484 u_error = -r28;
1485 }
1486 else {
1487 tcp->u_rval = r28;
1488 u_error = 0;
1489 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001490#else
1491#ifdef SH
1492 /* interpret R0 as return value or error number */
1493 if (r0 && (unsigned) -r0 < nerrnos) {
1494 tcp->u_rval = -1;
1495 u_error = -r0;
1496 }
1497 else {
1498 tcp->u_rval = r0;
1499 u_error = 0;
1500 }
Roland McGrathe1e584b2003-06-02 19:18:58 +00001501#else
Roland McGrathf5a47772003-06-26 22:40:42 +00001502#ifdef SH64
Roland McGrathe1e584b2003-06-02 19:18:58 +00001503 /* interpret result as return value or error number */
1504 if (r9 && (unsigned) -r9 < nerrnos) {
1505 tcp->u_rval = -1;
1506 u_error = -r9;
1507 }
1508 else {
1509 tcp->u_rval = r9;
1510 u_error = 0;
1511 }
Roland McGrathf5a47772003-06-26 22:40:42 +00001512#endif /* SH64 */
Wichert Akkermanccef6372002-05-01 16:39:22 +00001513#endif /* SH */
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001514#endif /* HPPA */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001515#endif /* SPARC */
1516#endif /* ALPHA */
1517#endif /* ARM */
1518#endif /* M68K */
1519#endif /* POWERPC */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001520#endif /* MIPS */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001521#endif /* IA64 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001522#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001523#endif /* I386 */
Michal Ludvig10a88d02002-10-07 14:31:00 +00001524#endif /* S390 || S390X */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001525#endif /* LINUX */
1526#ifdef SUNOS4
1527 /* get error code from user struct */
1528 if (upeek(pid, uoff(u_error), &u_error) < 0)
1529 return -1;
1530 u_error >>= 24; /* u_error is a char */
1531
1532 /* get system call return value */
1533 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1534 return -1;
1535#endif /* SUNOS4 */
1536#ifdef SVR4
1537#ifdef SPARC
1538 /* Judicious guessing goes a long way. */
1539 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1540 tcp->u_rval = -1;
1541 u_error = tcp->status.pr_reg[R_O0];
1542 }
1543 else {
1544 tcp->u_rval = tcp->status.pr_reg[R_O0];
1545 u_error = 0;
1546 }
1547#endif /* SPARC */
1548#ifdef I386
1549 /* Wanna know how to kill an hour single-stepping? */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001550 if (tcp->status.PR_REG[EFL] & 0x1) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001551 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001552 u_error = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001553 }
1554 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001555 tcp->u_rval = tcp->status.PR_REG[EAX];
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001556#ifdef HAVE_LONG_LONG
1557 tcp->u_lrval =
1558 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1559 tcp->status.PR_REG[EAX];
1560#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001561 u_error = 0;
1562 }
1563#endif /* I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00001564#ifdef X86_64
1565 /* Wanna know how to kill an hour single-stepping? */
1566 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1567 tcp->u_rval = -1;
1568 u_error = tcp->status.PR_REG[RAX];
1569 }
1570 else {
1571 tcp->u_rval = tcp->status.PR_REG[RAX];
1572 u_error = 0;
1573 }
1574#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001575#ifdef MIPS
1576 if (tcp->status.pr_reg[CTX_A3]) {
1577 tcp->u_rval = -1;
1578 u_error = tcp->status.pr_reg[CTX_V0];
1579 }
1580 else {
1581 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1582 u_error = 0;
1583 }
1584#endif /* MIPS */
1585#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001586#ifdef FREEBSD
1587 if (regs.r_eflags & PSL_C) {
1588 tcp->u_rval = -1;
1589 u_error = regs.r_eax;
1590 } else {
1591 tcp->u_rval = regs.r_eax;
1592 tcp->u_lrval =
1593 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1594 u_error = 0;
1595 }
Roland McGrath761b5d72002-12-15 23:58:31 +00001596#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001597 tcp->u_error = u_error;
1598 return 1;
1599}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001600
Roland McGrathb69f81b2002-12-21 23:25:18 +00001601int
1602force_result(tcp, error, rval)
1603 struct tcb *tcp;
1604 int error;
1605 long rval;
1606{
1607#ifdef LINUX
1608#if defined(S390) || defined(S390X)
1609 gpr2 = error ? -error : rval;
Roland McGrathb69f81b2002-12-21 23:25:18 +00001610 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
1611 return -1;
1612#else /* !S390 && !S390X */
1613#ifdef I386
1614 eax = error ? -error : rval;
1615 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
1616 return -1;
1617#else /* !I386 */
1618#ifdef X86_64
1619 rax = error ? -error : rval;
Roland McGrath998fac72004-06-23 01:40:45 +00001620 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 8), rax) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001621 return -1;
1622#else
1623#ifdef IA64
1624 if (ia32) {
1625 r8 = error ? -error : rval;
1626 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
1627 return -1;
1628 }
1629 else {
1630 if (error) {
1631 r8 = error;
1632 r10 = -1;
1633 }
1634 else {
1635 r8 = rval;
1636 r10 = 0;
1637 }
1638 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
1639 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
1640 return -1;
1641 }
1642#else /* !IA64 */
1643#ifdef MIPS
1644 if (error) {
1645 r2 = error;
1646 a3 = -1;
1647 }
1648 else {
1649 r2 = rval;
1650 a3 = 0;
1651 }
1652 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1653 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
1654 return -1;
1655#else
1656#ifdef POWERPC
Roland McGratheb285352003-01-14 09:59:00 +00001657 if (upeek(tcp->pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001658 return -1;
1659 if (error) {
1660 flags |= SO_MASK;
1661 result = error;
1662 }
1663 else {
1664 flags &= ~SO_MASK;
1665 result = rval;
1666 }
Roland McGratheb285352003-01-14 09:59:00 +00001667 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 ||
1668 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_R3), result) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001669 return -1;
1670#else /* !POWERPC */
1671#ifdef M68K
1672 d0 = error ? -error : rval;
1673 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
1674 return -1;
1675#else /* !M68K */
1676#ifdef ARM
Roland McGrath7c051d22003-06-26 22:29:32 +00001677 regs.ARM_r0 = error ? -error : rval;
1678 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), regs.ARM_r0) < 0)
Roland McGrathb69f81b2002-12-21 23:25:18 +00001679 return -1;
1680#else /* !ARM */
1681#ifdef ALPHA
1682 if (error) {
1683 a3 = -1;
1684 r0 = error;
1685 }
1686 else {
1687 a3 = 0;
1688 r0 = rval;
1689 }
1690 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1691 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
1692 return -1;
1693#else /* !ALPHA */
1694#ifdef SPARC
1695 if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)&regs, 0) < 0)
1696 return -1;
1697 if (error) {
1698 regs.r_psr |= PSR_C;
1699 regs.r_o0 = error;
1700 }
1701 else {
1702 regs.r_psr &= ~PSR_C;
1703 regs.r_o0 = rval;
1704 }
1705 if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)&regs, 0) < 0)
1706 return -1;
1707#else /* !SPARC */
1708#ifdef HPPA
1709 r28 = error ? -error : rval;
1710 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
1711 return -1;
1712#else
1713#ifdef SH
1714 r0 = error ? -error : rval;
1715 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
1716 return -1;
Roland McGrathe1e584b2003-06-02 19:18:58 +00001717#else
Roland McGrathf5a47772003-06-26 22:40:42 +00001718#ifdef SH64
Roland McGrathe1e584b2003-06-02 19:18:58 +00001719 r9 = error ? -error : rval;
1720 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)REG_GENERAL(9), r9) < 0)
1721 return -1;
Roland McGrathf5a47772003-06-26 22:40:42 +00001722#endif /* SH64 */
Roland McGrathb69f81b2002-12-21 23:25:18 +00001723#endif /* SH */
1724#endif /* HPPA */
1725#endif /* SPARC */
1726#endif /* ALPHA */
1727#endif /* ARM */
1728#endif /* M68K */
1729#endif /* POWERPC */
1730#endif /* MIPS */
1731#endif /* IA64 */
1732#endif /* X86_64 */
1733#endif /* I386 */
1734#endif /* S390 || S390X */
1735#endif /* LINUX */
1736#ifdef SUNOS4
1737 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
1738 error << 24) < 0 ||
1739 ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
1740 return -1;
1741#endif /* SUNOS4 */
1742#ifdef SVR4
1743 /* XXX no clue */
1744 return -1;
1745#endif /* SVR4 */
1746#ifdef FREEBSD
1747 if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1748 perror("pread");
1749 return -1;
1750 }
1751 if (error) {
1752 regs.r_eflags |= PSL_C;
1753 regs.r_eax = error;
1754 }
1755 else {
1756 regs.r_eflags &= ~PSL_C;
1757 regs.r_eax = rval;
1758 }
1759 if (pwrite(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1760 perror("pwrite");
1761 return -1;
1762 }
1763#endif /* FREEBSD */
1764
1765 /* All branches reach here on success (only). */
1766 tcp->u_error = error;
1767 tcp->u_rval = rval;
1768 return 0;
1769}
1770
Pavel Machek4dc3b142000-02-01 17:58:41 +00001771int syscall_enter(tcp)
1772struct tcb *tcp;
1773{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001774#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001775 int pid = tcp->pid;
Roland McGrath761b5d72002-12-15 23:58:31 +00001776#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001777#ifdef LINUX
Michal Ludvig10a88d02002-10-07 14:31:00 +00001778#if defined(S390) || defined(S390X)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001779 {
1780 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001781 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1782 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001783 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001784 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001785 for (i = 0; i < tcp->u_nargs; i++) {
Michal Ludvig10a88d02002-10-07 14:31:00 +00001786 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001787 return -1;
1788 }
1789 }
1790#elif defined (ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001791 {
1792 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001793 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1794 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001795 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001796 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001797 for (i = 0; i < tcp->u_nargs; i++) {
Wichert Akkermanb859bea1999-04-18 22:50:50 +00001798 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1799 * for scno somewhere above here!
1800 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001801 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1802 return -1;
1803 }
1804 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001805#elif defined (IA64)
1806 {
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001807 if (!ia32) {
1808 unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1809 /* be backwards compatible with kernel < 2.4.4... */
1810# ifndef PT_RBS_END
1811# define PT_RBS_END PT_AR_BSP
1812# endif
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001813
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001814 if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001815 return -1;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00001816 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1817 return -1;
1818
1819 sof = (cfm >> 0) & 0x7f;
1820 sol = (cfm >> 7) & 0x7f;
1821 out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1822
1823 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1824 && sysent[tcp->scno].nargs != -1)
1825 tcp->u_nargs = sysent[tcp->scno].nargs;
1826 else
1827 tcp->u_nargs = MAX_ARGS;
1828 for (i = 0; i < tcp->u_nargs; ++i) {
1829 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1830 sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1831 return -1;
1832 }
1833 } else {
1834 int i;
1835
1836 if (/* EBX = out0 */
1837 upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1838 /* ECX = out1 */
1839 || upeek(pid, PT_R9, (long *) &tcp->u_arg[1]) < 0
1840 /* EDX = out2 */
1841 || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1842 /* ESI = out3 */
1843 || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1844 /* EDI = out4 */
1845 || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1846 /* EBP = out5 */
1847 || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1848 return -1;
1849
1850 for (i = 0; i < 6; ++i)
1851 /* truncate away IVE sign-extension */
1852 tcp->u_arg[i] &= 0xffffffff;
1853
1854 if (tcp->scno >= 0 && tcp->scno < nsyscalls
1855 && sysent[tcp->scno].nargs != -1)
1856 tcp->u_nargs = sysent[tcp->scno].nargs;
1857 else
1858 tcp->u_nargs = 5;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001859 }
1860 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001861#elif defined (MIPS)
1862 {
1863 long sp;
1864 int i, nargs;
1865
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001866 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1867 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001868 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001869 nargs = tcp->u_nargs = MAX_ARGS;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001870 if(nargs > 4) {
1871 if(upeek(pid, REG_SP, &sp) < 0)
1872 return -1;
1873 for(i = 0; i < 4; i++) {
1874 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1875 return -1;
1876 }
1877 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1878 (char *)(tcp->u_arg + 4));
1879 } else {
1880 for(i = 0; i < nargs; i++) {
1881 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1882 return -1;
1883 }
1884 }
1885 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001886#elif defined (POWERPC)
Roland McGrath761b5d72002-12-15 23:58:31 +00001887#ifndef PT_ORIG_R3
1888#define PT_ORIG_R3 34
1889#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001890 {
1891 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001892 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1893 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001894 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001895 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001896 for (i = 0; i < tcp->u_nargs; i++) {
Roland McGratheb285352003-01-14 09:59:00 +00001897 if (upeek(pid, (i==0) ?
1898 (sizeof(unsigned long)*PT_ORIG_R3) :
1899 ((i+PT_R3)*sizeof(unsigned long)),
1900 &tcp->u_arg[i]) < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001901 return -1;
1902 }
1903 }
1904#elif defined (SPARC)
1905 {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001906 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001907
1908 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1909 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001910 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001911 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001912 for (i = 0; i < tcp->u_nargs; i++)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001913 tcp->u_arg[i] = *((&regs.r_o0) + i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001914 }
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001915#elif defined (HPPA)
1916 {
1917 int i;
1918
1919 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1920 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001921 else
Wichert Akkermanc1652e22001-03-27 12:17:16 +00001922 tcp->u_nargs = MAX_ARGS;
1923 for (i = 0; i < tcp->u_nargs; i++) {
1924 if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1925 return -1;
1926 }
1927 }
Roland McGrath0f87c492003-06-03 23:29:04 +00001928#elif defined(ARM)
1929 {
1930 int i;
1931
1932 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1933 tcp->u_nargs = sysent[tcp->scno].nargs;
1934 else
1935 tcp->u_nargs = MAX_ARGS;
1936 for (i = 0; i < tcp->u_nargs; i++)
1937 tcp->u_arg[i] = regs.uregs[i];
1938 }
Wichert Akkermanccef6372002-05-01 16:39:22 +00001939#elif defined(SH)
1940 {
Roland McGrath761b5d72002-12-15 23:58:31 +00001941 int i;
Wichert Akkermanccef6372002-05-01 16:39:22 +00001942 static int syscall_regs[] = {
1943 REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
1944 REG_REG0, REG_REG0+1, REG_REG0+2
1945 };
1946
1947 tcp->u_nargs = sysent[tcp->scno].nargs;
1948 for (i = 0; i < tcp->u_nargs; i++) {
1949 if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
1950 return -1;
1951 }
1952 }
Roland McGrathf5a47772003-06-26 22:40:42 +00001953#elif defined(SH64)
Roland McGrathe1e584b2003-06-02 19:18:58 +00001954 {
1955 int i;
1956 /* Registers used by SH5 Linux system calls for parameters */
1957 static int syscall_regs[] = { 2, 3, 4, 5, 6, 7 };
1958
1959 /*
1960 * TODO: should also check that the number of arguments encoded
1961 * in the trap number matches the number strace expects.
1962 */
1963 /*
1964 assert(sysent[tcp->scno].nargs <
1965 sizeof(syscall_regs)/sizeof(syscall_regs[0]));
1966 */
1967
1968 tcp->u_nargs = sysent[tcp->scno].nargs;
1969 for (i = 0; i < tcp->u_nargs; i++) {
1970 if (upeek(pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1971 return -1;
1972 }
1973 }
1974
Michal Ludvig0e035502002-09-23 15:41:01 +00001975#elif defined(X86_64)
1976 {
1977 int i;
1978 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
1979 {RDI,RSI,RDX,R10,R8,R9}, /* x86-64 ABI */
1980 {RBX,RCX,RDX,RDX,RSI,RDI,RBP} /* i386 ABI */
1981 };
Roland McGrath761b5d72002-12-15 23:58:31 +00001982
Michal Ludvig0e035502002-09-23 15:41:01 +00001983 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1984 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00001985 else
Michal Ludvig0e035502002-09-23 15:41:01 +00001986 tcp->u_nargs = MAX_ARGS;
1987 for (i = 0; i < tcp->u_nargs; i++) {
1988 if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
1989 return -1;
1990 }
1991 }
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001992#else /* Other architecture (like i386) (32bits specific) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001993 {
1994 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001995 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++) {
2000 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
2001 return -1;
2002 }
2003 }
Roland McGrath761b5d72002-12-15 23:58:31 +00002004#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002005#endif /* LINUX */
2006#ifdef SUNOS4
2007 {
2008 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002009 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2010 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00002011 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002012 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002013 for (i = 0; i < tcp->u_nargs; i++) {
2014 struct user *u;
2015
2016 if (upeek(pid, uoff(u_arg[0]) +
2017 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
2018 return -1;
2019 }
2020 }
2021#endif /* SUNOS4 */
2022#ifdef SVR4
2023#ifdef MIPS
2024 /*
2025 * SGI is broken: even though it has pr_sysarg, it doesn't
2026 * set them on system call entry. Get a clue.
2027 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002028 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002029 tcp->u_nargs = sysent[tcp->scno].nargs;
2030 else
2031 tcp->u_nargs = tcp->status.pr_nsysarg;
2032 if (tcp->u_nargs > 4) {
2033 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2034 4*sizeof(tcp->u_arg[0]));
2035 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
2036 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
2037 }
2038 else {
2039 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2040 tcp->u_nargs*sizeof(tcp->u_arg[0]));
2041 }
John Hughes25299712001-03-06 10:10:06 +00002042#elif UNIXWARE >= 2
2043 /*
2044 * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
2045 */
2046 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2047 tcp->u_nargs = sysent[tcp->scno].nargs;
2048 else
2049 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
2050 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2051 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2052#elif defined (HAVE_PR_SYSCALL)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002053 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002054 tcp->u_nargs = sysent[tcp->scno].nargs;
2055 else
2056 tcp->u_nargs = tcp->status.pr_nsysarg;
2057 {
2058 int i;
2059 for (i = 0; i < tcp->u_nargs; i++)
2060 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
2061 }
John Hughes25299712001-03-06 10:10:06 +00002062#elif defined (I386)
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002063 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002064 tcp->u_nargs = sysent[tcp->scno].nargs;
2065 else
2066 tcp->u_nargs = 5;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002067 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002068 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
John Hughes25299712001-03-06 10:10:06 +00002069#else
2070 I DONT KNOW WHAT TO DO
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002071#endif /* !HAVE_PR_SYSCALL */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002072#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002073#ifdef FREEBSD
2074 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2075 sysent[tcp->scno].nargs > tcp->status.val)
2076 tcp->u_nargs = sysent[tcp->scno].nargs;
Roland McGrath761b5d72002-12-15 23:58:31 +00002077 else
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002078 tcp->u_nargs = tcp->status.val;
2079 if (tcp->u_nargs < 0)
2080 tcp->u_nargs = 0;
2081 if (tcp->u_nargs > MAX_ARGS)
2082 tcp->u_nargs = MAX_ARGS;
2083 switch(regs.r_eax) {
2084 case SYS___syscall:
2085 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2086 regs.r_esp + sizeof(int) + sizeof(quad_t));
2087 break;
2088 case SYS_syscall:
2089 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2090 regs.r_esp + 2 * sizeof(int));
2091 break;
2092 default:
2093 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2094 regs.r_esp + sizeof(int));
2095 break;
2096 }
2097#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00002098 return 1;
2099}
2100
2101int
2102trace_syscall(tcp)
2103struct tcb *tcp;
2104{
2105 int sys_res;
2106 struct timeval tv;
2107 int res;
2108
2109 /* Measure the exit time as early as possible to avoid errors. */
2110 if (dtime && (tcp->flags & TCB_INSYSCALL))
2111 gettimeofday(&tv, NULL);
2112
2113 res = get_scno(tcp);
2114 if (res != 1)
2115 return res;
2116
2117 res = syscall_fixup(tcp);
2118 if (res != 1)
2119 return res;
2120
2121 if (tcp->flags & TCB_INSYSCALL) {
2122 long u_error;
2123 res = get_error(tcp);
2124 if (res != 1)
2125 return res;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002126
2127 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002128 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2129 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00002130 tcp->flags &= ~TCB_INSYSCALL;
2131 return 0;
2132 }
2133
2134 if (tcp->flags & TCB_REPRINT) {
2135 printleader(tcp);
2136 tprintf("<... ");
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002137 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Pavel Machek4dc3b142000-02-01 17:58:41 +00002138 tprintf("syscall_%lu", tcp->scno);
2139 else
2140 tprintf("%s", sysent[tcp->scno].sys_name);
2141 tprintf(" resumed> ");
2142 }
2143
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002144 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00002145 call_count[tcp->scno]++;
2146 if (tcp->u_error)
2147 error_count[tcp->scno]++;
2148 tv_sub(&tv, &tv, &tcp->etime);
2149#ifdef LINUX
2150 if (tv_cmp(&tv, &tcp->dtime) > 0) {
Roland McGrathee9c5b52003-11-01 22:11:22 +00002151 static struct timeval one_tick;
2152 if (one_tick.tv_usec == 0) {
2153 /* Initialize it. */
2154 struct itimerval it;
2155 memset(&it, 0, sizeof it);
2156 it.it_interval.tv_usec = 1;
2157 setitimer(ITIMER_REAL, &it, NULL);
2158 getitimer(ITIMER_REAL, &it);
2159 one_tick = it.it_interval;
2160 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00002161
2162 if (tv_nz(&tcp->dtime))
2163 tv = tcp->dtime;
2164 else if (tv_cmp(&tv, &one_tick) > 0) {
2165 if (tv_cmp(&shortest, &one_tick) < 0)
2166 tv = shortest;
2167 else
2168 tv = one_tick;
2169 }
2170 }
2171#endif /* LINUX */
2172 if (tv_cmp(&tv, &shortest) < 0)
2173 shortest = tv;
2174 tv_add(&tv_count[tcp->scno],
2175 &tv_count[tcp->scno], &tv);
2176 tcp->flags &= ~TCB_INSYSCALL;
2177 return 0;
2178 }
2179
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002180 if (tcp->scno >= nsyscalls || tcp->scno < 0
Pavel Machek4dc3b142000-02-01 17:58:41 +00002181 || (qual_flags[tcp->scno] & QUAL_RAW))
2182 sys_res = printargs(tcp);
Michal Ludvig17f8fb32002-11-06 13:17:21 +00002183 else {
2184 if (not_failing_only && tcp->u_error)
Roland McGrath761b5d72002-12-15 23:58:31 +00002185 return 0; /* ignore failed syscalls */
Pavel Machek4dc3b142000-02-01 17:58:41 +00002186 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
Roland McGrath761b5d72002-12-15 23:58:31 +00002187 }
Pavel Machek4dc3b142000-02-01 17:58:41 +00002188 u_error = tcp->u_error;
2189 tprintf(") ");
2190 tabto(acolumn);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002191 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2192 qual_flags[tcp->scno] & QUAL_RAW) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00002193 if (u_error)
2194 tprintf("= -1 (errno %ld)", u_error);
2195 else
2196 tprintf("= %#lx", tcp->u_rval);
2197 }
2198 else if (!(sys_res & RVAL_NONE) && u_error) {
2199 switch (u_error) {
2200#ifdef LINUX
2201 case ERESTARTSYS:
2202 tprintf("= ? ERESTARTSYS (To be restarted)");
2203 break;
2204 case ERESTARTNOINTR:
2205 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2206 break;
2207 case ERESTARTNOHAND:
2208 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2209 break;
Roland McGrath9c555e72003-07-09 09:47:59 +00002210 case ERESTART_RESTARTBLOCK:
2211 tprintf("= ? ERESTART_RESTARTBLOCK (To be restarted)");
2212 break;
Pavel Machek4dc3b142000-02-01 17:58:41 +00002213#endif /* LINUX */
2214 default:
2215 tprintf("= -1 ");
Wichert Akkerman4527dae2002-03-31 19:03:29 +00002216 if (u_error < 0)
2217 tprintf("E??? (errno %ld)", u_error);
Pavel Machek4dc3b142000-02-01 17:58:41 +00002218 else if (u_error < nerrnos)
Roland McGrath761b5d72002-12-15 23:58:31 +00002219 tprintf("%s (%s)", errnoent[u_error],
2220 strerror(u_error));
Pavel Machek4dc3b142000-02-01 17:58:41 +00002221 else
Roland McGrath761b5d72002-12-15 23:58:31 +00002222 tprintf("ERRNO_%ld (%s)", u_error,
2223 strerror(u_error));
Pavel Machek4dc3b142000-02-01 17:58:41 +00002224 break;
2225 }
2226 }
2227 else {
2228 if (sys_res & RVAL_NONE)
2229 tprintf("= ?");
2230 else {
2231 switch (sys_res & RVAL_MASK) {
2232 case RVAL_HEX:
2233 tprintf("= %#lx", tcp->u_rval);
2234 break;
2235 case RVAL_OCTAL:
2236 tprintf("= %#lo", tcp->u_rval);
2237 break;
2238 case RVAL_UDECIMAL:
2239 tprintf("= %lu", tcp->u_rval);
2240 break;
2241 case RVAL_DECIMAL:
2242 tprintf("= %ld", tcp->u_rval);
2243 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002244#ifdef HAVE_LONG_LONG
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002245 case RVAL_LHEX:
2246 tprintf("= %#llx", tcp->u_lrval);
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002247 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002248 case RVAL_LOCTAL:
2249 tprintf("= %#llo", tcp->u_lrval);
2250 break;
2251 case RVAL_LUDECIMAL:
2252 tprintf("= %llu", tcp->u_lrval);
2253 break;
2254 case RVAL_LDECIMAL:
2255 tprintf("= %lld", tcp->u_lrval);
2256 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00002257#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00002258 default:
2259 fprintf(stderr,
2260 "invalid rval format\n");
2261 break;
2262 }
2263 }
2264 if ((sys_res & RVAL_STR) && tcp->auxstr)
2265 tprintf(" (%s)", tcp->auxstr);
2266 }
2267 if (dtime) {
2268 tv_sub(&tv, &tv, &tcp->etime);
2269 tprintf(" <%ld.%06ld>",
2270 (long) tv.tv_sec, (long) tv.tv_usec);
2271 }
2272 printtrailer(tcp);
2273
2274 dumpio(tcp);
2275 if (fflush(tcp->outf) == EOF)
2276 return -1;
2277 tcp->flags &= ~TCB_INSYSCALL;
2278 return 0;
2279 }
2280
2281 /* Entering system call */
2282 res = syscall_enter(tcp);
2283 if (res != 1)
2284 return res;
2285
Pavel Machekd8ae7e32000-02-01 17:17:25 +00002286 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002287#ifdef LINUX
Michal Ludvig0e035502002-09-23 15:41:01 +00002288#if !defined (ALPHA) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002289 case SYS_socketcall:
2290 decode_subcall(tcp, SYS_socket_subcall,
2291 SYS_socket_nsubcalls, deref_style);
2292 break;
2293 case SYS_ipc:
2294 decode_subcall(tcp, SYS_ipc_subcall,
2295 SYS_ipc_nsubcalls, shift_style);
2296 break;
Michal Ludvig0e035502002-09-23 15:41:01 +00002297#endif /* !ALPHA && !MIPS && !SPARC && !HPPA && !X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002298#ifdef SPARC
2299 case SYS_socketcall:
2300 sparc_socket_decode (tcp);
2301 break;
2302#endif
2303#endif /* LINUX */
2304#ifdef SVR4
2305#ifdef SYS_pgrpsys_subcall
2306 case SYS_pgrpsys:
2307 decode_subcall(tcp, SYS_pgrpsys_subcall,
2308 SYS_pgrpsys_nsubcalls, shift_style);
2309 break;
2310#endif /* SYS_pgrpsys_subcall */
2311#ifdef SYS_sigcall_subcall
2312 case SYS_sigcall:
2313 decode_subcall(tcp, SYS_sigcall_subcall,
2314 SYS_sigcall_nsubcalls, mask_style);
2315 break;
2316#endif /* SYS_sigcall_subcall */
2317 case SYS_msgsys:
2318 decode_subcall(tcp, SYS_msgsys_subcall,
2319 SYS_msgsys_nsubcalls, shift_style);
2320 break;
2321 case SYS_shmsys:
2322 decode_subcall(tcp, SYS_shmsys_subcall,
2323 SYS_shmsys_nsubcalls, shift_style);
2324 break;
2325 case SYS_semsys:
2326 decode_subcall(tcp, SYS_semsys_subcall,
2327 SYS_semsys_nsubcalls, shift_style);
2328 break;
2329#if 0 /* broken */
2330 case SYS_utssys:
2331 decode_subcall(tcp, SYS_utssys_subcall,
2332 SYS_utssys_nsubcalls, shift_style);
2333 break;
2334#endif
2335 case SYS_sysfs:
2336 decode_subcall(tcp, SYS_sysfs_subcall,
2337 SYS_sysfs_nsubcalls, shift_style);
2338 break;
2339 case SYS_spcall:
2340 decode_subcall(tcp, SYS_spcall_subcall,
2341 SYS_spcall_nsubcalls, shift_style);
2342 break;
2343#ifdef SYS_context_subcall
2344 case SYS_context:
2345 decode_subcall(tcp, SYS_context_subcall,
2346 SYS_context_nsubcalls, shift_style);
2347 break;
2348#endif /* SYS_context_subcall */
Wichert Akkerman8829a551999-06-11 13:18:40 +00002349#ifdef SYS_door_subcall
2350 case SYS_door:
2351 decode_subcall(tcp, SYS_door_subcall,
2352 SYS_door_nsubcalls, door_style);
2353 break;
2354#endif /* SYS_door_subcall */
John Hughesbdf48f52001-03-06 15:08:09 +00002355#ifdef SYS_kaio_subcall
2356 case SYS_kaio:
2357 decode_subcall(tcp, SYS_kaio_subcall,
2358 SYS_kaio_nsubcalls, shift_style);
2359 break;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002360#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002361#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002362#ifdef FREEBSD
2363 case SYS_msgsys:
2364 case SYS_shmsys:
2365 case SYS_semsys:
2366 decode_subcall(tcp, 0, 0, table_style);
2367 break;
Wichert Akkerman7b3346b2001-10-09 23:47:38 +00002368#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002369#ifdef SUNOS4
2370 case SYS_semsys:
2371 decode_subcall(tcp, SYS_semsys_subcall,
2372 SYS_semsys_nsubcalls, shift_style);
2373 break;
2374 case SYS_msgsys:
2375 decode_subcall(tcp, SYS_msgsys_subcall,
2376 SYS_msgsys_nsubcalls, shift_style);
2377 break;
2378 case SYS_shmsys:
2379 decode_subcall(tcp, SYS_shmsys_subcall,
2380 SYS_shmsys_nsubcalls, shift_style);
2381 break;
2382#endif
2383 }
2384
2385 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002386 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002387 tcp->flags |= TCB_INSYSCALL;
2388 return 0;
2389 }
2390
2391 if (cflag) {
2392 gettimeofday(&tcp->etime, NULL);
2393 tcp->flags |= TCB_INSYSCALL;
2394 return 0;
2395 }
2396
2397 printleader(tcp);
2398 tcp->flags &= ~TCB_REPRINT;
2399 tcp_last = tcp;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002400 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002401 tprintf("syscall_%lu(", tcp->scno);
2402 else
2403 tprintf("%s(", sysent[tcp->scno].sys_name);
Roland McGrath761b5d72002-12-15 23:58:31 +00002404 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002405 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
2406 sys_res = printargs(tcp);
2407 else
2408 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2409 if (fflush(tcp->outf) == EOF)
2410 return -1;
2411 tcp->flags |= TCB_INSYSCALL;
2412 /* Measure the entrance time as late as possible to avoid errors. */
2413 if (dtime)
2414 gettimeofday(&tcp->etime, NULL);
2415 return sys_res;
2416}
2417
2418int
2419printargs(tcp)
2420struct tcb *tcp;
2421{
2422 if (entering(tcp)) {
2423 int i;
2424
2425 for (i = 0; i < tcp->u_nargs; i++)
2426 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2427 }
2428 return 0;
2429}
2430
2431long
2432getrval2(tcp)
2433struct tcb *tcp;
2434{
2435 long val = -1;
2436
2437#ifdef LINUX
2438#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002439 struct regs regs;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002440 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
2441 return -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002442 val = regs.r_o1;
Roland McGrath6b1d43e2003-03-31 01:05:01 +00002443#elif defined(SH)
2444 if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
2445 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002446#endif /* SPARC */
Roland McGrathb4ce1762004-03-01 20:30:48 +00002447#elif defined(IA64)
2448 if (upeek(tcp->pid, PT_R9, &val) < 0)
2449 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002450#endif /* LINUX */
2451
2452#ifdef SUNOS4
2453 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2454 return -1;
2455#endif /* SUNOS4 */
2456
2457#ifdef SVR4
2458#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002459 val = tcp->status.PR_REG[R_O1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002460#endif /* SPARC */
2461#ifdef I386
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002462 val = tcp->status.PR_REG[EDX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002463#endif /* I386 */
Michal Ludvig0e035502002-09-23 15:41:01 +00002464#ifdef X86_64
2465 val = tcp->status.PR_REG[RDX];
2466#endif /* X86_64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002467#ifdef MIPS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00002468 val = tcp->status.PR_REG[CTX_V1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002469#endif /* MIPS */
2470#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00002471#ifdef FREEBSD
2472 struct reg regs;
2473 pread(tcp->pfd_reg, &regs, sizeof(regs), 0);
2474 val = regs.r_edx;
Roland McGrath761b5d72002-12-15 23:58:31 +00002475#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00002476 return val;
2477}
2478
2479/*
2480 * Apparently, indirect system calls have already be converted by ptrace(2),
2481 * so if you see "indir" this program has gone astray.
2482 */
2483int
2484sys_indir(tcp)
2485struct tcb *tcp;
2486{
2487 int i, scno, nargs;
2488
2489 if (entering(tcp)) {
2490 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2491 fprintf(stderr, "Bogus syscall: %u\n", scno);
2492 return 0;
2493 }
2494 nargs = sysent[scno].nargs;
2495 tprintf("%s", sysent[scno].sys_name);
2496 for (i = 0; i < nargs; i++)
2497 tprintf(", %#lx", tcp->u_arg[i+1]);
2498 }
2499 return 0;
2500}
2501
2502static int
2503time_cmp(a, b)
2504void *a;
2505void *b;
2506{
2507 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
2508}
2509
2510static int
2511syscall_cmp(a, b)
2512void *a;
2513void *b;
2514{
2515 return strcmp(sysent[*((int *) a)].sys_name,
2516 sysent[*((int *) b)].sys_name);
2517}
2518
2519static int
2520count_cmp(a, b)
2521void *a;
2522void *b;
2523{
2524 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
2525
2526 return (m < n) ? 1 : (m > n) ? -1 : 0;
2527}
2528
2529static int (*sortfun)();
2530static struct timeval overhead = { -1, -1 };
2531
2532void
2533set_sortby(sortby)
2534char *sortby;
2535{
2536 if (strcmp(sortby, "time") == 0)
2537 sortfun = time_cmp;
2538 else if (strcmp(sortby, "calls") == 0)
2539 sortfun = count_cmp;
2540 else if (strcmp(sortby, "name") == 0)
2541 sortfun = syscall_cmp;
2542 else if (strcmp(sortby, "nothing") == 0)
2543 sortfun = NULL;
2544 else {
2545 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2546 exit(1);
2547 }
2548}
2549
2550void set_overhead(n)
2551int n;
2552{
2553 overhead.tv_sec = n / 1000000;
2554 overhead.tv_usec = n % 1000000;
2555}
2556
2557void
2558call_summary(outf)
2559FILE *outf;
2560{
2561 int i, j;
2562 int call_cum, error_cum;
2563 struct timeval tv_cum, dtv;
2564 double percent;
2565 char *dashes = "-------------------------";
2566 char error_str[16];
2567
2568 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2569 if (overhead.tv_sec == -1) {
2570 tv_mul(&overhead, &shortest, 8);
2571 tv_div(&overhead, &overhead, 10);
2572 }
2573 for (i = 0; i < nsyscalls; i++) {
2574 sorted_count[i] = i;
2575 if (call_count[i] == 0)
2576 continue;
2577 tv_mul(&dtv, &overhead, call_count[i]);
2578 tv_sub(&tv_count[i], &tv_count[i], &dtv);
2579 call_cum += call_count[i];
2580 error_cum += error_count[i];
2581 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
2582 }
2583 if (sortfun)
2584 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2585 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2586 "% time", "seconds", "usecs/call",
2587 "calls", "errors", "syscall");
2588 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2589 dashes, dashes, dashes, dashes, dashes, dashes);
2590 for (i = 0; i < nsyscalls; i++) {
2591 j = sorted_count[i];
2592 if (call_count[j] == 0)
2593 continue;
2594 tv_div(&dtv, &tv_count[j], call_count[j]);
2595 if (error_count[j])
2596 sprintf(error_str, "%d", error_count[j]);
2597 else
2598 error_str[0] = '\0';
2599 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
2600 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2601 percent, (long) tv_count[j].tv_sec,
2602 (long) tv_count[j].tv_usec,
2603 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2604 call_count[j], error_str, sysent[j].sys_name);
2605 }
2606 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2607 dashes, dashes, dashes, dashes, dashes, dashes);
2608 if (error_cum)
2609 sprintf(error_str, "%d", error_cum);
2610 else
2611 error_str[0] = '\0';
2612 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2613 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2614 call_cum, error_str, "total");
2615}