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