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