]> granicus.if.org Git - strace/blob - syscall.c
Get rid of powerpc_getreg(), it's a copy of upeek()
[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->pid, 4*(REG_REG0+1), &val) < 0)
687                 return -1;
688 # elif defined(IA64)
689         if (upeek(tcp->pid, 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 struct pt_regs ppc_regs;
755 static long ppc_result;
756 #elif defined(M68K)
757 static long m68k_d0;
758 #elif defined(BFIN)
759 static long bfin_r0;
760 #elif defined(ARM)
761 struct pt_regs arm_regs; /* not static */
762 # define ARCH_REGS_FOR_GETREGSET arm_regs
763 #elif defined(AARCH64)
764 static union {
765         struct user_pt_regs aarch64_r;
766         struct arm_pt_regs  arm_r;
767 } arm_regs_union;
768 # define aarch64_regs arm_regs_union.aarch64_r
769 # define arm_regs     arm_regs_union.arm_r
770 static struct iovec aarch64_io = {
771         .iov_base = &arm_regs_union
772 };
773 #elif defined(ALPHA)
774 static long alpha_r0;
775 static long alpha_a3;
776 #elif defined(AVR32)
777 static struct pt_regs avr32_regs;
778 #elif defined(SPARC) || defined(SPARC64)
779 struct pt_regs sparc_regs; /* not static */
780 #elif defined(LINUX_MIPSN32)
781 static long long mips_a3;
782 static long long mips_r2;
783 #elif defined(MIPS)
784 static long mips_a3;
785 static long mips_r2;
786 #elif defined(S390) || defined(S390X)
787 static long gpr2;
788 static long syscall_mode;
789 #elif defined(HPPA)
790 static long hppa_r28;
791 #elif defined(SH)
792 static long sh_r0;
793 #elif defined(SH64)
794 static long sh64_r9;
795 #elif defined(CRISV10) || defined(CRISV32)
796 static long cris_r10;
797 #elif defined(TILE)
798 struct pt_regs tile_regs;
799 #elif defined(MICROBLAZE)
800 static long microblaze_r3;
801 #elif defined(OR1K)
802 static struct user_regs_struct or1k_regs;
803 # define ARCH_REGS_FOR_GETREGSET or1k_regs
804 #elif defined(METAG)
805 static struct user_gp_regs metag_regs;
806 # define ARCH_REGS_FOR_GETREGSET metag_regs
807 #elif defined(XTENSA)
808 static long xtensa_a2;
809 #endif
810
811 void
812 printcall(struct tcb *tcp)
813 {
814 #define PRINTBADPC tprintf(sizeof(long) == 4 ? "[????????] " : \
815                            sizeof(long) == 8 ? "[????????????????] " : \
816                            NULL /* crash */)
817         if (get_regs_error) {
818                 PRINTBADPC;
819                 return;
820         }
821 #if defined(I386)
822         tprintf("[%08lx] ", i386_regs.eip);
823 #elif defined(S390) || defined(S390X)
824         long psw;
825         if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0) {
826                 PRINTBADPC;
827                 return;
828         }
829 # ifdef S390
830         tprintf("[%08lx] ", psw);
831 # elif S390X
832         tprintf("[%016lx] ", psw);
833 # endif
834 #elif defined(X86_64) || defined(X32)
835         if (x86_io.iov_len == sizeof(i386_regs)) {
836                 tprintf("[%08x] ", (unsigned) i386_regs.eip);
837         } else {
838 # if defined(X86_64)
839                 tprintf("[%016lx] ", (unsigned long) x86_64_regs.rip);
840 # elif defined(X32)
841                 /* Note: this truncates 64-bit rip to 32 bits */
842                 tprintf("[%08lx] ", (unsigned long) x86_64_regs.rip);
843 # endif
844         }
845 #elif defined(IA64)
846         long ip;
847         if (upeek(tcp->pid, PT_B0, &ip) < 0) {
848                 PRINTBADPC;
849                 return;
850         }
851         tprintf("[%08lx] ", ip);
852 #elif defined(POWERPC)
853         long pc = ppc_regs.nip;
854 # ifdef POWERPC64
855         tprintf("[%016lx] ", pc);
856 # else
857         tprintf("[%08lx] ", pc);
858 # endif
859 #elif defined(M68K)
860         long pc;
861         if (upeek(tcp->pid, 4*PT_PC, &pc) < 0) {
862                 tprints("[????????] ");
863                 return;
864         }
865         tprintf("[%08lx] ", pc);
866 #elif defined(ALPHA)
867         long pc;
868         if (upeek(tcp->pid, REG_PC, &pc) < 0) {
869                 tprints("[????????????????] ");
870                 return;
871         }
872         tprintf("[%08lx] ", pc);
873 #elif defined(SPARC)
874         tprintf("[%08lx] ", sparc_regs.pc);
875 #elif defined(SPARC64)
876         tprintf("[%08lx] ", sparc_regs.tpc);
877 #elif defined(HPPA)
878         long pc;
879         if (upeek(tcp->pid, PT_IAOQ0, &pc) < 0) {
880                 tprints("[????????] ");
881                 return;
882         }
883         tprintf("[%08lx] ", pc);
884 #elif defined(MIPS)
885         long pc;
886         if (upeek(tcp->pid, REG_EPC, &pc) < 0) {
887                 tprints("[????????] ");
888                 return;
889         }
890         tprintf("[%08lx] ", pc);
891 #elif defined(SH)
892         long pc;
893         if (upeek(tcp->pid, 4*REG_PC, &pc) < 0) {
894                 tprints("[????????] ");
895                 return;
896         }
897         tprintf("[%08lx] ", pc);
898 #elif defined(SH64)
899         long pc;
900         if (upeek(tcp->pid, REG_PC, &pc) < 0) {
901                 tprints("[????????????????] ");
902                 return;
903         }
904         tprintf("[%08lx] ", pc);
905 #elif defined(ARM)
906         tprintf("[%08lx] ", arm_regs.ARM_pc);
907 #elif defined(AARCH64)
908         /* tprintf("[%016lx] ", aarch64_regs.regs[???]); */
909 #elif defined(AVR32)
910         tprintf("[%08lx] ", avr32_regs.pc);
911 #elif defined(BFIN)
912         long pc;
913         if (upeek(tcp->pid, PT_PC, &pc) < 0) {
914                 PRINTBADPC;
915                 return;
916         }
917         tprintf("[%08lx] ", pc);
918 #elif defined(CRISV10)
919         long pc;
920         if (upeek(tcp->pid, 4*PT_IRP, &pc) < 0) {
921                 PRINTBADPC;
922                 return;
923         }
924         tprintf("[%08lx] ", pc);
925 #elif defined(CRISV32)
926         long pc;
927         if (upeek(tcp->pid, 4*PT_ERP, &pc) < 0) {
928                 PRINTBADPC;
929                 return;
930         }
931         tprintf("[%08lx] ", pc);
932 #elif defined(TILE)
933 # ifdef _LP64
934         tprintf("[%016lx] ", (unsigned long) tile_regs.pc);
935 # else
936         tprintf("[%08lx] ", (unsigned long) tile_regs.pc);
937 # endif
938 #elif defined(OR1K)
939         tprintf("[%08lx] ", or1k_regs.pc);
940 #elif defined(METAG)
941         tprintf("[%08lx] ", metag_regs.pc);
942 #elif defined(XTENSA)
943         long pc;
944         if (upeek(tcp->pid, REG_PC, &pc) < 0) {
945                 PRINTBADPC;
946                 return;
947         }
948         tprintf("[%08lx] ", pc);
949 #endif /* architecture */
950 }
951
952 /* Shuffle syscall numbers so that we don't have huge gaps in syscall table.
953  * The shuffling should be reversible: shuffle_scno(shuffle_scno(n)) == n.
954  */
955 #if defined(ARM) /* So far only ARM needs this */
956 static long
957 shuffle_scno(unsigned long scno)
958 {
959         if (scno <= ARM_LAST_ORDINARY_SYSCALL)
960                 return scno;
961
962         /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
963         if (scno == 0x000ffff0)
964                 return ARM_LAST_ORDINARY_SYSCALL+1;
965         if (scno == ARM_LAST_ORDINARY_SYSCALL+1)
966                 return 0x000ffff0;
967
968         /* Is it ARM specific syscall?
969          * Swap with [LAST_ORDINARY+2, LAST_ORDINARY+2 + LAST_SPECIAL] range.
970          */
971         if (scno >= 0x000f0000
972          && scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL
973         ) {
974                 return scno - 0x000f0000 + (ARM_LAST_ORDINARY_SYSCALL+2);
975         }
976         if (/* scno >= ARM_LAST_ORDINARY_SYSCALL+2 - always true */ 1
977          && scno <= (ARM_LAST_ORDINARY_SYSCALL+2) + ARM_LAST_SPECIAL_SYSCALL
978         ) {
979                 return scno + 0x000f0000 - (ARM_LAST_ORDINARY_SYSCALL+2);
980         }
981
982         return scno;
983 }
984 #else
985 # define shuffle_scno(scno) ((long)(scno))
986 #endif
987
988 static char*
989 undefined_scno_name(struct tcb *tcp)
990 {
991         static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
992
993         sprintf(buf, "syscall_%lu", shuffle_scno(tcp->scno));
994         return buf;
995 }
996
997 #ifdef POWERPC
998 /*
999  * PTRACE_GETREGS was added to the PowerPC kernel in v2.6.23,
1000  * we provide a slow fallback for old kernels.
1001  */
1002 static int powerpc_getregs_old(pid_t pid)
1003 {
1004         int i;
1005         long r;
1006
1007         r = upeek(pid, sizeof(long) * PT_MSR, &ppc_regs.msr);
1008         if (r)
1009                 goto out;
1010         r = upeek(pid, sizeof(long) * PT_CCR, &ppc_regs.ccr);
1011         if (r)
1012                 goto out;
1013         r = upeek(pid, sizeof(long) * PT_ORIG_R3, &ppc_regs.orig_gpr3);
1014         if (r)
1015                 goto out;
1016         for (i = 0; i <= 8; i++) {
1017                 r = upeek(pid, sizeof(long) * (PT_R0 + i), &ppc_regs.gpr[i]);
1018                 if (r)
1019                         goto out;
1020         }
1021  out:
1022         return r;
1023 }
1024 #endif
1025
1026 #ifndef get_regs
1027 long get_regs_error;
1028
1029 #if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
1030 static void get_regset(pid_t pid)
1031 {
1032 /* constant iovec */
1033 # if defined(ARM) \
1034   || defined(I386) \
1035   || defined(METAG) \
1036   || defined(OR1K)
1037         static struct iovec io = {
1038                 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1039                 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1040         };
1041         get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1042
1043 /* variable iovec */
1044 # elif defined(X86_64) || defined(X32)
1045         /* x86_io.iov_base = &x86_regs_union; - already is */
1046         x86_io.iov_len = sizeof(x86_regs_union);
1047         get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &x86_io);
1048 # elif defined(AARCH64)
1049         /* aarch64_io.iov_base = &arm_regs_union; - already is */
1050         aarch64_io.iov_len = sizeof(arm_regs_union);
1051         get_regs_error = ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &aarch64_io);
1052 # else
1053 #  warning both PTRACE_GETREGSET and NT_PRSTATUS are available but not yet used
1054 # endif
1055 }
1056 #endif /* PTRACE_GETREGSET && NT_PRSTATUS */
1057
1058 void
1059 get_regs(pid_t pid)
1060 {
1061 /* PTRACE_GETREGSET only */
1062 # if defined(METAG) || defined(OR1K) || defined(X32) || defined(AARCH64)
1063         get_regset(pid);
1064
1065 /* PTRACE_GETREGS only */
1066 # elif defined(AVR32)
1067         get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &avr32_regs);
1068 # elif defined(TILE)
1069         get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &tile_regs);
1070 # elif defined(SPARC) || defined(SPARC64)
1071         get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&sparc_regs, 0);
1072 # elif defined(POWERPC)
1073         get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, (long) &ppc_regs);
1074         if (get_regs_error && errno == EIO)
1075                 get_regs_error = powerpc_getregs_old(pid);
1076
1077 /* try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS */
1078 # else
1079 #  if defined(PTRACE_GETREGSET) && defined(NT_PRSTATUS)
1080         static int getregset_support;
1081
1082         if (getregset_support >= 0) {
1083                 get_regset(pid);
1084                 if (getregset_support > 0)
1085                         return;
1086                 if (get_regs_error >= 0) {
1087                         getregset_support = 1;
1088                         return;
1089                 }
1090                 if (errno == EPERM || errno == ESRCH)
1091                         return;
1092                 getregset_support = -1;
1093         }
1094 #  endif /* PTRACE_GETREGSET && NT_PRSTATUS */
1095 #  if defined(ARM)
1096         get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &arm_regs);
1097 #  elif defined(I386)
1098         get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &i386_regs);
1099 #  elif defined(X86_64)
1100         /* Use old method, with unreliable heuristical detection of 32-bitness. */
1101         x86_io.iov_len = sizeof(x86_64_regs);
1102         get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &x86_64_regs);
1103         if (!get_regs_error && x86_64_regs.cs == 0x23) {
1104                 x86_io.iov_len = sizeof(i386_regs);
1105                 /*
1106                  * The order is important: i386_regs and x86_64_regs
1107                  * are overlaid in memory!
1108                  */
1109                 i386_regs.ebx = x86_64_regs.rbx;
1110                 i386_regs.ecx = x86_64_regs.rcx;
1111                 i386_regs.edx = x86_64_regs.rdx;
1112                 i386_regs.esi = x86_64_regs.rsi;
1113                 i386_regs.edi = x86_64_regs.rdi;
1114                 i386_regs.ebp = x86_64_regs.rbp;
1115                 i386_regs.eax = x86_64_regs.rax;
1116                 /* i386_regs.xds = x86_64_regs.ds; unused by strace */
1117                 /* i386_regs.xes = x86_64_regs.es; ditto... */
1118                 /* i386_regs.xfs = x86_64_regs.fs; */
1119                 /* i386_regs.xgs = x86_64_regs.gs; */
1120                 i386_regs.orig_eax = x86_64_regs.orig_rax;
1121                 i386_regs.eip = x86_64_regs.rip;
1122                 /* i386_regs.xcs = x86_64_regs.cs; */
1123                 /* i386_regs.eflags = x86_64_regs.eflags; */
1124                 i386_regs.esp = x86_64_regs.rsp;
1125                 /* i386_regs.xss = x86_64_regs.ss; */
1126         }
1127 #  else
1128 #   error unhandled architecture
1129 #  endif /* ARM || I386 || X86_64 */
1130 # endif
1131 }
1132 #endif /* !get_regs */
1133
1134 /* Returns:
1135  * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1136  * 1: ok, continue in trace_syscall_entering().
1137  * other: error, trace_syscall_entering() should print error indicator
1138  *    ("????" etc) and bail out.
1139  */
1140 static int
1141 get_scno(struct tcb *tcp)
1142 {
1143         long scno = 0;
1144
1145 #if defined(S390) || defined(S390X)
1146         if (upeek(tcp->pid, PT_GPR2, &syscall_mode) < 0)
1147                 return -1;
1148
1149         if (syscall_mode != -ENOSYS) {
1150                 /*
1151                  * Since kernel version 2.5.44 the scno gets passed in gpr2.
1152                  */
1153                 scno = syscall_mode;
1154         } else {
1155                 /*
1156                  * Old style of "passing" the scno via the SVC instruction.
1157                  */
1158                 long psw;
1159                 long opcode, offset_reg, tmp;
1160                 void *svc_addr;
1161                 static const int gpr_offset[16] = {
1162                                 PT_GPR0,  PT_GPR1,  PT_ORIGGPR2, PT_GPR3,
1163                                 PT_GPR4,  PT_GPR5,  PT_GPR6,     PT_GPR7,
1164                                 PT_GPR8,  PT_GPR9,  PT_GPR10,    PT_GPR11,
1165                                 PT_GPR12, PT_GPR13, PT_GPR14,    PT_GPR15
1166                 };
1167
1168                 if (upeek(tcp->pid, PT_PSWADDR, &psw) < 0)
1169                         return -1;
1170                 errno = 0;
1171                 opcode = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)(psw - sizeof(long)), 0);
1172                 if (errno) {
1173                         perror_msg("peektext(psw-oneword)");
1174                         return -1;
1175                 }
1176
1177                 /*
1178                  *  We have to check if the SVC got executed directly or via an
1179                  *  EXECUTE instruction. In case of EXECUTE it is necessary to do
1180                  *  instruction decoding to derive the system call number.
1181                  *  Unfortunately the opcode sizes of EXECUTE and SVC are differently,
1182                  *  so that this doesn't work if a SVC opcode is part of an EXECUTE
1183                  *  opcode. Since there is no way to find out the opcode size this
1184                  *  is the best we can do...
1185                  */
1186                 if ((opcode & 0xff00) == 0x0a00) {
1187                         /* SVC opcode */
1188                         scno = opcode & 0xff;
1189                 }
1190                 else {
1191                         /* SVC got executed by EXECUTE instruction */
1192
1193                         /*
1194                          *  Do instruction decoding of EXECUTE. If you really want to
1195                          *  understand this, read the Principles of Operations.
1196                          */
1197                         svc_addr = (void *) (opcode & 0xfff);
1198
1199                         tmp = 0;
1200                         offset_reg = (opcode & 0x000f0000) >> 16;
1201                         if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
1202                                 return -1;
1203                         svc_addr += tmp;
1204
1205                         tmp = 0;
1206                         offset_reg = (opcode & 0x0000f000) >> 12;
1207                         if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
1208                                 return -1;
1209                         svc_addr += tmp;
1210
1211                         scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, svc_addr, 0);
1212                         if (errno)
1213                                 return -1;
1214 # if defined(S390X)
1215                         scno >>= 48;
1216 # else
1217                         scno >>= 16;
1218 # endif
1219                         tmp = 0;
1220                         offset_reg = (opcode & 0x00f00000) >> 20;
1221                         if (offset_reg && (upeek(tcp->pid, gpr_offset[offset_reg], &tmp) < 0))
1222                                 return -1;
1223
1224                         scno = (scno | tmp) & 0xff;
1225                 }
1226         }
1227 #elif defined(POWERPC)
1228         scno = ppc_regs.gpr[0];
1229 # ifdef POWERPC64
1230         int currpers;
1231
1232         /* Check for 64/32 bit mode. */
1233         /* SF is bit 0 of MSR */
1234         if ((ppc_regs.msr >> 63) & 1)
1235                 currpers = 0;
1236         else
1237                 currpers = 1;
1238         update_personality(tcp, currpers);
1239 # endif
1240 #elif defined(AVR32)
1241         scno = avr32_regs.r8;
1242 #elif defined(BFIN)
1243         if (upeek(tcp->pid, PT_ORIG_P0, &scno))
1244                 return -1;
1245 #elif defined(I386)
1246         scno = i386_regs.orig_eax;
1247 #elif defined(X86_64) || defined(X32)
1248 # ifndef __X32_SYSCALL_BIT
1249 #  define __X32_SYSCALL_BIT     0x40000000
1250 # endif
1251         int currpers;
1252 # if 1
1253         /* GETREGSET of NT_PRSTATUS tells us regset size,
1254          * which unambiguously detects i386.
1255          *
1256          * Linux kernel distinguishes x86-64 and x32 processes
1257          * solely by looking at __X32_SYSCALL_BIT:
1258          * arch/x86/include/asm/compat.h::is_x32_task():
1259          * if (task_pt_regs(current)->orig_ax & __X32_SYSCALL_BIT)
1260          *         return true;
1261          */
1262         if (x86_io.iov_len == sizeof(i386_regs)) {
1263                 scno = i386_regs.orig_eax;
1264                 currpers = 1;
1265         } else {
1266                 scno = x86_64_regs.orig_rax;
1267                 currpers = 0;
1268                 if (scno & __X32_SYSCALL_BIT) {
1269                         scno -= __X32_SYSCALL_BIT;
1270                         currpers = 2;
1271                 }
1272         }
1273 # elif 0
1274         /* cs = 0x33 for long mode (native 64 bit and x32)
1275          * cs = 0x23 for compatibility mode (32 bit)
1276          * ds = 0x2b for x32 mode (x86-64 in 32 bit)
1277          */
1278         scno = x86_64_regs.orig_rax;
1279         switch (x86_64_regs.cs) {
1280                 case 0x23: currpers = 1; break;
1281                 case 0x33:
1282                         if (x86_64_regs.ds == 0x2b) {
1283                                 currpers = 2;
1284                                 scno &= ~__X32_SYSCALL_BIT;
1285                         } else
1286                                 currpers = 0;
1287                         break;
1288                 default:
1289                         fprintf(stderr, "Unknown value CS=0x%08X while "
1290                                  "detecting personality of process "
1291                                  "PID=%d\n", (int)x86_64_regs.cs, tcp->pid);
1292                         currpers = current_personality;
1293                         break;
1294         }
1295 # elif 0
1296         /* This version analyzes the opcode of a syscall instruction.
1297          * (int 0x80 on i386 vs. syscall on x86-64)
1298          * It works, but is too complicated, and strictly speaking, unreliable.
1299          */
1300         unsigned long call, rip = x86_64_regs.rip;
1301         /* sizeof(syscall) == sizeof(int 0x80) == 2 */
1302         rip -= 2;
1303         errno = 0;
1304         call = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)rip, (char *)0);
1305         if (errno)
1306                 fprintf(stderr, "ptrace_peektext failed: %s\n",
1307                                 strerror(errno));
1308         switch (call & 0xffff) {
1309                 /* x86-64: syscall = 0x0f 0x05 */
1310                 case 0x050f: currpers = 0; break;
1311                 /* i386: int 0x80 = 0xcd 0x80 */
1312                 case 0x80cd: currpers = 1; break;
1313                 default:
1314                         currpers = current_personality;
1315                         fprintf(stderr,
1316                                 "Unknown syscall opcode (0x%04X) while "
1317                                 "detecting personality of process "
1318                                 "PID=%d\n", (int)call, tcp->pid);
1319                         break;
1320         }
1321 # endif
1322
1323 # ifdef X32
1324         /* If we are built for a x32 system, then personality 0 is x32
1325          * (not x86_64), and stracing of x86_64 apps is not supported.
1326          * Stracing of i386 apps is still supported.
1327          */
1328         if (currpers == 0) {
1329                 fprintf(stderr, "syscall_%lu(...) in unsupported "
1330                                 "64-bit mode of process PID=%d\n",
1331                         scno, tcp->pid);
1332                 return 0;
1333         }
1334         currpers &= ~2; /* map 2,1 to 0,1 */
1335 # endif
1336         update_personality(tcp, currpers);
1337 #elif defined(IA64)
1338 #       define IA64_PSR_IS      ((long)1 << 34)
1339         long psr;
1340         if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
1341                 ia32 = (psr & IA64_PSR_IS) != 0;
1342         if (ia32) {
1343                 if (upeek(tcp->pid, PT_R1, &scno) < 0)
1344                         return -1;
1345         } else {
1346                 if (upeek(tcp->pid, PT_R15, &scno) < 0)
1347                         return -1;
1348         }
1349 #elif defined(AARCH64)
1350         switch (aarch64_io.iov_len) {
1351                 case sizeof(aarch64_regs):
1352                         /* We are in 64-bit mode */
1353                         scno = aarch64_regs.regs[8];
1354                         update_personality(tcp, 1);
1355                         break;
1356                 case sizeof(arm_regs):
1357                         /* We are in 32-bit mode */
1358                         scno = arm_regs.ARM_r7;
1359                         update_personality(tcp, 0);
1360                         break;
1361         }
1362 #elif defined(ARM)
1363         if (arm_regs.ARM_ip != 0) {
1364                 /* It is not a syscall entry */
1365                 fprintf(stderr, "pid %d stray syscall exit\n", tcp->pid);
1366                 tcp->flags |= TCB_INSYSCALL;
1367                 return 0;
1368         }
1369         /* Note: we support only 32-bit CPUs, not 26-bit */
1370
1371         if (arm_regs.ARM_cpsr & 0x20) {
1372                 /* Thumb mode */
1373                 scno = arm_regs.ARM_r7;
1374         } else {
1375                 /* ARM mode */
1376                 errno = 0;
1377                 scno = ptrace(PTRACE_PEEKTEXT, tcp->pid, (void *)(arm_regs.ARM_pc - 4), NULL);
1378                 if (errno)
1379                         return -1;
1380
1381                 /* EABI syscall convention? */
1382                 if (scno == 0xef000000) {
1383                         scno = arm_regs.ARM_r7; /* yes */
1384                 } else {
1385                         if ((scno & 0x0ff00000) != 0x0f900000) {
1386                                 fprintf(stderr, "pid %d unknown syscall trap 0x%08lx\n",
1387                                         tcp->pid, scno);
1388                                 return -1;
1389                         }
1390                         /* Fixup the syscall number */
1391                         scno &= 0x000fffff;
1392                 }
1393         }
1394
1395         scno = shuffle_scno(scno);
1396 #elif defined(M68K)
1397         if (upeek(tcp->pid, 4*PT_ORIG_D0, &scno) < 0)
1398                 return -1;
1399 #elif defined(LINUX_MIPSN32)
1400         unsigned long long regs[38];
1401
1402         if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1403                 return -1;
1404         mips_a3 = regs[REG_A3];
1405         mips_r2 = regs[REG_V0];
1406
1407         scno = mips_r2;
1408         if (!SCNO_IN_RANGE(scno)) {
1409                 if (mips_a3 == 0 || mips_a3 == -1) {
1410                         if (debug_flag)
1411                                 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
1412                         return 0;
1413                 }
1414         }
1415 #elif defined(MIPS)
1416         if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
1417                 return -1;
1418         if (upeek(tcp->pid, REG_V0, &scno) < 0)
1419                 return -1;
1420
1421         if (!SCNO_IN_RANGE(scno)) {
1422                 if (mips_a3 == 0 || mips_a3 == -1) {
1423                         if (debug_flag)
1424                                 fprintf(stderr, "stray syscall exit: v0 = %ld\n", scno);
1425                         return 0;
1426                 }
1427         }
1428 #elif defined(ALPHA)
1429         if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
1430                 return -1;
1431         if (upeek(tcp->pid, REG_R0, &scno) < 0)
1432                 return -1;
1433
1434         /*
1435          * Do some sanity checks to figure out if it's
1436          * really a syscall entry
1437          */
1438         if (!SCNO_IN_RANGE(scno)) {
1439                 if (alpha_a3 == 0 || alpha_a3 == -1) {
1440                         if (debug_flag)
1441                                 fprintf(stderr, "stray syscall exit: r0 = %ld\n", scno);
1442                         return 0;
1443                 }
1444         }
1445 #elif defined(SPARC) || defined(SPARC64)
1446         /* Disassemble the syscall trap. */
1447         /* Retrieve the syscall trap instruction. */
1448         unsigned long trap;
1449         errno = 0;
1450 # if defined(SPARC64)
1451         trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.tpc, 0);
1452         trap >>= 32;
1453 # else
1454         trap = ptrace(PTRACE_PEEKTEXT, tcp->pid, (char *)sparc_regs.pc, 0);
1455 # endif
1456         if (errno)
1457                 return -1;
1458
1459         /* Disassemble the trap to see what personality to use. */
1460         switch (trap) {
1461         case 0x91d02010:
1462                 /* Linux/SPARC syscall trap. */
1463                 update_personality(tcp, 0);
1464                 break;
1465         case 0x91d0206d:
1466                 /* Linux/SPARC64 syscall trap. */
1467                 update_personality(tcp, 2);
1468                 break;
1469         case 0x91d02000:
1470                 /* SunOS syscall trap. (pers 1) */
1471                 fprintf(stderr, "syscall: SunOS no support\n");
1472                 return -1;
1473         case 0x91d02008:
1474                 /* Solaris 2.x syscall trap. (per 2) */
1475                 update_personality(tcp, 1);
1476                 break;
1477         case 0x91d02009:
1478                 /* NetBSD/FreeBSD syscall trap. */
1479                 fprintf(stderr, "syscall: NetBSD/FreeBSD not supported\n");
1480                 return -1;
1481         case 0x91d02027:
1482                 /* Solaris 2.x gettimeofday */
1483                 update_personality(tcp, 1);
1484                 break;
1485         default:
1486 # if defined(SPARC64)
1487                 fprintf(stderr, "syscall: unknown syscall trap %08lx %016lx\n", trap, sparc_regs.tpc);
1488 # else
1489                 fprintf(stderr, "syscall: unknown syscall trap %08lx %08lx\n", trap, sparc_regs.pc);
1490 # endif
1491                 return -1;
1492         }
1493
1494         /* Extract the system call number from the registers. */
1495         if (trap == 0x91d02027)
1496                 scno = 156;
1497         else
1498                 scno = sparc_regs.u_regs[U_REG_G1];
1499         if (scno == 0) {
1500                 scno = sparc_regs.u_regs[U_REG_O0];
1501                 memmove(&sparc_regs.u_regs[U_REG_O0], &sparc_regs.u_regs[U_REG_O1], 7*sizeof(sparc_regs.u_regs[0]));
1502         }
1503 #elif defined(HPPA)
1504         if (upeek(tcp->pid, PT_GR20, &scno) < 0)
1505                 return -1;
1506 #elif defined(SH)
1507         /*
1508          * In the new syscall ABI, the system call number is in R3.
1509          */
1510         if (upeek(tcp->pid, 4*(REG_REG0+3), &scno) < 0)
1511                 return -1;
1512
1513         if (scno < 0) {
1514                 /* Odd as it may seem, a glibc bug has been known to cause
1515                    glibc to issue bogus negative syscall numbers.  So for
1516                    our purposes, make strace print what it *should* have been */
1517                 long correct_scno = (scno & 0xff);
1518                 if (debug_flag)
1519                         fprintf(stderr,
1520                                 "Detected glibc bug: bogus system call"
1521                                 " number = %ld, correcting to %ld\n",
1522                                 scno,
1523                                 correct_scno);
1524                 scno = correct_scno;
1525         }
1526 #elif defined(SH64)
1527         if (upeek(tcp->pid, REG_SYSCALL, &scno) < 0)
1528                 return -1;
1529         scno &= 0xFFFF;
1530 #elif defined(CRISV10) || defined(CRISV32)
1531         if (upeek(tcp->pid, 4*PT_R9, &scno) < 0)
1532                 return -1;
1533 #elif defined(TILE)
1534         int currpers;
1535         scno = tile_regs.regs[10];
1536 # ifdef __tilepro__
1537         currpers = 1;
1538 # else
1539 #  ifndef PT_FLAGS_COMPAT
1540 #   define PT_FLAGS_COMPAT 0x10000  /* from Linux 3.8 on */
1541 #  endif
1542         if (tile_regs.flags & PT_FLAGS_COMPAT)
1543                 currpers = 1;
1544         else
1545                 currpers = 0;
1546 # endif
1547         update_personality(tcp, currpers);
1548 #elif defined(MICROBLAZE)
1549         if (upeek(tcp->pid, 0, &scno) < 0)
1550                 return -1;
1551 #elif defined(OR1K)
1552         scno = or1k_regs.gpr[11];
1553 #elif defined(METAG)
1554         scno = metag_regs.dx[0][1];     /* syscall number in D1Re0 (D1.0) */
1555 #elif defined(XTENSA)
1556         if (upeek(tcp->pid, SYSCALL_NR, &scno) < 0)
1557                 return -1;
1558 #endif
1559
1560         tcp->scno = scno;
1561         if (SCNO_IS_VALID(tcp->scno)) {
1562                 tcp->s_ent = &sysent[scno];
1563                 tcp->qual_flg = qual_flags[scno];
1564         } else {
1565                 static const struct_sysent unknown = {
1566                         .nargs = MAX_ARGS,
1567                         .sys_flags = 0,
1568                         .sys_func = printargs,
1569                         .sys_name = "unknown", /* not used */
1570                 };
1571                 tcp->s_ent = &unknown;
1572                 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1573         }
1574         return 1;
1575 }
1576
1577 /* Called at each syscall entry.
1578  * Returns:
1579  * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1580  * 1: ok, continue in trace_syscall_entering().
1581  * other: error, trace_syscall_entering() should print error indicator
1582  *    ("????" etc) and bail out.
1583  */
1584 static int
1585 syscall_fixup_on_sysenter(struct tcb *tcp)
1586 {
1587         /* A common case of "not a syscall entry" is post-execve SIGTRAP */
1588 #if defined(I386)
1589         if (i386_regs.eax != -ENOSYS) {
1590                 if (debug_flag)
1591                         fprintf(stderr, "not a syscall entry (eax = %ld)\n", i386_regs.eax);
1592                 return 0;
1593         }
1594 #elif defined(X86_64) || defined(X32)
1595         {
1596                 long rax;
1597                 if (x86_io.iov_len == sizeof(i386_regs)) {
1598                         /* Sign extend from 32 bits */
1599                         rax = (int32_t)i386_regs.eax;
1600                 } else {
1601                         /* Note: in X32 build, this truncates 64 to 32 bits */
1602                         rax = x86_64_regs.rax;
1603                 }
1604                 if (rax != -ENOSYS) {
1605                         if (debug_flag)
1606                                 fprintf(stderr, "not a syscall entry (rax = %ld)\n", rax);
1607                         return 0;
1608                 }
1609         }
1610 #elif defined(S390) || defined(S390X)
1611         /* TODO: we already fetched PT_GPR2 in get_scno
1612          * and stored it in syscall_mode, reuse it here
1613          * instead of re-fetching?
1614          */
1615         if (upeek(tcp->pid, PT_GPR2, &gpr2) < 0)
1616                 return -1;
1617         if (syscall_mode != -ENOSYS)
1618                 syscall_mode = tcp->scno;
1619         if (gpr2 != syscall_mode) {
1620                 if (debug_flag)
1621                         fprintf(stderr, "not a syscall entry (gpr2 = %ld)\n", gpr2);
1622                 return 0;
1623         }
1624 #elif defined(M68K)
1625         /* TODO? Eliminate upeek's in arches below like we did in x86 */
1626         if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
1627                 return -1;
1628         if (m68k_d0 != -ENOSYS) {
1629                 if (debug_flag)
1630                         fprintf(stderr, "not a syscall entry (d0 = %ld)\n", m68k_d0);
1631                 return 0;
1632         }
1633 #elif defined(IA64)
1634         if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
1635                 return -1;
1636         if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
1637                 return -1;
1638         if (ia32 && ia64_r8 != -ENOSYS) {
1639                 if (debug_flag)
1640                         fprintf(stderr, "not a syscall entry (r8 = %ld)\n", ia64_r8);
1641                 return 0;
1642         }
1643 #elif defined(CRISV10) || defined(CRISV32)
1644         if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
1645                 return -1;
1646         if (cris_r10 != -ENOSYS) {
1647                 if (debug_flag)
1648                         fprintf(stderr, "not a syscall entry (r10 = %ld)\n", cris_r10);
1649                 return 0;
1650         }
1651 #elif defined(MICROBLAZE)
1652         if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
1653                 return -1;
1654         if (microblaze_r3 != -ENOSYS) {
1655                 if (debug_flag)
1656                         fprintf(stderr, "not a syscall entry (r3 = %ld)\n", microblaze_r3);
1657                 return 0;
1658         }
1659 #endif
1660         return 1;
1661 }
1662
1663 static void
1664 internal_fork(struct tcb *tcp)
1665 {
1666 #if defined S390 || defined S390X || defined CRISV10 || defined CRISV32
1667 # define ARG_FLAGS      1
1668 #else
1669 # define ARG_FLAGS      0
1670 #endif
1671 #ifndef CLONE_UNTRACED
1672 # define CLONE_UNTRACED 0x00800000
1673 #endif
1674         if ((ptrace_setoptions
1675             & (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1676            == (PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK))
1677                 return;
1678
1679         if (!followfork)
1680                 return;
1681
1682         if (entering(tcp)) {
1683                 /*
1684                  * We won't see the new child if clone is called with
1685                  * CLONE_UNTRACED, so we keep the same logic with that option
1686                  * and don't trace it.
1687                  */
1688                 if ((tcp->s_ent->sys_func == sys_clone)
1689                  && (tcp->u_arg[ARG_FLAGS] & CLONE_UNTRACED)
1690                 )
1691                         return;
1692                 setbpt(tcp);
1693         } else {
1694                 if (tcp->flags & TCB_BPTSET)
1695                         clearbpt(tcp);
1696         }
1697 }
1698
1699 #if defined(TCB_WAITEXECVE)
1700 static void
1701 internal_exec(struct tcb *tcp)
1702 {
1703         /* Maybe we have post-execve SIGTRAP suppressed? */
1704         if (ptrace_setoptions & PTRACE_O_TRACEEXEC)
1705                 return; /* yes, no need to do anything */
1706
1707         if (exiting(tcp) && syserror(tcp))
1708                 /* Error in execve, no post-execve SIGTRAP expected */
1709                 tcp->flags &= ~TCB_WAITEXECVE;
1710         else
1711                 tcp->flags |= TCB_WAITEXECVE;
1712 }
1713 #endif
1714
1715 static void
1716 syscall_fixup_for_fork_exec(struct tcb *tcp)
1717 {
1718         /*
1719          * We must always trace a few critical system calls in order to
1720          * correctly support following forks in the presence of tracing
1721          * qualifiers.
1722          */
1723         int (*func)();
1724
1725         func = tcp->s_ent->sys_func;
1726
1727         if (   sys_fork == func
1728             || sys_vfork == func
1729             || sys_clone == func
1730            ) {
1731                 internal_fork(tcp);
1732                 return;
1733         }
1734
1735 #if defined(TCB_WAITEXECVE)
1736         if (   sys_execve == func
1737 # if defined(SPARC) || defined(SPARC64)
1738             || sys_execv == func
1739 # endif
1740            ) {
1741                 internal_exec(tcp);
1742                 return;
1743         }
1744 #endif
1745 }
1746
1747 /* Return -1 on error or 1 on success (never 0!) */
1748 static int
1749 get_syscall_args(struct tcb *tcp)
1750 {
1751         int i, nargs;
1752
1753         nargs = tcp->s_ent->nargs;
1754
1755 #if defined(S390) || defined(S390X)
1756         for (i = 0; i < nargs; ++i)
1757                 if (upeek(tcp->pid, i==0 ? PT_ORIGGPR2 : PT_GPR2 + i*sizeof(long), &tcp->u_arg[i]) < 0)
1758                         return -1;
1759 #elif defined(ALPHA)
1760         for (i = 0; i < nargs; ++i)
1761                 if (upeek(tcp->pid, REG_A0+i, &tcp->u_arg[i]) < 0)
1762                         return -1;
1763 #elif defined(IA64)
1764         if (!ia32) {
1765                 unsigned long *out0, cfm, sof, sol;
1766                 long rbs_end;
1767                 /* be backwards compatible with kernel < 2.4.4... */
1768 #               ifndef PT_RBS_END
1769 #                 define PT_RBS_END     PT_AR_BSP
1770 #               endif
1771
1772                 if (upeek(tcp->pid, PT_RBS_END, &rbs_end) < 0)
1773                         return -1;
1774                 if (upeek(tcp->pid, PT_CFM, (long *) &cfm) < 0)
1775                         return -1;
1776
1777                 sof = (cfm >> 0) & 0x7f;
1778                 sol = (cfm >> 7) & 0x7f;
1779                 out0 = ia64_rse_skip_regs((unsigned long *) rbs_end, -sof + sol);
1780
1781                 for (i = 0; i < nargs; ++i) {
1782                         if (umoven(tcp, (unsigned long) ia64_rse_skip_regs(out0, i),
1783                                    sizeof(long), (char *) &tcp->u_arg[i]) < 0)
1784                                 return -1;
1785                 }
1786         } else {
1787                 static const int argreg[MAX_ARGS] = { PT_R11 /* EBX = out0 */,
1788                                                       PT_R9  /* ECX = out1 */,
1789                                                       PT_R10 /* EDX = out2 */,
1790                                                       PT_R14 /* ESI = out3 */,
1791                                                       PT_R15 /* EDI = out4 */,
1792                                                       PT_R13 /* EBP = out5 */};
1793
1794                 for (i = 0; i < nargs; ++i) {
1795                         if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
1796                                 return -1;
1797                         /* truncate away IVE sign-extension */
1798                         tcp->u_arg[i] &= 0xffffffff;
1799                 }
1800         }
1801 #elif defined(LINUX_MIPSN32) || defined(LINUX_MIPSN64)
1802         /* N32 and N64 both use up to six registers.  */
1803         unsigned long long regs[38];
1804
1805         if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
1806                 return -1;
1807
1808         for (i = 0; i < nargs; ++i) {
1809                 tcp->u_arg[i] = regs[REG_A0 + i];
1810 # if defined(LINUX_MIPSN32)
1811                 tcp->ext_arg[i] = regs[REG_A0 + i];
1812 # endif
1813         }
1814 #elif defined(MIPS)
1815         if (nargs > 4) {
1816                 long sp;
1817
1818                 if (upeek(tcp->pid, REG_SP, &sp) < 0)
1819                         return -1;
1820                 for (i = 0; i < 4; ++i)
1821                         if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1822                                 return -1;
1823                 umoven(tcp, sp + 16, (nargs - 4) * sizeof(tcp->u_arg[0]),
1824                        (char *)(tcp->u_arg + 4));
1825         } else {
1826                 for (i = 0; i < nargs; ++i)
1827                         if (upeek(tcp->pid, REG_A0 + i, &tcp->u_arg[i]) < 0)
1828                                 return -1;
1829         }
1830 #elif defined(POWERPC)
1831         (void)i;
1832         (void)nargs;
1833         tcp->u_arg[0] = ppc_regs.orig_gpr3;
1834         tcp->u_arg[1] = ppc_regs.gpr[4];
1835         tcp->u_arg[2] = ppc_regs.gpr[5];
1836         tcp->u_arg[3] = ppc_regs.gpr[6];
1837         tcp->u_arg[4] = ppc_regs.gpr[7];
1838         tcp->u_arg[5] = ppc_regs.gpr[8];
1839 #elif defined(SPARC) || defined(SPARC64)
1840         for (i = 0; i < nargs; ++i)
1841                 tcp->u_arg[i] = sparc_regs.u_regs[U_REG_O0 + i];
1842 #elif defined(HPPA)
1843         for (i = 0; i < nargs; ++i)
1844                 if (upeek(tcp->pid, PT_GR26-4*i, &tcp->u_arg[i]) < 0)
1845                         return -1;
1846 #elif defined(ARM) || defined(AARCH64)
1847 # if defined(AARCH64)
1848         if (tcp->currpers == 1)
1849                 for (i = 0; i < nargs; ++i)
1850                         tcp->u_arg[i] = aarch64_regs.regs[i];
1851         else
1852 # endif
1853         for (i = 0; i < nargs; ++i)
1854                 tcp->u_arg[i] = arm_regs.uregs[i];
1855 #elif defined(AVR32)
1856         (void)i;
1857         (void)nargs;
1858         tcp->u_arg[0] = avr32_regs.r12;
1859         tcp->u_arg[1] = avr32_regs.r11;
1860         tcp->u_arg[2] = avr32_regs.r10;
1861         tcp->u_arg[3] = avr32_regs.r9;
1862         tcp->u_arg[4] = avr32_regs.r5;
1863         tcp->u_arg[5] = avr32_regs.r3;
1864 #elif defined(BFIN)
1865         static const int argreg[MAX_ARGS] = { PT_R0, PT_R1, PT_R2, PT_R3, PT_R4, PT_R5 };
1866
1867         for (i = 0; i < nargs; ++i)
1868                 if (upeek(tcp->pid, argreg[i], &tcp->u_arg[i]) < 0)
1869                         return -1;
1870 #elif defined(SH)
1871         static const int syscall_regs[MAX_ARGS] = {
1872                 4 * (REG_REG0+4), 4 * (REG_REG0+5), 4 * (REG_REG0+6),
1873                 4 * (REG_REG0+7), 4 * (REG_REG0  ), 4 * (REG_REG0+1)
1874         };
1875
1876         for (i = 0; i < nargs; ++i)
1877                 if (upeek(tcp->pid, syscall_regs[i], &tcp->u_arg[i]) < 0)
1878                         return -1;
1879 #elif defined(SH64)
1880         int i;
1881         /* Registers used by SH5 Linux system calls for parameters */
1882         static const int syscall_regs[MAX_ARGS] = { 2, 3, 4, 5, 6, 7 };
1883
1884         for (i = 0; i < nargs; ++i)
1885                 if (upeek(tcp->pid, REG_GENERAL(syscall_regs[i]), &tcp->u_arg[i]) < 0)
1886                         return -1;
1887 #elif defined(I386)
1888         (void)i;
1889         (void)nargs;
1890         tcp->u_arg[0] = i386_regs.ebx;
1891         tcp->u_arg[1] = i386_regs.ecx;
1892         tcp->u_arg[2] = i386_regs.edx;
1893         tcp->u_arg[3] = i386_regs.esi;
1894         tcp->u_arg[4] = i386_regs.edi;
1895         tcp->u_arg[5] = i386_regs.ebp;
1896 #elif defined(X86_64) || defined(X32)
1897         (void)i;
1898         (void)nargs;
1899         if (x86_io.iov_len != sizeof(i386_regs)) {
1900                 /* x86-64 or x32 ABI */
1901                 tcp->u_arg[0] = x86_64_regs.rdi;
1902                 tcp->u_arg[1] = x86_64_regs.rsi;
1903                 tcp->u_arg[2] = x86_64_regs.rdx;
1904                 tcp->u_arg[3] = x86_64_regs.r10;
1905                 tcp->u_arg[4] = x86_64_regs.r8;
1906                 tcp->u_arg[5] = x86_64_regs.r9;
1907 #  ifdef X32
1908                 tcp->ext_arg[0] = x86_64_regs.rdi;
1909                 tcp->ext_arg[1] = x86_64_regs.rsi;
1910                 tcp->ext_arg[2] = x86_64_regs.rdx;
1911                 tcp->ext_arg[3] = x86_64_regs.r10;
1912                 tcp->ext_arg[4] = x86_64_regs.r8;
1913                 tcp->ext_arg[5] = x86_64_regs.r9;
1914 #  endif
1915         } else {
1916                 /* i386 ABI */
1917                 /* Zero-extend from 32 bits */
1918                 /* Use widen_to_long(tcp->u_arg[N]) in syscall handlers
1919                  * if you need to use *sign-extended* parameter.
1920                  */
1921                 tcp->u_arg[0] = (long)(uint32_t)i386_regs.ebx;
1922                 tcp->u_arg[1] = (long)(uint32_t)i386_regs.ecx;
1923                 tcp->u_arg[2] = (long)(uint32_t)i386_regs.edx;
1924                 tcp->u_arg[3] = (long)(uint32_t)i386_regs.esi;
1925                 tcp->u_arg[4] = (long)(uint32_t)i386_regs.edi;
1926                 tcp->u_arg[5] = (long)(uint32_t)i386_regs.ebp;
1927         }
1928 #elif defined(MICROBLAZE)
1929         for (i = 0; i < nargs; ++i)
1930                 if (upeek(tcp->pid, (5 + i) * 4, &tcp->u_arg[i]) < 0)
1931                         return -1;
1932 #elif defined(CRISV10) || defined(CRISV32)
1933         static const int crisregs[MAX_ARGS] = {
1934                 4*PT_ORIG_R10, 4*PT_R11, 4*PT_R12,
1935                 4*PT_R13     , 4*PT_MOF, 4*PT_SRP
1936         };
1937
1938         for (i = 0; i < nargs; ++i)
1939                 if (upeek(tcp->pid, crisregs[i], &tcp->u_arg[i]) < 0)
1940                         return -1;
1941 #elif defined(TILE)
1942         for (i = 0; i < nargs; ++i)
1943                 tcp->u_arg[i] = tile_regs.regs[i];
1944 #elif defined(M68K)
1945         for (i = 0; i < nargs; ++i)
1946                 if (upeek(tcp->pid, (i < 5 ? i : i + 2)*4, &tcp->u_arg[i]) < 0)
1947                         return -1;
1948 #elif defined(OR1K)
1949         (void)nargs;
1950         for (i = 0; i < 6; ++i)
1951                 tcp->u_arg[i] = or1k_regs.gpr[3 + i];
1952 #elif defined(METAG)
1953         for (i = 0; i < nargs; i++)
1954                 /* arguments go backwards from D1Ar1 (D1.3) */
1955                 tcp->u_arg[i] = ((unsigned long *)&metag_regs.dx[3][1])[-i];
1956 #elif defined(XTENSA)
1957         /* arg0: a6, arg1: a3, arg2: a4, arg3: a5, arg4: a8, arg5: a9 */
1958         static const int xtensaregs[MAX_ARGS] = { 6, 3, 4, 5, 8, 9 };
1959         for (i = 0; i < nargs; ++i)
1960                 if (upeek(tcp->pid, REG_A_BASE + xtensaregs[i], &tcp->u_arg[i]) < 0)
1961                         return -1;
1962 #else /* Other architecture (32bits specific) */
1963         for (i = 0; i < nargs; ++i)
1964                 if (upeek(tcp->pid, i*4, &tcp->u_arg[i]) < 0)
1965                         return -1;
1966 #endif
1967         return 1;
1968 }
1969
1970 static int
1971 trace_syscall_entering(struct tcb *tcp)
1972 {
1973         int res, scno_good;
1974
1975 #if defined TCB_WAITEXECVE
1976         if (tcp->flags & TCB_WAITEXECVE) {
1977                 /* This is the post-execve SIGTRAP. */
1978                 tcp->flags &= ~TCB_WAITEXECVE;
1979                 return 0;
1980         }
1981 #endif
1982
1983         scno_good = res = (get_regs_error ? -1 : get_scno(tcp));
1984         if (res == 0)
1985                 return res;
1986         if (res == 1) {
1987                 res = syscall_fixup_on_sysenter(tcp);
1988                 if (res == 0)
1989                         return res;
1990                 if (res == 1)
1991                         res = get_syscall_args(tcp);
1992         }
1993
1994         if (res != 1) {
1995                 printleader(tcp);
1996                 if (scno_good != 1)
1997                         tprints("????" /* anti-trigraph gap */ "(");
1998                 else if (tcp->qual_flg & UNDEFINED_SCNO)
1999                         tprintf("%s(", undefined_scno_name(tcp));
2000                 else
2001                         tprintf("%s(", tcp->s_ent->sys_name);
2002                 /*
2003                  * " <unavailable>" will be added later by the code which
2004                  * detects ptrace errors.
2005                  */
2006                 goto ret;
2007         }
2008
2009         if (   sys_execve == tcp->s_ent->sys_func
2010 # if defined(SPARC) || defined(SPARC64)
2011             || sys_execv == tcp->s_ent->sys_func
2012 # endif
2013            ) {
2014                 hide_log_until_execve = 0;
2015         }
2016
2017 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
2018         while (1) {
2019 # ifdef SYS_socket_subcall
2020                 if (tcp->s_ent->sys_func == sys_socketcall) {
2021                         decode_socket_subcall(tcp);
2022                         break;
2023                 }
2024 # endif
2025 # ifdef SYS_ipc_subcall
2026                 if (tcp->s_ent->sys_func == sys_ipc) {
2027                         decode_ipc_subcall(tcp);
2028                         break;
2029                 }
2030 # endif
2031                 break;
2032         }
2033 #endif
2034
2035         if (need_fork_exec_workarounds)
2036                 syscall_fixup_for_fork_exec(tcp);
2037
2038         if (!(tcp->qual_flg & QUAL_TRACE)
2039          || (tracing_paths && !pathtrace_match(tcp))
2040         ) {
2041                 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
2042                 return 0;
2043         }
2044
2045         tcp->flags &= ~TCB_FILTERED;
2046
2047         if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
2048                 res = 0;
2049                 goto ret;
2050         }
2051
2052         printleader(tcp);
2053         if (tcp->qual_flg & UNDEFINED_SCNO)
2054                 tprintf("%s(", undefined_scno_name(tcp));
2055         else
2056                 tprintf("%s(", tcp->s_ent->sys_name);
2057         if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
2058                 res = printargs(tcp);
2059         else
2060                 res = tcp->s_ent->sys_func(tcp);
2061
2062         fflush(tcp->outf);
2063  ret:
2064         tcp->flags |= TCB_INSYSCALL;
2065         /* Measure the entrance time as late as possible to avoid errors. */
2066         if (Tflag || cflag)
2067                 gettimeofday(&tcp->etime, NULL);
2068         return res;
2069 }
2070
2071 /* Returns:
2072  * 1: ok, continue in trace_syscall_exiting().
2073  * -1: error, trace_syscall_exiting() should print error indicator
2074  *    ("????" etc) and bail out.
2075  */
2076 static int
2077 get_syscall_result(struct tcb *tcp)
2078 {
2079 #if defined(S390) || defined(S390X)
2080         if (upeek(tcp->pid, PT_GPR2, &gpr2) < 0)
2081                 return -1;
2082 #elif defined(POWERPC)
2083 # define SO_MASK 0x10000000
2084         ppc_result = ppc_regs.gpr[3];
2085         if (ppc_regs.ccr & SO_MASK)
2086                 ppc_result = -ppc_result;
2087 #elif defined(AVR32)
2088         /* already done by get_regs */
2089 #elif defined(BFIN)
2090         if (upeek(tcp->pid, PT_R0, &bfin_r0) < 0)
2091                 return -1;
2092 #elif defined(I386)
2093         /* already done by get_regs */
2094 #elif defined(X86_64) || defined(X32)
2095         /* already done by get_regs */
2096 #elif defined(IA64)
2097 #       define IA64_PSR_IS      ((long)1 << 34)
2098         long psr;
2099         if (upeek(tcp->pid, PT_CR_IPSR, &psr) >= 0)
2100                 ia32 = (psr & IA64_PSR_IS) != 0;
2101         if (upeek(tcp->pid, PT_R8, &ia64_r8) < 0)
2102                 return -1;
2103         if (upeek(tcp->pid, PT_R10, &ia64_r10) < 0)
2104                 return -1;
2105 #elif defined(ARM)
2106         /* already done by get_regs */
2107 #elif defined(AARCH64)
2108         /* register reading already done by get_regs */
2109
2110         /* Used to do this, but we did it on syscall entry already: */
2111         /* We are in 64-bit mode (personality 1) if register struct is aarch64_regs,
2112          * else it's personality 0.
2113          */
2114         /*update_personality(tcp, aarch64_io.iov_len == sizeof(aarch64_regs));*/
2115 #elif defined(M68K)
2116         if (upeek(tcp->pid, 4*PT_D0, &m68k_d0) < 0)
2117                 return -1;
2118 #elif defined(LINUX_MIPSN32)
2119         unsigned long long regs[38];
2120
2121         if (ptrace(PTRACE_GETREGS, tcp->pid, NULL, (long) &regs) < 0)
2122                 return -1;
2123         mips_a3 = regs[REG_A3];
2124         mips_r2 = regs[REG_V0];
2125 #elif defined(MIPS)
2126         if (upeek(tcp->pid, REG_A3, &mips_a3) < 0)
2127                 return -1;
2128         if (upeek(tcp->pid, REG_V0, &mips_r2) < 0)
2129                 return -1;
2130 #elif defined(ALPHA)
2131         if (upeek(tcp->pid, REG_A3, &alpha_a3) < 0)
2132                 return -1;
2133         if (upeek(tcp->pid, REG_R0, &alpha_r0) < 0)
2134                 return -1;
2135 #elif defined(SPARC) || defined(SPARC64)
2136         /* already done by get_regs */
2137 #elif defined(HPPA)
2138         if (upeek(tcp->pid, PT_GR28, &hppa_r28) < 0)
2139                 return -1;
2140 #elif defined(SH)
2141         /* new syscall ABI returns result in R0 */
2142         if (upeek(tcp->pid, 4*REG_REG0, (long *)&sh_r0) < 0)
2143                 return -1;
2144 #elif defined(SH64)
2145         /* ABI defines result returned in r9 */
2146         if (upeek(tcp->pid, REG_GENERAL(9), (long *)&sh64_r9) < 0)
2147                 return -1;
2148 #elif defined(CRISV10) || defined(CRISV32)
2149         if (upeek(tcp->pid, 4*PT_R10, &cris_r10) < 0)
2150                 return -1;
2151 #elif defined(TILE)
2152         /* already done by get_regs */
2153 #elif defined(MICROBLAZE)
2154         if (upeek(tcp->pid, 3 * 4, &microblaze_r3) < 0)
2155                 return -1;
2156 #elif defined(OR1K)
2157         /* already done by get_regs */
2158 #elif defined(METAG)
2159         /* already done by get_regs */
2160 #elif defined(XTENSA)
2161         if (upeek(tcp->pid, REG_A_BASE + 2, &xtensa_a2) < 0)
2162                 return -1;
2163 #endif
2164         return 1;
2165 }
2166
2167 /* Called at each syscall exit */
2168 static void
2169 syscall_fixup_on_sysexit(struct tcb *tcp)
2170 {
2171 #if defined(S390) || defined(S390X)
2172         if (syscall_mode != -ENOSYS)
2173                 syscall_mode = tcp->scno;
2174         if ((tcp->flags & TCB_WAITEXECVE)
2175                  && (gpr2 == -ENOSYS || gpr2 == tcp->scno)) {
2176                 /*
2177                  * Return from execve.
2178                  * Fake a return value of zero.  We leave the TCB_WAITEXECVE
2179                  * flag set for the post-execve SIGTRAP to see and reset.
2180                  */
2181                 gpr2 = 0;
2182         }
2183 #endif
2184 }
2185
2186 /*
2187  * Check the syscall return value register value for whether it is
2188  * a negated errno code indicating an error, or a success return value.
2189  */
2190 static inline int
2191 is_negated_errno(unsigned long int val)
2192 {
2193         unsigned long int max = -(long int) nerrnos;
2194 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
2195         if (current_wordsize < sizeof(val)) {
2196                 val = (unsigned int) val;
2197                 max = (unsigned int) max;
2198         }
2199 #endif
2200         return val > max;
2201 }
2202
2203 #if defined(X32)
2204 static inline int
2205 is_negated_errno_x32(unsigned long long val)
2206 {
2207         unsigned long long max = -(long long) nerrnos;
2208         /*
2209          * current_wordsize is 4 even in personality 0 (native X32)
2210          * but truncation _must not_ be done in it.
2211          * can't check current_wordsize here!
2212          */
2213         if (current_personality != 0) {
2214                 val = (uint32_t) val;
2215                 max = (uint32_t) max;
2216         }
2217         return val > max;
2218 }
2219 #endif
2220
2221 /* Returns:
2222  * 1: ok, continue in trace_syscall_exiting().
2223  * -1: error, trace_syscall_exiting() should print error indicator
2224  *    ("????" etc) and bail out.
2225  */
2226 static void
2227 get_error(struct tcb *tcp)
2228 {
2229         int u_error = 0;
2230         int check_errno = 1;
2231         if (tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS) {
2232                 check_errno = 0;
2233         }
2234 #if defined(S390) || defined(S390X)
2235         if (check_errno && is_negated_errno(gpr2)) {
2236                 tcp->u_rval = -1;
2237                 u_error = -gpr2;
2238         }
2239         else {
2240                 tcp->u_rval = gpr2;
2241         }
2242 #elif defined(I386)
2243         if (check_errno && is_negated_errno(i386_regs.eax)) {
2244                 tcp->u_rval = -1;
2245                 u_error = -i386_regs.eax;
2246         }
2247         else {
2248                 tcp->u_rval = i386_regs.eax;
2249         }
2250 #elif defined(X86_64)
2251         long rax;
2252         if (x86_io.iov_len == sizeof(i386_regs)) {
2253                 /* Sign extend from 32 bits */
2254                 rax = (int32_t)i386_regs.eax;
2255         } else {
2256                 rax = x86_64_regs.rax;
2257         }
2258         if (check_errno && is_negated_errno(rax)) {
2259                 tcp->u_rval = -1;
2260                 u_error = -rax;
2261         }
2262         else {
2263                 tcp->u_rval = rax;
2264         }
2265 #elif defined(X32)
2266         /* In X32, return value is 64-bit (llseek uses one).
2267          * Using merely "long rax" would not work.
2268          */
2269         long long rax;
2270         if (x86_io.iov_len == sizeof(i386_regs)) {
2271                 /* Sign extend from 32 bits */
2272                 rax = (int32_t)i386_regs.eax;
2273         } else {
2274                 rax = x86_64_regs.rax;
2275         }
2276         /* Careful: is_negated_errno() works only on longs */
2277         if (check_errno && is_negated_errno_x32(rax)) {
2278                 tcp->u_rval = -1;
2279                 u_error = -rax;
2280         }
2281         else {
2282                 tcp->u_rval = rax; /* truncating */
2283                 tcp->u_lrval = rax;
2284         }
2285 #elif defined(IA64)
2286         if (ia32) {
2287                 int err;
2288
2289                 err = (int)ia64_r8;
2290                 if (check_errno && is_negated_errno(err)) {
2291                         tcp->u_rval = -1;
2292                         u_error = -err;
2293                 }
2294                 else {
2295                         tcp->u_rval = err;
2296                 }
2297         } else {
2298                 if (check_errno && ia64_r10) {
2299                         tcp->u_rval = -1;
2300                         u_error = ia64_r8;
2301                 } else {
2302                         tcp->u_rval = ia64_r8;
2303                 }
2304         }
2305 #elif defined(MIPS)
2306         if (check_errno && mips_a3) {
2307                 tcp->u_rval = -1;
2308                 u_error = mips_r2;
2309         } else {
2310                 tcp->u_rval = mips_r2;
2311 # if defined(LINUX_MIPSN32)
2312                 tcp->u_lrval = mips_r2;
2313 # endif
2314         }
2315 #elif defined(POWERPC)
2316         if (check_errno && is_negated_errno(ppc_result)) {
2317                 tcp->u_rval = -1;
2318                 u_error = -ppc_result;
2319         }
2320         else {
2321                 tcp->u_rval = ppc_result;
2322         }
2323 #elif defined(M68K)
2324         if (check_errno && is_negated_errno(m68k_d0)) {
2325                 tcp->u_rval = -1;
2326                 u_error = -m68k_d0;
2327         }
2328         else {
2329                 tcp->u_rval = m68k_d0;
2330         }
2331 #elif defined(ARM) || defined(AARCH64)
2332 # if defined(AARCH64)
2333         if (tcp->currpers == 1) {
2334                 if (check_errno && is_negated_errno(aarch64_regs.regs[0])) {
2335                         tcp->u_rval = -1;
2336                         u_error = -aarch64_regs.regs[0];
2337                 }
2338                 else {
2339                         tcp->u_rval = aarch64_regs.regs[0];
2340                 }
2341         }
2342         else
2343 # endif
2344         {
2345                 if (check_errno && is_negated_errno(arm_regs.ARM_r0)) {
2346                         tcp->u_rval = -1;
2347                         u_error = -arm_regs.ARM_r0;
2348                 }
2349                 else {
2350                         tcp->u_rval = arm_regs.ARM_r0;
2351                 }
2352         }
2353 #elif defined(AVR32)
2354         if (check_errno && avr32_regs.r12 && (unsigned) -avr32_regs.r12 < nerrnos) {
2355                 tcp->u_rval = -1;
2356                 u_error = -avr32_regs.r12;
2357         }
2358         else {
2359                 tcp->u_rval = avr32_regs.r12;
2360         }
2361 #elif defined(BFIN)
2362         if (check_errno && is_negated_errno(bfin_r0)) {
2363                 tcp->u_rval = -1;
2364                 u_error = -bfin_r0;
2365         } else {
2366                 tcp->u_rval = bfin_r0;
2367         }
2368 #elif defined(ALPHA)
2369         if (check_errno && alpha_a3) {
2370                 tcp->u_rval = -1;
2371                 u_error = alpha_r0;
2372         }
2373         else {
2374                 tcp->u_rval = alpha_r0;
2375         }
2376 #elif defined(SPARC)
2377         if (check_errno && sparc_regs.psr & PSR_C) {
2378                 tcp->u_rval = -1;
2379                 u_error = sparc_regs.u_regs[U_REG_O0];
2380         }
2381         else {
2382                 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
2383         }
2384 #elif defined(SPARC64)
2385         if (check_errno && sparc_regs.tstate & 0x1100000000UL) {
2386                 tcp->u_rval = -1;
2387                 u_error = sparc_regs.u_regs[U_REG_O0];
2388         }
2389         else {
2390                 tcp->u_rval = sparc_regs.u_regs[U_REG_O0];
2391         }
2392 #elif defined(HPPA)
2393         if (check_errno && is_negated_errno(hppa_r28)) {
2394                 tcp->u_rval = -1;
2395                 u_error = -hppa_r28;
2396         }
2397         else {
2398                 tcp->u_rval = hppa_r28;
2399         }
2400 #elif defined(SH)
2401         if (check_errno && is_negated_errno(sh_r0)) {
2402                 tcp->u_rval = -1;
2403                 u_error = -sh_r0;
2404         }
2405         else {
2406                 tcp->u_rval = sh_r0;
2407         }
2408 #elif defined(SH64)
2409         if (check_errno && is_negated_errno(sh64_r9)) {
2410                 tcp->u_rval = -1;
2411                 u_error = -sh64_r9;
2412         }
2413         else {
2414                 tcp->u_rval = sh64_r9;
2415         }
2416 #elif defined(METAG)
2417         /* result pointer in D0Re0 (D0.0) */
2418         if (check_errno && is_negated_errno(metag_regs.dx[0][0])) {
2419                 tcp->u_rval = -1;
2420                 u_error = -metag_regs.dx[0][0];
2421         }
2422         else {
2423                 tcp->u_rval = metag_regs.dx[0][0];
2424         }
2425 #elif defined(CRISV10) || defined(CRISV32)
2426         if (check_errno && cris_r10 && (unsigned) -cris_r10 < nerrnos) {
2427                 tcp->u_rval = -1;
2428                 u_error = -cris_r10;
2429         }
2430         else {
2431                 tcp->u_rval = cris_r10;
2432         }
2433 #elif defined(TILE)
2434         /*
2435          * The standard tile calling convention returns the value (or negative
2436          * errno) in r0, and zero (or positive errno) in r1.
2437          * Until at least kernel 3.8, however, the r1 value is not reflected
2438          * in ptregs at this point, so we use r0 here.
2439          */
2440         if (check_errno && is_negated_errno(tile_regs.regs[0])) {
2441                 tcp->u_rval = -1;
2442                 u_error = -tile_regs.regs[0];
2443         } else {
2444                 tcp->u_rval = tile_regs.regs[0];
2445         }
2446 #elif defined(MICROBLAZE)
2447         if (check_errno && is_negated_errno(microblaze_r3)) {
2448                 tcp->u_rval = -1;
2449                 u_error = -microblaze_r3;
2450         }
2451         else {
2452                 tcp->u_rval = microblaze_r3;
2453         }
2454 #elif defined(OR1K)
2455         if (check_errno && is_negated_errno(or1k_regs.gpr[11])) {
2456                 tcp->u_rval = -1;
2457                 u_error = -or1k_regs.gpr[11];
2458         }
2459         else {
2460                 tcp->u_rval = or1k_regs.gpr[11];
2461         }
2462 #elif defined(XTENSA)
2463         if (check_errno && is_negated_errno(xtensa_a2)) {
2464                 tcp->u_rval = -1;
2465                 u_error = -xtensa_a2;
2466         }
2467         else {
2468                 tcp->u_rval = xtensa_a2;
2469         }
2470 #endif
2471         tcp->u_error = u_error;
2472 }
2473
2474 static void
2475 dumpio(struct tcb *tcp)
2476 {
2477         int (*func)();
2478
2479         if (syserror(tcp))
2480                 return;
2481         if ((unsigned long) tcp->u_arg[0] >= num_quals)
2482                 return;
2483         func = tcp->s_ent->sys_func;
2484         if (func == printargs)
2485                 return;
2486         if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
2487                 if (func == sys_read ||
2488                     func == sys_pread ||
2489                     func == sys_recv ||
2490                     func == sys_recvfrom)
2491                         dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
2492                 else if (func == sys_readv)
2493                         dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2494                 return;
2495         }
2496         if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
2497                 if (func == sys_write ||
2498                     func == sys_pwrite ||
2499                     func == sys_send ||
2500                     func == sys_sendto)
2501                         dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
2502                 else if (func == sys_writev)
2503                         dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
2504                 return;
2505         }
2506 }
2507
2508 static int
2509 trace_syscall_exiting(struct tcb *tcp)
2510 {
2511         int sys_res;
2512         struct timeval tv;
2513         int res;
2514         long u_error;
2515
2516         /* Measure the exit time as early as possible to avoid errors. */
2517         if (Tflag || cflag)
2518                 gettimeofday(&tv, NULL);
2519
2520 #if SUPPORTED_PERSONALITIES > 1
2521         update_personality(tcp, tcp->currpers);
2522 #endif
2523         res = (get_regs_error ? -1 : get_syscall_result(tcp));
2524         if (res == 1) {
2525                 syscall_fixup_on_sysexit(tcp); /* never fails */
2526                 get_error(tcp); /* never fails */
2527                 if (need_fork_exec_workarounds)
2528                         syscall_fixup_for_fork_exec(tcp);
2529                 if (filtered(tcp) || hide_log_until_execve)
2530                         goto ret;
2531         }
2532
2533         if (cflag) {
2534                 struct timeval t = tv;
2535                 count_syscall(tcp, &t);
2536                 if (cflag == CFLAG_ONLY_STATS) {
2537                         goto ret;
2538                 }
2539         }
2540
2541         /* If not in -ff mode, and printing_tcp != tcp,
2542          * then the log currently does not end with output
2543          * of _our syscall entry_, but with something else.
2544          * We need to say which syscall's return is this.
2545          *
2546          * Forced reprinting via TCB_REPRINT is used only by
2547          * "strace -ff -oLOG test/threaded_execve" corner case.
2548          * It's the only case when -ff mode needs reprinting.
2549          */
2550         if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
2551                 tcp->flags &= ~TCB_REPRINT;
2552                 printleader(tcp);
2553                 if (tcp->qual_flg & UNDEFINED_SCNO)
2554                         tprintf("<... %s resumed> ", undefined_scno_name(tcp));
2555                 else
2556                         tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
2557         }
2558         printing_tcp = tcp;
2559
2560         if (res != 1) {
2561                 /* There was error in one of prior ptrace ops */
2562                 tprints(") ");
2563                 tabto();
2564                 tprints("= ? <unavailable>\n");
2565                 line_ended();
2566                 tcp->flags &= ~TCB_INSYSCALL;
2567                 return res;
2568         }
2569
2570         sys_res = 0;
2571         if (tcp->qual_flg & QUAL_RAW) {
2572                 /* sys_res = printargs(tcp); - but it's nop on sysexit */
2573         } else {
2574         /* FIXME: not_failing_only (IOW, option -z) is broken:
2575          * failure of syscall is known only after syscall return.
2576          * Thus we end up with something like this on, say, ENOENT:
2577          *     open("doesnt_exist", O_RDONLY <unfinished ...>
2578          *     {next syscall decode}
2579          * whereas the intended result is that open(...) line
2580          * is not shown at all.
2581          */
2582                 if (not_failing_only && tcp->u_error)
2583                         goto ret;       /* ignore failed syscalls */
2584                 sys_res = tcp->s_ent->sys_func(tcp);
2585         }
2586
2587         tprints(") ");
2588         tabto();
2589         u_error = tcp->u_error;
2590         if (tcp->qual_flg & QUAL_RAW) {
2591                 if (u_error)
2592                         tprintf("= -1 (errno %ld)", u_error);
2593                 else
2594                         tprintf("= %#lx", tcp->u_rval);
2595         }
2596         else if (!(sys_res & RVAL_NONE) && u_error) {
2597                 switch (u_error) {
2598                 /* Blocked signals do not interrupt any syscalls.
2599                  * In this case syscalls don't return ERESTARTfoo codes.
2600                  *
2601                  * Deadly signals set to SIG_DFL interrupt syscalls
2602                  * and kill the process regardless of which of the codes below
2603                  * is returned by the interrupted syscall.
2604                  * In some cases, kernel forces a kernel-generated deadly
2605                  * signal to be unblocked and set to SIG_DFL (and thus cause
2606                  * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
2607                  * or SIGILL. (The alternative is to leave process spinning
2608                  * forever on the faulty instruction - not useful).
2609                  *
2610                  * SIG_IGNed signals and non-deadly signals set to SIG_DFL
2611                  * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
2612                  * but kernel will always restart them.
2613                  */
2614                 case ERESTARTSYS:
2615                         /* Most common type of signal-interrupted syscall exit code.
2616                          * The system call will be restarted with the same arguments
2617                          * if SA_RESTART is set; otherwise, it will fail with EINTR.
2618                          */
2619                         tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
2620                         break;
2621                 case ERESTARTNOINTR:
2622                         /* Rare. For example, fork() returns this if interrupted.
2623                          * SA_RESTART is ignored (assumed set): the restart is unconditional.
2624                          */
2625                         tprints("= ? ERESTARTNOINTR (To be restarted)");
2626                         break;
2627                 case ERESTARTNOHAND:
2628                         /* pause(), rt_sigsuspend() etc use this code.
2629                          * SA_RESTART is ignored (assumed not set):
2630                          * syscall won't restart (will return EINTR instead)
2631                          * even after signal with SA_RESTART set. However,
2632                          * after SIG_IGN or SIG_DFL signal it will restart
2633                          * (thus the name "restart only if has no handler").
2634                          */
2635                         tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
2636                         break;
2637                 case ERESTART_RESTARTBLOCK:
2638                         /* Syscalls like nanosleep(), poll() which can't be
2639                          * restarted with their original arguments use this
2640                          * code. Kernel will execute restart_syscall() instead,
2641                          * which changes arguments before restarting syscall.
2642                          * SA_RESTART is ignored (assumed not set) similarly
2643                          * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
2644                          * since restart data is saved in "restart block"
2645                          * in task struct, and if signal handler uses a syscall
2646                          * which in turn saves another such restart block,
2647                          * old data is lost and restart becomes impossible)
2648                          */
2649                         tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
2650                         break;
2651                 default:
2652                         if (u_error < 0)
2653                                 tprintf("= -1 E??? (errno %ld)", u_error);
2654                         else if (u_error < nerrnos)
2655                                 tprintf("= -1 %s (%s)", errnoent[u_error],
2656                                         strerror(u_error));
2657                         else
2658                                 tprintf("= -1 ERRNO_%ld (%s)", u_error,
2659                                         strerror(u_error));
2660                         break;
2661                 }
2662                 if ((sys_res & RVAL_STR) && tcp->auxstr)
2663                         tprintf(" (%s)", tcp->auxstr);
2664         }
2665         else {
2666                 if (sys_res & RVAL_NONE)
2667                         tprints("= ?");
2668                 else {
2669                         switch (sys_res & RVAL_MASK) {
2670                         case RVAL_HEX:
2671                                 tprintf("= %#lx", tcp->u_rval);
2672                                 break;
2673                         case RVAL_OCTAL:
2674                                 tprintf("= %#lo", tcp->u_rval);
2675                                 break;
2676                         case RVAL_UDECIMAL:
2677                                 tprintf("= %lu", tcp->u_rval);
2678                                 break;
2679                         case RVAL_DECIMAL:
2680                                 tprintf("= %ld", tcp->u_rval);
2681                                 break;
2682 #if defined(LINUX_MIPSN32) || defined(X32)
2683                         /*
2684                         case RVAL_LHEX:
2685                                 tprintf("= %#llx", tcp->u_lrval);
2686                                 break;
2687                         case RVAL_LOCTAL:
2688                                 tprintf("= %#llo", tcp->u_lrval);
2689                                 break;
2690                         */
2691                         case RVAL_LUDECIMAL:
2692                                 tprintf("= %llu", tcp->u_lrval);
2693                                 break;
2694                         /*
2695                         case RVAL_LDECIMAL:
2696                                 tprintf("= %lld", tcp->u_lrval);
2697                                 break;
2698                         */
2699 #endif
2700                         default:
2701                                 fprintf(stderr,
2702                                         "invalid rval format\n");
2703                                 break;
2704                         }
2705                 }
2706                 if ((sys_res & RVAL_STR) && tcp->auxstr)
2707                         tprintf(" (%s)", tcp->auxstr);
2708         }
2709         if (Tflag) {
2710                 tv_sub(&tv, &tv, &tcp->etime);
2711                 tprintf(" <%ld.%06ld>",
2712                         (long) tv.tv_sec, (long) tv.tv_usec);
2713         }
2714         tprints("\n");
2715         dumpio(tcp);
2716         line_ended();
2717
2718  ret:
2719         tcp->flags &= ~TCB_INSYSCALL;
2720         return 0;
2721 }
2722
2723 int
2724 trace_syscall(struct tcb *tcp)
2725 {
2726         return exiting(tcp) ?
2727                 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
2728 }