]> granicus.if.org Git - strace/blob - syscall.c
Add private data field to struct tcb
[strace] / syscall.c
1 /*
2  * Copyright (c) 1991, 1992 Paul Kranenburg <pk@cs.few.eur.nl>
3  * Copyright (c) 1993 Branko Lankester <branko@hacktic.nl>
4  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
5  * Copyright (c) 1996-1999 Wichert Akkerman <wichert@cistron.nl>
6  * Copyright (c) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
7  *                     Linux for s390 port by D.J. Barrow
8  *                    <barrow_dj@mail.yahoo.com,djbarrow@de.ibm.com>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include "defs.h"
35 #include "native_defs.h"
36 #include <sys/param.h>
37 #include <signal.h>
38
39 /* for struct iovec */
40 #include <sys/uio.h>
41
42 #include "regs.h"
43 #include "ptrace.h"
44
45 #if defined(SPARC64)
46 # undef PTRACE_GETREGS
47 # define PTRACE_GETREGS PTRACE_GETREGS64
48 # undef PTRACE_SETREGS
49 # define PTRACE_SETREGS PTRACE_SETREGS64
50 #endif
51
52 #if defined SPARC64
53 # include <asm/psrcompat.h>
54 #elif defined SPARC
55 # include <asm/psr.h>
56 #endif
57
58 #ifdef IA64
59 # include <asm/rse.h>
60 #endif
61
62 #ifndef NT_PRSTATUS
63 # define NT_PRSTATUS 1
64 #endif
65
66 #ifndef NSIG
67 # warning: NSIG is not defined, using 32
68 # define NSIG 32
69 #endif
70
71 #include "syscall.h"
72
73 /* Define these shorthand notations to simplify the syscallent files. */
74 #define TD TRACE_DESC
75 #define TF TRACE_FILE
76 #define TI TRACE_IPC
77 #define TN TRACE_NETWORK
78 #define TP TRACE_PROCESS
79 #define TS TRACE_SIGNAL
80 #define TM TRACE_MEMORY
81 #define NF SYSCALL_NEVER_FAILS
82 #define MA MAX_ARGS
83 #define SI STACKTRACE_INVALIDATE_CACHE
84 #define SE STACKTRACE_CAPTURE_ON_ENTER
85
86 #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
87
88 const struct_sysent sysent0[] = {
89 #include "syscallent.h"
90 };
91
92 #if SUPPORTED_PERSONALITIES > 1
93 # include PERSONALITY1_INCLUDE_FUNCS
94 static const struct_sysent sysent1[] = {
95 # include "syscallent1.h"
96 };
97 #endif
98
99 #if SUPPORTED_PERSONALITIES > 2
100 # include PERSONALITY2_INCLUDE_FUNCS
101 static const struct_sysent sysent2[] = {
102 # include "syscallent2.h"
103 };
104 #endif
105
106 /* Now undef them since short defines cause wicked namespace pollution. */
107 #undef SEN
108 #undef TD
109 #undef TF
110 #undef TI
111 #undef TN
112 #undef TP
113 #undef TS
114 #undef TM
115 #undef NF
116 #undef MA
117 #undef SI
118 #undef SE
119
120 /*
121  * `ioctlent[012].h' files are automatically generated by the auxiliary
122  * program `ioctlsort', such that the list is sorted by the `code' field.
123  * This has the side-effect of resolving the _IO.. macros into
124  * plain integers, eliminating the need to include here everything
125  * in "/usr/include".
126  */
127
128 const char *const errnoent0[] = {
129 #include "errnoent.h"
130 };
131 const char *const signalent0[] = {
132 #include "signalent.h"
133 };
134 const struct_ioctlent ioctlent0[] = {
135 #include "ioctlent0.h"
136 };
137
138 #if SUPPORTED_PERSONALITIES > 1
139 static const char *const errnoent1[] = {
140 # include "errnoent1.h"
141 };
142 static const char *const signalent1[] = {
143 # include "signalent1.h"
144 };
145 static const struct_ioctlent ioctlent1[] = {
146 # include "ioctlent1.h"
147 };
148 # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
149 static const struct_printers printers0 = {
150 # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
151 };
152 # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
153 static const struct_printers printers1 = {
154 # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
155 };
156 #endif
157
158 #if SUPPORTED_PERSONALITIES > 2
159 static const char *const errnoent2[] = {
160 # include "errnoent2.h"
161 };
162 static const char *const signalent2[] = {
163 # include "signalent2.h"
164 };
165 static const struct_ioctlent ioctlent2[] = {
166 # include "ioctlent2.h"
167 };
168 # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
169 static const struct_printers printers2 = {
170 # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
171 };
172 #endif
173
174 enum {
175         nsyscalls0 = ARRAY_SIZE(sysent0)
176 #if SUPPORTED_PERSONALITIES > 1
177         , nsyscalls1 = ARRAY_SIZE(sysent1)
178 # if SUPPORTED_PERSONALITIES > 2
179         , nsyscalls2 = ARRAY_SIZE(sysent2)
180 # endif
181 #endif
182 };
183
184 enum {
185         nerrnos0 = ARRAY_SIZE(errnoent0)
186 #if SUPPORTED_PERSONALITIES > 1
187         , nerrnos1 = ARRAY_SIZE(errnoent1)
188 # if SUPPORTED_PERSONALITIES > 2
189         , nerrnos2 = ARRAY_SIZE(errnoent2)
190 # endif
191 #endif
192 };
193
194 enum {
195         nsignals0 = ARRAY_SIZE(signalent0)
196 #if SUPPORTED_PERSONALITIES > 1
197         , nsignals1 = ARRAY_SIZE(signalent1)
198 # if SUPPORTED_PERSONALITIES > 2
199         , nsignals2 = ARRAY_SIZE(signalent2)
200 # endif
201 #endif
202 };
203
204 enum {
205         nioctlents0 = ARRAY_SIZE(ioctlent0)
206 #if SUPPORTED_PERSONALITIES > 1
207         , nioctlents1 = ARRAY_SIZE(ioctlent1)
208 # if SUPPORTED_PERSONALITIES > 2
209         , nioctlents2 = ARRAY_SIZE(ioctlent2)
210 # endif
211 #endif
212 };
213
214 #if SUPPORTED_PERSONALITIES > 1
215 const struct_sysent *sysent = sysent0;
216 const char *const *errnoent = errnoent0;
217 const char *const *signalent = signalent0;
218 const struct_ioctlent *ioctlent = ioctlent0;
219 const struct_printers *printers = &printers0;
220 #endif
221
222 unsigned nsyscalls = nsyscalls0;
223 unsigned nerrnos = nerrnos0;
224 unsigned nsignals = nsignals0;
225 unsigned nioctlents = nioctlents0;
226
227 unsigned num_quals;
228 qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
229
230 static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
231         nsyscalls0,
232 #if SUPPORTED_PERSONALITIES > 1
233         nsyscalls1,
234 #endif
235 #if SUPPORTED_PERSONALITIES > 2
236         nsyscalls2,
237 #endif
238 };
239 static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
240         sysent0,
241 #if SUPPORTED_PERSONALITIES > 1
242         sysent1,
243 #endif
244 #if SUPPORTED_PERSONALITIES > 2
245         sysent2,
246 #endif
247 };
248
249 enum {
250         MAX_NSYSCALLS1 = (nsyscalls0
251 #if SUPPORTED_PERSONALITIES > 1
252                         > nsyscalls1 ? nsyscalls0 : nsyscalls1
253 #endif
254                         ),
255         MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
256 #if SUPPORTED_PERSONALITIES > 2
257                         > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
258 #endif
259                         ),
260         MAX_NSYSCALLS = MAX_NSYSCALLS2,
261         /* We are ready for arches with up to 255 signals,
262          * even though the largest known signo is on MIPS and it is 128.
263          * The number of existing syscalls on all arches is
264          * larger that 255 anyway, so it is just a pedantic matter.
265          */
266         MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
267 };
268
269 #if SUPPORTED_PERSONALITIES > 1
270 unsigned current_personality;
271
272 # ifndef current_wordsize
273 unsigned current_wordsize;
274 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
275         PERSONALITY0_WORDSIZE,
276         PERSONALITY1_WORDSIZE,
277 # if SUPPORTED_PERSONALITIES > 2
278         PERSONALITY2_WORDSIZE,
279 # endif
280 };
281 # endif
282
283 void
284 set_personality(int personality)
285 {
286         nsyscalls = nsyscall_vec[personality];
287         sysent = sysent_vec[personality];
288
289         switch (personality) {
290         case 0:
291                 errnoent = errnoent0;
292                 nerrnos = nerrnos0;
293                 ioctlent = ioctlent0;
294                 nioctlents = nioctlents0;
295                 signalent = signalent0;
296                 nsignals = nsignals0;
297                 printers = &printers0;
298                 break;
299
300         case 1:
301                 errnoent = errnoent1;
302                 nerrnos = nerrnos1;
303                 ioctlent = ioctlent1;
304                 nioctlents = nioctlents1;
305                 signalent = signalent1;
306                 nsignals = nsignals1;
307                 printers = &printers1;
308                 break;
309
310 # if SUPPORTED_PERSONALITIES > 2
311         case 2:
312                 errnoent = errnoent2;
313                 nerrnos = nerrnos2;
314                 ioctlent = ioctlent2;
315                 nioctlents = nioctlents2;
316                 signalent = signalent2;
317                 nsignals = nsignals2;
318                 printers = &printers2;
319                 break;
320 # endif
321         }
322
323         current_personality = personality;
324 # ifndef current_wordsize
325         current_wordsize = personality_wordsize[personality];
326 # endif
327 }
328
329 static void
330 update_personality(struct tcb *tcp, unsigned int personality)
331 {
332         if (personality == current_personality)
333                 return;
334         set_personality(personality);
335
336         if (personality == tcp->currpers)
337                 return;
338         tcp->currpers = personality;
339
340 # undef PERSONALITY_NAMES
341 # if defined POWERPC64
342 #  define PERSONALITY_NAMES {"64 bit", "32 bit"}
343 # elif defined X86_64
344 #  define PERSONALITY_NAMES {"64 bit", "32 bit", "x32"}
345 # elif defined X32
346 #  define PERSONALITY_NAMES {"x32", "32 bit"}
347 # elif defined AARCH64
348 #  define PERSONALITY_NAMES {"64 bit", "32 bit"}
349 # elif defined TILE
350 #  define PERSONALITY_NAMES {"64-bit", "32-bit"}
351 # endif
352 # ifdef PERSONALITY_NAMES
353         if (!qflag) {
354                 static const char *const names[] = PERSONALITY_NAMES;
355                 error_msg("[ Process PID=%d runs in %s mode. ]",
356                           tcp->pid, names[personality]);
357         }
358 # endif
359 }
360 #endif
361
362 static int qual_syscall(), qual_signal(), qual_desc();
363
364 static const struct qual_options {
365         unsigned int bitflag;
366         const char *option_name;
367         int (*qualify)(const char *, int, int);
368         const char *argument_name;
369 } qual_options[] = {
370         { QUAL_TRACE,   "trace",        qual_syscall,   "system call"   },
371         { QUAL_TRACE,   "t",            qual_syscall,   "system call"   },
372         { QUAL_ABBREV,  "abbrev",       qual_syscall,   "system call"   },
373         { QUAL_ABBREV,  "a",            qual_syscall,   "system call"   },
374         { QUAL_VERBOSE, "verbose",      qual_syscall,   "system call"   },
375         { QUAL_VERBOSE, "v",            qual_syscall,   "system call"   },
376         { QUAL_RAW,     "raw",          qual_syscall,   "system call"   },
377         { QUAL_RAW,     "x",            qual_syscall,   "system call"   },
378         { QUAL_SIGNAL,  "signal",       qual_signal,    "signal"        },
379         { QUAL_SIGNAL,  "signals",      qual_signal,    "signal"        },
380         { QUAL_SIGNAL,  "s",            qual_signal,    "signal"        },
381         { QUAL_READ,    "read",         qual_desc,      "descriptor"    },
382         { QUAL_READ,    "reads",        qual_desc,      "descriptor"    },
383         { QUAL_READ,    "r",            qual_desc,      "descriptor"    },
384         { QUAL_WRITE,   "write",        qual_desc,      "descriptor"    },
385         { QUAL_WRITE,   "writes",       qual_desc,      "descriptor"    },
386         { QUAL_WRITE,   "w",            qual_desc,      "descriptor"    },
387         { 0,            NULL,           NULL,           NULL            },
388 };
389
390 static void
391 reallocate_qual(const unsigned int n)
392 {
393         unsigned p;
394         qualbits_t *qp;
395         for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
396                 qp = qual_vec[p] = xreallocarray(qual_vec[p], n,
397                                                  sizeof(qualbits_t));
398                 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
399         }
400         num_quals = n;
401 }
402
403 static void
404 qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
405 {
406         int p;
407
408         if (num_quals <= n)
409                 reallocate_qual(n + 1);
410
411         for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
412                 if (pers == p || pers < 0) {
413                         if (not)
414                                 qual_vec[p][n] &= ~bitflag;
415                         else
416                                 qual_vec[p][n] |= bitflag;
417                 }
418         }
419 }
420
421 static int
422 qual_syscall(const char *s, const unsigned int bitflag, const int not)
423 {
424         int p;
425         unsigned int i;
426         int rc = -1;
427
428         if (*s >= '0' && *s <= '9') {
429                 i = string_to_uint(s);
430                 if (i >= MAX_NSYSCALLS)
431                         return -1;
432                 qualify_one(i, bitflag, not, -1);
433                 return 0;
434         }
435
436         for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
437                 for (i = 0; i < nsyscall_vec[p]; i++) {
438                         if (sysent_vec[p][i].sys_name
439                          && strcmp(s, sysent_vec[p][i].sys_name) == 0
440                         ) {
441                                 qualify_one(i, bitflag, not, p);
442                                 rc = 0;
443                         }
444                 }
445         }
446
447         return rc;
448 }
449
450 static int
451 qual_signal(const char *s, const unsigned int bitflag, const int not)
452 {
453         unsigned int i;
454
455         if (*s >= '0' && *s <= '9') {
456                 int signo = string_to_uint(s);
457                 if (signo < 0 || signo > 255)
458                         return -1;
459                 qualify_one(signo, bitflag, not, -1);
460                 return 0;
461         }
462         if (strncasecmp(s, "SIG", 3) == 0)
463                 s += 3;
464         for (i = 0; i <= NSIG; i++) {
465                 if (strcasecmp(s, signame(i) + 3) == 0) {
466                         qualify_one(i, bitflag, not, -1);
467                         return 0;
468                 }
469         }
470         return -1;
471 }
472
473 static int
474 qual_desc(const char *s, const unsigned int bitflag, const int not)
475 {
476         if (*s >= '0' && *s <= '9') {
477                 int desc = string_to_uint(s);
478                 if (desc < 0 || desc > 0x7fff) /* paranoia */
479                         return -1;
480                 qualify_one(desc, bitflag, not, -1);
481                 return 0;
482         }
483         return -1;
484 }
485
486 static int
487 lookup_class(const char *s)
488 {
489         if (strcmp(s, "file") == 0)
490                 return TRACE_FILE;
491         if (strcmp(s, "ipc") == 0)
492                 return TRACE_IPC;
493         if (strcmp(s, "network") == 0)
494                 return TRACE_NETWORK;
495         if (strcmp(s, "process") == 0)
496                 return TRACE_PROCESS;
497         if (strcmp(s, "signal") == 0)
498                 return TRACE_SIGNAL;
499         if (strcmp(s, "desc") == 0)
500                 return TRACE_DESC;
501         if (strcmp(s, "memory") == 0)
502                 return TRACE_MEMORY;
503         return -1;
504 }
505
506 void
507 qualify(const char *s)
508 {
509         const struct qual_options *opt;
510         char *copy;
511         const char *p;
512         int not;
513         unsigned int i;
514
515         if (num_quals == 0)
516                 reallocate_qual(MIN_QUALS);
517
518         opt = &qual_options[0];
519         for (i = 0; (p = qual_options[i].option_name); i++) {
520                 unsigned int len = strlen(p);
521                 if (strncmp(s, p, len) == 0 && s[len] == '=') {
522                         opt = &qual_options[i];
523                         s += len + 1;
524                         break;
525                 }
526         }
527         not = 0;
528         if (*s == '!') {
529                 not = 1;
530                 s++;
531         }
532         if (strcmp(s, "none") == 0) {
533                 not = 1 - not;
534                 s = "all";
535         }
536         if (strcmp(s, "all") == 0) {
537                 for (i = 0; i < num_quals; i++) {
538                         qualify_one(i, opt->bitflag, not, -1);
539                 }
540                 return;
541         }
542         for (i = 0; i < num_quals; i++) {
543                 qualify_one(i, opt->bitflag, !not, -1);
544         }
545         copy = xstrdup(s);
546         for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
547                 int n;
548                 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
549                         unsigned pers;
550                         for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
551                                 for (i = 0; i < nsyscall_vec[pers]; i++)
552                                         if (sysent_vec[pers][i].sys_flags & n)
553                                                 qualify_one(i, opt->bitflag, not, pers);
554                         }
555                         continue;
556                 }
557                 if (opt->qualify(p, opt->bitflag, not)) {
558                         error_msg_and_die("invalid %s '%s'",
559                                 opt->argument_name, p);
560                 }
561         }
562         free(copy);
563         return;
564 }
565
566 #ifdef SYS_socket_subcall
567 static void
568 decode_socket_subcall(struct tcb *tcp)
569 {
570         unsigned long addr;
571         unsigned int n;
572
573         if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
574                 return;
575
576         tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
577         tcp->qual_flg = qual_flags[tcp->scno];
578         tcp->s_ent = &sysent[tcp->scno];
579         addr = tcp->u_arg[1];
580         n = tcp->s_ent->nargs;
581         if (sizeof(tcp->u_arg[0]) == current_wordsize) {
582                 memset(tcp->u_arg, 0, n * sizeof(tcp->u_arg[0]));
583                 (void) umoven(tcp, addr, n * sizeof(tcp->u_arg[0]), tcp->u_arg);
584         } else {
585                 unsigned int args[n];
586                 unsigned int i;
587
588                 memset(args, 0, sizeof(args));
589                 (void) umove(tcp, addr, &args);
590                 for (i = 0; i < n; ++i)
591                         tcp->u_arg[i] = args[i];
592         }
593 }
594 #endif
595
596 #ifdef SYS_ipc_subcall
597 static void
598 decode_ipc_subcall(struct tcb *tcp)
599 {
600         unsigned int i, n;
601
602         if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
603                 return;
604
605         tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
606         tcp->qual_flg = qual_flags[tcp->scno];
607         tcp->s_ent = &sysent[tcp->scno];
608         n = tcp->s_ent->nargs;
609         for (i = 0; i < n; i++)
610                 tcp->u_arg[i] = tcp->u_arg[i + 1];
611 }
612 #endif
613
614 #ifdef LINUX_MIPSO32
615 static void
616 decode_mips_subcall(struct tcb *tcp)
617 {
618         if (!SCNO_IS_VALID(tcp->u_arg[0]))
619                 return;
620         tcp->scno = tcp->u_arg[0];
621         tcp->qual_flg = qual_flags[tcp->scno];
622         tcp->s_ent = &sysent[tcp->scno];
623         memmove(&tcp->u_arg[0], &tcp->u_arg[1],
624                 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
625         /*
626          * Fetching the last arg of 7-arg syscalls (fadvise64_64
627          * and sync_file_range) would require additional code,
628          * see linux/mips/get_syscall_args.c
629          */
630 }
631 #endif
632
633 static void
634 dumpio(struct tcb *tcp)
635 {
636         int sen;
637
638         if (syserror(tcp))
639                 return;
640         if ((unsigned long) tcp->u_arg[0] >= num_quals)
641                 return;
642         sen = tcp->s_ent->sen;
643         if (SEN_printargs == sen)
644                 return;
645         if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
646                 switch (sen) {
647                 case SEN_read:
648                 case SEN_pread:
649                 case SEN_recv:
650                 case SEN_recvfrom:
651                         dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
652                         return;
653                 case SEN_readv:
654                 case SEN_preadv:
655                 case SEN_preadv2:
656                         dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
657                                      tcp->u_rval);
658                         return;
659                 case SEN_recvmsg:
660                         dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
661                         return;
662                 case SEN_recvmmsg:
663                         dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
664                         return;
665                 }
666         }
667         if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
668                 switch (sen) {
669                 case SEN_write:
670                 case SEN_pwrite:
671                 case SEN_send:
672                 case SEN_sendto:
673                         dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
674                         break;
675                 case SEN_writev:
676                 case SEN_pwritev:
677                 case SEN_pwritev2:
678                 case SEN_vmsplice:
679                         dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
680                         break;
681                 case SEN_sendmsg:
682                         dumpiov_in_msghdr(tcp, tcp->u_arg[1],
683                                           (unsigned long) -1L);
684                         break;
685                 case SEN_sendmmsg:
686                         dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
687                         break;
688                 }
689         }
690 }
691
692 /*
693  * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
694  * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
695  */
696 #if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
697 static long
698 shuffle_scno(unsigned long scno)
699 {
700         if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
701                 return scno;
702
703         /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
704         if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
705                 return 0x000ffff0;
706         if (scno == 0x000ffff0)
707                 return ARM_FIRST_SHUFFLED_SYSCALL;
708
709 #define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
710         /*
711          * Is it ARM specific syscall?
712          * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
713          * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
714          */
715         if (scno >= 0x000f0000 &&
716             scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
717                 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
718         }
719         if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
720                 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
721         }
722
723         return scno;
724 }
725 #else
726 # define shuffle_scno(scno) ((long)(scno))
727 #endif
728
729 const char *
730 syscall_name(long scno)
731 {
732         static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
733
734         if (SCNO_IS_VALID(scno))
735                 return sysent[scno].sys_name;
736         else {
737                 sprintf(buf, "syscall_%lu", shuffle_scno(scno));
738                 return buf;
739         }
740 }
741
742 static long get_regs_error;
743
744 void
745 clear_regs(void)
746 {
747         get_regs_error = -1;
748 }
749
750 static int get_syscall_args(struct tcb *);
751 static int get_syscall_result(struct tcb *);
752 static int arch_get_scno(struct tcb *tcp);
753 static void get_error(struct tcb *, const bool);
754 #if defined X86_64 || defined POWERPC
755 static int getregs_old(pid_t);
756 #endif
757
758 static int
759 trace_syscall_entering(struct tcb *tcp)
760 {
761         int res, scno_good;
762
763         scno_good = res = get_scno(tcp);
764         if (res == 0)
765                 return res;
766         if (res == 1)
767                 res = get_syscall_args(tcp);
768
769         if (res != 1) {
770                 printleader(tcp);
771                 if (scno_good != 1)
772                         tprints("????" /* anti-trigraph gap */ "(");
773                 else
774                         tprintf("%s(", syscall_name(tcp->scno));
775                 /*
776                  * " <unavailable>" will be added later by the code which
777                  * detects ptrace errors.
778                  */
779                 goto ret;
780         }
781
782 #ifdef LINUX_MIPSO32
783         if (SEN_syscall == tcp->s_ent->sen)
784                 decode_mips_subcall(tcp);
785 #endif
786
787         if (   SEN_execve == tcp->s_ent->sen
788 # if defined(SPARC) || defined(SPARC64)
789             || SEN_execv == tcp->s_ent->sen
790 # endif
791            ) {
792                 hide_log_until_execve = 0;
793         }
794
795 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
796         switch (tcp->s_ent->sen) {
797 # ifdef SYS_socket_subcall
798                 case SEN_socketcall:
799                         decode_socket_subcall(tcp);
800                         break;
801 # endif
802 # ifdef SYS_ipc_subcall
803                 case SEN_ipc:
804                         decode_ipc_subcall(tcp);
805                         break;
806 # endif
807         }
808 #endif
809
810         if (!(tcp->qual_flg & QUAL_TRACE)
811          || (tracing_paths && !pathtrace_match(tcp))
812         ) {
813                 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
814                 tcp->sys_func_rval = 0;
815                 return 0;
816         }
817
818         tcp->flags &= ~TCB_FILTERED;
819
820         if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
821                 res = 0;
822                 goto ret;
823         }
824
825 #ifdef USE_LIBUNWIND
826         if (stack_trace_enabled) {
827                 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
828                         unwind_capture_stacktrace(tcp);
829         }
830 #endif
831
832         printleader(tcp);
833         tprintf("%s(", syscall_name(tcp->scno));
834         if ((tcp->qual_flg & QUAL_RAW) && SEN_exit != tcp->s_ent->sen)
835                 res = printargs(tcp);
836         else
837                 res = tcp->s_ent->sys_func(tcp);
838
839         fflush(tcp->outf);
840  ret:
841         tcp->flags |= TCB_INSYSCALL;
842         tcp->sys_func_rval = res;
843         /* Measure the entrance time as late as possible to avoid errors. */
844         if (Tflag || cflag)
845                 gettimeofday(&tcp->etime, NULL);
846         return res;
847 }
848
849 static int
850 trace_syscall_exiting(struct tcb *tcp)
851 {
852         int sys_res;
853         struct timeval tv;
854         int res;
855         long u_error;
856
857         /* Measure the exit time as early as possible to avoid errors. */
858         if (Tflag || cflag)
859                 gettimeofday(&tv, NULL);
860
861 #ifdef USE_LIBUNWIND
862         if (stack_trace_enabled) {
863                 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
864                         unwind_cache_invalidate(tcp);
865         }
866 #endif
867
868 #if SUPPORTED_PERSONALITIES > 1
869         update_personality(tcp, tcp->currpers);
870 #endif
871         res = (get_regs_error ? -1 : get_syscall_result(tcp));
872         if (filtered(tcp) || hide_log_until_execve)
873                 goto ret;
874
875         if (cflag) {
876                 count_syscall(tcp, &tv);
877                 if (cflag == CFLAG_ONLY_STATS) {
878                         goto ret;
879                 }
880         }
881
882         /* If not in -ff mode, and printing_tcp != tcp,
883          * then the log currently does not end with output
884          * of _our syscall entry_, but with something else.
885          * We need to say which syscall's return is this.
886          *
887          * Forced reprinting via TCB_REPRINT is used only by
888          * "strace -ff -oLOG test/threaded_execve" corner case.
889          * It's the only case when -ff mode needs reprinting.
890          */
891         if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
892                 tcp->flags &= ~TCB_REPRINT;
893                 printleader(tcp);
894                 tprintf("<... %s resumed> ", syscall_name(tcp->scno));
895         }
896         printing_tcp = tcp;
897
898         tcp->s_prev_ent = NULL;
899         if (res != 1) {
900                 /* There was error in one of prior ptrace ops */
901                 tprints(") ");
902                 tabto();
903                 tprints("= ? <unavailable>\n");
904                 line_ended();
905                 tcp->flags &= ~TCB_INSYSCALL;
906                 tcp->sys_func_rval = 0;
907                 free_tcb_priv_data(tcp);
908                 return res;
909         }
910         tcp->s_prev_ent = tcp->s_ent;
911
912         sys_res = 0;
913         if (tcp->qual_flg & QUAL_RAW) {
914                 /* sys_res = printargs(tcp); - but it's nop on sysexit */
915         } else {
916         /* FIXME: not_failing_only (IOW, option -z) is broken:
917          * failure of syscall is known only after syscall return.
918          * Thus we end up with something like this on, say, ENOENT:
919          *     open("doesnt_exist", O_RDONLY <unfinished ...>
920          *     {next syscall decode}
921          * whereas the intended result is that open(...) line
922          * is not shown at all.
923          */
924                 if (not_failing_only && tcp->u_error)
925                         goto ret;       /* ignore failed syscalls */
926                 if (tcp->sys_func_rval & RVAL_DECODED)
927                         sys_res = tcp->sys_func_rval;
928                 else
929                         sys_res = tcp->s_ent->sys_func(tcp);
930         }
931
932         tprints(") ");
933         tabto();
934         u_error = tcp->u_error;
935         if (tcp->qual_flg & QUAL_RAW) {
936                 if (u_error)
937                         tprintf("= -1 (errno %ld)", u_error);
938                 else
939                         tprintf("= %#lx", tcp->u_rval);
940         }
941         else if (!(sys_res & RVAL_NONE) && u_error) {
942                 switch (u_error) {
943                 /* Blocked signals do not interrupt any syscalls.
944                  * In this case syscalls don't return ERESTARTfoo codes.
945                  *
946                  * Deadly signals set to SIG_DFL interrupt syscalls
947                  * and kill the process regardless of which of the codes below
948                  * is returned by the interrupted syscall.
949                  * In some cases, kernel forces a kernel-generated deadly
950                  * signal to be unblocked and set to SIG_DFL (and thus cause
951                  * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
952                  * or SIGILL. (The alternative is to leave process spinning
953                  * forever on the faulty instruction - not useful).
954                  *
955                  * SIG_IGNed signals and non-deadly signals set to SIG_DFL
956                  * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
957                  * but kernel will always restart them.
958                  */
959                 case ERESTARTSYS:
960                         /* Most common type of signal-interrupted syscall exit code.
961                          * The system call will be restarted with the same arguments
962                          * if SA_RESTART is set; otherwise, it will fail with EINTR.
963                          */
964                         tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
965                         break;
966                 case ERESTARTNOINTR:
967                         /* Rare. For example, fork() returns this if interrupted.
968                          * SA_RESTART is ignored (assumed set): the restart is unconditional.
969                          */
970                         tprints("= ? ERESTARTNOINTR (To be restarted)");
971                         break;
972                 case ERESTARTNOHAND:
973                         /* pause(), rt_sigsuspend() etc use this code.
974                          * SA_RESTART is ignored (assumed not set):
975                          * syscall won't restart (will return EINTR instead)
976                          * even after signal with SA_RESTART set. However,
977                          * after SIG_IGN or SIG_DFL signal it will restart
978                          * (thus the name "restart only if has no handler").
979                          */
980                         tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
981                         break;
982                 case ERESTART_RESTARTBLOCK:
983                         /* Syscalls like nanosleep(), poll() which can't be
984                          * restarted with their original arguments use this
985                          * code. Kernel will execute restart_syscall() instead,
986                          * which changes arguments before restarting syscall.
987                          * SA_RESTART is ignored (assumed not set) similarly
988                          * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
989                          * since restart data is saved in "restart block"
990                          * in task struct, and if signal handler uses a syscall
991                          * which in turn saves another such restart block,
992                          * old data is lost and restart becomes impossible)
993                          */
994                         tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
995                         break;
996                 default:
997                         if ((unsigned long) u_error < nerrnos
998                             && errnoent[u_error])
999                                 tprintf("= -1 %s (%s)", errnoent[u_error],
1000                                         strerror(u_error));
1001                         else
1002                                 tprintf("= -1 ERRNO_%lu (%s)", u_error,
1003                                         strerror(u_error));
1004                         break;
1005                 }
1006                 if ((sys_res & RVAL_STR) && tcp->auxstr)
1007                         tprintf(" (%s)", tcp->auxstr);
1008         }
1009         else {
1010                 if (sys_res & RVAL_NONE)
1011                         tprints("= ?");
1012                 else {
1013                         switch (sys_res & RVAL_MASK) {
1014                         case RVAL_HEX:
1015 #if SUPPORTED_PERSONALITIES > 1
1016                                 if (current_wordsize < sizeof(long))
1017                                         tprintf("= %#x",
1018                                                 (unsigned int) tcp->u_rval);
1019                                 else
1020 #endif
1021                                         tprintf("= %#lx", tcp->u_rval);
1022                                 break;
1023                         case RVAL_OCTAL:
1024                                 tprintf("= %#lo", tcp->u_rval);
1025                                 break;
1026                         case RVAL_UDECIMAL:
1027 #if SUPPORTED_PERSONALITIES > 1
1028                                 if (current_wordsize < sizeof(long))
1029                                         tprintf("= %u",
1030                                                 (unsigned int) tcp->u_rval);
1031                                 else
1032 #endif
1033                                         tprintf("= %lu", tcp->u_rval);
1034                                 break;
1035                         case RVAL_DECIMAL:
1036                                 tprintf("= %ld", tcp->u_rval);
1037                                 break;
1038                         case RVAL_FD:
1039                                 if (show_fd_path) {
1040                                         tprints("= ");
1041                                         printfd(tcp, tcp->u_rval);
1042                                 }
1043                                 else
1044                                         tprintf("= %ld", tcp->u_rval);
1045                                 break;
1046 #if HAVE_STRUCT_TCB_EXT_ARG
1047                         /*
1048                         case RVAL_LHEX:
1049                                 tprintf("= %#llx", tcp->u_lrval);
1050                                 break;
1051                         case RVAL_LOCTAL:
1052                                 tprintf("= %#llo", tcp->u_lrval);
1053                                 break;
1054                         */
1055                         case RVAL_LUDECIMAL:
1056                                 tprintf("= %llu", tcp->u_lrval);
1057                                 break;
1058                         /*
1059                         case RVAL_LDECIMAL:
1060                                 tprintf("= %lld", tcp->u_lrval);
1061                                 break;
1062                         */
1063 #endif /* HAVE_STRUCT_TCB_EXT_ARG */
1064                         default:
1065                                 error_msg("invalid rval format");
1066                                 break;
1067                         }
1068                 }
1069                 if ((sys_res & RVAL_STR) && tcp->auxstr)
1070                         tprintf(" (%s)", tcp->auxstr);
1071         }
1072         if (Tflag) {
1073                 tv_sub(&tv, &tv, &tcp->etime);
1074                 tprintf(" <%ld.%06ld>",
1075                         (long) tv.tv_sec, (long) tv.tv_usec);
1076         }
1077         tprints("\n");
1078         dumpio(tcp);
1079         line_ended();
1080
1081 #ifdef USE_LIBUNWIND
1082         if (stack_trace_enabled)
1083                 unwind_print_stacktrace(tcp);
1084 #endif
1085
1086  ret:
1087         tcp->flags &= ~TCB_INSYSCALL;
1088         tcp->sys_func_rval = 0;
1089         free_tcb_priv_data(tcp);
1090         return 0;
1091 }
1092
1093 int
1094 trace_syscall(struct tcb *tcp)
1095 {
1096         return exiting(tcp) ?
1097                 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
1098 }
1099
1100 bool
1101 is_erestart(struct tcb *tcp)
1102 {
1103         switch (tcp->u_error) {
1104                 case ERESTARTSYS:
1105                 case ERESTARTNOINTR:
1106                 case ERESTARTNOHAND:
1107                 case ERESTART_RESTARTBLOCK:
1108                         return true;
1109                 default:
1110                         return false;
1111         }
1112 }
1113
1114 static int saved_u_error;
1115
1116 void
1117 temporarily_clear_syserror(struct tcb *tcp)
1118 {
1119         saved_u_error = tcp->u_error;
1120         tcp->u_error = 0;
1121 }
1122
1123 void
1124 restore_cleared_syserror(struct tcb *tcp)
1125 {
1126         tcp->u_error = saved_u_error;
1127 }
1128
1129 #include "kernel_types.h"
1130
1131 /*
1132  * Check the syscall return value register value for whether it is
1133  * a negated errno code indicating an error, or a success return value.
1134  */
1135 static inline bool
1136 is_negated_errno(kernel_ulong_t val)
1137 {
1138         /* Linux kernel defines MAX_ERRNO to 4095. */
1139         kernel_ulong_t max = -(kernel_long_t) 4095;
1140
1141 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1142         if (current_wordsize < sizeof(val)) {
1143                 val = (uint32_t) val;
1144                 max = (uint32_t) max;
1145         }
1146 #elif defined X32
1147         /*
1148          * current_wordsize is 4 even in personality 0 (native X32)
1149          * but truncation _must not_ be done in it.
1150          * can't check current_wordsize here!
1151          */
1152         if (current_personality != 0) {
1153                 val = (uint32_t) val;
1154                 max = (uint32_t) max;
1155         }
1156 #endif
1157
1158         return val >= max;
1159 }
1160
1161 #include "arch_regs.c"
1162
1163 #ifdef HAVE_GETRVAL2
1164 # include "arch_getrval2.c"
1165 #endif
1166
1167 void
1168 print_pc(struct tcb *tcp)
1169 {
1170 #if defined ARCH_PC_REG
1171 # define ARCH_GET_PC 0
1172 #elif defined ARCH_PC_PEEK_ADDR
1173         long pc;
1174 # define ARCH_PC_REG pc
1175 # define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
1176 #else
1177 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1178 #endif
1179         if (get_regs_error || ARCH_GET_PC)
1180                 tprints(current_wordsize == 4 ? "[????????] "
1181                                               : "[????????????????] ");
1182         else
1183                 tprintf(current_wordsize == 4 ? "[%08lx] " : "[%016lx] ",
1184                         (unsigned long) ARCH_PC_REG);
1185 }
1186
1187 #if defined ARCH_REGS_FOR_GETREGSET
1188 static long
1189 get_regset(pid_t pid)
1190 {
1191 # ifdef ARCH_IOVEC_FOR_GETREGSET
1192         /* variable iovec */
1193         ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1194         return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1195                       &ARCH_IOVEC_FOR_GETREGSET);
1196 # else
1197         /* constant iovec */
1198         static struct iovec io = {
1199                 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1200                 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1201         };
1202         return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1203
1204 # endif
1205 }
1206 #endif /* ARCH_REGS_FOR_GETREGSET */
1207
1208 void
1209 get_regs(pid_t pid)
1210 {
1211 #undef USE_GET_SYSCALL_RESULT_REGS
1212 #ifdef ARCH_REGS_FOR_GETREGSET
1213 # ifdef X86_64
1214         /* Try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS. */
1215         static int getregset_support;
1216
1217         if (getregset_support >= 0) {
1218                 get_regs_error = get_regset(pid);
1219                 if (getregset_support > 0)
1220                         return;
1221                 if (get_regs_error >= 0) {
1222                         getregset_support = 1;
1223                         return;
1224                 }
1225                 if (errno == EPERM || errno == ESRCH)
1226                         return;
1227                 getregset_support = -1;
1228         }
1229         get_regs_error = getregs_old(pid);
1230 # else /* !X86_64 */
1231         /* Assume that PTRACE_GETREGSET works. */
1232         get_regs_error = get_regset(pid);
1233 # endif
1234 #elif defined ARCH_REGS_FOR_GETREGS
1235 # if defined SPARC || defined SPARC64
1236         /* SPARC systems have the meaning of data and addr reversed */
1237         get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&ARCH_REGS_FOR_GETREGS, 0);
1238 # elif defined POWERPC
1239         static bool old_kernel = 0;
1240         if (old_kernel)
1241                 goto old;
1242         get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1243         if (get_regs_error && errno == EIO) {
1244                 old_kernel = 1;
1245  old:
1246                 get_regs_error = getregs_old(pid);
1247         }
1248 # else
1249         /* Assume that PTRACE_GETREGS works. */
1250         get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1251 # endif
1252
1253 #else /* !ARCH_REGS_FOR_GETREGSET && !ARCH_REGS_FOR_GETREGS */
1254 # define USE_GET_SYSCALL_RESULT_REGS 1
1255 # warning get_regs is not implemented for this architecture yet
1256         get_regs_error = 0;
1257 #endif
1258 }
1259
1260 /*
1261  * Returns:
1262  * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1263  * 1: ok, continue in trace_syscall_entering().
1264  * other: error, trace_syscall_entering() should print error indicator
1265  *    ("????" etc) and bail out.
1266  */
1267 int
1268 get_scno(struct tcb *tcp)
1269 {
1270         if (get_regs_error)
1271                 return -1;
1272
1273         int rc = arch_get_scno(tcp);
1274         if (rc != 1)
1275                 return rc;
1276
1277         if (SCNO_IS_VALID(tcp->scno)) {
1278                 tcp->s_ent = &sysent[tcp->scno];
1279                 tcp->qual_flg = qual_flags[tcp->scno];
1280         } else {
1281                 static const struct_sysent unknown = {
1282                         .nargs = MAX_ARGS,
1283                         .sys_flags = 0,
1284                         .sys_func = printargs,
1285                         .sys_name = "system call",
1286                 };
1287                 tcp->s_ent = &unknown;
1288                 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1289                 if (debug_flag)
1290                         error_msg("pid %d invalid syscall %ld", tcp->pid, tcp->scno);
1291         }
1292         return 1;
1293 }
1294
1295 #ifdef USE_GET_SYSCALL_RESULT_REGS
1296 static int get_syscall_result_regs(struct tcb *);
1297 #endif
1298
1299 /* Returns:
1300  * 1: ok, continue in trace_syscall_exiting().
1301  * -1: error, trace_syscall_exiting() should print error indicator
1302  *    ("????" etc) and bail out.
1303  */
1304 static int
1305 get_syscall_result(struct tcb *tcp)
1306 {
1307 #ifdef USE_GET_SYSCALL_RESULT_REGS
1308         if (get_syscall_result_regs(tcp))
1309                 return -1;
1310 #endif
1311         tcp->u_error = 0;
1312         get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1313
1314         return 1;
1315 }
1316
1317 #include "get_scno.c"
1318 #include "get_syscall_args.c"
1319 #ifdef USE_GET_SYSCALL_RESULT_REGS
1320 # include "get_syscall_result.c"
1321 #endif
1322 #include "get_error.c"
1323 #if defined X86_64 || defined POWERPC
1324 # include "getregs_old.c"
1325 #endif