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