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