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