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>
38 /* for struct iovec */
45 # undef PTRACE_GETREGS
46 # define PTRACE_GETREGS PTRACE_GETREGS64
47 # undef PTRACE_SETREGS
48 # define PTRACE_SETREGS PTRACE_SETREGS64
52 # include <asm/psrcompat.h>
62 # define NT_PRSTATUS 1
66 # warning: NSIG is not defined, using 32
72 /* Define these shorthand notations to simplify the syscallent files. */
76 #define TN TRACE_NETWORK
77 #define TP TRACE_PROCESS
78 #define TS TRACE_SIGNAL
79 #define TM TRACE_MEMORY
80 #define NF SYSCALL_NEVER_FAILS
82 #define SI STACKTRACE_INVALIDATE_CACHE
83 #define SE STACKTRACE_CAPTURE_ON_ENTER
85 #define SEN_NAME(syscall_name) SEN_ ## syscall_name
86 #define SEN(syscall_name) SEN_NAME(syscall_name), SYS_FUNC_NAME(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. */
122 * `ioctlent[012].h' files are automatically generated by the auxiliary
123 * program `ioctlsort', such that the list is sorted by the `code' field.
124 * This has the side-effect of resolving the _IO.. macros into
125 * plain integers, eliminating the need to include here everything
129 const char *const errnoent0[] = {
130 #include "errnoent.h"
132 const char *const signalent0[] = {
133 #include "signalent.h"
135 const struct_ioctlent ioctlent0[] = {
136 #include "ioctlent0.h"
139 #if SUPPORTED_PERSONALITIES > 1
140 static const char *const errnoent1[] = {
141 # include "errnoent1.h"
143 static const char *const signalent1[] = {
144 # include "signalent1.h"
146 static const struct_ioctlent ioctlent1[] = {
147 # include "ioctlent1.h"
149 # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
150 static const struct_printers printers0 = {
151 # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
153 # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
154 static const struct_printers printers1 = {
155 # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
159 #if SUPPORTED_PERSONALITIES > 2
160 static const char *const errnoent2[] = {
161 # include "errnoent2.h"
163 static const char *const signalent2[] = {
164 # include "signalent2.h"
166 static const struct_ioctlent ioctlent2[] = {
167 # include "ioctlent2.h"
169 # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
170 static const struct_printers printers2 = {
171 # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
176 nsyscalls0 = ARRAY_SIZE(sysent0)
177 #if SUPPORTED_PERSONALITIES > 1
178 , nsyscalls1 = ARRAY_SIZE(sysent1)
179 # if SUPPORTED_PERSONALITIES > 2
180 , nsyscalls2 = ARRAY_SIZE(sysent2)
186 nerrnos0 = ARRAY_SIZE(errnoent0)
187 #if SUPPORTED_PERSONALITIES > 1
188 , nerrnos1 = ARRAY_SIZE(errnoent1)
189 # if SUPPORTED_PERSONALITIES > 2
190 , nerrnos2 = ARRAY_SIZE(errnoent2)
196 nsignals0 = ARRAY_SIZE(signalent0)
197 #if SUPPORTED_PERSONALITIES > 1
198 , nsignals1 = ARRAY_SIZE(signalent1)
199 # if SUPPORTED_PERSONALITIES > 2
200 , nsignals2 = ARRAY_SIZE(signalent2)
206 nioctlents0 = ARRAY_SIZE(ioctlent0)
207 #if SUPPORTED_PERSONALITIES > 1
208 , nioctlents1 = ARRAY_SIZE(ioctlent1)
209 # if SUPPORTED_PERSONALITIES > 2
210 , nioctlents2 = ARRAY_SIZE(ioctlent2)
215 #if SUPPORTED_PERSONALITIES > 1
216 const struct_sysent *sysent = sysent0;
217 const char *const *errnoent = errnoent0;
218 const char *const *signalent = signalent0;
219 const struct_ioctlent *ioctlent = ioctlent0;
220 const struct_printers *printers = &printers0;
223 unsigned nsyscalls = nsyscalls0;
224 unsigned nerrnos = nerrnos0;
225 unsigned nsignals = nsignals0;
226 unsigned nioctlents = nioctlents0;
229 qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
231 static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
233 #if SUPPORTED_PERSONALITIES > 1
236 #if SUPPORTED_PERSONALITIES > 2
240 static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
242 #if SUPPORTED_PERSONALITIES > 1
245 #if SUPPORTED_PERSONALITIES > 2
251 MAX_NSYSCALLS1 = (nsyscalls0
252 #if SUPPORTED_PERSONALITIES > 1
253 > nsyscalls1 ? nsyscalls0 : nsyscalls1
256 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
257 #if SUPPORTED_PERSONALITIES > 2
258 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
261 MAX_NSYSCALLS = MAX_NSYSCALLS2,
262 /* We are ready for arches with up to 255 signals,
263 * even though the largest known signo is on MIPS and it is 128.
264 * The number of existing syscalls on all arches is
265 * larger that 255 anyway, so it is just a pedantic matter.
267 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
270 #if SUPPORTED_PERSONALITIES > 1
271 unsigned current_personality;
273 # ifndef current_wordsize
274 unsigned current_wordsize;
275 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
276 PERSONALITY0_WORDSIZE,
277 PERSONALITY1_WORDSIZE,
278 # if SUPPORTED_PERSONALITIES > 2
279 PERSONALITY2_WORDSIZE,
285 set_personality(int personality)
287 nsyscalls = nsyscall_vec[personality];
288 sysent = sysent_vec[personality];
290 switch (personality) {
292 errnoent = errnoent0;
294 ioctlent = ioctlent0;
295 nioctlents = nioctlents0;
296 signalent = signalent0;
297 nsignals = nsignals0;
298 printers = &printers0;
302 errnoent = errnoent1;
304 ioctlent = ioctlent1;
305 nioctlents = nioctlents1;
306 signalent = signalent1;
307 nsignals = nsignals1;
308 printers = &printers1;
311 # if SUPPORTED_PERSONALITIES > 2
313 errnoent = errnoent2;
315 ioctlent = ioctlent2;
316 nioctlents = nioctlents2;
317 signalent = signalent2;
318 nsignals = nsignals2;
319 printers = &printers2;
324 current_personality = personality;
325 # ifndef current_wordsize
326 current_wordsize = personality_wordsize[personality];
331 update_personality(struct tcb *tcp, unsigned int personality)
333 if (personality == current_personality)
335 set_personality(personality);
337 if (personality == tcp->currpers)
339 tcp->currpers = personality;
341 # undef PERSONALITY_NAMES
342 # if defined POWERPC64
343 # define PERSONALITY_NAMES {"64 bit", "32 bit"}
344 # elif defined X86_64
345 # define PERSONALITY_NAMES {"64 bit", "32 bit", "x32"}
347 # define PERSONALITY_NAMES {"x32", "32 bit"}
348 # elif defined AARCH64
349 # define PERSONALITY_NAMES {"64 bit", "32 bit"}
351 # define PERSONALITY_NAMES {"64-bit", "32-bit"}
353 # ifdef PERSONALITY_NAMES
355 static const char *const names[] = PERSONALITY_NAMES;
356 error_msg("[ Process PID=%d runs in %s mode. ]",
357 tcp->pid, names[personality]);
363 static int qual_syscall(), qual_signal(), qual_desc();
365 static const struct qual_options {
366 unsigned int bitflag;
367 const char *option_name;
368 int (*qualify)(const char *, int, int);
369 const char *argument_name;
371 { QUAL_TRACE, "trace", qual_syscall, "system call" },
372 { QUAL_TRACE, "t", qual_syscall, "system call" },
373 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
374 { QUAL_ABBREV, "a", qual_syscall, "system call" },
375 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
376 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
377 { QUAL_RAW, "raw", qual_syscall, "system call" },
378 { QUAL_RAW, "x", qual_syscall, "system call" },
379 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
380 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
381 { QUAL_SIGNAL, "s", qual_signal, "signal" },
382 { QUAL_READ, "read", qual_desc, "descriptor" },
383 { QUAL_READ, "reads", qual_desc, "descriptor" },
384 { QUAL_READ, "r", qual_desc, "descriptor" },
385 { QUAL_WRITE, "write", qual_desc, "descriptor" },
386 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
387 { QUAL_WRITE, "w", qual_desc, "descriptor" },
388 { 0, NULL, NULL, NULL },
392 reallocate_qual(const unsigned int n)
396 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
397 qp = qual_vec[p] = xreallocarray(qual_vec[p], n,
399 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
405 qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
410 reallocate_qual(n + 1);
412 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
413 if (pers == p || pers < 0) {
415 qual_vec[p][n] &= ~bitflag;
417 qual_vec[p][n] |= bitflag;
423 qual_syscall(const char *s, const unsigned int bitflag, const int not)
429 if (*s >= '0' && *s <= '9') {
430 i = string_to_uint(s);
431 if (i >= MAX_NSYSCALLS)
433 qualify_one(i, bitflag, not, -1);
437 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
438 for (i = 0; i < nsyscall_vec[p]; i++) {
439 if (sysent_vec[p][i].sys_name
440 && strcmp(s, sysent_vec[p][i].sys_name) == 0
442 qualify_one(i, bitflag, not, p);
452 qual_signal(const char *s, const unsigned int bitflag, const int not)
456 if (*s >= '0' && *s <= '9') {
457 int signo = string_to_uint(s);
458 if (signo < 0 || signo > 255)
460 qualify_one(signo, bitflag, not, -1);
463 if (strncasecmp(s, "SIG", 3) == 0)
465 for (i = 0; i <= NSIG; i++) {
466 if (strcasecmp(s, signame(i) + 3) == 0) {
467 qualify_one(i, bitflag, not, -1);
475 qual_desc(const char *s, const unsigned int bitflag, const int not)
477 if (*s >= '0' && *s <= '9') {
478 int desc = string_to_uint(s);
479 if (desc < 0 || desc > 0x7fff) /* paranoia */
481 qualify_one(desc, bitflag, not, -1);
488 lookup_class(const char *s)
490 if (strcmp(s, "file") == 0)
492 if (strcmp(s, "ipc") == 0)
494 if (strcmp(s, "network") == 0)
495 return TRACE_NETWORK;
496 if (strcmp(s, "process") == 0)
497 return TRACE_PROCESS;
498 if (strcmp(s, "signal") == 0)
500 if (strcmp(s, "desc") == 0)
502 if (strcmp(s, "memory") == 0)
508 qualify(const char *s)
510 const struct qual_options *opt;
517 reallocate_qual(MIN_QUALS);
519 opt = &qual_options[0];
520 for (i = 0; (p = qual_options[i].option_name); i++) {
521 unsigned int len = strlen(p);
522 if (strncmp(s, p, len) == 0 && s[len] == '=') {
523 opt = &qual_options[i];
533 if (strcmp(s, "none") == 0) {
537 if (strcmp(s, "all") == 0) {
538 for (i = 0; i < num_quals; i++) {
539 qualify_one(i, opt->bitflag, not, -1);
543 for (i = 0; i < num_quals; i++) {
544 qualify_one(i, opt->bitflag, !not, -1);
547 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
549 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
551 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
552 for (i = 0; i < nsyscall_vec[pers]; i++)
553 if (sysent_vec[pers][i].sys_flags & n)
554 qualify_one(i, opt->bitflag, not, pers);
558 if (opt->qualify(p, opt->bitflag, not)) {
559 error_msg_and_die("invalid %s '%s'",
560 opt->argument_name, p);
567 #ifdef SYS_socket_subcall
569 decode_socket_subcall(struct tcb *tcp)
574 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
577 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
578 tcp->qual_flg = qual_flags[tcp->scno];
579 tcp->s_ent = &sysent[tcp->scno];
580 addr = tcp->u_arg[1];
581 n = tcp->s_ent->nargs;
582 if (sizeof(tcp->u_arg[0]) == current_wordsize) {
583 memset(tcp->u_arg, 0, n * sizeof(tcp->u_arg[0]));
584 (void) umoven(tcp, addr, n * sizeof(tcp->u_arg[0]), tcp->u_arg);
586 unsigned int args[n];
589 memset(args, 0, sizeof(args));
590 (void) umove(tcp, addr, &args);
591 for (i = 0; i < n; ++i)
592 tcp->u_arg[i] = args[i];
597 #ifdef SYS_ipc_subcall
599 decode_ipc_subcall(struct tcb *tcp)
603 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
606 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
607 tcp->qual_flg = qual_flags[tcp->scno];
608 tcp->s_ent = &sysent[tcp->scno];
609 n = tcp->s_ent->nargs;
610 for (i = 0; i < n; i++)
611 tcp->u_arg[i] = tcp->u_arg[i + 1];
617 decode_mips_subcall(struct tcb *tcp)
619 if (!SCNO_IS_VALID(tcp->u_arg[0]))
621 tcp->scno = tcp->u_arg[0];
622 tcp->qual_flg = qual_flags[tcp->scno];
623 tcp->s_ent = &sysent[tcp->scno];
624 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
625 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
627 * Fetching the last arg of 7-arg syscalls (fadvise64_64
628 * and sync_file_range) would require additional code,
629 * see linux/mips/get_syscall_args.c
635 return printargs(tcp);
640 printargs(struct tcb *tcp)
644 int n = tcp->s_ent->nargs;
645 for (i = 0; i < n; i++)
646 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
652 printargs_lu(struct tcb *tcp)
656 int n = tcp->s_ent->nargs;
657 for (i = 0; i < n; i++)
658 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
664 printargs_ld(struct tcb *tcp)
668 int n = tcp->s_ent->nargs;
669 for (i = 0; i < n; i++)
670 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
676 dumpio(struct tcb *tcp)
682 if ((unsigned long) tcp->u_arg[0] >= num_quals)
684 sen = tcp->s_ent->sen;
685 if (SEN_printargs == sen)
687 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
693 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
696 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
699 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
702 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
706 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
712 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
715 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
718 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
721 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
728 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
729 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
731 #if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
733 shuffle_scno(unsigned long scno)
735 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
738 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
739 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
741 if (scno == 0x000ffff0)
742 return ARM_FIRST_SHUFFLED_SYSCALL;
744 #define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
746 * Is it ARM specific syscall?
747 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
748 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
750 if (scno >= 0x000f0000 &&
751 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
752 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
754 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
755 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
761 # define shuffle_scno(scno) ((long)(scno))
765 syscall_name(long scno)
767 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
769 if (SCNO_IS_VALID(scno))
770 return sysent[scno].sys_name;
772 sprintf(buf, "syscall_%lu", shuffle_scno(scno));
777 static long get_regs_error;
785 static int get_syscall_args(struct tcb *);
786 static int get_syscall_result(struct tcb *);
787 static int arch_get_scno(struct tcb *tcp);
788 static void get_error(struct tcb *, const bool);
789 #if defined X86_64 || defined POWERPC
790 static int getregs_old(pid_t);
794 trace_syscall_entering(struct tcb *tcp)
798 scno_good = res = get_scno(tcp);
802 res = get_syscall_args(tcp);
807 tprints("????" /* anti-trigraph gap */ "(");
808 else if (tcp->qual_flg & UNDEFINED_SCNO)
809 tprintf("%s(", syscall_name(tcp->scno));
811 tprintf("%s(", tcp->s_ent->sys_name);
813 * " <unavailable>" will be added later by the code which
814 * detects ptrace errors.
820 if (SEN_syscall == tcp->s_ent->sen)
821 decode_mips_subcall(tcp);
824 if ( SEN_execve == tcp->s_ent->sen
825 # if defined(SPARC) || defined(SPARC64)
826 || SEN_execv == tcp->s_ent->sen
829 hide_log_until_execve = 0;
832 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
833 switch (tcp->s_ent->sen) {
834 # ifdef SYS_socket_subcall
836 decode_socket_subcall(tcp);
839 # ifdef SYS_ipc_subcall
841 decode_ipc_subcall(tcp);
847 if (!(tcp->qual_flg & QUAL_TRACE)
848 || (tracing_paths && !pathtrace_match(tcp))
850 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
851 tcp->sys_func_rval = 0;
855 tcp->flags &= ~TCB_FILTERED;
857 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
863 if (stack_trace_enabled) {
864 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
865 unwind_capture_stacktrace(tcp);
870 if (tcp->qual_flg & UNDEFINED_SCNO)
871 tprintf("%s(", syscall_name(tcp->scno));
873 tprintf("%s(", tcp->s_ent->sys_name);
874 if ((tcp->qual_flg & QUAL_RAW) && SEN_exit != tcp->s_ent->sen)
875 res = printargs(tcp);
877 res = tcp->s_ent->sys_func(tcp);
881 tcp->flags |= TCB_INSYSCALL;
882 tcp->sys_func_rval = res;
883 /* Measure the entrance time as late as possible to avoid errors. */
885 gettimeofday(&tcp->etime, NULL);
890 trace_syscall_exiting(struct tcb *tcp)
897 /* Measure the exit time as early as possible to avoid errors. */
899 gettimeofday(&tv, NULL);
902 if (stack_trace_enabled) {
903 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
904 unwind_cache_invalidate(tcp);
908 #if SUPPORTED_PERSONALITIES > 1
909 update_personality(tcp, tcp->currpers);
911 res = (get_regs_error ? -1 : get_syscall_result(tcp));
912 if (filtered(tcp) || hide_log_until_execve)
916 count_syscall(tcp, &tv);
917 if (cflag == CFLAG_ONLY_STATS) {
922 /* If not in -ff mode, and printing_tcp != tcp,
923 * then the log currently does not end with output
924 * of _our syscall entry_, but with something else.
925 * We need to say which syscall's return is this.
927 * Forced reprinting via TCB_REPRINT is used only by
928 * "strace -ff -oLOG test/threaded_execve" corner case.
929 * It's the only case when -ff mode needs reprinting.
931 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
932 tcp->flags &= ~TCB_REPRINT;
934 if (tcp->qual_flg & UNDEFINED_SCNO)
935 tprintf("<... %s resumed> ", syscall_name(tcp->scno));
937 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
941 tcp->s_prev_ent = NULL;
943 /* There was error in one of prior ptrace ops */
946 tprints("= ? <unavailable>\n");
948 tcp->flags &= ~TCB_INSYSCALL;
949 tcp->sys_func_rval = 0;
952 tcp->s_prev_ent = tcp->s_ent;
955 if (tcp->qual_flg & QUAL_RAW) {
956 /* sys_res = printargs(tcp); - but it's nop on sysexit */
958 /* FIXME: not_failing_only (IOW, option -z) is broken:
959 * failure of syscall is known only after syscall return.
960 * Thus we end up with something like this on, say, ENOENT:
961 * open("doesnt_exist", O_RDONLY <unfinished ...>
962 * {next syscall decode}
963 * whereas the intended result is that open(...) line
964 * is not shown at all.
966 if (not_failing_only && tcp->u_error)
967 goto ret; /* ignore failed syscalls */
968 if (tcp->sys_func_rval & RVAL_DECODED)
969 sys_res = tcp->sys_func_rval;
971 sys_res = tcp->s_ent->sys_func(tcp);
976 u_error = tcp->u_error;
977 if (tcp->qual_flg & QUAL_RAW) {
979 tprintf("= -1 (errno %ld)", u_error);
981 tprintf("= %#lx", tcp->u_rval);
983 else if (!(sys_res & RVAL_NONE) && u_error) {
985 /* Blocked signals do not interrupt any syscalls.
986 * In this case syscalls don't return ERESTARTfoo codes.
988 * Deadly signals set to SIG_DFL interrupt syscalls
989 * and kill the process regardless of which of the codes below
990 * is returned by the interrupted syscall.
991 * In some cases, kernel forces a kernel-generated deadly
992 * signal to be unblocked and set to SIG_DFL (and thus cause
993 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
994 * or SIGILL. (The alternative is to leave process spinning
995 * forever on the faulty instruction - not useful).
997 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
998 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
999 * but kernel will always restart them.
1002 /* Most common type of signal-interrupted syscall exit code.
1003 * The system call will be restarted with the same arguments
1004 * if SA_RESTART is set; otherwise, it will fail with EINTR.
1006 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
1008 case ERESTARTNOINTR:
1009 /* Rare. For example, fork() returns this if interrupted.
1010 * SA_RESTART is ignored (assumed set): the restart is unconditional.
1012 tprints("= ? ERESTARTNOINTR (To be restarted)");
1014 case ERESTARTNOHAND:
1015 /* pause(), rt_sigsuspend() etc use this code.
1016 * SA_RESTART is ignored (assumed not set):
1017 * syscall won't restart (will return EINTR instead)
1018 * even after signal with SA_RESTART set. However,
1019 * after SIG_IGN or SIG_DFL signal it will restart
1020 * (thus the name "restart only if has no handler").
1022 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
1024 case ERESTART_RESTARTBLOCK:
1025 /* Syscalls like nanosleep(), poll() which can't be
1026 * restarted with their original arguments use this
1027 * code. Kernel will execute restart_syscall() instead,
1028 * which changes arguments before restarting syscall.
1029 * SA_RESTART is ignored (assumed not set) similarly
1030 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
1031 * since restart data is saved in "restart block"
1032 * in task struct, and if signal handler uses a syscall
1033 * which in turn saves another such restart block,
1034 * old data is lost and restart becomes impossible)
1036 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
1039 if ((unsigned long) u_error < nerrnos
1040 && errnoent[u_error])
1041 tprintf("= -1 %s (%s)", errnoent[u_error],
1044 tprintf("= -1 ERRNO_%lu (%s)", u_error,
1048 if ((sys_res & RVAL_STR) && tcp->auxstr)
1049 tprintf(" (%s)", tcp->auxstr);
1052 if (sys_res & RVAL_NONE)
1055 switch (sys_res & RVAL_MASK) {
1057 #if SUPPORTED_PERSONALITIES > 1
1058 if (current_wordsize < sizeof(long))
1060 (unsigned int) tcp->u_rval);
1063 tprintf("= %#lx", tcp->u_rval);
1066 tprintf("= %#lo", tcp->u_rval);
1069 tprintf("= %lu", tcp->u_rval);
1072 tprintf("= %ld", tcp->u_rval);
1077 printfd(tcp, tcp->u_rval);
1080 tprintf("= %ld", tcp->u_rval);
1082 #if defined(LINUX_MIPSN32) || defined(X32)
1085 tprintf("= %#llx", tcp->u_lrval);
1088 tprintf("= %#llo", tcp->u_lrval);
1091 case RVAL_LUDECIMAL:
1092 tprintf("= %llu", tcp->u_lrval);
1096 tprintf("= %lld", tcp->u_lrval);
1101 error_msg("invalid rval format");
1105 if ((sys_res & RVAL_STR) && tcp->auxstr)
1106 tprintf(" (%s)", tcp->auxstr);
1109 tv_sub(&tv, &tv, &tcp->etime);
1110 tprintf(" <%ld.%06ld>",
1111 (long) tv.tv_sec, (long) tv.tv_usec);
1117 #ifdef USE_LIBUNWIND
1118 if (stack_trace_enabled)
1119 unwind_print_stacktrace(tcp);
1123 tcp->flags &= ~TCB_INSYSCALL;
1124 tcp->sys_func_rval = 0;
1129 trace_syscall(struct tcb *tcp)
1131 return exiting(tcp) ?
1132 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
1136 is_erestart(struct tcb *tcp)
1138 switch (tcp->u_error) {
1140 case ERESTARTNOINTR:
1141 case ERESTARTNOHAND:
1142 case ERESTART_RESTARTBLOCK:
1149 static int saved_u_error;
1152 temporarily_clear_syserror(struct tcb *tcp)
1154 saved_u_error = tcp->u_error;
1159 restore_cleared_syserror(struct tcb *tcp)
1161 tcp->u_error = saved_u_error;
1164 #include "kernel_types.h"
1167 * Check the syscall return value register value for whether it is
1168 * a negated errno code indicating an error, or a success return value.
1171 is_negated_errno(kernel_ulong_t val)
1173 /* Linux kernel defines MAX_ERRNO to 4095. */
1174 kernel_ulong_t max = -(kernel_long_t) 4095;
1176 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1177 if (current_wordsize < sizeof(val)) {
1178 val = (uint32_t) val;
1179 max = (uint32_t) max;
1183 * current_wordsize is 4 even in personality 0 (native X32)
1184 * but truncation _must not_ be done in it.
1185 * can't check current_wordsize here!
1187 if (current_personality != 0) {
1188 val = (uint32_t) val;
1189 max = (uint32_t) max;
1196 #include "arch_regs.c"
1198 #ifdef HAVE_GETRVAL2
1199 # include "arch_getrval2.c"
1203 print_pc(struct tcb *tcp)
1205 #if defined ARCH_PC_REG
1206 # define ARCH_GET_PC 0
1207 #elif defined ARCH_PC_PEEK_ADDR
1209 # define ARCH_PC_REG pc
1210 # define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
1212 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1214 if (get_regs_error || ARCH_GET_PC)
1215 tprints(current_wordsize == 4 ? "[????????] "
1216 : "[????????????????] ");
1218 tprintf(current_wordsize == 4 ? "[%08lx] " : "[%016lx] ",
1219 (unsigned long) ARCH_PC_REG);
1222 #if defined ARCH_REGS_FOR_GETREGSET
1224 get_regset(pid_t pid)
1226 # ifdef ARCH_IOVEC_FOR_GETREGSET
1227 /* variable iovec */
1228 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1229 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1230 &ARCH_IOVEC_FOR_GETREGSET);
1232 /* constant iovec */
1233 static struct iovec io = {
1234 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1235 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1237 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1241 #endif /* ARCH_REGS_FOR_GETREGSET */
1246 #undef USE_GET_SYSCALL_RESULT_REGS
1247 #ifdef ARCH_REGS_FOR_GETREGSET
1249 /* Try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS. */
1250 static int getregset_support;
1252 if (getregset_support >= 0) {
1253 get_regs_error = get_regset(pid);
1254 if (getregset_support > 0)
1256 if (get_regs_error >= 0) {
1257 getregset_support = 1;
1260 if (errno == EPERM || errno == ESRCH)
1262 getregset_support = -1;
1264 get_regs_error = getregs_old(pid);
1265 # else /* !X86_64 */
1266 /* Assume that PTRACE_GETREGSET works. */
1267 get_regs_error = get_regset(pid);
1269 #elif defined ARCH_REGS_FOR_GETREGS
1270 # if defined SPARC || defined SPARC64
1271 /* SPARC systems have the meaning of data and addr reversed */
1272 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&ARCH_REGS_FOR_GETREGS, 0);
1273 # elif defined POWERPC
1274 static bool old_kernel = 0;
1277 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1278 if (get_regs_error && errno == EIO) {
1281 get_regs_error = getregs_old(pid);
1284 /* Assume that PTRACE_GETREGS works. */
1285 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
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
1297 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1298 * 1: ok, continue in trace_syscall_entering().
1299 * other: error, trace_syscall_entering() should print error indicator
1300 * ("????" etc) and bail out.
1303 get_scno(struct tcb *tcp)
1308 int rc = arch_get_scno(tcp);
1312 if (SCNO_IS_VALID(tcp->scno)) {
1313 tcp->s_ent = &sysent[tcp->scno];
1314 tcp->qual_flg = qual_flags[tcp->scno];
1316 static const struct_sysent unknown = {
1319 .sys_func = printargs,
1320 .sys_name = "system call",
1322 tcp->s_ent = &unknown;
1323 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1325 error_msg("pid %d invalid syscall %ld", tcp->pid, tcp->scno);
1330 #ifdef USE_GET_SYSCALL_RESULT_REGS
1331 static int get_syscall_result_regs(struct tcb *);
1335 * 1: ok, continue in trace_syscall_exiting().
1336 * -1: error, trace_syscall_exiting() should print error indicator
1337 * ("????" etc) and bail out.
1340 get_syscall_result(struct tcb *tcp)
1342 #ifdef USE_GET_SYSCALL_RESULT_REGS
1343 if (get_syscall_result_regs(tcp))
1347 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1352 #include "get_scno.c"
1353 #include "get_syscall_args.c"
1354 #ifdef USE_GET_SYSCALL_RESULT_REGS
1355 # include "get_syscall_result.c"
1357 #include "get_error.c"
1358 #if defined X86_64 || defined POWERPC
1359 # include "getregs_old.c"