]> granicus.if.org Git - strace/blob - syscall.c
retro-actively mention Linux/hppa support in the changelog
[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 #ifdef SYS_semconfig
427   { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
428 #else
429   { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
430 #endif
431   { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
432 };
433 #endif /* FREEBSD */
434
435 #if !(defined(LINUX) && ( defined(ALPHA) || defined(IA64) || defined(MIPS) ))
436
437 const int socket_map [] = {
438                /* SYS_SOCKET      */ 97,
439                /* SYS_BIND        */ 104,
440                /* SYS_CONNECT     */ 98,
441                /* SYS_LISTEN      */ 106,
442                /* SYS_ACCEPT      */ 99,
443                /* SYS_GETSOCKNAME */ 150,
444                /* SYS_GETPEERNAME */ 141,
445                /* SYS_SOCKETPAIR  */ 135,
446                /* SYS_SEND        */ 101,
447                /* SYS_RECV        */ 102,
448                /* SYS_SENDTO      */ 133,
449                /* SYS_RECVFROM    */ 125,
450                /* SYS_SHUTDOWN    */ 134,
451                /* SYS_SETSOCKOPT  */ 105,
452                /* SYS_GETSOCKOPT  */ 118,
453                /* SYS_SENDMSG     */ 114,
454                /* SYS_RECVMSG     */ 113
455 };
456
457 void
458 sparc_socket_decode (tcp)
459 struct tcb *tcp;
460 {
461         volatile long addr;
462         volatile int i, n;
463
464         if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
465                 return;
466         }
467         tcp->scno = socket_map [tcp->u_arg [0]-1];
468         n = tcp->u_nargs = sysent [tcp->scno].nargs;
469         addr = tcp->u_arg [1];
470         for (i = 0; i < n; i++){
471                 int arg;
472                 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
473                         arg = 0;
474                 tcp->u_arg [i] = arg;
475                 addr += sizeof (arg);
476         }
477 }
478
479 static void
480 decode_subcall(tcp, subcall, nsubcalls, style)
481 struct tcb *tcp;
482 int subcall;
483 int nsubcalls;
484 enum subcall_style style;
485 {
486         int i, addr, mask, arg;
487
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 #elif defined(HPPA)
637         static long r28;
638 #endif 
639 #endif /* LINUX */
640 #ifdef FREEBSD
641         struct reg regs;
642 #endif /* FREEBSD */    
643
644 int
645 get_scno(tcp)
646 struct tcb *tcp;
647 {
648         long scno = 0;
649 #ifndef USE_PROCFS
650         int pid = tcp->pid;
651 #endif /* !PROCFS */    
652
653 #ifdef LINUX
654 #if defined(S390)
655         if (upeek(tcp->pid,PT_PSWADDR,&pc) < 0)
656                 return -1;
657         scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-4),0);
658         if (errno)
659                 return -1;
660         scno&=0xFF;
661 #elif defined (POWERPC)
662         if (upeek(pid, 4*PT_R0, &scno) < 0)
663                 return -1;
664         if (!(tcp->flags & TCB_INSYSCALL)) {
665                 /* Check if we return from execve. */
666                 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
667                         tcp->flags &= ~TCB_WAITEXECVE;
668                         return 0;
669                 }
670         }
671 #elif defined (I386)
672         if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
673                 return -1;
674 #elif defined(IA64)
675 #define IA64_PSR_IS     ((long)1 << 34)
676         if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
677                 ia32 = (psr & IA64_PSR_IS);
678         if (!(tcp->flags & TCB_INSYSCALL)) {
679                 if (ia32) {
680                         if (upeek(pid, PT_R8, &scno) < 0)
681                                 return -1;
682                 } else {
683                         if (upeek (pid, PT_R15, &scno) < 0)
684                                 return -1;
685                 }
686         } else {
687                 /* syscall in progress */
688                 if (upeek (pid, PT_R8, &r8) < 0)
689                         return -1;
690                 if (upeek (pid, PT_R10, &r10) < 0)
691                         return -1;
692         }
693 #elif defined (ARM)
694         { 
695             long pc;
696             upeek(pid, 4*15, &pc);
697             umoven(tcp, pc-4, 4, (char *)&scno);
698             scno &= 0x000fffff;
699         }
700 #elif defined (M68K)
701         if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
702                 return -1;
703 #elif defined (MIPS)
704         if (upeek(pid, REG_A3, &a3) < 0)
705                 return -1;
706
707         if(!(tcp->flags & TCB_INSYSCALL)) {
708                 if (upeek(pid, REG_V0, &scno) < 0)
709                         return -1;
710
711                 if (scno < 0 || scno > nsyscalls) {
712                         if(a3 == 0 || a3 == -1) {
713                                 if(debug)
714                                         fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
715                                 return 0;
716                         }
717                 }
718         } else {
719                 if (upeek(pid, REG_V0, &r2) < 0)
720                         return -1;
721         }
722 #elif defined (ALPHA)
723         if (upeek(pid, REG_A3, &a3) < 0)
724                 return -1;
725
726         if (!(tcp->flags & TCB_INSYSCALL)) {
727                 if (upeek(pid, REG_R0, &scno) < 0)
728                         return -1;
729
730                 /* Check if we return from execve. */
731                 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
732                         tcp->flags &= ~TCB_WAITEXECVE;
733                         return 0;
734                 }
735
736                 /*
737                  * Do some sanity checks to figure out if it's
738                  * really a syscall entry
739                  */
740                 if (scno < 0 || scno > nsyscalls) {
741                         if (a3 == 0 || a3 == -1) {
742                                 if (debug)
743                                         fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
744                                 return 0;
745                         }
746                 }
747         }
748         else {
749                 if (upeek(pid, REG_R0, &r0) < 0)
750                         return -1;
751         }
752 #elif defined (SPARC)
753         /* Everything we need is in the current register set. */
754         if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
755                 return -1;
756
757         /* If we are entering, then disassemble the syscall trap. */
758         if (!(tcp->flags & TCB_INSYSCALL)) {
759                 /* Retrieve the syscall trap instruction. */
760                 errno = 0;
761                 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
762                 if (errno)
763                         return -1;
764
765                 /* Disassemble the trap to see what personality to use. */
766                 switch (trap) {
767                 case 0x91d02010:
768                         /* Linux/SPARC syscall trap. */
769                         set_personality(0);
770                         break;
771                 case 0x91d0206d:
772                         /* Linux/SPARC64 syscall trap. */
773                         fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
774                         return -1;
775                 case 0x91d02000:
776                         /* SunOS syscall trap. (pers 1) */
777                         fprintf(stderr,"syscall: SunOS no support\n");
778                         return -1;
779                 case 0x91d02008:
780                         /* Solaris 2.x syscall trap. (per 2) */
781                         set_personality(1);
782                         break; 
783                 case 0x91d02009:
784                         /* NetBSD/FreeBSD syscall trap. */
785                         fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
786                         return -1;
787                 case 0x91d02027:
788                         /* Solaris 2.x gettimeofday */
789                         set_personality(1);
790                         break;
791                 default:
792                         /* Unknown syscall trap. */
793                         if(tcp->flags & TCB_WAITEXECVE) {
794                                 tcp->flags &= ~TCB_WAITEXECVE;
795                                 return 0;
796                         }
797                         fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
798                         return -1;
799                 }
800
801                 /* Extract the system call number from the registers. */
802                 if (trap == 0x91d02027)
803                         scno = 156;
804                 else
805                         scno = regs.r_g1;
806                 if (scno == 0) {
807                         scno = regs.r_o0;
808                         memmove (&regs.r_o0, &regs.r_o1, 7*sizeof(regs.r_o0));
809                 }
810         }
811 #elif defined(HPPA)
812         if (upeek(pid, PT_GR20, &scno) < 0)
813                 return -1;
814         if (!(tcp->flags & TCB_INSYSCALL)) {
815                 /* Check if we return from execve. */
816                 if ((tcp->flags & TCB_WAITEXECVE)) {
817                         tcp->flags &= ~TCB_WAITEXECVE;
818                         return 0;
819                 }
820         }
821 #endif 
822 #endif /* LINUX */
823 #ifdef SUNOS4
824         if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
825                 return -1;
826 #endif
827 #ifdef USE_PROCFS
828 #ifdef HAVE_PR_SYSCALL
829         scno = tcp->status.PR_SYSCALL;
830 #else /* !HAVE_PR_SYSCALL */
831 #ifndef FREEBSD
832         scno = tcp->status.PR_WHAT;
833 #else /* FREEBSD */
834         if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
835                 perror("pread");
836                 return -1;
837         }
838         switch (regs.r_eax) {
839         case SYS_syscall:
840         case SYS___syscall:
841                 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
842                 break;
843         default:
844                 scno = regs.r_eax;
845                 break;
846         }
847 #endif /* FREEBSD */
848 #endif /* !HAVE_PR_SYSCALL */
849 #endif /* USE_PROCFS */
850         if (!(tcp->flags & TCB_INSYSCALL))
851                 tcp->scno = scno;
852         return 1;
853 }
854
855
856 int
857 syscall_fixup(tcp)
858 struct tcb *tcp;
859 {
860 #ifndef USE_PROCFS
861         int pid = tcp->pid;
862 #else /* USE_PROCFS */  
863         int scno = tcp->scno;
864
865         if (!(tcp->flags & TCB_INSYSCALL)) {
866                 if (tcp->status.PR_WHY != PR_SYSENTRY) {
867                         if (
868                             scno == SYS_fork
869 #ifdef SYS_vfork
870                             || scno == SYS_vfork
871 #endif /* SYS_vfork */
872                             ) {
873                                 /* We are returning in the child, fake it. */
874                                 tcp->status.PR_WHY = PR_SYSENTRY;
875                                 trace_syscall(tcp);
876                                 tcp->status.PR_WHY = PR_SYSEXIT;
877                         }
878                         else {
879                                 fprintf(stderr, "syscall: missing entry\n");
880                                 tcp->flags |= TCB_INSYSCALL;
881                         }
882                 }
883         }
884         else {
885                 if (tcp->status.PR_WHY != PR_SYSEXIT) {
886                         fprintf(stderr, "syscall: missing exit\n");
887                         tcp->flags &= ~TCB_INSYSCALL;
888                 }
889         }
890 #endif /* USE_PROCFS */
891 #ifdef SUNOS4
892         if (!(tcp->flags & TCB_INSYSCALL)) {
893                 if (scno == 0) {
894                         fprintf(stderr, "syscall: missing entry\n");
895                         tcp->flags |= TCB_INSYSCALL;
896                 }
897         }
898         else {
899                 if (scno != 0) {
900                         if (debug) {
901                                 /*
902                                  * This happens when a signal handler
903                                  * for a signal which interrupted a
904                                  * a system call makes another system call.
905                                  */
906                                 fprintf(stderr, "syscall: missing exit\n");
907                         }
908                         tcp->flags &= ~TCB_INSYSCALL;
909                 }
910         }
911 #endif /* SUNOS4 */
912 #ifdef LINUX
913 #if defined (I386)
914         if (upeek(pid, 4*EAX, &eax) < 0)
915                 return -1;
916         if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
917                 if (debug)
918                         fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
919                 return 0;
920         }
921 #elif defined (S390)
922         if (upeek(pid, PT_GPR2, &gpr2) < 0)
923                 return -1;
924         if (gpr2 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
925                 if (debug)
926                         fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
927                 return 0;
928         }
929 #elif defined (POWERPC)
930 # define SO_MASK 0x10000000
931         if (upeek(pid, 4*PT_CCR, &flags) < 0)
932                 return -1;
933         if (upeek(pid, 4*PT_R3, &result) < 0)
934                 return -1;
935         if (flags & SO_MASK)
936                 result = -result;
937 #elif defined (M68K)
938         if (upeek(pid, 4*PT_D0, &d0) < 0)
939                 return -1;
940         if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
941                 if (debug)
942                         fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
943                 return 0;
944         }
945 #elif defined (ARM)
946         if (upeek(pid, 4*0, (long *)&r0) < 0)
947                 return -1;
948         if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
949                 if (debug)
950                         fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
951                 return 0;
952         }
953 #elif defined (HPPA)
954         if (upeek(pid, PT_GR28, &r28) < 0)
955                 return -1;
956 #endif
957 #endif /* LINUX */
958         return 1;
959 }
960
961 int
962 get_error(tcp)
963 struct tcb *tcp;
964 {
965         int u_error = 0;
966 #ifdef LINUX
967 #ifdef S390
968                 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
969                         tcp->u_rval = -1;
970                         u_error = -gpr2;
971                 }
972                 else {
973                         tcp->u_rval = gpr2;
974                         u_error = 0;
975                 }
976 #else /* !S390 */
977 #ifdef I386
978                 if (eax < 0 && -eax < nerrnos) {
979                         tcp->u_rval = -1;
980                         u_error = -eax;
981                 }
982                 else {
983                         tcp->u_rval = eax;
984                         u_error = 0;
985                 }
986 #else /* !I386 */
987 #ifdef IA64
988                 if (ia32) {
989                         int err;
990
991                         err = (int)r8;
992                         if (err < 0 && -err < nerrnos) {
993                                 tcp->u_rval = -1;
994                                 u_error = -err;
995                         }
996                         else {
997                                 tcp->u_rval = err;
998                                 u_error = 0;
999                         }
1000                 } else {
1001                         if (r10) {
1002                                 tcp->u_rval = -1;
1003                                 u_error = r8;
1004                         } else {
1005                                 tcp->u_rval = r8;
1006                                 u_error = 0;
1007                         }
1008                 }
1009 #else /* !IA64 */
1010 #ifdef MIPS
1011                 if (a3) {
1012                         tcp->u_rval = -1;
1013                         u_error = r2;
1014                 } else {
1015                         tcp->u_rval = r2;
1016                         u_error = 0;
1017                 }
1018 #else
1019 #ifdef POWERPC
1020                 if (result && (unsigned) -result < nerrnos) {
1021                         tcp->u_rval = -1;
1022                         u_error = -result;
1023                 }
1024                 else {
1025                         tcp->u_rval = result;
1026                         u_error = 0;
1027                 }
1028 #else /* !POWERPC */
1029 #ifdef M68K
1030                 if (d0 && (unsigned) -d0 < nerrnos) {
1031                         tcp->u_rval = -1;
1032                         u_error = -d0;
1033                 }
1034                 else {
1035                         tcp->u_rval = d0;
1036                         u_error = 0;
1037                 }
1038 #else /* !M68K */
1039 #ifdef ARM
1040                 if (r0 && (unsigned) -r0 < nerrnos) {
1041                         tcp->u_rval = -1;
1042                         u_error = -r0;
1043                 }
1044                 else {
1045                         tcp->u_rval = r0;
1046                         u_error = 0;
1047                 }
1048 #else /* !ARM */
1049 #ifdef ALPHA
1050                 if (a3) {
1051                         tcp->u_rval = -1;
1052                         u_error = r0;
1053                 }
1054                 else {
1055                         tcp->u_rval = r0;
1056                         u_error = 0;
1057                 }
1058 #else /* !ALPHA */
1059 #ifdef SPARC
1060                 if (regs.r_psr & PSR_C) {
1061                         tcp->u_rval = -1;
1062                         u_error = regs.r_o0;
1063                 }
1064                 else {
1065                         tcp->u_rval = regs.r_o0;
1066                         u_error = 0;
1067                 }
1068 #else /* !SPARC */
1069 #ifdef HPPA
1070                 if (r28 && (unsigned) -r28 < nerrnos) {
1071                         tcp->u_rval = -1;
1072                         u_error = -r28;
1073                 }
1074                 else {
1075                         tcp->u_rval = r28;
1076                         u_error = 0;
1077                 }
1078 #endif /* HPPA */
1079 #endif /* SPARC */
1080 #endif /* ALPHA */
1081 #endif /* ARM */
1082 #endif /* M68K */
1083 #endif /* POWERPC */
1084 #endif /* MIPS */
1085 #endif /* IA64 */
1086 #endif /* I386 */
1087 #endif /* S390 */
1088 #endif /* LINUX */
1089 #ifdef SUNOS4
1090                 /* get error code from user struct */
1091                 if (upeek(pid, uoff(u_error), &u_error) < 0)
1092                         return -1;
1093                 u_error >>= 24; /* u_error is a char */
1094
1095                 /* get system call return value */
1096                 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1097                         return -1;
1098 #endif /* SUNOS4 */
1099 #ifdef SVR4
1100 #ifdef SPARC
1101                 /* Judicious guessing goes a long way. */
1102                 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1103                         tcp->u_rval = -1;
1104                         u_error = tcp->status.pr_reg[R_O0];
1105                 }
1106                 else {
1107                         tcp->u_rval = tcp->status.pr_reg[R_O0];
1108                         u_error = 0;
1109                 }
1110 #endif /* SPARC */
1111 #ifdef I386
1112                 /* Wanna know how to kill an hour single-stepping? */
1113                 if (tcp->status.PR_REG[EFL] & 0x1) {
1114                         tcp->u_rval = -1;
1115                         u_error = tcp->status.PR_REG[EAX];
1116                 }
1117                 else {
1118                         tcp->u_rval = tcp->status.PR_REG[EAX];
1119 #ifdef HAVE_LONG_LONG
1120                         tcp->u_lrval =
1121                                 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1122                                 tcp->status.PR_REG[EAX];
1123 #endif
1124                         u_error = 0;
1125                 }
1126 #endif /* I386 */
1127 #ifdef MIPS
1128                 if (tcp->status.pr_reg[CTX_A3]) {
1129                         tcp->u_rval = -1;
1130                         u_error = tcp->status.pr_reg[CTX_V0];
1131                 }
1132                 else {
1133                         tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1134                         u_error = 0;
1135                 }
1136 #endif /* MIPS */
1137 #endif /* SVR4 */
1138 #ifdef FREEBSD
1139                 if (regs.r_eflags & PSL_C) {
1140                         tcp->u_rval = -1;
1141                         u_error = regs.r_eax;
1142                 } else {
1143                         tcp->u_rval = regs.r_eax;
1144                         tcp->u_lrval =
1145                           ((unsigned long long) regs.r_edx << 32) +  regs.r_eax;
1146                         u_error = 0;
1147                 }
1148 #endif /* FREEBSD */    
1149         tcp->u_error = u_error;
1150         return 1;
1151 }
1152
1153 int syscall_enter(tcp)
1154 struct tcb *tcp;
1155 {
1156 #ifndef USE_PROCFS
1157         int pid = tcp->pid;
1158 #endif /* !USE_PROCFS */        
1159 #ifdef LINUX
1160 #if defined(S390)
1161         {
1162                 int i;
1163                 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1164                         tcp->u_nargs = sysent[tcp->scno].nargs;
1165                 else 
1166                         tcp->u_nargs = MAX_ARGS;
1167                 for (i = 0; i < tcp->u_nargs; i++) {
1168                         if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+(i<<2), &tcp->u_arg[i]) < 0)
1169                                 return -1;
1170                 }
1171         }
1172 #elif defined (ALPHA)
1173         {
1174                 int i;
1175                 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1176                         tcp->u_nargs = sysent[tcp->scno].nargs;
1177                 else 
1178                         tcp->u_nargs = MAX_ARGS;
1179                 for (i = 0; i < tcp->u_nargs; i++) {
1180                         /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1181                          * for scno somewhere above here!
1182                          */
1183                         if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1184                                 return -1;
1185                 }
1186         }
1187 #elif defined (IA64)
1188         {
1189                 unsigned long *bsp, cfm, i;
1190
1191                 if (upeek(pid, PT_AR_BSP, (long *) &bsp) < 0)
1192                         return -1;
1193                 if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1194                         return -1;
1195
1196                 bsp = ia64_rse_skip_regs(bsp, -(cfm & 0x7f));
1197
1198                 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1199                         tcp->u_nargs = sysent[tcp->scno].nargs;
1200                 else 
1201                         tcp->u_nargs = MAX_ARGS;
1202                 for (i = 0; i < tcp->u_nargs; ++i) {
1203                         if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(bsp, i), sizeof(long),
1204                                    (char *) &tcp->u_arg[i])
1205                             < 0)
1206                                 return -1;
1207                 }
1208         }
1209 #elif defined (MIPS)
1210         {
1211                 long sp;
1212                 int i, nargs;
1213
1214                 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1215                         nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1216                 else 
1217                         nargs = tcp->u_nargs = MAX_ARGS;
1218                 if(nargs > 4) {
1219                         if(upeek(pid, REG_SP, &sp) < 0)
1220                                 return -1;
1221                         for(i = 0; i < 4; i++) {
1222                                 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1223                                         return -1;
1224                         }
1225                         umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1226                                (char *)(tcp->u_arg + 4));
1227                 } else {
1228                         for(i = 0; i < nargs; i++) {
1229                                 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1230                                         return -1;
1231                         }
1232                 }
1233         }
1234 #elif defined (POWERPC)
1235         {
1236                 int i;
1237                 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1238                         tcp->u_nargs = sysent[tcp->scno].nargs;
1239                 else 
1240                         tcp->u_nargs = MAX_ARGS;
1241                 for (i = 0; i < tcp->u_nargs; i++) {
1242                         if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1243                                 return -1;
1244                 }
1245         }
1246 #elif defined (SPARC)
1247         {
1248                 int i;
1249
1250                 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1251                         tcp->u_nargs = sysent[tcp->scno].nargs;
1252                 else 
1253                         tcp->u_nargs = MAX_ARGS;
1254                 for (i = 0; i < tcp->u_nargs; i++)
1255                         tcp->u_arg[i] = *((&regs.r_o0) + i);
1256         }
1257 #elif defined (HPPA)
1258         {
1259                 int i;
1260
1261                 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1262                         tcp->u_nargs = sysent[tcp->scno].nargs;
1263                 else 
1264                         tcp->u_nargs = MAX_ARGS;
1265                 for (i = 0; i < tcp->u_nargs; i++) {
1266                         if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1267                                 return -1;
1268                 }
1269         }
1270 #else /* Other architecture (like i386) (32bits specific) */
1271         {
1272                 int i;
1273                 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1274                         tcp->u_nargs = sysent[tcp->scno].nargs;
1275                 else 
1276                         tcp->u_nargs = MAX_ARGS;
1277                 for (i = 0; i < tcp->u_nargs; i++) {
1278                         if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1279                                 return -1;
1280                 }
1281         }
1282 #endif 
1283 #endif /* LINUX */
1284 #ifdef SUNOS4
1285         {
1286                 int i;
1287                 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1288                         tcp->u_nargs = sysent[tcp->scno].nargs;
1289                 else 
1290                         tcp->u_nargs = MAX_ARGS;
1291                 for (i = 0; i < tcp->u_nargs; i++) {
1292                         struct user *u;
1293
1294                         if (upeek(pid, uoff(u_arg[0]) +
1295                             (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1296                                 return -1;
1297                 }
1298         }
1299 #endif /* SUNOS4 */
1300 #ifdef SVR4
1301 #ifdef MIPS
1302         /*
1303          * SGI is broken: even though it has pr_sysarg, it doesn't
1304          * set them on system call entry.  Get a clue.
1305          */
1306         if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1307                 tcp->u_nargs = sysent[tcp->scno].nargs;
1308         else
1309                 tcp->u_nargs = tcp->status.pr_nsysarg;
1310         if (tcp->u_nargs > 4) {
1311                 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1312                         4*sizeof(tcp->u_arg[0]));
1313                 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1314                         (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1315         }
1316         else {
1317                 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1318                         tcp->u_nargs*sizeof(tcp->u_arg[0]));
1319         }
1320 #elif UNIXWARE >= 2
1321         /*
1322          * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
1323          */
1324         if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1325                 tcp->u_nargs = sysent[tcp->scno].nargs;
1326         else
1327                 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1328         umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1329                 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1330 #elif defined (HAVE_PR_SYSCALL)
1331         if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1332                 tcp->u_nargs = sysent[tcp->scno].nargs;
1333         else
1334                 tcp->u_nargs = tcp->status.pr_nsysarg;
1335         {
1336                 int i;
1337                 for (i = 0; i < tcp->u_nargs; i++)
1338                         tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1339         }
1340 #elif defined (I386)
1341         if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1342                 tcp->u_nargs = sysent[tcp->scno].nargs;
1343         else
1344                 tcp->u_nargs = 5;
1345         umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1346                 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1347 #else
1348         I DONT KNOW WHAT TO DO
1349 #endif /* !HAVE_PR_SYSCALL */
1350 #endif /* SVR4 */
1351 #ifdef FREEBSD
1352         if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1353             sysent[tcp->scno].nargs > tcp->status.val)
1354                 tcp->u_nargs = sysent[tcp->scno].nargs;
1355         else 
1356                 tcp->u_nargs = tcp->status.val;
1357         if (tcp->u_nargs < 0)
1358                 tcp->u_nargs = 0;
1359         if (tcp->u_nargs > MAX_ARGS)
1360                 tcp->u_nargs = MAX_ARGS;
1361         switch(regs.r_eax) {
1362         case SYS___syscall:
1363                 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1364                       regs.r_esp + sizeof(int) + sizeof(quad_t));
1365           break;
1366         case SYS_syscall:
1367                 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1368                       regs.r_esp + 2 * sizeof(int));
1369           break;
1370         default:
1371                 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1372                       regs.r_esp + sizeof(int));
1373           break;
1374         }
1375 #endif /* FREEBSD */
1376         return 1;
1377 }
1378
1379 int
1380 trace_syscall(tcp)
1381 struct tcb *tcp;
1382 {
1383         int sys_res;
1384         struct timeval tv;
1385         int res;
1386
1387         /* Measure the exit time as early as possible to avoid errors. */
1388         if (dtime && (tcp->flags & TCB_INSYSCALL))
1389                 gettimeofday(&tv, NULL);
1390
1391         res = get_scno(tcp);
1392         if (res != 1)
1393                 return res;
1394
1395         res = syscall_fixup(tcp);
1396         if (res != 1)
1397                 return res;
1398
1399         if (tcp->flags & TCB_INSYSCALL) {
1400                 long u_error;
1401                 res = get_error(tcp);
1402                 if (res != 1)
1403                         return res;
1404                 u_error = tcp->u_error;
1405
1406
1407                 internal_syscall(tcp);
1408                 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
1409                     !(qual_flags[tcp->scno] & QUAL_TRACE)) {
1410                         tcp->flags &= ~TCB_INSYSCALL;
1411                         return 0;
1412                 }
1413
1414                 if (tcp->flags & TCB_REPRINT) {
1415                         printleader(tcp);
1416                         tprintf("<... ");
1417                         if (tcp->scno >= nsyscalls || tcp->scno < 0)
1418                                 tprintf("syscall_%lu", tcp->scno);
1419                         else
1420                                 tprintf("%s", sysent[tcp->scno].sys_name);
1421                         tprintf(" resumed> ");
1422                 }
1423
1424                 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
1425                         call_count[tcp->scno]++;
1426                         if (tcp->u_error)
1427                                 error_count[tcp->scno]++;
1428                         tv_sub(&tv, &tv, &tcp->etime);
1429 #ifdef LINUX
1430                         if (tv_cmp(&tv, &tcp->dtime) > 0) {
1431                                 static struct timeval one_tick =
1432                                         { 0, 1000000 / HZ };
1433
1434                                 if (tv_nz(&tcp->dtime))
1435                                         tv = tcp->dtime;
1436                                 else if (tv_cmp(&tv, &one_tick) > 0) {
1437                                         if (tv_cmp(&shortest, &one_tick) < 0)
1438                                                 tv = shortest;
1439                                         else
1440                                                 tv = one_tick;
1441                                 }
1442                         }
1443 #endif /* LINUX */
1444                         if (tv_cmp(&tv, &shortest) < 0)
1445                                 shortest = tv;
1446                         tv_add(&tv_count[tcp->scno],
1447                                 &tv_count[tcp->scno], &tv);
1448                         tcp->flags &= ~TCB_INSYSCALL;
1449                         return 0;
1450                 }
1451
1452                 if (tcp->scno >= nsyscalls || tcp->scno < 0
1453                     || (qual_flags[tcp->scno] & QUAL_RAW))
1454                         sys_res = printargs(tcp);
1455                 else
1456                         sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1457                 u_error = tcp->u_error;
1458                 tprintf(") ");
1459                 tabto(acolumn);
1460                 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
1461                     qual_flags[tcp->scno] & QUAL_RAW) {
1462                         if (u_error)
1463                                 tprintf("= -1 (errno %ld)", u_error);
1464                         else
1465                                 tprintf("= %#lx", tcp->u_rval);
1466                 }
1467                 else if (!(sys_res & RVAL_NONE) && u_error) {
1468                         switch (u_error) {
1469 #ifdef LINUX
1470                         case ERESTARTSYS:
1471                                 tprintf("= ? ERESTARTSYS (To be restarted)");
1472                                 break;
1473                         case ERESTARTNOINTR:
1474                                 tprintf("= ? ERESTARTNOINTR (To be restarted)");
1475                                 break;
1476                         case ERESTARTNOHAND:
1477                                 tprintf("= ? ERESTARTNOHAND (To be restarted)");
1478                                 break;
1479 #endif /* LINUX */
1480                         default:
1481                                 tprintf("= -1 ");
1482                                 if (u_error < nerrnos && u_error < sys_nerr)
1483                                         tprintf("%s (%s)", errnoent[u_error],
1484                                                 sys_errlist[u_error]);
1485                                 else if (u_error < nerrnos)
1486                                         tprintf("%s (errno %ld)",
1487                                                 errnoent[u_error], u_error);
1488                                 else if (u_error < sys_nerr)
1489                                         tprintf("ERRNO_%ld (%s)", u_error,
1490                                                 sys_errlist[u_error]);
1491                                 else
1492                                         tprintf("E??? (errno %ld)", u_error);
1493                                 break;
1494                         }
1495                 }
1496                 else {
1497                         if (sys_res & RVAL_NONE)
1498                                 tprintf("= ?");
1499                         else {
1500                                 switch (sys_res & RVAL_MASK) {
1501                                 case RVAL_HEX:
1502                                         tprintf("= %#lx", tcp->u_rval);
1503                                         break;
1504                                 case RVAL_OCTAL:
1505                                         tprintf("= %#lo", tcp->u_rval);
1506                                         break;
1507                                 case RVAL_UDECIMAL:
1508                                         tprintf("= %lu", tcp->u_rval);
1509                                         break;
1510                                 case RVAL_DECIMAL:
1511                                         tprintf("= %ld", tcp->u_rval);
1512                                         break;
1513 #ifdef HAVE_LONG_LONG
1514                                 case RVAL_LHEX:
1515                                         tprintf("= %#llx", tcp->u_lrval);
1516                                         break;
1517                                 case RVAL_LOCTAL:
1518                                         tprintf("= %#llo", tcp->u_lrval);
1519                                         break;
1520                                 case RVAL_LUDECIMAL:
1521                                         tprintf("= %llu", tcp->u_lrval);
1522                                         break;
1523                                 case RVAL_LDECIMAL:
1524                                         tprintf("= %lld", tcp->u_lrval);
1525                                         break;
1526 #endif
1527                                 default:
1528                                         fprintf(stderr,
1529                                                 "invalid rval format\n");
1530                                         break;
1531                                 }
1532                         }
1533                         if ((sys_res & RVAL_STR) && tcp->auxstr)
1534                                 tprintf(" (%s)", tcp->auxstr);
1535                 }
1536                 if (dtime) {
1537                         tv_sub(&tv, &tv, &tcp->etime);
1538                         tprintf(" <%ld.%06ld>",
1539                                 (long) tv.tv_sec, (long) tv.tv_usec);
1540                 }
1541                 printtrailer(tcp);
1542
1543                 dumpio(tcp);
1544                 if (fflush(tcp->outf) == EOF)
1545                         return -1;
1546                 tcp->flags &= ~TCB_INSYSCALL;
1547                 return 0;
1548         }
1549
1550         /* Entering system call */
1551         res = syscall_enter(tcp);
1552         if (res != 1)
1553                 return res;
1554
1555         switch (tcp->scno + NR_SYSCALL_BASE) {
1556 #ifdef LINUX
1557 #if !defined (ALPHA) && !defined(IA64) && !defined(SPARC) && !defined(MIPS) && !defined(HPPA)
1558         case SYS_socketcall:
1559                 decode_subcall(tcp, SYS_socket_subcall,
1560                         SYS_socket_nsubcalls, deref_style);
1561                 break;
1562         case SYS_ipc:
1563                 decode_subcall(tcp, SYS_ipc_subcall,
1564                         SYS_ipc_nsubcalls, shift_style);
1565                 break;
1566 #endif /* !ALPHA && !IA64 && !MIPS && !SPARC */
1567 #ifdef SPARC
1568         case SYS_socketcall:
1569                 sparc_socket_decode (tcp);
1570                 break;
1571 #endif
1572 #endif /* LINUX */
1573 #ifdef SVR4
1574 #ifdef SYS_pgrpsys_subcall
1575         case SYS_pgrpsys:
1576                 decode_subcall(tcp, SYS_pgrpsys_subcall,
1577                         SYS_pgrpsys_nsubcalls, shift_style);
1578                 break;
1579 #endif /* SYS_pgrpsys_subcall */
1580 #ifdef SYS_sigcall_subcall
1581         case SYS_sigcall:
1582                 decode_subcall(tcp, SYS_sigcall_subcall,
1583                         SYS_sigcall_nsubcalls, mask_style);
1584                 break;
1585 #endif /* SYS_sigcall_subcall */
1586         case SYS_msgsys:
1587                 decode_subcall(tcp, SYS_msgsys_subcall,
1588                         SYS_msgsys_nsubcalls, shift_style);
1589                 break;
1590         case SYS_shmsys:
1591                 decode_subcall(tcp, SYS_shmsys_subcall,
1592                         SYS_shmsys_nsubcalls, shift_style);
1593                 break;
1594         case SYS_semsys:
1595                 decode_subcall(tcp, SYS_semsys_subcall,
1596                         SYS_semsys_nsubcalls, shift_style);
1597                 break;
1598 #if 0 /* broken */
1599         case SYS_utssys:
1600                 decode_subcall(tcp, SYS_utssys_subcall,
1601                         SYS_utssys_nsubcalls, shift_style);
1602                 break;
1603 #endif
1604         case SYS_sysfs:
1605                 decode_subcall(tcp, SYS_sysfs_subcall,
1606                         SYS_sysfs_nsubcalls, shift_style);
1607                 break;
1608         case SYS_spcall:
1609                 decode_subcall(tcp, SYS_spcall_subcall,
1610                         SYS_spcall_nsubcalls, shift_style);
1611                 break;
1612 #ifdef SYS_context_subcall
1613         case SYS_context:
1614                 decode_subcall(tcp, SYS_context_subcall,
1615                         SYS_context_nsubcalls, shift_style);
1616                 break;
1617 #endif /* SYS_context_subcall */
1618 #ifdef SYS_door_subcall
1619         case SYS_door:
1620                 decode_subcall(tcp, SYS_door_subcall,
1621                         SYS_door_nsubcalls, door_style);
1622                 break;
1623 #endif /* SYS_door_subcall */
1624 #ifdef SYS_kaio_subcall
1625         case SYS_kaio:
1626                 decode_subcall(tcp, SYS_kaio_subcall,
1627                         SYS_kaio_nsubcalls, shift_style);
1628                 break;
1629 #endif  
1630 #endif /* SVR4 */
1631 #ifdef FREEBSD
1632         case SYS_msgsys:
1633         case SYS_shmsys:
1634         case SYS_semsys:
1635                 decode_subcall(tcp, 0, 0, table_style);
1636                 break;
1637 #endif          
1638 #ifdef SUNOS4
1639         case SYS_semsys:
1640                 decode_subcall(tcp, SYS_semsys_subcall,
1641                         SYS_semsys_nsubcalls, shift_style);
1642                 break;
1643         case SYS_msgsys:
1644                 decode_subcall(tcp, SYS_msgsys_subcall,
1645                         SYS_msgsys_nsubcalls, shift_style);
1646                 break;
1647         case SYS_shmsys:
1648                 decode_subcall(tcp, SYS_shmsys_subcall,
1649                         SYS_shmsys_nsubcalls, shift_style);
1650                 break;
1651 #endif
1652         }
1653
1654         internal_syscall(tcp);
1655         if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
1656                 tcp->flags |= TCB_INSYSCALL;
1657                 return 0;
1658         }
1659
1660         if (cflag) {
1661                 gettimeofday(&tcp->etime, NULL);
1662                 tcp->flags |= TCB_INSYSCALL;
1663                 return 0;
1664         }
1665
1666         printleader(tcp);
1667         tcp->flags &= ~TCB_REPRINT;
1668         tcp_last = tcp;
1669         if (tcp->scno >= nsyscalls || tcp->scno < 0)
1670                 tprintf("syscall_%lu(", tcp->scno);
1671         else
1672                 tprintf("%s(", sysent[tcp->scno].sys_name);
1673         if (tcp->scno >= nsyscalls || tcp->scno < 0 || 
1674             ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1675                 sys_res = printargs(tcp);
1676         else
1677                 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1678         if (fflush(tcp->outf) == EOF)
1679                 return -1;
1680         tcp->flags |= TCB_INSYSCALL;
1681         /* Measure the entrance time as late as possible to avoid errors. */
1682         if (dtime)
1683                 gettimeofday(&tcp->etime, NULL);
1684         return sys_res;
1685 }
1686
1687 int
1688 printargs(tcp)
1689 struct tcb *tcp;
1690 {
1691         if (entering(tcp)) {
1692                 int i;
1693
1694                 for (i = 0; i < tcp->u_nargs; i++)
1695                         tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1696         }
1697         return 0;
1698 }
1699
1700 long
1701 getrval2(tcp)
1702 struct tcb *tcp;
1703 {
1704         long val = -1;
1705
1706 #ifdef LINUX
1707 #ifdef SPARC
1708         struct regs regs;
1709         if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
1710                 return -1;
1711         val = regs.r_o1;
1712 #endif /* SPARC */
1713 #endif /* LINUX */
1714
1715 #ifdef SUNOS4
1716         if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1717                 return -1;
1718 #endif /* SUNOS4 */
1719
1720 #ifdef SVR4
1721 #ifdef SPARC
1722         val = tcp->status.PR_REG[R_O1];
1723 #endif /* SPARC */
1724 #ifdef I386
1725         val = tcp->status.PR_REG[EDX];
1726 #endif /* I386 */
1727 #ifdef MIPS
1728         val = tcp->status.PR_REG[CTX_V1];
1729 #endif /* MIPS */
1730 #endif /* SVR4 */
1731 #ifdef FREEBSD
1732         struct reg regs;
1733         pread(tcp->pfd_reg, &regs, sizeof(regs), 0);
1734         val = regs.r_edx;
1735 #endif  
1736         return val;
1737 }
1738
1739 /*
1740  * Apparently, indirect system calls have already be converted by ptrace(2),
1741  * so if you see "indir" this program has gone astray.
1742  */
1743 int
1744 sys_indir(tcp)
1745 struct tcb *tcp;
1746 {
1747         int i, scno, nargs;
1748
1749         if (entering(tcp)) {
1750                 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1751                         fprintf(stderr, "Bogus syscall: %u\n", scno);
1752                         return 0;
1753                 }
1754                 nargs = sysent[scno].nargs;
1755                 tprintf("%s", sysent[scno].sys_name);
1756                 for (i = 0; i < nargs; i++)
1757                         tprintf(", %#lx", tcp->u_arg[i+1]);
1758         }
1759         return 0;
1760 }
1761
1762 static int
1763 time_cmp(a, b)
1764 void *a;
1765 void *b;
1766 {
1767         return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1768 }
1769
1770 static int
1771 syscall_cmp(a, b)
1772 void *a;
1773 void *b;
1774 {
1775         return strcmp(sysent[*((int *) a)].sys_name,
1776                 sysent[*((int *) b)].sys_name);
1777 }
1778
1779 static int
1780 count_cmp(a, b)
1781 void *a;
1782 void *b;
1783 {
1784         int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1785
1786         return (m < n) ? 1 : (m > n) ? -1 : 0;
1787 }
1788
1789 static int (*sortfun)();
1790 static struct timeval overhead = { -1, -1 };
1791
1792 void
1793 set_sortby(sortby)
1794 char *sortby;
1795 {
1796         if (strcmp(sortby, "time") == 0)
1797                 sortfun = time_cmp;
1798         else if (strcmp(sortby, "calls") == 0)
1799                 sortfun = count_cmp;
1800         else if (strcmp(sortby, "name") == 0)
1801                 sortfun = syscall_cmp;
1802         else if (strcmp(sortby, "nothing") == 0)
1803                 sortfun = NULL;
1804         else {
1805                 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1806                 exit(1);
1807         }
1808 }
1809
1810 void set_overhead(n)
1811 int n;
1812 {
1813         overhead.tv_sec = n / 1000000;
1814         overhead.tv_usec = n % 1000000;
1815 }
1816
1817 void
1818 call_summary(outf)
1819 FILE *outf;
1820 {
1821         int i, j;
1822         int call_cum, error_cum;
1823         struct timeval tv_cum, dtv;
1824         double percent;
1825         char *dashes = "-------------------------";
1826         char error_str[16];
1827
1828         call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1829         if (overhead.tv_sec == -1) {
1830                 tv_mul(&overhead, &shortest, 8);
1831                 tv_div(&overhead, &overhead, 10);
1832         }
1833         for (i = 0; i < nsyscalls; i++) {
1834                 sorted_count[i] = i;
1835                 if (call_count[i] == 0)
1836                         continue;
1837                 tv_mul(&dtv, &overhead, call_count[i]);
1838                 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1839                 call_cum += call_count[i];
1840                 error_cum += error_count[i];
1841                 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1842         }
1843         if (sortfun)
1844                 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1845         fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1846                 "% time", "seconds", "usecs/call",
1847                 "calls", "errors", "syscall");
1848         fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1849                 dashes, dashes, dashes, dashes, dashes, dashes);
1850         for (i = 0; i < nsyscalls; i++) {
1851                 j = sorted_count[i];
1852                 if (call_count[j] == 0)
1853                         continue;
1854                 tv_div(&dtv, &tv_count[j], call_count[j]);
1855                 if (error_count[j])
1856                         sprintf(error_str, "%d", error_count[j]);
1857                 else
1858                         error_str[0] = '\0';
1859                 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1860                 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1861                         percent, (long) tv_count[j].tv_sec,
1862                         (long) tv_count[j].tv_usec,
1863                         (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1864                         call_count[j], error_str, sysent[j].sys_name);
1865         }
1866         fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1867                 dashes, dashes, dashes, dashes, dashes, dashes);
1868         if (error_cum)
1869                 sprintf(error_str, "%d", error_cum);
1870         else
1871                 error_str[0] = '\0';
1872         fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1873                 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1874                 call_cum, error_str, "total");
1875 }