]> granicus.if.org Git - strace/blob - syscall.c
Fix meaning of negated sets in fault expressions
[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 n = string_to_uint_upto(s, MAX_NSYSCALLS - 1);
461         if (n < 0)
462                 return false;
463
464         if (not && fopts) {
465                 /* set bitflag for all syscall numbers except n */
466                 unsigned int p;
467                 for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
468                         unsigned int i;
469
470                         for (i = 0; i < nsyscall_vec[p]; ++i) {
471                                 if (i != (unsigned int) n
472                                     && sysent_vec[p][i].sys_name) {
473                                         qualify_one(i, bitflag, 0, p, fopts);
474                                 }
475                         }
476                 }
477         } else {
478                 qualify_one(n, bitflag, not, -1, fopts);
479         }
480
481         return true;
482 }
483
484 static int
485 lookup_class(const char *s)
486 {
487         if (strcmp(s, "all") == 0)
488                 return 0;
489         if (strcmp(s, "file") == 0)
490                 return TRACE_FILE;
491         if (strcmp(s, "ipc") == 0)
492                 return TRACE_IPC;
493         if (strcmp(s, "network") == 0)
494                 return TRACE_NETWORK;
495         if (strcmp(s, "process") == 0)
496                 return TRACE_PROCESS;
497         if (strcmp(s, "signal") == 0)
498                 return TRACE_SIGNAL;
499         if (strcmp(s, "desc") == 0)
500                 return TRACE_DESC;
501         if (strcmp(s, "memory") == 0)
502                 return TRACE_MEMORY;
503         return -1;
504 }
505
506 static bool
507 qualify_syscall_class(const char *const s, const unsigned int bitflag,
508                       const int not, const struct fault_opts *const fopts)
509 {
510         unsigned int p;
511         const int n = lookup_class(s);
512
513         if (n < 0)
514                 return false;
515
516         for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
517                 unsigned int i;
518
519                 for (i = 0; i < nsyscall_vec[p]; ++i) {
520                         if (!sysent_vec[p][i].sys_name)
521                                 continue;
522                         const bool match = (sysent_vec[p][i].sys_flags & n) == n;
523                         if (match ^ (not && fopts)) {
524                                 qualify_one(i, bitflag, not && !fopts, p, fopts);
525                         }
526                 }
527         }
528
529         return true;
530 }
531
532 static bool
533 qualify_syscall_name(const char *const s, const unsigned int bitflag,
534                      const int not, const struct fault_opts *const fopts)
535 {
536         bool found = false;
537         unsigned int p;
538
539         for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
540                 unsigned int i;
541
542                 for (i = 0; i < nsyscall_vec[p]; ++i) {
543                         if (!sysent_vec[p][i].sys_name)
544                                 continue;
545                         const bool match = !strcmp(s, sysent_vec[p][i].sys_name);
546                         found = found || match;
547                         if (match ^ (not && fopts)) {
548                                 qualify_one(i, bitflag, not && !fopts, p, fopts);
549                         }
550                 }
551         }
552
553         return found;
554 }
555
556 static int
557 qual_syscall_ex(const char *const s, const unsigned int bitflag,
558                 const int not, const struct fault_opts *const fopts)
559 {
560         if (qualify_scno(s, bitflag, not, fopts)
561             || qualify_syscall_class(s, bitflag, not, fopts)
562             || qualify_syscall_name(s, bitflag, not, fopts)) {
563                 return 0;
564         }
565
566         return -1;
567 }
568
569 static int
570 qual_syscall(const char *const s, const unsigned int bitflag, const int not)
571 {
572         return qual_syscall_ex(s, bitflag, not, NULL);
573 }
574
575 /*
576  * Returns NULL if STR does not start with PREFIX,
577  * or a pointer to the first char in STR after PREFIX.
578  */
579 static const char *
580 strip_prefix(const char *prefix, const char *str)
581 {
582         size_t len = strlen(prefix);
583
584         return (len > strlen(str) || memcmp(prefix, str, len))
585                ? NULL : str + len;
586 }
587
588 static int
589 find_errno_by_name(const char *name)
590 {
591         unsigned int i;
592
593         for (i = 1; i < nerrnos; ++i) {
594                 if (errnoent[i] && (strcmp(name, errnoent[i]) == 0))
595                         return i;
596         }
597
598         return -1;
599 }
600
601 static bool
602 parse_fault_token(const char *const token, struct fault_opts *const fopts)
603 {
604         const char *val;
605         int intval;
606
607         if ((val = strip_prefix("when=", token))) {
608                 /*
609                  *      == 1+1
610                  * F    == F+0
611                  * F+   == F+1
612                  * F+S
613                  */
614                 char *end;
615                 intval = string_to_uint_ex(val, &end, 0xffff, "+");
616                 if (intval < 1)
617                         return false;
618
619                 fopts->first = intval;
620
621                 if (*end) {
622                         val = end + 1;
623                         if (*val) {
624                                 /* F+S */
625                                 intval = string_to_uint_upto(val, 0xffff);
626                                 if (intval < 1)
627                                         return false;
628                                 fopts->step = intval;
629                         } else {
630                                 /* F+ == F+1 */
631                                 fopts->step = 1;
632                         }
633                 } else {
634                         /* F == F+0 */
635                         fopts->step = 0;
636                 }
637         } else if ((val = strip_prefix("error=", token))) {
638                 intval = string_to_uint_upto(val, 4095);
639                 if (intval < 0)
640                         intval = find_errno_by_name(val);
641                 if (intval < 1)
642                         return false;
643                 fopts->err = intval;
644         } else {
645                 return false;
646         }
647
648         return true;
649 }
650
651 static char *
652 parse_fault_expression(const char *const s, char **buf,
653                        struct fault_opts *const fopts)
654 {
655         char *saveptr = NULL;
656         char *name = NULL;
657         char *token;
658
659         *buf = xstrdup(s);
660         for (token = strtok_r(*buf, ":", &saveptr); token;
661              token = strtok_r(NULL, ":", &saveptr)) {
662                 if (!name)
663                         name = token;
664                 else if (!parse_fault_token(token, fopts))
665                         goto parse_error;
666         }
667
668         if (name)
669                 return name;
670
671 parse_error:
672         free(*buf);
673         return *buf = NULL;
674 }
675
676 static int
677 qual_fault(const char *const s, const unsigned int bitflag, const int not)
678 {
679         struct fault_opts opts = {
680                 .first = 1,
681                 .step = 1,
682                 .err = 0
683         };
684
685         char *buf = NULL;
686         char *name = parse_fault_expression(s, &buf, &opts);
687         char *saveptr = NULL;
688         const char *token;
689         int rc = -1;
690
691         if (!name)
692                 return -1;
693
694         for (token = strtok_r(name, ",", &saveptr); token;
695              token = strtok_r(NULL, ",", &saveptr)) {
696                 rc = qual_syscall_ex(token, bitflag, not, &opts);
697                 if (rc)
698                         break;
699         }
700
701         free(buf);
702         return rc;
703 }
704
705 static int
706 qual_signal(const char *s, const unsigned int bitflag, const int not)
707 {
708         int i;
709
710         if (*s >= '0' && *s <= '9') {
711                 i = string_to_uint_upto(s, 255);
712                 if (i < 0)
713                         return -1;
714                 qualify_one(i, bitflag, not, -1, NULL);
715                 return 0;
716         }
717         if (strncasecmp(s, "SIG", 3) == 0)
718                 s += 3;
719         for (i = 0; i <= NSIG; ++i) {
720                 const char *name = signame(i);
721                 if (strncasecmp(name, "SIG", 3) != 0)
722                         continue;
723                 name += 3;
724
725                 if (strcasecmp(name, s) != 0)
726                         continue;
727                 qualify_one(i, bitflag, not, -1, NULL);
728                 return 0;
729         }
730         return -1;
731 }
732
733 static int
734 qual_desc(const char *s, const unsigned int bitflag, const int not)
735 {
736         int desc = string_to_uint_upto(s, 0x7fff);
737         if (desc < 0)
738                 return -1;
739         qualify_one(desc, bitflag, not, -1, NULL);
740         return 0;
741 }
742
743 void
744 qualify(const char *s)
745 {
746         const struct qual_options *opt;
747         char *copy;
748         const char *p;
749         int not;
750         unsigned int i;
751
752         if (num_quals == 0) {
753                 reallocate_qual(MIN_QUALS);
754                 reallocate_fault(MIN_QUALS);
755         }
756
757         opt = &qual_options[0];
758         for (i = 0; (p = qual_options[i].option_name); i++) {
759                 unsigned int len = strlen(p);
760                 if (strncmp(s, p, len) == 0 && s[len] == '=') {
761                         opt = &qual_options[i];
762                         s += len + 1;
763                         break;
764                 }
765         }
766         not = 0;
767         if (*s == '!') {
768                 not = 1;
769                 s++;
770         }
771         if (strcmp(s, "none") == 0) {
772                 not = 1 - not;
773                 s = "all";
774         }
775         if (opt->bitflag == QUAL_FAULT) {
776                 if (opt->qualify(s, opt->bitflag, not)) {
777                         error_msg_and_die("invalid %s '%s'",
778                                 opt->argument_name, s);
779                 }
780                 return;
781         }
782         if (strcmp(s, "all") == 0) {
783                 for (i = 0; i < num_quals; ++i) {
784                         qualify_one(i, opt->bitflag, not, -1, NULL);
785                 }
786                 return;
787         }
788         for (i = 0; i < num_quals; ++i) {
789                 qualify_one(i, opt->bitflag, !not, -1, NULL);
790         }
791         copy = xstrdup(s);
792         for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
793                 if (opt->qualify(p, opt->bitflag, not)) {
794                         error_msg_and_die("invalid %s '%s'",
795                                 opt->argument_name, p);
796                 }
797         }
798         free(copy);
799         return;
800 }
801
802 #ifdef SYS_socket_subcall
803 static void
804 decode_socket_subcall(struct tcb *tcp)
805 {
806         const int call = tcp->u_arg[0];
807
808         if (call < 1 || call >= SYS_socket_nsubcalls)
809                 return;
810
811         const unsigned long scno = SYS_socket_subcall + call;
812         const unsigned int nargs = sysent[scno].nargs;
813         uint64_t buf[nargs];
814
815         if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
816                 return;
817
818         tcp->scno = scno;
819         tcp->qual_flg = qual_flags[scno];
820         tcp->s_ent = &sysent[scno];
821
822         unsigned int i;
823         for (i = 0; i < nargs; ++i)
824                 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
825                                 ? ((uint32_t *) (void *) buf)[i] : buf[i];
826 }
827 #endif
828
829 #ifdef SYS_ipc_subcall
830 static void
831 decode_ipc_subcall(struct tcb *tcp)
832 {
833         unsigned int call = tcp->u_arg[0];
834         const unsigned int version = call >> 16;
835
836         if (version) {
837 # if defined S390 || defined S390X
838                 return;
839 # else
840 #  ifdef SPARC64
841                 if (current_wordsize == 8)
842                         return;
843 #  endif
844                 set_tcb_priv_ulong(tcp, version);
845                 call &= 0xffff;
846 # endif
847         }
848
849         switch (call) {
850                 case  1: case  2: case  3: case  4:
851                 case 11: case 12: case 13: case 14:
852                 case 21: case 22: case 23: case 24:
853                         break;
854                 default:
855                         return;
856         }
857
858         tcp->scno = SYS_ipc_subcall + call;
859         tcp->qual_flg = qual_flags[tcp->scno];
860         tcp->s_ent = &sysent[tcp->scno];
861
862         const unsigned int n = tcp->s_ent->nargs;
863         unsigned int i;
864         for (i = 0; i < n; i++)
865                 tcp->u_arg[i] = tcp->u_arg[i + 1];
866 }
867 #endif
868
869 #ifdef LINUX_MIPSO32
870 static void
871 decode_mips_subcall(struct tcb *tcp)
872 {
873         if (!SCNO_IS_VALID(tcp->u_arg[0]))
874                 return;
875         tcp->scno = tcp->u_arg[0];
876         tcp->qual_flg = qual_flags[tcp->scno];
877         tcp->s_ent = &sysent[tcp->scno];
878         memmove(&tcp->u_arg[0], &tcp->u_arg[1],
879                 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
880         /*
881          * Fetching the last arg of 7-arg syscalls (fadvise64_64
882          * and sync_file_range) requires additional code,
883          * see linux/mips/get_syscall_args.c
884          */
885         if (tcp->s_ent->nargs == MAX_ARGS) {
886                 if (umoven(tcp,
887                            mips_REG_SP + MAX_ARGS * sizeof(tcp->u_arg[0]),
888                            sizeof(tcp->u_arg[0]),
889                            &tcp->u_arg[MAX_ARGS - 1]) < 0)
890                 tcp->u_arg[MAX_ARGS - 1] = 0;
891         }
892 }
893 #endif /* LINUX_MIPSO32 */
894
895 static void
896 dumpio(struct tcb *tcp)
897 {
898         int sen;
899
900         if (syserror(tcp))
901                 return;
902         if ((unsigned long) tcp->u_arg[0] >= num_quals)
903                 return;
904         sen = tcp->s_ent->sen;
905         if (SEN_printargs == sen)
906                 return;
907         if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
908                 switch (sen) {
909                 case SEN_read:
910                 case SEN_pread:
911                 case SEN_recv:
912                 case SEN_recvfrom:
913                 case SEN_mq_timedreceive:
914                         dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
915                         return;
916                 case SEN_readv:
917                 case SEN_preadv:
918                 case SEN_preadv2:
919                         dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
920                                      tcp->u_rval);
921                         return;
922                 case SEN_recvmsg:
923                         dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
924                         return;
925                 case SEN_recvmmsg:
926                         dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
927                         return;
928                 }
929         }
930         if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
931                 switch (sen) {
932                 case SEN_write:
933                 case SEN_pwrite:
934                 case SEN_send:
935                 case SEN_sendto:
936                 case SEN_mq_timedsend:
937                         dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
938                         break;
939                 case SEN_writev:
940                 case SEN_pwritev:
941                 case SEN_pwritev2:
942                 case SEN_vmsplice:
943                         dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
944                         break;
945                 case SEN_sendmsg:
946                         dumpiov_in_msghdr(tcp, tcp->u_arg[1],
947                                           (unsigned long) -1L);
948                         break;
949                 case SEN_sendmmsg:
950                         dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
951                         break;
952                 }
953         }
954 }
955
956 /*
957  * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
958  * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
959  */
960 #if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
961 static long
962 shuffle_scno(unsigned long scno)
963 {
964         if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
965                 return scno;
966
967         /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
968         if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
969                 return 0x000ffff0;
970         if (scno == 0x000ffff0)
971                 return ARM_FIRST_SHUFFLED_SYSCALL;
972
973 #define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
974         /*
975          * Is it ARM specific syscall?
976          * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
977          * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
978          */
979         if (scno >= 0x000f0000 &&
980             scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
981                 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
982         }
983         if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
984                 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
985         }
986
987         return scno;
988 }
989 #else
990 # define shuffle_scno(scno) ((long)(scno))
991 #endif
992
993 const char *
994 syscall_name(long scno)
995 {
996         static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
997
998         if (SCNO_IS_VALID(scno))
999                 return sysent[scno].sys_name;
1000         else {
1001                 sprintf(buf, "syscall_%lu", scno);
1002                 return buf;
1003         }
1004 }
1005
1006 const char *
1007 err_name(unsigned long err)
1008 {
1009         if ((err < nerrnos) && errnoent[err])
1010                 return errnoent[err];
1011
1012         return NULL;
1013 }
1014
1015 static long get_regs_error;
1016
1017 void
1018 clear_regs(void)
1019 {
1020         get_regs_error = -1;
1021 }
1022
1023 static int get_syscall_args(struct tcb *);
1024 static int get_syscall_result(struct tcb *);
1025 static int arch_get_scno(struct tcb *tcp);
1026 static int arch_set_scno(struct tcb *, long);
1027 static void get_error(struct tcb *, const bool);
1028 static int arch_set_error(struct tcb *);
1029
1030 static struct fault_opts *
1031 tcb_fault_opts(struct tcb *tcp)
1032 {
1033         return (SCNO_IN_RANGE(tcp->scno) && tcp->fault_vec[current_personality])
1034                ? &tcp->fault_vec[current_personality][tcp->scno] : NULL;
1035 }
1036
1037
1038 static long
1039 inject_syscall_fault_entering(struct tcb *tcp)
1040 {
1041         if (!tcp->fault_vec[current_personality]) {
1042                 tcp->fault_vec[current_personality] =
1043                         xreallocarray(NULL, num_faults,
1044                                       sizeof(struct fault_opts));
1045                 memcpy(tcp->fault_vec[current_personality],
1046                        fault_vec[current_personality],
1047                        num_faults * sizeof(struct fault_opts));
1048         }
1049
1050         struct fault_opts *opts = tcb_fault_opts(tcp);
1051
1052         if (opts->first == 0)
1053                 return 0;
1054
1055         --opts->first;
1056
1057         if (opts->first != 0)
1058                 return 0;
1059
1060         opts->first = opts->step;
1061
1062         if (!arch_set_scno(tcp, -1))
1063                 tcp->flags |= TCB_FAULT_INJ;
1064
1065         return 0;
1066 }
1067
1068 static long
1069 update_syscall_fault_exiting(struct tcb *tcp)
1070 {
1071         struct fault_opts *opts = tcb_fault_opts(tcp);
1072
1073         if (opts && opts->err && tcp->u_error != opts->err) {
1074                 unsigned long u_error = tcp->u_error;
1075                 tcp->u_error = opts->err;
1076                 if (arch_set_error(tcp))
1077                         tcp->u_error = u_error;
1078         }
1079
1080         return 0;
1081 }
1082
1083 static int
1084 trace_syscall_entering(struct tcb *tcp)
1085 {
1086         int res, scno_good;
1087
1088         scno_good = res = get_scno(tcp);
1089         if (res == 0)
1090                 return res;
1091         if (res == 1)
1092                 res = get_syscall_args(tcp);
1093
1094         if (res != 1) {
1095                 printleader(tcp);
1096                 tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
1097                 /*
1098                  * " <unavailable>" will be added later by the code which
1099                  * detects ptrace errors.
1100                  */
1101                 goto ret;
1102         }
1103
1104 #ifdef LINUX_MIPSO32
1105         if (SEN_syscall == tcp->s_ent->sen)
1106                 decode_mips_subcall(tcp);
1107 #endif
1108
1109 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
1110         switch (tcp->s_ent->sen) {
1111 # ifdef SYS_socket_subcall
1112                 case SEN_socketcall:
1113                         decode_socket_subcall(tcp);
1114                         break;
1115 # endif
1116 # ifdef SYS_ipc_subcall
1117                 case SEN_ipc:
1118                         decode_ipc_subcall(tcp);
1119                         break;
1120 # endif
1121         }
1122 #endif
1123
1124         /* Restrain from fault injection while the trace executes strace code. */
1125         if (hide_log(tcp)) {
1126                 tcp->qual_flg &= ~QUAL_FAULT;
1127         }
1128
1129         switch (tcp->s_ent->sen) {
1130                 case SEN_execve:
1131                 case SEN_execveat:
1132 #if defined SPARC || defined SPARC64
1133                 case SEN_execv:
1134 #endif
1135                         tcp->flags &= ~TCB_HIDE_LOG;
1136                         break;
1137         }
1138
1139         if (!(tcp->qual_flg & QUAL_TRACE)
1140          || (tracing_paths && !pathtrace_match(tcp))
1141         ) {
1142                 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
1143                 tcp->sys_func_rval = 0;
1144                 return 0;
1145         }
1146
1147         tcp->flags &= ~TCB_FILTERED;
1148
1149         if (hide_log(tcp)) {
1150                 res = 0;
1151                 goto ret;
1152         }
1153
1154         if (tcp->qual_flg & QUAL_FAULT)
1155                 inject_syscall_fault_entering(tcp);
1156
1157         if (cflag == CFLAG_ONLY_STATS) {
1158                 res = 0;
1159                 goto ret;
1160         }
1161
1162 #ifdef USE_LIBUNWIND
1163         if (stack_trace_enabled) {
1164                 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
1165                         unwind_capture_stacktrace(tcp);
1166         }
1167 #endif
1168
1169         printleader(tcp);
1170         tprintf("%s(", tcp->s_ent->sys_name);
1171         if (tcp->qual_flg & QUAL_RAW)
1172                 res = printargs(tcp);
1173         else
1174                 res = tcp->s_ent->sys_func(tcp);
1175
1176         fflush(tcp->outf);
1177  ret:
1178         tcp->flags |= TCB_INSYSCALL;
1179         tcp->sys_func_rval = res;
1180         /* Measure the entrance time as late as possible to avoid errors. */
1181         if (Tflag || cflag)
1182                 gettimeofday(&tcp->etime, NULL);
1183         return res;
1184 }
1185
1186 static bool
1187 syscall_fault_injected(struct tcb *tcp)
1188 {
1189         return tcp->flags & TCB_FAULT_INJ;
1190 }
1191
1192 static int
1193 trace_syscall_exiting(struct tcb *tcp)
1194 {
1195         int sys_res;
1196         struct timeval tv;
1197         int res;
1198         unsigned long u_error;
1199         const char *u_error_str;
1200
1201         /* Measure the exit time as early as possible to avoid errors. */
1202         if (Tflag || cflag)
1203                 gettimeofday(&tv, NULL);
1204
1205 #ifdef USE_LIBUNWIND
1206         if (stack_trace_enabled) {
1207                 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
1208                         unwind_cache_invalidate(tcp);
1209         }
1210 #endif
1211
1212 #if SUPPORTED_PERSONALITIES > 1
1213         update_personality(tcp, tcp->currpers);
1214 #endif
1215         res = (get_regs_error ? -1 : get_syscall_result(tcp));
1216         if (filtered(tcp) || hide_log(tcp))
1217                 goto ret;
1218
1219         if (syserror(tcp) && syscall_fault_injected(tcp))
1220                 update_syscall_fault_exiting(tcp);
1221
1222         if (cflag) {
1223                 count_syscall(tcp, &tv);
1224                 if (cflag == CFLAG_ONLY_STATS) {
1225                         goto ret;
1226                 }
1227         }
1228
1229         /* If not in -ff mode, and printing_tcp != tcp,
1230          * then the log currently does not end with output
1231          * of _our syscall entry_, but with something else.
1232          * We need to say which syscall's return is this.
1233          *
1234          * Forced reprinting via TCB_REPRINT is used only by
1235          * "strace -ff -oLOG test/threaded_execve" corner case.
1236          * It's the only case when -ff mode needs reprinting.
1237          */
1238         if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
1239                 tcp->flags &= ~TCB_REPRINT;
1240                 printleader(tcp);
1241                 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
1242         }
1243         printing_tcp = tcp;
1244
1245         tcp->s_prev_ent = NULL;
1246         if (res != 1) {
1247                 /* There was error in one of prior ptrace ops */
1248                 tprints(") ");
1249                 tabto();
1250                 tprints("= ? <unavailable>\n");
1251                 line_ended();
1252                 tcp->flags &= ~(TCB_INSYSCALL | TCB_FAULT_INJ);
1253                 tcp->sys_func_rval = 0;
1254                 free_tcb_priv_data(tcp);
1255                 return res;
1256         }
1257         tcp->s_prev_ent = tcp->s_ent;
1258
1259         sys_res = 0;
1260         if (tcp->qual_flg & QUAL_RAW) {
1261                 /* sys_res = printargs(tcp); - but it's nop on sysexit */
1262         } else {
1263         /* FIXME: not_failing_only (IOW, option -z) is broken:
1264          * failure of syscall is known only after syscall return.
1265          * Thus we end up with something like this on, say, ENOENT:
1266          *     open("doesnt_exist", O_RDONLY <unfinished ...>
1267          *     {next syscall decode}
1268          * whereas the intended result is that open(...) line
1269          * is not shown at all.
1270          */
1271                 if (not_failing_only && tcp->u_error)
1272                         goto ret;       /* ignore failed syscalls */
1273                 if (tcp->sys_func_rval & RVAL_DECODED)
1274                         sys_res = tcp->sys_func_rval;
1275                 else
1276                         sys_res = tcp->s_ent->sys_func(tcp);
1277         }
1278
1279         tprints(") ");
1280         tabto();
1281         u_error = tcp->u_error;
1282
1283         if (tcp->qual_flg & QUAL_RAW) {
1284                 if (u_error) {
1285                         tprintf("= -1 (errno %lu)", u_error);
1286                         if (syscall_fault_injected(tcp))
1287                                 tprints(" (INJECTED)");
1288                 } else {
1289                         tprintf("= %#lx", tcp->u_rval);
1290                 }
1291         }
1292         else if (!(sys_res & RVAL_NONE) && u_error) {
1293                 switch (u_error) {
1294                 /* Blocked signals do not interrupt any syscalls.
1295                  * In this case syscalls don't return ERESTARTfoo codes.
1296                  *
1297                  * Deadly signals set to SIG_DFL interrupt syscalls
1298                  * and kill the process regardless of which of the codes below
1299                  * is returned by the interrupted syscall.
1300                  * In some cases, kernel forces a kernel-generated deadly
1301                  * signal to be unblocked and set to SIG_DFL (and thus cause
1302                  * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
1303                  * or SIGILL. (The alternative is to leave process spinning
1304                  * forever on the faulty instruction - not useful).
1305                  *
1306                  * SIG_IGNed signals and non-deadly signals set to SIG_DFL
1307                  * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
1308                  * but kernel will always restart them.
1309                  */
1310                 case ERESTARTSYS:
1311                         /* Most common type of signal-interrupted syscall exit code.
1312                          * The system call will be restarted with the same arguments
1313                          * if SA_RESTART is set; otherwise, it will fail with EINTR.
1314                          */
1315                         tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
1316                         break;
1317                 case ERESTARTNOINTR:
1318                         /* Rare. For example, fork() returns this if interrupted.
1319                          * SA_RESTART is ignored (assumed set): the restart is unconditional.
1320                          */
1321                         tprints("= ? ERESTARTNOINTR (To be restarted)");
1322                         break;
1323                 case ERESTARTNOHAND:
1324                         /* pause(), rt_sigsuspend() etc use this code.
1325                          * SA_RESTART is ignored (assumed not set):
1326                          * syscall won't restart (will return EINTR instead)
1327                          * even after signal with SA_RESTART set. However,
1328                          * after SIG_IGN or SIG_DFL signal it will restart
1329                          * (thus the name "restart only if has no handler").
1330                          */
1331                         tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
1332                         break;
1333                 case ERESTART_RESTARTBLOCK:
1334                         /* Syscalls like nanosleep(), poll() which can't be
1335                          * restarted with their original arguments use this
1336                          * code. Kernel will execute restart_syscall() instead,
1337                          * which changes arguments before restarting syscall.
1338                          * SA_RESTART is ignored (assumed not set) similarly
1339                          * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
1340                          * since restart data is saved in "restart block"
1341                          * in task struct, and if signal handler uses a syscall
1342                          * which in turn saves another such restart block,
1343                          * old data is lost and restart becomes impossible)
1344                          */
1345                         tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
1346                         break;
1347                 default:
1348                         u_error_str = err_name(u_error);
1349                         if (u_error_str)
1350                                 tprintf("= -1 %s (%s)",
1351                                         u_error_str, strerror(u_error));
1352                         else
1353                                 tprintf("= -1 %lu (%s)",
1354                                         u_error, strerror(u_error));
1355                         break;
1356                 }
1357                 if (syscall_fault_injected(tcp))
1358                         tprintf(" (INJECTED)");
1359                 if ((sys_res & RVAL_STR) && tcp->auxstr)
1360                         tprintf(" (%s)", tcp->auxstr);
1361         }
1362         else {
1363                 if (sys_res & RVAL_NONE)
1364                         tprints("= ?");
1365                 else {
1366                         switch (sys_res & RVAL_MASK) {
1367                         case RVAL_HEX:
1368 #if SUPPORTED_PERSONALITIES > 1
1369                                 if (current_wordsize < sizeof(long))
1370                                         tprintf("= %#x",
1371                                                 (unsigned int) tcp->u_rval);
1372                                 else
1373 #endif
1374                                         tprintf("= %#lx", tcp->u_rval);
1375                                 break;
1376                         case RVAL_OCTAL:
1377                                 tprints("= ");
1378                                 print_numeric_long_umask(tcp->u_rval);
1379                                 break;
1380                         case RVAL_UDECIMAL:
1381 #if SUPPORTED_PERSONALITIES > 1
1382                                 if (current_wordsize < sizeof(long))
1383                                         tprintf("= %u",
1384                                                 (unsigned int) tcp->u_rval);
1385                                 else
1386 #endif
1387                                         tprintf("= %lu", tcp->u_rval);
1388                                 break;
1389                         case RVAL_DECIMAL:
1390                                 tprintf("= %ld", tcp->u_rval);
1391                                 break;
1392                         case RVAL_FD:
1393                                 if (show_fd_path) {
1394                                         tprints("= ");
1395                                         printfd(tcp, tcp->u_rval);
1396                                 }
1397                                 else
1398                                         tprintf("= %ld", tcp->u_rval);
1399                                 break;
1400 #if HAVE_STRUCT_TCB_EXT_ARG
1401                         /*
1402                         case RVAL_LHEX:
1403                                 tprintf("= %#llx", tcp->u_lrval);
1404                                 break;
1405                         case RVAL_LOCTAL:
1406                                 tprintf("= %#llo", tcp->u_lrval);
1407                                 break;
1408                         */
1409                         case RVAL_LUDECIMAL:
1410                                 tprintf("= %llu", tcp->u_lrval);
1411                                 break;
1412                         /*
1413                         case RVAL_LDECIMAL:
1414                                 tprintf("= %lld", tcp->u_lrval);
1415                                 break;
1416                         */
1417 #endif /* HAVE_STRUCT_TCB_EXT_ARG */
1418                         default:
1419                                 error_msg("invalid rval format");
1420                                 break;
1421                         }
1422                 }
1423                 if ((sys_res & RVAL_STR) && tcp->auxstr)
1424                         tprintf(" (%s)", tcp->auxstr);
1425         }
1426         if (Tflag) {
1427                 tv_sub(&tv, &tv, &tcp->etime);
1428                 tprintf(" <%ld.%06ld>",
1429                         (long) tv.tv_sec, (long) tv.tv_usec);
1430         }
1431         tprints("\n");
1432         dumpio(tcp);
1433         line_ended();
1434
1435 #ifdef USE_LIBUNWIND
1436         if (stack_trace_enabled)
1437                 unwind_print_stacktrace(tcp);
1438 #endif
1439
1440  ret:
1441         tcp->flags &= ~(TCB_INSYSCALL | TCB_FAULT_INJ);
1442         tcp->sys_func_rval = 0;
1443         free_tcb_priv_data(tcp);
1444         return 0;
1445 }
1446
1447 int
1448 trace_syscall(struct tcb *tcp)
1449 {
1450         return exiting(tcp) ?
1451                 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
1452 }
1453
1454 bool
1455 is_erestart(struct tcb *tcp)
1456 {
1457         switch (tcp->u_error) {
1458                 case ERESTARTSYS:
1459                 case ERESTARTNOINTR:
1460                 case ERESTARTNOHAND:
1461                 case ERESTART_RESTARTBLOCK:
1462                         return true;
1463                 default:
1464                         return false;
1465         }
1466 }
1467
1468 static unsigned long saved_u_error;
1469
1470 void
1471 temporarily_clear_syserror(struct tcb *tcp)
1472 {
1473         saved_u_error = tcp->u_error;
1474         tcp->u_error = 0;
1475 }
1476
1477 void
1478 restore_cleared_syserror(struct tcb *tcp)
1479 {
1480         tcp->u_error = saved_u_error;
1481 }
1482
1483 #include "kernel_types.h"
1484
1485 /*
1486  * Check the syscall return value register value for whether it is
1487  * a negated errno code indicating an error, or a success return value.
1488  */
1489 static inline bool
1490 is_negated_errno(kernel_ulong_t val)
1491 {
1492         /* Linux kernel defines MAX_ERRNO to 4095. */
1493         kernel_ulong_t max = -(kernel_long_t) 4095;
1494
1495 #if defined X86_64 || defined X32
1496         /*
1497          * current_wordsize is 4 for x32 personality
1498          * but truncation _must not_ be done in it, so
1499          * check current_personality instead.
1500          */
1501         if (current_personality == 1) {
1502                 val = (uint32_t) val;
1503                 max = (uint32_t) max;
1504         }
1505 #elif SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1506         if (current_wordsize < sizeof(val)) {
1507                 val = (uint32_t) val;
1508                 max = (uint32_t) max;
1509         }
1510 #endif
1511
1512         return val >= max;
1513 }
1514
1515 #include "arch_regs.c"
1516
1517 #ifdef HAVE_GETRVAL2
1518 # include "arch_getrval2.c"
1519 #endif
1520
1521 void
1522 print_pc(struct tcb *tcp)
1523 {
1524 #if defined ARCH_PC_REG
1525 # define ARCH_GET_PC 0
1526 #elif defined ARCH_PC_PEEK_ADDR
1527         long pc;
1528 # define ARCH_PC_REG pc
1529 # define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
1530 #else
1531 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1532 #endif
1533         if (get_regs_error || ARCH_GET_PC)
1534                 tprints(current_wordsize == 4 ? "[????????] "
1535                                               : "[????????????????] ");
1536         else
1537                 tprintf(current_wordsize == 4 ? "[%08lx] " : "[%016lx] ",
1538                         (unsigned long) ARCH_PC_REG);
1539 }
1540
1541 #include "getregs_old.h"
1542
1543 #undef ptrace_getregset_or_getregs
1544 #undef ptrace_setregset_or_setregs
1545 #ifdef ARCH_REGS_FOR_GETREGSET
1546
1547 # define ptrace_getregset_or_getregs ptrace_getregset
1548 static long
1549 ptrace_getregset(pid_t pid)
1550 {
1551 # ifdef ARCH_IOVEC_FOR_GETREGSET
1552         /* variable iovec */
1553         ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1554         return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1555                       &ARCH_IOVEC_FOR_GETREGSET);
1556 # else
1557         /* constant iovec */
1558         static struct iovec io = {
1559                 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1560                 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1561         };
1562         return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1563
1564 # endif
1565 }
1566
1567 # ifndef HAVE_GETREGS_OLD
1568 #  define ptrace_setregset_or_setregs ptrace_setregset
1569 static int
1570 ptrace_setregset(pid_t pid)
1571 {
1572 #  ifdef ARCH_IOVEC_FOR_GETREGSET
1573         /* variable iovec */
1574         return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1575                       &ARCH_IOVEC_FOR_GETREGSET);
1576 #  else
1577         /* constant iovec */
1578         static struct iovec io = {
1579                 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1580                 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1581         };
1582         return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1583 #  endif
1584 }
1585 # endif /* !HAVE_GETREGS_OLD */
1586
1587 #elif defined ARCH_REGS_FOR_GETREGS
1588
1589 # define ptrace_getregset_or_getregs ptrace_getregs
1590 static long
1591 ptrace_getregs(pid_t pid)
1592 {
1593 # if defined SPARC || defined SPARC64
1594         /* SPARC systems have the meaning of data and addr reversed */
1595         return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1596 # else
1597         return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1598 # endif
1599 }
1600
1601 # ifndef HAVE_GETREGS_OLD
1602 #  define ptrace_setregset_or_setregs ptrace_setregs
1603 static int
1604 ptrace_setregs(pid_t pid)
1605 {
1606 #  if defined SPARC || defined SPARC64
1607         /* SPARC systems have the meaning of data and addr reversed */
1608         return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1609 #  else
1610         return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1611 #  endif
1612 }
1613 # endif /* !HAVE_GETREGS_OLD */
1614
1615 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1616
1617 void
1618 get_regs(pid_t pid)
1619 {
1620 #undef USE_GET_SYSCALL_RESULT_REGS
1621 #ifdef ptrace_getregset_or_getregs
1622
1623 # ifdef HAVE_GETREGS_OLD
1624         /*
1625          * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1626          * fallback to getregs_old.
1627          */
1628         static int use_getregs_old;
1629         if (use_getregs_old < 0) {
1630                 get_regs_error = ptrace_getregset_or_getregs(pid);
1631                 return;
1632         } else if (use_getregs_old == 0) {
1633                 get_regs_error = ptrace_getregset_or_getregs(pid);
1634                 if (get_regs_error >= 0) {
1635                         use_getregs_old = -1;
1636                         return;
1637                 }
1638                 if (errno == EPERM || errno == ESRCH)
1639                         return;
1640                 use_getregs_old = 1;
1641         }
1642         get_regs_error = getregs_old(pid);
1643 # else /* !HAVE_GETREGS_OLD */
1644         /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1645         get_regs_error = ptrace_getregset_or_getregs(pid);
1646 # endif /* !HAVE_GETREGS_OLD */
1647
1648 #else /* !ptrace_getregset_or_getregs */
1649
1650 # define USE_GET_SYSCALL_RESULT_REGS 1
1651 # warning get_regs is not implemented for this architecture yet
1652         get_regs_error = 0;
1653
1654 #endif /* !ptrace_getregset_or_getregs */
1655 }
1656
1657 #ifdef ptrace_setregset_or_setregs
1658 static int
1659 set_regs(pid_t pid)
1660 {
1661         return ptrace_setregset_or_setregs(pid);
1662 }
1663 #endif /* ptrace_setregset_or_setregs */
1664
1665 struct sysent_buf {
1666         struct tcb *tcp;
1667         struct_sysent ent;
1668         char buf[sizeof("syscall_%lu") + sizeof(long) * 3];
1669 };
1670
1671 static void
1672 free_sysent_buf(void *ptr)
1673 {
1674         struct sysent_buf *s = ptr;
1675         s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1676         free(ptr);
1677 }
1678
1679 /*
1680  * Returns:
1681  * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1682  * 1: ok, continue in trace_syscall_entering().
1683  * other: error, trace_syscall_entering() should print error indicator
1684  *    ("????" etc) and bail out.
1685  */
1686 int
1687 get_scno(struct tcb *tcp)
1688 {
1689         if (get_regs_error)
1690                 return -1;
1691
1692         int rc = arch_get_scno(tcp);
1693         if (rc != 1)
1694                 return rc;
1695
1696         if (SCNO_IS_VALID(tcp->scno)) {
1697                 tcp->s_ent = &sysent[tcp->scno];
1698                 tcp->qual_flg = qual_flags[tcp->scno];
1699         } else {
1700                 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1701
1702                 s->tcp = tcp;
1703                 s->ent.nargs = MAX_ARGS;
1704                 s->ent.sen = SEN_printargs;
1705                 s->ent.sys_func = printargs;
1706                 s->ent.sys_name = s->buf;
1707                 sprintf(s->buf, "syscall_%lu", shuffle_scno(tcp->scno));
1708
1709                 tcp->s_ent = &s->ent;
1710                 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1711
1712                 set_tcb_priv_data(tcp, s, free_sysent_buf);
1713
1714                 if (debug_flag)
1715                         error_msg("pid %d invalid syscall %ld", tcp->pid, tcp->scno);
1716         }
1717         return 1;
1718 }
1719
1720 #ifdef USE_GET_SYSCALL_RESULT_REGS
1721 static int get_syscall_result_regs(struct tcb *);
1722 #endif
1723
1724 /* Returns:
1725  * 1: ok, continue in trace_syscall_exiting().
1726  * -1: error, trace_syscall_exiting() should print error indicator
1727  *    ("????" etc) and bail out.
1728  */
1729 static int
1730 get_syscall_result(struct tcb *tcp)
1731 {
1732 #ifdef USE_GET_SYSCALL_RESULT_REGS
1733         if (get_syscall_result_regs(tcp))
1734                 return -1;
1735 #endif
1736         tcp->u_error = 0;
1737         get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1738
1739         return 1;
1740 }
1741
1742 #include "get_scno.c"
1743 #include "set_scno.c"
1744 #include "get_syscall_args.c"
1745 #ifdef USE_GET_SYSCALL_RESULT_REGS
1746 # include "get_syscall_result.c"
1747 #endif
1748 #include "get_error.c"
1749 #include "set_error.c"
1750 #ifdef HAVE_GETREGS_OLD
1751 # include "getregs_old.c"
1752 #endif