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