]> granicus.if.org Git - strace/blob - syscall.c
1a9c5fc297fc104773a6d1c3054f555e66d2e681
[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 const struct_sysent sysent0[] = {
85 #include "syscallent.h"
86 };
87
88 #if SUPPORTED_PERSONALITIES > 1
89 static const struct_sysent sysent1[] = {
90 # include "syscallent1.h"
91 };
92 #endif
93
94 #if SUPPORTED_PERSONALITIES > 2
95 static const struct_sysent sysent2[] = {
96 # include "syscallent2.h"
97 };
98 #endif
99
100 /* Now undef them since short defines cause wicked namespace pollution. */
101 #undef TD
102 #undef TF
103 #undef TI
104 #undef TN
105 #undef TP
106 #undef TS
107 #undef TM
108 #undef NF
109 #undef MA
110 #undef SI
111 #undef SE
112
113 /*
114  * `ioctlent[012].h' files are automatically generated by the auxiliary
115  * program `ioctlsort', such that the list is sorted by the `code' field.
116  * This has the side-effect of resolving the _IO.. macros into
117  * plain integers, eliminating the need to include here everything
118  * in "/usr/include".
119  */
120
121 const char *const errnoent0[] = {
122 #include "errnoent.h"
123 };
124 const char *const signalent0[] = {
125 #include "signalent.h"
126 };
127 const struct_ioctlent ioctlent0[] = {
128 #include "ioctlent0.h"
129 };
130
131 #if SUPPORTED_PERSONALITIES > 1
132 static const char *const errnoent1[] = {
133 # include "errnoent1.h"
134 };
135 static const char *const signalent1[] = {
136 # include "signalent1.h"
137 };
138 static const struct_ioctlent ioctlent1[] = {
139 # include "ioctlent1.h"
140 };
141 #endif
142
143 #if SUPPORTED_PERSONALITIES > 2
144 static const char *const errnoent2[] = {
145 # include "errnoent2.h"
146 };
147 static const char *const signalent2[] = {
148 # include "signalent2.h"
149 };
150 static const struct_ioctlent ioctlent2[] = {
151 # include "ioctlent2.h"
152 };
153 #endif
154
155 enum {
156         nsyscalls0 = ARRAY_SIZE(sysent0)
157 #if SUPPORTED_PERSONALITIES > 1
158         , nsyscalls1 = ARRAY_SIZE(sysent1)
159 # if SUPPORTED_PERSONALITIES > 2
160         , nsyscalls2 = ARRAY_SIZE(sysent2)
161 # endif
162 #endif
163 };
164
165 enum {
166         nerrnos0 = ARRAY_SIZE(errnoent0)
167 #if SUPPORTED_PERSONALITIES > 1
168         , nerrnos1 = ARRAY_SIZE(errnoent1)
169 # if SUPPORTED_PERSONALITIES > 2
170         , nerrnos2 = ARRAY_SIZE(errnoent2)
171 # endif
172 #endif
173 };
174
175 enum {
176         nsignals0 = ARRAY_SIZE(signalent0)
177 #if SUPPORTED_PERSONALITIES > 1
178         , nsignals1 = ARRAY_SIZE(signalent1)
179 # if SUPPORTED_PERSONALITIES > 2
180         , nsignals2 = ARRAY_SIZE(signalent2)
181 # endif
182 #endif
183 };
184
185 enum {
186         nioctlents0 = ARRAY_SIZE(ioctlent0)
187 #if SUPPORTED_PERSONALITIES > 1
188         , nioctlents1 = ARRAY_SIZE(ioctlent1)
189 # if SUPPORTED_PERSONALITIES > 2
190         , nioctlents2 = ARRAY_SIZE(ioctlent2)
191 # endif
192 #endif
193 };
194
195 #if SUPPORTED_PERSONALITIES > 1
196 const struct_sysent *sysent = sysent0;
197 const char *const *errnoent = errnoent0;
198 const char *const *signalent = signalent0;
199 const struct_ioctlent *ioctlent = ioctlent0;
200 #endif
201 unsigned nsyscalls = nsyscalls0;
202 unsigned nerrnos = nerrnos0;
203 unsigned nsignals = nsignals0;
204 unsigned nioctlents = nioctlents0;
205
206 unsigned num_quals;
207 qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
208
209 static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
210         nsyscalls0,
211 #if SUPPORTED_PERSONALITIES > 1
212         nsyscalls1,
213 #endif
214 #if SUPPORTED_PERSONALITIES > 2
215         nsyscalls2,
216 #endif
217 };
218 static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
219         sysent0,
220 #if SUPPORTED_PERSONALITIES > 1
221         sysent1,
222 #endif
223 #if SUPPORTED_PERSONALITIES > 2
224         sysent2,
225 #endif
226 };
227
228 enum {
229         MAX_NSYSCALLS1 = (nsyscalls0
230 #if SUPPORTED_PERSONALITIES > 1
231                         > nsyscalls1 ? nsyscalls0 : nsyscalls1
232 #endif
233                         ),
234         MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
235 #if SUPPORTED_PERSONALITIES > 2
236                         > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
237 #endif
238                         ),
239         MAX_NSYSCALLS = MAX_NSYSCALLS2,
240         /* We are ready for arches with up to 255 signals,
241          * even though the largest known signo is on MIPS and it is 128.
242          * The number of existing syscalls on all arches is
243          * larger that 255 anyway, so it is just a pedantic matter.
244          */
245         MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
246 };
247
248 #if SUPPORTED_PERSONALITIES > 1
249 unsigned current_personality;
250
251 # ifndef current_wordsize
252 unsigned current_wordsize;
253 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
254         PERSONALITY0_WORDSIZE,
255         PERSONALITY1_WORDSIZE,
256 # if SUPPORTED_PERSONALITIES > 2
257         PERSONALITY2_WORDSIZE,
258 # endif
259 };
260 # endif
261
262 void
263 set_personality(int personality)
264 {
265         nsyscalls = nsyscall_vec[personality];
266         sysent = sysent_vec[personality];
267
268         switch (personality) {
269         case 0:
270                 errnoent = errnoent0;
271                 nerrnos = nerrnos0;
272                 ioctlent = ioctlent0;
273                 nioctlents = nioctlents0;
274                 signalent = signalent0;
275                 nsignals = nsignals0;
276                 break;
277
278         case 1:
279                 errnoent = errnoent1;
280                 nerrnos = nerrnos1;
281                 ioctlent = ioctlent1;
282                 nioctlents = nioctlents1;
283                 signalent = signalent1;
284                 nsignals = nsignals1;
285                 break;
286
287 # if SUPPORTED_PERSONALITIES > 2
288         case 2:
289                 errnoent = errnoent2;
290                 nerrnos = nerrnos2;
291                 ioctlent = ioctlent2;
292                 nioctlents = nioctlents2;
293                 signalent = signalent2;
294                 nsignals = nsignals2;
295                 break;
296 # endif
297         }
298
299         current_personality = personality;
300 # ifndef current_wordsize
301         current_wordsize = personality_wordsize[personality];
302 # endif
303 }
304
305 static void
306 update_personality(struct tcb *tcp, unsigned int personality)
307 {
308         if (personality == current_personality)
309                 return;
310         set_personality(personality);
311
312         if (personality == tcp->currpers)
313                 return;
314         tcp->currpers = personality;
315
316 # if defined(POWERPC64)
317         if (!qflag) {
318                 static const char *const names[] = {"64 bit", "32 bit"};
319                 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
320                         tcp->pid, names[personality]);
321         }
322 # elif defined(X86_64)
323         if (!qflag) {
324                 static const char *const names[] = {"64 bit", "32 bit", "x32"};
325                 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
326                         tcp->pid, names[personality]);
327         }
328 # elif defined(X32)
329         if (!qflag) {
330                 static const char *const names[] = {"x32", "32 bit"};
331                 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
332                         tcp->pid, names[personality]);
333         }
334 # elif defined(AARCH64)
335         if (!qflag) {
336                 static const char *const names[] = {"32-bit", "AArch64"};
337                 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
338                         tcp->pid, names[personality]);
339         }
340 # elif defined(TILE)
341         if (!qflag) {
342                 static const char *const names[] = {"64-bit", "32-bit"};
343                 fprintf(stderr, "[ Process PID=%d runs in %s mode. ]\n",
344                         tcp->pid, names[personality]);
345         }
346 # endif
347 }
348 #endif
349
350 static int qual_syscall(), qual_signal(), qual_desc();
351
352 static const struct qual_options {
353         unsigned int bitflag;
354         const char *option_name;
355         int (*qualify)(const char *, int, int);
356         const char *argument_name;
357 } qual_options[] = {
358         { QUAL_TRACE,   "trace",        qual_syscall,   "system call"   },
359         { QUAL_TRACE,   "t",            qual_syscall,   "system call"   },
360         { QUAL_ABBREV,  "abbrev",       qual_syscall,   "system call"   },
361         { QUAL_ABBREV,  "a",            qual_syscall,   "system call"   },
362         { QUAL_VERBOSE, "verbose",      qual_syscall,   "system call"   },
363         { QUAL_VERBOSE, "v",            qual_syscall,   "system call"   },
364         { QUAL_RAW,     "raw",          qual_syscall,   "system call"   },
365         { QUAL_RAW,     "x",            qual_syscall,   "system call"   },
366         { QUAL_SIGNAL,  "signal",       qual_signal,    "signal"        },
367         { QUAL_SIGNAL,  "signals",      qual_signal,    "signal"        },
368         { QUAL_SIGNAL,  "s",            qual_signal,    "signal"        },
369         { QUAL_READ,    "read",         qual_desc,      "descriptor"    },
370         { QUAL_READ,    "reads",        qual_desc,      "descriptor"    },
371         { QUAL_READ,    "r",            qual_desc,      "descriptor"    },
372         { QUAL_WRITE,   "write",        qual_desc,      "descriptor"    },
373         { QUAL_WRITE,   "writes",       qual_desc,      "descriptor"    },
374         { QUAL_WRITE,   "w",            qual_desc,      "descriptor"    },
375         { 0,            NULL,           NULL,           NULL            },
376 };
377
378 static void
379 reallocate_qual(const unsigned int n)
380 {
381         unsigned p;
382         qualbits_t *qp;
383         for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
384                 qp = qual_vec[p] = xreallocarray(qual_vec[p], n,
385                                                  sizeof(qualbits_t));
386                 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
387         }
388         num_quals = n;
389 }
390
391 static void
392 qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
393 {
394         int p;
395
396         if (num_quals <= n)
397                 reallocate_qual(n + 1);
398
399         for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
400                 if (pers == p || pers < 0) {
401                         if (not)
402                                 qual_vec[p][n] &= ~bitflag;
403                         else
404                                 qual_vec[p][n] |= bitflag;
405                 }
406         }
407 }
408
409 static int
410 qual_syscall(const char *s, const unsigned int bitflag, const int not)
411 {
412         int p;
413         unsigned int i;
414         int rc = -1;
415
416         if (*s >= '0' && *s <= '9') {
417                 i = string_to_uint(s);
418                 if (i >= MAX_NSYSCALLS)
419                         return -1;
420                 qualify_one(i, bitflag, not, -1);
421                 return 0;
422         }
423
424         for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
425                 for (i = 0; i < nsyscall_vec[p]; i++) {
426                         if (sysent_vec[p][i].sys_name
427                          && strcmp(s, sysent_vec[p][i].sys_name) == 0
428                         ) {
429                                 qualify_one(i, bitflag, not, p);
430                                 rc = 0;
431                         }
432                 }
433         }
434
435         return rc;
436 }
437
438 static int
439 qual_signal(const char *s, const unsigned int bitflag, const int not)
440 {
441         unsigned int i;
442
443         if (*s >= '0' && *s <= '9') {
444                 int signo = string_to_uint(s);
445                 if (signo < 0 || signo > 255)
446                         return -1;
447                 qualify_one(signo, bitflag, not, -1);
448                 return 0;
449         }
450         if (strncasecmp(s, "SIG", 3) == 0)
451                 s += 3;
452         for (i = 0; i <= NSIG; i++) {
453                 if (strcasecmp(s, signame(i) + 3) == 0) {
454                         qualify_one(i, bitflag, not, -1);
455                         return 0;
456                 }
457         }
458         return -1;
459 }
460
461 static int
462 qual_desc(const char *s, const unsigned int bitflag, const int not)
463 {
464         if (*s >= '0' && *s <= '9') {
465                 int desc = string_to_uint(s);
466                 if (desc < 0 || desc > 0x7fff) /* paranoia */
467                         return -1;
468                 qualify_one(desc, bitflag, not, -1);
469                 return 0;
470         }
471         return -1;
472 }
473
474 static int
475 lookup_class(const char *s)
476 {
477         if (strcmp(s, "file") == 0)
478                 return TRACE_FILE;
479         if (strcmp(s, "ipc") == 0)
480                 return TRACE_IPC;
481         if (strcmp(s, "network") == 0)
482                 return TRACE_NETWORK;
483         if (strcmp(s, "process") == 0)
484                 return TRACE_PROCESS;
485         if (strcmp(s, "signal") == 0)
486                 return TRACE_SIGNAL;
487         if (strcmp(s, "desc") == 0)
488                 return TRACE_DESC;
489         if (strcmp(s, "memory") == 0)
490                 return TRACE_MEMORY;
491         return -1;
492 }
493
494 void
495 qualify(const char *s)
496 {
497         const struct qual_options *opt;
498         char *copy;
499         const char *p;
500         int not;
501         unsigned int i;
502
503         if (num_quals == 0)
504                 reallocate_qual(MIN_QUALS);
505
506         opt = &qual_options[0];
507         for (i = 0; (p = qual_options[i].option_name); i++) {
508                 unsigned int len = strlen(p);
509                 if (strncmp(s, p, len) == 0 && s[len] == '=') {
510                         opt = &qual_options[i];
511                         s += len + 1;
512                         break;
513                 }
514         }
515         not = 0;
516         if (*s == '!') {
517                 not = 1;
518                 s++;
519         }
520         if (strcmp(s, "none") == 0) {
521                 not = 1 - not;
522                 s = "all";
523         }
524         if (strcmp(s, "all") == 0) {
525                 for (i = 0; i < num_quals; i++) {
526                         qualify_one(i, opt->bitflag, not, -1);
527                 }
528                 return;
529         }
530         for (i = 0; i < num_quals; i++) {
531                 qualify_one(i, opt->bitflag, !not, -1);
532         }
533         copy = xstrdup(s);
534         for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
535                 int n;
536                 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
537                         unsigned pers;
538                         for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
539                                 for (i = 0; i < nsyscall_vec[pers]; i++)
540                                         if (sysent_vec[pers][i].sys_flags & n)
541                                                 qualify_one(i, opt->bitflag, not, pers);
542                         }
543                         continue;
544                 }
545                 if (opt->qualify(p, opt->bitflag, not)) {
546                         error_msg_and_die("invalid %s '%s'",
547                                 opt->argument_name, p);
548                 }
549         }
550         free(copy);
551         return;
552 }
553
554 #ifdef SYS_socket_subcall
555 static void
556 decode_socket_subcall(struct tcb *tcp)
557 {
558         unsigned long addr;
559         unsigned int n;
560
561         if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
562                 return;
563
564         tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
565         tcp->qual_flg = qual_flags[tcp->scno];
566         tcp->s_ent = &sysent[tcp->scno];
567         addr = tcp->u_arg[1];
568         n = tcp->s_ent->nargs;
569         if (sizeof(tcp->u_arg[0]) == current_wordsize) {
570                 memset(tcp->u_arg, 0, n * sizeof(tcp->u_arg[0]));
571                 (void) umoven(tcp, addr, n * sizeof(tcp->u_arg[0]), tcp->u_arg);
572         } else {
573                 unsigned int args[n];
574                 unsigned int i;
575
576                 memset(args, 0, sizeof(args));
577                 (void) umove(tcp, addr, &args);
578                 for (i = 0; i < n; ++i)
579                         tcp->u_arg[i] = args[i];
580         }
581 }
582 #endif
583
584 #ifdef SYS_ipc_subcall
585 static void
586 decode_ipc_subcall(struct tcb *tcp)
587 {
588         unsigned int i, n;
589
590         if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
591                 return;
592
593         tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
594         tcp->qual_flg = qual_flags[tcp->scno];
595         tcp->s_ent = &sysent[tcp->scno];
596         n = tcp->s_ent->nargs;
597         for (i = 0; i < n; i++)
598                 tcp->u_arg[i] = tcp->u_arg[i + 1];
599 }
600 #endif
601
602 #ifdef LINUX_MIPSO32
603 static void
604 decode_mips_subcall(struct tcb *tcp)
605 {
606         if (!SCNO_IS_VALID(tcp->u_arg[0]))
607                 return;
608         tcp->scno = tcp->u_arg[0];
609         tcp->qual_flg = qual_flags[tcp->scno];
610         tcp->s_ent = &sysent[tcp->scno];
611         memmove(&tcp->u_arg[0], &tcp->u_arg[1],
612                 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
613         /*
614          * Fetching the last arg of 7-arg syscalls (fadvise64_64
615          * and sync_file_range) would require additional code,
616          * see linux/mips/get_syscall_args.c
617          */
618 }
619
620 SYS_FUNC(syscall)
621 {
622         return printargs(tcp);
623 }
624 #endif
625
626 int
627 printargs(struct tcb *tcp)
628 {
629         if (entering(tcp)) {
630                 int i;
631                 int n = tcp->s_ent->nargs;
632                 for (i = 0; i < n; i++)
633                         tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
634         }
635         return 0;
636 }
637
638 int
639 printargs_lu(struct tcb *tcp)
640 {
641         if (entering(tcp)) {
642                 int i;
643                 int n = tcp->s_ent->nargs;
644                 for (i = 0; i < n; i++)
645                         tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
646         }
647         return 0;
648 }
649
650 int
651 printargs_ld(struct tcb *tcp)
652 {
653         if (entering(tcp)) {
654                 int i;
655                 int n = tcp->s_ent->nargs;
656                 for (i = 0; i < n; i++)
657                         tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
658         }
659         return 0;
660 }
661
662 static void
663 dumpio(struct tcb *tcp)
664 {
665         int (*func)();
666
667         if (syserror(tcp))
668                 return;
669         if ((unsigned long) tcp->u_arg[0] >= num_quals)
670                 return;
671         func = tcp->s_ent->sys_func;
672         if (func == printargs)
673                 return;
674         if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
675                 if (func == sys_read ||
676                     func == sys_pread ||
677                     func == sys_recv ||
678                     func == sys_recvfrom) {
679                         dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
680                         return;
681                 } else if (func == sys_readv) {
682                         dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
683                         return;
684 #if HAVE_SENDMSG
685                 } else if (func == sys_recvmsg) {
686                         dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
687                         return;
688                 } else if (func == sys_recvmmsg) {
689                         dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
690                         return;
691 #endif
692                 }
693         }
694         if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
695                 if (func == sys_write ||
696                     func == sys_pwrite ||
697                     func == sys_send ||
698                     func == sys_sendto)
699                         dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
700                 else if (func == sys_writev)
701                         dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
702 #if HAVE_SENDMSG
703                 else if (func == sys_sendmsg)
704                         dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
705                 else if (func == sys_sendmmsg)
706                         dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
707 #endif
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 (sys_syscall == tcp->s_ent->sys_func)
796                 decode_mips_subcall(tcp);
797 #endif
798
799         if (   sys_execve == tcp->s_ent->sys_func
800 # if defined(SPARC) || defined(SPARC64)
801             || sys_execv == tcp->s_ent->sys_func
802 # endif
803            ) {
804                 hide_log_until_execve = 0;
805         }
806
807 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
808         while (1) {
809 # ifdef SYS_socket_subcall
810                 if (tcp->s_ent->sys_func == sys_socketcall) {
811                         decode_socket_subcall(tcp);
812                         break;
813                 }
814 # endif
815 # ifdef SYS_ipc_subcall
816                 if (tcp->s_ent->sys_func == sys_ipc) {
817                         decode_ipc_subcall(tcp);
818                         break;
819                 }
820 # endif
821                 break;
822         }
823 #endif
824
825         if (!(tcp->qual_flg & QUAL_TRACE)
826          || (tracing_paths && !pathtrace_match(tcp))
827         ) {
828                 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
829                 return 0;
830         }
831
832         tcp->flags &= ~TCB_FILTERED;
833
834         if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
835                 res = 0;
836                 goto ret;
837         }
838
839 #ifdef USE_LIBUNWIND
840         if (stack_trace_enabled) {
841                 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
842                         unwind_capture_stacktrace(tcp);
843         }
844 #endif
845
846         printleader(tcp);
847         if (tcp->qual_flg & UNDEFINED_SCNO)
848                 tprintf("%s(", undefined_scno_name(tcp));
849         else
850                 tprintf("%s(", tcp->s_ent->sys_name);
851         if ((tcp->qual_flg & QUAL_RAW) && tcp->s_ent->sys_func != sys_exit)
852                 res = printargs(tcp);
853         else
854                 res = tcp->s_ent->sys_func(tcp);
855
856         fflush(tcp->outf);
857  ret:
858         tcp->flags |= TCB_INSYSCALL;
859         /* Measure the entrance time as late as possible to avoid errors. */
860         if (Tflag || cflag)
861                 gettimeofday(&tcp->etime, NULL);
862         return res;
863 }
864
865 static int
866 trace_syscall_exiting(struct tcb *tcp)
867 {
868         int sys_res;
869         struct timeval tv;
870         int res;
871         long u_error;
872
873         /* Measure the exit time as early as possible to avoid errors. */
874         if (Tflag || cflag)
875                 gettimeofday(&tv, NULL);
876
877 #ifdef USE_LIBUNWIND
878         if (stack_trace_enabled) {
879                 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
880                         unwind_cache_invalidate(tcp);
881         }
882 #endif
883
884 #if SUPPORTED_PERSONALITIES > 1
885         update_personality(tcp, tcp->currpers);
886 #endif
887         res = (get_regs_error ? -1 : get_syscall_result(tcp));
888         if (res == 1) {
889                 if (filtered(tcp) || hide_log_until_execve)
890                         goto ret;
891         }
892
893         if (cflag) {
894                 count_syscall(tcp, &tv);
895                 if (cflag == CFLAG_ONLY_STATS) {
896                         goto ret;
897                 }
898         }
899
900         /* If not in -ff mode, and printing_tcp != tcp,
901          * then the log currently does not end with output
902          * of _our syscall entry_, but with something else.
903          * We need to say which syscall's return is this.
904          *
905          * Forced reprinting via TCB_REPRINT is used only by
906          * "strace -ff -oLOG test/threaded_execve" corner case.
907          * It's the only case when -ff mode needs reprinting.
908          */
909         if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
910                 tcp->flags &= ~TCB_REPRINT;
911                 printleader(tcp);
912                 if (tcp->qual_flg & UNDEFINED_SCNO)
913                         tprintf("<... %s resumed> ", undefined_scno_name(tcp));
914                 else
915                         tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
916         }
917         printing_tcp = tcp;
918
919         tcp->s_prev_ent = NULL;
920         if (res != 1) {
921                 /* There was error in one of prior ptrace ops */
922                 tprints(") ");
923                 tabto();
924                 tprints("= ? <unavailable>\n");
925                 line_ended();
926                 tcp->flags &= ~TCB_INSYSCALL;
927                 return res;
928         }
929         tcp->s_prev_ent = tcp->s_ent;
930
931         sys_res = 0;
932         if (tcp->qual_flg & QUAL_RAW) {
933                 /* sys_res = printargs(tcp); - but it's nop on sysexit */
934         } else {
935         /* FIXME: not_failing_only (IOW, option -z) is broken:
936          * failure of syscall is known only after syscall return.
937          * Thus we end up with something like this on, say, ENOENT:
938          *     open("doesnt_exist", O_RDONLY <unfinished ...>
939          *     {next syscall decode}
940          * whereas the intended result is that open(...) line
941          * is not shown at all.
942          */
943                 if (not_failing_only && tcp->u_error)
944                         goto ret;       /* ignore failed syscalls */
945                 sys_res = tcp->s_ent->sys_func(tcp);
946         }
947
948         tprints(") ");
949         tabto();
950         u_error = tcp->u_error;
951         if (tcp->qual_flg & QUAL_RAW) {
952                 if (u_error)
953                         tprintf("= -1 (errno %ld)", u_error);
954                 else
955                         tprintf("= %#lx", tcp->u_rval);
956         }
957         else if (!(sys_res & RVAL_NONE) && u_error) {
958                 switch (u_error) {
959                 /* Blocked signals do not interrupt any syscalls.
960                  * In this case syscalls don't return ERESTARTfoo codes.
961                  *
962                  * Deadly signals set to SIG_DFL interrupt syscalls
963                  * and kill the process regardless of which of the codes below
964                  * is returned by the interrupted syscall.
965                  * In some cases, kernel forces a kernel-generated deadly
966                  * signal to be unblocked and set to SIG_DFL (and thus cause
967                  * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
968                  * or SIGILL. (The alternative is to leave process spinning
969                  * forever on the faulty instruction - not useful).
970                  *
971                  * SIG_IGNed signals and non-deadly signals set to SIG_DFL
972                  * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
973                  * but kernel will always restart them.
974                  */
975                 case ERESTARTSYS:
976                         /* Most common type of signal-interrupted syscall exit code.
977                          * The system call will be restarted with the same arguments
978                          * if SA_RESTART is set; otherwise, it will fail with EINTR.
979                          */
980                         tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
981                         break;
982                 case ERESTARTNOINTR:
983                         /* Rare. For example, fork() returns this if interrupted.
984                          * SA_RESTART is ignored (assumed set): the restart is unconditional.
985                          */
986                         tprints("= ? ERESTARTNOINTR (To be restarted)");
987                         break;
988                 case ERESTARTNOHAND:
989                         /* pause(), rt_sigsuspend() etc use this code.
990                          * SA_RESTART is ignored (assumed not set):
991                          * syscall won't restart (will return EINTR instead)
992                          * even after signal with SA_RESTART set. However,
993                          * after SIG_IGN or SIG_DFL signal it will restart
994                          * (thus the name "restart only if has no handler").
995                          */
996                         tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
997                         break;
998                 case ERESTART_RESTARTBLOCK:
999                         /* Syscalls like nanosleep(), poll() which can't be
1000                          * restarted with their original arguments use this
1001                          * code. Kernel will execute restart_syscall() instead,
1002                          * which changes arguments before restarting syscall.
1003                          * SA_RESTART is ignored (assumed not set) similarly
1004                          * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
1005                          * since restart data is saved in "restart block"
1006                          * in task struct, and if signal handler uses a syscall
1007                          * which in turn saves another such restart block,
1008                          * old data is lost and restart becomes impossible)
1009                          */
1010                         tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
1011                         break;
1012                 default:
1013                         if ((unsigned long) u_error < nerrnos
1014                             && errnoent[u_error])
1015                                 tprintf("= -1 %s (%s)", errnoent[u_error],
1016                                         strerror(u_error));
1017                         else
1018                                 tprintf("= -1 ERRNO_%lu (%s)", u_error,
1019                                         strerror(u_error));
1020                         break;
1021                 }
1022                 if ((sys_res & RVAL_STR) && tcp->auxstr)
1023                         tprintf(" (%s)", tcp->auxstr);
1024         }
1025         else {
1026                 if (sys_res & RVAL_NONE)
1027                         tprints("= ?");
1028                 else {
1029                         switch (sys_res & RVAL_MASK) {
1030                         case RVAL_HEX:
1031 #if SUPPORTED_PERSONALITIES > 1
1032                                 if (current_wordsize < sizeof(long))
1033                                         tprintf("= %#x",
1034                                                 (unsigned int) tcp->u_rval);
1035                                 else
1036 #endif
1037                                         tprintf("= %#lx", tcp->u_rval);
1038                                 break;
1039                         case RVAL_OCTAL:
1040                                 tprintf("= %#lo", tcp->u_rval);
1041                                 break;
1042                         case RVAL_UDECIMAL:
1043                                 tprintf("= %lu", tcp->u_rval);
1044                                 break;
1045                         case RVAL_DECIMAL:
1046                                 tprintf("= %ld", tcp->u_rval);
1047                                 break;
1048                         case RVAL_FD:
1049                                 if (show_fd_path) {
1050                                         tprints("= ");
1051                                         printfd(tcp, tcp->u_rval);
1052                                 }
1053                                 else
1054                                         tprintf("= %ld", tcp->u_rval);
1055                                 break;
1056 #if defined(LINUX_MIPSN32) || defined(X32)
1057                         /*
1058                         case RVAL_LHEX:
1059                                 tprintf("= %#llx", tcp->u_lrval);
1060                                 break;
1061                         case RVAL_LOCTAL:
1062                                 tprintf("= %#llo", tcp->u_lrval);
1063                                 break;
1064                         */
1065                         case RVAL_LUDECIMAL:
1066                                 tprintf("= %llu", tcp->u_lrval);
1067                                 break;
1068                         /*
1069                         case RVAL_LDECIMAL:
1070                                 tprintf("= %lld", tcp->u_lrval);
1071                                 break;
1072                         */
1073 #endif
1074                         default:
1075                                 fprintf(stderr,
1076                                         "invalid rval format\n");
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         return 0;
1100 }
1101
1102 int
1103 trace_syscall(struct tcb *tcp)
1104 {
1105         return exiting(tcp) ?
1106                 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
1107 }
1108
1109 /*
1110  * Cannot rely on __kernel_[u]long_t being defined,
1111  * it is quite a recent feature of <asm/posix_types.h>.
1112  */
1113 #ifdef __kernel_long_t
1114 typedef __kernel_long_t kernel_long_t;
1115 typedef __kernel_ulong_t kernel_ulong_t;
1116 #else
1117 # ifdef X32
1118 typedef long long kernel_long_t;
1119 typedef unsigned long long kernel_ulong_t;
1120 # else
1121 typedef long kernel_long_t;
1122 typedef unsigned long kernel_ulong_t;
1123 # endif
1124 #endif
1125
1126 /*
1127  * Check the syscall return value register value for whether it is
1128  * a negated errno code indicating an error, or a success return value.
1129  */
1130 static inline bool
1131 is_negated_errno(kernel_ulong_t val)
1132 {
1133         /* Linux kernel defines MAX_ERRNO to 4095. */
1134         kernel_ulong_t max = -(kernel_long_t) 4095;
1135
1136 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1137         if (current_wordsize < sizeof(val)) {
1138                 val = (uint32_t) val;
1139                 max = (uint32_t) max;
1140         }
1141 #elif defined X32
1142         /*
1143          * current_wordsize is 4 even in personality 0 (native X32)
1144          * but truncation _must not_ be done in it.
1145          * can't check current_wordsize here!
1146          */
1147         if (current_personality != 0) {
1148                 val = (uint32_t) val;
1149                 max = (uint32_t) max;
1150         }
1151 #endif
1152
1153         return val >= max;
1154 }
1155
1156 #include "arch_regs.c"
1157
1158 #ifdef HAVE_GETRVAL2
1159 # include "arch_getrval2.c"
1160 #endif
1161
1162 void
1163 print_pc(struct tcb *tcp)
1164 {
1165         const char *fmt;
1166         const char *bad;
1167
1168 #ifdef current_wordsize
1169 # define pc_wordsize current_wordsize
1170 #else
1171 # define pc_wordsize personality_wordsize[tcp->currpers]
1172 #endif
1173
1174         if (pc_wordsize == 4) {
1175                 fmt = "[%08lx] ";
1176                 bad = "[????????] ";
1177         } else {
1178                 fmt = "[%016lx] ";
1179                 bad = "[????????????????] ";
1180         }
1181
1182 #undef pc_wordsize
1183 #define PRINTBADPC tprints(bad)
1184
1185         if (get_regs_error) {
1186                 PRINTBADPC;
1187                 return;
1188         }
1189
1190 #include "print_pc.c"
1191 }
1192
1193 #if defined X86_64 || defined POWERPC
1194 # include "getregs_old.c"
1195 #endif
1196
1197 #if defined ARCH_REGS_FOR_GETREGSET
1198 static long
1199 get_regset(pid_t pid)
1200 {
1201 # ifdef ARCH_IOVEC_FOR_GETREGSET
1202         /* variable iovec */
1203         ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1204         return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1205                       &ARCH_IOVEC_FOR_GETREGSET);
1206 # else
1207         /* constant iovec */
1208         static struct iovec io = {
1209                 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1210                 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1211         };
1212         return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1213
1214 # endif
1215 }
1216 #endif /* ARCH_REGS_FOR_GETREGSET */
1217
1218 void
1219 get_regs(pid_t pid)
1220 {
1221 #ifdef ARCH_REGS_FOR_GETREGSET
1222 # ifdef X86_64
1223         /* Try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS. */
1224         static int getregset_support;
1225
1226         if (getregset_support >= 0) {
1227                 get_regs_error = get_regset(pid);
1228                 if (getregset_support > 0)
1229                         return;
1230                 if (get_regs_error >= 0) {
1231                         getregset_support = 1;
1232                         return;
1233                 }
1234                 if (errno == EPERM || errno == ESRCH)
1235                         return;
1236                 getregset_support = -1;
1237         }
1238         getregs_old(pid);
1239 # else /* !X86_64 */
1240         /* Assume that PTRACE_GETREGSET works. */
1241         get_regs_error = get_regset(pid);
1242 # endif
1243 #elif defined ARCH_REGS_FOR_GETREGS
1244 # if defined SPARC || defined SPARC64
1245         /* SPARC systems have the meaning of data and addr reversed */
1246         get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&ARCH_REGS_FOR_GETREGS, 0);
1247 # elif defined POWERPC
1248         static bool old_kernel = 0;
1249         if (old_kernel)
1250                 goto old;
1251         get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1252         if (get_regs_error && errno == EIO) {
1253                 old_kernel = 1;
1254  old:
1255                 get_regs_error = getregs_old(pid);
1256         }
1257 # else
1258         /* Assume that PTRACE_GETREGS works. */
1259         get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1260 # endif
1261
1262 #else /* !ARCH_REGS_FOR_GETREGSET && !ARCH_REGS_FOR_GETREGS */
1263 #  warning get_regs is not implemented for this architecture yet
1264         get_regs_error = 0;
1265 #endif
1266 }
1267
1268 /* Returns:
1269  * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1270  * 1: ok, continue in trace_syscall_entering().
1271  * other: error, trace_syscall_entering() should print error indicator
1272  *    ("????" etc) and bail out.
1273  */
1274 int
1275 get_scno(struct tcb *tcp)
1276 {
1277         if (get_regs_error)
1278                 return -1;
1279
1280         long scno = 0;
1281
1282 #include "get_scno.c"
1283
1284         tcp->scno = scno;
1285         if (SCNO_IS_VALID(tcp->scno)) {
1286                 tcp->s_ent = &sysent[scno];
1287                 tcp->qual_flg = qual_flags[scno];
1288         } else {
1289                 static const struct_sysent unknown = {
1290                         .nargs = MAX_ARGS,
1291                         .sys_flags = 0,
1292                         .sys_func = printargs,
1293                         .sys_name = "system call",
1294                 };
1295                 tcp->s_ent = &unknown;
1296                 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1297                 if (debug_flag)
1298                         fprintf(stderr, "pid %d invalid syscall %ld\n",
1299                                 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 }