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