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(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
87 const struct_sysent sysent0[] = {
88 #include "syscallent.h"
91 #if SUPPORTED_PERSONALITIES > 1
92 # include PERSONALITY1_INCLUDE_FUNCS
93 static const struct_sysent sysent1[] = {
94 # include "syscallent1.h"
98 #if SUPPORTED_PERSONALITIES > 2
99 # include PERSONALITY2_INCLUDE_FUNCS
100 static const struct_sysent sysent2[] = {
101 # include "syscallent2.h"
105 /* Now undef them since short defines cause wicked namespace pollution. */
120 * `ioctlent[012].h' files are automatically generated by the auxiliary
121 * program `ioctlsort', such that the list is sorted by the `code' field.
122 * This has the side-effect of resolving the _IO.. macros into
123 * plain integers, eliminating the need to include here everything
127 const char *const errnoent0[] = {
128 #include "errnoent.h"
130 const char *const signalent0[] = {
131 #include "signalent.h"
133 const struct_ioctlent ioctlent0[] = {
134 #include "ioctlent0.h"
137 #if SUPPORTED_PERSONALITIES > 1
138 static const char *const errnoent1[] = {
139 # include "errnoent1.h"
141 static const char *const signalent1[] = {
142 # include "signalent1.h"
144 static const struct_ioctlent ioctlent1[] = {
145 # include "ioctlent1.h"
147 # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
148 static const struct_printers printers0 = {
149 # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
151 # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
152 static const struct_printers printers1 = {
153 # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
157 #if SUPPORTED_PERSONALITIES > 2
158 static const char *const errnoent2[] = {
159 # include "errnoent2.h"
161 static const char *const signalent2[] = {
162 # include "signalent2.h"
164 static const struct_ioctlent ioctlent2[] = {
165 # include "ioctlent2.h"
167 # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
168 static const struct_printers printers2 = {
169 # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
174 nsyscalls0 = ARRAY_SIZE(sysent0)
175 #if SUPPORTED_PERSONALITIES > 1
176 , nsyscalls1 = ARRAY_SIZE(sysent1)
177 # if SUPPORTED_PERSONALITIES > 2
178 , nsyscalls2 = ARRAY_SIZE(sysent2)
184 nerrnos0 = ARRAY_SIZE(errnoent0)
185 #if SUPPORTED_PERSONALITIES > 1
186 , nerrnos1 = ARRAY_SIZE(errnoent1)
187 # if SUPPORTED_PERSONALITIES > 2
188 , nerrnos2 = ARRAY_SIZE(errnoent2)
194 nsignals0 = ARRAY_SIZE(signalent0)
195 #if SUPPORTED_PERSONALITIES > 1
196 , nsignals1 = ARRAY_SIZE(signalent1)
197 # if SUPPORTED_PERSONALITIES > 2
198 , nsignals2 = ARRAY_SIZE(signalent2)
204 nioctlents0 = ARRAY_SIZE(ioctlent0)
205 #if SUPPORTED_PERSONALITIES > 1
206 , nioctlents1 = ARRAY_SIZE(ioctlent1)
207 # if SUPPORTED_PERSONALITIES > 2
208 , nioctlents2 = ARRAY_SIZE(ioctlent2)
213 #if SUPPORTED_PERSONALITIES > 1
214 const struct_sysent *sysent = sysent0;
215 const char *const *errnoent = errnoent0;
216 const char *const *signalent = signalent0;
217 const struct_ioctlent *ioctlent = ioctlent0;
218 const struct_printers *printers = &printers0;
221 unsigned nsyscalls = nsyscalls0;
222 unsigned nerrnos = nerrnos0;
223 unsigned nsignals = nsignals0;
224 unsigned nioctlents = nioctlents0;
227 qualbits_t *qual_vec[SUPPORTED_PERSONALITIES];
229 static const unsigned nsyscall_vec[SUPPORTED_PERSONALITIES] = {
231 #if SUPPORTED_PERSONALITIES > 1
234 #if SUPPORTED_PERSONALITIES > 2
238 static const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
240 #if SUPPORTED_PERSONALITIES > 1
243 #if SUPPORTED_PERSONALITIES > 2
249 MAX_NSYSCALLS1 = (nsyscalls0
250 #if SUPPORTED_PERSONALITIES > 1
251 > nsyscalls1 ? nsyscalls0 : nsyscalls1
254 MAX_NSYSCALLS2 = (MAX_NSYSCALLS1
255 #if SUPPORTED_PERSONALITIES > 2
256 > nsyscalls2 ? MAX_NSYSCALLS1 : nsyscalls2
259 MAX_NSYSCALLS = MAX_NSYSCALLS2,
260 /* We are ready for arches with up to 255 signals,
261 * even though the largest known signo is on MIPS and it is 128.
262 * The number of existing syscalls on all arches is
263 * larger that 255 anyway, so it is just a pedantic matter.
265 MIN_QUALS = MAX_NSYSCALLS > 255 ? MAX_NSYSCALLS : 255
268 #if SUPPORTED_PERSONALITIES > 1
269 unsigned current_personality;
271 # ifndef current_wordsize
272 unsigned current_wordsize;
273 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
274 PERSONALITY0_WORDSIZE,
275 PERSONALITY1_WORDSIZE,
276 # if SUPPORTED_PERSONALITIES > 2
277 PERSONALITY2_WORDSIZE,
283 set_personality(int personality)
285 nsyscalls = nsyscall_vec[personality];
286 sysent = sysent_vec[personality];
288 switch (personality) {
290 errnoent = errnoent0;
292 ioctlent = ioctlent0;
293 nioctlents = nioctlents0;
294 signalent = signalent0;
295 nsignals = nsignals0;
296 printers = &printers0;
300 errnoent = errnoent1;
302 ioctlent = ioctlent1;
303 nioctlents = nioctlents1;
304 signalent = signalent1;
305 nsignals = nsignals1;
306 printers = &printers1;
309 # if SUPPORTED_PERSONALITIES > 2
311 errnoent = errnoent2;
313 ioctlent = ioctlent2;
314 nioctlents = nioctlents2;
315 signalent = signalent2;
316 nsignals = nsignals2;
317 printers = &printers2;
322 current_personality = personality;
323 # ifndef current_wordsize
324 current_wordsize = personality_wordsize[personality];
329 update_personality(struct tcb *tcp, unsigned int personality)
331 if (personality == current_personality)
333 set_personality(personality);
335 if (personality == tcp->currpers)
337 tcp->currpers = personality;
339 # undef PERSONALITY_NAMES
340 # if defined POWERPC64
341 # define PERSONALITY_NAMES {"64 bit", "32 bit"}
342 # elif defined X86_64
343 # define PERSONALITY_NAMES {"64 bit", "32 bit", "x32"}
345 # define PERSONALITY_NAMES {"x32", "32 bit"}
346 # elif defined AARCH64
347 # define PERSONALITY_NAMES {"64 bit", "32 bit"}
349 # define PERSONALITY_NAMES {"64-bit", "32-bit"}
351 # ifdef PERSONALITY_NAMES
353 static const char *const names[] = PERSONALITY_NAMES;
354 error_msg("[ Process PID=%d runs in %s mode. ]",
355 tcp->pid, names[personality]);
361 static int qual_syscall(), qual_signal(), qual_desc();
363 static const struct qual_options {
364 unsigned int bitflag;
365 const char *option_name;
366 int (*qualify)(const char *, int, int);
367 const char *argument_name;
369 { QUAL_TRACE, "trace", qual_syscall, "system call" },
370 { QUAL_TRACE, "t", qual_syscall, "system call" },
371 { QUAL_ABBREV, "abbrev", qual_syscall, "system call" },
372 { QUAL_ABBREV, "a", qual_syscall, "system call" },
373 { QUAL_VERBOSE, "verbose", qual_syscall, "system call" },
374 { QUAL_VERBOSE, "v", qual_syscall, "system call" },
375 { QUAL_RAW, "raw", qual_syscall, "system call" },
376 { QUAL_RAW, "x", qual_syscall, "system call" },
377 { QUAL_SIGNAL, "signal", qual_signal, "signal" },
378 { QUAL_SIGNAL, "signals", qual_signal, "signal" },
379 { QUAL_SIGNAL, "s", qual_signal, "signal" },
380 { QUAL_READ, "read", qual_desc, "descriptor" },
381 { QUAL_READ, "reads", qual_desc, "descriptor" },
382 { QUAL_READ, "r", qual_desc, "descriptor" },
383 { QUAL_WRITE, "write", qual_desc, "descriptor" },
384 { QUAL_WRITE, "writes", qual_desc, "descriptor" },
385 { QUAL_WRITE, "w", qual_desc, "descriptor" },
386 { 0, NULL, NULL, NULL },
390 reallocate_qual(const unsigned int n)
394 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
395 qp = qual_vec[p] = xreallocarray(qual_vec[p], n,
397 memset(&qp[num_quals], 0, (n - num_quals) * sizeof(qualbits_t));
403 qualify_one(const unsigned int n, unsigned int bitflag, const int not, const int pers)
408 reallocate_qual(n + 1);
410 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
411 if (pers == p || pers < 0) {
413 qual_vec[p][n] &= ~bitflag;
415 qual_vec[p][n] |= bitflag;
421 qual_syscall(const char *s, const unsigned int bitflag, const int not)
427 if (*s >= '0' && *s <= '9') {
428 i = string_to_uint(s);
429 if (i >= MAX_NSYSCALLS)
431 qualify_one(i, bitflag, not, -1);
435 for (p = 0; p < SUPPORTED_PERSONALITIES; p++) {
436 for (i = 0; i < nsyscall_vec[p]; i++) {
437 if (sysent_vec[p][i].sys_name
438 && strcmp(s, sysent_vec[p][i].sys_name) == 0
440 qualify_one(i, bitflag, not, p);
450 qual_signal(const char *s, const unsigned int bitflag, const int not)
454 if (*s >= '0' && *s <= '9') {
455 int signo = string_to_uint(s);
456 if (signo < 0 || signo > 255)
458 qualify_one(signo, bitflag, not, -1);
461 if (strncasecmp(s, "SIG", 3) == 0)
463 for (i = 0; i <= NSIG; i++) {
464 if (strcasecmp(s, signame(i) + 3) == 0) {
465 qualify_one(i, bitflag, not, -1);
473 qual_desc(const char *s, const unsigned int bitflag, const int not)
475 if (*s >= '0' && *s <= '9') {
476 int desc = string_to_uint(s);
477 if (desc < 0 || desc > 0x7fff) /* paranoia */
479 qualify_one(desc, bitflag, not, -1);
486 lookup_class(const char *s)
488 if (strcmp(s, "file") == 0)
490 if (strcmp(s, "ipc") == 0)
492 if (strcmp(s, "network") == 0)
493 return TRACE_NETWORK;
494 if (strcmp(s, "process") == 0)
495 return TRACE_PROCESS;
496 if (strcmp(s, "signal") == 0)
498 if (strcmp(s, "desc") == 0)
500 if (strcmp(s, "memory") == 0)
506 qualify(const char *s)
508 const struct qual_options *opt;
515 reallocate_qual(MIN_QUALS);
517 opt = &qual_options[0];
518 for (i = 0; (p = qual_options[i].option_name); i++) {
519 unsigned int len = strlen(p);
520 if (strncmp(s, p, len) == 0 && s[len] == '=') {
521 opt = &qual_options[i];
531 if (strcmp(s, "none") == 0) {
535 if (strcmp(s, "all") == 0) {
536 for (i = 0; i < num_quals; i++) {
537 qualify_one(i, opt->bitflag, not, -1);
541 for (i = 0; i < num_quals; i++) {
542 qualify_one(i, opt->bitflag, !not, -1);
545 for (p = strtok(copy, ","); p; p = strtok(NULL, ",")) {
547 if (opt->bitflag == QUAL_TRACE && (n = lookup_class(p)) > 0) {
549 for (pers = 0; pers < SUPPORTED_PERSONALITIES; pers++) {
550 for (i = 0; i < nsyscall_vec[pers]; i++)
551 if (sysent_vec[pers][i].sys_flags & n)
552 qualify_one(i, opt->bitflag, not, pers);
556 if (opt->qualify(p, opt->bitflag, not)) {
557 error_msg_and_die("invalid %s '%s'",
558 opt->argument_name, p);
565 #ifdef SYS_socket_subcall
567 decode_socket_subcall(struct tcb *tcp)
572 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_socket_nsubcalls)
575 tcp->scno = SYS_socket_subcall + tcp->u_arg[0];
576 tcp->qual_flg = qual_flags[tcp->scno];
577 tcp->s_ent = &sysent[tcp->scno];
578 addr = tcp->u_arg[1];
579 n = tcp->s_ent->nargs;
580 if (sizeof(tcp->u_arg[0]) == current_wordsize) {
581 memset(tcp->u_arg, 0, n * sizeof(tcp->u_arg[0]));
582 (void) umoven(tcp, addr, n * sizeof(tcp->u_arg[0]), tcp->u_arg);
584 unsigned int args[n];
587 memset(args, 0, sizeof(args));
588 (void) umove(tcp, addr, &args);
589 for (i = 0; i < n; ++i)
590 tcp->u_arg[i] = args[i];
595 #ifdef SYS_ipc_subcall
597 decode_ipc_subcall(struct tcb *tcp)
601 if (tcp->u_arg[0] < 0 || tcp->u_arg[0] >= SYS_ipc_nsubcalls)
604 tcp->scno = SYS_ipc_subcall + tcp->u_arg[0];
605 tcp->qual_flg = qual_flags[tcp->scno];
606 tcp->s_ent = &sysent[tcp->scno];
607 n = tcp->s_ent->nargs;
608 for (i = 0; i < n; i++)
609 tcp->u_arg[i] = tcp->u_arg[i + 1];
615 decode_mips_subcall(struct tcb *tcp)
617 if (!SCNO_IS_VALID(tcp->u_arg[0]))
619 tcp->scno = tcp->u_arg[0];
620 tcp->qual_flg = qual_flags[tcp->scno];
621 tcp->s_ent = &sysent[tcp->scno];
622 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
623 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
625 * Fetching the last arg of 7-arg syscalls (fadvise64_64
626 * and sync_file_range) would require additional code,
627 * see linux/mips/get_syscall_args.c
633 return printargs(tcp);
638 printargs(struct tcb *tcp)
642 int n = tcp->s_ent->nargs;
643 for (i = 0; i < n; i++)
644 tprintf("%s%#lx", i ? ", " : "", tcp->u_arg[i]);
650 printargs_lu(struct tcb *tcp)
654 int n = tcp->s_ent->nargs;
655 for (i = 0; i < n; i++)
656 tprintf("%s%lu", i ? ", " : "", tcp->u_arg[i]);
662 printargs_ld(struct tcb *tcp)
666 int n = tcp->s_ent->nargs;
667 for (i = 0; i < n; i++)
668 tprintf("%s%ld", i ? ", " : "", tcp->u_arg[i]);
674 dumpio(struct tcb *tcp)
680 if ((unsigned long) tcp->u_arg[0] >= num_quals)
682 sen = tcp->s_ent->sen;
683 if (SEN_printargs == sen)
685 if (qual_flags[tcp->u_arg[0]] & QUAL_READ) {
691 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
694 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
697 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
700 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
704 if (qual_flags[tcp->u_arg[0]] & QUAL_WRITE) {
710 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
713 dumpiov(tcp, tcp->u_arg[2], tcp->u_arg[1]);
716 dumpiov_in_msghdr(tcp, tcp->u_arg[1]);
719 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
726 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
727 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
729 #if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
731 shuffle_scno(unsigned long scno)
733 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
736 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
737 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
739 if (scno == 0x000ffff0)
740 return ARM_FIRST_SHUFFLED_SYSCALL;
742 #define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
744 * Is it ARM specific syscall?
745 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
746 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
748 if (scno >= 0x000f0000 &&
749 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
750 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
752 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
753 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
759 # define shuffle_scno(scno) ((long)(scno))
763 syscall_name(long scno)
765 static char buf[sizeof("syscall_%lu") + sizeof(long)*3];
767 if (SCNO_IS_VALID(scno))
768 return sysent[scno].sys_name;
770 sprintf(buf, "syscall_%lu", shuffle_scno(scno));
775 static long get_regs_error;
783 static int get_syscall_args(struct tcb *);
784 static int get_syscall_result(struct tcb *);
785 static int arch_get_scno(struct tcb *tcp);
786 static void get_error(struct tcb *, const bool);
787 #if defined X86_64 || defined POWERPC
788 static int getregs_old(pid_t);
792 trace_syscall_entering(struct tcb *tcp)
796 scno_good = res = get_scno(tcp);
800 res = get_syscall_args(tcp);
805 tprints("????" /* anti-trigraph gap */ "(");
806 else if (tcp->qual_flg & UNDEFINED_SCNO)
807 tprintf("%s(", syscall_name(tcp->scno));
809 tprintf("%s(", tcp->s_ent->sys_name);
811 * " <unavailable>" will be added later by the code which
812 * detects ptrace errors.
818 if (SEN_syscall == tcp->s_ent->sen)
819 decode_mips_subcall(tcp);
822 if ( SEN_execve == tcp->s_ent->sen
823 # if defined(SPARC) || defined(SPARC64)
824 || SEN_execv == tcp->s_ent->sen
827 hide_log_until_execve = 0;
830 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
831 switch (tcp->s_ent->sen) {
832 # ifdef SYS_socket_subcall
834 decode_socket_subcall(tcp);
837 # ifdef SYS_ipc_subcall
839 decode_ipc_subcall(tcp);
845 if (!(tcp->qual_flg & QUAL_TRACE)
846 || (tracing_paths && !pathtrace_match(tcp))
848 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
849 tcp->sys_func_rval = 0;
853 tcp->flags &= ~TCB_FILTERED;
855 if (cflag == CFLAG_ONLY_STATS || hide_log_until_execve) {
861 if (stack_trace_enabled) {
862 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
863 unwind_capture_stacktrace(tcp);
868 if (tcp->qual_flg & UNDEFINED_SCNO)
869 tprintf("%s(", syscall_name(tcp->scno));
871 tprintf("%s(", tcp->s_ent->sys_name);
872 if ((tcp->qual_flg & QUAL_RAW) && SEN_exit != tcp->s_ent->sen)
873 res = printargs(tcp);
875 res = tcp->s_ent->sys_func(tcp);
879 tcp->flags |= TCB_INSYSCALL;
880 tcp->sys_func_rval = res;
881 /* Measure the entrance time as late as possible to avoid errors. */
883 gettimeofday(&tcp->etime, NULL);
888 trace_syscall_exiting(struct tcb *tcp)
895 /* Measure the exit time as early as possible to avoid errors. */
897 gettimeofday(&tv, NULL);
900 if (stack_trace_enabled) {
901 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
902 unwind_cache_invalidate(tcp);
906 #if SUPPORTED_PERSONALITIES > 1
907 update_personality(tcp, tcp->currpers);
909 res = (get_regs_error ? -1 : get_syscall_result(tcp));
910 if (filtered(tcp) || hide_log_until_execve)
914 count_syscall(tcp, &tv);
915 if (cflag == CFLAG_ONLY_STATS) {
920 /* If not in -ff mode, and printing_tcp != tcp,
921 * then the log currently does not end with output
922 * of _our syscall entry_, but with something else.
923 * We need to say which syscall's return is this.
925 * Forced reprinting via TCB_REPRINT is used only by
926 * "strace -ff -oLOG test/threaded_execve" corner case.
927 * It's the only case when -ff mode needs reprinting.
929 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
930 tcp->flags &= ~TCB_REPRINT;
932 if (tcp->qual_flg & UNDEFINED_SCNO)
933 tprintf("<... %s resumed> ", syscall_name(tcp->scno));
935 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
939 tcp->s_prev_ent = NULL;
941 /* There was error in one of prior ptrace ops */
944 tprints("= ? <unavailable>\n");
946 tcp->flags &= ~TCB_INSYSCALL;
947 tcp->sys_func_rval = 0;
950 tcp->s_prev_ent = tcp->s_ent;
953 if (tcp->qual_flg & QUAL_RAW) {
954 /* sys_res = printargs(tcp); - but it's nop on sysexit */
956 /* FIXME: not_failing_only (IOW, option -z) is broken:
957 * failure of syscall is known only after syscall return.
958 * Thus we end up with something like this on, say, ENOENT:
959 * open("doesnt_exist", O_RDONLY <unfinished ...>
960 * {next syscall decode}
961 * whereas the intended result is that open(...) line
962 * is not shown at all.
964 if (not_failing_only && tcp->u_error)
965 goto ret; /* ignore failed syscalls */
966 if (tcp->sys_func_rval & RVAL_DECODED)
967 sys_res = tcp->sys_func_rval;
969 sys_res = tcp->s_ent->sys_func(tcp);
974 u_error = tcp->u_error;
975 if (tcp->qual_flg & QUAL_RAW) {
977 tprintf("= -1 (errno %ld)", u_error);
979 tprintf("= %#lx", tcp->u_rval);
981 else if (!(sys_res & RVAL_NONE) && u_error) {
983 /* Blocked signals do not interrupt any syscalls.
984 * In this case syscalls don't return ERESTARTfoo codes.
986 * Deadly signals set to SIG_DFL interrupt syscalls
987 * and kill the process regardless of which of the codes below
988 * is returned by the interrupted syscall.
989 * In some cases, kernel forces a kernel-generated deadly
990 * signal to be unblocked and set to SIG_DFL (and thus cause
991 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
992 * or SIGILL. (The alternative is to leave process spinning
993 * forever on the faulty instruction - not useful).
995 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
996 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
997 * but kernel will always restart them.
1000 /* Most common type of signal-interrupted syscall exit code.
1001 * The system call will be restarted with the same arguments
1002 * if SA_RESTART is set; otherwise, it will fail with EINTR.
1004 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
1006 case ERESTARTNOINTR:
1007 /* Rare. For example, fork() returns this if interrupted.
1008 * SA_RESTART is ignored (assumed set): the restart is unconditional.
1010 tprints("= ? ERESTARTNOINTR (To be restarted)");
1012 case ERESTARTNOHAND:
1013 /* pause(), rt_sigsuspend() etc use this code.
1014 * SA_RESTART is ignored (assumed not set):
1015 * syscall won't restart (will return EINTR instead)
1016 * even after signal with SA_RESTART set. However,
1017 * after SIG_IGN or SIG_DFL signal it will restart
1018 * (thus the name "restart only if has no handler").
1020 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
1022 case ERESTART_RESTARTBLOCK:
1023 /* Syscalls like nanosleep(), poll() which can't be
1024 * restarted with their original arguments use this
1025 * code. Kernel will execute restart_syscall() instead,
1026 * which changes arguments before restarting syscall.
1027 * SA_RESTART is ignored (assumed not set) similarly
1028 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
1029 * since restart data is saved in "restart block"
1030 * in task struct, and if signal handler uses a syscall
1031 * which in turn saves another such restart block,
1032 * old data is lost and restart becomes impossible)
1034 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
1037 if ((unsigned long) u_error < nerrnos
1038 && errnoent[u_error])
1039 tprintf("= -1 %s (%s)", errnoent[u_error],
1042 tprintf("= -1 ERRNO_%lu (%s)", u_error,
1046 if ((sys_res & RVAL_STR) && tcp->auxstr)
1047 tprintf(" (%s)", tcp->auxstr);
1050 if (sys_res & RVAL_NONE)
1053 switch (sys_res & RVAL_MASK) {
1055 #if SUPPORTED_PERSONALITIES > 1
1056 if (current_wordsize < sizeof(long))
1058 (unsigned int) tcp->u_rval);
1061 tprintf("= %#lx", tcp->u_rval);
1064 tprintf("= %#lo", tcp->u_rval);
1067 tprintf("= %lu", tcp->u_rval);
1070 tprintf("= %ld", tcp->u_rval);
1075 printfd(tcp, tcp->u_rval);
1078 tprintf("= %ld", tcp->u_rval);
1080 #if defined(LINUX_MIPSN32) || defined(X32)
1083 tprintf("= %#llx", tcp->u_lrval);
1086 tprintf("= %#llo", tcp->u_lrval);
1089 case RVAL_LUDECIMAL:
1090 tprintf("= %llu", tcp->u_lrval);
1094 tprintf("= %lld", tcp->u_lrval);
1099 error_msg("invalid rval format");
1103 if ((sys_res & RVAL_STR) && tcp->auxstr)
1104 tprintf(" (%s)", tcp->auxstr);
1107 tv_sub(&tv, &tv, &tcp->etime);
1108 tprintf(" <%ld.%06ld>",
1109 (long) tv.tv_sec, (long) tv.tv_usec);
1115 #ifdef USE_LIBUNWIND
1116 if (stack_trace_enabled)
1117 unwind_print_stacktrace(tcp);
1121 tcp->flags &= ~TCB_INSYSCALL;
1122 tcp->sys_func_rval = 0;
1127 trace_syscall(struct tcb *tcp)
1129 return exiting(tcp) ?
1130 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp);
1134 is_erestart(struct tcb *tcp)
1136 switch (tcp->u_error) {
1138 case ERESTARTNOINTR:
1139 case ERESTARTNOHAND:
1140 case ERESTART_RESTARTBLOCK:
1147 static int saved_u_error;
1150 temporarily_clear_syserror(struct tcb *tcp)
1152 saved_u_error = tcp->u_error;
1157 restore_cleared_syserror(struct tcb *tcp)
1159 tcp->u_error = saved_u_error;
1162 #include "kernel_types.h"
1165 * Check the syscall return value register value for whether it is
1166 * a negated errno code indicating an error, or a success return value.
1169 is_negated_errno(kernel_ulong_t val)
1171 /* Linux kernel defines MAX_ERRNO to 4095. */
1172 kernel_ulong_t max = -(kernel_long_t) 4095;
1174 #if SUPPORTED_PERSONALITIES > 1 && SIZEOF_LONG > 4
1175 if (current_wordsize < sizeof(val)) {
1176 val = (uint32_t) val;
1177 max = (uint32_t) max;
1181 * current_wordsize is 4 even in personality 0 (native X32)
1182 * but truncation _must not_ be done in it.
1183 * can't check current_wordsize here!
1185 if (current_personality != 0) {
1186 val = (uint32_t) val;
1187 max = (uint32_t) max;
1194 #include "arch_regs.c"
1196 #ifdef HAVE_GETRVAL2
1197 # include "arch_getrval2.c"
1201 print_pc(struct tcb *tcp)
1203 #if defined ARCH_PC_REG
1204 # define ARCH_GET_PC 0
1205 #elif defined ARCH_PC_PEEK_ADDR
1207 # define ARCH_PC_REG pc
1208 # define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
1210 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1212 if (get_regs_error || ARCH_GET_PC)
1213 tprints(current_wordsize == 4 ? "[????????] "
1214 : "[????????????????] ");
1216 tprintf(current_wordsize == 4 ? "[%08lx] " : "[%016lx] ",
1217 (unsigned long) ARCH_PC_REG);
1220 #if defined ARCH_REGS_FOR_GETREGSET
1222 get_regset(pid_t pid)
1224 # ifdef ARCH_IOVEC_FOR_GETREGSET
1225 /* variable iovec */
1226 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1227 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1228 &ARCH_IOVEC_FOR_GETREGSET);
1230 /* constant iovec */
1231 static struct iovec io = {
1232 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1233 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1235 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1239 #endif /* ARCH_REGS_FOR_GETREGSET */
1244 #undef USE_GET_SYSCALL_RESULT_REGS
1245 #ifdef ARCH_REGS_FOR_GETREGSET
1247 /* Try PTRACE_GETREGSET first, fallback to PTRACE_GETREGS. */
1248 static int getregset_support;
1250 if (getregset_support >= 0) {
1251 get_regs_error = get_regset(pid);
1252 if (getregset_support > 0)
1254 if (get_regs_error >= 0) {
1255 getregset_support = 1;
1258 if (errno == EPERM || errno == ESRCH)
1260 getregset_support = -1;
1262 get_regs_error = getregs_old(pid);
1263 # else /* !X86_64 */
1264 /* Assume that PTRACE_GETREGSET works. */
1265 get_regs_error = get_regset(pid);
1267 #elif defined ARCH_REGS_FOR_GETREGS
1268 # if defined SPARC || defined SPARC64
1269 /* SPARC systems have the meaning of data and addr reversed */
1270 get_regs_error = ptrace(PTRACE_GETREGS, pid, (char *)&ARCH_REGS_FOR_GETREGS, 0);
1271 # elif defined POWERPC
1272 static bool old_kernel = 0;
1275 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1276 if (get_regs_error && errno == EIO) {
1279 get_regs_error = getregs_old(pid);
1282 /* Assume that PTRACE_GETREGS works. */
1283 get_regs_error = ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1286 #else /* !ARCH_REGS_FOR_GETREGSET && !ARCH_REGS_FOR_GETREGS */
1287 # define USE_GET_SYSCALL_RESULT_REGS 1
1288 # warning get_regs is not implemented for this architecture yet
1295 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1296 * 1: ok, continue in trace_syscall_entering().
1297 * other: error, trace_syscall_entering() should print error indicator
1298 * ("????" etc) and bail out.
1301 get_scno(struct tcb *tcp)
1306 int rc = arch_get_scno(tcp);
1310 if (SCNO_IS_VALID(tcp->scno)) {
1311 tcp->s_ent = &sysent[tcp->scno];
1312 tcp->qual_flg = qual_flags[tcp->scno];
1314 static const struct_sysent unknown = {
1317 .sys_func = printargs,
1318 .sys_name = "system call",
1320 tcp->s_ent = &unknown;
1321 tcp->qual_flg = UNDEFINED_SCNO | QUAL_RAW | DEFAULT_QUAL_FLAGS;
1323 error_msg("pid %d invalid syscall %ld", tcp->pid, tcp->scno);
1328 #ifdef USE_GET_SYSCALL_RESULT_REGS
1329 static int get_syscall_result_regs(struct tcb *);
1333 * 1: ok, continue in trace_syscall_exiting().
1334 * -1: error, trace_syscall_exiting() should print error indicator
1335 * ("????" etc) and bail out.
1338 get_syscall_result(struct tcb *tcp)
1340 #ifdef USE_GET_SYSCALL_RESULT_REGS
1341 if (get_syscall_result_regs(tcp))
1345 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1350 #include "get_scno.c"
1351 #include "get_syscall_args.c"
1352 #ifdef USE_GET_SYSCALL_RESULT_REGS
1353 # include "get_syscall_result.c"
1355 #include "get_error.c"
1356 #if defined X86_64 || defined POWERPC
1357 # include "getregs_old.c"