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