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