]> granicus.if.org Git - strace/blob - syscall.c
Add sparc patches from Jakub Jelinek
[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  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  *      $Id$
30  */
31
32 #include "defs.h"
33
34 #include <signal.h>
35 #include <time.h>
36 #include <errno.h>
37 #include <sys/user.h>
38 #include <sys/syscall.h>
39 #include <sys/param.h>
40 #if __GLIBC__ == 2 && __GLIBC_MINOR__ >= 1 && (defined(I386) || defined(M68K))
41 # include <sys/reg.h>
42 #endif
43
44 #if defined LINUX && __GLIBC__ < 2
45 #include <linux/ptrace.h>
46 #endif /* LINUX */
47
48 #ifndef SYS_ERRLIST_DECLARED
49 extern int sys_nerr;
50 extern char *sys_errlist[];
51 #endif /* SYS_ERRLIST_DECLARED */
52
53 #ifdef LINUX
54 #ifndef ERESTARTSYS
55 #define ERESTARTSYS     512
56 #endif
57 #ifndef ERESTARTNOINTR
58 #define ERESTARTNOINTR  513
59 #endif
60 #ifndef ERESTARTNOHAND
61 #define ERESTARTNOHAND  514     /* restart if no handler.. */
62 #endif
63 #ifndef ENOIOCTLCMD
64 #define ENOIOCTLCMD     515     /* No ioctl command */
65 #endif
66 #ifndef NSIG
67 #define NSIG 32
68 #endif
69 #ifdef ARM
70 #undef NSIG
71 #define NSIG 32
72 #endif
73 #endif /* LINUX */
74
75 #include "syscall.h"
76
77 /* Define these shorthand notations to simplify the syscallent files. */
78 #define TF TRACE_FILE
79 #define TI TRACE_IPC
80 #define TN TRACE_NETWORK
81 #define TP TRACE_PROCESS
82 #define TS TRACE_SIGNAL
83
84 struct sysent sysent0[] = {
85 #include "syscallent.h"
86 };
87 int nsyscalls0 = sizeof sysent0 / sizeof sysent0[0];
88
89 #if SUPPORTED_PERSONALITIES >= 2
90 struct sysent sysent1[] = {
91 #include "syscallent1.h"
92 };
93 int nsyscalls1 = sizeof sysent1 / sizeof sysent1[0];
94 #endif /* SUPPORTED_PERSONALITIES >= 2 */
95
96 #if SUPPORTED_PERSONALITIES >= 3
97 struct sysent sysent2[] = {
98 #include "syscallent2.h"
99 };
100 int nsyscalls2 = sizeof sysent2 / sizeof sysent2[0];
101 #endif /* SUPPORTED_PERSONALITIES >= 3 */
102
103 struct sysent *sysent;
104 int nsyscalls;
105
106 /* Now undef them since short defines cause wicked namespace pollution. */
107 #undef TF
108 #undef TI
109 #undef TN
110 #undef TP
111 #undef TS
112
113 char *errnoent0[] = {
114 #include "errnoent.h"
115 };
116 int nerrnos0 = sizeof errnoent0 / sizeof errnoent0[0];
117
118 #if SUPPORTED_PERSONALITIES >= 2
119 char *errnoent1[] = {
120 #include "errnoent1.h"
121 };
122 int nerrnos1 = sizeof errnoent1 / sizeof errnoent1[0];
123 #endif /* SUPPORTED_PERSONALITIES >= 2 */
124
125 #if SUPPORTED_PERSONALITIES >= 3
126 char *errnoent2[] = {
127 #include "errnoent2.h"
128 };
129 int nerrnos2 = sizeof errnoent2 / sizeof errnoent2[0];
130 #endif /* SUPPORTED_PERSONALITIES >= 3 */
131
132 char **errnoent;
133 int nerrnos;
134
135 int current_personality;
136
137 int
138 set_personality(int personality)
139 {
140         switch (personality) {
141         case 0:
142                 errnoent = errnoent0;
143                 nerrnos = nerrnos0;
144                 sysent = sysent0;
145                 nsyscalls = nsyscalls0;
146                 ioctlent = ioctlent0;
147                 nioctlents = nioctlents0;
148                 signalent = signalent0;
149                 nsignals = nsignals0;
150                 break;
151
152 #if SUPPORTED_PERSONALITIES >= 2
153         case 1:
154                 errnoent = errnoent1;
155                 nerrnos = nerrnos1;
156                 sysent = sysent1;
157                 nsyscalls = nsyscalls1;
158                 ioctlent = ioctlent1;
159                 nioctlents = nioctlents1;
160                 signalent = signalent1;
161                 nsignals = nsignals1;
162                 break;
163 #endif /* SUPPORTED_PERSONALITIES >= 2 */
164
165 #if SUPPORTED_PERSONALITIES >= 3
166         case 2:
167                 errnoent = errnoent2;
168                 nerrnos = nerrnos2;
169                 sysent = sysent2;
170                 nsyscalls = nsyscalls2;
171                 ioctlent = ioctlent2;
172                 nioctlents = nioctlents2;
173                 signalent = signalent2;
174                 nsignals = nsignals2;
175                 break;
176 #endif /* SUPPORTED_PERSONALITIES >= 3 */
177
178         default:
179                 return -1;
180         }
181
182         current_personality = personality;
183         return 0;
184 }
185
186 int qual_flags[MAX_QUALS];
187
188 static int call_count[MAX_QUALS];
189 static int error_count[MAX_QUALS];
190 static struct timeval tv_count[MAX_QUALS];
191 static int sorted_count[MAX_QUALS];
192
193 static struct timeval shortest = { 1000000, 0 };
194
195 static int lookup_syscall(), lookup_signal(), lookup_fault(), lookup_desc();
196
197 static struct qual_options {
198         int bitflag;
199         char *option_name;
200         int (*lookup)();
201         char *argument_name;
202 } qual_options[] = {
203         { QUAL_TRACE,   "trace",        lookup_syscall, "system call"   },
204         { QUAL_TRACE,   "t",            lookup_syscall, "system call"   },
205         { QUAL_ABBREV,  "abbrev",       lookup_syscall, "system call"   },
206         { QUAL_ABBREV,  "a",            lookup_syscall, "system call"   },
207         { QUAL_VERBOSE, "verbose",      lookup_syscall, "system call"   },
208         { QUAL_VERBOSE, "v",            lookup_syscall, "system call"   },
209         { QUAL_RAW,     "raw",          lookup_syscall, "system call"   },
210         { QUAL_RAW,     "x",            lookup_syscall, "system call"   },
211         { QUAL_SIGNAL,  "signal",       lookup_signal,  "signal"        },
212         { QUAL_SIGNAL,  "signals",      lookup_signal,  "signal"        },
213         { QUAL_SIGNAL,  "s",            lookup_signal,  "signal"        },
214         { QUAL_FAULT,   "fault",        lookup_fault,   "fault"         },
215         { QUAL_FAULT,   "faults",       lookup_fault,   "fault"         },
216         { QUAL_FAULT,   "m",            lookup_fault,   "fault"         },
217         { QUAL_READ,    "read",         lookup_desc,    "descriptor"    },
218         { QUAL_READ,    "reads",        lookup_desc,    "descriptor"    },
219         { QUAL_READ,    "r",            lookup_desc,    "descriptor"    },
220         { QUAL_WRITE,   "write",        lookup_desc,    "descriptor"    },
221         { QUAL_WRITE,   "writes",       lookup_desc,    "descriptor"    },
222         { QUAL_WRITE,   "w",            lookup_desc,    "descriptor"    },
223         { 0,            NULL,           NULL,           NULL            },
224 };
225
226 static int
227 lookup_syscall(s)
228 char *s;
229 {
230         int i;
231
232         for (i = 0; i < nsyscalls; i++) {
233                 if (strcmp(s, sysent[i].sys_name) == 0)
234                         return i;
235         }
236         return -1;
237 }
238
239 static int
240 lookup_signal(s)
241 char *s;
242 {
243         int i;
244         char buf[32];
245
246         if (s && *s && isdigit(*s))
247                 return atoi(s);
248         strcpy(buf, s);
249         s = buf;
250         for (i = 0; s[i]; i++)
251                 s[i] = toupper(s[i]);
252         if (strncmp(s, "SIG", 3) == 0)
253                 s += 3;
254         for (i = 0; i <= NSIG; i++) {
255                 if (strcmp(s, signame(i) + 3) == 0)
256                         return i;
257         }
258         return -1;
259 }
260
261 static int
262 lookup_fault(s)
263 char *s;
264 {
265         return -1;
266 }
267
268 static int
269 lookup_desc(s)
270 char *s;
271 {
272         if (s && *s && isdigit(*s))
273                 return atoi(s);
274         return -1;
275 }
276
277 static int
278 lookup_class(s)
279 char *s;
280 {
281         if (strcmp(s, "file") == 0)
282                 return TRACE_FILE;
283         if (strcmp(s, "ipc") == 0)
284                 return TRACE_IPC;
285         if (strcmp(s, "network") == 0)
286                 return TRACE_NETWORK;
287         if (strcmp(s, "process") == 0)
288                 return TRACE_PROCESS;
289         if (strcmp(s, "signal") == 0)
290                 return TRACE_SIGNAL;
291         return -1;
292 }
293
294 void
295 qualify(s)
296 char *s;
297 {
298         struct qual_options *opt;
299         int not;
300         char *p;
301         int i, n;
302
303         opt = &qual_options[0];
304         for (i = 0; (p = qual_options[i].option_name); i++) {
305                 n = strlen(p);
306                 if (strncmp(s, p, n) == 0 && s[n] == '=') {
307                         opt = &qual_options[i];
308                         s += n + 1;
309                         break;
310                 }
311         }
312         not = 0;
313         if (*s == '!') {
314                 not = 1;
315                 s++;
316         }
317         if (strcmp(s, "none") == 0) {
318                 not = 1 - not;
319                 s = "all";
320         }
321         if (strcmp(s, "all") == 0) {
322                 for (i = 0; i < MAX_QUALS; i++) {
323                         if (not)
324                                 qual_flags[i] &= ~opt->bitflag;
325                         else
326                                 qual_flags[i] |= opt->bitflag;
327                 }
328                 return;
329         }
330         for (i = 0; i < MAX_QUALS; i++) {
331                 if (not)
332                         qual_flags[i] |= opt->bitflag;
333                 else
334                         qual_flags[i] &= ~opt->bitflag;
335         }
336         for (p = strtok(s, ","); p; p = strtok(NULL, ",")) {
337                 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
338                         for (i = 0; i < MAX_QUALS; i++) {
339                                 if (sysent[i].sys_flags & n) {
340                                         if (not)
341                                                 qual_flags[i] &= ~opt->bitflag;
342                                         else
343                                                 qual_flags[i] |= opt->bitflag;
344                                 }
345                         }
346                         continue;
347                 }
348                 if ((n = (*opt->lookup)(p)) < 0) {
349                         fprintf(stderr, "strace: invalid %s `%s'\n",
350                                 opt->argument_name, p);
351                         exit(1);
352                 }
353                 if (not)
354                         qual_flags[n] &= ~opt->bitflag;
355                 else
356                         qual_flags[n] |= opt->bitflag;
357         }
358         return;
359 }
360
361 static void
362 dumpio(tcp)
363 struct tcb *tcp;
364 {
365         if (syserror(tcp))
366                 return;
367         if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
368                 return;
369 #ifdef __arm__
370         switch (tcp->scno + __NR_SYSCALL_BASE) {
371 #else
372         switch (tcp->scno) {
373 #endif
374         case SYS_read:
375 #ifdef SYS_recv
376         case SYS_recv:
377 #endif
378 #ifdef SYS_recvfrom
379         case SYS_recvfrom:
380 #endif
381                 if (qual_flags[tcp->u_arg[0]] & QUAL_READ)
382                         dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
383                 break;
384         case SYS_write:
385 #ifdef SYS_send
386         case SYS_send:
387 #endif
388 #ifdef SYS_sendto
389         case SYS_sendto:
390 #endif
391                 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE)
392                         dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
393                 break;
394         }
395 }
396
397 enum subcall_style { shift_style, deref_style, mask_style };
398
399 #if !(defined(LINUX) && defined(ALPHA))
400
401 const int socket_map [] = {
402                /* SYS_SOCKET      */ 97,
403                /* SYS_BIND        */ 104,
404                /* SYS_CONNECT     */ 98,
405                /* SYS_LISTEN      */ 106,
406                /* SYS_ACCEPT      */ 99,
407                /* SYS_GETSOCKNAME */ 150,
408                /* SYS_GETPEERNAME */ 141,
409                /* SYS_SOCKETPAIR  */ 135,
410                /* SYS_SEND        */ 101,
411                /* SYS_RECV        */ 102,
412                /* SYS_SENDTO      */ 133,
413                /* SYS_RECVFROM    */ 125,
414                /* SYS_SHUTDOWN    */ 134,
415                /* SYS_SETSOCKOPT  */ 105,
416                /* SYS_GETSOCKOPT  */ 118,
417                /* SYS_SENDMSG     */ 114,
418                /* SYS_RECVMSG     */ 113
419 };
420
421 void
422 sparc_socket_decode (struct tcb *tcp)
423 {
424         volatile long addr;
425         volatile int i, n;
426
427         if (tcp->u_arg [0] < 1 || tcp->u_arg [0] > sizeof(socket_map)/sizeof(int)+1){
428                 return;
429         }
430         tcp->scno = socket_map [tcp->u_arg [0]-1];
431         n = tcp->u_nargs = sysent [tcp->scno].nargs;
432         addr = tcp->u_arg [1];
433         for (i = 0; i < n; i++){
434                 int arg;
435                 if (umoven (tcp, addr, sizeof (arg), (void *) &arg) < 0)
436                         arg = 0;
437                 tcp->u_arg [i] = arg;
438                 addr += sizeof (arg);
439         }
440 }
441
442 static void
443 decode_subcall(tcp, subcall, nsubcalls, style)
444 struct tcb *tcp;
445 int subcall;
446 int nsubcalls;
447 enum subcall_style style;
448 {
449         int i, addr, mask, arg;
450
451         if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= nsubcalls)
452                 return;
453         switch (style) {
454         case shift_style:
455                 tcp->scno = subcall + tcp->u_arg[0];
456                 if (sysent[tcp->scno].nargs != -1)
457                         tcp->u_nargs = sysent[tcp->scno].nargs;
458                 else
459                         tcp->u_nargs--;
460                 for (i = 0; i < tcp->u_nargs; i++)
461                         tcp->u_arg[i] = tcp->u_arg[i + 1];
462                 break;
463         case deref_style:
464                 tcp->scno = subcall + tcp->u_arg[0];
465                 addr = tcp->u_arg[1];
466                 for (i = 0; i < sysent[tcp->scno].nargs; i++) {
467                         if (umove(tcp, addr, &arg) < 0)
468                                 arg = 0;
469                         tcp->u_arg[i] = arg;
470                         addr += sizeof(arg);
471                 }
472                 tcp->u_nargs = sysent[tcp->scno].nargs;
473                 break;
474         case mask_style:
475                 mask = (tcp->u_arg[0] >> 8) & 0xff;
476                 tcp->u_arg[0] &= 0xff;
477                 for (i = 0; mask; i++)
478                         mask >>= 1;
479                 tcp->scno = subcall + i;
480                 if (sysent[tcp->scno].nargs != -1)
481                         tcp->u_nargs = sysent[tcp->scno].nargs;
482                 break;
483         }
484 }
485 #endif
486
487 struct tcb *tcp_last = NULL;
488
489 static int
490 internal_syscall(tcp)
491 struct tcb *tcp;
492 {
493         /*
494          * We must always trace a few critical system calls in order to
495          * correctly support following forks in the presence of tracing
496          * qualifiers.
497          */
498 #ifdef __arm__
499         switch (tcp->scno + __NR_SYSCALL_BASE) {
500 #else
501         switch (tcp->scno) {
502 #endif
503 #ifdef SYS_fork
504         case SYS_fork:
505 #endif
506 #ifdef SYS_vfork
507         case SYS_vfork:
508 #endif
509 #ifdef SYS_clone
510         case SYS_clone:
511 #endif
512                 internal_fork(tcp);
513                 break;
514
515 #ifdef SYS_execv
516         case SYS_execv:
517 #endif
518 #ifdef SYS_execve
519         case SYS_execve:
520 #endif
521                 internal_exec(tcp);
522                 break;
523
524 #ifdef SYS_wait
525         case SYS_wait:
526 #endif
527 #ifdef SYS_wait4
528         case SYS_wait4:
529 #endif
530 #ifdef SYS_waitpid
531         case SYS_waitpid:
532 #endif
533 #ifdef SYS_waitsys
534         case SYS_waitsys:
535 #endif
536                 internal_wait(tcp);
537                 break;
538
539 #ifdef SYS_exit
540         case SYS_exit:
541 #endif
542                 internal_exit(tcp);
543                 break;
544         }
545         return 0;
546 }
547
548 int
549 trace_syscall(tcp)
550 struct tcb *tcp;
551 {
552         int sys_res;
553         struct timeval tv;
554         long scno = 0;
555 #ifdef LINUX
556 #if defined (I386)
557         long eax;
558 #elif defined (POWERPC)
559         long result,flags;
560 #elif defined (M68K)
561         int d0;
562 #elif defined (ARM)
563         int r0;
564 #elif defined (ALPHA)
565         long r0;
566         long a3;
567 #elif defined (SPARC)
568         struct pt_regs regs;
569         unsigned long trap;
570 #endif 
571 #endif /* LINUX */
572
573 #ifndef SVR4
574         int pid = tcp->pid;
575 #endif /* !SVR4 */
576
577         /* Measure the exit time as early as possible to avoid errors. */
578         if (dtime && (tcp->flags & TCB_INSYSCALL))
579                 gettimeofday(&tv, NULL);
580 #ifdef LINUX
581 #if defined (POWERPC)
582         if (upeek(pid, 4*PT_R0, &scno) < 0)
583                 return -1;
584         if (!(tcp->flags & TCB_INSYSCALL)) {
585                 /* Check if we return from execve. */
586                 if (scno == 0 && (tcp->flags & TCB_WAITEXECVE)) {
587                         tcp->flags &= ~TCB_WAITEXECVE;
588                         return 0;
589                 }
590         }
591 #elif defined (I386)
592         if (upeek(pid, 4*ORIG_EAX, &scno) < 0)
593                 return -1;
594 #elif defined (ARM)
595         { 
596             long pc;
597             upeek(pid, 4*15, &pc);
598             umoven(tcp, pc-4, 4, (char *)&scno);
599             scno &= 0x000fffff;
600         }
601 #elif defined (M68K)
602         if (upeek(pid, 4*PT_ORIG_D0, &scno) < 0)
603                 return -1;
604 #elif defined (ALPHA)
605         if (upeek(pid, REG_A3, &a3) < 0)
606                 return -1;
607
608         if (!(tcp->flags & TCB_INSYSCALL)) {
609                 if (upeek(pid, REG_R0, &scno) < 0)
610                         return -1;
611
612                 /* Check if we return from execve. */
613                 if (scno == 0 && tcp->flags & TCB_WAITEXECVE) {
614                         tcp->flags &= ~TCB_WAITEXECVE;
615                         return 0;
616                 }
617
618                 /*
619                  * Do some sanity checks to figure out if it's
620                  * really a syscall entry
621                  */
622                 if (scno < 0 || scno > nsyscalls) {
623                         if (a3 == 0 || a3 == -1) {
624                                 if (debug)
625                                         fprintf (stderr, "stray syscall exit: r0 = %ld\n", scno);
626                                 return 0;
627                         }
628                 }
629         }
630         else {
631                 if (upeek(pid, REG_R0, &r0) < 0)
632                         return -1;
633         }
634 #elif defined (SPARC)
635         /* Everything we need is in the current register set. */
636         if (ptrace(PTRACE_GETREGS,pid,(char *)&regs,0) < 0)
637                 return -1;
638
639         memmove (&regs.u_regs [1], &regs.u_regs [0],
640                  sizeof (regs.u_regs) - sizeof (regs.u_regs [0]));
641
642         /* If we are entering, then disassemble the syscall trap. */
643         if (!(tcp->flags & TCB_INSYSCALL)) {
644                 /* Retrieve the syscall trap instruction. */
645                 errno = 0;
646                 trap = ptrace(PTRACE_PEEKTEXT,pid,(char *)regs.pc,0);
647                 if (errno)
648                         return -1;
649
650                 /* Disassemble the trap to see what personality to use. */
651                 switch (trap) {
652                 case 0x91d02010:
653                         /* Linux/SPARC syscall trap. */
654                         set_personality(0);
655                         break;
656                 case 0x91d0206d:
657                         /* Linux/SPARC64 syscall trap. */
658                         fprintf(stderr,"syscall: Linux/SPARC64 not supported yet\n");
659                         return -1;
660                 case 0x91d02000:
661                         /* SunOS syscall trap. (pers 1) */
662                         fprintf(stderr,"syscall: SunOS no support\n");
663                         return -1;
664                 case 0x91d02008:
665                         /* Solaris 2.x syscall trap. (per 2) */
666                         set_personality(1);
667                         break; 
668                 case 0x91d02009:
669                         /* NetBSD/FreeBSD syscall trap. */
670                         fprintf(stderr,"syscall: NetBSD/FreeBSD not supported\n");
671                         return -1;
672                 case 0x91d02027:
673                         /* Solaris 2.x gettimeofday */
674                         set_personality(1);
675                         break;
676                 default:
677                         /* Unknown syscall trap. */
678                         if(tcp->flags & TCB_WAITEXECVE) {
679                                 tcp->flags &= ~TCB_WAITEXECVE;
680                                 return 0;
681                         }
682                         fprintf(stderr,"syscall: unknown syscall trap %08x %08x\n", trap, regs.pc);
683                         return -1;
684                 }
685
686                 /* Extract the system call number from the registers. */
687                 if (trap == 0x91d02027)
688                         scno = 156;
689                 else
690                         scno = regs.u_regs[UREG_G1];
691                 if (scno == 0) {
692                         scno = regs.u_regs[UREG_I0];
693                         memmove (&regs.u_regs[UREG_I0], &regs.u_regs[UREG_I1], 7*sizeof(regs.u_regs[0]));
694                 }
695         }
696 #endif 
697 #endif /* LINUX */
698 #ifdef SUNOS4
699         if (upeek(pid, uoff(u_arg[7]), &scno) < 0)
700                 return -1;
701 #endif
702 #ifdef SVR4
703 #ifdef HAVE_PR_SYSCALL
704         scno = tcp->status.pr_syscall;
705 #else /* !HAVE_PR_SYSCALL */
706         scno = tcp->status.pr_what;
707 #endif /* !HAVE_PR_SYSCALL */
708         if (!(tcp->flags & TCB_INSYSCALL)) {
709                 if (tcp->status.pr_why != PR_SYSENTRY) {
710                         if (
711                             scno == SYS_fork
712 #ifdef SYS_vfork
713                             || scno == SYS_vfork
714 #endif /* SYS_vfork */
715                             ) {
716                                 /* We are returning in the child, fake it. */
717                                 tcp->status.pr_why = PR_SYSENTRY;
718                                 trace_syscall(tcp);
719                                 tcp->status.pr_why = PR_SYSEXIT;
720                         }
721                         else {
722                                 fprintf(stderr, "syscall: missing entry\n");
723                                 tcp->flags |= TCB_INSYSCALL;
724                         }
725                 }
726         }
727         else {
728                 if (tcp->status.pr_why != PR_SYSEXIT) {
729                         fprintf(stderr, "syscall: missing exit\n");
730                         tcp->flags &= ~TCB_INSYSCALL;
731                 }
732         }
733 #endif /* SVR4 */
734 #ifdef SUNOS4
735         if (!(tcp->flags & TCB_INSYSCALL)) {
736                 if (scno == 0) {
737                         fprintf(stderr, "syscall: missing entry\n");
738                         tcp->flags |= TCB_INSYSCALL;
739                 }
740         }
741         else {
742                 if (scno != 0) {
743                         if (debug) {
744                                 /*
745                                  * This happens when a signal handler
746                                  * for a signal which interrupted a
747                                  * a system call makes another system call.
748                                  */
749                                 fprintf(stderr, "syscall: missing exit\n");
750                         }
751                         tcp->flags &= ~TCB_INSYSCALL;
752                 }
753         }
754 #endif /* SUNOS4 */
755 #ifdef LINUX
756 #if defined (I386)
757         if (upeek(pid, 4*EAX, &eax) < 0)
758                 return -1;
759         if (eax != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
760                 if (debug)
761                         fprintf(stderr, "stray syscall exit: eax = %ld\n", eax);
762                 return 0;
763         }
764 #elif defined (POWERPC)
765 # define SO_MASK 0x10000000
766         if (upeek(pid, 4*PT_CCR, &flags) < 0)
767                 return -1;
768         if (upeek(pid, 4*PT_R3, &result) < 0)
769                 return -1;
770         if (flags & SO_MASK)
771                 result = -result;
772 #elif defined (M68K)
773         if (upeek(pid, 4*PT_D0, &d0) < 0)
774                 return -1;
775         if (d0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
776                 if (debug)
777                         fprintf(stderr, "stray syscall exit: d0 = %ld\n", d0);
778                 return 0;
779         }
780 #elif defined (ARM)
781         if (upeek(pid, 4*0, (long *)&r0) < 0)
782                 return -1;
783         if ( 0 && r0 != -ENOSYS && !(tcp->flags & TCB_INSYSCALL)) {
784                 if (debug)
785                         fprintf(stderr, "stray syscall exit: d0 = %ld\n", r0);
786                 return 0;
787         }
788 #else
789 #endif
790 #endif /* LINUX */
791
792         if (tcp->flags & TCB_INSYSCALL) {
793                 long u_error;
794
795 #ifdef LINUX
796 #ifdef I386
797                 if (eax < 0 && -eax < nerrnos) {
798                         tcp->u_rval = -1;
799                         u_error = -eax;
800                 }
801                 else {
802                         tcp->u_rval = eax;
803                         u_error = 0;
804                 }
805 #else /* !I386 */
806 #ifdef POWERPC
807                 if (result && (unsigned) -result < nerrnos) {
808                         tcp->u_rval = -1;
809                         u_error = -result;
810                 }
811                 else {
812                         tcp->u_rval = result;
813                         u_error = 0;
814                 }
815 #else /* !POWERPC */
816 #ifdef M68K
817                 if (d0 && (unsigned) -d0 < nerrnos) {
818                         tcp->u_rval = -1;
819                         u_error = -d0;
820                 }
821                 else {
822                         tcp->u_rval = d0;
823                         u_error = 0;
824                 }
825 #else /* !M68K */
826 #ifdef ARM
827                 if (r0 && (unsigned) -r0 < nerrnos) {
828                         tcp->u_rval = -1;
829                         u_error = -r0;
830                 }
831                 else {
832                         tcp->u_rval = r0;
833                         u_error = 0;
834                 }
835 #else /* !ARM */
836 #ifdef ALPHA
837                 if (a3) {
838                         tcp->u_rval = -1;
839                         u_error = r0;
840                 }
841                 else {
842                         tcp->u_rval = r0;
843                         u_error = 0;
844                 }
845 #else /* !ALPHA */
846 #ifdef SPARC
847                 if (regs.psr & PSR_C) {
848                         tcp->u_rval = -1;
849                         u_error = regs.u_regs[UREG_I0];
850                 }
851                 else {
852                         tcp->u_rval = regs.u_regs[UREG_I0];
853                         u_error = 0;
854                 }
855 #endif /* SPARC */
856 #endif /* ALPHA */
857 #endif /* ARM */
858 #endif /* M68K */
859 #endif /* POWERPC */
860 #endif /* I386 */
861 #endif /* LINUX */
862 #ifdef SUNOS4
863                 /* get error code from user struct */
864                 if (upeek(pid, uoff(u_error), &u_error) < 0)
865                         return -1;
866                 u_error >>= 24; /* u_error is a char */
867
868                 /* get system call return value */
869                 if (upeek(pid, uoff(u_rval1), &tcp->u_rval) < 0)
870                         return -1;
871 #endif /* SUNOS4 */
872 #ifdef SVR4
873 #ifdef SPARC
874                 /* Judicious guessing goes a long way. */
875                 if (tcp->status.pr_reg[R_PSR] & 0x100000) {
876                         tcp->u_rval = -1;
877                         u_error = tcp->status.pr_reg[R_O0];
878                 }
879                 else {
880                         tcp->u_rval = tcp->status.pr_reg[R_O0];
881                         u_error = 0;
882                 }
883 #endif /* SPARC */
884 #ifdef I386
885                 /* Wanna know how to kill an hour single-stepping? */
886                 if (tcp->status.pr_reg[EFL] & 0x1) {
887                         tcp->u_rval = -1;
888                         u_error = tcp->status.pr_reg[EAX];
889                 }
890                 else {
891                         tcp->u_rval = tcp->status.pr_reg[EAX];
892                         u_error = 0;
893                 }
894 #endif /* I386 */
895 #ifdef MIPS
896                 if (tcp->status.pr_reg[CTX_A3]) {
897                         tcp->u_rval = -1;
898                         u_error = tcp->status.pr_reg[CTX_V0];
899                 }
900                 else {
901                         tcp->u_rval = tcp->status.pr_reg[CTX_V0];
902                         u_error = 0;
903                 }
904 #endif /* MIPS */
905 #endif /* SVR4 */
906                 tcp->u_error = u_error;
907
908                 internal_syscall(tcp);
909                 if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
910                         tcp->flags &= ~TCB_INSYSCALL;
911                         return 0;
912                 }
913
914                 if (tcp->flags & TCB_REPRINT) {
915                         printleader(tcp);
916                         tprintf("<... ");
917                         if (tcp->scno >= nsyscalls)
918                                 tprintf("syscall_%lu", tcp->scno);
919                         else
920                                 tprintf("%s", sysent[tcp->scno].sys_name);
921                         tprintf(" resumed> ");
922                 }
923
924                 if (cflag) {
925                         call_count[tcp->scno]++;
926                         if (u_error)
927                                 error_count[tcp->scno]++;
928                         tv_sub(&tv, &tv, &tcp->etime);
929 #ifdef LINUX
930                         if (tv_cmp(&tv, &tcp->dtime) > 0) {
931                                 static struct timeval one_tick =
932                                         { 0, 1000000 / HZ };
933
934                                 if (tv_nz(&tcp->dtime))
935                                         tv = tcp->dtime;
936                                 else if (tv_cmp(&tv, &one_tick) > 0) {
937                                         if (tv_cmp(&shortest, &one_tick) < 0)
938                                                 tv = shortest;
939                                         else
940                                                 tv = one_tick;
941                                 }
942                         }
943 #endif /* LINUX */
944                         if (tv_cmp(&tv, &shortest) < 0)
945                                 shortest = tv;
946                         tv_add(&tv_count[tcp->scno],
947                                 &tv_count[tcp->scno], &tv);
948                         tcp->flags &= ~TCB_INSYSCALL;
949                         return 0;
950                 }
951
952                 if (tcp->scno >= nsyscalls
953                     || (qual_flags[tcp->scno] & QUAL_RAW))
954                         sys_res = printargs(tcp);
955                 else
956                         sys_res = (*sysent[tcp->scno].sys_func)(tcp);
957                 u_error = tcp->u_error;
958                 tprintf(") ");
959                 tabto(acolumn);
960                 if (qual_flags[tcp->scno] & QUAL_RAW) {
961                         if (u_error)
962                                 tprintf("= -1 (errno %ld)", u_error);
963                         else
964                                 tprintf("= %#lx", tcp->u_rval);
965                 }
966                 else if (!(sys_res & RVAL_NONE) && u_error) {
967 #ifdef LINUX
968                         switch (u_error) {
969                         case ERESTARTSYS:
970                                 tprintf("= ? ERESTARTSYS (To be restarted)");
971                                 break;
972                         case ERESTARTNOINTR:
973                                 tprintf("= ? ERESTARTNOINTR (To be restarted)");
974                                 break;
975                         case ERESTARTNOHAND:
976                                 tprintf("= ? ERESTARTNOHAND (To be restarted)");
977                                 break;
978                         default:
979 #endif /* LINUX */
980                                 tprintf("= -1 ");
981                                 if (u_error < nerrnos && u_error < sys_nerr)
982                                         tprintf("%s (%s)", errnoent[u_error],
983                                                 sys_errlist[u_error]);
984                                 else if (u_error < nerrnos)
985                                         tprintf("%s (errno %ld)",
986                                                 errnoent[u_error], u_error);
987                                 else if (u_error < sys_nerr)
988                                         tprintf("ERRNO_%ld (%s)", u_error,
989                                                 sys_errlist[u_error]);
990                                 else
991                                         tprintf("E??? (errno %ld)", u_error);
992 #ifdef LINUX
993                                 break;
994                         }
995 #endif /* LINUX */
996                 }
997                 else {
998                         if (sys_res & RVAL_NONE)
999                                 tprintf("= ?");
1000                         else {
1001                                 switch (sys_res & RVAL_MASK) {
1002                                 case RVAL_HEX:
1003                                         tprintf("= %#lx", tcp->u_rval);
1004                                         break;
1005                                 case RVAL_OCTAL:
1006                                         tprintf("= %#lo", tcp->u_rval);
1007                                         break;
1008                                 case RVAL_UDECIMAL:
1009                                         tprintf("= %lu", tcp->u_rval);
1010                                         break;
1011                                 case RVAL_DECIMAL:
1012                                         tprintf("= %ld", tcp->u_rval);
1013                                         break;
1014                                 default:
1015                                         fprintf(stderr,
1016                                                 "invalid rval format\n");
1017                                         break;
1018                                 }
1019                         }
1020                         if ((sys_res & RVAL_STR) && tcp->auxstr)
1021                                 tprintf(" (%s)", tcp->auxstr);
1022                 }
1023                 if (dtime) {
1024                         tv_sub(&tv, &tv, &tcp->etime);
1025                         tprintf(" <%ld.%06ld>",
1026                                 (long) tv.tv_sec, (long) tv.tv_usec);
1027                 }
1028                 printtrailer(tcp);
1029
1030                 dumpio(tcp);
1031                 if (fflush(tcp->outf) == EOF)
1032                         return -1;
1033                 tcp->flags &= ~TCB_INSYSCALL;
1034                 return 0;
1035         }
1036
1037         /* Entering system call */
1038         tcp->scno = scno;
1039 #ifdef LINUX
1040 #if defined (ALPHA)
1041         {
1042                 int i;
1043                 tcp->u_nargs = sysent[tcp->scno].nargs;
1044                 for (i = 0; i < tcp->u_nargs; i++) {
1045                         /* WTA: if scno is out-of-bounds this will bomb. Add range-check
1046                          * for scno somewhere above here!
1047                          */
1048                         if (upeek(pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1049                                 return -1;
1050                 }
1051         }
1052 #elif defined (POWERPC)
1053         {
1054                 int i;
1055                 tcp->u_nargs = sysent[tcp->scno].nargs;
1056                 for (i = 0; i < tcp->u_nargs; i++) {
1057                         if (upeek(pid, (i==0) ? (4*PT_ORIG_R3) : ((i+PT_R3)*4), &tcp->u_arg[i]) < 0)
1058                                 return -1;
1059                 }
1060         }
1061 #elif defined (SPARC)
1062         {
1063                 int i, offset;
1064                  
1065                 offset = UREG_I0;
1066                 tcp->u_nargs = sysent[tcp->scno].nargs;
1067                 for (i = 0; i < tcp->u_nargs; i++)
1068                         tcp->u_arg[i] = regs.u_regs[offset + i];
1069         }
1070 #else 
1071         {
1072                 int i;
1073                 tcp->u_nargs = sysent[tcp->scno].nargs;
1074                 for (i = 0; i < tcp->u_nargs; i++) {
1075                         if (upeek(pid, i*4, &tcp->u_arg[i]) < 0)
1076                                 return -1;
1077                 }
1078         }
1079 #endif 
1080 #endif /* LINUX */
1081 #ifdef SUNOS4
1082         {
1083                 int i;
1084                 tcp->u_nargs = sysent[tcp->scno].nargs;
1085                 for (i = 0; i < tcp->u_nargs; i++) {
1086                         struct user *u;
1087
1088                         if (upeek(pid, uoff(u_arg[0]) +
1089                             (i*sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1090                                 return -1;
1091                 }
1092         }
1093 #endif /* SUNOS4 */
1094 #ifdef SVR4
1095 #ifdef MIPS
1096         /*
1097          * SGI is broken: even though it has pr_sysarg, it doesn't
1098          * set them on system call entry.  Get a clue.
1099          */
1100         if (sysent[tcp->scno].nargs != -1)
1101                 tcp->u_nargs = sysent[tcp->scno].nargs;
1102         else
1103                 tcp->u_nargs = tcp->status.pr_nsysarg;
1104         if (tcp->u_nargs > 4) {
1105                 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1106                         4*sizeof(tcp->u_arg[0]));
1107                 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1108                         (tcp->u_nargs - 4)*sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1109         }
1110         else {
1111                 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1112                         tcp->u_nargs*sizeof(tcp->u_arg[0]));
1113         }
1114 #else /* !MIPS */
1115 #ifdef HAVE_PR_SYSCALL
1116         if (sysent[tcp->scno].nargs != -1)
1117                 tcp->u_nargs = sysent[tcp->scno].nargs;
1118         else
1119                 tcp->u_nargs = tcp->status.pr_nsysarg;
1120         {
1121                 int i;
1122                 for (i = 0; i < tcp->u_nargs; i++)
1123                         tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1124         }
1125 #else /* !HAVE_PR_SYSCALL */
1126 #ifdef I386
1127         if (sysent[tcp->scno].nargs != -1)
1128                 tcp->u_nargs = sysent[tcp->scno].nargs;
1129         else
1130                 tcp->u_nargs = 5;
1131         umoven(tcp, tcp->status.pr_reg[UESP] + 4,
1132                 tcp->u_nargs*sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1133 #endif /* I386 */
1134 #endif /* !HAVE_PR_SYSCALL */
1135 #endif /* !MIPS */
1136 #endif /* SVR4 */
1137 #ifdef __arm__
1138         switch (tcp->scno + __NR_SYSCALL_BASE) {
1139 #else
1140         switch (tcp->scno) {
1141 #endif
1142 #ifdef LINUX
1143 #if !defined (ALPHA) && !defined(SPARC)
1144         case SYS_socketcall:
1145                 decode_subcall(tcp, SYS_socket_subcall,
1146                         SYS_socket_nsubcalls, deref_style);
1147                 break;
1148         case SYS_ipc:
1149                 decode_subcall(tcp, SYS_ipc_subcall,
1150                         SYS_ipc_nsubcalls, shift_style);
1151                 break;
1152 #endif /* !ALPHA && !SPARC */
1153 #ifdef SPARC
1154         case SYS_socketcall:
1155                 sparc_socket_decode (tcp);
1156                 break;
1157 #endif
1158 #endif /* LINUX */
1159 #ifdef SVR4
1160 #ifdef SYS_pgrpsys_subcall
1161         case SYS_pgrpsys:
1162                 decode_subcall(tcp, SYS_pgrpsys_subcall,
1163                         SYS_pgrpsys_nsubcalls, shift_style);
1164                 break;
1165 #endif /* SYS_pgrpsys_subcall */
1166 #ifdef SYS_sigcall_subcall
1167         case SYS_sigcall:
1168                 decode_subcall(tcp, SYS_sigcall_subcall,
1169                         SYS_sigcall_nsubcalls, mask_style);
1170                 break;
1171 #endif /* SYS_sigcall_subcall */
1172         case SYS_msgsys:
1173                 decode_subcall(tcp, SYS_msgsys_subcall,
1174                         SYS_msgsys_nsubcalls, shift_style);
1175                 break;
1176         case SYS_shmsys:
1177                 decode_subcall(tcp, SYS_shmsys_subcall,
1178                         SYS_shmsys_nsubcalls, shift_style);
1179                 break;
1180         case SYS_semsys:
1181                 decode_subcall(tcp, SYS_semsys_subcall,
1182                         SYS_semsys_nsubcalls, shift_style);
1183                 break;
1184 #if 0 /* broken */
1185         case SYS_utssys:
1186                 decode_subcall(tcp, SYS_utssys_subcall,
1187                         SYS_utssys_nsubcalls, shift_style);
1188                 break;
1189 #endif
1190         case SYS_sysfs:
1191                 decode_subcall(tcp, SYS_sysfs_subcall,
1192                         SYS_sysfs_nsubcalls, shift_style);
1193                 break;
1194         case SYS_spcall:
1195                 decode_subcall(tcp, SYS_spcall_subcall,
1196                         SYS_spcall_nsubcalls, shift_style);
1197                 break;
1198 #ifdef SYS_context_subcall
1199         case SYS_context:
1200                 decode_subcall(tcp, SYS_context_subcall,
1201                         SYS_context_nsubcalls, shift_style);
1202                 break;
1203 #endif /* SYS_context_subcall */
1204 #endif /* SVR4 */
1205 #ifdef SUNOS4
1206         case SYS_semsys:
1207                 decode_subcall(tcp, SYS_semsys_subcall,
1208                         SYS_semsys_nsubcalls, shift_style);
1209                 break;
1210         case SYS_msgsys:
1211                 decode_subcall(tcp, SYS_msgsys_subcall,
1212                         SYS_msgsys_nsubcalls, shift_style);
1213                 break;
1214         case SYS_shmsys:
1215                 decode_subcall(tcp, SYS_shmsys_subcall,
1216                         SYS_shmsys_nsubcalls, shift_style);
1217                 break;
1218 #endif
1219         }
1220
1221         internal_syscall(tcp);
1222         if (!(qual_flags[tcp->scno] & QUAL_TRACE)) {
1223                 tcp->flags |= TCB_INSYSCALL;
1224                 return 0;
1225         }
1226
1227         if (cflag) {
1228                 gettimeofday(&tcp->etime, NULL);
1229                 tcp->flags |= TCB_INSYSCALL;
1230                 return 0;
1231         }
1232
1233         printleader(tcp);
1234         tcp->flags &= ~TCB_REPRINT;
1235         tcp_last = tcp;
1236         if (tcp->scno >= nsyscalls)
1237                 tprintf("syscall_%lu(", tcp->scno);
1238         else
1239                 tprintf("%s(", sysent[tcp->scno].sys_name);
1240         if (tcp->scno >= nsyscalls ||
1241             ((qual_flags[tcp->scno] & QUAL_RAW) && tcp->scno != SYS_exit))
1242                 sys_res = printargs(tcp);
1243         else
1244                 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
1245         if (fflush(tcp->outf) == EOF)
1246                 return -1;
1247         tcp->flags |= TCB_INSYSCALL;
1248         /* Measure the entrance time as late as possible to avoid errors. */
1249         if (dtime)
1250                 gettimeofday(&tcp->etime, NULL);
1251         return sys_res;
1252 }
1253
1254 int
1255 printargs(tcp)
1256 struct tcb *tcp;
1257 {
1258         if (entering(tcp)) {
1259                 int i;
1260
1261                 for (i = 0; i < tcp->u_nargs; i++)
1262                         tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
1263         }
1264         return 0;
1265 }
1266
1267 long
1268 getrval2(tcp)
1269 struct tcb *tcp;
1270 {
1271         long val = -1;
1272
1273 #ifdef LINUX
1274 #ifdef SPARC
1275         struct pt_regs regs;
1276         if (ptrace(PTRACE_GETREGS,tcp->pid,(char *)&regs,0) < 0)
1277                 return -1;
1278         val = regs.u_regs[UREG_I1];
1279 #endif /* SPARC */
1280 #endif /* LINUX */
1281
1282 #ifdef SUNOS4
1283         if (upeek(tcp->pid, uoff(u_rval2), &val) < 0)
1284                 return -1;
1285 #endif /* SUNOS4 */
1286
1287 #ifdef SVR4
1288 #ifdef SPARC
1289         val = tcp->status.pr_reg[R_O1];
1290 #endif /* SPARC */
1291 #ifdef I386
1292         val = tcp->status.pr_reg[EDX];
1293 #endif /* I386 */
1294 #ifdef MIPS
1295         val = tcp->status.pr_reg[CTX_V1];
1296 #endif /* MIPS */
1297 #endif /* SVR4 */
1298
1299         return val;
1300 }
1301
1302 /*
1303  * Apparently, indirect system calls have already be converted by ptrace(2),
1304  * so if you see "indir" this program has gone astray.
1305  */
1306 int
1307 sys_indir(tcp)
1308 struct tcb *tcp;
1309 {
1310         int i, scno, nargs;
1311
1312         if (entering(tcp)) {
1313                 if ((scno = tcp->u_arg[0]) > nsyscalls) {
1314                         fprintf(stderr, "Bogus syscall: %u\n", scno);
1315                         return 0;
1316                 }
1317                 nargs = sysent[scno].nargs;
1318                 tprintf("%s", sysent[scno].sys_name);
1319                 for (i = 0; i < nargs; i++)
1320                         tprintf(", %#lx", tcp->u_arg[i+1]);
1321         }
1322         return 0;
1323 }
1324
1325 static int
1326 time_cmp(a, b)
1327 void *a;
1328 void *b;
1329 {
1330         return -tv_cmp(&tv_count[*((int *) a)], &tv_count[*((int *) b)]);
1331 }
1332
1333 static int
1334 syscall_cmp(a, b)
1335 void *a;
1336 void *b;
1337 {
1338         return strcmp(sysent[*((int *) a)].sys_name,
1339                 sysent[*((int *) b)].sys_name);
1340 }
1341
1342 static int
1343 count_cmp(a, b)
1344 void *a;
1345 void *b;
1346 {
1347         int m = call_count[*((int *) a)], n = call_count[*((int *) b)];
1348
1349         return (m < n) ? 1 : (m > n) ? -1 : 0;
1350 }
1351
1352 static int (*sortfun)();
1353 static struct timeval overhead = { -1, -1 };
1354
1355 void
1356 set_sortby(sortby)
1357 char *sortby;
1358 {
1359         if (strcmp(sortby, "time") == 0)
1360                 sortfun = time_cmp;
1361         else if (strcmp(sortby, "calls") == 0)
1362                 sortfun = count_cmp;
1363         else if (strcmp(sortby, "name") == 0)
1364                 sortfun = syscall_cmp;
1365         else if (strcmp(sortby, "nothing") == 0)
1366                 sortfun = NULL;
1367         else {
1368                 fprintf(stderr, "invalid sortby: `%s'\n", sortby);
1369                 exit(1);
1370         }
1371 }
1372
1373 void set_overhead(n)
1374 int n;
1375 {
1376         overhead.tv_sec = n / 1000000;
1377         overhead.tv_usec = n % 1000000;
1378 }
1379
1380 void
1381 call_summary(outf)
1382 FILE *outf;
1383 {
1384         int i, j;
1385         int call_cum, error_cum;
1386         struct timeval tv_cum, dtv;
1387         double percent;
1388         char *dashes = "-------------------------";
1389         char error_str[16];
1390
1391         call_cum = error_cum = tv_cum.tv_sec = tv_cum.tv_usec = 0;
1392         if (overhead.tv_sec == -1) {
1393                 tv_mul(&overhead, &shortest, 8);
1394                 tv_div(&overhead, &overhead, 10);
1395         }
1396         for (i = 0; i < nsyscalls; i++) {
1397                 sorted_count[i] = i;
1398                 if (call_count[i] == 0)
1399                         continue;
1400                 tv_mul(&dtv, &overhead, call_count[i]);
1401                 tv_sub(&tv_count[i], &tv_count[i], &dtv);
1402                 call_cum += call_count[i];
1403                 error_cum += error_count[i];
1404                 tv_add(&tv_cum, &tv_cum, &tv_count[i]);
1405         }
1406         if (sortfun)
1407                 qsort((void *) sorted_count, nsyscalls, sizeof(int), sortfun);
1408         fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %s\n",
1409                 "% time", "seconds", "usecs/call",
1410                 "calls", "errors", "syscall");
1411         fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1412                 dashes, dashes, dashes, dashes, dashes, dashes);
1413         for (i = 0; i < nsyscalls; i++) {
1414                 j = sorted_count[i];
1415                 if (call_count[j] == 0)
1416                         continue;
1417                 tv_div(&dtv, &tv_count[j], call_count[j]);
1418                 if (error_count[j])
1419                         sprintf(error_str, "%d", error_count[j]);
1420                 else
1421                         error_str[0] = '\0';
1422                 percent = 100.0*tv_float(&tv_count[j])/tv_float(&tv_cum);
1423                 fprintf(outf, "%6.2f %4ld.%06ld %11ld %9d %9.9s %s\n",
1424                         percent, (long) tv_count[j].tv_sec,
1425                         (long) tv_count[j].tv_usec,
1426                         (long) 1000000 * dtv.tv_sec + dtv.tv_usec,
1427                         call_count[j], error_str, sysent[j].sys_name);
1428         }
1429         fprintf(outf, "%6.6s %11.11s %11.11s %9.9s %9.9s %-16.16s\n",
1430                 dashes, dashes, dashes, dashes, dashes, dashes);
1431         if (error_cum)
1432                 sprintf(error_str, "%d", error_cum);
1433         else
1434                 error_str[0] = '\0';
1435         fprintf(outf, "%6.6s %4ld.%06ld %11.11s %9d %9.9s %s\n",
1436                 "100.00", (long) tv_cum.tv_sec, (long) tv_cum.tv_usec, "",
1437                 call_cum, error_str, "total");
1438 }