]> granicus.if.org Git - strace/blob - syscall.c
Merge Harald Böhme's solaris patches
[strace] / syscall.c
1 /*
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>
5  * 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>
9  * 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>
44
45 #if HAVE_ASM_REG_H
46 #include <asm/reg.h>
47 #endif
48
49 #ifdef HAVE_SYS_REG_H
50 #include <sys/reg.h>
51 #ifndef PTRACE_PEEKUSR
52 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
53 #endif
54 #elif defined(HAVE_LINUX_PTRACE_H)
55 #undef PTRACE_SYSCALL
56 #include <linux/ptrace.h>
57 #endif
58
59 #if defined(LINUX) && defined(IA64)
60 # include <asm/ptrace_offsets.h>
61 # include <asm/rse.h>
62 #endif
63
64 #ifndef SYS_ERRLIST_DECLARED
65 extern int sys_nerr;
66 extern char *sys_errlist[];
67 #endif /* SYS_ERRLIST_DECLARED */
68
69 #define NR_SYSCALL_BASE 0
70 #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
89 #undef NR_SYSCALL_BASE
90 #define NR_SYSCALL_BASE __NR_SYSCALL_BASE
91 #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
103 struct sysent sysent0[] = {
104 #include "syscallent.h"
105 };
106 int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
107
108 #if SUPPORTED_PERSONALITIES >= 2
109 struct sysent sysent1[] = {
110 #include "syscallent1.h"
111 };
112 int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
113 #endif /* SUPPORTED_PERSONALITIES >= 2 */
114
115 #if SUPPORTED_PERSONALITIES >= 3
116 struct sysent sysent2[] = {
117 #include "syscallent2.h"
118 };
119 int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
120 #endif /* SUPPORTED_PERSONALITIES >= 3 */
121
122 struct sysent *sysent;
123 int 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
132 char *errnoent0[] = {
133 #include "errnoent.h"
134 };
135 int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
136
137 #if SUPPORTED_PERSONALITIES >= 2
138 char *errnoent1[] = {
139 #include "errnoent1.h"
140 };
141 int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
142 #endif /* SUPPORTED_PERSONALITIES >= 2 */
143
144 #if SUPPORTED_PERSONALITIES >= 3
145 char *errnoent2[] = {
146 #include "errnoent2.h"
147 };
148 int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
149 #endif /* SUPPORTED_PERSONALITIES >= 3 */
150
151 char **errnoent;
152 int nerrnos;
153
154 int current_personality;
155
156 int
157 set_personality(personality)
158 int personality;
159 {
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
206 int qual_flags[MAX_QUALS];
207
208 static int call_count[MAX_QUALS];
209 static int error_count[MAX_QUALS];
210 static struct timeval tv_count[MAX_QUALS];
211 static int sorted_count[MAX_QUALS];
212
213 static struct timeval shortest = { 1000000, 0 };
214
215 static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc();
216
217 static 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
246 static int
247 lookup_syscall(s)
248 char *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
259 static int
260 lookup_signal(s)
261 char *s;
262 {
263         int i;
264         char buf[32];
265
266         if (s && *s && isdigit((unsigned char)*s))
267                 return atoi(s);
268         strcpy(buf, s);
269         s = buf;
270         for (i = 0; s[i]; i++)
271                 s[i] = toupper((unsigned char)(s[i]));
272         if (strncmp(s, "SIG", 3) == 0)
273                 s += 3;
274         for (i = 0; i <= NSIG; i++) {
275                 if (strcmp(s, signame(i) + 3) == 0)
276                         return i;
277         }
278         return -1;
279 }
280
281 static int
282 lookup_fault(s)
283 char *s;
284 {
285         return -1;
286 }
287
288 static int
289 lookup_desc(s)
290 char *s;
291 {
292         if (s && *s && isdigit((unsigned char)*s))
293                 return atoi(s);
294         return -1;
295 }
296
297 static int
298 lookup_class(s)
299 char *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
314 void
315 qualify(s)
316 char *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
381 static void
382 dumpio(tcp)
383 struct tcb *tcp;
384 {
385         if (syserror(tcp))
386                 return;
387         if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
388                 return;
389         switch (tcp->scno + NR_SYSCALL_BASE) {
390         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
413 #ifndef FREEBSD
414 enum subcall_style { shift_style, deref_style, mask_style, door_style };
415 #else /* FREEBSD */
416 enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
417
418 struct subcall {
419   int call;
420   int nsubcalls;
421   int subcalls[5];
422 };
423
424 const 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 */
430
431 #if !(defined(LINUX) && ( defined(ALPHA) || defined(IA64) || defined(MIPS) ))
432
433 const 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
453 void
454 sparc_socket_decode (tcp)
455 struct tcb *tcp;
456 {
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
475 static void
476 decode_subcall(tcp, subcall, nsubcalls, style)
477 struct tcb *tcp;
478 int subcall;
479 int nsubcalls;
480 enum subcall_style style;
481 {
482         int i, addr, mask, arg;
483
484 #ifndef FREEBSD
485         if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
486                 return;
487 #endif  
488         switch (style) {
489         case shift_style:
490                 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
491                         return;
492                 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:
501                 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
502                         return;
503                 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;
515                 for (i = 0; mask; i++)
516                         mask >>= 1;
517                 if (i >= nsubcalls)
518                         return;
519                 tcp->u_arg[0] &= 0xff;
520                 tcp->scno = subcall + i;
521                 if (sysent[tcp->scno].nargs != -1)
522                         tcp->u_nargs = sysent[tcp->scno].nargs;
523                 break;
524         case door_style:
525                 /*
526                  * Oh, yuck.  The call code is the *sixth* argument.
527                  * (don't you mean the *last* argument? - JH)
528                  */
529                 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
530                         return;
531                 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;
537 #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 */
549         }
550 }
551 #endif
552
553 struct tcb *tcp_last = NULL;
554
555 static int
556 internal_syscall(tcp)
557 struct 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          */
564         switch (tcp->scno + NR_SYSCALL_BASE) {
565 #ifdef SYS_fork
566         case SYS_fork:
567 #endif
568 #ifdef SYS_vfork
569         case SYS_vfork:
570 #endif
571                 internal_fork(tcp);
572                 break;
573 #ifdef SYS_clone
574         case SYS_clone:
575                 internal_clone(tcp);
576                 break;
577 #endif
578 #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
611
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 */
638 #ifdef FREEBSD
639         struct reg regs;
640 #endif /* FREEBSD */    
641
642 int
643 get_scno(tcp)
644 struct tcb *tcp;
645 {
646         long scno = 0;
647 #ifndef USE_PROCFS
648         int pid = tcp->pid;
649 #endif /* !PROCFS */    
650
651 #ifdef LINUX
652 #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)
660         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;
672 #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         }
691 #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;
701 #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         }
720 #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
755         /* If we are entering, then disassemble the syscall trap. */
756         if (!(tcp->flags & TCB_INSYSCALL)) {
757                 /* Retrieve the syscall trap instruction. */
758                 errno = 0;
759                 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
760                 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;
769                 case 0x91d0206d:
770                         /* Linux/SPARC64 syscall trap. */
771                         fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
772                         return -1;
773                 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                         }
795                         fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
796                         return -1;
797                 }
798
799                 /* Extract the system call number from the registers. */
800                 if (trap == 0x91d02027)
801                         scno = 156;
802                 else
803                         scno = regs.r_g1;
804                 if (scno == 0) {
805                         scno = regs.r_o0;
806                         memmove (&regs.r_o0, &regs.r_o1, 7*sizeof(regs.r_o0));
807                 }
808         }
809 #endif 
810 #endif /* LINUX */
811 #ifdef SUNOS4
812         if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
813                 return -1;
814 #endif
815 #ifdef USE_PROCFS
816 #ifdef HAVE_PR_SYSCALL
817         scno = tcp->status.PR_SYSCALL;
818 #else /* !HAVE_PR_SYSCALL */
819 #ifndef FREEBSD
820         scno = tcp->status.PR_WHAT;
821 #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 */
836 #endif /* !HAVE_PR_SYSCALL */
837 #endif /* USE_PROCFS */
838         if (!(tcp->flags & TCB_INSYSCALL))
839                 tcp->scno = scno;
840         return 1;
841 }
842
843
844 int
845 syscall_fixup(tcp)
846 struct tcb *tcp;
847 {
848 #ifndef USE_PROCFS
849         int pid = tcp->pid;
850 #else /* USE_PROCFS */  
851         int scno = tcp->scno;
852
853         if (!(tcp->flags & TCB_INSYSCALL)) {
854                 if (tcp->status.PR_WHY != PR_SYSENTRY) {
855                         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. */
862                                 tcp->status.PR_WHY = PR_SYSENTRY;
863                                 trace_syscall(tcp);
864                                 tcp->status.PR_WHY = PR_SYSEXIT;
865                         }
866                         else {
867                                 fprintf(stderr, "syscall: missing entry\n");
868                                 tcp->flags |= TCB_INSYSCALL;
869                         }
870                 }
871         }
872         else {
873                 if (tcp->status.PR_WHY != PR_SYSEXIT) {
874                         fprintf(stderr, "syscall: missing exit\n");
875                         tcp->flags &= ~TCB_INSYSCALL;
876                 }
877         }
878 #endif /* USE_PROCFS */
879 #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         }
909 #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         }
917 #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 */
944         return 1;
945 }
946
947 int
948 get_error(tcp)
949 struct tcb *tcp;
950 {
951         int u_error = 0;
952 #ifdef LINUX
953 #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 */
963 #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 */
973 #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 */
996 #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
1005 #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
1046                 if (regs.r_psr & PSR_C) {
1047                         tcp->u_rval = -1;
1048                         u_error = regs.r_o0;
1049                 }
1050                 else {
1051                         tcp->u_rval = regs.r_o0;
1052                         u_error = 0;
1053                 }
1054 #endif /* SPARC */
1055 #endif /* ALPHA */
1056 #endif /* ARM */
1057 #endif /* M68K */
1058 #endif /* POWERPC */
1059 #endif /* MIPS */
1060 #endif /* IA64 */
1061 #endif /* I386 */
1062 #endif /* S390 */
1063 #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? */
1088                 if (tcp->status.PR_REG[EFL] & 0x1) {
1089                         tcp->u_rval = -1;
1090                         u_error = tcp->status.PR_REG[EAX];
1091                 }
1092                 else {
1093                         tcp->u_rval = tcp->status.PR_REG[EAX];
1094 #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
1099                         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 */
1113 #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 */    
1124         tcp->u_error = u_error;
1125         return 1;
1126 }
1127
1128 int syscall_enter(tcp)
1129 struct tcb *tcp;
1130 {
1131 #ifndef USE_PROCFS
1132         int pid = tcp->pid;
1133 #endif /* !USE_PROCFS */        
1134 #ifdef LINUX
1135 #if defined(S390)
1136         {
1137                 int i;
1138                 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;
1142                 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)
1148         {
1149                 int i;
1150                 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;
1154                 for (i = 0; i < tcp->u_nargs; i++) {
1155                         /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1156                          * for scno somewhere above here!
1157                          */
1158                         if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1159                                 return -1;
1160                 }
1161         }
1162 #elif defined (IA64)
1163         {
1164                 unsigned long *bsp, cfm, i;
1165
1166                 if (upeek(pid, PT_AR_BSP, (long *) &bsp) < 0)
1167                         return -1;
1168                 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1169                         return -1;
1170
1171                 bsp = ia64_rse_skip_regs(bsp, -(cfm & 0x7f));
1172
1173                 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;
1177                 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         }
1184 #elif defined (MIPS)
1185         {
1186                 long sp;
1187                 int i, nargs;
1188
1189                 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;
1193                 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         }
1209 #elif defined (POWERPC)
1210         {
1211                 int i;
1212                 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;
1216                 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         {
1223                 int i;
1224
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;
1229                 for (i = 0; i < tcp->u_nargs; i++)
1230                         tcp->u_arg[i] = *((&regs.r_o0) + i);
1231         }
1232 #else /* Other architecture (like i386) (32bits specific) */
1233         {
1234                 int i;
1235                 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;
1239                 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;
1249                 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;
1253                 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          */
1268         if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1269                 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 #elif UNIXWARE >= 2
1283         /*
1284          * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
1285          */
1286         if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1287                 tcp->u_nargs = sysent[tcp->scno].nargs;
1288         else
1289                 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1290         umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1291                 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1292 #elif defined (HAVE_PR_SYSCALL)
1293         if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1294                 tcp->u_nargs = sysent[tcp->scno].nargs;
1295         else
1296                 tcp->u_nargs = tcp->status.pr_nsysarg;
1297         {
1298                 int i;
1299                 for (i = 0; i < tcp->u_nargs; i++)
1300                         tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1301         }
1302 #elif defined (I386)
1303         if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1304                 tcp->u_nargs = sysent[tcp->scno].nargs;
1305         else
1306                 tcp->u_nargs = 5;
1307         umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1308                 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1309 #else
1310         I DONT KNOW WHAT TO DO
1311 #endif /* !HAVE_PR_SYSCALL */
1312 #endif /* SVR4 */
1313 #ifdef FREEBSD
1314         if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1315             sysent[tcp->scno].nargs > tcp->status.val)
1316                 tcp->u_nargs = sysent[tcp->scno].nargs;
1317         else 
1318                 tcp->u_nargs = tcp->status.val;
1319         if (tcp->u_nargs < 0)
1320                 tcp->u_nargs = 0;
1321         if (tcp->u_nargs > MAX_ARGS)
1322                 tcp->u_nargs = MAX_ARGS;
1323         switch(regs.r_eax) {
1324         case SYS___syscall:
1325                 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1326                       regs.r_esp + sizeof(int) + sizeof(quad_t));
1327           break;
1328         case SYS_syscall:
1329                 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1330                       regs.r_esp + 2 * sizeof(int));
1331           break;
1332         default:
1333                 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1334                       regs.r_esp + sizeof(int));
1335           break;
1336         }
1337 #endif /* FREEBSD */
1338         return 1;
1339 }
1340
1341 int
1342 trace_syscall(tcp)
1343 struct tcb *tcp;
1344 {
1345         int sys_res;
1346         struct timeval tv;
1347         int res;
1348
1349         /* Measure the exit time as early as possible to avoid errors. */
1350         if (dtime && (tcp->flags & TCB_INSYSCALL))
1351                 gettimeofday(&tv, NULL);
1352
1353         res = get_scno(tcp);
1354         if (res != 1)
1355                 return res;
1356
1357         res = syscall_fixup(tcp);
1358         if (res != 1)
1359                 return res;
1360
1361         if (tcp->flags & TCB_INSYSCALL) {
1362                 long u_error;
1363                 res = get_error(tcp);
1364                 if (res != 1)
1365                         return res;
1366                 u_error = tcp->u_error;
1367
1368
1369                 internal_syscall(tcp);
1370                 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1371                     !(qual_flags[tcp->scno] & QUAL_TRACE)) {
1372                         tcp->flags &= ~TCB_INSYSCALL;
1373                         return 0;
1374                 }
1375
1376                 if (tcp->flags & TCB_REPRINT) {
1377                         printleader(tcp);
1378                         tprintf("<... ");
1379                         if (tcp->scno >= nsyscalls || tcp->scno < 0)
1380                                 tprintf("syscall_%lu", tcp->scno);
1381                         else
1382                                 tprintf("%s", sysent[tcp->scno].sys_name);
1383                         tprintf(" resumed> ");
1384                 }
1385
1386                 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
1387                         call_count[tcp->scno]++;
1388                         if (tcp->u_error)
1389                                 error_count[tcp->scno]++;
1390                         tv_sub(&tv, &tv, &tcp->etime);
1391 #ifdef LINUX
1392                         if (tv_cmp(&tv, &tcp->dtime) > 0) {
1393                                 static struct timeval one_tick =
1394                                         { 0, 1000000 / HZ };
1395
1396                                 if (tv_nz(&tcp->dtime))
1397                                         tv = tcp->dtime;
1398                                 else if (tv_cmp(&tv, &one_tick) > 0) {
1399                                         if (tv_cmp(&shortest, &one_tick) < 0)
1400                                                 tv = shortest;
1401                                         else
1402                                                 tv = one_tick;
1403                                 }
1404                         }
1405 #endif /* LINUX */
1406                         if (tv_cmp(&tv, &shortest) < 0)
1407                                 shortest = tv;
1408                         tv_add(&tv_count[tcp->scno],
1409                                 &tv_count[tcp->scno], &tv);
1410                         tcp->flags &= ~TCB_INSYSCALL;
1411                         return 0;
1412                 }
1413
1414                 if (tcp->scno >= nsyscalls || tcp->scno < 0
1415                     || (qual_flags[tcp->scno] & QUAL_RAW))
1416                         sys_res = printargs(tcp);
1417                 else
1418                         sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1419                 u_error = tcp->u_error;
1420                 tprintf(") ");
1421                 tabto(acolumn);
1422                 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
1423                     qual_flags[tcp->scno] & QUAL_RAW) {
1424                         if (u_error)
1425                                 tprintf("= -1 (errno %ld)", u_error);
1426                         else
1427                                 tprintf("= %#lx", tcp->u_rval);
1428                 }
1429                 else if (!(sys_res & RVAL_NONE) && u_error) {
1430                         switch (u_error) {
1431 #ifdef LINUX
1432                         case ERESTARTSYS:
1433                                 tprintf("= ? ERESTARTSYS (To be restarted)");
1434                                 break;
1435                         case ERESTARTNOINTR:
1436                                 tprintf("= ? ERESTARTNOINTR (To be restarted)");
1437                                 break;
1438                         case ERESTARTNOHAND:
1439                                 tprintf("= ? ERESTARTNOHAND (To be restarted)");
1440                                 break;
1441 #endif /* LINUX */
1442                         default:
1443                                 tprintf("= -1 ");
1444                                 if (u_error < nerrnos && u_error < sys_nerr)
1445                                         tprintf("%s (%s)", errnoent[u_error],
1446                                                 sys_errlist[u_error]);
1447                                 else if (u_error < nerrnos)
1448                                         tprintf("%s (errno %ld)",
1449                                                 errnoent[u_error], u_error);
1450                                 else if (u_error < sys_nerr)
1451                                         tprintf("ERRNO_%ld (%s)", u_error,
1452                                                 sys_errlist[u_error]);
1453                                 else
1454                                         tprintf("E??? (errno %ld)", u_error);
1455                                 break;
1456                         }
1457                 }
1458                 else {
1459                         if (sys_res & RVAL_NONE)
1460                                 tprintf("= ?");
1461                         else {
1462                                 switch (sys_res & RVAL_MASK) {
1463                                 case RVAL_HEX:
1464                                         tprintf("= %#lx", tcp->u_rval);
1465                                         break;
1466                                 case RVAL_OCTAL:
1467                                         tprintf("= %#lo", tcp->u_rval);
1468                                         break;
1469                                 case RVAL_UDECIMAL:
1470                                         tprintf("= %lu", tcp->u_rval);
1471                                         break;
1472                                 case RVAL_DECIMAL:
1473                                         tprintf("= %ld", tcp->u_rval);
1474                                         break;
1475 #ifdef HAVE_LONG_LONG
1476                                 case RVAL_LHEX:
1477                                         tprintf("= %#llx", tcp->u_lrval);
1478                                         break;
1479                                 case RVAL_LOCTAL:
1480                                         tprintf("= %#llo", tcp->u_lrval);
1481                                         break;
1482                                 case RVAL_LUDECIMAL:
1483                                         tprintf("= %llu", tcp->u_lrval);
1484                                         break;
1485                                 case RVAL_LDECIMAL:
1486                                         tprintf("= %lld", tcp->u_lrval);
1487                                         break;
1488 #endif
1489                                 default:
1490                                         fprintf(stderr,
1491                                                 "invalid rval format\n");
1492                                         break;
1493                                 }
1494                         }
1495                         if ((sys_res & RVAL_STR) && tcp->auxstr)
1496                                 tprintf(" (%s)", tcp->auxstr);
1497                 }
1498                 if (dtime) {
1499                         tv_sub(&tv, &tv, &tcp->etime);
1500                         tprintf(" <%ld.%06ld>",
1501                                 (long) tv.tv_sec, (long) tv.tv_usec);
1502                 }
1503                 printtrailer(tcp);
1504
1505                 dumpio(tcp);
1506                 if (fflush(tcp->outf) == EOF)
1507                         return -1;
1508                 tcp->flags &= ~TCB_INSYSCALL;
1509                 return 0;
1510         }
1511
1512         /* Entering system call */
1513         res = syscall_enter(tcp);
1514         if (res != 1)
1515                 return res;
1516
1517         switch (tcp->scno + NR_SYSCALL_BASE) {
1518 #ifdef LINUX
1519 #if !defined (ALPHA) && !defined(IA64) && !defined(SPARC) && !defined(MIPS)
1520         case SYS_socketcall:
1521                 decode_subcall(tcp, SYS_socket_subcall,
1522                         SYS_socket_nsubcalls, deref_style);
1523                 break;
1524         case SYS_ipc:
1525                 decode_subcall(tcp, SYS_ipc_subcall,
1526                         SYS_ipc_nsubcalls, shift_style);
1527                 break;
1528 #endif /* !ALPHA && !IA64 && !MIPS && !SPARC */
1529 #ifdef SPARC
1530         case SYS_socketcall:
1531                 sparc_socket_decode (tcp);
1532                 break;
1533 #endif
1534 #endif /* LINUX */
1535 #ifdef SVR4
1536 #ifdef SYS_pgrpsys_subcall
1537         case SYS_pgrpsys:
1538                 decode_subcall(tcp, SYS_pgrpsys_subcall,
1539                         SYS_pgrpsys_nsubcalls, shift_style);
1540                 break;
1541 #endif /* SYS_pgrpsys_subcall */
1542 #ifdef SYS_sigcall_subcall
1543         case SYS_sigcall:
1544                 decode_subcall(tcp, SYS_sigcall_subcall,
1545                         SYS_sigcall_nsubcalls, mask_style);
1546                 break;
1547 #endif /* SYS_sigcall_subcall */
1548         case SYS_msgsys:
1549                 decode_subcall(tcp, SYS_msgsys_subcall,
1550                         SYS_msgsys_nsubcalls, shift_style);
1551                 break;
1552         case SYS_shmsys:
1553                 decode_subcall(tcp, SYS_shmsys_subcall,
1554                         SYS_shmsys_nsubcalls, shift_style);
1555                 break;
1556         case SYS_semsys:
1557                 decode_subcall(tcp, SYS_semsys_subcall,
1558                         SYS_semsys_nsubcalls, shift_style);
1559                 break;
1560 #if 0 /* broken */
1561         case SYS_utssys:
1562                 decode_subcall(tcp, SYS_utssys_subcall,
1563                         SYS_utssys_nsubcalls, shift_style);
1564                 break;
1565 #endif
1566         case SYS_sysfs:
1567                 decode_subcall(tcp, SYS_sysfs_subcall,
1568                         SYS_sysfs_nsubcalls, shift_style);
1569                 break;
1570         case SYS_spcall:
1571                 decode_subcall(tcp, SYS_spcall_subcall,
1572                         SYS_spcall_nsubcalls, shift_style);
1573                 break;
1574 #ifdef SYS_context_subcall
1575         case SYS_context:
1576                 decode_subcall(tcp, SYS_context_subcall,
1577                         SYS_context_nsubcalls, shift_style);
1578                 break;
1579 #endif /* SYS_context_subcall */
1580 #ifdef SYS_door_subcall
1581         case SYS_door:
1582                 decode_subcall(tcp, SYS_door_subcall,
1583                         SYS_door_nsubcalls, door_style);
1584                 break;
1585 #endif /* SYS_door_subcall */
1586 #ifdef SYS_kaio_subcall
1587         case SYS_kaio:
1588                 decode_subcall(tcp, SYS_kaio_subcall,
1589                         SYS_kaio_nsubcalls, shift_style);
1590                 break;
1591 #endif  
1592 #endif /* SVR4 */
1593 #ifdef FREEBSD
1594         case SYS_msgsys:
1595         case SYS_shmsys:
1596         case SYS_semsys:
1597                 decode_subcall(tcp, 0, 0, table_style);
1598                 break;
1599 #endif          
1600 #ifdef SUNOS4
1601         case SYS_semsys:
1602                 decode_subcall(tcp, SYS_semsys_subcall,
1603                         SYS_semsys_nsubcalls, shift_style);
1604                 break;
1605         case SYS_msgsys:
1606                 decode_subcall(tcp, SYS_msgsys_subcall,
1607                         SYS_msgsys_nsubcalls, shift_style);
1608                 break;
1609         case SYS_shmsys:
1610                 decode_subcall(tcp, SYS_shmsys_subcall,
1611                         SYS_shmsys_nsubcalls, shift_style);
1612                 break;
1613 #endif
1614         }
1615
1616         internal_syscall(tcp);
1617         if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
1618                 tcp->flags |= TCB_INSYSCALL;
1619                 return 0;
1620         }
1621
1622         if (cflag) {
1623                 gettimeofday(&tcp->etime, NULL);
1624                 tcp->flags |= TCB_INSYSCALL;
1625                 return 0;
1626         }
1627
1628         printleader(tcp);
1629         tcp->flags &= ~TCB_REPRINT;
1630         tcp_last = tcp;
1631         if (tcp->scno >= nsyscalls || tcp->scno < 0)
1632                 tprintf("syscall_%lu(", tcp->scno);
1633         else
1634                 tprintf("%s(", sysent[tcp->scno].sys_name);
1635         if (tcp->scno >= nsyscalls || tcp->scno < 0 || 
1636             ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1637                 sys_res = printargs(tcp);
1638         else
1639                 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1640         if (fflush(tcp->outf) == EOF)
1641                 return -1;
1642         tcp->flags |= TCB_INSYSCALL;
1643         /* Measure the entrance time as late as possible to avoid errors. */
1644         if (dtime)
1645                 gettimeofday(&tcp->etime, NULL);
1646         return sys_res;
1647 }
1648
1649 int
1650 printargs(tcp)
1651 struct tcb *tcp;
1652 {
1653         if (entering(tcp)) {
1654                 int i;
1655
1656                 for (i = 0; i < tcp->u_nargs; i++)
1657                         tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1658         }
1659         return 0;
1660 }
1661
1662 long
1663 getrval2(tcp)
1664 struct tcb *tcp;
1665 {
1666         long val = -1;
1667
1668 #ifdef LINUX
1669 #ifdef SPARC
1670         struct regs regs;
1671         if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
1672                 return -1;
1673         val = regs.r_o1;
1674 #endif /* SPARC */
1675 #endif /* LINUX */
1676
1677 #ifdef SUNOS4
1678         if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1679                 return -1;
1680 #endif /* SUNOS4 */
1681
1682 #ifdef SVR4
1683 #ifdef SPARC
1684         val = tcp->status.PR_REG[R_O1];
1685 #endif /* SPARC */
1686 #ifdef I386
1687         val = tcp->status.PR_REG[EDX];
1688 #endif /* I386 */
1689 #ifdef MIPS
1690         val = tcp->status.PR_REG[CTX_V1];
1691 #endif /* MIPS */
1692 #endif /* SVR4 */
1693 #ifdef FREEBSD
1694         struct reg regs;
1695         pread(tcp->pfd_reg, &regs, sizeof(regs), 0);
1696         val = regs.r_edx;
1697 #endif  
1698         return val;
1699 }
1700
1701 /*
1702  * Apparently, indirect system calls have already be converted by ptrace(2),
1703  * so if you see "indir" this program has gone astray.
1704  */
1705 int
1706 sys_indir(tcp)
1707 struct tcb *tcp;
1708 {
1709         int i, scno, nargs;
1710
1711         if (entering(tcp)) {
1712                 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1713                         fprintf(stderr, "Bogus syscall: %u\n", scno);
1714                         return 0;
1715                 }
1716                 nargs = sysent[scno].nargs;
1717                 tprintf("%s", sysent[scno].sys_name);
1718                 for (i = 0; i < nargs; i++)
1719                         tprintf(", %#lx", tcp->u_arg[i+1]);
1720         }
1721         return 0;
1722 }
1723
1724 static int
1725 time_cmp(a, b)
1726 void *a;
1727 void *b;
1728 {
1729         return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1730 }
1731
1732 static int
1733 syscall_cmp(a, b)
1734 void *a;
1735 void *b;
1736 {
1737         return strcmp(sysent[*((int *) a)].sys_name,
1738                 sysent[*((int *) b)].sys_name);
1739 }
1740
1741 static int
1742 count_cmp(a, b)
1743 void *a;
1744 void *b;
1745 {
1746         int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1747
1748         return (m < n) ? 1 : (m > n) ? -1 : 0;
1749 }
1750
1751 static int (*sortfun)();
1752 static struct timeval overhead = { -1, -1 };
1753
1754 void
1755 set_sortby(sortby)
1756 char *sortby;
1757 {
1758         if (strcmp(sortby, "time") == 0)
1759                 sortfun = time_cmp;
1760         else if (strcmp(sortby, "calls") == 0)
1761                 sortfun = count_cmp;
1762         else if (strcmp(sortby, "name") == 0)
1763                 sortfun = syscall_cmp;
1764         else if (strcmp(sortby, "nothing") == 0)
1765                 sortfun = NULL;
1766         else {
1767                 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1768                 exit(1);
1769         }
1770 }
1771
1772 void set_overhead(n)
1773 int n;
1774 {
1775         overhead.tv_sec = n / 1000000;
1776         overhead.tv_usec = n % 1000000;
1777 }
1778
1779 void
1780 call_summary(outf)
1781 FILE *outf;
1782 {
1783         int i, j;
1784         int call_cum, error_cum;
1785         struct timeval tv_cum, dtv;
1786         double percent;
1787         char *dashes = "-------------------------";
1788         char error_str[16];
1789
1790         call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1791         if (overhead.tv_sec == -1) {
1792                 tv_mul(&overhead, &shortest, 8);
1793                 tv_div(&overhead, &overhead, 10);
1794         }
1795         for (i = 0; i < nsyscalls; i++) {
1796                 sorted_count[i] = i;
1797                 if (call_count[i] == 0)
1798                         continue;
1799                 tv_mul(&dtv, &overhead, call_count[i]);
1800                 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1801                 call_cum += call_count[i];
1802                 error_cum += error_count[i];
1803                 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1804         }
1805         if (sortfun)
1806                 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1807         fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1808                 "% time", "seconds", "usecs/call",
1809                 "calls", "errors", "syscall");
1810         fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1811                 dashes, dashes, dashes, dashes, dashes, dashes);
1812         for (i = 0; i < nsyscalls; i++) {
1813                 j = sorted_count[i];
1814                 if (call_count[j] == 0)
1815                         continue;
1816                 tv_div(&dtv, &tv_count[j], call_count[j]);
1817                 if (error_count[j])
1818                         sprintf(error_str, "%d", error_count[j]);
1819                 else
1820                         error_str[0] = '\0';
1821                 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1822                 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1823                         percent, (long) tv_count[j].tv_sec,
1824                         (long) tv_count[j].tv_usec,
1825                         (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1826                         call_count[j], error_str, sysent[j].sys_name);
1827         }
1828         fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1829                 dashes, dashes, dashes, dashes, dashes, dashes);
1830         if (error_cum)
1831                 sprintf(error_str, "%d", error_cum);
1832         else
1833                 error_str[0] = '\0';
1834         fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1835                 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1836                 call_cum, error_str, "total");
1837 }