blob: e59b16fb210409a9056520ff12c9235eef4e2b9d [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 Akkerman9ce1a631999-08-29 23:15:07 +000046#include <asm/reg.h>
47#endif
48
Wichert Akkerman15dea971999-10-06 13:06:34 +000049#ifdef HAVE_SYS_REG_H
50#include <sys/reg.h>
51#ifndef PTRACE_PEEKUSR
52# define PTRACE_PEEKUSR PTRACE_PEEKUSER
53#endif
Wichert Akkermanfaf72222000-02-19 23:59:03 +000054#elif defined(HAVE_LINUX_PTRACE_H)
55#undef PTRACE_SYSCALL
56#include <linux/ptrace.h>
Wichert Akkerman15dea971999-10-06 13:06:34 +000057#endif
58
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +000059#if defined(LINUX) && defined(IA64)
60# include <asm/ptrace_offsets.h>
61# include <asm/rse.h>
62#endif
63
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000064#ifndef SYS_ERRLIST_DECLARED
65extern int sys_nerr;
66extern char *sys_errlist[];
67#endif /* SYS_ERRLIST_DECLARED */
68
Pavel Machekd8ae7e32000-02-01 17:17:25 +000069#define NR_SYSCALL_BASE 0
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000070#ifdef LINUX
71#ifndef ERESTARTSYS
72#define ERESTARTSYS 512
73#endif
74#ifndef ERESTARTNOINTR
75#define ERESTARTNOINTR 513
76#endif
77#ifndef ERESTARTNOHAND
78#define ERESTARTNOHAND 514 /* restart if no handler.. */
79#endif
80#ifndef ENOIOCTLCMD
81#define ENOIOCTLCMD 515 /* No ioctl command */
82#endif
83#ifndef NSIG
84#define NSIG 32
85#endif
86#ifdef ARM
87#undef NSIG
88#define NSIG 32
Pavel Machekd8ae7e32000-02-01 17:17:25 +000089#undef NR_SYSCALL_BASE
90#define NR_SYSCALL_BASE __NR_SYSCALL_BASE
Wichert Akkerman76baf7c1999-02-19 00:21:36 +000091#endif
92#endif /* LINUX */
93
94#include "syscall.h"
95
96/* Define these shorthand notations to simplify the syscallent files. */
97#define TF TRACE_FILE
98#define TI TRACE_IPC
99#define TN TRACE_NETWORK
100#define TP TRACE_PROCESS
101#define TS TRACE_SIGNAL
102
103struct sysent sysent0[] = {
104#include "syscallent.h"
105};
106int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
107
108#if SUPPORTED_PERSONALITIES >= 2
109struct sysent sysent1[] = {
110#include "syscallent1.h"
111};
112int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
113#endif /* SUPPORTED_PERSONALITIES >= 2 */
114
115#if SUPPORTED_PERSONALITIES >= 3
116struct sysent sysent2[] = {
117#include "syscallent2.h"
118};
119int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
120#endif /* SUPPORTED_PERSONALITIES >= 3 */
121
122struct sysent *sysent;
123int nsyscalls;
124
125/* Now undef them since short defines cause wicked namespace pollution. */
126#undef TF
127#undef TI
128#undef TN
129#undef TP
130#undef TS
131
132char *errnoent0[] = {
133#include "errnoent.h"
134};
135int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
136
137#if SUPPORTED_PERSONALITIES >= 2
138char *errnoent1[] = {
139#include "errnoent1.h"
140};
141int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
142#endif /* SUPPORTED_PERSONALITIES >= 2 */
143
144#if SUPPORTED_PERSONALITIES >= 3
145char *errnoent2[] = {
146#include "errnoent2.h"
147};
148int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
149#endif /* SUPPORTED_PERSONALITIES >= 3 */
150
151char **errnoent;
152int nerrnos;
153
154int current_personality;
155
156int
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000157set_personality(personality)
158int personality;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000159{
160 switch (personality) {
161 case 0:
162 errnoent = errnoent0;
163 nerrnos = nerrnos0;
164 sysent = sysent0;
165 nsyscalls = nsyscalls0;
166 ioctlent = ioctlent0;
167 nioctlents = nioctlents0;
168 signalent = signalent0;
169 nsignals = nsignals0;
170 break;
171
172#if SUPPORTED_PERSONALITIES >= 2
173 case 1:
174 errnoent = errnoent1;
175 nerrnos = nerrnos1;
176 sysent = sysent1;
177 nsyscalls = nsyscalls1;
178 ioctlent = ioctlent1;
179 nioctlents = nioctlents1;
180 signalent = signalent1;
181 nsignals = nsignals1;
182 break;
183#endif /* SUPPORTED_PERSONALITIES >= 2 */
184
185#if SUPPORTED_PERSONALITIES >= 3
186 case 2:
187 errnoent = errnoent2;
188 nerrnos = nerrnos2;
189 sysent = sysent2;
190 nsyscalls = nsyscalls2;
191 ioctlent = ioctlent2;
192 nioctlents = nioctlents2;
193 signalent = signalent2;
194 nsignals = nsignals2;
195 break;
196#endif /* SUPPORTED_PERSONALITIES >= 3 */
197
198 default:
199 return -1;
200 }
201
202 current_personality = personality;
203 return 0;
204}
205
206int qual_flags[MAX_QUALS];
207
208static int call_count[MAX_QUALS];
209static int error_count[MAX_QUALS];
210static struct timeval tv_count[MAX_QUALS];
211static int sorted_count[MAX_QUALS];
212
213static struct timeval shortest = { 1000000, 0 };
214
215static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc();
216
217static struct qual_options {
218 int bitflag;
219 char *option_name;
220 int (*lookup)();
221 char *argument_name;
222} qual_options[] = {
223 { QUAL_TRACE, "trace", lookup_syscall, "system call" },
224 { QUAL_TRACE, "t", lookup_syscall, "system call" },
225 { QUAL_ABBREV, "abbrev", lookup_syscall, "system call" },
226 { QUAL_ABBREV, "a", lookup_syscall, "system call" },
227 { QUAL_VERBOSE, "verbose", lookup_syscall, "system call" },
228 { QUAL_VERBOSE, "v", lookup_syscall, "system call" },
229 { QUAL_RAW, "raw", lookup_syscall, "system call" },
230 { QUAL_RAW, "x", lookup_syscall, "system call" },
231 { QUAL_SIGNAL, "signal", lookup_signal, "signal" },
232 { QUAL_SIGNAL, "signals", lookup_signal, "signal" },
233 { QUAL_SIGNAL, "s", lookup_signal, "signal" },
234 { QUAL_FAULT, "fault", lookup_fault, "fault" },
235 { QUAL_FAULT, "faults", lookup_fault, "fault" },
236 { QUAL_FAULT, "m", lookup_fault, "fault" },
237 { QUAL_READ, "read", lookup_desc, "descriptor" },
238 { QUAL_READ, "reads", lookup_desc, "descriptor" },
239 { QUAL_READ, "r", lookup_desc, "descriptor" },
240 { QUAL_WRITE, "write", lookup_desc, "descriptor" },
241 { QUAL_WRITE, "writes", lookup_desc, "descriptor" },
242 { QUAL_WRITE, "w", lookup_desc, "descriptor" },
243 { 0, NULL, NULL, NULL },
244};
245
246static int
247lookup_syscall(s)
248char *s;
249{
250 int i;
251
252 for (i = 0; i < nsyscalls; i++) {
253 if (strcmp(s, sysent[i].sys_name) == 0)
254 return i;
255 }
256 return -1;
257}
258
259static int
260lookup_signal(s)
261char *s;
262{
263 int i;
264 char buf[32];
265
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000266 if (s && *s && isdigit((unsigned char)*s))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000267 return atoi(s);
268 strcpy(buf, s);
269 s = buf;
270 for (i = 0; s[i]; i++)
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000271 s[i] = toupper((unsigned char)(s[i]));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000272 if (strncmp(s, "SIG", 3) == 0)
273 s += 3;
274 for (i = 0; i <= NSIG; i++) {
Nate Sammonsce780fc1999-03-29 23:23:13 +0000275 if (strcmp(s, signame(i) + 3) == 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000276 return i;
277 }
278 return -1;
279}
280
281static int
282lookup_fault(s)
283char *s;
284{
285 return -1;
286}
287
288static int
289lookup_desc(s)
290char *s;
291{
Wichert Akkerman2ee6e452000-02-18 15:36:12 +0000292 if (s && *s && isdigit((unsigned char)*s))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000293 return atoi(s);
294 return -1;
295}
296
297static int
298lookup_class(s)
299char *s;
300{
301 if (strcmp(s, "file") == 0)
302 return TRACE_FILE;
303 if (strcmp(s, "ipc") == 0)
304 return TRACE_IPC;
305 if (strcmp(s, "network") == 0)
306 return TRACE_NETWORK;
307 if (strcmp(s, "process") == 0)
308 return TRACE_PROCESS;
309 if (strcmp(s, "signal") == 0)
310 return TRACE_SIGNAL;
311 return -1;
312}
313
314void
315qualify(s)
316char *s;
317{
318 struct qual_options *opt;
319 int not;
320 char *p;
321 int i, n;
322
323 opt = &qual_options[0];
324 for (i = 0; (p = qual_options[i].option_name); i++) {
325 n = strlen(p);
326 if (strncmp(s, p, n) == 0 && s[n] == '=') {
327 opt = &qual_options[i];
328 s += n + 1;
329 break;
330 }
331 }
332 not = 0;
333 if (*s == '!') {
334 not = 1;
335 s++;
336 }
337 if (strcmp(s, "none") == 0) {
338 not = 1 - not;
339 s = "all";
340 }
341 if (strcmp(s, "all") == 0) {
342 for (i = 0; i < MAX_QUALS; i++) {
343 if (not)
344 qual_flags[i] &= ~opt->bitflag;
345 else
346 qual_flags[i] |= opt->bitflag;
347 }
348 return;
349 }
350 for (i = 0; i < MAX_QUALS; i++) {
351 if (not)
352 qual_flags[i] |= opt->bitflag;
353 else
354 qual_flags[i] &= ~opt->bitflag;
355 }
356 for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
357 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
358 for (i = 0; i < MAX_QUALS; i++) {
359 if (sysent[i].sys_flags & n) {
360 if (not)
361 qual_flags[i] &= ~opt->bitflag;
362 else
363 qual_flags[i] |= opt->bitflag;
364 }
365 }
366 continue;
367 }
368 if ((n = (*opt->lookup)(p)) < 0) {
369 fprintf(stderr, "strace: invalid %s `%s'\n",
370 opt->argument_name, p);
371 exit(1);
372 }
373 if (not)
374 qual_flags[n] &= ~opt->bitflag;
375 else
376 qual_flags[n] |= opt->bitflag;
377 }
378 return;
379}
380
381static void
382dumpio(tcp)
383struct tcb *tcp;
384{
385 if (syserror(tcp))
386 return;
387 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
388 return;
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000389 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000390 case SYS_read:
391#ifdef SYS_recv
392 case SYS_recv:
393#endif
394#ifdef SYS_recvfrom
395 case SYS_recvfrom:
396#endif
397 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
398 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
399 break;
400 case SYS_write:
401#ifdef SYS_send
402 case SYS_send:
403#endif
404#ifdef SYS_sendto
405 case SYS_sendto:
406#endif
407 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
408 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
409 break;
410 }
411}
412
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000413#ifndef FREEBSD
Wichert Akkerman8829a551999-06-11 13:18:40 +0000414enum subcall_style { shift_style, deref_style, mask_style, door_style };
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000415#else /* FREEBSD */
416enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
417
418struct subcall {
419 int call;
420 int nsubcalls;
421 int subcalls[5];
422};
423
424const struct subcall subcalls_table[] = {
425 { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
426 { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
427 { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
428};
429#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000430
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000431#if !(defined(LINUX) && ( defined(ALPHA) || defined(IA64) || defined(MIPS) ))
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000432
433const int socket_map [] = {
434 /* SYS_SOCKET */ 97,
435 /* SYS_BIND */ 104,
436 /* SYS_CONNECT */ 98,
437 /* SYS_LISTEN */ 106,
438 /* SYS_ACCEPT */ 99,
439 /* SYS_GETSOCKNAME */ 150,
440 /* SYS_GETPEERNAME */ 141,
441 /* SYS_SOCKETPAIR */ 135,
442 /* SYS_SEND */ 101,
443 /* SYS_RECV */ 102,
444 /* SYS_SENDTO */ 133,
445 /* SYS_RECVFROM */ 125,
446 /* SYS_SHUTDOWN */ 134,
447 /* SYS_SETSOCKOPT */ 105,
448 /* SYS_GETSOCKOPT */ 118,
449 /* SYS_SENDMSG */ 114,
450 /* SYS_RECVMSG */ 113
451};
452
453void
Wichert Akkermane6f876c1999-06-22 15:28:30 +0000454sparc_socket_decode (tcp)
455struct tcb *tcp;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000456{
457 volatile long addr;
458 volatile int i, n;
459
460 if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
461 return;
462 }
463 tcp->scno = socket_map [tcp->u_arg [0]-1];
464 n = tcp->u_nargs = sysent [tcp->scno].nargs;
465 addr = tcp->u_arg [1];
466 for (i = 0; i < n; i++){
467 int arg;
468 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
469 arg = 0;
470 tcp->u_arg [i] = arg;
471 addr += sizeof (arg);
472 }
473}
474
475static void
476decode_subcall(tcp, subcall, nsubcalls, style)
477struct tcb *tcp;
478int subcall;
479int nsubcalls;
480enum subcall_style style;
481{
482 int i, addr, mask, arg;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000483
484#ifndef FREEBSD
485 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
486 return;
487#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000488 switch (style) {
489 case shift_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000490 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
491 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000492 tcp->scno = subcall + tcp->u_arg[0];
493 if (sysent[tcp->scno].nargs != -1)
494 tcp->u_nargs = sysent[tcp->scno].nargs;
495 else
496 tcp->u_nargs--;
497 for (i = 0; i < tcp->u_nargs; i++)
498 tcp->u_arg[i] = tcp->u_arg[i + 1];
499 break;
500 case deref_style:
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000501 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
502 return;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000503 tcp->scno = subcall + tcp->u_arg[0];
504 addr = tcp->u_arg[1];
505 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
506 if (umove(tcp, addr, &arg) < 0)
507 arg = 0;
508 tcp->u_arg[i] = arg;
509 addr += sizeof(arg);
510 }
511 tcp->u_nargs = sysent[tcp->scno].nargs;
512 break;
513 case mask_style:
514 mask = (tcp->u_arg[0] >> 8) & 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000515 for (i = 0; mask; i++)
516 mask >>= 1;
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000517 if (i >= nsubcalls)
518 return;
519 tcp->u_arg[0] &= 0xff;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000520 tcp->scno = subcall + i;
521 if (sysent[tcp->scno].nargs != -1)
522 tcp->u_nargs = sysent[tcp->scno].nargs;
523 break;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000524 case door_style:
525 /*
526 * Oh, yuck. The call code is the *sixth* argument.
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000527 * (don't you mean the *last* argument? - JH)
Wichert Akkerman8829a551999-06-11 13:18:40 +0000528 */
Wichert Akkerman16a03d22000-08-10 02:14:04 +0000529 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
530 return;
Wichert Akkerman8829a551999-06-11 13:18:40 +0000531 tcp->scno = subcall + tcp->u_arg[5];
532 if (sysent[tcp->scno].nargs != -1)
533 tcp->u_nargs = sysent[tcp->scno].nargs;
534 else
535 tcp->u_nargs--;
536 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000537#ifdef FREEBSD
538 case table_style:
539 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
540 if (subcalls_table[i].call == tcp->scno) break;
541 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
542 tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
543 tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
544 for (i = 0; i < tcp->u_nargs; i++)
545 tcp->u_arg[i] = tcp->u_arg[i + 1];
546 }
547 break;
548#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000549 }
550}
551#endif
552
553struct tcb *tcp_last = NULL;
554
555static int
556internal_syscall(tcp)
557struct tcb *tcp;
558{
559 /*
560 * We must always trace a few critical system calls in order to
561 * correctly support following forks in the presence of tracing
562 * qualifiers.
563 */
Pavel Machekd8ae7e32000-02-01 17:17:25 +0000564 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000565#ifdef SYS_fork
566 case SYS_fork:
567#endif
568#ifdef SYS_vfork
569 case SYS_vfork:
570#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000571 internal_fork(tcp);
572 break;
Wichert Akkerman7a0b6491999-12-23 15:08:17 +0000573#ifdef SYS_clone
574 case SYS_clone:
575 internal_clone(tcp);
576 break;
577#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000578#ifdef SYS_execv
579 case SYS_execv:
580#endif
581#ifdef SYS_execve
582 case SYS_execve:
583#endif
584 internal_exec(tcp);
585 break;
586
587#ifdef SYS_wait
588 case SYS_wait:
589#endif
590#ifdef SYS_wait4
591 case SYS_wait4:
592#endif
593#ifdef SYS_waitpid
594 case SYS_waitpid:
595#endif
596#ifdef SYS_waitsys
597 case SYS_waitsys:
598#endif
599 internal_wait(tcp);
600 break;
601
602#ifdef SYS_exit
603 case SYS_exit:
604#endif
605 internal_exit(tcp);
606 break;
607 }
608 return 0;
609}
610
Wichert Akkermanc7926982000-04-10 22:22:31 +0000611
612#ifdef LINUX
613#if defined (I386)
614 static long eax;
615#elif defined (IA64)
616 long r8, r10, psr;
617 long ia32 = 0;
618#elif defined (POWERPC)
619 static long result,flags;
620#elif defined (M68K)
621 static int d0;
622#elif defined (ARM)
623 static int r0;
624#elif defined (ALPHA)
625 static long r0;
626 static long a3;
627#elif defined (SPARC)
628 static struct pt_regs regs;
629 static unsigned long trap;
630#elif defined(MIPS)
631 static long a3;
632 static long r2;
633#elif defined(S390)
634 static long gpr2;
635 static long pc;
636#endif
637#endif /* LINUX */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000638#ifdef FREEBSD
639 struct reg regs;
640#endif /* FREEBSD */
Wichert Akkermanc7926982000-04-10 22:22:31 +0000641
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000642int
Pavel Machek4dc3b142000-02-01 17:58:41 +0000643get_scno(tcp)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000644struct tcb *tcp;
645{
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000646 long scno = 0;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000647#ifndef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000648 int pid = tcp->pid;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000649#endif /* !PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000650
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000651#ifdef LINUX
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +0000652#if defined(S390)
653 if (upeek(tcp->pid,PT_PSWADDR,&pc) < 0)
654 return -1;
655 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-4),0);
656 if (errno)
657 return -1;
658 scno&=0xFF;
659#elif defined (POWERPC)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000660 if (upeek(pid, 4*PT_R0, &scno) < 0)
661 return -1;
662 if (!(tcp->flags & TCB_INSYSCALL)) {
663 /* Check if we return from execve. */
664 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
665 tcp->flags &= ~TCB_WAITEXECVE;
666 return 0;
667 }
668 }
669#elif defined (I386)
670 if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
671 return -1;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000672#elif defined(IA64)
673#define IA64_PSR_IS ((long)1 << 34)
674 if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
675 ia32 = (psr & IA64_PSR_IS);
676 if (!(tcp->flags & TCB_INSYSCALL)) {
677 if (ia32) {
678 if (upeek(pid, PT_R8, &scno) < 0)
679 return -1;
680 } else {
681 if (upeek (pid, PT_R15, &scno) < 0)
682 return -1;
683 }
684 } else {
685 /* syscall in progress */
686 if (upeek (pid, PT_R8, &r8) < 0)
687 return -1;
688 if (upeek (pid, PT_R10, &r10) < 0)
689 return -1;
690 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000691#elif defined (ARM)
692 {
693 long pc;
694 upeek(pid, 4*15, &pc);
695 umoven(tcp, pc-4, 4, (char *)&scno);
696 scno &= 0x000fffff;
697 }
698#elif defined (M68K)
699 if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
700 return -1;
Wichert Akkermanf90da011999-10-31 21:15:38 +0000701#elif defined (MIPS)
702 if (upeek(pid, REG_A3, &a3) < 0)
703 return -1;
704
705 if(!(tcp->flags & TCB_INSYSCALL)) {
706 if (upeek(pid, REG_V0, &scno) < 0)
707 return -1;
708
709 if (scno < 0 || scno > nsyscalls) {
710 if(a3 == 0 || a3 == -1) {
711 if(debug)
712 fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
713 return 0;
714 }
715 }
716 } else {
717 if (upeek(pid, REG_V0, &r2) < 0)
718 return -1;
719 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000720#elif defined (ALPHA)
721 if (upeek(pid, REG_A3, &a3) < 0)
722 return -1;
723
724 if (!(tcp->flags & TCB_INSYSCALL)) {
725 if (upeek(pid, REG_R0, &scno) < 0)
726 return -1;
727
728 /* Check if we return from execve. */
729 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
730 tcp->flags &= ~TCB_WAITEXECVE;
731 return 0;
732 }
733
734 /*
735 * Do some sanity checks to figure out if it's
736 * really a syscall entry
737 */
738 if (scno < 0 || scno > nsyscalls) {
739 if (a3 == 0 || a3 == -1) {
740 if (debug)
741 fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
742 return 0;
743 }
744 }
745 }
746 else {
747 if (upeek(pid, REG_R0, &r0) < 0)
748 return -1;
749 }
750#elif defined (SPARC)
751 /* Everything we need is in the current register set. */
752 if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
753 return -1;
754
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000755 /* If we are entering, then disassemble the syscall trap. */
756 if (!(tcp->flags & TCB_INSYSCALL)) {
757 /* Retrieve the syscall trap instruction. */
758 errno = 0;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000759 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000760 if (errno)
761 return -1;
762
763 /* Disassemble the trap to see what personality to use. */
764 switch (trap) {
765 case 0x91d02010:
766 /* Linux/SPARC syscall trap. */
767 set_personality(0);
768 break;
Wichert Akkermandacfb6e1999-06-03 14:21:07 +0000769 case 0x91d0206d:
770 /* Linux/SPARC64 syscall trap. */
771 fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
772 return -1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000773 case 0x91d02000:
774 /* SunOS syscall trap. (pers 1) */
775 fprintf(stderr,"syscall: SunOS no support\n");
776 return -1;
777 case 0x91d02008:
778 /* Solaris 2.x syscall trap. (per 2) */
779 set_personality(1);
780 break;
781 case 0x91d02009:
782 /* NetBSD/FreeBSD syscall trap. */
783 fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
784 return -1;
785 case 0x91d02027:
786 /* Solaris 2.x gettimeofday */
787 set_personality(1);
788 break;
789 default:
790 /* Unknown syscall trap. */
791 if(tcp->flags & TCB_WAITEXECVE) {
792 tcp->flags &= ~TCB_WAITEXECVE;
793 return 0;
794 }
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000795 fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000796 return -1;
797 }
798
799 /* Extract the system call number from the registers. */
800 if (trap == 0x91d02027)
801 scno = 156;
802 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000803 scno = regs.r_g1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000804 if (scno == 0) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000805 scno = regs.r_o0;
806 memmove (&regs.r_o0, &regs.r_o1, 7*sizeof(regs.r_o0));
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000807 }
808 }
809#endif
810#endif /* LINUX */
811#ifdef SUNOS4
812 if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
813 return -1;
814#endif
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000815#ifdef USE_PROCFS
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000816#ifdef HAVE_PR_SYSCALL
817 scno = tcp->status.pr_syscall;
818#else /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000819#ifndef FREEBSD
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000820 scno = tcp->status.PR_WHAT;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000821#else /* FREEBSD */
822 if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
823 perror("pread");
824 return -1;
825 }
826 switch (regs.r_eax) {
827 case SYS_syscall:
828 case SYS___syscall:
829 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
830 break;
831 default:
832 scno = regs.r_eax;
833 break;
834 }
835#endif /* FREEBSD */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000836#endif /* !HAVE_PR_SYSCALL */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000837#endif /* USE_PROCFS */
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +0000838 if (!(tcp->flags & TCB_INSYSCALL))
839 tcp->scno = scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +0000840 return 1;
841}
842
Pavel Machek4dc3b142000-02-01 17:58:41 +0000843
844int
845syscall_fixup(tcp)
846struct tcb *tcp;
847{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000848#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +0000849 int pid = tcp->pid;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000850#else /* USE_PROCFS */
851 int scno = tcp->scno;
Pavel Machek4dc3b142000-02-01 17:58:41 +0000852
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000853 if (!(tcp->flags & TCB_INSYSCALL)) {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000854 if (tcp->status.PR_WHY != PR_SYSENTRY) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000855 if (
856 scno == SYS_fork
857#ifdef SYS_vfork
858 || scno == SYS_vfork
859#endif /* SYS_vfork */
860 ) {
861 /* We are returning in the child, fake it. */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000862 tcp->status.PR_WHY = PR_SYSENTRY;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000863 trace_syscall(tcp);
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000864 tcp->status.PR_WHY = PR_SYSEXIT;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000865 }
866 else {
867 fprintf(stderr, "syscall: missing entry\n");
868 tcp->flags |= TCB_INSYSCALL;
869 }
870 }
871 }
872 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +0000873 if (tcp->status.PR_WHY != PR_SYSEXIT) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000874 fprintf(stderr, "syscall: missing exit\n");
875 tcp->flags &= ~TCB_INSYSCALL;
876 }
877 }
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +0000878#endif /* USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000879#ifdef SUNOS4
880 if (!(tcp->flags & TCB_INSYSCALL)) {
881 if (scno == 0) {
882 fprintf(stderr, "syscall: missing entry\n");
883 tcp->flags |= TCB_INSYSCALL;
884 }
885 }
886 else {
887 if (scno != 0) {
888 if (debug) {
889 /*
890 * This happens when a signal handler
891 * for a signal which interrupted a
892 * a system call makes another system call.
893 */
894 fprintf(stderr, "syscall: missing exit\n");
895 }
896 tcp->flags &= ~TCB_INSYSCALL;
897 }
898 }
899#endif /* SUNOS4 */
900#ifdef LINUX
901#if defined (I386)
902 if (upeek(pid, 4*EAX, &eax) < 0)
903 return -1;
904 if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
905 if (debug)
906 fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
907 return 0;
908 }
Wichert Akkerman12f75d12000-02-14 16:23:40 +0000909#elif defined (S390)
910 if (upeek(pid, PT_GPR2, &gpr2) < 0)
911 return -1;
912 if (gpr2 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
913 if (debug)
914 fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
915 return 0;
916 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000917#elif defined (POWERPC)
918# define SO_MASK 0x10000000
919 if (upeek(pid, 4*PT_CCR, &flags) < 0)
920 return -1;
921 if (upeek(pid, 4*PT_R3, &result) < 0)
922 return -1;
923 if (flags & SO_MASK)
924 result = -result;
925#elif defined (M68K)
926 if (upeek(pid, 4*PT_D0, &d0) < 0)
927 return -1;
928 if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
929 if (debug)
930 fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
931 return 0;
932 }
933#elif defined (ARM)
934 if (upeek(pid, 4*0, (long *)&r0) < 0)
935 return -1;
936 if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
937 if (debug)
938 fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
939 return 0;
940 }
941#else
942#endif
943#endif /* LINUX */
Pavel Machek4dc3b142000-02-01 17:58:41 +0000944 return 1;
945}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000946
Pavel Machek4dc3b142000-02-01 17:58:41 +0000947int
948get_error(tcp)
949struct tcb *tcp;
950{
951 int u_error = 0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000952#ifdef LINUX
Wichert Akkerman12f75d12000-02-14 16:23:40 +0000953#ifdef S390
954 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
955 tcp->u_rval = -1;
956 u_error = -gpr2;
957 }
958 else {
959 tcp->u_rval = gpr2;
960 u_error = 0;
961 }
962#else /* !S390 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +0000963#ifdef I386
964 if (eax < 0 && -eax < nerrnos) {
965 tcp->u_rval = -1;
966 u_error = -eax;
967 }
968 else {
969 tcp->u_rval = eax;
970 u_error = 0;
971 }
972#else /* !I386 */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +0000973#ifdef IA64
974 if (ia32) {
975 int err;
976
977 err = (int)r8;
978 if (err < 0 && -err < nerrnos) {
979 tcp->u_rval = -1;
980 u_error = -err;
981 }
982 else {
983 tcp->u_rval = err;
984 u_error = 0;
985 }
986 } else {
987 if (r10) {
988 tcp->u_rval = -1;
989 u_error = r8;
990 } else {
991 tcp->u_rval = r8;
992 u_error = 0;
993 }
994 }
995#else /* !IA64 */
Wichert Akkermanf90da011999-10-31 21:15:38 +0000996#ifdef MIPS
997 if (a3) {
998 tcp->u_rval = -1;
999 u_error = r2;
1000 } else {
1001 tcp->u_rval = r2;
1002 u_error = 0;
1003 }
1004#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001005#ifdef POWERPC
1006 if (result && (unsigned) -result < nerrnos) {
1007 tcp->u_rval = -1;
1008 u_error = -result;
1009 }
1010 else {
1011 tcp->u_rval = result;
1012 u_error = 0;
1013 }
1014#else /* !POWERPC */
1015#ifdef M68K
1016 if (d0 && (unsigned) -d0 < nerrnos) {
1017 tcp->u_rval = -1;
1018 u_error = -d0;
1019 }
1020 else {
1021 tcp->u_rval = d0;
1022 u_error = 0;
1023 }
1024#else /* !M68K */
1025#ifdef ARM
1026 if (r0 && (unsigned) -r0 < nerrnos) {
1027 tcp->u_rval = -1;
1028 u_error = -r0;
1029 }
1030 else {
1031 tcp->u_rval = r0;
1032 u_error = 0;
1033 }
1034#else /* !ARM */
1035#ifdef ALPHA
1036 if (a3) {
1037 tcp->u_rval = -1;
1038 u_error = r0;
1039 }
1040 else {
1041 tcp->u_rval = r0;
1042 u_error = 0;
1043 }
1044#else /* !ALPHA */
1045#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001046 if (regs.r_psr & PSR_C) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001047 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001048 u_error = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001049 }
1050 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001051 tcp->u_rval = regs.r_o0;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001052 u_error = 0;
1053 }
1054#endif /* SPARC */
1055#endif /* ALPHA */
1056#endif /* ARM */
1057#endif /* M68K */
1058#endif /* POWERPC */
Wichert Akkermanf90da011999-10-31 21:15:38 +00001059#endif /* MIPS */
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001060#endif /* IA64 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001061#endif /* I386 */
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001062#endif /* S390 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001063#endif /* LINUX */
1064#ifdef SUNOS4
1065 /* get error code from user struct */
1066 if (upeek(pid, uoff(u_error), &u_error) < 0)
1067 return -1;
1068 u_error >>= 24; /* u_error is a char */
1069
1070 /* get system call return value */
1071 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1072 return -1;
1073#endif /* SUNOS4 */
1074#ifdef SVR4
1075#ifdef SPARC
1076 /* Judicious guessing goes a long way. */
1077 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1078 tcp->u_rval = -1;
1079 u_error = tcp->status.pr_reg[R_O0];
1080 }
1081 else {
1082 tcp->u_rval = tcp->status.pr_reg[R_O0];
1083 u_error = 0;
1084 }
1085#endif /* SPARC */
1086#ifdef I386
1087 /* Wanna know how to kill an hour single-stepping? */
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001088 if (tcp->status.PR_REG[EFL] & 0x1) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001089 tcp->u_rval = -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001090 u_error = tcp->status.PR_REG[EAX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001091 }
1092 else {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001093 tcp->u_rval = tcp->status.PR_REG[EAX];
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001094#ifdef HAVE_LONG_LONG
1095 tcp->u_lrval =
1096 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1097 tcp->status.PR_REG[EAX];
1098#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001099 u_error = 0;
1100 }
1101#endif /* I386 */
1102#ifdef MIPS
1103 if (tcp->status.pr_reg[CTX_A3]) {
1104 tcp->u_rval = -1;
1105 u_error = tcp->status.pr_reg[CTX_V0];
1106 }
1107 else {
1108 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1109 u_error = 0;
1110 }
1111#endif /* MIPS */
1112#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001113#ifdef FREEBSD
1114 if (regs.r_eflags & PSL_C) {
1115 tcp->u_rval = -1;
1116 u_error = regs.r_eax;
1117 } else {
1118 tcp->u_rval = regs.r_eax;
1119 tcp->u_lrval =
1120 ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
1121 u_error = 0;
1122 }
1123#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001124 tcp->u_error = u_error;
1125 return 1;
1126}
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001127
Pavel Machek4dc3b142000-02-01 17:58:41 +00001128int syscall_enter(tcp)
1129struct tcb *tcp;
1130{
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001131#ifndef USE_PROCFS
Pavel Machek4dc3b142000-02-01 17:58:41 +00001132 int pid = tcp->pid;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001133#endif /* !USE_PROCFS */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001134#ifdef LINUX
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001135#if defined(S390)
1136 {
1137 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001138 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1139 tcp->u_nargs = sysent[tcp->scno].nargs;
1140 else
1141 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman4dc8a2a1999-12-23 14:20:14 +00001142 for (i = 0; i < tcp->u_nargs; i++) {
1143 if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+(i<<2), &tcp->u_arg[i]) < 0)
1144 return -1;
1145 }
1146 }
1147#elif defined (ALPHA)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001148 {
1149 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001150 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1151 tcp->u_nargs = sysent[tcp->scno].nargs;
1152 else
1153 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001154 for (i = 0; i < tcp->u_nargs; i++) {
Wichert Akkermanb859bea1999-04-18 22:50:50 +00001155 /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1156 * for scno somewhere above here!
1157 */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001158 if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1159 return -1;
1160 }
1161 }
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001162#elif defined (IA64)
1163 {
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +00001164 unsigned long *bsp, cfm, i;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001165
1166 if (upeek(pid, PT_AR_BSP, (long *) &bsp) < 0)
1167 return -1;
Wichert Akkerman5ae21ea2000-05-01 01:53:59 +00001168 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1169 return -1;
1170
1171 bsp = ia64_rse_skip_regs(bsp, -(cfm & 0x7f));
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001172
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001173 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1174 tcp->u_nargs = sysent[tcp->scno].nargs;
1175 else
1176 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001177 for (i = 0; i < tcp->u_nargs; ++i) {
1178 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(bsp, i), sizeof(long),
1179 (char *) &tcp->u_arg[i])
1180 < 0)
1181 return -1;
1182 }
1183 }
Wichert Akkermanf90da011999-10-31 21:15:38 +00001184#elif defined (MIPS)
1185 {
1186 long sp;
1187 int i, nargs;
1188
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001189 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1190 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1191 else
1192 nargs = tcp->u_nargs = MAX_ARGS;
Wichert Akkermanf90da011999-10-31 21:15:38 +00001193 if(nargs > 4) {
1194 if(upeek(pid, REG_SP, &sp) < 0)
1195 return -1;
1196 for(i = 0; i < 4; i++) {
1197 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1198 return -1;
1199 }
1200 umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1201 (char *)(tcp->u_arg + 4));
1202 } else {
1203 for(i = 0; i < nargs; i++) {
1204 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1205 return -1;
1206 }
1207 }
1208 }
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001209#elif defined (POWERPC)
1210 {
1211 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001212 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1213 tcp->u_nargs = sysent[tcp->scno].nargs;
1214 else
1215 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001216 for (i = 0; i < tcp->u_nargs; i++) {
1217 if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1218 return -1;
1219 }
1220 }
1221#elif defined (SPARC)
1222 {
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001223 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001224
1225 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1226 tcp->u_nargs = sysent[tcp->scno].nargs;
1227 else
1228 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001229 for (i = 0; i < tcp->u_nargs; i++)
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001230 tcp->u_arg[i] = *((&regs.r_o0) + i);
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001231 }
Wichert Akkermanfaf72222000-02-19 23:59:03 +00001232#else /* Other architecture (like i386) (32bits specific) */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001233 {
1234 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001235 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1236 tcp->u_nargs = sysent[tcp->scno].nargs;
1237 else
1238 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001239 for (i = 0; i < tcp->u_nargs; i++) {
1240 if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1241 return -1;
1242 }
1243 }
1244#endif
1245#endif /* LINUX */
1246#ifdef SUNOS4
1247 {
1248 int i;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001249 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1250 tcp->u_nargs = sysent[tcp->scno].nargs;
1251 else
1252 tcp->u_nargs = MAX_ARGS;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001253 for (i = 0; i < tcp->u_nargs; i++) {
1254 struct user *u;
1255
1256 if (upeek(pid, uoff(u_arg[0]) +
1257 (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1258 return -1;
1259 }
1260 }
1261#endif /* SUNOS4 */
1262#ifdef SVR4
1263#ifdef MIPS
1264 /*
1265 * SGI is broken: even though it has pr_sysarg, it doesn't
1266 * set them on system call entry. Get a clue.
1267 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001268 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001269 tcp->u_nargs = sysent[tcp->scno].nargs;
1270 else
1271 tcp->u_nargs = tcp->status.pr_nsysarg;
1272 if (tcp->u_nargs > 4) {
1273 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1274 4*sizeof(tcp->u_arg[0]));
1275 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1276 (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1277 }
1278 else {
1279 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1280 tcp->u_nargs*sizeof(tcp->u_arg[0]));
1281 }
1282#else /* !MIPS */
1283#ifdef HAVE_PR_SYSCALL
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001284 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001285 tcp->u_nargs = sysent[tcp->scno].nargs;
1286 else
1287 tcp->u_nargs = tcp->status.pr_nsysarg;
1288 {
1289 int i;
1290 for (i = 0; i < tcp->u_nargs; i++)
1291 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1292 }
1293#else /* !HAVE_PR_SYSCALL */
1294#ifdef I386
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001295 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001296 tcp->u_nargs = sysent[tcp->scno].nargs;
1297 else
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001298#if UNIXWARE >= 2
1299 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1300#else
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001301 tcp->u_nargs = 5;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001302#endif
1303 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001304 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1305#endif /* I386 */
1306#endif /* !HAVE_PR_SYSCALL */
1307#endif /* !MIPS */
1308#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001309#ifdef FREEBSD
1310 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1311 sysent[tcp->scno].nargs > tcp->status.val)
1312 tcp->u_nargs = sysent[tcp->scno].nargs;
1313 else
1314 tcp->u_nargs = tcp->status.val;
1315 if (tcp->u_nargs < 0)
1316 tcp->u_nargs = 0;
1317 if (tcp->u_nargs > MAX_ARGS)
1318 tcp->u_nargs = MAX_ARGS;
1319 switch(regs.r_eax) {
1320 case SYS___syscall:
1321 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1322 regs.r_esp + sizeof(int) + sizeof(quad_t));
1323 break;
1324 case SYS_syscall:
1325 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1326 regs.r_esp + 2 * sizeof(int));
1327 break;
1328 default:
1329 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1330 regs.r_esp + sizeof(int));
1331 break;
1332 }
1333#endif /* FREEBSD */
Pavel Machek4dc3b142000-02-01 17:58:41 +00001334 return 1;
1335}
1336
1337int
1338trace_syscall(tcp)
1339struct tcb *tcp;
1340{
1341 int sys_res;
1342 struct timeval tv;
1343 int res;
1344
1345 /* Measure the exit time as early as possible to avoid errors. */
1346 if (dtime && (tcp->flags & TCB_INSYSCALL))
1347 gettimeofday(&tv, NULL);
1348
1349 res = get_scno(tcp);
1350 if (res != 1)
1351 return res;
1352
1353 res = syscall_fixup(tcp);
1354 if (res != 1)
1355 return res;
1356
1357 if (tcp->flags & TCB_INSYSCALL) {
1358 long u_error;
1359 res = get_error(tcp);
1360 if (res != 1)
1361 return res;
1362 u_error = tcp->u_error;
1363
1364
1365 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001366 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1367 !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00001368 tcp->flags &= ~TCB_INSYSCALL;
1369 return 0;
1370 }
1371
1372 if (tcp->flags & TCB_REPRINT) {
1373 printleader(tcp);
1374 tprintf("<... ");
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001375 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Pavel Machek4dc3b142000-02-01 17:58:41 +00001376 tprintf("syscall_%lu", tcp->scno);
1377 else
1378 tprintf("%s", sysent[tcp->scno].sys_name);
1379 tprintf(" resumed> ");
1380 }
1381
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001382 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00001383 call_count[tcp->scno]++;
1384 if (tcp->u_error)
1385 error_count[tcp->scno]++;
1386 tv_sub(&tv, &tv, &tcp->etime);
1387#ifdef LINUX
1388 if (tv_cmp(&tv, &tcp->dtime) > 0) {
1389 static struct timeval one_tick =
1390 { 0, 1000000 / HZ };
1391
1392 if (tv_nz(&tcp->dtime))
1393 tv = tcp->dtime;
1394 else if (tv_cmp(&tv, &one_tick) > 0) {
1395 if (tv_cmp(&shortest, &one_tick) < 0)
1396 tv = shortest;
1397 else
1398 tv = one_tick;
1399 }
1400 }
1401#endif /* LINUX */
1402 if (tv_cmp(&tv, &shortest) < 0)
1403 shortest = tv;
1404 tv_add(&tv_count[tcp->scno],
1405 &tv_count[tcp->scno], &tv);
1406 tcp->flags &= ~TCB_INSYSCALL;
1407 return 0;
1408 }
1409
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001410 if (tcp->scno >= nsyscalls || tcp->scno < 0
Pavel Machek4dc3b142000-02-01 17:58:41 +00001411 || (qual_flags[tcp->scno] & QUAL_RAW))
1412 sys_res = printargs(tcp);
1413 else
1414 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1415 u_error = tcp->u_error;
1416 tprintf(") ");
1417 tabto(acolumn);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001418 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
1419 qual_flags[tcp->scno] & QUAL_RAW) {
Pavel Machek4dc3b142000-02-01 17:58:41 +00001420 if (u_error)
1421 tprintf("= -1 (errno %ld)", u_error);
1422 else
1423 tprintf("= %#lx", tcp->u_rval);
1424 }
1425 else if (!(sys_res & RVAL_NONE) && u_error) {
1426 switch (u_error) {
1427#ifdef LINUX
1428 case ERESTARTSYS:
1429 tprintf("= ? ERESTARTSYS (To be restarted)");
1430 break;
1431 case ERESTARTNOINTR:
1432 tprintf("= ? ERESTARTNOINTR (To be restarted)");
1433 break;
1434 case ERESTARTNOHAND:
1435 tprintf("= ? ERESTARTNOHAND (To be restarted)");
1436 break;
1437#endif /* LINUX */
1438 default:
1439 tprintf("= -1 ");
1440 if (u_error < nerrnos && u_error < sys_nerr)
1441 tprintf("%s (%s)", errnoent[u_error],
1442 sys_errlist[u_error]);
1443 else if (u_error < nerrnos)
1444 tprintf("%s (errno %ld)",
1445 errnoent[u_error], u_error);
1446 else if (u_error < sys_nerr)
1447 tprintf("ERRNO_%ld (%s)", u_error,
1448 sys_errlist[u_error]);
1449 else
1450 tprintf("E??? (errno %ld)", u_error);
1451 break;
1452 }
1453 }
1454 else {
1455 if (sys_res & RVAL_NONE)
1456 tprintf("= ?");
1457 else {
1458 switch (sys_res & RVAL_MASK) {
1459 case RVAL_HEX:
1460 tprintf("= %#lx", tcp->u_rval);
1461 break;
1462 case RVAL_OCTAL:
1463 tprintf("= %#lo", tcp->u_rval);
1464 break;
1465 case RVAL_UDECIMAL:
1466 tprintf("= %lu", tcp->u_rval);
1467 break;
1468 case RVAL_DECIMAL:
1469 tprintf("= %ld", tcp->u_rval);
1470 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001471#ifdef HAVE_LONG_LONG
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001472 case RVAL_LHEX:
1473 tprintf("= %#llx", tcp->u_lrval);
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001474 break;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001475 case RVAL_LOCTAL:
1476 tprintf("= %#llo", tcp->u_lrval);
1477 break;
1478 case RVAL_LUDECIMAL:
1479 tprintf("= %llu", tcp->u_lrval);
1480 break;
1481 case RVAL_LDECIMAL:
1482 tprintf("= %lld", tcp->u_lrval);
1483 break;
Wichert Akkerman16a03d22000-08-10 02:14:04 +00001484#endif
Pavel Machek4dc3b142000-02-01 17:58:41 +00001485 default:
1486 fprintf(stderr,
1487 "invalid rval format\n");
1488 break;
1489 }
1490 }
1491 if ((sys_res & RVAL_STR) && tcp->auxstr)
1492 tprintf(" (%s)", tcp->auxstr);
1493 }
1494 if (dtime) {
1495 tv_sub(&tv, &tv, &tcp->etime);
1496 tprintf(" <%ld.%06ld>",
1497 (long) tv.tv_sec, (long) tv.tv_usec);
1498 }
1499 printtrailer(tcp);
1500
1501 dumpio(tcp);
1502 if (fflush(tcp->outf) == EOF)
1503 return -1;
1504 tcp->flags &= ~TCB_INSYSCALL;
1505 return 0;
1506 }
1507
1508 /* Entering system call */
1509 res = syscall_enter(tcp);
1510 if (res != 1)
1511 return res;
1512
Pavel Machekd8ae7e32000-02-01 17:17:25 +00001513 switch (tcp->scno + NR_SYSCALL_BASE) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001514#ifdef LINUX
Wichert Akkerman8b1b40c2000-02-03 21:58:30 +00001515#if !defined (ALPHA) && !defined(IA64) && !defined(SPARC) && !defined(MIPS)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001516 case SYS_socketcall:
1517 decode_subcall(tcp, SYS_socket_subcall,
1518 SYS_socket_nsubcalls, deref_style);
1519 break;
1520 case SYS_ipc:
1521 decode_subcall(tcp, SYS_ipc_subcall,
1522 SYS_ipc_nsubcalls, shift_style);
1523 break;
Wichert Akkerman12f75d12000-02-14 16:23:40 +00001524#endif /* !ALPHA && !IA64 && !MIPS && !SPARC */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001525#ifdef SPARC
1526 case SYS_socketcall:
1527 sparc_socket_decode (tcp);
1528 break;
1529#endif
1530#endif /* LINUX */
1531#ifdef SVR4
1532#ifdef SYS_pgrpsys_subcall
1533 case SYS_pgrpsys:
1534 decode_subcall(tcp, SYS_pgrpsys_subcall,
1535 SYS_pgrpsys_nsubcalls, shift_style);
1536 break;
1537#endif /* SYS_pgrpsys_subcall */
1538#ifdef SYS_sigcall_subcall
1539 case SYS_sigcall:
1540 decode_subcall(tcp, SYS_sigcall_subcall,
1541 SYS_sigcall_nsubcalls, mask_style);
1542 break;
1543#endif /* SYS_sigcall_subcall */
1544 case SYS_msgsys:
1545 decode_subcall(tcp, SYS_msgsys_subcall,
1546 SYS_msgsys_nsubcalls, shift_style);
1547 break;
1548 case SYS_shmsys:
1549 decode_subcall(tcp, SYS_shmsys_subcall,
1550 SYS_shmsys_nsubcalls, shift_style);
1551 break;
1552 case SYS_semsys:
1553 decode_subcall(tcp, SYS_semsys_subcall,
1554 SYS_semsys_nsubcalls, shift_style);
1555 break;
1556#if 0 /* broken */
1557 case SYS_utssys:
1558 decode_subcall(tcp, SYS_utssys_subcall,
1559 SYS_utssys_nsubcalls, shift_style);
1560 break;
1561#endif
1562 case SYS_sysfs:
1563 decode_subcall(tcp, SYS_sysfs_subcall,
1564 SYS_sysfs_nsubcalls, shift_style);
1565 break;
1566 case SYS_spcall:
1567 decode_subcall(tcp, SYS_spcall_subcall,
1568 SYS_spcall_nsubcalls, shift_style);
1569 break;
1570#ifdef SYS_context_subcall
1571 case SYS_context:
1572 decode_subcall(tcp, SYS_context_subcall,
1573 SYS_context_nsubcalls, shift_style);
1574 break;
1575#endif /* SYS_context_subcall */
Wichert Akkerman8829a551999-06-11 13:18:40 +00001576#ifdef SYS_door_subcall
1577 case SYS_door:
1578 decode_subcall(tcp, SYS_door_subcall,
1579 SYS_door_nsubcalls, door_style);
1580 break;
1581#endif /* SYS_door_subcall */
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001582#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001583#ifdef FREEBSD
1584 case SYS_msgsys:
1585 case SYS_shmsys:
1586 case SYS_semsys:
1587 decode_subcall(tcp, 0, 0, table_style);
1588 break;
1589#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001590#ifdef SUNOS4
1591 case SYS_semsys:
1592 decode_subcall(tcp, SYS_semsys_subcall,
1593 SYS_semsys_nsubcalls, shift_style);
1594 break;
1595 case SYS_msgsys:
1596 decode_subcall(tcp, SYS_msgsys_subcall,
1597 SYS_msgsys_nsubcalls, shift_style);
1598 break;
1599 case SYS_shmsys:
1600 decode_subcall(tcp, SYS_shmsys_subcall,
1601 SYS_shmsys_nsubcalls, shift_style);
1602 break;
1603#endif
1604 }
1605
1606 internal_syscall(tcp);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001607 if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001608 tcp->flags |= TCB_INSYSCALL;
1609 return 0;
1610 }
1611
1612 if (cflag) {
1613 gettimeofday(&tcp->etime, NULL);
1614 tcp->flags |= TCB_INSYSCALL;
1615 return 0;
1616 }
1617
1618 printleader(tcp);
1619 tcp->flags &= ~TCB_REPRINT;
1620 tcp_last = tcp;
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001621 if (tcp->scno >= nsyscalls || tcp->scno < 0)
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001622 tprintf("syscall_%lu(", tcp->scno);
1623 else
1624 tprintf("%s(", sysent[tcp->scno].sys_name);
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001625 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001626 ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1627 sys_res = printargs(tcp);
1628 else
1629 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1630 if (fflush(tcp->outf) == EOF)
1631 return -1;
1632 tcp->flags |= TCB_INSYSCALL;
1633 /* Measure the entrance time as late as possible to avoid errors. */
1634 if (dtime)
1635 gettimeofday(&tcp->etime, NULL);
1636 return sys_res;
1637}
1638
1639int
1640printargs(tcp)
1641struct tcb *tcp;
1642{
1643 if (entering(tcp)) {
1644 int i;
1645
1646 for (i = 0; i < tcp->u_nargs; i++)
1647 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1648 }
1649 return 0;
1650}
1651
1652long
1653getrval2(tcp)
1654struct tcb *tcp;
1655{
1656 long val = -1;
1657
1658#ifdef LINUX
1659#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001660 struct regs regs;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001661 if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
1662 return -1;
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001663 val = regs.r_o1;
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001664#endif /* SPARC */
1665#endif /* LINUX */
1666
1667#ifdef SUNOS4
1668 if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1669 return -1;
1670#endif /* SUNOS4 */
1671
1672#ifdef SVR4
1673#ifdef SPARC
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001674 val = tcp->status.PR_REG[R_O1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001675#endif /* SPARC */
1676#ifdef I386
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001677 val = tcp->status.PR_REG[EDX];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001678#endif /* I386 */
1679#ifdef MIPS
Wichert Akkerman9ce1a631999-08-29 23:15:07 +00001680 val = tcp->status.PR_REG[CTX_V1];
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001681#endif /* MIPS */
1682#endif /* SVR4 */
Wichert Akkermanbf79f2e2000-09-01 21:03:06 +00001683#ifdef FREEBSD
1684 struct reg regs;
1685 pread(tcp->pfd_reg, &regs, sizeof(regs), 0);
1686 val = regs.r_edx;
1687#endif
Wichert Akkerman76baf7c1999-02-19 00:21:36 +00001688 return val;
1689}
1690
1691/*
1692 * Apparently, indirect system calls have already be converted by ptrace(2),
1693 * so if you see "indir" this program has gone astray.
1694 */
1695int
1696sys_indir(tcp)
1697struct tcb *tcp;
1698{
1699 int i, scno, nargs;
1700
1701 if (entering(tcp)) {
1702 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1703 fprintf(stderr, "Bogus syscall: %u\n", scno);
1704 return 0;
1705 }
1706 nargs = sysent[scno].nargs;
1707 tprintf("%s", sysent[scno].sys_name);
1708 for (i = 0; i < nargs; i++)
1709 tprintf(", %#lx", tcp->u_arg[i+1]);
1710 }
1711 return 0;
1712}
1713
1714static int
1715time_cmp(a, b)
1716void *a;
1717void *b;
1718{
1719 return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1720}
1721
1722static int
1723syscall_cmp(a, b)
1724void *a;
1725void *b;
1726{
1727 return strcmp(sysent[*((int *) a)].sys_name,
1728 sysent[*((int *) b)].sys_name);
1729}
1730
1731static int
1732count_cmp(a, b)
1733void *a;
1734void *b;
1735{
1736 int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1737
1738 return (m < n) ? 1 : (m > n) ? -1 : 0;
1739}
1740
1741static int (*sortfun)();
1742static struct timeval overhead = { -1, -1 };
1743
1744void
1745set_sortby(sortby)
1746char *sortby;
1747{
1748 if (strcmp(sortby, "time") == 0)
1749 sortfun = time_cmp;
1750 else if (strcmp(sortby, "calls") == 0)
1751 sortfun = count_cmp;
1752 else if (strcmp(sortby, "name") == 0)
1753 sortfun = syscall_cmp;
1754 else if (strcmp(sortby, "nothing") == 0)
1755 sortfun = NULL;
1756 else {
1757 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1758 exit(1);
1759 }
1760}
1761
1762void set_overhead(n)
1763int n;
1764{
1765 overhead.tv_sec = n / 1000000;
1766 overhead.tv_usec = n % 1000000;
1767}
1768
1769void
1770call_summary(outf)
1771FILE *outf;
1772{
1773 int i, j;
1774 int call_cum, error_cum;
1775 struct timeval tv_cum, dtv;
1776 double percent;
1777 char *dashes = "-------------------------";
1778 char error_str[16];
1779
1780 call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1781 if (overhead.tv_sec == -1) {
1782 tv_mul(&overhead, &shortest, 8);
1783 tv_div(&overhead, &overhead, 10);
1784 }
1785 for (i = 0; i < nsyscalls; i++) {
1786 sorted_count[i] = i;
1787 if (call_count[i] == 0)
1788 continue;
1789 tv_mul(&dtv, &overhead, call_count[i]);
1790 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1791 call_cum += call_count[i];
1792 error_cum += error_count[i];
1793 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1794 }
1795 if (sortfun)
1796 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1797 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1798 "% time", "seconds", "usecs/call",
1799 "calls", "errors", "syscall");
1800 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1801 dashes, dashes, dashes, dashes, dashes, dashes);
1802 for (i = 0; i < nsyscalls; i++) {
1803 j = sorted_count[i];
1804 if (call_count[j] == 0)
1805 continue;
1806 tv_div(&dtv, &tv_count[j], call_count[j]);
1807 if (error_count[j])
1808 sprintf(error_str, "%d", error_count[j]);
1809 else
1810 error_str[0] = '\0';
1811 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1812 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1813 percent, (long) tv_count[j].tv_sec,
1814 (long) tv_count[j].tv_usec,
1815 (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1816 call_count[j], error_str, sysent[j].sys_name);
1817 }
1818 fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1819 dashes, dashes, dashes, dashes, dashes, dashes);
1820 if (error_cum)
1821 sprintf(error_str, "%d", error_cum);
1822 else
1823 error_str[0] = '\0';
1824 fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1825 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1826 call_cum, error_str, "total");
1827}