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