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