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>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
35 #include "native_defs.h"
36 #include <sys/param.h>
39 /* for struct iovec */
46 # undef PTRACE_GETREGS
47 # define PTRACE_GETREGS PTRACE_GETREGS64
48 # undef PTRACE_SETREGS
49 # define PTRACE_SETREGS PTRACE_SETREGS64
53 # include <asm/psrcompat.h>
63 # define NT_PRSTATUS 1
67 # warning: NSIG is not defined, using 32
73 /* Define these shorthand notations to simplify the syscallent files. */
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
83 #define SI STACKTRACE_INVALIDATE_CACHE
84 #define SE STACKTRACE_CAPTURE_ON_ENTER
86 #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
88 const struct_sysent sysent0[] = {
89 #include "syscallent.h"
92 #if SUPPORTED_PERSONALITIES > 1
93 # include PERSONALITY1_INCLUDE_FUNCS
94 static const struct_sysent sysent1[] = {
95 # include "syscallent1.h"
99 #if SUPPORTED_PERSONALITIES > 2
100 # include PERSONALITY2_INCLUDE_FUNCS
101 static const struct_sysent sysent2[] = {
102 # include "syscallent2.h"
106 /* Now undef them since short defines cause wicked namespace pollution. */
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
128 const char *const errnoent0[] = {
129 #include "errnoent.h"
131 const char *const signalent0[] = {
132 #include "signalent.h"
134 const struct_ioctlent ioctlent0[] = {
135 #include "ioctlent0.h"
138 #if SUPPORTED_PERSONALITIES > 1
139 static const char *const errnoent1[] = {
140 # include "errnoent1.h"
142 static const char *const signalent1[] = {
143 # include "signalent1.h"
145 static const struct_ioctlent ioctlent1[] = {
146 # include "ioctlent1.h"
148 # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
149 static const struct_printers printers0 = {
150 # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
152 # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
153 static const struct_printers printers1 = {
154 # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
158 #if SUPPORTED_PERSONALITIES > 2
159 static const char *const errnoent2[] = {
160 # include "errnoent2.h"
162 static const char *const signalent2[] = {
163 # include "signalent2.h"
165 static const struct_ioctlent ioctlent2[] = {
166 # include "ioctlent2.h"
168 # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
169 static const struct_printers printers2 = {
170 # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
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)
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)
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)
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)
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;
222 unsigned nsyscalls = nsyscalls0;
223 unsigned nerrnos = nerrnos0;
224 unsigned nsignals = nsignals0;
225 unsigned nioctlents = nioctlents0;
228 qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
230 static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
232 #if SUPPORTED_PERSONALITIES > 1
235 #if SUPPORTED_PERSONALITIES > 2
239 static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
241 #if SUPPORTED_PERSONALITIES > 1
244 #if SUPPORTED_PERSONALITIES > 2
250 MAX_NSYSCALLS1 = (nsyscalls0
251 #if SUPPORTED_PERSONALITIES > 1
252 > nsyscalls1 ? nsyscalls0 : nsyscalls1
255 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
256 #if SUPPORTED_PERSONALITIES > 2
257 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
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.
266 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
269 #if SUPPORTED_PERSONALITIES > 1
270 unsigned current_personality;
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,
284 set_personality(int personality)
286 nsyscalls = nsyscall_vec[personality];
287 sysent = sysent_vec[personality];
289 switch (personality) {
291 errnoent = errnoent0;
293 ioctlent = ioctlent0;
294 nioctlents = nioctlents0;
295 signalent = signalent0;
296 nsignals = nsignals0;
297 printers = &printers0;
301 errnoent = errnoent1;
303 ioctlent = ioctlent1;
304 nioctlents = nioctlents1;
305 signalent = signalent1;
306 nsignals = nsignals1;
307 printers = &printers1;
310 # if SUPPORTED_PERSONALITIES > 2
312 errnoent = errnoent2;
314 ioctlent = ioctlent2;
315 nioctlents = nioctlents2;
316 signalent = signalent2;
317 nsignals = nsignals2;
318 printers = &printers2;
323 current_personality = personality;
324 # ifndef current_wordsize
325 current_wordsize = personality_wordsize[personality];
330 update_personality(struct tcb *tcp, unsigned int personality)
332 if (personality == current_personality)
334 set_personality(personality);
336 if (personality == tcp->currpers)
338 tcp->currpers = personality;
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"}
346 # define PERSONALITY_NAMES {"x32", "32 bit"}
347 # elif defined AARCH64
348 # define PERSONALITY_NAMES {"64 bit", "32 bit"}
350 # define PERSONALITY_NAMES {"64-bit", "32-bit"}
352 # ifdef PERSONALITY_NAMES
354 static const char *const names[] = PERSONALITY_NAMES;
355 error_msg("[ Process PID=%d runs in %s mode. ]",
356 tcp->pid, names[personality]);
362 static int qual_syscall(), qual_signal(), qual_desc();
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;
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 },
391 reallocate_qual(const unsigned int n)
395 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
396 qp = qual_vec[p] = xreallocarray(qual_vec[p], n,
398 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
404 qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
409 reallocate_qual(n + 1);
411 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
412 if (pers == p || pers < 0) {
414 qual_vec[p][n] &= ~bitflag;
416 qual_vec[p][n] |= bitflag;
422 qual_syscall(const char *s, const unsigned int bitflag, const int not)
428 if (*s >= '0' && *s <= '9') {
429 i = string_to_uint(s);
430 if (i >= MAX_NSYSCALLS)
432 qualify_one(i, bitflag, not, -1);
436 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
437 for (i = 0; i < nsyscall_vec[p]; i++) {
438 if (sysent_vec[p][i].sys_name
439 && strcmp(s, sysent_vec[p][i].sys_name) == 0
441 qualify_one(i, bitflag, not, p);
451 qual_signal(const char *s, const unsigned int bitflag, const int not)
455 if (*s >= '0' && *s <= '9') {
456 int signo = string_to_uint(s);
457 if (signo < 0 || signo > 255)
459 qualify_one(signo, bitflag, not, -1);
462 if (strncasecmp(s, "SIG", 3) == 0)
464 for (i = 0; i <= NSIG; i++) {
465 if (strcasecmp(s, signame(i) + 3) == 0) {
466 qualify_one(i, bitflag, not, -1);
474 qual_desc(const char *s, const unsigned int bitflag, const int not)
476 if (*s >= '0' && *s <= '9') {
477 int desc = string_to_uint(s);
478 if (desc < 0 || desc > 0x7fff) /* paranoia */
480 qualify_one(desc, bitflag, not, -1);
487 lookup_class(const char *s)
489 if (strcmp(s, "file") == 0)
491 if (strcmp(s, "ipc") == 0)
493 if (strcmp(s, "network") == 0)
494 return TRACE_NETWORK;
495 if (strcmp(s, "process") == 0)
496 return TRACE_PROCESS;
497 if (strcmp(s, "signal") == 0)
499 if (strcmp(s, "desc") == 0)
501 if (strcmp(s, "memory") == 0)
507 qualify(const char *s)
509 const struct qual_options *opt;
516 reallocate_qual(MIN_QUALS);
518 opt = &qual_options[0];
519 for (i = 0; (p = qual_options[i].option_name); i++) {
520 unsigned int len = strlen(p);
521 if (strncmp(s, p, len) == 0 && s[len] == '=') {
522 opt = &qual_options[i];
532 if (strcmp(s, "none") == 0) {
536 if (strcmp(s, "all") == 0) {
537 for (i = 0; i < num_quals; i++) {
538 qualify_one(i, opt->bitflag, not, -1);
542 for (i = 0; i < num_quals; i++) {
543 qualify_one(i, opt->bitflag, !not, -1);
546 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
548 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
550 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
551 for (i = 0; i < nsyscall_vec[pers]; i++)
552 if (sysent_vec[pers][i].sys_flags & n)
553 qualify_one(i, opt->bitflag, not, pers);
557 if (opt->qualify(p, opt->bitflag, not)) {
558 error_msg_and_die("invalid %s '%s'",
559 opt->argument_name, p);
566 #ifdef SYS_socket_subcall
568 decode_socket_subcall(struct tcb *tcp)
573 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
576 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
577 tcp->qual_flg = qual_flags[tcp->scno];
578 tcp->s_ent = &sysent[tcp->scno];
579 addr = tcp->u_arg[1];
580 n = tcp->s_ent->nargs;
581 if (sizeof(tcp->u_arg[0]) == current_wordsize) {
582 memset(tcp->u_arg, 0, n * sizeof(tcp->u_arg[0]));
583 (void) umoven(tcp, addr, n * sizeof(tcp->u_arg[0]), tcp->u_arg);
585 unsigned int args[n];
588 memset(args, 0, sizeof(args));
589 (void) umove(tcp, addr, &args);
590 for (i = 0; i < n; ++i)
591 tcp->u_arg[i] = args[i];
596 #ifdef SYS_ipc_subcall
598 decode_ipc_subcall(struct tcb *tcp)
602 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
605 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
606 tcp->qual_flg = qual_flags[tcp->scno];
607 tcp->s_ent = &sysent[tcp->scno];
608 n = tcp->s_ent->nargs;
609 for (i = 0; i < n; i++)
610 tcp->u_arg[i] = tcp->u_arg[i + 1];
616 decode_mips_subcall(struct tcb *tcp)
618 if (!SCNO_IS_VALID(tcp->u_arg[0]))
620 tcp->scno = tcp->u_arg[0];
621 tcp->qual_flg = qual_flags[tcp->scno];
622 tcp->s_ent = &sysent[tcp->scno];
623 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
624 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
626 * Fetching the last arg of 7-arg syscalls (fadvise64_64
627 * and sync_file_range) would require additional code,
628 * see linux/mips/get_syscall_args.c
634 printargs(struct tcb *tcp)
638 int n = tcp->s_ent->nargs;
639 for (i = 0; i < n; i++)
640 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
646 printargs_lu(struct tcb *tcp)
650 int n = tcp->s_ent->nargs;
651 for (i = 0; i < n; i++)
652 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
658 printargs_ld(struct tcb *tcp)
662 int n = tcp->s_ent->nargs;
663 for (i = 0; i < n; i++)
664 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
670 dumpio(struct tcb *tcp)
676 if ((unsigned long) tcp->u_arg[0] >= num_quals)
678 sen = tcp->s_ent->sen;
679 if (SEN_printargs == sen)
681 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
687 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
690 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
694 dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
697 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
701 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
707 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
710 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
713 dumpiov_in_msghdr(tcp, tcp->u_arg[1],
714 (unsigned long) -1L);
717 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
724 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
725 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
727 #if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
729 shuffle_scno(unsigned long scno)
731 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
734 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
735 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
737 if (scno == 0x000ffff0)
738 return ARM_FIRST_SHUFFLED_SYSCALL;
740 #define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
742 * Is it ARM specific syscall?
743 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
744 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
746 if (scno >= 0x000f0000 &&
747 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
748 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
750 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
751 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
757 # define shuffle_scno(scno) ((long)(scno))
761 syscall_name(long scno)
763 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
765 if (SCNO_IS_VALID(scno))
766 return sysent[scno].sys_name;
768 sprintf(buf, "syscall_%lu", shuffle_scno(scno));
773 static long get_regs_error;
781 static int get_syscall_args(struct tcb *);
782 static int get_syscall_result(struct tcb *);
783 static int arch_get_scno(struct tcb *tcp);
784 static void get_error(struct tcb *, const bool);
785 #if defined X86_64 || defined POWERPC
786 static int getregs_old(pid_t);
790 trace_syscall_entering(struct tcb *tcp)
794 scno_good = res = get_scno(tcp);
798 res = get_syscall_args(tcp);
803 tprints("????" /* anti-trigraph gap */ "(");
804 else if (tcp->qual_flg & UNDEFINED_SCNO)
805 tprintf("%s(", syscall_name(tcp->scno));
807 tprintf("%s(", tcp->s_ent->sys_name);
809 * " <unavailable>" will be added later by the code which
810 * detects ptrace errors.
816 if (SEN_syscall == tcp->s_ent->sen)
817 decode_mips_subcall(tcp);
820 if ( SEN_execve == tcp->s_ent->sen
821 # if defined(SPARC) || defined(SPARC64)
822 || SEN_execv == tcp->s_ent->sen
825 hide_log_until_execve = 0;
828 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
829 switch (tcp->s_ent->sen) {
830 # ifdef SYS_socket_subcall
832 decode_socket_subcall(tcp);
835 # ifdef SYS_ipc_subcall
837 decode_ipc_subcall(tcp);
843 if (!(tcp->qual_flg & QUAL_TRACE)
844 || (tracing_paths && !pathtrace_match(tcp))
846 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
847 tcp->sys_func_rval = 0;
851 tcp->flags &= ~TCB_FILTERED;
853 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
859 if (stack_trace_enabled) {
860 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
861 unwind_capture_stacktrace(tcp);
866 if (tcp->qual_flg & UNDEFINED_SCNO)
867 tprintf("%s(", syscall_name(tcp->scno));
869 tprintf("%s(", tcp->s_ent->sys_name);
870 if ((tcp->qual_flg & QUAL_RAW) && SEN_exit != tcp->s_ent->sen)
871 res = printargs(tcp);
873 res = tcp->s_ent->sys_func(tcp);
877 tcp->flags |= TCB_INSYSCALL;
878 tcp->sys_func_rval = res;
879 /* Measure the entrance time as late as possible to avoid errors. */
881 gettimeofday(&tcp->etime, NULL);
886 trace_syscall_exiting(struct tcb *tcp)
893 /* Measure the exit time as early as possible to avoid errors. */
895 gettimeofday(&tv, NULL);
898 if (stack_trace_enabled) {
899 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
900 unwind_cache_invalidate(tcp);
904 #if SUPPORTED_PERSONALITIES > 1
905 update_personality(tcp, tcp->currpers);
907 res = (get_regs_error ? -1 : get_syscall_result(tcp));
908 if (filtered(tcp) || hide_log_until_execve)
912 count_syscall(tcp, &tv);
913 if (cflag == CFLAG_ONLY_STATS) {
918 /* If not in -ff mode, and printing_tcp != tcp,
919 * then the log currently does not end with output
920 * of _our syscall entry_, but with something else.
921 * We need to say which syscall's return is this.
923 * Forced reprinting via TCB_REPRINT is used only by
924 * "strace -ff -oLOG test/threaded_execve" corner case.
925 * It's the only case when -ff mode needs reprinting.
927 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
928 tcp->flags &= ~TCB_REPRINT;
930 if (tcp->qual_flg & UNDEFINED_SCNO)
931 tprintf("<... %s resumed> ", syscall_name(tcp->scno));
933 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
937 tcp->s_prev_ent = NULL;
939 /* There was error in one of prior ptrace ops */
942 tprints("= ? <unavailable>\n");
944 tcp->flags &= ~TCB_INSYSCALL;
945 tcp->sys_func_rval = 0;
948 tcp->s_prev_ent = tcp->s_ent;
951 if (tcp->qual_flg & QUAL_RAW) {
952 /* sys_res = printargs(tcp); - but it's nop on sysexit */
954 /* FIXME: not_failing_only (IOW, option -z) is broken:
955 * failure of syscall is known only after syscall return.
956 * Thus we end up with something like this on, say, ENOENT:
957 * open("doesnt_exist", O_RDONLY <unfinished ...>
958 * {next syscall decode}
959 * whereas the intended result is that open(...) line
960 * is not shown at all.
962 if (not_failing_only && tcp->u_error)
963 goto ret; /* ignore failed syscalls */
964 if (tcp->sys_func_rval & RVAL_DECODED)
965 sys_res = tcp->sys_func_rval;
967 sys_res = tcp->s_ent->sys_func(tcp);
972 u_error = tcp->u_error;
973 if (tcp->qual_flg & QUAL_RAW) {
975 tprintf("= -1 (errno %ld)", u_error);
977 tprintf("= %#lx", tcp->u_rval);
979 else if (!(sys_res & RVAL_NONE) && u_error) {
981 /* Blocked signals do not interrupt any syscalls.
982 * In this case syscalls don't return ERESTARTfoo codes.
984 * Deadly signals set to SIG_DFL interrupt syscalls
985 * and kill the process regardless of which of the codes below
986 * is returned by the interrupted syscall.
987 * In some cases, kernel forces a kernel-generated deadly
988 * signal to be unblocked and set to SIG_DFL (and thus cause
989 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
990 * or SIGILL. (The alternative is to leave process spinning
991 * forever on the faulty instruction - not useful).
993 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
994 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
995 * but kernel will always restart them.
998 /* Most common type of signal-interrupted syscall exit code.
999 * The system call will be restarted with the same arguments
1000 * if SA_RESTART is set; otherwise, it will fail with EINTR.
1002 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
1004 case ERESTARTNOINTR:
1005 /* Rare. For example, fork() returns this if interrupted.
1006 * SA_RESTART is ignored (assumed set): the restart is unconditional.
1008 tprints("= ? ERESTARTNOINTR (To be restarted)");
1010 case ERESTARTNOHAND:
1011 /* pause(), rt_sigsuspend() etc use this code.
1012 * SA_RESTART is ignored (assumed not set):
1013 * syscall won't restart (will return EINTR instead)
1014 * even after signal with SA_RESTART set. However,
1015 * after SIG_IGN or SIG_DFL signal it will restart
1016 * (thus the name "restart only if has no handler").
1018 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
1020 case ERESTART_RESTARTBLOCK:
1021 /* Syscalls like nanosleep(), poll() which can't be
1022 * restarted with their original arguments use this
1023 * code. Kernel will execute restart_syscall() instead,
1024 * which changes arguments before restarting syscall.
1025 * SA_RESTART is ignored (assumed not set) similarly
1026 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
1027 * since restart data is saved in "restart block"
1028 * in task struct, and if signal handler uses a syscall
1029 * which in turn saves another such restart block,
1030 * old data is lost and restart becomes impossible)
1032 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
1035 if ((unsigned long) u_error < nerrnos
1036 && errnoent[u_error])
1037 tprintf("= -1 %s (%s)", errnoent[u_error],
1040 tprintf("= -1 ERRNO_%lu (%s)", u_error,
1044 if ((sys_res & RVAL_STR) && tcp->auxstr)
1045 tprintf(" (%s)", tcp->auxstr);
1048 if (sys_res & RVAL_NONE)
1051 switch (sys_res & RVAL_MASK) {
1053 #if SUPPORTED_PERSONALITIES > 1
1054 if (current_wordsize < sizeof(long))
1056 (unsigned int) tcp->u_rval);
1059 tprintf("= %#lx", tcp->u_rval);
1062 tprintf("= %#lo", tcp->u_rval);
1065 #if SUPPORTED_PERSONALITIES > 1
1066 if (current_wordsize < sizeof(long))
1068 (unsigned int) tcp->u_rval);
1071 tprintf("= %lu", tcp->u_rval);
1074 tprintf("= %ld", tcp->u_rval);
1079 printfd(tcp, tcp->u_rval);
1082 tprintf("= %ld", tcp->u_rval);
1084 #if defined(LINUX_MIPSN32) || defined(X32)
1087 tprintf("= %#llx", tcp->u_lrval);
1090 tprintf("= %#llo", tcp->u_lrval);
1093 case RVAL_LUDECIMAL:
1094 tprintf("= %llu", tcp->u_lrval);
1098 tprintf("= %lld", tcp->u_lrval);
1103 error_msg("invalid rval format");
1107 if ((sys_res & RVAL_STR) && tcp->auxstr)
1108 tprintf(" (%s)", tcp->auxstr);
1111 tv_sub(&tv, &tv, &tcp->etime);
1112 tprintf(" <%ld.%06ld>",
1113 (long) tv.tv_sec, (long) tv.tv_usec);
1119 #ifdef USE_LIBUNWIND
1120 if (stack_trace_enabled)
1121 unwind_print_stacktrace(tcp);
1125 tcp->flags &= ~TCB_INSYSCALL;
1126 tcp->sys_func_rval = 0;
1131 trace_syscall(struct tcb *tcp)
1133 return exiting(tcp) ?
1134 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
1138 is_erestart(struct tcb *tcp)
1140 switch (tcp->u_error) {
1142 case ERESTARTNOINTR:
1143 case ERESTARTNOHAND:
1144 case ERESTART_RESTARTBLOCK:
1151 static int saved_u_error;
1154 temporarily_clear_syserror(struct tcb *tcp)
1156 saved_u_error = tcp->u_error;
1161 restore_cleared_syserror(struct tcb *tcp)
1163 tcp->u_error = saved_u_error;
1166 #include "kernel_types.h"
1169 * Check the syscall return value register value for whether it is
1170 * a negated errno code indicating an error, or a success return value.
1173 is_negated_errno(kernel_ulong_t val)
1175 /* Linux kernel defines MAX_ERRNO to 4095. */
1176 kernel_ulong_t max = -(kernel_long_t) 4095;
1178 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1179 if (current_wordsize < sizeof(val)) {
1180 val = (uint32_t) val;
1181 max = (uint32_t) max;
1185 * current_wordsize is 4 even in personality 0 (native X32)
1186 * but truncation _must not_ be done in it.
1187 * can't check current_wordsize here!
1189 if (current_personality != 0) {
1190 val = (uint32_t) val;
1191 max = (uint32_t) max;
1198 #include "arch_regs.c"
1200 #ifdef HAVE_GETRVAL2
1201 # include "arch_getrval2.c"
1205 print_pc(struct tcb *tcp)
1207 #if defined ARCH_PC_REG
1208 # define ARCH_GET_PC 0
1209 #elif defined ARCH_PC_PEEK_ADDR
1211 # define ARCH_PC_REG pc
1212 # define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
1214 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1216 if (get_regs_error || ARCH_GET_PC)
1217 tprints(current_wordsize == 4 ? "[????????] "
1218 : "[????????????????] ");
1220 tprintf(current_wordsize == 4 ? "[%08lx] " : "[%016lx] ",
1221 (unsigned long) ARCH_PC_REG);
1224 #if defined ARCH_REGS_FOR_GETREGSET
1226 get_regset(pid_t pid)
1228 # ifdef ARCH_IOVEC_FOR_GETREGSET
1229 /* variable iovec */
1230 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1231 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1232 &ARCH_IOVEC_FOR_GETREGSET);
1234 /* constant iovec */
1235 static struct iovec io = {
1236 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1237 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1239 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1243 #endif /* ARCH_REGS_FOR_GETREGSET */
1248 #undef USE_GET_SYSCALL_RESULT_REGS
1249 #ifdef ARCH_REGS_FOR_GETREGSET
1251 /* Try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS. */
1252 static int getregset_support;
1254 if (getregset_support >= 0) {
1255 get_regs_error = get_regset(pid);
1256 if (getregset_support > 0)
1258 if (get_regs_error >= 0) {
1259 getregset_support = 1;
1262 if (errno == EPERM || errno == ESRCH)
1264 getregset_support = -1;
1266 get_regs_error = getregs_old(pid);
1267 # else /* !X86_64 */
1268 /* Assume that PTRACE_GETREGSET works. */
1269 get_regs_error = get_regset(pid);
1271 #elif defined ARCH_REGS_FOR_GETREGS
1272 # if defined SPARC || defined SPARC64
1273 /* SPARC systems have the meaning of data and addr reversed */
1274 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&ARCH_REGS_FOR_GETREGS, 0);
1275 # elif defined POWERPC
1276 static bool old_kernel = 0;
1279 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1280 if (get_regs_error && errno == EIO) {
1283 get_regs_error = getregs_old(pid);
1286 /* Assume that PTRACE_GETREGS works. */
1287 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1290 #else /* !ARCH_REGS_FOR_GETREGSET && !ARCH_REGS_FOR_GETREGS */
1291 # define USE_GET_SYSCALL_RESULT_REGS 1
1292 # warning get_regs is not implemented for this architecture yet
1299 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1300 * 1: ok, continue in trace_syscall_entering().
1301 * other: error, trace_syscall_entering() should print error indicator
1302 * ("????" etc) and bail out.
1305 get_scno(struct tcb *tcp)
1310 int rc = arch_get_scno(tcp);
1314 if (SCNO_IS_VALID(tcp->scno)) {
1315 tcp->s_ent = &sysent[tcp->scno];
1316 tcp->qual_flg = qual_flags[tcp->scno];
1318 static const struct_sysent unknown = {
1321 .sys_func = printargs,
1322 .sys_name = "system call",
1324 tcp->s_ent = &unknown;
1325 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1327 error_msg("pid %d invalid syscall %ld", tcp->pid, tcp->scno);
1332 #ifdef USE_GET_SYSCALL_RESULT_REGS
1333 static int get_syscall_result_regs(struct tcb *);
1337 * 1: ok, continue in trace_syscall_exiting().
1338 * -1: error, trace_syscall_exiting() should print error indicator
1339 * ("????" etc) and bail out.
1342 get_syscall_result(struct tcb *tcp)
1344 #ifdef USE_GET_SYSCALL_RESULT_REGS
1345 if (get_syscall_result_regs(tcp))
1349 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1354 #include "get_scno.c"
1355 #include "get_syscall_args.c"
1356 #ifdef USE_GET_SYSCALL_RESULT_REGS
1357 # include "get_syscall_result.c"
1359 #include "get_error.c"
1360 #if defined X86_64 || defined POWERPC
1361 # include "getregs_old.c"