]> granicus.if.org Git - strace/blob - syscall.c
Split qual_syscall into separate functions
[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_signal(const char *, unsigned int, int);
364 static int qual_syscall(const char *, unsigned int, int);
365
366 static const struct qual_options {
367         unsigned int bitflag;
368         const char *option_name;
369         int (*qualify)(const char *, unsigned int, int);
370         const char *argument_name;
371 } qual_options[] = {
372         { QUAL_TRACE,   "trace",        qual_syscall,   "system call"   },
373         { QUAL_TRACE,   "t",            qual_syscall,   "system call"   },
374         { QUAL_ABBREV,  "abbrev",       qual_syscall,   "system call"   },
375         { QUAL_ABBREV,  "a",            qual_syscall,   "system call"   },
376         { QUAL_VERBOSE, "verbose",      qual_syscall,   "system call"   },
377         { QUAL_VERBOSE, "v",            qual_syscall,   "system call"   },
378         { QUAL_RAW,     "raw",          qual_syscall,   "system call"   },
379         { QUAL_RAW,     "x",            qual_syscall,   "system call"   },
380         { QUAL_SIGNAL,  "signal",       qual_signal,    "signal"        },
381         { QUAL_SIGNAL,  "signals",      qual_signal,    "signal"        },
382         { QUAL_SIGNAL,  "s",            qual_signal,    "signal"        },
383         { QUAL_READ,    "read",         qual_desc,      "descriptor"    },
384         { QUAL_READ,    "reads",        qual_desc,      "descriptor"    },
385         { QUAL_READ,    "r",            qual_desc,      "descriptor"    },
386         { QUAL_WRITE,   "write",        qual_desc,      "descriptor"    },
387         { QUAL_WRITE,   "writes",       qual_desc,      "descriptor"    },
388         { QUAL_WRITE,   "w",            qual_desc,      "descriptor"    },
389         { 0,            NULL,           NULL,           NULL            },
390 };
391
392 static void
393 reallocate_vec(void **vec, unsigned int old_nmemb,
394                size_t size, unsigned int new_nmemb)
395 {
396         unsigned int p;
397
398         for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
399                 vec[p] = xreallocarray(vec[p], new_nmemb, size);
400                 memset(vec[p] + size * old_nmemb, 0,
401                        (new_nmemb - old_nmemb) * size);
402         }
403 }
404
405 static void
406 reallocate_qual(const unsigned int n)
407 {
408         reallocate_vec((void **) qual_vec, num_quals, sizeof(qualbits_t), n);
409         num_quals = n;
410 }
411
412 static void
413 qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
414 {
415         int p;
416
417         if (num_quals <= n)
418                 reallocate_qual(n + 1);
419
420         for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
421                 if (pers == p || pers < 0) {
422                         if (not)
423                                 qual_vec[p][n] &= ~bitflag;
424                         else
425                                 qual_vec[p][n] |= bitflag;
426                 }
427         }
428 }
429
430 static bool
431 qualify_scno(const char *const s, const unsigned int bitflag,
432              const int not)
433 {
434         unsigned int i;
435
436         if (*s < '0' || *s > '9' || (i = string_to_uint(s)) >= MAX_NSYSCALLS)
437                 return false;
438
439         qualify_one(i, bitflag, not, -1);
440         return true;
441 }
442
443 static int
444 lookup_class(const char *s)
445 {
446         if (strcmp(s, "file") == 0)
447                 return TRACE_FILE;
448         if (strcmp(s, "ipc") == 0)
449                 return TRACE_IPC;
450         if (strcmp(s, "network") == 0)
451                 return TRACE_NETWORK;
452         if (strcmp(s, "process") == 0)
453                 return TRACE_PROCESS;
454         if (strcmp(s, "signal") == 0)
455                 return TRACE_SIGNAL;
456         if (strcmp(s, "desc") == 0)
457                 return TRACE_DESC;
458         if (strcmp(s, "memory") == 0)
459                 return TRACE_MEMORY;
460         return -1;
461 }
462
463 static bool
464 qualify_syscall_class(const char *const s, const unsigned int bitflag,
465                       const int not)
466 {
467         unsigned int p;
468         const int n = lookup_class(s);
469
470         if (n < 0)
471                 return false;
472
473         for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
474                 unsigned int i;
475
476                 for (i = 0; i < nsyscall_vec[p]; ++i) {
477                         if (sysent_vec[p][i].sys_name
478                             && (sysent_vec[p][i].sys_flags & n) == n) {
479                                 qualify_one(i, bitflag, not, p);
480                         }
481                 }
482         }
483
484         return true;
485 }
486
487 static bool
488 qualify_syscall_name(const char *const s, const unsigned int bitflag,
489                      const int not)
490 {
491         bool found = false;
492         unsigned int p;
493
494         for (p = 0; p < SUPPORTED_PERSONALITIES; ++p) {
495                 unsigned int i;
496
497                 for (i = 0; i < nsyscall_vec[p]; ++i) {
498                         if (sysent_vec[p][i].sys_name
499                             && strcmp(s, sysent_vec[p][i].sys_name) == 0) {
500                                 qualify_one(i, bitflag, not, p);
501                                 found = true;
502                         }
503                 }
504         }
505
506         return found;
507 }
508
509 static int
510 qual_syscall(const char *s, const unsigned int bitflag, const int not)
511 {
512         if (qualify_scno(s, bitflag, not)
513             || qualify_syscall_class(s, bitflag, not)
514             || qualify_syscall_name(s, bitflag, not)) {
515                 return 0;
516         }
517
518         return -1;
519 }
520
521 static int
522 qual_signal(const char *s, const unsigned int bitflag, const int not)
523 {
524         unsigned int i;
525
526         if (*s >= '0' && *s <= '9') {
527                 int signo = string_to_uint(s);
528                 if (signo < 0 || signo > 255)
529                         return -1;
530                 qualify_one(signo, bitflag, not, -1);
531                 return 0;
532         }
533         if (strncasecmp(s, "SIG", 3) == 0)
534                 s += 3;
535         for (i = 0; i <= NSIG; i++) {
536                 if (strcasecmp(s, signame(i) + 3) == 0) {
537                         qualify_one(i, bitflag, not, -1);
538                         return 0;
539                 }
540         }
541         return -1;
542 }
543
544 static int
545 qual_desc(const char *s, const unsigned int bitflag, const int not)
546 {
547         if (*s >= '0' && *s <= '9') {
548                 int desc = string_to_uint(s);
549                 if (desc < 0 || desc > 0x7fff) /* paranoia */
550                         return -1;
551                 qualify_one(desc, bitflag, not, -1);
552                 return 0;
553         }
554         return -1;
555 }
556
557 void
558 qualify(const char *s)
559 {
560         const struct qual_options *opt;
561         char *copy;
562         const char *p;
563         int not;
564         unsigned int i;
565
566         if (num_quals == 0)
567                 reallocate_qual(MIN_QUALS);
568
569         opt = &qual_options[0];
570         for (i = 0; (p = qual_options[i].option_name); i++) {
571                 unsigned int len = strlen(p);
572                 if (strncmp(s, p, len) == 0 && s[len] == '=') {
573                         opt = &qual_options[i];
574                         s += len + 1;
575                         break;
576                 }
577         }
578         not = 0;
579         if (*s == '!') {
580                 not = 1;
581                 s++;
582         }
583         if (strcmp(s, "none") == 0) {
584                 not = 1 - not;
585                 s = "all";
586         }
587         if (strcmp(s, "all") == 0) {
588                 for (i = 0; i < num_quals; i++) {
589                         qualify_one(i, opt->bitflag, not, -1);
590                 }
591                 return;
592         }
593         for (i = 0; i < num_quals; i++) {
594                 qualify_one(i, opt->bitflag, !not, -1);
595         }
596         copy = xstrdup(s);
597         for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
598                 if (opt->qualify(p, opt->bitflag, not)) {
599                         error_msg_and_die("invalid %s '%s'",
600                                 opt->argument_name, p);
601                 }
602         }
603         free(copy);
604         return;
605 }
606
607 #ifdef SYS_socket_subcall
608 static void
609 decode_socket_subcall(struct tcb *tcp)
610 {
611         const int call = tcp->u_arg[0];
612
613         if (call < 1 || call >= SYS_socket_nsubcalls)
614                 return;
615
616         const unsigned long scno = SYS_socket_subcall + call;
617         const unsigned int nargs = sysent[scno].nargs;
618         uint64_t buf[nargs];
619
620         if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
621                 return;
622
623         tcp->scno = scno;
624         tcp->qual_flg = qual_flags[scno];
625         tcp->s_ent = &sysent[scno];
626
627         unsigned int i;
628         for (i = 0; i < nargs; ++i)
629                 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
630                                 ? ((uint32_t *) (void *) buf)[i] : buf[i];
631 }
632 #endif
633
634 #ifdef SYS_ipc_subcall
635 static void
636 decode_ipc_subcall(struct tcb *tcp)
637 {
638         unsigned int call = tcp->u_arg[0];
639         const unsigned int version = call >> 16;
640
641         if (version) {
642 # if defined S390 || defined S390X
643                 return;
644 # else
645 #  ifdef SPARC64
646                 if (current_wordsize == 8)
647                         return;
648 #  endif
649                 set_tcb_priv_ulong(tcp, version);
650                 call &= 0xffff;
651 # endif
652         }
653
654         switch (call) {
655                 case  1: case  2: case  3: case  4:
656                 case 11: case 12: case 13: case 14:
657                 case 21: case 22: case 23: case 24:
658                         break;
659                 default:
660                         return;
661         }
662
663         tcp->scno = SYS_ipc_subcall + call;
664         tcp->qual_flg = qual_flags[tcp->scno];
665         tcp->s_ent = &sysent[tcp->scno];
666
667         const unsigned int n = tcp->s_ent->nargs;
668         unsigned int i;
669         for (i = 0; i < n; i++)
670                 tcp->u_arg[i] = tcp->u_arg[i + 1];
671 }
672 #endif
673
674 #ifdef LINUX_MIPSO32
675 static void
676 decode_mips_subcall(struct tcb *tcp)
677 {
678         if (!SCNO_IS_VALID(tcp->u_arg[0]))
679                 return;
680         tcp->scno = tcp->u_arg[0];
681         tcp->qual_flg = qual_flags[tcp->scno];
682         tcp->s_ent = &sysent[tcp->scno];
683         memmove(&tcp->u_arg[0], &tcp->u_arg[1],
684                 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
685         /*
686          * Fetching the last arg of 7-arg syscalls (fadvise64_64
687          * and sync_file_range) requires additional code,
688          * see linux/mips/get_syscall_args.c
689          */
690         if (tcp->s_ent->nargs == MAX_ARGS) {
691                 if (umoven(tcp,
692                            mips_REG_SP + MAX_ARGS * sizeof(tcp->u_arg[0]),
693                            sizeof(tcp->u_arg[0]),
694                            &tcp->u_arg[MAX_ARGS - 1]) < 0)
695                 tcp->u_arg[MAX_ARGS - 1] = 0;
696         }
697 }
698 #endif /* LINUX_MIPSO32 */
699
700 static void
701 dumpio(struct tcb *tcp)
702 {
703         int sen;
704
705         if (syserror(tcp))
706                 return;
707         if ((unsigned long) tcp->u_arg[0] >= num_quals)
708                 return;
709         sen = tcp->s_ent->sen;
710         if (SEN_printargs == sen)
711                 return;
712         if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
713                 switch (sen) {
714                 case SEN_read:
715                 case SEN_pread:
716                 case SEN_recv:
717                 case SEN_recvfrom:
718                 case SEN_mq_timedreceive:
719                         dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
720                         return;
721                 case SEN_readv:
722                 case SEN_preadv:
723                 case SEN_preadv2:
724                         dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
725                                      tcp->u_rval);
726                         return;
727                 case SEN_recvmsg:
728                         dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
729                         return;
730                 case SEN_recvmmsg:
731                         dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
732                         return;
733                 }
734         }
735         if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
736                 switch (sen) {
737                 case SEN_write:
738                 case SEN_pwrite:
739                 case SEN_send:
740                 case SEN_sendto:
741                 case SEN_mq_timedsend:
742                         dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
743                         break;
744                 case SEN_writev:
745                 case SEN_pwritev:
746                 case SEN_pwritev2:
747                 case SEN_vmsplice:
748                         dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
749                         break;
750                 case SEN_sendmsg:
751                         dumpiov_in_msghdr(tcp, tcp->u_arg[1],
752                                           (unsigned long) -1L);
753                         break;
754                 case SEN_sendmmsg:
755                         dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
756                         break;
757                 }
758         }
759 }
760
761 /*
762  * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
763  * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
764  */
765 #if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
766 static long
767 shuffle_scno(unsigned long scno)
768 {
769         if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
770                 return scno;
771
772         /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
773         if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
774                 return 0x000ffff0;
775         if (scno == 0x000ffff0)
776                 return ARM_FIRST_SHUFFLED_SYSCALL;
777
778 #define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
779         /*
780          * Is it ARM specific syscall?
781          * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
782          * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
783          */
784         if (scno >= 0x000f0000 &&
785             scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
786                 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
787         }
788         if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
789                 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
790         }
791
792         return scno;
793 }
794 #else
795 # define shuffle_scno(scno) ((long)(scno))
796 #endif
797
798 const char *
799 syscall_name(long scno)
800 {
801         static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
802
803         if (SCNO_IS_VALID(scno))
804                 return sysent[scno].sys_name;
805         else {
806                 sprintf(buf, "syscall_%lu", scno);
807                 return buf;
808         }
809 }
810
811 const char *
812 err_name(unsigned long err)
813 {
814         if ((err < nerrnos) && errnoent[err])
815                 return errnoent[err];
816
817         return NULL;
818 }
819
820 static long get_regs_error;
821
822 void
823 clear_regs(void)
824 {
825         get_regs_error = -1;
826 }
827
828 static int get_syscall_args(struct tcb *);
829 static int get_syscall_result(struct tcb *);
830 static int arch_get_scno(struct tcb *tcp);
831 static void get_error(struct tcb *, const bool);
832
833 static int
834 trace_syscall_entering(struct tcb *tcp)
835 {
836         int res, scno_good;
837
838         scno_good = res = get_scno(tcp);
839         if (res == 0)
840                 return res;
841         if (res == 1)
842                 res = get_syscall_args(tcp);
843
844         if (res != 1) {
845                 printleader(tcp);
846                 tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
847                 /*
848                  * " <unavailable>" will be added later by the code which
849                  * detects ptrace errors.
850                  */
851                 goto ret;
852         }
853
854 #ifdef LINUX_MIPSO32
855         if (SEN_syscall == tcp->s_ent->sen)
856                 decode_mips_subcall(tcp);
857 #endif
858
859         if (   SEN_execve == tcp->s_ent->sen
860 # if defined(SPARC) || defined(SPARC64)
861             || SEN_execv == tcp->s_ent->sen
862 # endif
863            ) {
864                 hide_log_until_execve = 0;
865         }
866
867 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
868         switch (tcp->s_ent->sen) {
869 # ifdef SYS_socket_subcall
870                 case SEN_socketcall:
871                         decode_socket_subcall(tcp);
872                         break;
873 # endif
874 # ifdef SYS_ipc_subcall
875                 case SEN_ipc:
876                         decode_ipc_subcall(tcp);
877                         break;
878 # endif
879         }
880 #endif
881
882         if (!(tcp->qual_flg & QUAL_TRACE)
883          || (tracing_paths && !pathtrace_match(tcp))
884         ) {
885                 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
886                 tcp->sys_func_rval = 0;
887                 return 0;
888         }
889
890         tcp->flags &= ~TCB_FILTERED;
891
892         if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
893                 res = 0;
894                 goto ret;
895         }
896
897 #ifdef USE_LIBUNWIND
898         if (stack_trace_enabled) {
899                 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
900                         unwind_capture_stacktrace(tcp);
901         }
902 #endif
903
904         printleader(tcp);
905         tprintf("%s(", tcp->s_ent->sys_name);
906         if ((tcp->qual_flg & QUAL_RAW) && SEN_exit != tcp->s_ent->sen)
907                 res = printargs(tcp);
908         else
909                 res = tcp->s_ent->sys_func(tcp);
910
911         fflush(tcp->outf);
912  ret:
913         tcp->flags |= TCB_INSYSCALL;
914         tcp->sys_func_rval = res;
915         /* Measure the entrance time as late as possible to avoid errors. */
916         if (Tflag || cflag)
917                 gettimeofday(&tcp->etime, NULL);
918         return res;
919 }
920
921 static int
922 trace_syscall_exiting(struct tcb *tcp)
923 {
924         int sys_res;
925         struct timeval tv;
926         int res;
927         unsigned long u_error;
928         const char *u_error_str;
929
930         /* Measure the exit time as early as possible to avoid errors. */
931         if (Tflag || cflag)
932                 gettimeofday(&tv, NULL);
933
934 #ifdef USE_LIBUNWIND
935         if (stack_trace_enabled) {
936                 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
937                         unwind_cache_invalidate(tcp);
938         }
939 #endif
940
941 #if SUPPORTED_PERSONALITIES > 1
942         update_personality(tcp, tcp->currpers);
943 #endif
944         res = (get_regs_error ? -1 : get_syscall_result(tcp));
945         if (filtered(tcp) || hide_log_until_execve)
946                 goto ret;
947
948         if (cflag) {
949                 count_syscall(tcp, &tv);
950                 if (cflag == CFLAG_ONLY_STATS) {
951                         goto ret;
952                 }
953         }
954
955         /* If not in -ff mode, and printing_tcp != tcp,
956          * then the log currently does not end with output
957          * of _our syscall entry_, but with something else.
958          * We need to say which syscall's return is this.
959          *
960          * Forced reprinting via TCB_REPRINT is used only by
961          * "strace -ff -oLOG test/threaded_execve" corner case.
962          * It's the only case when -ff mode needs reprinting.
963          */
964         if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
965                 tcp->flags &= ~TCB_REPRINT;
966                 printleader(tcp);
967                 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
968         }
969         printing_tcp = tcp;
970
971         tcp->s_prev_ent = NULL;
972         if (res != 1) {
973                 /* There was error in one of prior ptrace ops */
974                 tprints(") ");
975                 tabto();
976                 tprints("= ? <unavailable>\n");
977                 line_ended();
978                 tcp->flags &= ~TCB_INSYSCALL;
979                 tcp->sys_func_rval = 0;
980                 free_tcb_priv_data(tcp);
981                 return res;
982         }
983         tcp->s_prev_ent = tcp->s_ent;
984
985         sys_res = 0;
986         if (tcp->qual_flg & QUAL_RAW) {
987                 /* sys_res = printargs(tcp); - but it's nop on sysexit */
988         } else {
989         /* FIXME: not_failing_only (IOW, option -z) is broken:
990          * failure of syscall is known only after syscall return.
991          * Thus we end up with something like this on, say, ENOENT:
992          *     open("doesnt_exist", O_RDONLY <unfinished ...>
993          *     {next syscall decode}
994          * whereas the intended result is that open(...) line
995          * is not shown at all.
996          */
997                 if (not_failing_only && tcp->u_error)
998                         goto ret;       /* ignore failed syscalls */
999                 if (tcp->sys_func_rval & RVAL_DECODED)
1000                         sys_res = tcp->sys_func_rval;
1001                 else
1002                         sys_res = tcp->s_ent->sys_func(tcp);
1003         }
1004
1005         tprints(") ");
1006         tabto();
1007         u_error = tcp->u_error;
1008         if (tcp->qual_flg & QUAL_RAW) {
1009                 if (u_error)
1010                         tprintf("= -1 (errno %lu)", u_error);
1011                 else
1012                         tprintf("= %#lx", tcp->u_rval);
1013         }
1014         else if (!(sys_res & RVAL_NONE) && u_error) {
1015                 switch (u_error) {
1016                 /* Blocked signals do not interrupt any syscalls.
1017                  * In this case syscalls don't return ERESTARTfoo codes.
1018                  *
1019                  * Deadly signals set to SIG_DFL interrupt syscalls
1020                  * and kill the process regardless of which of the codes below
1021                  * is returned by the interrupted syscall.
1022                  * In some cases, kernel forces a kernel-generated deadly
1023                  * signal to be unblocked and set to SIG_DFL (and thus cause
1024                  * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
1025                  * or SIGILL. (The alternative is to leave process spinning
1026                  * forever on the faulty instruction - not useful).
1027                  *
1028                  * SIG_IGNed signals and non-deadly signals set to SIG_DFL
1029                  * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
1030                  * but kernel will always restart them.
1031                  */
1032                 case ERESTARTSYS:
1033                         /* Most common type of signal-interrupted syscall exit code.
1034                          * The system call will be restarted with the same arguments
1035                          * if SA_RESTART is set; otherwise, it will fail with EINTR.
1036                          */
1037                         tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
1038                         break;
1039                 case ERESTARTNOINTR:
1040                         /* Rare. For example, fork() returns this if interrupted.
1041                          * SA_RESTART is ignored (assumed set): the restart is unconditional.
1042                          */
1043                         tprints("= ? ERESTARTNOINTR (To be restarted)");
1044                         break;
1045                 case ERESTARTNOHAND:
1046                         /* pause(), rt_sigsuspend() etc use this code.
1047                          * SA_RESTART is ignored (assumed not set):
1048                          * syscall won't restart (will return EINTR instead)
1049                          * even after signal with SA_RESTART set. However,
1050                          * after SIG_IGN or SIG_DFL signal it will restart
1051                          * (thus the name "restart only if has no handler").
1052                          */
1053                         tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
1054                         break;
1055                 case ERESTART_RESTARTBLOCK:
1056                         /* Syscalls like nanosleep(), poll() which can't be
1057                          * restarted with their original arguments use this
1058                          * code. Kernel will execute restart_syscall() instead,
1059                          * which changes arguments before restarting syscall.
1060                          * SA_RESTART is ignored (assumed not set) similarly
1061                          * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
1062                          * since restart data is saved in "restart block"
1063                          * in task struct, and if signal handler uses a syscall
1064                          * which in turn saves another such restart block,
1065                          * old data is lost and restart becomes impossible)
1066                          */
1067                         tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
1068                         break;
1069                 default:
1070                         u_error_str = err_name(u_error);
1071                         if (u_error_str)
1072                                 tprintf("= -1 %s (%s)",
1073                                         u_error_str, strerror(u_error));
1074                         else
1075                                 tprintf("= -1 %lu (%s)",
1076                                         u_error, strerror(u_error));
1077                         break;
1078                 }
1079                 if ((sys_res & RVAL_STR) && tcp->auxstr)
1080                         tprintf(" (%s)", tcp->auxstr);
1081         }
1082         else {
1083                 if (sys_res & RVAL_NONE)
1084                         tprints("= ?");
1085                 else {
1086                         switch (sys_res & RVAL_MASK) {
1087                         case RVAL_HEX:
1088 #if SUPPORTED_PERSONALITIES > 1
1089                                 if (current_wordsize < sizeof(long))
1090                                         tprintf("= %#x",
1091                                                 (unsigned int) tcp->u_rval);
1092                                 else
1093 #endif
1094                                         tprintf("= %#lx", tcp->u_rval);
1095                                 break;
1096                         case RVAL_OCTAL:
1097                                 tprints("= ");
1098                                 print_numeric_long_umask(tcp->u_rval);
1099                                 break;
1100                         case RVAL_UDECIMAL:
1101 #if SUPPORTED_PERSONALITIES > 1
1102                                 if (current_wordsize < sizeof(long))
1103                                         tprintf("= %u",
1104                                                 (unsigned int) tcp->u_rval);
1105                                 else
1106 #endif
1107                                         tprintf("= %lu", tcp->u_rval);
1108                                 break;
1109                         case RVAL_DECIMAL:
1110                                 tprintf("= %ld", tcp->u_rval);
1111                                 break;
1112                         case RVAL_FD:
1113                                 if (show_fd_path) {
1114                                         tprints("= ");
1115                                         printfd(tcp, tcp->u_rval);
1116                                 }
1117                                 else
1118                                         tprintf("= %ld", tcp->u_rval);
1119                                 break;
1120 #if HAVE_STRUCT_TCB_EXT_ARG
1121                         /*
1122                         case RVAL_LHEX:
1123                                 tprintf("= %#llx", tcp->u_lrval);
1124                                 break;
1125                         case RVAL_LOCTAL:
1126                                 tprintf("= %#llo", tcp->u_lrval);
1127                                 break;
1128                         */
1129                         case RVAL_LUDECIMAL:
1130                                 tprintf("= %llu", tcp->u_lrval);
1131                                 break;
1132                         /*
1133                         case RVAL_LDECIMAL:
1134                                 tprintf("= %lld", tcp->u_lrval);
1135                                 break;
1136                         */
1137 #endif /* HAVE_STRUCT_TCB_EXT_ARG */
1138                         default:
1139                                 error_msg("invalid rval format");
1140                                 break;
1141                         }
1142                 }
1143                 if ((sys_res & RVAL_STR) && tcp->auxstr)
1144                         tprintf(" (%s)", tcp->auxstr);
1145         }
1146         if (Tflag) {
1147                 tv_sub(&tv, &tv, &tcp->etime);
1148                 tprintf(" <%ld.%06ld>",
1149                         (long) tv.tv_sec, (long) tv.tv_usec);
1150         }
1151         tprints("\n");
1152         dumpio(tcp);
1153         line_ended();
1154
1155 #ifdef USE_LIBUNWIND
1156         if (stack_trace_enabled)
1157                 unwind_print_stacktrace(tcp);
1158 #endif
1159
1160  ret:
1161         tcp->flags &= ~TCB_INSYSCALL;
1162         tcp->sys_func_rval = 0;
1163         free_tcb_priv_data(tcp);
1164         return 0;
1165 }
1166
1167 int
1168 trace_syscall(struct tcb *tcp)
1169 {
1170         return exiting(tcp) ?
1171                 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
1172 }
1173
1174 bool
1175 is_erestart(struct tcb *tcp)
1176 {
1177         switch (tcp->u_error) {
1178                 case ERESTARTSYS:
1179                 case ERESTARTNOINTR:
1180                 case ERESTARTNOHAND:
1181                 case ERESTART_RESTARTBLOCK:
1182                         return true;
1183                 default:
1184                         return false;
1185         }
1186 }
1187
1188 static unsigned long saved_u_error;
1189
1190 void
1191 temporarily_clear_syserror(struct tcb *tcp)
1192 {
1193         saved_u_error = tcp->u_error;
1194         tcp->u_error = 0;
1195 }
1196
1197 void
1198 restore_cleared_syserror(struct tcb *tcp)
1199 {
1200         tcp->u_error = saved_u_error;
1201 }
1202
1203 #include "kernel_types.h"
1204
1205 /*
1206  * Check the syscall return value register value for whether it is
1207  * a negated errno code indicating an error, or a success return value.
1208  */
1209 static inline bool
1210 is_negated_errno(kernel_ulong_t val)
1211 {
1212         /* Linux kernel defines MAX_ERRNO to 4095. */
1213         kernel_ulong_t max = -(kernel_long_t) 4095;
1214
1215 #if defined X86_64 || defined X32
1216         /*
1217          * current_wordsize is 4 for x32 personality
1218          * but truncation _must not_ be done in it, so
1219          * check current_personality instead.
1220          */
1221         if (current_personality == 1) {
1222                 val = (uint32_t) val;
1223                 max = (uint32_t) max;
1224         }
1225 #elif SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1226         if (current_wordsize < sizeof(val)) {
1227                 val = (uint32_t) val;
1228                 max = (uint32_t) max;
1229         }
1230 #endif
1231
1232         return val >= max;
1233 }
1234
1235 #include "arch_regs.c"
1236
1237 #ifdef HAVE_GETRVAL2
1238 # include "arch_getrval2.c"
1239 #endif
1240
1241 void
1242 print_pc(struct tcb *tcp)
1243 {
1244 #if defined ARCH_PC_REG
1245 # define ARCH_GET_PC 0
1246 #elif defined ARCH_PC_PEEK_ADDR
1247         long pc;
1248 # define ARCH_PC_REG pc
1249 # define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
1250 #else
1251 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1252 #endif
1253         if (get_regs_error || ARCH_GET_PC)
1254                 tprints(current_wordsize == 4 ? "[????????] "
1255                                               : "[????????????????] ");
1256         else
1257                 tprintf(current_wordsize == 4 ? "[%08lx] " : "[%016lx] ",
1258                         (unsigned long) ARCH_PC_REG);
1259 }
1260
1261 #include "getregs_old.h"
1262
1263 #undef ptrace_getregset_or_getregs
1264 #ifdef ARCH_REGS_FOR_GETREGSET
1265
1266 # define ptrace_getregset_or_getregs ptrace_getregset
1267 static long
1268 ptrace_getregset(pid_t pid)
1269 {
1270 # ifdef ARCH_IOVEC_FOR_GETREGSET
1271         /* variable iovec */
1272         ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1273         return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1274                       &ARCH_IOVEC_FOR_GETREGSET);
1275 # else
1276         /* constant iovec */
1277         static struct iovec io = {
1278                 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1279                 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1280         };
1281         return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1282
1283 # endif
1284 }
1285
1286 #elif defined ARCH_REGS_FOR_GETREGS
1287
1288 # define ptrace_getregset_or_getregs ptrace_getregs
1289 static long
1290 ptrace_getregs(pid_t pid)
1291 {
1292 # if defined SPARC || defined SPARC64
1293         /* SPARC systems have the meaning of data and addr reversed */
1294         return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1295 # else
1296         return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1297 # endif
1298 }
1299
1300 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1301
1302 void
1303 get_regs(pid_t pid)
1304 {
1305 #undef USE_GET_SYSCALL_RESULT_REGS
1306 #ifdef ptrace_getregset_or_getregs
1307
1308 # ifdef HAVE_GETREGS_OLD
1309         /*
1310          * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1311          * fallback to getregs_old.
1312          */
1313         static int use_getregs_old;
1314         if (use_getregs_old < 0) {
1315                 get_regs_error = ptrace_getregset_or_getregs(pid);
1316                 return;
1317         } else if (use_getregs_old == 0) {
1318                 get_regs_error = ptrace_getregset_or_getregs(pid);
1319                 if (get_regs_error >= 0) {
1320                         use_getregs_old = -1;
1321                         return;
1322                 }
1323                 if (errno == EPERM || errno == ESRCH)
1324                         return;
1325                 use_getregs_old = 1;
1326         }
1327         get_regs_error = getregs_old(pid);
1328 # else /* !HAVE_GETREGS_OLD */
1329         /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1330         get_regs_error = ptrace_getregset_or_getregs(pid);
1331 # endif /* !HAVE_GETREGS_OLD */
1332
1333 #else /* !ptrace_getregset_or_getregs */
1334
1335 # define USE_GET_SYSCALL_RESULT_REGS 1
1336 # warning get_regs is not implemented for this architecture yet
1337         get_regs_error = 0;
1338
1339 #endif /* !ptrace_getregset_or_getregs */
1340 }
1341
1342 struct sysent_buf {
1343         struct tcb *tcp;
1344         struct_sysent ent;
1345         char buf[sizeof("syscall_%lu") + sizeof(long) * 3];
1346 };
1347
1348 static void
1349 free_sysent_buf(void *ptr)
1350 {
1351         struct sysent_buf *s = ptr;
1352         s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1353         free(ptr);
1354 }
1355
1356 /*
1357  * Returns:
1358  * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1359  * 1: ok, continue in trace_syscall_entering().
1360  * other: error, trace_syscall_entering() should print error indicator
1361  *    ("????" etc) and bail out.
1362  */
1363 int
1364 get_scno(struct tcb *tcp)
1365 {
1366         if (get_regs_error)
1367                 return -1;
1368
1369         int rc = arch_get_scno(tcp);
1370         if (rc != 1)
1371                 return rc;
1372
1373         if (SCNO_IS_VALID(tcp->scno)) {
1374                 tcp->s_ent = &sysent[tcp->scno];
1375                 tcp->qual_flg = qual_flags[tcp->scno];
1376         } else {
1377                 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1378
1379                 s->tcp = tcp;
1380                 s->ent.nargs = MAX_ARGS;
1381                 s->ent.sen = SEN_printargs;
1382                 s->ent.sys_func = printargs;
1383                 s->ent.sys_name = s->buf;
1384                 sprintf(s->buf, "syscall_%lu", shuffle_scno(tcp->scno));
1385
1386                 tcp->s_ent = &s->ent;
1387                 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1388
1389                 set_tcb_priv_data(tcp, s, free_sysent_buf);
1390
1391                 if (debug_flag)
1392                         error_msg("pid %d invalid syscall %ld", tcp->pid, tcp->scno);
1393         }
1394         return 1;
1395 }
1396
1397 #ifdef USE_GET_SYSCALL_RESULT_REGS
1398 static int get_syscall_result_regs(struct tcb *);
1399 #endif
1400
1401 /* Returns:
1402  * 1: ok, continue in trace_syscall_exiting().
1403  * -1: error, trace_syscall_exiting() should print error indicator
1404  *    ("????" etc) and bail out.
1405  */
1406 static int
1407 get_syscall_result(struct tcb *tcp)
1408 {
1409 #ifdef USE_GET_SYSCALL_RESULT_REGS
1410         if (get_syscall_result_regs(tcp))
1411                 return -1;
1412 #endif
1413         tcp->u_error = 0;
1414         get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1415
1416         return 1;
1417 }
1418
1419 #include "get_scno.c"
1420 #include "get_syscall_args.c"
1421 #ifdef USE_GET_SYSCALL_RESULT_REGS
1422 # include "get_syscall_result.c"
1423 #endif
1424 #include "get_error.c"
1425 #ifdef HAVE_GETREGS_OLD
1426 # include "getregs_old.c"
1427 #endif