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