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