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