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