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