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