]> granicus.if.org Git - strace/blob - syscall.c
More robust error check for vasprintf
[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         if (!syserror(tcp))
720                 return 0;
721         switch (tcp->u_error) {
722                 case ERESTARTSYS:
723                 case ERESTARTNOINTR:
724                 case ERESTARTNOHAND:
725                 case ERESTART_RESTARTBLOCK:
726                         return 1;
727                 default:
728                         break;
729         }
730 #endif /* LINUX */
731         return 0;
732 }
733
734 struct tcb *tcp_last = NULL;
735
736 #ifdef LINUX
737 # if defined(I386)
738 struct pt_regs i386_regs;
739 # elif defined(X86_64)
740 /*
741  * On 32 bits, pt_regs and user_regs_struct are the same,
742  * but on 64 bits, user_regs_struct has six more fields:
743  * fs_base, gs_base, ds, es, fs, gs.
744  * PTRACE_GETREGS fills them too, so struct pt_regs would overflow.
745  */
746 static struct user_regs_struct x86_64_regs;
747 # elif defined (IA64)
748 long r8, r10, psr; /* TODO: make static? */
749 long ia32 = 0; /* not static */
750 # elif defined (POWERPC)
751 static long result;
752 # elif defined (M68K)
753 static long d0;
754 # elif defined(BFIN)
755 static long r0;
756 # elif defined (ARM)
757 static struct pt_regs regs;
758 # elif defined (ALPHA)
759 static long r0;
760 static long a3;
761 # elif defined(AVR32)
762 static struct pt_regs regs;
763 # elif defined (SPARC) || defined (SPARC64)
764 static struct pt_regs regs;
765 static unsigned long trap;
766 # elif defined(LINUX_MIPSN32)
767 static long long a3;
768 static long long r2;
769 # elif defined(MIPS)
770 static long a3;
771 static long r2;
772 # elif defined(S390) || defined(S390X)
773 static long gpr2;
774 static long pc;
775 static long syscall_mode;
776 # elif defined(HPPA)
777 static long r28;
778 # elif defined(SH)
779 static long r0;
780 # elif defined(SH64)
781 static long r9;
782 # elif defined(CRISV10) || defined(CRISV32)
783 static long r10;
784 # elif defined(MICROBLAZE)
785 static long r3;
786 # endif
787 #endif /* LINUX */
788 #ifdef FREEBSD
789 struct reg regs; /* TODO: make static? */
790 #endif /* FREEBSD */
791
792 /* Returns:
793  * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
794  * 1: ok, continue in trace_syscall().
795  * other: error, trace_syscall() should print error indicator
796  *    ("????" etc) and bail out.
797  */
798 #ifndef USE_PROCFS
799 static
800 #endif
801 int
802 get_scno(struct tcb *tcp)
803 {
804         long scno = 0;
805
806 #ifdef LINUX
807 # if defined(S390) || defined(S390X)
808         if (upeek(tcp, PT_GPR2, &syscall_mode) < 0)
809                 return -1;
810
811         if (syscall_mode != -ENOSYS) {
812                 /*
813                  * Since kernel version 2.5.44 the scno gets passed in gpr2.
814                  */
815                 scno = syscall_mode;
816         } else {
817                 /*
818                  * Old style of "passing" the scno via the SVC instruction.
819                  */
820                 long opcode, offset_reg, tmp;
821                 void *svc_addr;
822                 static const int gpr_offset[16] = {
823                                 PT_GPR0,  PT_GPR1,  PT_ORIGGPR2, PT_GPR3,
824                                 PT_GPR4,  PT_GPR5,  PT_GPR6,     PT_GPR7,
825                                 PT_GPR8,  PT_GPR9,  PT_GPR10,    PT_GPR11,
826                                 PT_GPR12, PT_GPR13, PT_GPR14,    PT_GPR15
827                 };
828
829                 if (upeek(tcp, PT_PSWADDR, &pc) < 0)
830                         return -1;
831                 errno = 0;
832                 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(pc-sizeof(long)), 0);
833                 if (errno) {
834                         perror("peektext(pc-oneword)");
835                         return -1;
836                 }
837
838                 /*
839                  *  We have to check if the SVC got executed directly or via an
840                  *  EXECUTE instruction. In case of EXECUTE it is necessary to do
841                  *  instruction decoding to derive the system call number.
842                  *  Unfortunately the opcode sizes of EXECUTE and SVC are differently,
843                  *  so that this doesn't work if a SVC opcode is part of an EXECUTE
844                  *  opcode. Since there is no way to find out the opcode size this
845                  *  is the best we can do...
846                  */
847                 if ((opcode & 0xff00) == 0x0a00) {
848                         /* SVC opcode */
849                         scno = opcode & 0xff;
850                 }
851                 else {
852                         /* SVC got executed by EXECUTE instruction */
853
854                         /*
855                          *  Do instruction decoding of EXECUTE. If you really want to
856                          *  understand this, read the Principles of Operations.
857                          */
858                         svc_addr = (void *) (opcode & 0xfff);
859
860                         tmp = 0;
861                         offset_reg = (opcode & 0x000f0000) >> 16;
862                         if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
863                                 return -1;
864                         svc_addr += tmp;
865
866                         tmp = 0;
867                         offset_reg = (opcode & 0x0000f000) >> 12;
868                         if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
869                                 return -1;
870                         svc_addr += tmp;
871
872                         scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
873                         if (errno)
874                                 return -1;
875 #  if defined(S390X)
876                         scno >>= 48;
877 #  else
878                         scno >>= 16;
879 #  endif
880                         tmp = 0;
881                         offset_reg = (opcode & 0x00f00000) >> 20;
882                         if (offset_reg && (upeek(tcp, gpr_offset[offset_reg], &tmp) < 0))
883                                 return -1;
884
885                         scno = (scno | tmp) & 0xff;
886                 }
887         }
888 # elif defined (POWERPC)
889         if (upeek(tcp, sizeof(unsigned long)*PT_R0, &scno) < 0)
890                 return -1;
891 #  ifdef POWERPC64
892         /* TODO: speed up strace by not doing this at every syscall.
893          * We only need to do it after execve.
894          */
895         int currpers;
896         long val;
897         int pid = tcp->pid;
898
899         /* Check for 64/32 bit mode. */
900         if (upeek(tcp, sizeof(unsigned long)*PT_MSR, &val) < 0)
901                 return -1;
902         /* SF is bit 0 of MSR */
903         if (val < 0)
904                 currpers = 0;
905         else
906                 currpers = 1;
907         update_personality(tcp, currpers);
908 #  endif
909 # elif defined(AVR32)
910         /* Read complete register set in one go. */
911         if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, &regs) < 0)
912                 return -1;
913         scno = regs.r8;
914 # elif defined(BFIN)
915         if (upeek(tcp, PT_ORIG_P0, &scno))
916                 return -1;
917 # elif defined (I386)
918         if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &i386_regs) < 0)
919                 return -1;
920         scno = i386_regs.orig_eax;
921 # elif defined (X86_64)
922         int currpers;
923         if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &x86_64_regs) < 0)
924                 return -1;
925         scno = x86_64_regs.orig_rax;
926
927         /* Check CS register value. On x86-64 linux it is:
928          *      0x33    for long mode (64 bit)
929          *      0x23    for compatibility mode (32 bit)
930          */
931         switch (x86_64_regs.cs) {
932                 case 0x23: currpers = 1; break;
933                 case 0x33: currpers = 0; break;
934                 default:
935                         fprintf(stderr, "Unknown value CS=0x%08X while "
936                                  "detecting personality of process "
937                                  "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
938                         currpers = current_personality;
939                         break;
940         }
941 #  if 0
942         /* This version analyzes the opcode of a syscall instruction.
943          * (int 0x80 on i386 vs. syscall on x86-64)
944          * It works, but is too complicated.
945          */
946         unsigned long val, rip, i;
947
948         rip = x86_64_regs.rip;
949
950         /* sizeof(syscall) == sizeof(int 0x80) == 2 */
951         rip -= 2;
952         errno = 0;
953
954         call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
955         if (errno)
956                 fprintf(stderr, "ptrace_peektext failed: %s\n",
957                                 strerror(errno));
958         switch (call & 0xffff) {
959                 /* x86-64: syscall = 0x0f 0x05 */
960                 case 0x050f: currpers = 0; break;
961                 /* i386: int 0x80 = 0xcd 0x80 */
962                 case 0x80cd: currpers = 1; break;
963                 default:
964                         currpers = current_personality;
965                         fprintf(stderr,
966                                 "Unknown syscall opcode (0x%04X) while "
967                                 "detecting personality of process "
968                                 "PID=%d\n", (int)call, tcp->pid);
969                         break;
970         }
971 #  endif
972         update_personality(tcp, currpers);
973 # elif defined(IA64)
974 #       define IA64_PSR_IS      ((long)1 << 34)
975         if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
976                 ia32 = (psr & IA64_PSR_IS) != 0;
977         if (ia32) {
978                 if (upeek(tcp, PT_R1, &scno) < 0)
979                         return -1;
980         } else {
981                 if (upeek(tcp, PT_R15, &scno) < 0)
982                         return -1;
983         }
984 # elif defined (ARM)
985         /* Read complete register set in one go. */
986         if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (void *)&regs) == -1)
987                 return -1;
988
989         /*
990          * We only need to grab the syscall number on syscall entry.
991          */
992         if (regs.ARM_ip == 0) {
993                 /*
994                  * Note: we only deal with only 32-bit CPUs here.
995                  */
996                 if (regs.ARM_cpsr & 0x20) {
997                         /*
998                          * Get the Thumb-mode system call number
999                          */
1000                         scno = regs.ARM_r7;
1001                 } else {
1002                         /*
1003                          * Get the ARM-mode system call number
1004                          */
1005                         errno = 0;
1006                         scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(regs.ARM_pc - 4), NULL);
1007                         if (errno)
1008                                 return -1;
1009
1010                         /* Handle the EABI syscall convention.  We do not
1011                            bother converting structures between the two
1012                            ABIs, but basic functionality should work even
1013                            if strace and the traced program have different
1014                            ABIs.  */
1015                         if (scno == 0xef000000) {
1016                                 scno = regs.ARM_r7;
1017                         } else {
1018                                 if ((scno & 0x0ff00000) != 0x0f900000) {
1019                                         fprintf(stderr, "syscall: unknown syscall trap 0x%08lx\n",
1020                                                 scno);
1021                                         return -1;
1022                                 }
1023
1024                                 /*
1025                                  * Fixup the syscall number
1026                                  */
1027                                 scno &= 0x000fffff;
1028                         }
1029                 }
1030                 if (scno & 0x0f0000) {
1031                         /*
1032                          * Handle ARM specific syscall
1033                          */
1034                         update_personality(tcp, 1);
1035                         scno &= 0x0000ffff;
1036                 } else
1037                         update_personality(tcp, 0);
1038
1039         } else {
1040                 fprintf(stderr, "pid %d stray syscall entry\n", tcp->pid);
1041                 tcp->flags |= TCB_INSYSCALL;
1042         }
1043 # elif defined (M68K)
1044         if (upeek(tcp, 4*PT_ORIG_D0, &scno) < 0)
1045                 return -1;
1046 # elif defined (LINUX_MIPSN32)
1047         unsigned long long regs[38];
1048
1049         if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1050                 return -1;
1051         a3 = regs[REG_A3];
1052         r2 = regs[REG_V0];
1053
1054         scno = r2;
1055         if (!SCNO_IN_RANGE(scno)) {
1056                 if (a3 == 0 || a3 == -1) {
1057                         if (debug)
1058                                 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
1059                         return 0;
1060                 }
1061         }
1062 # elif defined (MIPS)
1063         if (upeek(tcp, REG_A3, &a3) < 0)
1064                 return -1;
1065         if (upeek(tcp, REG_V0, &scno) < 0)
1066                 return -1;
1067
1068         if (!SCNO_IN_RANGE(scno)) {
1069                 if (a3 == 0 || a3 == -1) {
1070                         if (debug)
1071                                 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
1072                         return 0;
1073                 }
1074         }
1075 # elif defined (ALPHA)
1076         if (upeek(tcp, REG_A3, &a3) < 0)
1077                 return -1;
1078         if (upeek(tcp, REG_R0, &scno) < 0)
1079                 return -1;
1080
1081         /*
1082          * Do some sanity checks to figure out if it's
1083          * really a syscall entry
1084          */
1085         if (!SCNO_IN_RANGE(scno)) {
1086                 if (a3 == 0 || a3 == -1) {
1087                         if (debug)
1088                                 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
1089                         return 0;
1090                 }
1091         }
1092 # elif defined (SPARC) || defined (SPARC64)
1093         /* Everything we need is in the current register set. */
1094         if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)&regs, 0) < 0)
1095                 return -1;
1096
1097         /* Disassemble the syscall trap. */
1098         /* Retrieve the syscall trap instruction. */
1099         errno = 0;
1100 #  if defined(SPARC64)
1101         trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)regs.tpc, 0);
1102         trap >>= 32;
1103 #  else
1104         trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)regs.pc, 0);
1105 #  endif
1106         if (errno)
1107                 return -1;
1108
1109         /* Disassemble the trap to see what personality to use. */
1110         switch (trap) {
1111         case 0x91d02010:
1112                 /* Linux/SPARC syscall trap. */
1113                 update_personality(tcp, 0);
1114                 break;
1115         case 0x91d0206d:
1116                 /* Linux/SPARC64 syscall trap. */
1117                 update_personality(tcp, 2);
1118                 break;
1119         case 0x91d02000:
1120                 /* SunOS syscall trap. (pers 1) */
1121                 fprintf(stderr, "syscall: SunOS no support\n");
1122                 return -1;
1123         case 0x91d02008:
1124                 /* Solaris 2.x syscall trap. (per 2) */
1125                 update_personality(tcp, 1);
1126                 break;
1127         case 0x91d02009:
1128                 /* NetBSD/FreeBSD syscall trap. */
1129                 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1130                 return -1;
1131         case 0x91d02027:
1132                 /* Solaris 2.x gettimeofday */
1133                 update_personality(tcp, 1);
1134                 break;
1135         default:
1136 #  if defined (SPARC64)
1137                 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, regs.tpc);
1138 #  else
1139                 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, regs.pc);
1140 #  endif
1141                 return -1;
1142         }
1143
1144         /* Extract the system call number from the registers. */
1145         if (trap == 0x91d02027)
1146                 scno = 156;
1147         else
1148                 scno = regs.u_regs[U_REG_G1];
1149         if (scno == 0) {
1150                 scno = regs.u_regs[U_REG_O0];
1151                 memmove(&regs.u_regs[U_REG_O0], &regs.u_regs[U_REG_O1], 7*sizeof(regs.u_regs[0]));
1152         }
1153 # elif defined(HPPA)
1154         if (upeek(tcp, PT_GR20, &scno) < 0)
1155                 return -1;
1156 # elif defined(SH)
1157         /*
1158          * In the new syscall ABI, the system call number is in R3.
1159          */
1160         if (upeek(tcp, 4*(REG_REG0+3), &scno) < 0)
1161                 return -1;
1162
1163         if (scno < 0) {
1164                 /* Odd as it may seem, a glibc bug has been known to cause
1165                    glibc to issue bogus negative syscall numbers.  So for
1166                    our purposes, make strace print what it *should* have been */
1167                 long correct_scno = (scno & 0xff);
1168                 if (debug)
1169                         fprintf(stderr,
1170                                 "Detected glibc bug: bogus system call"
1171                                 " number = %ld, correcting to %ld\n",
1172                                 scno,
1173                                 correct_scno);
1174                 scno = correct_scno;
1175         }
1176 # elif defined(SH64)
1177         if (upeek(tcp, REG_SYSCALL, &scno) < 0)
1178                 return -1;
1179         scno &= 0xFFFF;
1180 # elif defined(CRISV10) || defined(CRISV32)
1181         if (upeek(tcp, 4*PT_R9, &scno) < 0)
1182                 return -1;
1183 # elif defined(TILE)
1184         if (upeek(tcp, PTREGS_OFFSET_REG(10), &scno) < 0)
1185                 return -1;
1186 # elif defined(MICROBLAZE)
1187         if (upeek(tcp, 0, &scno) < 0)
1188                 return -1;
1189 # endif
1190 #endif /* LINUX */
1191
1192 #ifdef SUNOS4
1193         if (upeek(tcp, uoff(u_arg[7]), &scno) < 0)
1194                 return -1;
1195 #elif defined(SH)
1196         /* new syscall ABI returns result in R0 */
1197         if (upeek(tcp, 4*REG_REG0, (long *)&r0) < 0)
1198                 return -1;
1199 #elif defined(SH64)
1200         /* ABI defines result returned in r9 */
1201         if (upeek(tcp, REG_GENERAL(9), (long *)&r9) < 0)
1202                 return -1;
1203 #endif
1204
1205 #ifdef USE_PROCFS
1206 # ifdef HAVE_PR_SYSCALL
1207         scno = tcp->status.PR_SYSCALL;
1208 # else
1209 #  ifndef FREEBSD
1210         scno = tcp->status.PR_WHAT;
1211 #  else
1212         if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1213                 perror("pread");
1214                 return -1;
1215         }
1216         switch (regs.r_eax) {
1217         case SYS_syscall:
1218         case SYS___syscall:
1219                 pread(tcp->pfd, &scno, sizeof(scno), regs.r_esp + sizeof(int));
1220                 break;
1221         default:
1222                 scno = regs.r_eax;
1223                 break;
1224         }
1225 #  endif /* FREEBSD */
1226 # endif /* !HAVE_PR_SYSCALL */
1227 #endif /* USE_PROCFS */
1228
1229         tcp->scno = scno;
1230         return 1;
1231 }
1232
1233 long
1234 known_scno(struct tcb *tcp)
1235 {
1236         long scno = tcp->scno;
1237 #if SUPPORTED_PERSONALITIES > 1
1238         if (SCNO_IN_RANGE(scno) && sysent[scno].native_scno != 0)
1239                 scno = sysent[scno].native_scno;
1240         else
1241 #endif
1242                 scno += NR_SYSCALL_BASE;
1243         return scno;
1244 }
1245
1246 /* Called at each syscall entry.
1247  * Returns:
1248  * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
1249  * 1: ok, continue in trace_syscall().
1250  * other: error, trace_syscall() should print error indicator
1251  *    ("????" etc) and bail out.
1252  */
1253 static int
1254 syscall_fixup_on_sysenter(struct tcb *tcp)
1255 {
1256 #ifdef USE_PROCFS
1257         int scno = known_scno(tcp);
1258
1259         if (tcp->status.PR_WHY != PR_SYSENTRY) {
1260                 if (
1261                     scno == SYS_fork
1262 #ifdef SYS_vfork
1263                     || scno == SYS_vfork
1264 #endif
1265 #ifdef SYS_fork1
1266                     || scno == SYS_fork1
1267 #endif
1268 #ifdef SYS_forkall
1269                     || scno == SYS_forkall
1270 #endif
1271 #ifdef SYS_rfork1
1272                     || scno == SYS_rfork1
1273 #endif
1274 #ifdef SYS_rforkall
1275                     || scno == SYS_rforkall
1276 #endif
1277                     ) {
1278                         /* We are returning in the child, fake it. */
1279                         tcp->status.PR_WHY = PR_SYSENTRY;
1280                         trace_syscall(tcp);
1281                         tcp->status.PR_WHY = PR_SYSEXIT;
1282                 }
1283                 else {
1284                         fprintf(stderr, "syscall: missing entry\n");
1285                         tcp->flags |= TCB_INSYSCALL;
1286                 }
1287         }
1288 #endif /* USE_PROCFS */
1289
1290 #ifdef SUNOS4
1291         if (scno == 0) {
1292                 fprintf(stderr, "syscall: missing entry\n");
1293                 tcp->flags |= TCB_INSYSCALL;
1294         }
1295 #endif
1296
1297 #ifdef LINUX
1298         /* A common case of "not a syscall entry" is post-execve SIGTRAP */
1299 #if defined (I386)
1300         if (i386_regs.eax != -ENOSYS) {
1301                 if (debug)
1302                         fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1303                 return 0;
1304         }
1305 #elif defined (X86_64)
1306         {
1307                 long rax = x86_64_regs.rax;
1308                 if (current_personality == 1)
1309                         rax = (int)rax; /* sign extend from 32 bits */
1310                 if (rax != -ENOSYS) {
1311                         if (debug)
1312                                 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1313                         return 0;
1314                 }
1315         }
1316 #elif defined (S390) || defined (S390X)
1317         /* TODO: we already fetched PT_GPR2 in get_scno
1318          * and stored it in syscall_mode, reuse it here
1319          * instead of re-fetching?
1320          */
1321         if (upeek(tcp, PT_GPR2, &gpr2) < 0)
1322                 return -1;
1323         if (syscall_mode != -ENOSYS)
1324                 syscall_mode = tcp->scno;
1325         if (gpr2 != syscall_mode) {
1326                 if (debug)
1327                         fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2);
1328                 return 0;
1329         }
1330 #elif defined (M68K)
1331         /* TODO? Eliminate upeek's in arches below like we did in x86 */
1332         if (upeek(tcp, 4*PT_D0, &d0) < 0)
1333                 return -1;
1334         if (d0 != -ENOSYS) {
1335                 if (debug)
1336                         fprintf(stderr, "not a syscall entry (d0 = %ld)\n", d0);
1337                 return 0;
1338         }
1339 #elif defined(IA64)
1340         if (upeek(tcp, PT_R10, &r10) < 0)
1341                 return -1;
1342         if (upeek(tcp, PT_R8, &r8) < 0)
1343                 return -1;
1344         if (ia32 && r8 != -ENOSYS) {
1345                 if (debug)
1346                         fprintf(stderr, "not a syscall entry (r8 = %ld)\n", r8);
1347                 return 0;
1348         }
1349 #elif defined(CRISV10) || defined(CRISV32)
1350         if (upeek(tcp, 4*PT_R10, &r10) < 0)
1351                 return -1;
1352         if (r10 != -ENOSYS) {
1353                 if (debug)
1354                         fprintf(stderr, "not a syscall entry (r10 = %ld)\n", r10);
1355                 return 0;
1356         }
1357 #elif defined(MICROBLAZE)
1358         if (upeek(tcp, 3 * 4, &r3) < 0)
1359                 return -1;
1360         if (r3 != -ENOSYS) {
1361                 if (debug)
1362                         fprintf(stderr, "not a syscall entry (r3 = %ld)\n", r3);
1363                 return 0;
1364         }
1365 #endif
1366 #endif /* LINUX */
1367         return 1;
1368 }
1369
1370 static int
1371 internal_syscall(struct tcb *tcp)
1372 {
1373         /*
1374          * We must always trace a few critical system calls in order to
1375          * correctly support following forks in the presence of tracing
1376          * qualifiers.
1377          */
1378         int (*func)();
1379
1380         if (!SCNO_IN_RANGE(tcp->scno))
1381                 return 0;
1382
1383         func = sysent[tcp->scno].sys_func;
1384
1385         if (   sys_fork == func
1386 #if defined(FREEBSD) || defined(LINUX) || defined(SUNOS4)
1387             || sys_vfork == func
1388 #endif
1389 #ifdef LINUX
1390             || sys_clone == func
1391 #endif
1392 #if UNIXWARE > 2
1393             || sys_rfork == func
1394 #endif
1395            )
1396                 return internal_fork(tcp);
1397
1398 #if defined SUNOS4 || (defined LINUX && defined TCB_WAITEXECVE)
1399         if (   sys_execve == func
1400 # if defined(SPARC) || defined(SPARC64) || defined(SUNOS4)
1401             || sys_execv == func
1402 # endif
1403 # if UNIXWARE > 2
1404             || sys_rexecve == func
1405 # endif
1406            )
1407                 return internal_exec(tcp);
1408 #endif
1409
1410         return 0;
1411 }
1412
1413 static int
1414 syscall_enter(struct tcb *tcp)
1415 {
1416 #ifdef LINUX
1417         int i, nargs;
1418
1419         if (SCNO_IN_RANGE(tcp->scno))
1420                 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1421         else
1422                 nargs = tcp->u_nargs = MAX_ARGS;
1423
1424 # if defined(S390) || defined(S390X)
1425         for (i = 0; i < nargs; ++i)
1426                 if (upeek(tcp, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
1427                         return -1;
1428 # elif defined(ALPHA)
1429         for (i = 0; i < nargs; ++i)
1430                 if (upeek(tcp, REG_A0+i, &tcp->u_arg[i]) < 0)
1431                         return -1;
1432 # elif defined(IA64)
1433         if (!ia32) {
1434                 unsigned long *out0, cfm, sof, sol;
1435                 long rbs_end;
1436                 /* be backwards compatible with kernel < 2.4.4... */
1437 #               ifndef PT_RBS_END
1438 #                 define PT_RBS_END     PT_AR_BSP
1439 #               endif
1440
1441                 if (upeek(tcp, PT_RBS_END, &rbs_end) < 0)
1442                         return -1;
1443                 if (upeek(tcp, PT_CFM, (long *) &cfm) < 0)
1444                         return -1;
1445
1446                 sof = (cfm >> 0) & 0x7f;
1447                 sol = (cfm >> 7) & 0x7f;
1448                 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1449
1450                 for (i = 0; i < nargs; ++i) {
1451                         if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1452                                    sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1453                                 return -1;
1454                 }
1455         } else {
1456                 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1457                                                       PT_R9  /* ECX = out1 */,
1458                                                       PT_R10 /* EDX = out2 */,
1459                                                       PT_R14 /* ESI = out3 */,
1460                                                       PT_R15 /* EDI = out4 */,
1461                                                       PT_R13 /* EBP = out5 */};
1462
1463                 for (i = 0; i < nargs; ++i) {
1464                         if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1465                                 return -1;
1466                         /* truncate away IVE sign-extension */
1467                         tcp->u_arg[i] &= 0xffffffff;
1468                 }
1469         }
1470 # elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
1471         /* N32 and N64 both use up to six registers.  */
1472         unsigned long long regs[38];
1473
1474         if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1475                 return -1;
1476
1477         for (i = 0; i < nargs; ++i) {
1478                 tcp->u_arg[i] = regs[REG_A0 + i];
1479 #  if defined(LINUX_MIPSN32)
1480                 tcp->ext_arg[i] = regs[REG_A0 + i];
1481 #  endif
1482         }
1483 # elif defined(MIPS)
1484         if (nargs > 4) {
1485                 long sp;
1486
1487                 if (upeek(tcp, REG_SP, &sp) < 0)
1488                         return -1;
1489                 for (i = 0; i < 4; ++i)
1490                         if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1491                                 return -1;
1492                 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
1493                        (char *)(tcp->u_arg + 4));
1494         } else {
1495                 for (i = 0; i < nargs; ++i)
1496                         if (upeek(tcp, REG_A0 + i, &tcp->u_arg[i]) < 0)
1497                                 return -1;
1498         }
1499 # elif defined(POWERPC)
1500 #  ifndef PT_ORIG_R3
1501 #   define PT_ORIG_R3 34
1502 #  endif
1503         for (i = 0; i < nargs; ++i) {
1504                 if (upeek(tcp, (i==0) ?
1505                         (sizeof(unsigned long) * PT_ORIG_R3) :
1506                         ((i+PT_R3) * sizeof(unsigned long)),
1507                                 &tcp->u_arg[i]) < 0)
1508                         return -1;
1509         }
1510 # elif defined(SPARC) || defined(SPARC64)
1511         for (i = 0; i < nargs; ++i)
1512                 tcp->u_arg[i] = regs.u_regs[U_REG_O0 + i];
1513 # elif defined(HPPA)
1514         for (i = 0; i < nargs; ++i)
1515                 if (upeek(tcp, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1516                         return -1;
1517 # elif defined(ARM)
1518         for (i = 0; i < nargs; ++i)
1519                 tcp->u_arg[i] = regs.uregs[i];
1520 # elif defined(AVR32)
1521         (void)i;
1522         (void)nargs;
1523         tcp->u_arg[0] = regs.r12;
1524         tcp->u_arg[1] = regs.r11;
1525         tcp->u_arg[2] = regs.r10;
1526         tcp->u_arg[3] = regs.r9;
1527         tcp->u_arg[4] = regs.r5;
1528         tcp->u_arg[5] = regs.r3;
1529 # elif defined(BFIN)
1530         static const int argreg[MAX_ARGS] = { PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5 };
1531
1532         for (i = 0; i < nargs; ++i)
1533                 if (upeek(tcp, argreg[i], &tcp->u_arg[i]) < 0)
1534                         return -1;
1535 # elif defined(SH)
1536         static const int syscall_regs[MAX_ARGS] = {
1537                 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1538                 4 * (REG_REG0+7), 4 * (REG_REG0  ), 4 * (REG_REG0+1)
1539         };
1540
1541         for (i = 0; i < nargs; ++i)
1542                 if (upeek(tcp, syscall_regs[i], &tcp->u_arg[i]) < 0)
1543                         return -1;
1544 # elif defined(SH64)
1545         int i;
1546         /* Registers used by SH5 Linux system calls for parameters */
1547         static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
1548
1549         for (i = 0; i < nargs; ++i)
1550                 if (upeek(tcp, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1551                         return -1;
1552 # elif defined(X86_64)
1553         (void)i;
1554         (void)nargs;
1555         if (current_personality == 0) { /* x86-64 ABI */
1556                 tcp->u_arg[0] = x86_64_regs.rdi;
1557                 tcp->u_arg[1] = x86_64_regs.rsi;
1558                 tcp->u_arg[2] = x86_64_regs.rdx;
1559                 tcp->u_arg[3] = x86_64_regs.r10;
1560                 tcp->u_arg[4] = x86_64_regs.r8;
1561                 tcp->u_arg[5] = x86_64_regs.r9;
1562         } else { /* i386 ABI */
1563                 /* Sign-extend lower 32 bits */
1564                 tcp->u_arg[0] = (long)(int)x86_64_regs.rbx;
1565                 tcp->u_arg[1] = (long)(int)x86_64_regs.rcx;
1566                 tcp->u_arg[2] = (long)(int)x86_64_regs.rdx;
1567                 tcp->u_arg[3] = (long)(int)x86_64_regs.rsi;
1568                 tcp->u_arg[4] = (long)(int)x86_64_regs.rdi;
1569                 tcp->u_arg[5] = (long)(int)x86_64_regs.rbp;
1570         }
1571 # elif defined(MICROBLAZE)
1572         for (i = 0; i < nargs; ++i)
1573                 if (upeek(tcp, (5 + i) * 4, &tcp->u_arg[i]) < 0)
1574                         return -1;
1575 # elif defined(CRISV10) || defined(CRISV32)
1576         static const int crisregs[MAX_ARGS] = {
1577                 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
1578                 4*PT_R13     , 4*PT_MOF, 4*PT_SRP
1579         };
1580
1581         for (i = 0; i < nargs; ++i)
1582                 if (upeek(tcp, crisregs[i], &tcp->u_arg[i]) < 0)
1583                         return -1;
1584 # elif defined(TILE)
1585         for (i = 0; i < nargs; ++i)
1586                 if (upeek(tcp, PTREGS_OFFSET_REG(i), &tcp->u_arg[i]) < 0)
1587                         return -1;
1588 # elif defined(M68K)
1589         for (i = 0; i < nargs; ++i)
1590                 if (upeek(tcp, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
1591                         return -1;
1592 # elif defined(I386)
1593         (void)i;
1594         (void)nargs;
1595         tcp->u_arg[0] = i386_regs.ebx;
1596         tcp->u_arg[1] = i386_regs.ecx;
1597         tcp->u_arg[2] = i386_regs.edx;
1598         tcp->u_arg[3] = i386_regs.esi;
1599         tcp->u_arg[4] = i386_regs.edi;
1600         tcp->u_arg[5] = i386_regs.ebp;
1601 # else /* Other architecture (32bits specific) */
1602         for (i = 0; i < nargs; ++i)
1603                 if (upeek(tcp, i*4, &tcp->u_arg[i]) < 0)
1604                         return -1;
1605 # endif
1606 #endif /* LINUX */
1607 #ifdef SUNOS4
1608         int i, nargs;
1609         if (SCNO_IN_RANGE(tcp->scno))
1610                 nargs = tcp->u_nargs = sysent[tcp->scno].nargs;
1611         else
1612                 nargs = tcp->u_nargs = MAX_ARGS;
1613         for (i = 0; i < nargs; i++) {
1614                 struct user *u;
1615
1616                 if (upeek(tcp, uoff(u_arg[0]) +
1617                     (i * sizeof(u->u_arg[0])), &tcp->u_arg[i]) < 0)
1618                         return -1;
1619         }
1620 #endif /* SUNOS4 */
1621 #ifdef SVR4
1622 # ifdef MIPS
1623         /*
1624          * SGI is broken: even though it has pr_sysarg, it doesn't
1625          * set them on system call entry.  Get a clue.
1626          */
1627         if (SCNO_IN_RANGE(tcp->scno))
1628                 tcp->u_nargs = sysent[tcp->scno].nargs;
1629         else
1630                 tcp->u_nargs = tcp->status.pr_nsysarg;
1631         if (tcp->u_nargs > 4) {
1632                 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1633                         4 * sizeof(tcp->u_arg[0]));
1634                 umoven(tcp, tcp->status.pr_reg[CTX_SP] + 16,
1635                         (tcp->u_nargs - 4) * sizeof(tcp->u_arg[0]), (char *) (tcp->u_arg + 4));
1636         }
1637         else {
1638                 memcpy(tcp->u_arg, &tcp->status.pr_reg[CTX_A0],
1639                         tcp->u_nargs * sizeof(tcp->u_arg[0]));
1640         }
1641 # elif UNIXWARE >= 2
1642         /*
1643          * Like SGI, UnixWare doesn't set pr_sysarg until system call exit
1644          */
1645         if (SCNO_IN_RANGE(tcp->scno))
1646                 tcp->u_nargs = sysent[tcp->scno].nargs;
1647         else
1648                 tcp->u_nargs = tcp->status.pr_lwp.pr_nsysarg;
1649         umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1650                 tcp->u_nargs * sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1651 # elif defined(HAVE_PR_SYSCALL)
1652         int i;
1653         if (SCNO_IN_RANGE(tcp->scno))
1654                 tcp->u_nargs = sysent[tcp->scno].nargs;
1655         else
1656                 tcp->u_nargs = tcp->status.pr_nsysarg;
1657         for (i = 0; i < tcp->u_nargs; i++)
1658                 tcp->u_arg[i] = tcp->status.pr_sysarg[i];
1659 # elif defined(I386)
1660         if (SCNO_IN_RANGE(tcp->scno))
1661                 tcp->u_nargs = sysent[tcp->scno].nargs;
1662         else
1663                 tcp->u_nargs = 5;
1664         if (tcp->u_nargs > 0)
1665                 umoven(tcp, tcp->status.PR_REG[UESP] + 4,
1666                         tcp->u_nargs * sizeof(tcp->u_arg[0]), (char *) tcp->u_arg);
1667 # else
1668         I DONT KNOW WHAT TO DO
1669 # endif
1670 #endif /* SVR4 */
1671 #ifdef FREEBSD
1672         if (SCNO_IN_RANGE(tcp->scno) &&
1673             sysent[tcp->scno].nargs > tcp->status.val)
1674                 tcp->u_nargs = sysent[tcp->scno].nargs;
1675         else
1676                 tcp->u_nargs = tcp->status.val;
1677         if (tcp->u_nargs < 0)
1678                 tcp->u_nargs = 0;
1679         if (tcp->u_nargs > MAX_ARGS)
1680                 tcp->u_nargs = MAX_ARGS;
1681         switch (regs.r_eax) {
1682         case SYS___syscall:
1683                 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1684                       regs.r_esp + sizeof(int) + sizeof(quad_t));
1685                 break;
1686         case SYS_syscall:
1687                 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1688                       regs.r_esp + 2 * sizeof(int));
1689                 break;
1690         default:
1691                 pread(tcp->pfd, &tcp->u_arg, tcp->u_nargs * sizeof(unsigned long),
1692                       regs.r_esp + sizeof(int));
1693                 break;
1694         }
1695 #endif /* FREEBSD */
1696         return 1;
1697 }
1698
1699 static int
1700 trace_syscall_entering(struct tcb *tcp)
1701 {
1702         int res, scno_good;
1703
1704 #if defined TCB_WAITEXECVE
1705         if (tcp->flags & TCB_WAITEXECVE) {
1706                 /* This is the post-execve SIGTRAP. */
1707                 tcp->flags &= ~TCB_WAITEXECVE;
1708                 return 0;
1709         }
1710 #endif
1711
1712         scno_good = res = get_scno(tcp);
1713         if (res == 0)
1714                 return res;
1715         if (res == 1)
1716                 res = syscall_fixup_on_sysenter(tcp);
1717         if (res == 0)
1718                 return res;
1719         if (res == 1)
1720                 res = syscall_enter(tcp);
1721         if (res == 0)
1722                 return res;
1723
1724         if (res != 1) {
1725                 printleader(tcp);
1726                 tcp->flags &= ~TCB_REPRINT;
1727                 tcp_last = tcp;
1728                 if (scno_good != 1)
1729                         tprintf("????" /* anti-trigraph gap */ "(");
1730                 else if (!SCNO_IN_RANGE(tcp->scno))
1731                         tprintf("syscall_%lu(", tcp->scno);
1732                 else
1733                         tprintf("%s(", sysent[tcp->scno].sys_name);
1734                 /*
1735                  * " <unavailable>" will be added later by the code which
1736                  * detects ptrace errors.
1737                  */
1738                 goto ret;
1739         }
1740
1741         switch (known_scno(tcp)) {
1742 #ifdef SYS_socket_subcall
1743         case SYS_socketcall:
1744                 decode_subcall(tcp, SYS_socket_subcall,
1745                         SYS_socket_nsubcalls, deref_style);
1746                 break;
1747 #endif
1748 #ifdef SYS_ipc_subcall
1749         case SYS_ipc:
1750                 decode_subcall(tcp, SYS_ipc_subcall,
1751                         SYS_ipc_nsubcalls, shift_style);
1752                 break;
1753 #endif
1754 #ifdef SVR4
1755 #ifdef SYS_pgrpsys_subcall
1756         case SYS_pgrpsys:
1757                 decode_subcall(tcp, SYS_pgrpsys_subcall,
1758                         SYS_pgrpsys_nsubcalls, shift_style);
1759                 break;
1760 #endif /* SYS_pgrpsys_subcall */
1761 #ifdef SYS_sigcall_subcall
1762         case SYS_sigcall:
1763                 decode_subcall(tcp, SYS_sigcall_subcall,
1764                         SYS_sigcall_nsubcalls, mask_style);
1765                 break;
1766 #endif /* SYS_sigcall_subcall */
1767         case SYS_msgsys:
1768                 decode_subcall(tcp, SYS_msgsys_subcall,
1769                         SYS_msgsys_nsubcalls, shift_style);
1770                 break;
1771         case SYS_shmsys:
1772                 decode_subcall(tcp, SYS_shmsys_subcall,
1773                         SYS_shmsys_nsubcalls, shift_style);
1774                 break;
1775         case SYS_semsys:
1776                 decode_subcall(tcp, SYS_semsys_subcall,
1777                         SYS_semsys_nsubcalls, shift_style);
1778                 break;
1779         case SYS_sysfs:
1780                 decode_subcall(tcp, SYS_sysfs_subcall,
1781                         SYS_sysfs_nsubcalls, shift_style);
1782                 break;
1783         case SYS_spcall:
1784                 decode_subcall(tcp, SYS_spcall_subcall,
1785                         SYS_spcall_nsubcalls, shift_style);
1786                 break;
1787 #ifdef SYS_context_subcall
1788         case SYS_context:
1789                 decode_subcall(tcp, SYS_context_subcall,
1790                         SYS_context_nsubcalls, shift_style);
1791                 break;
1792 #endif /* SYS_context_subcall */
1793 #ifdef SYS_door_subcall
1794         case SYS_door:
1795                 decode_subcall(tcp, SYS_door_subcall,
1796                         SYS_door_nsubcalls, door_style);
1797                 break;
1798 #endif /* SYS_door_subcall */
1799 #ifdef SYS_kaio_subcall
1800         case SYS_kaio:
1801                 decode_subcall(tcp, SYS_kaio_subcall,
1802                         SYS_kaio_nsubcalls, shift_style);
1803                 break;
1804 #endif
1805 #endif /* SVR4 */
1806 #ifdef FREEBSD
1807         case SYS_msgsys:
1808         case SYS_shmsys:
1809         case SYS_semsys:
1810                 decode_subcall(tcp, 0, 0, table_style);
1811                 break;
1812 #endif
1813 #ifdef SUNOS4
1814         case SYS_semsys:
1815                 decode_subcall(tcp, SYS_semsys_subcall,
1816                         SYS_semsys_nsubcalls, shift_style);
1817                 break;
1818         case SYS_msgsys:
1819                 decode_subcall(tcp, SYS_msgsys_subcall,
1820                         SYS_msgsys_nsubcalls, shift_style);
1821                 break;
1822         case SYS_shmsys:
1823                 decode_subcall(tcp, SYS_shmsys_subcall,
1824                         SYS_shmsys_nsubcalls, shift_style);
1825                 break;
1826 #endif
1827         }
1828
1829         internal_syscall(tcp);
1830
1831         if ((SCNO_IN_RANGE(tcp->scno) &&
1832              !(qual_flags[tcp->scno] & QUAL_TRACE)) ||
1833             (tracing_paths && !pathtrace_match(tcp))) {
1834                 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
1835                 return 0;
1836         }
1837
1838         tcp->flags &= ~TCB_FILTERED;
1839
1840         if (cflag == CFLAG_ONLY_STATS) {
1841                 res = 0;
1842                 goto ret;
1843         }
1844
1845         printleader(tcp);
1846         tcp->flags &= ~TCB_REPRINT;
1847         tcp_last = tcp;
1848         if (!SCNO_IN_RANGE(tcp->scno))
1849                 tprintf("syscall_%lu(", tcp->scno);
1850         else
1851                 tprintf("%s(", sysent[tcp->scno].sys_name);
1852         if (!SCNO_IN_RANGE(tcp->scno) ||
1853             ((qual_flags[tcp->scno] & QUAL_RAW) &&
1854              sysent[tcp->scno].sys_func != sys_exit))
1855                 res = printargs(tcp);
1856         else
1857                 res = (*sysent[tcp->scno].sys_func)(tcp);
1858
1859         if (fflush(tcp->outf) == EOF)
1860                 return -1;
1861  ret:
1862         tcp->flags |= TCB_INSYSCALL;
1863         /* Measure the entrance time as late as possible to avoid errors. */
1864         if (dtime || cflag)
1865                 gettimeofday(&tcp->etime, NULL);
1866         return res;
1867 }
1868
1869 /* Returns:
1870  * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
1871  * 1: ok, continue in trace_syscall().
1872  * other: error, trace_syscall() should print error indicator
1873  *    ("????" etc) and bail out.
1874  */
1875 static int
1876 get_syscall_result(struct tcb *tcp)
1877 {
1878 #ifdef LINUX
1879 # if defined(S390) || defined(S390X)
1880         if (upeek(tcp, PT_GPR2, &gpr2) < 0)
1881                 return -1;
1882 # elif defined (POWERPC)
1883 # define SO_MASK 0x10000000
1884         {
1885                 long flags;
1886                 if (upeek(tcp, sizeof(unsigned long)*PT_CCR, &flags) < 0)
1887                         return -1;
1888                 if (upeek(tcp, sizeof(unsigned long)*PT_R3, &result) < 0)
1889                         return -1;
1890                 if (flags & SO_MASK)
1891                         result = -result;
1892         }
1893 # elif defined(AVR32)
1894         /* Read complete register set in one go. */
1895         if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, &regs) < 0)
1896                 return -1;
1897 # elif defined(BFIN)
1898         if (upeek(tcp, PT_R0, &r0) < 0)
1899                 return -1;
1900 # elif defined (I386)
1901         if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &i386_regs) < 0)
1902                 return -1;
1903 # elif defined (X86_64)
1904         if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &x86_64_regs) < 0)
1905                 return -1;
1906 # elif defined(IA64)
1907 #       define IA64_PSR_IS      ((long)1 << 34)
1908         if (upeek(tcp, PT_CR_IPSR, &psr) >= 0)
1909                 ia32 = (psr & IA64_PSR_IS) != 0;
1910         if (upeek(tcp, PT_R8, &r8) < 0)
1911                 return -1;
1912         if (upeek(tcp, PT_R10, &r10) < 0)
1913                 return -1;
1914 # elif defined (ARM)
1915         /* Read complete register set in one go. */
1916         if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (void *)&regs) == -1)
1917                 return -1;
1918 # elif defined (M68K)
1919         if (upeek(tcp, 4*PT_D0, &d0) < 0)
1920                 return -1;
1921 # elif defined (LINUX_MIPSN32)
1922         unsigned long long regs[38];
1923
1924         if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1925                 return -1;
1926         a3 = regs[REG_A3];
1927         r2 = regs[REG_V0];
1928 # elif defined (MIPS)
1929         if (upeek(tcp, REG_A3, &a3) < 0)
1930                 return -1;
1931         if (upeek(tcp, REG_V0, &r2) < 0)
1932                 return -1;
1933 # elif defined (ALPHA)
1934         if (upeek(tcp, REG_A3, &a3) < 0)
1935                 return -1;
1936         if (upeek(tcp, REG_R0, &r0) < 0)
1937                 return -1;
1938 # elif defined (SPARC) || defined (SPARC64)
1939         /* Everything we need is in the current register set. */
1940         if (ptrace(PTRACE_GETREGS, tcp->pid, (char *)&regs, 0) < 0)
1941                 return -1;
1942 # elif defined(HPPA)
1943         if (upeek(tcp, PT_GR28, &r28) < 0)
1944                 return -1;
1945 # elif defined(SH)
1946 # elif defined(SH64)
1947 # elif defined(CRISV10) || defined(CRISV32)
1948         if (upeek(tcp, 4*PT_R10, &r10) < 0)
1949                 return -1;
1950 # elif defined(TILE)
1951 # elif defined(MICROBLAZE)
1952         if (upeek(tcp, 3 * 4, &r3) < 0)
1953                 return -1;
1954 # endif
1955 #endif /* LINUX */
1956
1957 #ifdef SUNOS4
1958 #elif defined(SH)
1959         /* new syscall ABI returns result in R0 */
1960         if (upeek(tcp, 4*REG_REG0, (long *)&r0) < 0)
1961                 return -1;
1962 #elif defined(SH64)
1963         /* ABI defines result returned in r9 */
1964         if (upeek(tcp, REG_GENERAL(9), (long *)&r9) < 0)
1965                 return -1;
1966 #endif
1967
1968 #ifdef USE_PROCFS
1969 # ifndef HAVE_PR_SYSCALL
1970 #  ifdef FREEBSD
1971         if (pread(tcp->pfd_reg, &regs, sizeof(regs), 0) < 0) {
1972                 perror("pread");
1973                 return -1;
1974         }
1975 #  endif /* FREEBSD */
1976 # endif /* !HAVE_PR_SYSCALL */
1977 #endif /* USE_PROCFS */
1978
1979         return 1;
1980 }
1981
1982 /* Called at each syscall exit.
1983  * Returns:
1984  * 0: "ignore this ptrace stop", bail out of trace_syscall() silently.
1985  * 1: ok, continue in trace_syscall().
1986  * other: error, trace_syscall() should print error indicator
1987  *    ("????" etc) and bail out.
1988  */
1989 static int
1990 syscall_fixup_on_sysexit(struct tcb *tcp)
1991 {
1992 #ifdef USE_PROCFS
1993         if (tcp->status.PR_WHY != PR_SYSEXIT) {
1994                 fprintf(stderr, "syscall: missing exit\n");
1995                 tcp->flags &= ~TCB_INSYSCALL;
1996         }
1997 #endif /* USE_PROCFS */
1998
1999 #ifdef SUNOS4
2000         {
2001                 int scno = known_scno(tcp);
2002                 if (scno != 0) {
2003                         if (debug) {
2004                                 /*
2005                                  * This happens when a signal handler
2006                                  * for a signal which interrupted a
2007                                  * a system call makes another system call.
2008                                  */
2009                                 fprintf(stderr, "syscall: missing exit\n");
2010                         }
2011                         tcp->flags &= ~TCB_INSYSCALL;
2012                 }
2013         }
2014 #endif /* SUNOS4 */
2015
2016 #ifdef LINUX
2017 # if defined (S390) || defined (S390X)
2018         if (syscall_mode != -ENOSYS)
2019                 syscall_mode = tcp->scno;
2020         if ((tcp->flags & TCB_WAITEXECVE)
2021                  && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
2022                 /*
2023                  * Return from execve.
2024                  * Fake a return value of zero.  We leave the TCB_WAITEXECVE
2025                  * flag set for the post-execve SIGTRAP to see and reset.
2026                  */
2027                 gpr2 = 0;
2028         }
2029 # endif
2030 #endif /* LINUX */
2031         return 1;
2032 }
2033
2034 #ifdef LINUX
2035 /*
2036  * Check the syscall return value register value for whether it is
2037  * a negated errno code indicating an error, or a success return value.
2038  */
2039 static inline int
2040 is_negated_errno(unsigned long int val)
2041 {
2042         unsigned long int max = -(long int) nerrnos;
2043 # if SUPPORTED_PERSONALITIES > 1
2044         if (personality_wordsize[current_personality] < sizeof(val)) {
2045                 val = (unsigned int) val;
2046                 max = (unsigned int) max;
2047         }
2048 # endif
2049         return val > max;
2050 }
2051 #endif
2052
2053 static int
2054 get_error(struct tcb *tcp)
2055 {
2056         int u_error = 0;
2057 #ifdef LINUX
2058         int check_errno = 1;
2059         if (SCNO_IN_RANGE(tcp->scno) &&
2060             sysent[tcp->scno].sys_flags & SYSCALL_NEVER_FAILS) {
2061                 check_errno = 0;
2062         }
2063 # if defined(S390) || defined(S390X)
2064         if (check_errno && is_negated_errno(gpr2)) {
2065                 tcp->u_rval = -1;
2066                 u_error = -gpr2;
2067         }
2068         else {
2069                 tcp->u_rval = gpr2;
2070         }
2071 # elif defined(I386)
2072         if (check_errno && is_negated_errno(i386_regs.eax)) {
2073                 tcp->u_rval = -1;
2074                 u_error = -i386_regs.eax;
2075         }
2076         else {
2077                 tcp->u_rval = i386_regs.eax;
2078         }
2079 # elif defined(X86_64)
2080         if (check_errno && is_negated_errno(x86_64_regs.rax)) {
2081                 tcp->u_rval = -1;
2082                 u_error = -x86_64_regs.rax;
2083         }
2084         else {
2085                 tcp->u_rval = x86_64_regs.rax;
2086         }
2087 # elif defined(IA64)
2088         if (ia32) {
2089                 int err;
2090
2091                 err = (int)r8;
2092                 if (check_errno && is_negated_errno(err)) {
2093                         tcp->u_rval = -1;
2094                         u_error = -err;
2095                 }
2096                 else {
2097                         tcp->u_rval = err;
2098                 }
2099         } else {
2100                 if (check_errno && r10) {
2101                         tcp->u_rval = -1;
2102                         u_error = r8;
2103                 } else {
2104                         tcp->u_rval = r8;
2105                 }
2106         }
2107 # elif defined(MIPS)
2108         if (check_errno && a3) {
2109                 tcp->u_rval = -1;
2110                 u_error = r2;
2111         } else {
2112                 tcp->u_rval = r2;
2113         }
2114 # elif defined(POWERPC)
2115         if (check_errno && is_negated_errno(result)) {
2116                 tcp->u_rval = -1;
2117                 u_error = -result;
2118         }
2119         else {
2120                 tcp->u_rval = result;
2121         }
2122 # elif defined(M68K)
2123         if (check_errno && is_negated_errno(d0)) {
2124                 tcp->u_rval = -1;
2125                 u_error = -d0;
2126         }
2127         else {
2128                 tcp->u_rval = d0;
2129         }
2130 # elif defined(ARM)
2131         if (check_errno && is_negated_errno(regs.ARM_r0)) {
2132                 tcp->u_rval = -1;
2133                 u_error = -regs.ARM_r0;
2134         }
2135         else {
2136                 tcp->u_rval = regs.ARM_r0;
2137         }
2138 # elif defined(AVR32)
2139         if (check_errno && regs.r12 && (unsigned) -regs.r12 < nerrnos) {
2140                 tcp->u_rval = -1;
2141                 u_error = -regs.r12;
2142         }
2143         else {
2144                 tcp->u_rval = regs.r12;
2145         }
2146 # elif defined(BFIN)
2147         if (check_errno && is_negated_errno(r0)) {
2148                 tcp->u_rval = -1;
2149                 u_error = -r0;
2150         } else {
2151                 tcp->u_rval = r0;
2152         }
2153 # elif defined(ALPHA)
2154         if (check_errno && a3) {
2155                 tcp->u_rval = -1;
2156                 u_error = r0;
2157         }
2158         else {
2159                 tcp->u_rval = r0;
2160         }
2161 # elif defined(SPARC)
2162         if (check_errno && regs.psr & PSR_C) {
2163                 tcp->u_rval = -1;
2164                 u_error = regs.u_regs[U_REG_O0];
2165         }
2166         else {
2167                 tcp->u_rval = regs.u_regs[U_REG_O0];
2168         }
2169 # elif defined(SPARC64)
2170         if (check_errno && regs.tstate & 0x1100000000UL) {
2171                 tcp->u_rval = -1;
2172                 u_error = regs.u_regs[U_REG_O0];
2173         }
2174         else {
2175                 tcp->u_rval = regs.u_regs[U_REG_O0];
2176         }
2177 # elif defined(HPPA)
2178         if (check_errno && is_negated_errno(r28)) {
2179                 tcp->u_rval = -1;
2180                 u_error = -r28;
2181         }
2182         else {
2183                 tcp->u_rval = r28;
2184         }
2185 # elif defined(SH)
2186         if (check_errno && is_negated_errno(r0)) {
2187                 tcp->u_rval = -1;
2188                 u_error = -r0;
2189         }
2190         else {
2191                 tcp->u_rval = r0;
2192         }
2193 # elif defined(SH64)
2194         if (check_errno && is_negated_errno(r9)) {
2195                 tcp->u_rval = -1;
2196                 u_error = -r9;
2197         }
2198         else {
2199                 tcp->u_rval = r9;
2200         }
2201 # elif defined(CRISV10) || defined(CRISV32)
2202         if (check_errno && r10 && (unsigned) -r10 < nerrnos) {
2203                 tcp->u_rval = -1;
2204                 u_error = -r10;
2205         }
2206         else {
2207                 tcp->u_rval = r10;
2208         }
2209 # elif defined(TILE)
2210         long rval;
2211         if (upeek(tcp, PTREGS_OFFSET_REG(0), &rval) < 0)
2212                 return -1;
2213         if (check_errno && rval < 0 && rval > -nerrnos) {
2214                 tcp->u_rval = -1;
2215                 u_error = -rval;
2216         }
2217         else {
2218                 tcp->u_rval = rval;
2219         }
2220 # elif defined(MICROBLAZE)
2221         if (check_errno && is_negated_errno(r3)) {
2222                 tcp->u_rval = -1;
2223                 u_error = -r3;
2224         }
2225         else {
2226                 tcp->u_rval = r3;
2227         }
2228 # endif
2229 #endif /* LINUX */
2230 #ifdef SUNOS4
2231         /* get error code from user struct */
2232         if (upeek(tcp, uoff(u_error), &u_error) < 0)
2233                 return -1;
2234         u_error >>= 24; /* u_error is a char */
2235
2236         /* get system call return value */
2237         if (upeek(tcp, uoff(u_rval1), &tcp->u_rval) < 0)
2238                 return -1;
2239 #endif /* SUNOS4 */
2240 #ifdef SVR4
2241 # ifdef SPARC
2242         /* Judicious guessing goes a long way. */
2243         if (tcp->status.pr_reg[R_PSR] & 0x100000) {
2244                 tcp->u_rval = -1;
2245                 u_error = tcp->status.pr_reg[R_O0];
2246         }
2247         else {
2248                 tcp->u_rval = tcp->status.pr_reg[R_O0];
2249         }
2250 # endif /* SPARC */
2251 # ifdef I386
2252         if (tcp->status.PR_REG[EFL] & 0x1) {
2253                 tcp->u_rval = -1;
2254                 u_error = tcp->status.PR_REG[EAX];
2255         }
2256         else {
2257                 tcp->u_rval = tcp->status.PR_REG[EAX];
2258 #  ifdef HAVE_LONG_LONG
2259                 tcp->u_lrval =
2260                         ((unsigned long long) tcp->status.PR_REG[EDX] << 32) +
2261                         tcp->status.PR_REG[EAX];
2262 #  endif
2263         }
2264 # endif /* I386 */
2265 # ifdef X86_64
2266         if (tcp->status.PR_REG[EFLAGS] & 0x1) {
2267                 tcp->u_rval = -1;
2268                 u_error = tcp->status.PR_REG[RAX];
2269         }
2270         else {
2271                 tcp->u_rval = tcp->status.PR_REG[RAX];
2272         }
2273 # endif /* X86_64 */
2274 # ifdef MIPS
2275         if (tcp->status.pr_reg[CTX_A3]) {
2276                 tcp->u_rval = -1;
2277                 u_error = tcp->status.pr_reg[CTX_V0];
2278         }
2279         else {
2280                 tcp->u_rval = tcp->status.pr_reg[CTX_V0];
2281         }
2282 # endif /* MIPS */
2283 #endif /* SVR4 */
2284 #ifdef FREEBSD
2285         if (regs.r_eflags & PSL_C) {
2286                 tcp->u_rval = -1;
2287                 u_error = regs.r_eax;
2288         } else {
2289                 tcp->u_rval = regs.r_eax;
2290                 tcp->u_lrval =
2291                   ((unsigned long long) regs.r_edx << 32) + regs.r_eax;
2292         }
2293 #endif /* FREEBSD */
2294         tcp->u_error = u_error;
2295         return 1;
2296 }
2297
2298 static void
2299 dumpio(struct tcb *tcp)
2300 {
2301         if (syserror(tcp))
2302                 return;
2303         if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= MAX_QUALS)
2304                 return;
2305         if (!SCNO_IN_RANGE(tcp->scno))
2306                 return;
2307         if (sysent[tcp->scno].sys_func == printargs)
2308                 return;
2309         if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
2310                 if (sysent[tcp->scno].sys_func == sys_read ||
2311                     sysent[tcp->scno].sys_func == sys_pread ||
2312                     sysent[tcp->scno].sys_func == sys_pread64 ||
2313                     sysent[tcp->scno].sys_func == sys_recv ||
2314                     sysent[tcp->scno].sys_func == sys_recvfrom)
2315                         dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
2316                 else if (sysent[tcp->scno].sys_func == sys_readv)
2317                         dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2318                 return;
2319         }
2320         if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
2321                 if (sysent[tcp->scno].sys_func == sys_write ||
2322                     sysent[tcp->scno].sys_func == sys_pwrite ||
2323                     sysent[tcp->scno].sys_func == sys_pwrite64 ||
2324                     sysent[tcp->scno].sys_func == sys_send ||
2325                     sysent[tcp->scno].sys_func == sys_sendto)
2326                         dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
2327                 else if (sysent[tcp->scno].sys_func == sys_writev)
2328                         dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2329                 return;
2330         }
2331 }
2332
2333 static int
2334 trace_syscall_exiting(struct tcb *tcp)
2335 {
2336         int sys_res;
2337         struct timeval tv;
2338         int res;
2339         long u_error;
2340
2341         /* Measure the exit time as early as possible to avoid errors. */
2342         if (dtime || cflag)
2343                 gettimeofday(&tv, NULL);
2344
2345 #if SUPPORTED_PERSONALITIES > 1
2346         update_personality(tcp, tcp->currpers);
2347 #endif
2348         res = get_syscall_result(tcp);
2349         if (res == 0)
2350                 return res;
2351         if (res == 1)
2352                 res = syscall_fixup_on_sysexit(tcp);
2353         if (res == 0)
2354                 return res;
2355         if (res == 1)
2356                 res = get_error(tcp);
2357         if (res == 0)
2358                 return res;
2359         if (res == 1)
2360                 internal_syscall(tcp);
2361
2362         if (res == 1 && filtered(tcp)) {
2363                 goto ret;
2364         }
2365
2366         if (tcp->flags & TCB_REPRINT) {
2367                 printleader(tcp);
2368                 if (!SCNO_IN_RANGE(tcp->scno))
2369                         tprintf("<... syscall_%lu resumed> ", tcp->scno);
2370                 else
2371                         tprintf("<... %s resumed> ", sysent[tcp->scno].sys_name);
2372         }
2373
2374         if (cflag) {
2375                 struct timeval t = tv;
2376                 count_syscall(tcp, &t);
2377                 if (cflag == CFLAG_ONLY_STATS) {
2378                         goto ret;
2379                 }
2380         }
2381
2382         if (res != 1) {
2383                 tprints(") ");
2384                 tabto();
2385                 tprints("= ? <unavailable>");
2386                 printtrailer();
2387                 tcp->flags &= ~TCB_INSYSCALL;
2388                 return res;
2389         }
2390
2391         if (!SCNO_IN_RANGE(tcp->scno)
2392             || (qual_flags[tcp->scno] & QUAL_RAW))
2393                 sys_res = printargs(tcp);
2394         else {
2395         /* FIXME: not_failing_only (IOW, option -z) is broken:
2396          * failure of syscall is known only after syscall return.
2397          * Thus we end up with something like this on, say, ENOENT:
2398          *     open("doesnt_exist", O_RDONLY <unfinished ...>
2399          *     {next syscall decode}
2400          * whereas the intended result is that open(...) line
2401          * is not shown at all.
2402          */
2403                 if (not_failing_only && tcp->u_error)
2404                         goto ret;       /* ignore failed syscalls */
2405                 sys_res = (*sysent[tcp->scno].sys_func)(tcp);
2406         }
2407
2408         tprints(") ");
2409         tabto();
2410         u_error = tcp->u_error;
2411         if (!SCNO_IN_RANGE(tcp->scno) ||
2412             qual_flags[tcp->scno] & QUAL_RAW) {
2413                 if (u_error)
2414                         tprintf("= -1 (errno %ld)", u_error);
2415                 else
2416                         tprintf("= %#lx", tcp->u_rval);
2417         }
2418         else if (!(sys_res & RVAL_NONE) && u_error) {
2419                 switch (u_error) {
2420 #ifdef LINUX
2421                 /* Blocked signals do not interrupt any syscalls.
2422                  * In this case syscalls don't return ERESTARTfoo codes.
2423                  *
2424                  * Deadly signals set to SIG_DFL interrupt syscalls
2425                  * and kill the process regardless of which of the codes below
2426                  * is returned by the interrupted syscall.
2427                  * In some cases, kernel forces a kernel-generated deadly
2428                  * signal to be unblocked and set to SIG_DFL (and thus cause
2429                  * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2430                  * or SIGILL. (The alternative is to leave process spinning
2431                  * forever on the faulty instruction - not useful).
2432                  *
2433                  * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2434                  * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2435                  * but kernel will always restart them.
2436                  */
2437                 case ERESTARTSYS:
2438                         /* Most common type of signal-interrupted syscall exit code.
2439                          * The system call will be restarted with the same arguments
2440                          * if SA_RESTART is set; otherwise, it will fail with EINTR.
2441                          */
2442                         tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
2443                         break;
2444                 case ERESTARTNOINTR:
2445                         /* Rare. For example, fork() returns this if interrupted.
2446                          * SA_RESTART is ignored (assumed set): the restart is unconditional.
2447                          */
2448                         tprints("= ? ERESTARTNOINTR (To be restarted)");
2449                         break;
2450                 case ERESTARTNOHAND:
2451                         /* pause(), rt_sigsuspend() etc use this code.
2452                          * SA_RESTART is ignored (assumed not set):
2453                          * syscall won't restart (will return EINTR instead)
2454                          * even after signal with SA_RESTART set.
2455                          * However, after SIG_IGN or SIG_DFL signal it will.
2456                          */
2457                         tprints("= ? ERESTARTNOHAND (Interrupted by signal)");
2458                         break;
2459                 case ERESTART_RESTARTBLOCK:
2460                         /* Syscalls like nanosleep(), poll() which can't be
2461                          * restarted with their original arguments use this
2462                          * code. Kernel will execute restart_syscall() instead,
2463                          * which changes arguments before restarting syscall.
2464                          * SA_RESTART is ignored (assumed not set) similarly
2465                          * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2466                          * since restart data is saved in "restart block"
2467                          * in task struct, and if signal handler uses a syscall
2468                          * which in turn saves another such restart block,
2469                          * old data is lost and restart becomes impossible)
2470                          */
2471                         tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
2472                         break;
2473 #endif /* LINUX */
2474                 default:
2475                         if (u_error < 0)
2476                                 tprintf("= -1 E??? (errno %ld)", u_error);
2477                         else if (u_error < nerrnos)
2478                                 tprintf("= -1 %s (%s)", errnoent[u_error],
2479                                         strerror(u_error));
2480                         else
2481                                 tprintf("= -1 ERRNO_%ld (%s)", u_error,
2482                                         strerror(u_error));
2483                         break;
2484                 }
2485                 if ((sys_res & RVAL_STR) && tcp->auxstr)
2486                         tprintf(" (%s)", tcp->auxstr);
2487         }
2488         else {
2489                 if (sys_res & RVAL_NONE)
2490                         tprints("= ?");
2491                 else {
2492                         switch (sys_res & RVAL_MASK) {
2493                         case RVAL_HEX:
2494                                 tprintf("= %#lx", tcp->u_rval);
2495                                 break;
2496                         case RVAL_OCTAL:
2497                                 tprintf("= %#lo", tcp->u_rval);
2498                                 break;
2499                         case RVAL_UDECIMAL:
2500                                 tprintf("= %lu", tcp->u_rval);
2501                                 break;
2502                         case RVAL_DECIMAL:
2503                                 tprintf("= %ld", tcp->u_rval);
2504                                 break;
2505 #ifdef HAVE_LONG_LONG
2506                         case RVAL_LHEX:
2507                                 tprintf("= %#llx", tcp->u_lrval);
2508                                 break;
2509                         case RVAL_LOCTAL:
2510                                 tprintf("= %#llo", tcp->u_lrval);
2511                                 break;
2512                         case RVAL_LUDECIMAL:
2513                                 tprintf("= %llu", tcp->u_lrval);
2514                                 break;
2515                         case RVAL_LDECIMAL:
2516                                 tprintf("= %lld", tcp->u_lrval);
2517                                 break;
2518 #endif
2519                         default:
2520                                 fprintf(stderr,
2521                                         "invalid rval format\n");
2522                                 break;
2523                         }
2524                 }
2525                 if ((sys_res & RVAL_STR) && tcp->auxstr)
2526                         tprintf(" (%s)", tcp->auxstr);
2527         }
2528         if (dtime) {
2529                 tv_sub(&tv, &tv, &tcp->etime);
2530                 tprintf(" <%ld.%06ld>",
2531                         (long) tv.tv_sec, (long) tv.tv_usec);
2532         }
2533         printtrailer();
2534
2535         dumpio(tcp);
2536         if (fflush(tcp->outf) == EOF)
2537                 return -1;
2538  ret:
2539         tcp->flags &= ~TCB_INSYSCALL;
2540         return 0;
2541 }
2542
2543 int
2544 trace_syscall(struct tcb *tcp)
2545 {
2546         return exiting(tcp) ?
2547                 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2548 }