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