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