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