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