]> granicus.if.org Git - strace/blob - syscall.c
2004-08-31 Roland McGrath <roland@redhat.com>
[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 #if defined (SPARC) || defined (SPARC64)
47 #  define fpq kernel_fpq
48 #  define fq kernel_fq
49 #  define fpu kernel_fpu
50 #endif
51 #include <asm/reg.h>
52 #if defined (SPARC) || defined (SPARC64)
53 #  undef fpq
54 #  undef fq
55 #  undef fpu
56 #endif
57 #endif
58
59 #ifdef HAVE_SYS_REG_H
60 #include <sys/reg.h>
61 #ifndef PTRACE_PEEKUSR
62 # define PTRACE_PEEKUSR PTRACE_PEEKUSER
63 #endif
64 #elif defined(HAVE_LINUX_PTRACE_H)
65 #undef PTRACE_SYSCALL
66 # ifdef HAVE_STRUCT_IA64_FPREG
67 #  define ia64_fpreg XXX_ia64_fpreg
68 # endif
69 # ifdef HAVE_STRUCT_PT_ALL_USER_REGS
70 #  define pt_all_user_regs XXX_pt_all_user_regs
71 # endif
72 #include <linux/ptrace.h>
73 # undef ia64_fpreg
74 # undef pt_all_user_regs
75 #endif
76
77 #if defined (LINUX) && defined (SPARC64)
78 # define r_pc r_tpc
79 # undef PTRACE_GETREGS
80 # define PTRACE_GETREGS PTRACE_GETREGS64
81 # undef PTRACE_SETREGS
82 # define PTRACE_SETREGS PTRACE_SETREGS64
83 #endif /* LINUX && SPARC64 */
84
85 #if defined(LINUX) && defined(IA64)
86 # include <asm/ptrace_offsets.h>
87 # include <asm/rse.h>
88 #endif
89
90 #define NR_SYSCALL_BASE 0
91 #ifdef LINUX
92 #ifndef ERESTARTSYS
93 #define ERESTARTSYS     512
94 #endif
95 #ifndef ERESTARTNOINTR
96 #define ERESTARTNOINTR  513
97 #endif
98 #ifndef ERESTARTNOHAND
99 #define ERESTARTNOHAND  514     /* restart if no handler.. */
100 #endif
101 #ifndef ENOIOCTLCMD
102 #define ENOIOCTLCMD     515     /* No ioctl command */
103 #endif
104 #ifndef ERESTART_RESTARTBLOCK
105 #define ERESTART_RESTARTBLOCK 516       /* restart by calling sys_restart_syscall */
106 #endif
107 #ifndef NSIG
108 #define NSIG 32
109 #endif
110 #ifdef ARM
111 #undef NSIG
112 #define NSIG 32
113 #undef NR_SYSCALL_BASE
114 #define NR_SYSCALL_BASE __NR_SYSCALL_BASE
115 #endif
116 #endif /* LINUX */
117
118 #include "syscall.h"
119
120 /* Define these shorthand notations to simplify the syscallent files. */
121 #define TF TRACE_FILE
122 #define TI TRACE_IPC
123 #define TN TRACE_NETWORK
124 #define TP TRACE_PROCESS
125 #define TS TRACE_SIGNAL
126
127 struct sysent sysent0[] = {
128 #include "syscallent.h"
129 };
130 int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
131
132 #if SUPPORTED_PERSONALITIES >= 2
133 struct sysent sysent1[] = {
134 #include "syscallent1.h"
135 };
136 int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
137 #endif /* SUPPORTED_PERSONALITIES >= 2 */
138
139 #if SUPPORTED_PERSONALITIES >= 3
140 struct sysent sysent2[] = {
141 #include "syscallent2.h"
142 };
143 int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
144 #endif /* SUPPORTED_PERSONALITIES >= 3 */
145
146 struct sysent *sysent;
147 int nsyscalls;
148
149 /* Now undef them since short defines cause wicked namespace pollution. */
150 #undef TF
151 #undef TI
152 #undef TN
153 #undef TP
154 #undef TS
155
156 char *errnoent0[] = {
157 #include "errnoent.h"
158 };
159 int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
160
161 #if SUPPORTED_PERSONALITIES >= 2
162 char *errnoent1[] = {
163 #include "errnoent1.h"
164 };
165 int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
166 #endif /* SUPPORTED_PERSONALITIES >= 2 */
167
168 #if SUPPORTED_PERSONALITIES >= 3
169 char *errnoent2[] = {
170 #include "errnoent2.h"
171 };
172 int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
173 #endif /* SUPPORTED_PERSONALITIES >= 3 */
174
175 char **errnoent;
176 int nerrnos;
177
178 int current_personality;
179
180 int
181 set_personality(personality)
182 int personality;
183 {
184         switch (personality) {
185         case 0:
186                 errnoent = errnoent0;
187                 nerrnos = nerrnos0;
188                 sysent = sysent0;
189                 nsyscalls = nsyscalls0;
190                 ioctlent = ioctlent0;
191                 nioctlents = nioctlents0;
192                 signalent = signalent0;
193                 nsignals = nsignals0;
194                 break;
195
196 #if SUPPORTED_PERSONALITIES >= 2
197         case 1:
198                 errnoent = errnoent1;
199                 nerrnos = nerrnos1;
200                 sysent = sysent1;
201                 nsyscalls = nsyscalls1;
202                 ioctlent = ioctlent1;
203                 nioctlents = nioctlents1;
204                 signalent = signalent1;
205                 nsignals = nsignals1;
206                 break;
207 #endif /* SUPPORTED_PERSONALITIES >= 2 */
208
209 #if SUPPORTED_PERSONALITIES >= 3
210         case 2:
211                 errnoent = errnoent2;
212                 nerrnos = nerrnos2;
213                 sysent = sysent2;
214                 nsyscalls = nsyscalls2;
215                 ioctlent = ioctlent2;
216                 nioctlents = nioctlents2;
217                 signalent = signalent2;
218                 nsignals = nsignals2;
219                 break;
220 #endif /* SUPPORTED_PERSONALITIES >= 3 */
221
222         default:
223                 return -1;
224         }
225
226         current_personality = personality;
227         return 0;
228 }
229
230 int qual_flags[MAX_QUALS];
231
232 static int call_count[MAX_QUALS];
233 static int error_count[MAX_QUALS];
234 static struct timeval tv_count[MAX_QUALS];
235 static int sorted_count[MAX_QUALS];
236
237 static struct timeval shortest = { 1000000, 0 };
238
239 static int qual_syscall(), qual_signal(), qual_fault(), qual_desc();
240
241 static struct qual_options {
242         int bitflag;
243         char *option_name;
244         int (*qualify)();
245         char *argument_name;
246 } qual_options[] = {
247         { QUAL_TRACE,   "trace",        qual_syscall,   "system call"   },
248         { QUAL_TRACE,   "t",            qual_syscall,   "system call"   },
249         { QUAL_ABBREV,  "abbrev",       qual_syscall,   "system call"   },
250         { QUAL_ABBREV,  "a",            qual_syscall,   "system call"   },
251         { QUAL_VERBOSE, "verbose",      qual_syscall,   "system call"   },
252         { QUAL_VERBOSE, "v",            qual_syscall,   "system call"   },
253         { QUAL_RAW,     "raw",          qual_syscall,   "system call"   },
254         { QUAL_RAW,     "x",            qual_syscall,   "system call"   },
255         { QUAL_SIGNAL,  "signal",       qual_signal,    "signal"        },
256         { QUAL_SIGNAL,  "signals",      qual_signal,    "signal"        },
257         { QUAL_SIGNAL,  "s",            qual_signal,    "signal"        },
258         { QUAL_FAULT,   "fault",        qual_fault,     "fault"         },
259         { QUAL_FAULT,   "faults",       qual_fault,     "fault"         },
260         { QUAL_FAULT,   "m",            qual_fault,     "fault"         },
261         { QUAL_READ,    "read",         qual_desc,      "descriptor"    },
262         { QUAL_READ,    "reads",        qual_desc,      "descriptor"    },
263         { QUAL_READ,    "r",            qual_desc,      "descriptor"    },
264         { QUAL_WRITE,   "write",        qual_desc,      "descriptor"    },
265         { QUAL_WRITE,   "writes",       qual_desc,      "descriptor"    },
266         { QUAL_WRITE,   "w",            qual_desc,      "descriptor"    },
267         { 0,            NULL,           NULL,           NULL            },
268 };
269
270 static void
271 qualify_one(n, opt, not)
272         int n;
273         struct qual_options *opt;
274         int not;
275 {
276         if (not)
277                 qual_flags[n] &= ~opt->bitflag;
278         else
279                 qual_flags[n] |= opt->bitflag;
280 }
281
282 static int
283 qual_syscall(s, opt, not)
284         char *s;
285         struct qual_options *opt;
286         int not;
287 {
288         int i;
289         int any = 0;
290
291         for (i = 0; i < nsyscalls; i++) {
292                 if (strcmp(s, sysent[i].sys_name) == 0) {
293                         qualify_one(i, opt, not);
294                         any = 1;
295                 }
296         }
297         return !any;
298 }
299
300 static int
301 qual_signal(s, opt, not)
302         char *s;
303         struct qual_options *opt;
304         int not;
305 {
306         int i;
307         char buf[32];
308
309         if (s && *s && isdigit((unsigned char)*s)) {
310                 qualify_one(atoi(s), opt, not);
311                 return 1;
312         }
313         if (strlen(s) >= sizeof buf)
314                 return 0;
315         strcpy(buf, s);
316         s = buf;
317         for (i = 0; s[i]; i++)
318                 s[i] = toupper((unsigned char)(s[i]));
319         if (strncmp(s, "SIG", 3) == 0)
320                 s += 3;
321         for (i = 0; i <= NSIG; i++)
322                 if (strcmp(s, signame(i) + 3) == 0) {
323                         qualify_one(atoi(s), opt, not);
324                         return 1;
325                 }
326         return 0;
327 }
328
329 static int
330 qual_fault(s, opt, not)
331         char *s;
332         struct qual_options *opt;
333         int not;
334 {
335         return -1;
336 }
337
338 static int
339 qual_desc(s, opt, not)
340         char *s;
341         struct qual_options *opt;
342         int not;
343 {
344         if (s && *s && isdigit((unsigned char)*s)) {
345                 qualify_one(atoi(s), opt, not);
346                 return 0;
347         }
348         return -1;
349 }
350
351 static int
352 lookup_class(s)
353         char *s;
354 {
355         if (strcmp(s, "file") == 0)
356                 return TRACE_FILE;
357         if (strcmp(s, "ipc") == 0)
358                 return TRACE_IPC;
359         if (strcmp(s, "network") == 0)
360                 return TRACE_NETWORK;
361         if (strcmp(s, "process") == 0)
362                 return TRACE_PROCESS;
363         if (strcmp(s, "signal") == 0)
364                 return TRACE_SIGNAL;
365         return -1;
366 }
367
368 void
369 qualify(s)
370 char *s;
371 {
372         struct qual_options *opt;
373         int not;
374         char *p;
375         int i, n;
376
377         opt = &qual_options[0];
378         for (i = 0; (p = qual_options[i].option_name); i++) {
379                 n = strlen(p);
380                 if (strncmp(s, p, n) == 0 && s[n] == '=') {
381                         opt = &qual_options[i];
382                         s += n + 1;
383                         break;
384                 }
385         }
386         not = 0;
387         if (*s == '!') {
388                 not = 1;
389                 s++;
390         }
391         if (strcmp(s, "none") == 0) {
392                 not = 1 - not;
393                 s = "all";
394         }
395         if (strcmp(s, "all") == 0) {
396                 for (i = 0; i < MAX_QUALS; i++) {
397                         if (not)
398                                 qual_flags[i] &= ~opt->bitflag;
399                         else
400                                 qual_flags[i] |= opt->bitflag;
401                 }
402                 return;
403         }
404         for (i = 0; i < MAX_QUALS; i++) {
405                 if (not)
406                         qual_flags[i] |= opt->bitflag;
407                 else
408                         qual_flags[i] &= ~opt->bitflag;
409         }
410         for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
411                 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
412                         for (i = 0; i < MAX_QUALS; i++) {
413                                 if (sysent[i].sys_flags & n) {
414                                         if (not)
415                                                 qual_flags[i] &= ~opt->bitflag;
416                                         else
417                                                 qual_flags[i] |= opt->bitflag;
418                                 }
419                         }
420                         continue;
421                 }
422                 if (opt->qualify(p, opt, not)) {
423                         fprintf(stderr, "strace: invalid %s `%s'\n",
424                                 opt->argument_name, p);
425                         exit(1);
426                 }
427         }
428         return;
429 }
430
431 static void
432 dumpio(tcp)
433 struct tcb *tcp;
434 {
435         if (syserror(tcp))
436                 return;
437         if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
438                 return;
439         switch (tcp->scno + NR_SYSCALL_BASE) {
440         case SYS_read:
441 #ifdef SYS_pread64
442         case SYS_pread64:
443 #endif
444 #if defined SYS_pread && SYS_pread64 != SYS_pread
445         case SYS_pread:
446 #endif
447 #ifdef SYS_recv
448         case SYS_recv:
449 #endif
450 #ifdef SYS_recvfrom
451         case SYS_recvfrom:
452 #endif
453                 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
454                         dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
455                 break;
456         case SYS_write:
457 #ifdef SYS_pwrite64
458         case SYS_pwrite64:
459 #endif
460 #if defined SYS_pwrite && SYS_pwrite64 != SYS_pwrite
461         case SYS_pwrite:
462 #endif
463 #ifdef SYS_send
464         case SYS_send:
465 #endif
466 #ifdef SYS_sendto
467         case SYS_sendto:
468 #endif
469                 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
470                         dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
471                 break;
472 #ifdef SYS_readv
473         case SYS_readv:
474                 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
475                         dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
476                 break;
477 #endif
478 #ifdef SYS_writev
479         case SYS_writev:
480
481                 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
482                         dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
483                 break;
484 #endif
485         }
486 }
487
488 #ifndef FREEBSD
489 enum subcall_style { shift_style, deref_style, mask_style, door_style };
490 #else /* FREEBSD */
491 enum subcall_style { shift_style, deref_style, mask_style, door_style, table_style };
492
493 struct subcall {
494   int call;
495   int nsubcalls;
496   int subcalls[5];
497 };
498
499 const struct subcall subcalls_table[] = {
500   { SYS_shmsys, 5, { SYS_shmat, SYS_shmctl, SYS_shmdt, SYS_shmget, SYS_shmctl } },
501 #ifdef SYS_semconfig
502   { SYS_semsys, 4, { SYS___semctl, SYS_semget, SYS_semop, SYS_semconfig } },
503 #else
504   { SYS_semsys, 3, { SYS___semctl, SYS_semget, SYS_semop } },
505 #endif
506   { SYS_msgsys, 4, { SYS_msgctl, SYS_msgget, SYS_msgsnd, SYS_msgrcv } },
507 };
508 #endif /* FREEBSD */
509
510 #if !(defined(LINUX) && ( defined(ALPHA) || defined(MIPS) ))
511
512 const int socket_map [] = {
513                /* SYS_SOCKET      */ 97,
514                /* SYS_BIND        */ 104,
515                /* SYS_CONNECT     */ 98,
516                /* SYS_LISTEN      */ 106,
517                /* SYS_ACCEPT      */ 99,
518                /* SYS_GETSOCKNAME */ 150,
519                /* SYS_GETPEERNAME */ 141,
520                /* SYS_SOCKETPAIR  */ 135,
521                /* SYS_SEND        */ 101,
522                /* SYS_RECV        */ 102,
523                /* SYS_SENDTO      */ 133,
524                /* SYS_RECVFROM    */ 125,
525                /* SYS_SHUTDOWN    */ 134,
526                /* SYS_SETSOCKOPT  */ 105,
527                /* SYS_GETSOCKOPT  */ 118,
528                /* SYS_SENDMSG     */ 114,
529                /* SYS_RECVMSG     */ 113
530 };
531
532 void
533 sparc_socket_decode (tcp)
534 struct tcb *tcp;
535 {
536         volatile long addr;
537         volatile int i, n;
538
539         if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
540                 return;
541         }
542         tcp->scno = socket_map [tcp->u_arg [0]-1];
543         n = tcp->u_nargs = sysent [tcp->scno].nargs;
544         addr = tcp->u_arg [1];
545         for (i = 0; i < n; i++){
546                 int arg;
547                 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
548                         arg = 0;
549                 tcp->u_arg [i] = arg;
550                 addr += sizeof (arg);
551         }
552 }
553
554 void
555 decode_subcall(tcp, subcall, nsubcalls, style)
556 struct tcb *tcp;
557 int subcall;
558 int nsubcalls;
559 enum subcall_style style;
560 {
561         long addr, mask, arg;
562         int i;
563
564         switch (style) {
565         case shift_style:
566                 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
567                         return;
568                 tcp->scno = subcall + tcp->u_arg[0];
569                 if (sysent[tcp->scno].nargs != -1)
570                         tcp->u_nargs = sysent[tcp->scno].nargs;
571                 else
572                         tcp->u_nargs--;
573                 for (i = 0; i < tcp->u_nargs; i++)
574                         tcp->u_arg[i] = tcp->u_arg[i + 1];
575                 break;
576         case deref_style:
577                 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
578                         return;
579                 tcp->scno = subcall + tcp->u_arg[0];
580                 addr = tcp->u_arg[1];
581                 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
582                         if (umove(tcp, addr, &arg) < 0)
583                                 arg = 0;
584                         tcp->u_arg[i] = arg;
585                         addr += sizeof(arg);
586                 }
587                 tcp->u_nargs = sysent[tcp->scno].nargs;
588                 break;
589         case mask_style:
590                 mask = (tcp->u_arg[0] >> 8) & 0xff;
591                 for (i = 0; mask; i++)
592                         mask >>= 1;
593                 if (i >= nsubcalls)
594                         return;
595                 tcp->u_arg[0] &= 0xff;
596                 tcp->scno = subcall + i;
597                 if (sysent[tcp->scno].nargs != -1)
598                         tcp->u_nargs = sysent[tcp->scno].nargs;
599                 break;
600         case door_style:
601                 /*
602                  * Oh, yuck.  The call code is the *sixth* argument.
603                  * (don't you mean the *last* argument? - JH)
604                  */
605                 if (tcp->u_arg[5] < 0 || tcp->u_arg[5] >= nsubcalls)
606                         return;
607                 tcp->scno = subcall + tcp->u_arg[5];
608                 if (sysent[tcp->scno].nargs != -1)
609                         tcp->u_nargs = sysent[tcp->scno].nargs;
610                 else
611                         tcp->u_nargs--;
612                 break;
613 #ifdef FREEBSD
614         case table_style:
615                 for (i = 0; i < sizeof(subcalls_table) / sizeof(struct subcall); i++)
616                         if (subcalls_table[i].call == tcp->scno) break;
617                 if (i < sizeof(subcalls_table) / sizeof(struct subcall) &&
618                     tcp->u_arg[0] >= 0 && tcp->u_arg[0] < subcalls_table[i].nsubcalls) {
619                         tcp->scno = subcalls_table[i].subcalls[tcp->u_arg[0]];
620                         for (i = 0; i < tcp->u_nargs; i++)
621                                 tcp->u_arg[i] = tcp->u_arg[i + 1];
622                 }
623                 break;
624 #endif /* FREEBSD */
625         }
626 }
627 #endif
628
629 struct tcb *tcp_last = NULL;
630
631 static int
632 internal_syscall(tcp)
633 struct tcb *tcp;
634 {
635         /*
636          * We must always trace a few critical system calls in order to
637          * correctly support following forks in the presence of tracing
638          * qualifiers.
639          */
640         switch (tcp->scno + NR_SYSCALL_BASE) {
641 #ifdef SYS_fork
642         case SYS_fork:
643 #endif
644 #ifdef SYS_vfork
645         case SYS_vfork:
646 #endif
647 #ifdef SYS_fork1
648         case SYS_fork1:
649 #endif
650 #ifdef SYS_forkall
651         case SYS_forkall:
652 #endif
653 #ifdef SYS_rfork1
654         case SYS_rfork1:
655 #endif
656 #ifdef SYS_rforkall
657         case SYS_rforkall:
658 #endif
659 #ifdef SYS_rfork
660         case SYS_rfork:
661 #endif
662                 internal_fork(tcp);
663                 break;
664 #ifdef SYS_clone
665         case SYS_clone:
666                 internal_clone(tcp);
667                 break;
668 #endif
669 #ifdef SYS_clone2
670         case SYS_clone2:
671                 internal_clone(tcp);
672                 break;
673 #endif
674 #ifdef SYS_execv
675         case SYS_execv:
676 #endif
677 #ifdef SYS_execve
678         case SYS_execve:
679 #endif
680 #ifdef SYS_rexecve
681         case SYS_rexecve:
682 #endif
683                 internal_exec(tcp);
684                 break;
685
686 #ifdef SYS_wait
687         case SYS_wait:
688 #endif
689 #ifdef SYS_wait4
690         case SYS_wait4:
691 #endif
692 #ifdef SYS32_wait4
693         case SYS32_wait4:
694 #endif
695 #ifdef SYS_waitpid
696         case SYS_waitpid:
697 #endif
698 #ifdef SYS_waitsys
699         case SYS_waitsys:
700 #endif
701                 internal_wait(tcp);
702                 break;
703
704 #ifdef SYS_exit
705         case SYS_exit:
706 #endif
707 #ifdef SYS32_exit
708         case SYS32_exit:
709 #endif
710 #ifdef __NR_exit_group
711         case __NR_exit_group:
712 #endif
713 #ifdef IA64
714         case 252: /* IA-32 __NR_exit_group */
715 #endif
716                 internal_exit(tcp);
717                 break;
718         }
719         return 0;
720 }
721
722
723 #ifdef LINUX
724 #if defined (I386)
725         static long eax;
726 #elif defined (IA64)
727         long r8, r10, psr;
728         long ia32 = 0;
729 #elif defined (POWERPC)
730         static long result,flags;
731 #elif defined (M68K)
732         static int d0;
733 #elif defined (ARM)
734         static struct pt_regs regs;
735 #elif defined (ALPHA)
736         static long r0;
737         static long a3;
738 #elif defined (SPARC) || defined (SPARC64)
739         static struct regs regs;
740         static unsigned long trap;
741 #elif defined(MIPS)
742         static long a3;
743         static long r2;
744 #elif defined(S390) || defined(S390X)
745         static long gpr2;
746         static long pc;
747         static long syscall_mode;
748 #elif defined(HPPA)
749         static long r28;
750 #elif defined(SH)
751        static long r0;
752 #elif defined(SH64)
753        static long r9;
754 #elif defined(X86_64)
755        static long rax;
756 #endif
757 #endif /* LINUX */
758 #ifdef FREEBSD
759         struct reg regs;
760 #endif /* FREEBSD */
761
762 int
763 get_scno(tcp)
764 struct tcb *tcp;
765 {
766         long scno = 0;
767 #ifndef USE_PROCFS
768         int pid = tcp->pid;
769 #endif /* !PROCFS */
770
771 #ifdef LINUX
772 #if defined(S390) || defined(S390X)
773         if (tcp->flags & TCB_WAITEXECVE) {
774                 /*
775                  * When the execve system call completes successfully, the
776                  * new process still has -ENOSYS (old style) or __NR_execve
777                  * (new style) in gpr2.  We cannot recover the scno again
778                  * by disassembly, because the image that executed the
779                  * syscall is gone now.  Fortunately, we don't want it.  We
780                  * leave the flag set so that syscall_fixup can fake the
781                  * result.
782                  */
783                 if (tcp->flags & TCB_INSYSCALL)
784                         return 1;
785                 /*
786                  * This is the SIGTRAP after execve.  We cannot try to read
787                  * the system call here either.
788                  */
789                 tcp->flags &= ~TCB_WAITEXECVE;
790                 return 0;
791         }
792
793         if (upeek(pid, PT_GPR2, &syscall_mode) < 0)
794                         return -1;
795
796         if (syscall_mode != -ENOSYS) {
797                 /*
798                  * Since kernel version 2.5.44 the scno gets passed in gpr2.
799                  */
800                 scno = syscall_mode;
801         } else {
802                 /*
803                  * Old style of "passing" the scno via the SVC instruction.
804                  */
805
806                 long opcode, offset_reg, tmp;
807                 void * svc_addr;
808                 int gpr_offset[16] = {PT_GPR0,  PT_GPR1,  PT_ORIGGPR2, PT_GPR3,
809                                       PT_GPR4,  PT_GPR5,  PT_GPR6,     PT_GPR7,
810                                       PT_GPR8,  PT_GPR9,  PT_GPR10,    PT_GPR11,
811                                       PT_GPR12, PT_GPR13, PT_GPR14,    PT_GPR15};
812
813                 if (upeek(pid, PT_PSWADDR, &pc) < 0)
814                         return -1;
815                 errno = 0;
816                 opcode = ptrace(PTRACE_PEEKTEXT, pid, (char *)(pc-sizeof(long)), 0);
817                 if (errno) {
818                         perror("peektext(pc-oneword)");
819                         return -1;
820                 }
821
822                 /*
823                  *  We have to check if the SVC got executed directly or via an
824                  *  EXECUTE instruction. In case of EXECUTE it is necessary to do
825                  *  instruction decoding to derive the system call number.
826                  *  Unfortunately the opcode sizes of EXECUTE and SVC are differently,
827                  *  so that this doesn't work if a SVC opcode is part of an EXECUTE
828                  *  opcode. Since there is no way to find out the opcode size this
829                  *  is the best we can do...
830                  */
831
832                 if ((opcode & 0xff00) == 0x0a00) {
833                         /* SVC opcode */
834                         scno = opcode & 0xff;
835                 }
836                 else {
837                         /* SVC got executed by EXECUTE instruction */
838
839                         /*
840                          *  Do instruction decoding of EXECUTE. If you really want to
841                          *  understand this, read the Principles of Operations.
842                          */
843                         svc_addr = (void *) (opcode & 0xfff);
844
845                         tmp = 0;
846                         offset_reg = (opcode & 0x000f0000) >> 16;
847                         if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
848                                 return -1;
849                         svc_addr += tmp;
850
851                         tmp = 0;
852                         offset_reg = (opcode & 0x0000f000) >> 12;
853                         if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
854                                 return -1;
855                         svc_addr += tmp;
856
857                         scno = ptrace(PTRACE_PEEKTEXT, pid, svc_addr, 0);
858                         if (errno)
859                                 return -1;
860 #if defined(S390X)
861                         scno >>= 48;
862 #else
863                         scno >>= 16;
864 #endif
865                         tmp = 0;
866                         offset_reg = (opcode & 0x00f00000) >> 20;
867                         if (offset_reg && (upeek(pid, gpr_offset[offset_reg], &tmp) < 0))
868                                 return -1;
869
870                         scno = (scno | tmp) & 0xff;
871                 }
872         }
873 #elif defined (POWERPC)
874         if (upeek(pid, sizeof(unsigned long)*PT_R0, &scno) < 0)
875                 return -1;
876         if (!(tcp->flags & TCB_INSYSCALL)) {
877                 /* Check if we return from execve. */
878                 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
879                         tcp->flags &= ~TCB_WAITEXECVE;
880                         return 0;
881                 }
882         }
883 #elif defined (I386)
884         if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
885                 return -1;
886 #elif defined (X86_64)
887         if (upeek(pid, 8*ORIG_RAX, &scno) < 0)
888                 return -1;
889
890         if (!(tcp->flags & TCB_INSYSCALL)) {
891                 static int currpers=-1;
892                 long val;
893
894                 /* Check CS register value. On x86-64 linux it is:
895                  *      0x33    for long mode (64 bit)
896                  *      0x23    for compatibility mode (32 bit)
897                  * It takes only one ptrace and thus doesn't need
898                  * to be cached.
899                  */
900                 if (upeek(pid, 8*CS, &val) < 0)
901                         return -1;
902                 switch(val)
903                 {
904                         case 0x23: currpers = 1; break;
905                         case 0x33: currpers = 0; break;
906                         default:
907                                 fprintf(stderr, "Unknown value CS=0x%02X while "
908                                          "detecting personality of process "
909                                          "PID=%d\n", (int)val, pid);
910                                 currpers = current_personality;
911                                 break;
912                 }
913 #if 0
914                 /* This version analyzes the opcode of a syscall instruction.
915                  * (int 0x80 on i386 vs. syscall on x86-64)
916                  * It works, but is too complicated.
917                  */
918                 unsigned long val, rip, i;
919
920                 if(upeek(pid, 8*RIP, &rip)<0)
921                         perror("upeek(RIP)");
922
923                 /* sizeof(syscall) == sizeof(int 0x80) == 2 */
924                 rip-=2;
925                 errno = 0;
926
927                 call = ptrace(PTRACE_PEEKTEXT,pid,(char *)rip,0);
928                 if (errno)
929                         printf("ptrace_peektext failed: %s\n",
930                                         strerror(errno));
931                 switch (call & 0xffff)
932                 {
933                         /* x86-64: syscall = 0x0f 0x05 */
934                         case 0x050f: currpers = 0; break;
935                         /* i386: int 0x80 = 0xcd 0x80 */
936                         case 0x80cd: currpers = 1; break;
937                         default:
938                                 currpers = current_personality;
939                                 fprintf(stderr,
940                                         "Unknown syscall opcode (0x%04X) while "
941                                         "detecting personality of process "
942                                         "PID=%d\n", (int)call, pid);
943                                 break;
944                 }
945 #endif
946                 if(currpers != current_personality)
947                 {
948                         char *names[]={"64 bit", "32 bit"};
949                         set_personality(currpers);
950                         printf("[ Process PID=%d runs in %s mode. ]\n",
951                                         pid, names[current_personality]);
952                 }
953         }
954 #elif defined(IA64)
955 #       define IA64_PSR_IS      ((long)1 << 34)
956         if (upeek (pid, PT_CR_IPSR, &psr) >= 0)
957                 ia32 = (psr & IA64_PSR_IS) != 0;
958         if (!(tcp->flags & TCB_INSYSCALL)) {
959                 if (ia32) {
960                         if (upeek(pid, PT_R1, &scno) < 0)       /* orig eax */
961                                 return -1;
962                 } else {
963                         if (upeek (pid, PT_R15, &scno) < 0)
964                                 return -1;
965                 }
966                 /* Check if we return from execve. */
967                 if (tcp->flags & TCB_WAITEXECVE) {
968                         tcp->flags &= ~TCB_WAITEXECVE;
969                         return 0;
970                 }
971         } else {
972                 /* syscall in progress */
973                 if (upeek (pid, PT_R8, &r8) < 0)
974                         return -1;
975                 if (upeek (pid, PT_R10, &r10) < 0)
976                         return -1;
977         }
978 #elif defined (ARM)
979         /*
980          * Read complete register set in one go.
981          */
982         if (ptrace(PTRACE_GETREGS, pid, NULL, (void *)&regs) == -1)
983                 return -1;
984
985         /*
986          * We only need to grab the syscall number on syscall entry.
987          */
988         if (regs.ARM_ip == 0) {
989                 /*
990                  * Note: we only deal with only 32-bit CPUs here.
991                  */
992                 if (regs.ARM_cpsr & 0x20) {
993                         /*
994                          * Get the Thumb-mode system call number
995                          */
996                         scno = regs.ARM_r7;
997                 } else {
998                         /*
999                          * Get the ARM-mode system call number
1000                          */
1001                         errno = 0;
1002                         scno = ptrace(PTRACE_PEEKTEXT, pid, (void *)(regs.ARM_pc - 4), NULL);
1003                         if (errno)
1004                                 return -1;
1005
1006                         if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
1007                                 tcp->flags &= ~TCB_WAITEXECVE;
1008                                 return 0;
1009                         }
1010
1011                         if ((scno & 0x0ff00000) != 0x0f900000) {
1012                                 fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
1013                                         scno);
1014                                 return -1;
1015                         }
1016
1017                         /*
1018                          * Fixup the syscall number
1019                          */
1020                         scno &= 0x000fffff;
1021                 }
1022
1023                 if (tcp->flags & TCB_INSYSCALL) {
1024                         fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
1025                         tcp->flags &= ~TCB_INSYSCALL;
1026                 }
1027         } else {
1028                 if (!(tcp->flags & TCB_INSYSCALL)) {
1029                         fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
1030                         tcp->flags |= TCB_INSYSCALL;
1031                 }
1032         }
1033 #elif defined (M68K)
1034         if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
1035                 return -1;
1036 #elif defined (MIPS)
1037         if (upeek(pid, REG_A3, &a3) < 0)
1038                 return -1;
1039
1040         if(!(tcp->flags & TCB_INSYSCALL)) {
1041                 if (upeek(pid, REG_V0, &scno) < 0)
1042                         return -1;
1043
1044                 if (scno < 0 || scno > nsyscalls) {
1045                         if(a3 == 0 || a3 == -1) {
1046                                 if(debug)
1047                                         fprintf (stderr, "stray syscall exit: v0 = %ld\n", scno);
1048                                 return 0;
1049                         }
1050                 }
1051         } else {
1052                 if (upeek(pid, REG_V0, &r2) < 0)
1053                         return -1;
1054         }
1055 #elif defined (ALPHA)
1056         if (upeek(pid, REG_A3, &a3) < 0)
1057                 return -1;
1058
1059         if (!(tcp->flags & TCB_INSYSCALL)) {
1060                 if (upeek(pid, REG_R0, &scno) < 0)
1061                         return -1;
1062
1063                 /* Check if we return from execve. */
1064                 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1065                         tcp->flags &= ~TCB_WAITEXECVE;
1066                         return 0;
1067                 }
1068
1069                 /*
1070                  * Do some sanity checks to figure out if it's
1071                  * really a syscall entry
1072                  */
1073                 if (scno < 0 || scno > nsyscalls) {
1074                         if (a3 == 0 || a3 == -1) {
1075                                 if (debug)
1076                                         fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
1077                                 return 0;
1078                         }
1079                 }
1080         }
1081         else {
1082                 if (upeek(pid, REG_R0, &r0) < 0)
1083                         return -1;
1084         }
1085 #elif defined (SPARC) || defined (SPARC64)
1086         /* Everything we need is in the current register set. */
1087         if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
1088                 return -1;
1089
1090         /* If we are entering, then disassemble the syscall trap. */
1091         if (!(tcp->flags & TCB_INSYSCALL)) {
1092                 /* Retrieve the syscall trap instruction. */
1093                 errno = 0;
1094                 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.r_pc,0);
1095 #if defined(SPARC64)
1096                 trap >>= 32;
1097 #endif
1098                 if (errno)
1099                         return -1;
1100
1101                 /* Disassemble the trap to see what personality to use. */
1102                 switch (trap) {
1103                 case 0x91d02010:
1104                         /* Linux/SPARC syscall trap. */
1105                         set_personality(0);
1106                         break;
1107                 case 0x91d0206d:
1108                         /* Linux/SPARC64 syscall trap. */
1109                         set_personality(2);
1110                         break;
1111                 case 0x91d02000:
1112                         /* SunOS syscall trap. (pers 1) */
1113                         fprintf(stderr,"syscall: SunOS no support\n");
1114                         return -1;
1115                 case 0x91d02008:
1116                         /* Solaris 2.x syscall trap. (per 2) */
1117                         set_personality(1);
1118                         break;
1119                 case 0x91d02009:
1120                         /* NetBSD/FreeBSD syscall trap. */
1121                         fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
1122                         return -1;
1123                 case 0x91d02027:
1124                         /* Solaris 2.x gettimeofday */
1125                         set_personality(1);
1126                         break;
1127                 default:
1128                         /* Unknown syscall trap. */
1129                         if(tcp->flags & TCB_WAITEXECVE) {
1130                                 tcp->flags &= ~TCB_WAITEXECVE;
1131                                 return 0;
1132                         }
1133 #if defined (SPARC64)
1134                         fprintf(stderr,"syscall: unknown syscall trap %08lx %016lx\n", trap, regs.r_tpc);
1135 #else
1136                         fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.r_pc);
1137 #endif
1138                         return -1;
1139                 }
1140
1141                 /* Extract the system call number from the registers. */
1142                 if (trap == 0x91d02027)
1143                         scno = 156;
1144                 else
1145                         scno = regs.r_g1;
1146                 if (scno == 0) {
1147                         scno = regs.r_o0;
1148                         memmove (&regs.r_o0, &regs.r_o1, 7*sizeof(regs.r_o0));
1149                 }
1150         }
1151 #elif defined(HPPA)
1152         if (upeek(pid, PT_GR20, &scno) < 0)
1153                 return -1;
1154         if (!(tcp->flags & TCB_INSYSCALL)) {
1155                 /* Check if we return from execve. */
1156                 if ((tcp->flags & TCB_WAITEXECVE)) {
1157                         tcp->flags &= ~TCB_WAITEXECVE;
1158                         return 0;
1159                 }
1160         }
1161 #elif defined(SH)
1162        /*
1163         * In the new syscall ABI, the system call number is in R3.
1164         */
1165        if (upeek(pid, 4*(REG_REG0+3), &scno) < 0)
1166                return -1;
1167
1168        if (scno < 0) {
1169            /* Odd as it may seem, a glibc bug has been known to cause
1170               glibc to issue bogus negative syscall numbers.  So for
1171               our purposes, make strace print what it *should* have been */
1172            long correct_scno = (scno & 0xff);
1173            if (debug)
1174                fprintf(stderr,
1175                    "Detected glibc bug: bogus system call number = %ld, "
1176                    "correcting to %ld\n",
1177                    scno,
1178                    correct_scno);
1179            scno = correct_scno;
1180        }
1181
1182
1183        if (!(tcp->flags & TCB_INSYSCALL)) {
1184                /* Check if we return from execve. */
1185                if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
1186                        tcp->flags &= ~TCB_WAITEXECVE;
1187                        return 0;
1188                }
1189        }
1190 #elif defined(SH64)
1191         if (upeek(pid, REG_SYSCALL, &scno) < 0)
1192                 return -1;
1193         scno &= 0xFFFF;
1194
1195         if (!(tcp->flags & TCB_INSYSCALL)) {
1196                 /* Check if we return from execve. */
1197                 if (tcp->flags & TCB_WAITEXECVE) {
1198                         tcp->flags &= ~TCB_WAITEXECVE;
1199                         return 0;
1200                 }
1201         }
1202 #endif /* SH64 */
1203 #endif /* LINUX */
1204 #ifdef SUNOS4
1205         if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
1206                 return -1;
1207 #elif defined(SH)
1208         /* new syscall ABI returns result in R0 */
1209         if (upeek(pid, 4*REG_REG0, (long *)&r0) < 0)
1210                 return -1;
1211 #elif defined(SH64)
1212         /* ABI defines result returned in r9 */
1213         if (upeek(pid, REG_GENERAL(9), (long *)&r9) < 0)
1214                 return -1;
1215
1216 #endif
1217 #ifdef USE_PROCFS
1218 #ifdef HAVE_PR_SYSCALL
1219         scno = tcp->status.PR_SYSCALL;
1220 #else /* !HAVE_PR_SYSCALL */
1221 #ifndef FREEBSD
1222         scno = tcp->status.PR_WHAT;
1223 #else /* FREEBSD */
1224         if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1225                 perror("pread");
1226                 return -1;
1227         }
1228         switch (regs.r_eax) {
1229         case SYS_syscall:
1230         case SYS___syscall:
1231                 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1232                 break;
1233         default:
1234                 scno = regs.r_eax;
1235                 break;
1236         }
1237 #endif /* FREEBSD */
1238 #endif /* !HAVE_PR_SYSCALL */
1239 #endif /* USE_PROCFS */
1240         if (!(tcp->flags & TCB_INSYSCALL))
1241                 tcp->scno = scno;
1242         return 1;
1243 }
1244
1245
1246 int
1247 syscall_fixup(tcp)
1248 struct tcb *tcp;
1249 {
1250 #ifndef USE_PROCFS
1251         int pid = tcp->pid;
1252 #else /* USE_PROCFS */
1253         int scno = tcp->scno;
1254
1255         if (!(tcp->flags & TCB_INSYSCALL)) {
1256                 if (tcp->status.PR_WHY != PR_SYSENTRY) {
1257                         if (
1258                             scno == SYS_fork
1259 #ifdef SYS_vfork
1260                             || scno == SYS_vfork
1261 #endif /* SYS_vfork */
1262 #ifdef SYS_fork1
1263                             || scno == SYS_fork1
1264 #endif /* SYS_fork1 */
1265 #ifdef SYS_forkall
1266                             || scno == SYS_forkall
1267 #endif /* SYS_forkall */
1268 #ifdef SYS_rfork1
1269                             || scno == SYS_rfork1
1270 #endif /* SYS_fork1 */
1271 #ifdef SYS_rforkall
1272                             || scno == SYS_rforkall
1273 #endif /* SYS_rforkall */
1274                             ) {
1275                                 /* We are returning in the child, fake it. */
1276                                 tcp->status.PR_WHY = PR_SYSENTRY;
1277                                 trace_syscall(tcp);
1278                                 tcp->status.PR_WHY = PR_SYSEXIT;
1279                         }
1280                         else {
1281                                 fprintf(stderr, "syscall: missing entry\n");
1282                                 tcp->flags |= TCB_INSYSCALL;
1283                         }
1284                 }
1285         }
1286         else {
1287                 if (tcp->status.PR_WHY != PR_SYSEXIT) {
1288                         fprintf(stderr, "syscall: missing exit\n");
1289                         tcp->flags &= ~TCB_INSYSCALL;
1290                 }
1291         }
1292 #endif /* USE_PROCFS */
1293 #ifdef SUNOS4
1294         if (!(tcp->flags & TCB_INSYSCALL)) {
1295                 if (scno == 0) {
1296                         fprintf(stderr, "syscall: missing entry\n");
1297                         tcp->flags |= TCB_INSYSCALL;
1298                 }
1299         }
1300         else {
1301                 if (scno != 0) {
1302                         if (debug) {
1303                                 /*
1304                                  * This happens when a signal handler
1305                                  * for a signal which interrupted a
1306                                  * a system call makes another system call.
1307                                  */
1308                                 fprintf(stderr, "syscall: missing exit\n");
1309                         }
1310                         tcp->flags &= ~TCB_INSYSCALL;
1311                 }
1312         }
1313 #endif /* SUNOS4 */
1314 #ifdef LINUX
1315 #if defined (I386)
1316         if (upeek(pid, 4*EAX, &eax) < 0)
1317                 return -1;
1318         if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1319                 if (debug)
1320                         fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
1321                 return 0;
1322         }
1323 #elif defined (X86_64)
1324         if (upeek(pid, 8*RAX, &rax) < 0)
1325                 return -1;
1326         if (current_personality == 1)
1327                 rax = (long int)(int)rax; /* sign extend from 32 bits */
1328         if (rax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1329                 if (debug)
1330                         fprintf(stderr, "stray syscall exit: rax = %ld\n", rax);
1331                 return 0;
1332         }
1333 #elif defined (S390) || defined (S390X)
1334         if (upeek(pid, PT_GPR2, &gpr2) < 0)
1335                 return -1;
1336         if (syscall_mode != -ENOSYS)
1337                 syscall_mode = tcp->scno;
1338         if (gpr2 != syscall_mode && !(tcp->flags & TCB_INSYSCALL)) {
1339                 if (debug)
1340                         fprintf(stderr, "stray syscall exit: gpr2 = %ld\n", gpr2);
1341                 return 0;
1342         }
1343         else if (((tcp->flags & (TCB_INSYSCALL|TCB_WAITEXECVE))
1344                   == (TCB_INSYSCALL|TCB_WAITEXECVE))
1345                  && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
1346                 /*
1347                  * Fake a return value of zero.  We leave the TCB_WAITEXECVE
1348                  * flag set for the post-execve SIGTRAP to see and reset.
1349                  */
1350                 gpr2 = 0;
1351         }
1352 #elif defined (POWERPC)
1353 # define SO_MASK 0x10000000
1354         if (upeek(pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1355                 return -1;
1356         if (upeek(pid, sizeof(unsigned long)*PT_R3, &result) < 0)
1357                 return -1;
1358         if (flags & SO_MASK)
1359                 result = -result;
1360 #elif defined (M68K)
1361         if (upeek(pid, 4*PT_D0, &d0) < 0)
1362                 return -1;
1363         if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1364                 if (debug)
1365                         fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
1366                 return 0;
1367         }
1368 #elif defined (ARM)
1369         /*
1370          * Nothing required
1371          */
1372 #elif defined (HPPA)
1373         if (upeek(pid, PT_GR28, &r28) < 0)
1374                 return -1;
1375 #elif defined(IA64)
1376         if (upeek(pid, PT_R10, &r10) < 0)
1377                 return -1;
1378         if (upeek(pid, PT_R8, &r8) < 0)
1379                 return -1;
1380         if (ia32 && r8 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
1381                 if (debug)
1382                         fprintf(stderr, "stray syscall exit: r8 = %ld\n", r8);
1383                 return 0;
1384         }
1385 #endif
1386 #endif /* LINUX */
1387         return 1;
1388 }
1389
1390 int
1391 get_error(tcp)
1392 struct tcb *tcp;
1393 {
1394         int u_error = 0;
1395 #ifdef LINUX
1396 #if defined(S390) || defined(S390X)
1397                 if (gpr2 && (unsigned) -gpr2 < nerrnos) {
1398                         tcp->u_rval = -1;
1399                         u_error = -gpr2;
1400                 }
1401                 else {
1402                         tcp->u_rval = gpr2;
1403                         u_error = 0;
1404                 }
1405 #else /* !S390 && !S390X */
1406 #ifdef I386
1407                 if (eax < 0 && -eax < nerrnos) {
1408                         tcp->u_rval = -1;
1409                         u_error = -eax;
1410                 }
1411                 else {
1412                         tcp->u_rval = eax;
1413                         u_error = 0;
1414                 }
1415 #else /* !I386 */
1416 #ifdef X86_64
1417                 if (rax < 0 && -rax < nerrnos) {
1418                         tcp->u_rval = -1;
1419                         u_error = -rax;
1420                 }
1421                 else {
1422                         tcp->u_rval = rax;
1423                         u_error = 0;
1424                 }
1425 #else
1426 #ifdef IA64
1427                 if (ia32) {
1428                         int err;
1429
1430                         err = (int)r8;
1431                         if (err < 0 && -err < nerrnos) {
1432                                 tcp->u_rval = -1;
1433                                 u_error = -err;
1434                         }
1435                         else {
1436                                 tcp->u_rval = err;
1437                                 u_error = 0;
1438                         }
1439                 } else {
1440                         if (r10) {
1441                                 tcp->u_rval = -1;
1442                                 u_error = r8;
1443                         } else {
1444                                 tcp->u_rval = r8;
1445                                 u_error = 0;
1446                         }
1447                 }
1448 #else /* !IA64 */
1449 #ifdef MIPS
1450                 if (a3) {
1451                         tcp->u_rval = -1;
1452                         u_error = r2;
1453                 } else {
1454                         tcp->u_rval = r2;
1455                         u_error = 0;
1456                 }
1457 #else
1458 #ifdef POWERPC
1459                 if (result && (unsigned long) -result < nerrnos) {
1460                         tcp->u_rval = -1;
1461                         u_error = -result;
1462                 }
1463                 else {
1464                         tcp->u_rval = result;
1465                         u_error = 0;
1466                 }
1467 #else /* !POWERPC */
1468 #ifdef M68K
1469                 if (d0 && (unsigned) -d0 < nerrnos) {
1470                         tcp->u_rval = -1;
1471                         u_error = -d0;
1472                 }
1473                 else {
1474                         tcp->u_rval = d0;
1475                         u_error = 0;
1476                 }
1477 #else /* !M68K */
1478 #ifdef ARM
1479                 if (regs.ARM_r0 && (unsigned) -regs.ARM_r0 < nerrnos) {
1480                         tcp->u_rval = -1;
1481                         u_error = -regs.ARM_r0;
1482                 }
1483                 else {
1484                         tcp->u_rval = regs.ARM_r0;
1485                         u_error = 0;
1486                 }
1487 #else /* !ARM */
1488 #ifdef ALPHA
1489                 if (a3) {
1490                         tcp->u_rval = -1;
1491                         u_error = r0;
1492                 }
1493                 else {
1494                         tcp->u_rval = r0;
1495                         u_error = 0;
1496                 }
1497 #else /* !ALPHA */
1498 #ifdef SPARC
1499                 if (regs.r_psr & PSR_C) {
1500                         tcp->u_rval = -1;
1501                         u_error = regs.r_o0;
1502                 }
1503                 else {
1504                         tcp->u_rval = regs.r_o0;
1505                         u_error = 0;
1506                 }
1507 #else /* !SPARC */
1508 #ifdef SPARC64
1509                 if (regs.r_tstate & 0x1100000000UL) {
1510                         tcp->u_rval = -1;
1511                         u_error = regs.r_o0;
1512                 }
1513                 else {
1514                         tcp->u_rval = regs.r_o0;
1515                         u_error = 0;
1516                 }
1517 #else /* !SPARC64 */
1518 #ifdef HPPA
1519                 if (r28 && (unsigned) -r28 < nerrnos) {
1520                         tcp->u_rval = -1;
1521                         u_error = -r28;
1522                 }
1523                 else {
1524                         tcp->u_rval = r28;
1525                         u_error = 0;
1526                 }
1527 #else
1528 #ifdef SH
1529                /* interpret R0 as return value or error number */
1530                if (r0 && (unsigned) -r0 < nerrnos) {
1531                        tcp->u_rval = -1;
1532                        u_error = -r0;
1533                }
1534                else {
1535                        tcp->u_rval = r0;
1536                        u_error = 0;
1537                }
1538 #else
1539 #ifdef SH64
1540                 /* interpret result as return value or error number */
1541                 if (r9 && (unsigned) -r9 < nerrnos) {
1542                         tcp->u_rval = -1;
1543                         u_error = -r9;
1544                 }
1545                 else {
1546                         tcp->u_rval = r9;
1547                         u_error = 0;
1548                 }
1549 #endif /* SH64 */
1550 #endif /* SH */
1551 #endif /* HPPA */
1552 #endif /* SPARC */
1553 #endif /* SPARC64 */
1554 #endif /* ALPHA */
1555 #endif /* ARM */
1556 #endif /* M68K */
1557 #endif /* POWERPC */
1558 #endif /* MIPS */
1559 #endif /* IA64 */
1560 #endif /* X86_64 */
1561 #endif /* I386 */
1562 #endif /* S390 || S390X */
1563 #endif /* LINUX */
1564 #ifdef SUNOS4
1565                 /* get error code from user struct */
1566                 if (upeek(pid, uoff(u_error), &u_error) < 0)
1567                         return -1;
1568                 u_error >>= 24; /* u_error is a char */
1569
1570                 /* get system call return value */
1571                 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
1572                         return -1;
1573 #endif /* SUNOS4 */
1574 #ifdef SVR4
1575 #ifdef SPARC
1576                 /* Judicious guessing goes a long way. */
1577                 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
1578                         tcp->u_rval = -1;
1579                         u_error = tcp->status.pr_reg[R_O0];
1580                 }
1581                 else {
1582                         tcp->u_rval = tcp->status.pr_reg[R_O0];
1583                         u_error = 0;
1584                 }
1585 #endif /* SPARC */
1586 #ifdef I386
1587                 /* Wanna know how to kill an hour single-stepping? */
1588                 if (tcp->status.PR_REG[EFL] & 0x1) {
1589                         tcp->u_rval = -1;
1590                         u_error = tcp->status.PR_REG[EAX];
1591                 }
1592                 else {
1593                         tcp->u_rval = tcp->status.PR_REG[EAX];
1594 #ifdef HAVE_LONG_LONG
1595                         tcp->u_lrval =
1596                                 ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
1597                                 tcp->status.PR_REG[EAX];
1598 #endif
1599                         u_error = 0;
1600                 }
1601 #endif /* I386 */
1602 #ifdef X86_64
1603                 /* Wanna know how to kill an hour single-stepping? */
1604                 if (tcp->status.PR_REG[EFLAGS] & 0x1) {
1605                         tcp->u_rval = -1;
1606                         u_error = tcp->status.PR_REG[RAX];
1607                 }
1608                 else {
1609                         tcp->u_rval = tcp->status.PR_REG[RAX];
1610                         u_error = 0;
1611                 }
1612 #endif /* X86_64 */
1613 #ifdef MIPS
1614                 if (tcp->status.pr_reg[CTX_A3]) {
1615                         tcp->u_rval = -1;
1616                         u_error = tcp->status.pr_reg[CTX_V0];
1617                 }
1618                 else {
1619                         tcp->u_rval = tcp->status.pr_reg[CTX_V0];
1620                         u_error = 0;
1621                 }
1622 #endif /* MIPS */
1623 #endif /* SVR4 */
1624 #ifdef FREEBSD
1625                 if (regs.r_eflags & PSL_C) {
1626                         tcp->u_rval = -1;
1627                         u_error = regs.r_eax;
1628                 } else {
1629                         tcp->u_rval = regs.r_eax;
1630                         tcp->u_lrval =
1631                           ((unsigned long long) regs.r_edx << 32) +  regs.r_eax;
1632                         u_error = 0;
1633                 }
1634 #endif /* FREEBSD */
1635         tcp->u_error = u_error;
1636         return 1;
1637 }
1638
1639 int
1640 force_result(tcp, error, rval)
1641         struct tcb *tcp;
1642         int error;
1643         long rval;
1644 {
1645 #ifdef LINUX
1646 #if defined(S390) || defined(S390X)
1647         gpr2 = error ? -error : rval;
1648         if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)PT_GPR2, gpr2) < 0)
1649                 return -1;
1650 #else /* !S390 && !S390X */
1651 #ifdef I386
1652         eax = error ? -error : rval;
1653         if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(EAX * 4), eax) < 0)
1654                 return -1;
1655 #else /* !I386 */
1656 #ifdef X86_64
1657         rax = error ? -error : rval;
1658         if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(RAX * 8), rax) < 0)
1659                 return -1;
1660 #else
1661 #ifdef IA64
1662         if (ia32) {
1663                 r8 = error ? -error : rval;
1664                 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0)
1665                         return -1;
1666         }
1667         else {
1668                 if (error) {
1669                         r8 = error;
1670                         r10 = -1;
1671                 }
1672                 else {
1673                         r8 = rval;
1674                         r10 = 0;
1675                 }
1676                 if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R8), r8) < 0 ||
1677                     ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_R10), r10) < 0)
1678                         return -1;
1679         }
1680 #else /* !IA64 */
1681 #ifdef MIPS
1682         if (error) {
1683                 r2 = error;
1684                 a3 = -1;
1685         }
1686         else {
1687                 r2 = rval;
1688                 a3 = 0;
1689         }
1690         if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1691             ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_V0), r2) < 0)
1692                 return -1;
1693 #else
1694 #ifdef POWERPC
1695         if (upeek(tcp->pid, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1696                 return -1;
1697         if (error) {
1698                 flags |= SO_MASK;
1699                 result = error;
1700         }
1701         else {
1702                 flags &= ~SO_MASK;
1703                 result = rval;
1704         }
1705         if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_CCR), flags) < 0 ||
1706             ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(sizeof(unsigned long)*PT_R3), result) < 0)
1707                 return -1;
1708 #else /* !POWERPC */
1709 #ifdef M68K
1710         d0 = error ? -error : rval;
1711         if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*PT_D0), d0) < 0)
1712                 return -1;
1713 #else /* !M68K */
1714 #ifdef ARM
1715        regs.ARM_r0 = error ? -error : rval;
1716        if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*0), regs.ARM_r0) < 0)
1717                 return -1;
1718 #else /* !ARM */
1719 #ifdef ALPHA
1720         if (error) {
1721                 a3 = -1;
1722                 r0 = error;
1723         }
1724         else {
1725                 a3 = 0;
1726                 r0 = rval;
1727         }
1728         if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_A3), a3) < 0 ||
1729             ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(REG_R0), r0) < 0)
1730                 return -1;
1731 #else /* !ALPHA */
1732 #ifdef SPARC
1733         if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)&regs, 0) < 0)
1734                 return -1;
1735         if (error) {
1736                 regs.r_psr |= PSR_C;
1737                 regs.r_o0 = error;
1738         }
1739         else {
1740                 regs.r_psr &= ~PSR_C;
1741                 regs.r_o0 = rval;
1742         }
1743         if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)&regs, 0) < 0)
1744                 return -1;
1745 #else /* !SPARC */
1746 #ifdef SPARC64
1747         if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)&regs, 0) < 0)
1748                 return -1;
1749         if (error) {
1750                 regs.r_tstate |= 0x1100000000UL;
1751                 regs.r_o0 = error;
1752         }
1753         else {
1754                 regs.r_tstate &= ~0x1100000000UL;
1755                 regs.r_o0 = rval;
1756         }
1757         if (ptrace(PTRACE_SETREGS, tcp->pid, (char *)&regs, 0) < 0)
1758                 return -1;
1759 #else /* !SPARC64 */
1760 #ifdef HPPA
1761         r28 = error ? -error : rval;
1762         if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(PT_GR28), r28) < 0)
1763                 return -1;
1764 #else
1765 #ifdef SH
1766         r0 = error ? -error : rval;
1767         if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)(4*REG_REG0), r0) < 0)
1768                 return -1;
1769 #else
1770 #ifdef SH64
1771         r9 = error ? -error : rval;
1772         if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)REG_GENERAL(9), r9) < 0)
1773                 return -1;
1774 #endif /* SH64 */
1775 #endif /* SH */
1776 #endif /* HPPA */
1777 #endif /* SPARC */
1778 #endif /* SPARC64 */
1779 #endif /* ALPHA */
1780 #endif /* ARM */
1781 #endif /* M68K */
1782 #endif /* POWERPC */
1783 #endif /* MIPS */
1784 #endif /* IA64 */
1785 #endif /* X86_64 */
1786 #endif /* I386 */
1787 #endif /* S390 || S390X */
1788 #endif /* LINUX */
1789 #ifdef SUNOS4
1790         if (ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_error),
1791                    error << 24) < 0 ||
1792             ptrace(PTRACE_POKEUSER, tcp->pid, (char*)uoff(u_rval1), rval) < 0)
1793                 return -1;
1794 #endif /* SUNOS4 */
1795 #ifdef SVR4
1796         /* XXX no clue */
1797         return -1;
1798 #endif /* SVR4 */
1799 #ifdef FREEBSD
1800         if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1801                 perror("pread");
1802                 return -1;
1803         }
1804         if (error) {
1805                 regs.r_eflags |= PSL_C;
1806                 regs.r_eax = error;
1807         }
1808         else {
1809                 regs.r_eflags &= ~PSL_C;
1810                 regs.r_eax = rval;
1811         }
1812         if (pwrite(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1813                 perror("pwrite");
1814                 return -1;
1815         }
1816 #endif /* FREEBSD */
1817
1818         /* All branches reach here on success (only).  */
1819         tcp->u_error = error;
1820         tcp->u_rval = rval;
1821         return 0;
1822 }
1823
1824 int syscall_enter(tcp)
1825 struct tcb *tcp;
1826 {
1827 #ifndef USE_PROCFS
1828         int pid = tcp->pid;
1829 #endif /* !USE_PROCFS */
1830 #ifdef LINUX
1831 #if defined(S390) || defined(S390X)
1832         {
1833                 int i;
1834                 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1835                         tcp->u_nargs = sysent[tcp->scno].nargs;
1836                 else
1837                         tcp->u_nargs = MAX_ARGS;
1838                 for (i = 0; i < tcp->u_nargs; i++) {
1839                         if (upeek(pid,i==0 ? PT_ORIGGPR2:PT_GPR2+i*sizeof(long), &tcp->u_arg[i]) < 0)
1840                                 return -1;
1841                 }
1842         }
1843 #elif defined (ALPHA)
1844         {
1845                 int i;
1846                 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1847                         tcp->u_nargs = sysent[tcp->scno].nargs;
1848                 else
1849                         tcp->u_nargs = MAX_ARGS;
1850                 for (i = 0; i < tcp->u_nargs; i++) {
1851                         /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1852                          * for scno somewhere above here!
1853                          */
1854                         if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1855                                 return -1;
1856                 }
1857         }
1858 #elif defined (IA64)
1859         {
1860                 if (!ia32) {
1861                         unsigned long *out0, *rbs_end, cfm, sof, sol, i;
1862                         /* be backwards compatible with kernel < 2.4.4... */
1863 #                       ifndef PT_RBS_END
1864 #                         define PT_RBS_END     PT_AR_BSP
1865 #                       endif
1866
1867                         if (upeek(pid, PT_RBS_END, (long *) &rbs_end) < 0)
1868                                 return -1;
1869                         if (upeek(pid, PT_CFM, (long *) &cfm) < 0)
1870                                 return -1;
1871
1872                         sof = (cfm >> 0) & 0x7f;
1873                         sol = (cfm >> 7) & 0x7f;
1874                         out0 = ia64_rse_skip_regs(rbs_end, -sof + sol);
1875
1876                         if (tcp->scno >= 0 && tcp->scno < nsyscalls
1877                             && sysent[tcp->scno].nargs != -1)
1878                                 tcp->u_nargs = sysent[tcp->scno].nargs;
1879                         else
1880                                 tcp->u_nargs = MAX_ARGS;
1881                         for (i = 0; i < tcp->u_nargs; ++i) {
1882                                 if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1883                                            sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1884                                         return -1;
1885                         }
1886                 } else {
1887                         int i;
1888
1889                         if (/* EBX = out0 */
1890                             upeek(pid, PT_R11, (long *) &tcp->u_arg[0]) < 0
1891                             /* ECX = out1 */
1892                             || upeek(pid, PT_R9,  (long *) &tcp->u_arg[1]) < 0
1893                             /* EDX = out2 */
1894                             || upeek(pid, PT_R10, (long *) &tcp->u_arg[2]) < 0
1895                             /* ESI = out3 */
1896                             || upeek(pid, PT_R14, (long *) &tcp->u_arg[3]) < 0
1897                             /* EDI = out4 */
1898                             || upeek(pid, PT_R15, (long *) &tcp->u_arg[4]) < 0
1899                             /* EBP = out5 */
1900                             || upeek(pid, PT_R13, (long *) &tcp->u_arg[5]) < 0)
1901                                 return -1;
1902
1903                         for (i = 0; i < 6; ++i)
1904                                 /* truncate away IVE sign-extension */
1905                                 tcp->u_arg[i] &= 0xffffffff;
1906
1907                         if (tcp->scno >= 0 && tcp->scno < nsyscalls
1908                             && sysent[tcp->scno].nargs != -1)
1909                                 tcp->u_nargs = sysent[tcp->scno].nargs;
1910                         else
1911                                 tcp->u_nargs = 5;
1912                 }
1913         }
1914 #elif defined (MIPS)
1915         {
1916                 long sp;
1917                 int i, nargs;
1918
1919                 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1920                         nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1921                 else
1922                         nargs = tcp->u_nargs = MAX_ARGS;
1923                 if(nargs > 4) {
1924                         if(upeek(pid, REG_SP, &sp) < 0)
1925                                 return -1;
1926                         for(i = 0; i < 4; i++) {
1927                                 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i])<0)
1928                                         return -1;
1929                         }
1930                         umoven(tcp, sp+16, (nargs-4) * sizeof(tcp->u_arg[0]),
1931                                (char *)(tcp->u_arg + 4));
1932                 } else {
1933                         for(i = 0; i < nargs; i++) {
1934                                 if (upeek(pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1935                                         return -1;
1936                         }
1937                 }
1938         }
1939 #elif defined (POWERPC)
1940 #ifndef PT_ORIG_R3
1941 #define PT_ORIG_R3 34
1942 #endif
1943         {
1944                 int i;
1945                 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1946                         tcp->u_nargs = sysent[tcp->scno].nargs;
1947                 else
1948                         tcp->u_nargs = MAX_ARGS;
1949                 for (i = 0; i < tcp->u_nargs; i++) {
1950                         if (upeek(pid, (i==0) ?
1951                                 (sizeof(unsigned long)*PT_ORIG_R3) :
1952                                 ((i+PT_R3)*sizeof(unsigned long)),
1953                                         &tcp->u_arg[i]) < 0)
1954                                 return -1;
1955                 }
1956         }
1957 #elif defined (SPARC) || defined (SPARC64)
1958         {
1959                 int i;
1960
1961                 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1962                         tcp->u_nargs = sysent[tcp->scno].nargs;
1963                 else
1964                         tcp->u_nargs = MAX_ARGS;
1965                 for (i = 0; i < tcp->u_nargs; i++)
1966                         tcp->u_arg[i] = *((&regs.r_o0) + i);
1967         }
1968 #elif defined (HPPA)
1969         {
1970                 int i;
1971
1972                 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1973                         tcp->u_nargs = sysent[tcp->scno].nargs;
1974                 else
1975                         tcp->u_nargs = MAX_ARGS;
1976                 for (i = 0; i < tcp->u_nargs; i++) {
1977                         if (upeek(pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1978                                 return -1;
1979                 }
1980         }
1981 #elif defined(ARM)
1982         {
1983                 int i;
1984
1985                 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
1986                         tcp->u_nargs = sysent[tcp->scno].nargs;
1987                 else
1988                         tcp->u_nargs = MAX_ARGS;
1989                 for (i = 0; i < tcp->u_nargs; i++)
1990                         tcp->u_arg[i] = regs.uregs[i];
1991         }
1992 #elif defined(SH)
1993        {
1994                int i;
1995                static int syscall_regs[] = {
1996                    REG_REG0+4, REG_REG0+5, REG_REG0+6, REG_REG0+7,
1997                    REG_REG0, REG_REG0+1, REG_REG0+2
1998                    };
1999
2000                tcp->u_nargs = sysent[tcp->scno].nargs;
2001                for (i = 0; i < tcp->u_nargs; i++) {
2002                        if (upeek(pid, 4*syscall_regs[i], &tcp->u_arg[i]) < 0)
2003                                return -1;
2004                }
2005         }
2006 #elif defined(SH64)
2007         {
2008                 int i;
2009                 /* Registers used by SH5 Linux system calls for parameters */
2010                 static int syscall_regs[] = { 2, 3, 4, 5, 6, 7 };
2011
2012                 /*
2013                  * TODO: should also check that the number of arguments encoded
2014                  *       in the trap number matches the number strace expects.
2015                  */
2016                 /*
2017                     assert(sysent[tcp->scno].nargs <
2018                         sizeof(syscall_regs)/sizeof(syscall_regs[0]));
2019                  */
2020
2021                 tcp->u_nargs = sysent[tcp->scno].nargs;
2022                 for (i = 0; i < tcp->u_nargs; i++) {
2023                         if (upeek(pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
2024                                 return -1;
2025                 }
2026         }
2027
2028 #elif defined(X86_64)
2029         {
2030                 int i;
2031                 static int argreg[SUPPORTED_PERSONALITIES][MAX_ARGS] = {
2032                         {RDI,RSI,RDX,R10,R8,R9},        /* x86-64 ABI */
2033                         {RBX,RCX,RDX,RDX,RSI,RDI,RBP}   /* i386 ABI */
2034                 };
2035
2036                 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2037                         tcp->u_nargs = sysent[tcp->scno].nargs;
2038                 else
2039                         tcp->u_nargs = MAX_ARGS;
2040                 for (i = 0; i < tcp->u_nargs; i++) {
2041                         if (upeek(pid, argreg[current_personality][i]*8, &tcp->u_arg[i]) < 0)
2042                                 return -1;
2043                 }
2044         }
2045 #else /* Other architecture (like i386) (32bits specific) */
2046         {
2047                 int i;
2048                 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2049                         tcp->u_nargs = sysent[tcp->scno].nargs;
2050                 else
2051                         tcp->u_nargs = MAX_ARGS;
2052                 for (i = 0; i < tcp->u_nargs; i++) {
2053                         if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
2054                                 return -1;
2055                 }
2056         }
2057 #endif
2058 #endif /* LINUX */
2059 #ifdef SUNOS4
2060         {
2061                 int i;
2062                 if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2063                         tcp->u_nargs = sysent[tcp->scno].nargs;
2064                 else
2065                         tcp->u_nargs = MAX_ARGS;
2066                 for (i = 0; i < tcp->u_nargs; i++) {
2067                         struct user *u;
2068
2069                         if (upeek(pid, uoff(u_arg[0]) +
2070                             (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
2071                                 return -1;
2072                 }
2073         }
2074 #endif /* SUNOS4 */
2075 #ifdef SVR4
2076 #ifdef MIPS
2077         /*
2078          * SGI is broken: even though it has pr_sysarg, it doesn't
2079          * set them on system call entry.  Get a clue.
2080          */
2081         if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2082                 tcp->u_nargs = sysent[tcp->scno].nargs;
2083         else
2084                 tcp->u_nargs = tcp->status.pr_nsysarg;
2085         if (tcp->u_nargs > 4) {
2086                 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2087                         4*sizeof(tcp->u_arg[0]));
2088                 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
2089                         (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
2090         }
2091         else {
2092                 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
2093                         tcp->u_nargs*sizeof(tcp->u_arg[0]));
2094         }
2095 #elif UNIXWARE >= 2
2096         /*
2097          * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
2098          */
2099         if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2100                 tcp->u_nargs = sysent[tcp->scno].nargs;
2101         else
2102                 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
2103         umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2104                 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2105 #elif defined (HAVE_PR_SYSCALL)
2106         if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2107                 tcp->u_nargs = sysent[tcp->scno].nargs;
2108         else
2109                 tcp->u_nargs = tcp->status.pr_nsysarg;
2110         {
2111                 int i;
2112                 for (i = 0; i < tcp->u_nargs; i++)
2113                         tcp->u_arg[i] = tcp->status.pr_sysarg[i];
2114         }
2115 #elif defined (I386)
2116         if (tcp->scno >= 0 && tcp->scno < nsyscalls && sysent[tcp->scno].nargs != -1)
2117                 tcp->u_nargs = sysent[tcp->scno].nargs;
2118         else
2119                 tcp->u_nargs = 5;
2120         umoven(tcp, tcp->status.PR_REG[UESP] + 4,
2121                 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
2122 #else
2123         I DONT KNOW WHAT TO DO
2124 #endif /* !HAVE_PR_SYSCALL */
2125 #endif /* SVR4 */
2126 #ifdef FREEBSD
2127         if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2128             sysent[tcp->scno].nargs > tcp->status.val)
2129                 tcp->u_nargs = sysent[tcp->scno].nargs;
2130         else
2131                 tcp->u_nargs = tcp->status.val;
2132         if (tcp->u_nargs < 0)
2133                 tcp->u_nargs = 0;
2134         if (tcp->u_nargs > MAX_ARGS)
2135                 tcp->u_nargs = MAX_ARGS;
2136         switch(regs.r_eax) {
2137         case SYS___syscall:
2138                 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2139                       regs.r_esp + sizeof(int) + sizeof(quad_t));
2140           break;
2141         case SYS_syscall:
2142                 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2143                       regs.r_esp + 2 * sizeof(int));
2144           break;
2145         default:
2146                 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
2147                       regs.r_esp + sizeof(int));
2148           break;
2149         }
2150 #endif /* FREEBSD */
2151         return 1;
2152 }
2153
2154 int
2155 trace_syscall(tcp)
2156 struct tcb *tcp;
2157 {
2158         int sys_res;
2159         struct timeval tv;
2160         int res;
2161
2162         /* Measure the exit time as early as possible to avoid errors. */
2163         if (dtime && (tcp->flags & TCB_INSYSCALL))
2164                 gettimeofday(&tv, NULL);
2165
2166         res = get_scno(tcp);
2167         if (res != 1)
2168                 return res;
2169
2170         res = syscall_fixup(tcp);
2171         if (res != 1)
2172                 return res;
2173
2174         if (tcp->flags & TCB_INSYSCALL) {
2175                 long u_error;
2176                 res = get_error(tcp);
2177                 if (res != 1)
2178                         return res;
2179
2180                 internal_syscall(tcp);
2181                 if (tcp->scno >= 0 && tcp->scno < nsyscalls &&
2182                     !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2183                         tcp->flags &= ~TCB_INSYSCALL;
2184                         return 0;
2185                 }
2186
2187                 if (tcp->flags & TCB_REPRINT) {
2188                         printleader(tcp);
2189                         tprintf("<... ");
2190                         if (tcp->scno >= nsyscalls || tcp->scno < 0)
2191                                 tprintf("syscall_%lu", tcp->scno);
2192                         else
2193                                 tprintf("%s", sysent[tcp->scno].sys_name);
2194                         tprintf(" resumed> ");
2195                 }
2196
2197                 if (cflag && tcp->scno < nsyscalls && tcp->scno >= 0) {
2198                         call_count[tcp->scno]++;
2199                         if (tcp->u_error)
2200                                 error_count[tcp->scno]++;
2201                         tv_sub(&tv, &tv, &tcp->etime);
2202 #ifdef LINUX
2203                         if (tv_cmp(&tv, &tcp->dtime) > 0) {
2204                                 static struct timeval one_tick;
2205                                 if (one_tick.tv_usec == 0) {
2206                                         /* Initialize it.  */
2207                                         struct itimerval it;
2208                                         memset(&it, 0, sizeof it);
2209                                         it.it_interval.tv_usec = 1;
2210                                         setitimer(ITIMER_REAL, &it, NULL);
2211                                         getitimer(ITIMER_REAL, &it);
2212                                         one_tick = it.it_interval;
2213                                 }
2214
2215                                 if (tv_nz(&tcp->dtime))
2216                                         tv = tcp->dtime;
2217                                 else if (tv_cmp(&tv, &one_tick) > 0) {
2218                                         if (tv_cmp(&shortest, &one_tick) < 0)
2219                                                 tv = shortest;
2220                                         else
2221                                                 tv = one_tick;
2222                                 }
2223                         }
2224 #endif /* LINUX */
2225                         if (tv_cmp(&tv, &shortest) < 0)
2226                                 shortest = tv;
2227                         tv_add(&tv_count[tcp->scno],
2228                                 &tv_count[tcp->scno], &tv);
2229                         tcp->flags &= ~TCB_INSYSCALL;
2230                         return 0;
2231                 }
2232
2233                 if (tcp->scno >= nsyscalls || tcp->scno < 0
2234                     || (qual_flags[tcp->scno] & QUAL_RAW))
2235                         sys_res = printargs(tcp);
2236                 else {
2237                         if (not_failing_only && tcp->u_error)
2238                                 return 0;       /* ignore failed syscalls */
2239                         sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2240                 }
2241                 u_error = tcp->u_error;
2242                 tprintf(") ");
2243                 tabto(acolumn);
2244                 if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2245                     qual_flags[tcp->scno] & QUAL_RAW) {
2246                         if (u_error)
2247                                 tprintf("= -1 (errno %ld)", u_error);
2248                         else
2249                                 tprintf("= %#lx", tcp->u_rval);
2250                 }
2251                 else if (!(sys_res & RVAL_NONE) && u_error) {
2252                         switch (u_error) {
2253 #ifdef LINUX
2254                         case ERESTARTSYS:
2255                                 tprintf("= ? ERESTARTSYS (To be restarted)");
2256                                 break;
2257                         case ERESTARTNOINTR:
2258                                 tprintf("= ? ERESTARTNOINTR (To be restarted)");
2259                                 break;
2260                         case ERESTARTNOHAND:
2261                                 tprintf("= ? ERESTARTNOHAND (To be restarted)");
2262                                 break;
2263                         case ERESTART_RESTARTBLOCK:
2264                                 tprintf("= ? ERESTART_RESTARTBLOCK (To be restarted)");
2265                                 break;
2266 #endif /* LINUX */
2267                         default:
2268                                 tprintf("= -1 ");
2269                                 if (u_error < 0)
2270                                         tprintf("E??? (errno %ld)", u_error);
2271                                 else if (u_error < nerrnos)
2272                                         tprintf("%s (%s)", errnoent[u_error],
2273                                                 strerror(u_error));
2274                                 else
2275                                         tprintf("ERRNO_%ld (%s)", u_error,
2276                                                 strerror(u_error));
2277                                 break;
2278                         }
2279                 }
2280                 else {
2281                         if (sys_res & RVAL_NONE)
2282                                 tprintf("= ?");
2283                         else {
2284                                 switch (sys_res & RVAL_MASK) {
2285                                 case RVAL_HEX:
2286                                         tprintf("= %#lx", tcp->u_rval);
2287                                         break;
2288                                 case RVAL_OCTAL:
2289                                         tprintf("= %#lo", tcp->u_rval);
2290                                         break;
2291                                 case RVAL_UDECIMAL:
2292                                         tprintf("= %lu", tcp->u_rval);
2293                                         break;
2294                                 case RVAL_DECIMAL:
2295                                         tprintf("= %ld", tcp->u_rval);
2296                                         break;
2297 #ifdef HAVE_LONG_LONG
2298                                 case RVAL_LHEX:
2299                                         tprintf("= %#llx", tcp->u_lrval);
2300                                         break;
2301                                 case RVAL_LOCTAL:
2302                                         tprintf("= %#llo", tcp->u_lrval);
2303                                         break;
2304                                 case RVAL_LUDECIMAL:
2305                                         tprintf("= %llu", tcp->u_lrval);
2306                                         break;
2307                                 case RVAL_LDECIMAL:
2308                                         tprintf("= %lld", tcp->u_lrval);
2309                                         break;
2310 #endif
2311                                 default:
2312                                         fprintf(stderr,
2313                                                 "invalid rval format\n");
2314                                         break;
2315                                 }
2316                         }
2317                         if ((sys_res & RVAL_STR) && tcp->auxstr)
2318                                 tprintf(" (%s)", tcp->auxstr);
2319                 }
2320                 if (dtime) {
2321                         tv_sub(&tv, &tv, &tcp->etime);
2322                         tprintf(" <%ld.%06ld>",
2323                                 (long) tv.tv_sec, (long) tv.tv_usec);
2324                 }
2325                 printtrailer(tcp);
2326
2327                 dumpio(tcp);
2328                 if (fflush(tcp->outf) == EOF)
2329                         return -1;
2330                 tcp->flags &= ~TCB_INSYSCALL;
2331                 return 0;
2332         }
2333
2334         /* Entering system call */
2335         res = syscall_enter(tcp);
2336         if (res != 1)
2337                 return res;
2338
2339         switch (tcp->scno + NR_SYSCALL_BASE) {
2340 #ifdef LINUX
2341 #if !defined (ALPHA) && !defined(SPARC) && !defined(SPARC64) && !defined(MIPS) && !defined(HPPA) && !defined(X86_64)
2342         case SYS_socketcall:
2343                 decode_subcall(tcp, SYS_socket_subcall,
2344                         SYS_socket_nsubcalls, deref_style);
2345                 break;
2346         case SYS_ipc:
2347                 decode_subcall(tcp, SYS_ipc_subcall,
2348                         SYS_ipc_nsubcalls, shift_style);
2349                 break;
2350 #endif /* !ALPHA && !MIPS && !SPARC && !SPARC64 && !HPPA && !X86_64 */
2351 #if defined (SPARC) || defined (SPARC64)
2352         case SYS_socketcall:
2353                 sparc_socket_decode (tcp);
2354                 break;
2355 #endif
2356 #endif /* LINUX */
2357 #ifdef SVR4
2358 #ifdef SYS_pgrpsys_subcall
2359         case SYS_pgrpsys:
2360                 decode_subcall(tcp, SYS_pgrpsys_subcall,
2361                         SYS_pgrpsys_nsubcalls, shift_style);
2362                 break;
2363 #endif /* SYS_pgrpsys_subcall */
2364 #ifdef SYS_sigcall_subcall
2365         case SYS_sigcall:
2366                 decode_subcall(tcp, SYS_sigcall_subcall,
2367                         SYS_sigcall_nsubcalls, mask_style);
2368                 break;
2369 #endif /* SYS_sigcall_subcall */
2370         case SYS_msgsys:
2371                 decode_subcall(tcp, SYS_msgsys_subcall,
2372                         SYS_msgsys_nsubcalls, shift_style);
2373                 break;
2374         case SYS_shmsys:
2375                 decode_subcall(tcp, SYS_shmsys_subcall,
2376                         SYS_shmsys_nsubcalls, shift_style);
2377                 break;
2378         case SYS_semsys:
2379                 decode_subcall(tcp, SYS_semsys_subcall,
2380                         SYS_semsys_nsubcalls, shift_style);
2381                 break;
2382 #if 0 /* broken */
2383         case SYS_utssys:
2384                 decode_subcall(tcp, SYS_utssys_subcall,
2385                         SYS_utssys_nsubcalls, shift_style);
2386                 break;
2387 #endif
2388         case SYS_sysfs:
2389                 decode_subcall(tcp, SYS_sysfs_subcall,
2390                         SYS_sysfs_nsubcalls, shift_style);
2391                 break;
2392         case SYS_spcall:
2393                 decode_subcall(tcp, SYS_spcall_subcall,
2394                         SYS_spcall_nsubcalls, shift_style);
2395                 break;
2396 #ifdef SYS_context_subcall
2397         case SYS_context:
2398                 decode_subcall(tcp, SYS_context_subcall,
2399                         SYS_context_nsubcalls, shift_style);
2400                 break;
2401 #endif /* SYS_context_subcall */
2402 #ifdef SYS_door_subcall
2403         case SYS_door:
2404                 decode_subcall(tcp, SYS_door_subcall,
2405                         SYS_door_nsubcalls, door_style);
2406                 break;
2407 #endif /* SYS_door_subcall */
2408 #ifdef SYS_kaio_subcall
2409         case SYS_kaio:
2410                 decode_subcall(tcp, SYS_kaio_subcall,
2411                         SYS_kaio_nsubcalls, shift_style);
2412                 break;
2413 #endif
2414 #endif /* SVR4 */
2415 #ifdef FREEBSD
2416         case SYS_msgsys:
2417         case SYS_shmsys:
2418         case SYS_semsys:
2419                 decode_subcall(tcp, 0, 0, table_style);
2420                 break;
2421 #endif
2422 #ifdef SUNOS4
2423         case SYS_semsys:
2424                 decode_subcall(tcp, SYS_semsys_subcall,
2425                         SYS_semsys_nsubcalls, shift_style);
2426                 break;
2427         case SYS_msgsys:
2428                 decode_subcall(tcp, SYS_msgsys_subcall,
2429                         SYS_msgsys_nsubcalls, shift_style);
2430                 break;
2431         case SYS_shmsys:
2432                 decode_subcall(tcp, SYS_shmsys_subcall,
2433                         SYS_shmsys_nsubcalls, shift_style);
2434                 break;
2435 #endif
2436         }
2437
2438         internal_syscall(tcp);
2439         if (tcp->scno >=0 && tcp->scno < nsyscalls && !(qual_flags[tcp->scno] & QUAL_TRACE)) {
2440                 tcp->flags |= TCB_INSYSCALL;
2441                 return 0;
2442         }
2443
2444         if (cflag) {
2445                 gettimeofday(&tcp->etime, NULL);
2446                 tcp->flags |= TCB_INSYSCALL;
2447                 return 0;
2448         }
2449
2450         printleader(tcp);
2451         tcp->flags &= ~TCB_REPRINT;
2452         tcp_last = tcp;
2453         if (tcp->scno >= nsyscalls || tcp->scno < 0)
2454                 tprintf("syscall_%lu(", tcp->scno);
2455         else
2456                 tprintf("%s(", sysent[tcp->scno].sys_name);
2457         if (tcp->scno >= nsyscalls || tcp->scno < 0 ||
2458             ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
2459                 sys_res = printargs(tcp);
2460         else
2461                 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2462         if (fflush(tcp->outf) == EOF)
2463                 return -1;
2464         tcp->flags |= TCB_INSYSCALL;
2465         /* Measure the entrance time as late as possible to avoid errors. */
2466         if (dtime)
2467                 gettimeofday(&tcp->etime, NULL);
2468         return sys_res;
2469 }
2470
2471 int
2472 printargs(tcp)
2473 struct tcb *tcp;
2474 {
2475         if (entering(tcp)) {
2476                 int i;
2477
2478                 for (i = 0; i < tcp->u_nargs; i++)
2479                         tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
2480         }
2481         return 0;
2482 }
2483
2484 long
2485 getrval2(tcp)
2486 struct tcb *tcp;
2487 {
2488         long val = -1;
2489
2490 #ifdef LINUX
2491 #if defined (SPARC) || defined (SPARC64)
2492         struct regs regs;
2493         if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
2494                 return -1;
2495         val = regs.r_o1;
2496 #elif defined(SH)
2497         if (upeek(tcp->pid, 4*(REG_REG0+1), &val) < 0)
2498                 return -1;
2499 #endif /* SPARC || SPARC64 */
2500 #elif defined(IA64)
2501         if (upeek(tcp->pid, PT_R9, &val) < 0)
2502                 return -1;
2503 #endif /* LINUX */
2504
2505 #ifdef SUNOS4
2506         if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
2507                 return -1;
2508 #endif /* SUNOS4 */
2509
2510 #ifdef SVR4
2511 #ifdef SPARC
2512         val = tcp->status.PR_REG[R_O1];
2513 #endif /* SPARC */
2514 #ifdef I386
2515         val = tcp->status.PR_REG[EDX];
2516 #endif /* I386 */
2517 #ifdef X86_64
2518         val = tcp->status.PR_REG[RDX];
2519 #endif /* X86_64 */
2520 #ifdef MIPS
2521         val = tcp->status.PR_REG[CTX_V1];
2522 #endif /* MIPS */
2523 #endif /* SVR4 */
2524 #ifdef FREEBSD
2525         struct reg regs;
2526         pread(tcp->pfd_reg, &regs, sizeof(regs), 0);
2527         val = regs.r_edx;
2528 #endif
2529         return val;
2530 }
2531
2532 /*
2533  * Apparently, indirect system calls have already be converted by ptrace(2),
2534  * so if you see "indir" this program has gone astray.
2535  */
2536 int
2537 sys_indir(tcp)
2538 struct tcb *tcp;
2539 {
2540         int i, scno, nargs;
2541
2542         if (entering(tcp)) {
2543                 if ((scno = tcp->u_arg[0]) > nsyscalls) {
2544                         fprintf(stderr, "Bogus syscall: %u\n", scno);
2545                         return 0;
2546                 }
2547                 nargs = sysent[scno].nargs;
2548                 tprintf("%s", sysent[scno].sys_name);
2549                 for (i = 0; i < nargs; i++)
2550                         tprintf(", %#lx", tcp->u_arg[i+1]);
2551         }
2552         return 0;
2553 }
2554
2555 static int
2556 time_cmp(a, b)
2557 void *a;
2558 void *b;
2559 {
2560         return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
2561 }
2562
2563 static int
2564 syscall_cmp(a, b)
2565 void *a;
2566 void *b;
2567 {
2568         return strcmp(sysent[*((int *) a)].sys_name,
2569                 sysent[*((int *) b)].sys_name);
2570 }
2571
2572 static int
2573 count_cmp(a, b)
2574 void *a;
2575 void *b;
2576 {
2577         int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
2578
2579         return (m < n) ? 1 : (m > n) ? -1 : 0;
2580 }
2581
2582 static int (*sortfun)();
2583 static struct timeval overhead = { -1, -1 };
2584
2585 void
2586 set_sortby(sortby)
2587 char *sortby;
2588 {
2589         if (strcmp(sortby, "time") == 0)
2590                 sortfun = time_cmp;
2591         else if (strcmp(sortby, "calls") == 0)
2592                 sortfun = count_cmp;
2593         else if (strcmp(sortby, "name") == 0)
2594                 sortfun = syscall_cmp;
2595         else if (strcmp(sortby, "nothing") == 0)
2596                 sortfun = NULL;
2597         else {
2598                 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
2599                 exit(1);
2600         }
2601 }
2602
2603 void set_overhead(n)
2604 int n;
2605 {
2606         overhead.tv_sec = n / 1000000;
2607         overhead.tv_usec = n % 1000000;
2608 }
2609
2610 void
2611 call_summary(outf)
2612 FILE *outf;
2613 {
2614         int i, j;
2615         int call_cum, error_cum;
2616         struct timeval tv_cum, dtv;
2617         double percent;
2618         char *dashes = "-------------------------";
2619         char error_str[16];
2620
2621         call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
2622         if (overhead.tv_sec == -1) {
2623                 tv_mul(&overhead, &shortest, 8);
2624                 tv_div(&overhead, &overhead, 10);
2625         }
2626         for (i = 0; i < nsyscalls; i++) {
2627                 sorted_count[i] = i;
2628                 if (call_count[i] == 0)
2629                         continue;
2630                 tv_mul(&dtv, &overhead, call_count[i]);
2631                 tv_sub(&tv_count[i], &tv_count[i], &dtv);
2632                 call_cum += call_count[i];
2633                 error_cum += error_count[i];
2634                 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
2635         }
2636         if (sortfun)
2637                 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
2638         fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
2639                 "% time", "seconds", "usecs/call",
2640                 "calls", "errors", "syscall");
2641         fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2642                 dashes, dashes, dashes, dashes, dashes, dashes);
2643         for (i = 0; i < nsyscalls; i++) {
2644                 j = sorted_count[i];
2645                 if (call_count[j] == 0)
2646                         continue;
2647                 tv_div(&dtv, &tv_count[j], call_count[j]);
2648                 if (error_count[j])
2649                         sprintf(error_str, "%d", error_count[j]);
2650                 else
2651                         error_str[0] = '\0';
2652                 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
2653                 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
2654                         percent, (long) tv_count[j].tv_sec,
2655                         (long) tv_count[j].tv_usec,
2656                         (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
2657                         call_count[j], error_str, sysent[j].sys_name);
2658         }
2659         fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
2660                 dashes, dashes, dashes, dashes, dashes, dashes);
2661         if (error_cum)
2662                 sprintf(error_str, "%d", error_cum);
2663         else
2664                 error_str[0] = '\0';
2665         fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
2666                 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
2667                 call_cum, error_str, "total");
2668 }