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