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