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 * Copyright (c) 1999-2017 The strace developers.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include "native_defs.h"
38 #include <sys/param.h>
40 /* for struct iovec */
43 /* for __X32_SYSCALL_BIT */
44 #include <asm/unistd.h>
50 # undef PTRACE_GETREGS
51 # define PTRACE_GETREGS PTRACE_GETREGS64
52 # undef PTRACE_SETREGS
53 # define PTRACE_SETREGS PTRACE_SETREGS64
57 # include <asm/psrcompat.h>
67 # define NT_PRSTATUS 1
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 TST TRACE_STAT
81 #define TLST TRACE_LSTAT
82 #define TFST TRACE_FSTAT
83 #define TSTA TRACE_STAT_LIKE
84 #define TSF TRACE_STATFS
85 #define TFSF TRACE_FSTATFS
86 #define TSFA TRACE_STATFS_LIKE
87 #define NF SYSCALL_NEVER_FAILS
89 #define SI STACKTRACE_INVALIDATE_CACHE
90 #define SE STACKTRACE_CAPTURE_ON_ENTER
91 #define CST COMPAT_SYSCALL_TYPES
93 #define SEN(syscall_name) SEN_ ## syscall_name, SYS_FUNC_NAME(sys_ ## syscall_name)
95 const struct_sysent sysent0[] = {
96 #include "syscallent.h"
99 #if SUPPORTED_PERSONALITIES > 1
100 # include PERSONALITY1_INCLUDE_FUNCS
101 static const struct_sysent sysent1[] = {
102 # include "syscallent1.h"
106 #if SUPPORTED_PERSONALITIES > 2
107 # include PERSONALITY2_INCLUDE_FUNCS
108 static const struct_sysent sysent2[] = {
109 # include "syscallent2.h"
113 /* Now undef them since short defines cause wicked namespace pollution. */
136 * `ioctlent[012].h' files are automatically generated by the auxiliary
137 * program `ioctlsort', such that the list is sorted by the `code' field.
138 * This has the side-effect of resolving the _IO.. macros into
139 * plain integers, eliminating the need to include here everything
143 const char *const errnoent0[] = {
144 #include "errnoent.h"
146 const char *const signalent0[] = {
147 #include "signalent.h"
149 const struct_ioctlent ioctlent0[] = {
150 #include "ioctlent0.h"
153 #if SUPPORTED_PERSONALITIES > 1
154 static const char *const errnoent1[] = {
155 # include "errnoent1.h"
157 static const char *const signalent1[] = {
158 # include "signalent1.h"
160 static const struct_ioctlent ioctlent1[] = {
161 # include "ioctlent1.h"
163 # include PERSONALITY0_INCLUDE_PRINTERS_DECLS
164 static const struct_printers printers0 = {
165 # include PERSONALITY0_INCLUDE_PRINTERS_DEFS
167 # include PERSONALITY1_INCLUDE_PRINTERS_DECLS
168 static const struct_printers printers1 = {
169 # include PERSONALITY1_INCLUDE_PRINTERS_DEFS
173 #if SUPPORTED_PERSONALITIES > 2
174 static const char *const errnoent2[] = {
175 # include "errnoent2.h"
177 static const char *const signalent2[] = {
178 # include "signalent2.h"
180 static const struct_ioctlent ioctlent2[] = {
181 # include "ioctlent2.h"
183 # include PERSONALITY2_INCLUDE_PRINTERS_DECLS
184 static const struct_printers printers2 = {
185 # include PERSONALITY2_INCLUDE_PRINTERS_DEFS
190 nsyscalls0 = ARRAY_SIZE(sysent0)
191 #if SUPPORTED_PERSONALITIES > 1
192 , nsyscalls1 = ARRAY_SIZE(sysent1)
193 # if SUPPORTED_PERSONALITIES > 2
194 , nsyscalls2 = ARRAY_SIZE(sysent2)
200 nerrnos0 = ARRAY_SIZE(errnoent0)
201 #if SUPPORTED_PERSONALITIES > 1
202 , nerrnos1 = ARRAY_SIZE(errnoent1)
203 # if SUPPORTED_PERSONALITIES > 2
204 , nerrnos2 = ARRAY_SIZE(errnoent2)
210 nsignals0 = ARRAY_SIZE(signalent0)
211 #if SUPPORTED_PERSONALITIES > 1
212 , nsignals1 = ARRAY_SIZE(signalent1)
213 # if SUPPORTED_PERSONALITIES > 2
214 , nsignals2 = ARRAY_SIZE(signalent2)
220 nioctlents0 = ARRAY_SIZE(ioctlent0)
221 #if SUPPORTED_PERSONALITIES > 1
222 , nioctlents1 = ARRAY_SIZE(ioctlent1)
223 # if SUPPORTED_PERSONALITIES > 2
224 , nioctlents2 = ARRAY_SIZE(ioctlent2)
229 #if SUPPORTED_PERSONALITIES > 1
230 const struct_sysent *sysent = sysent0;
231 const char *const *errnoent = errnoent0;
232 const char *const *signalent = signalent0;
233 const struct_ioctlent *ioctlent = ioctlent0;
234 const struct_printers *printers = &printers0;
237 unsigned nsyscalls = nsyscalls0;
238 unsigned nerrnos = nerrnos0;
239 unsigned nsignals = nsignals0;
240 unsigned nioctlents = nioctlents0;
242 const unsigned int nsyscall_vec[SUPPORTED_PERSONALITIES] = {
244 #if SUPPORTED_PERSONALITIES > 1
247 #if SUPPORTED_PERSONALITIES > 2
251 const struct_sysent *const sysent_vec[SUPPORTED_PERSONALITIES] = {
253 #if SUPPORTED_PERSONALITIES > 1
256 #if SUPPORTED_PERSONALITIES > 2
261 #if SUPPORTED_PERSONALITIES > 1
262 unsigned current_personality;
264 # ifndef current_wordsize
265 unsigned current_wordsize;
266 static const int personality_wordsize[SUPPORTED_PERSONALITIES] = {
267 PERSONALITY0_WORDSIZE,
268 PERSONALITY1_WORDSIZE,
269 # if SUPPORTED_PERSONALITIES > 2
270 PERSONALITY2_WORDSIZE,
275 # ifndef current_klongsize
276 unsigned current_klongsize;
277 static const int personality_klongsize[SUPPORTED_PERSONALITIES] = {
278 PERSONALITY0_KLONGSIZE,
279 PERSONALITY1_KLONGSIZE,
280 # if SUPPORTED_PERSONALITIES > 2
281 PERSONALITY2_KLONGSIZE,
287 set_personality(int personality)
289 nsyscalls = nsyscall_vec[personality];
290 sysent = sysent_vec[personality];
292 switch (personality) {
294 errnoent = errnoent0;
296 ioctlent = ioctlent0;
297 nioctlents = nioctlents0;
298 signalent = signalent0;
299 nsignals = nsignals0;
300 printers = &printers0;
304 errnoent = errnoent1;
306 ioctlent = ioctlent1;
307 nioctlents = nioctlents1;
308 signalent = signalent1;
309 nsignals = nsignals1;
310 printers = &printers1;
313 # if SUPPORTED_PERSONALITIES > 2
315 errnoent = errnoent2;
317 ioctlent = ioctlent2;
318 nioctlents = nioctlents2;
319 signalent = signalent2;
320 nsignals = nsignals2;
321 printers = &printers2;
326 current_personality = personality;
327 # ifndef current_wordsize
328 current_wordsize = personality_wordsize[personality];
330 # ifndef current_klongsize
331 current_klongsize = personality_klongsize[personality];
336 update_personality(struct tcb *tcp, unsigned int personality)
338 if (personality == current_personality)
340 set_personality(personality);
342 if (personality == tcp->currpers)
344 tcp->currpers = personality;
346 # undef PERSONALITY_NAMES
347 # if defined POWERPC64
348 # define PERSONALITY_NAMES {"64 bit", "32 bit"}
349 # elif defined X86_64
350 # define PERSONALITY_NAMES {"64 bit", "32 bit", "x32"}
352 # define PERSONALITY_NAMES {"x32", "32 bit"}
353 # elif defined AARCH64
354 # define PERSONALITY_NAMES {"64 bit", "32 bit"}
356 # define PERSONALITY_NAMES {"64-bit", "32-bit"}
358 # ifdef PERSONALITY_NAMES
360 static const char *const names[] = PERSONALITY_NAMES;
361 error_msg("[ Process PID=%d runs in %s mode. ]",
362 tcp->pid, names[personality]);
368 #ifdef SYS_socket_subcall
370 decode_socket_subcall(struct tcb *tcp)
372 const int call = tcp->u_arg[0];
374 if (call < 1 || call >= SYS_socket_nsubcalls)
377 const kernel_ulong_t scno = SYS_socket_subcall + call;
378 const unsigned int nargs = sysent[scno].nargs;
381 if (umoven(tcp, tcp->u_arg[1], nargs * current_wordsize, buf) < 0)
385 tcp->qual_flg = qual_flags(scno);
386 tcp->s_ent = &sysent[scno];
389 for (i = 0; i < nargs; ++i)
390 tcp->u_arg[i] = (sizeof(uint32_t) == current_wordsize)
391 ? ((uint32_t *) (void *) buf)[i] : buf[i];
395 #ifdef SYS_ipc_subcall
397 decode_ipc_subcall(struct tcb *tcp)
399 unsigned int call = tcp->u_arg[0];
400 const unsigned int version = call >> 16;
403 # if defined S390 || defined S390X
407 if (current_wordsize == 8)
410 set_tcb_priv_ulong(tcp, version);
416 case 1: case 2: case 3: case 4:
417 case 11: case 12: case 13: case 14:
418 case 21: case 22: case 23: case 24:
424 tcp->scno = SYS_ipc_subcall + call;
425 tcp->qual_flg = qual_flags(tcp->scno);
426 tcp->s_ent = &sysent[tcp->scno];
428 const unsigned int n = tcp->s_ent->nargs;
430 for (i = 0; i < n; i++)
431 tcp->u_arg[i] = tcp->u_arg[i + 1];
437 decode_mips_subcall(struct tcb *tcp)
439 if (!scno_is_valid(tcp->u_arg[0]))
441 tcp->scno = tcp->u_arg[0];
442 tcp->qual_flg = qual_flags(tcp->scno);
443 tcp->s_ent = &sysent[tcp->scno];
444 memmove(&tcp->u_arg[0], &tcp->u_arg[1],
445 sizeof(tcp->u_arg) - sizeof(tcp->u_arg[0]));
447 * Fetching the last arg of 7-arg syscalls (fadvise64_64
448 * and sync_file_range) requires additional code,
449 * see linux/mips/get_syscall_args.c
451 if (tcp->s_ent->nargs == MAX_ARGS) {
453 mips_REG_SP + MAX_ARGS * sizeof(tcp->u_arg[0]),
454 sizeof(tcp->u_arg[0]),
455 &tcp->u_arg[MAX_ARGS - 1]) < 0)
456 tcp->u_arg[MAX_ARGS - 1] = 0;
459 #endif /* LINUX_MIPSO32 */
462 dumpio(struct tcb *tcp)
467 int fd = tcp->u_arg[0];
471 if (is_number_in_set(fd, &read_set)) {
472 switch (tcp->s_ent->sen) {
477 case SEN_mq_timedreceive:
478 dumpstr(tcp, tcp->u_arg[1], tcp->u_rval);
483 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1],
487 dumpiov_in_msghdr(tcp, tcp->u_arg[1], tcp->u_rval);
490 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
494 if (is_number_in_set(fd, &write_set)) {
495 switch (tcp->s_ent->sen) {
500 case SEN_mq_timedsend:
501 dumpstr(tcp, tcp->u_arg[1], tcp->u_arg[2]);
507 dumpiov_upto(tcp, tcp->u_arg[2], tcp->u_arg[1], -1);
510 dumpiov_in_msghdr(tcp, tcp->u_arg[1], -1);
513 dumpiov_in_mmsghdr(tcp, tcp->u_arg[1]);
520 * Shuffle syscall numbers so that we don't have huge gaps in syscall table.
521 * The shuffling should be an involution: shuffle_scno(shuffle_scno(n)) == n.
523 static kernel_ulong_t
524 shuffle_scno(kernel_ulong_t scno)
526 #if defined(ARM) || defined(AARCH64) /* So far only 32-bit ARM needs this */
527 if (scno < ARM_FIRST_SHUFFLED_SYSCALL)
530 /* __ARM_NR_cmpxchg? Swap with LAST_ORDINARY+1 */
531 if (scno == ARM_FIRST_SHUFFLED_SYSCALL)
533 if (scno == 0x000ffff0)
534 return ARM_FIRST_SHUFFLED_SYSCALL;
536 # define ARM_SECOND_SHUFFLED_SYSCALL (ARM_FIRST_SHUFFLED_SYSCALL + 1)
538 * Is it ARM specific syscall?
539 * Swap [0x000f0000, 0x000f0000 + LAST_SPECIAL] range
540 * with [SECOND_SHUFFLED, SECOND_SHUFFLED + LAST_SPECIAL] range.
542 if (scno >= 0x000f0000 &&
543 scno <= 0x000f0000 + ARM_LAST_SPECIAL_SYSCALL) {
544 return scno - 0x000f0000 + ARM_SECOND_SHUFFLED_SYSCALL;
546 if (scno <= ARM_SECOND_SHUFFLED_SYSCALL + ARM_LAST_SPECIAL_SYSCALL) {
547 return scno + 0x000f0000 - ARM_SECOND_SHUFFLED_SYSCALL;
549 #endif /* ARM || AARCH64 */
555 err_name(unsigned long err)
557 if ((err < nerrnos) && errnoent[err])
558 return errnoent[err];
563 static long get_regs_error;
571 static void get_regs(pid_t pid);
572 static int get_syscall_args(struct tcb *);
573 static int get_syscall_result(struct tcb *);
574 static int arch_get_scno(struct tcb *tcp);
575 static int arch_set_scno(struct tcb *, kernel_ulong_t);
576 static void get_error(struct tcb *, const bool);
577 static int arch_set_error(struct tcb *);
578 static int arch_set_success(struct tcb *);
580 struct inject_opts *inject_vec[SUPPORTED_PERSONALITIES];
582 static struct inject_opts *
583 tcb_inject_opts(struct tcb *tcp)
585 return (scno_in_range(tcp->scno) && tcp->inject_vec[current_personality])
586 ? &tcp->inject_vec[current_personality][tcp->scno] : NULL;
591 tamper_with_syscall_entering(struct tcb *tcp, unsigned int *signo)
593 if (!tcp->inject_vec[current_personality]) {
594 tcp->inject_vec[current_personality] =
595 xcalloc(nsyscalls, sizeof(**inject_vec));
596 memcpy(tcp->inject_vec[current_personality],
597 inject_vec[current_personality],
598 nsyscalls * sizeof(**inject_vec));
601 struct inject_opts *opts = tcb_inject_opts(tcp);
603 if (!opts || opts->first == 0)
608 if (opts->first != 0)
611 opts->first = opts->step;
614 *signo = opts->signo;
615 if (opts->rval != INJECT_OPTS_RVAL_DEFAULT && !arch_set_scno(tcp, -1))
616 tcp->flags |= TCB_TAMPERED;
622 tamper_with_syscall_exiting(struct tcb *tcp)
624 struct inject_opts *opts = tcb_inject_opts(tcp);
629 if (opts->rval >= 0) {
630 kernel_long_t u_rval = tcp->u_rval;
632 tcp->u_rval = opts->rval;
633 if (arch_set_success(tcp)) {
634 tcp->u_rval = u_rval;
639 unsigned long new_error = -opts->rval;
641 if (new_error != tcp->u_error && new_error <= MAX_ERRNO_VALUE) {
642 unsigned long u_error = tcp->u_error;
644 tcp->u_error = new_error;
645 if (arch_set_error(tcp)) {
646 tcp->u_error = u_error;
655 trace_syscall_entering(struct tcb *tcp, unsigned int *sig)
657 int res = get_scno(tcp);
663 res = get_syscall_args(tcp);
667 tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
669 * " <unavailable>" will be added later by the code which
670 * detects ptrace errors.
676 if (SEN_syscall == tcp->s_ent->sen)
677 decode_mips_subcall(tcp);
680 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
681 switch (tcp->s_ent->sen) {
682 # ifdef SYS_socket_subcall
684 decode_socket_subcall(tcp);
687 # ifdef SYS_ipc_subcall
689 decode_ipc_subcall(tcp);
695 /* Restrain from fault injection while the trace executes strace code. */
697 tcp->qual_flg &= ~QUAL_INJECT;
700 switch (tcp->s_ent->sen) {
703 #if defined SPARC || defined SPARC64
706 tcp->flags &= ~TCB_HIDE_LOG;
710 if (!(tcp->qual_flg & QUAL_TRACE)
711 || (tracing_paths && !pathtrace_match(tcp))
713 tcp->flags |= TCB_INSYSCALL | TCB_FILTERED;
714 tcp->sys_func_rval = 0;
718 tcp->flags &= ~TCB_FILTERED;
725 if (tcp->qual_flg & QUAL_INJECT)
726 tamper_with_syscall_entering(tcp, sig);
728 if (cflag == CFLAG_ONLY_STATS) {
734 if (stack_trace_enabled) {
735 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
736 unwind_capture_stacktrace(tcp);
741 tprintf("%s(", tcp->s_ent->sys_name);
742 if (tcp->qual_flg & QUAL_RAW)
743 res = printargs(tcp);
745 res = tcp->s_ent->sys_func(tcp);
749 tcp->flags |= TCB_INSYSCALL;
750 tcp->sys_func_rval = res;
751 /* Measure the entrance time as late as possible to avoid errors. */
753 gettimeofday(&tcp->etime, NULL);
758 syscall_tampered(struct tcb *tcp)
760 return tcp->flags & TCB_TAMPERED;
764 trace_syscall_exiting(struct tcb *tcp)
768 /* Measure the exit time as early as possible to avoid errors. */
769 if ((Tflag || cflag) && !(filtered(tcp) || hide_log(tcp)))
770 gettimeofday(&tv, NULL);
773 if (stack_trace_enabled) {
774 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
775 unwind_cache_invalidate(tcp);
779 if (filtered(tcp) || hide_log(tcp))
783 #if SUPPORTED_PERSONALITIES > 1
784 update_personality(tcp, tcp->currpers);
786 int res = (get_regs_error ? -1 : get_syscall_result(tcp));
788 if (syserror(tcp) && syscall_tampered(tcp))
789 tamper_with_syscall_exiting(tcp);
792 count_syscall(tcp, &tv);
793 if (cflag == CFLAG_ONLY_STATS) {
798 /* If not in -ff mode, and printing_tcp != tcp,
799 * then the log currently does not end with output
800 * of _our syscall entry_, but with something else.
801 * We need to say which syscall's return is this.
803 * Forced reprinting via TCB_REPRINT is used only by
804 * "strace -ff -oLOG test/threaded_execve" corner case.
805 * It's the only case when -ff mode needs reprinting.
807 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
808 tcp->flags &= ~TCB_REPRINT;
810 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
814 tcp->s_prev_ent = NULL;
816 /* There was error in one of prior ptrace ops */
819 tprints("= ? <unavailable>\n");
821 tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED);
822 tcp->sys_func_rval = 0;
823 free_tcb_priv_data(tcp);
826 tcp->s_prev_ent = tcp->s_ent;
829 if (tcp->qual_flg & QUAL_RAW) {
830 /* sys_res = printargs(tcp); - but it's nop on sysexit */
832 /* FIXME: not_failing_only (IOW, option -z) is broken:
833 * failure of syscall is known only after syscall return.
834 * Thus we end up with something like this on, say, ENOENT:
835 * open("doesnt_exist", O_RDONLY <unfinished ...>
836 * {next syscall decode}
837 * whereas the intended result is that open(...) line
838 * is not shown at all.
840 if (not_failing_only && tcp->u_error)
841 goto ret; /* ignore failed syscalls */
842 if (tcp->sys_func_rval & RVAL_DECODED)
843 sys_res = tcp->sys_func_rval;
845 sys_res = tcp->s_ent->sys_func(tcp);
850 unsigned long u_error = tcp->u_error;
852 if (tcp->qual_flg & QUAL_RAW) {
854 tprintf("= -1 (errno %lu)", u_error);
856 tprintf("= %#" PRI_klx, tcp->u_rval);
858 if (syscall_tampered(tcp))
859 tprints(" (INJECTED)");
861 else if (!(sys_res & RVAL_NONE) && u_error) {
862 const char *u_error_str;
865 /* Blocked signals do not interrupt any syscalls.
866 * In this case syscalls don't return ERESTARTfoo codes.
868 * Deadly signals set to SIG_DFL interrupt syscalls
869 * and kill the process regardless of which of the codes below
870 * is returned by the interrupted syscall.
871 * In some cases, kernel forces a kernel-generated deadly
872 * signal to be unblocked and set to SIG_DFL (and thus cause
873 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
874 * or SIGILL. (The alternative is to leave process spinning
875 * forever on the faulty instruction - not useful).
877 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
878 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
879 * but kernel will always restart them.
882 /* Most common type of signal-interrupted syscall exit code.
883 * The system call will be restarted with the same arguments
884 * if SA_RESTART is set; otherwise, it will fail with EINTR.
886 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
889 /* Rare. For example, fork() returns this if interrupted.
890 * SA_RESTART is ignored (assumed set): the restart is unconditional.
892 tprints("= ? ERESTARTNOINTR (To be restarted)");
895 /* pause(), rt_sigsuspend() etc use this code.
896 * SA_RESTART is ignored (assumed not set):
897 * syscall won't restart (will return EINTR instead)
898 * even after signal with SA_RESTART set. However,
899 * after SIG_IGN or SIG_DFL signal it will restart
900 * (thus the name "restart only if has no handler").
902 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
904 case ERESTART_RESTARTBLOCK:
905 /* Syscalls like nanosleep(), poll() which can't be
906 * restarted with their original arguments use this
907 * code. Kernel will execute restart_syscall() instead,
908 * which changes arguments before restarting syscall.
909 * SA_RESTART is ignored (assumed not set) similarly
910 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
911 * since restart data is saved in "restart block"
912 * in task struct, and if signal handler uses a syscall
913 * which in turn saves another such restart block,
914 * old data is lost and restart becomes impossible)
916 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
919 u_error_str = err_name(u_error);
921 tprintf("= -1 %s (%s)",
922 u_error_str, strerror(u_error));
924 tprintf("= -1 %lu (%s)",
925 u_error, strerror(u_error));
928 if (syscall_tampered(tcp))
929 tprints(" (INJECTED)");
930 if ((sys_res & RVAL_STR) && tcp->auxstr)
931 tprintf(" (%s)", tcp->auxstr);
934 if (sys_res & RVAL_NONE)
937 switch (sys_res & RVAL_MASK) {
939 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
940 if (current_wordsize < sizeof(tcp->u_rval)) {
942 (unsigned int) tcp->u_rval);
946 tprintf("= %#" PRI_klx, tcp->u_rval);
951 print_numeric_long_umask(tcp->u_rval);
954 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
955 if (current_wordsize < sizeof(tcp->u_rval)) {
957 (unsigned int) tcp->u_rval);
961 tprintf("= %" PRI_klu, tcp->u_rval);
965 tprintf("= %" PRI_kld, tcp->u_rval);
970 printfd(tcp, tcp->u_rval);
973 tprintf("= %" PRI_kld, tcp->u_rval);
976 error_msg("invalid rval format");
980 if ((sys_res & RVAL_STR) && tcp->auxstr)
981 tprintf(" (%s)", tcp->auxstr);
982 if (syscall_tampered(tcp))
983 tprints(" (INJECTED)");
986 tv_sub(&tv, &tv, &tcp->etime);
987 tprintf(" <%ld.%06ld>",
988 (long) tv.tv_sec, (long) tv.tv_usec);
995 if (stack_trace_enabled)
996 unwind_print_stacktrace(tcp);
1000 tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED);
1001 tcp->sys_func_rval = 0;
1002 free_tcb_priv_data(tcp);
1007 trace_syscall(struct tcb *tcp, unsigned int *signo)
1009 return exiting(tcp) ?
1010 trace_syscall_exiting(tcp) : trace_syscall_entering(tcp, signo);
1014 is_erestart(struct tcb *tcp)
1016 switch (tcp->u_error) {
1018 case ERESTARTNOINTR:
1019 case ERESTARTNOHAND:
1020 case ERESTART_RESTARTBLOCK:
1027 static unsigned long saved_u_error;
1030 temporarily_clear_syserror(struct tcb *tcp)
1032 saved_u_error = tcp->u_error;
1037 restore_cleared_syserror(struct tcb *tcp)
1039 tcp->u_error = saved_u_error;
1043 * Check the syscall return value register value for whether it is
1044 * a negated errno code indicating an error, or a success return value.
1047 is_negated_errno(kernel_ulong_t val)
1049 /* Linux kernel defines MAX_ERRNO to 4095. */
1050 kernel_ulong_t max = -(kernel_long_t) 4095;
1052 #ifndef current_klongsize
1053 if (current_klongsize < sizeof(val)) {
1054 val = (uint32_t) val;
1055 max = (uint32_t) max;
1057 #endif /* !current_klongsize */
1062 #include "arch_regs.c"
1064 #ifdef HAVE_GETRVAL2
1065 # include "arch_getrval2.c"
1069 print_pc(struct tcb *tcp)
1071 #if defined ARCH_PC_REG
1072 # define ARCH_GET_PC 0
1073 #elif defined ARCH_PC_PEEK_ADDR
1075 # define ARCH_PC_REG pc
1076 # define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
1078 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1081 if (get_regs_error || ARCH_GET_PC)
1082 tprints(current_wordsize == 4 ? "[????????] "
1083 : "[????????????????] ");
1085 tprintf(current_wordsize == 4
1086 ? "[%08" PRI_klx "] " : "[%016" PRI_klx "] ",
1087 (kernel_ulong_t) ARCH_PC_REG);
1090 #include "getregs_old.h"
1092 #undef ptrace_getregset_or_getregs
1093 #undef ptrace_setregset_or_setregs
1094 #ifdef ARCH_REGS_FOR_GETREGSET
1096 # define ptrace_getregset_or_getregs ptrace_getregset
1098 ptrace_getregset(pid_t pid)
1100 # ifdef ARCH_IOVEC_FOR_GETREGSET
1101 /* variable iovec */
1102 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1103 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1104 &ARCH_IOVEC_FOR_GETREGSET);
1106 /* constant iovec */
1107 static struct iovec io = {
1108 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1109 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1111 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1116 # ifndef HAVE_GETREGS_OLD
1117 # define ptrace_setregset_or_setregs ptrace_setregset
1119 ptrace_setregset(pid_t pid)
1121 # ifdef ARCH_IOVEC_FOR_GETREGSET
1122 /* variable iovec */
1123 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1124 &ARCH_IOVEC_FOR_GETREGSET);
1126 /* constant iovec */
1127 static struct iovec io = {
1128 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1129 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1131 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1134 # endif /* !HAVE_GETREGS_OLD */
1136 #elif defined ARCH_REGS_FOR_GETREGS
1138 # define ptrace_getregset_or_getregs ptrace_getregs
1140 ptrace_getregs(pid_t pid)
1142 # if defined SPARC || defined SPARC64
1143 /* SPARC systems have the meaning of data and addr reversed */
1144 return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1146 return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1150 # ifndef HAVE_GETREGS_OLD
1151 # define ptrace_setregset_or_setregs ptrace_setregs
1153 ptrace_setregs(pid_t pid)
1155 # if defined SPARC || defined SPARC64
1156 /* SPARC systems have the meaning of data and addr reversed */
1157 return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1159 return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1162 # endif /* !HAVE_GETREGS_OLD */
1164 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1169 #undef USE_GET_SYSCALL_RESULT_REGS
1170 #ifdef ptrace_getregset_or_getregs
1172 if (get_regs_error != -1)
1175 # ifdef HAVE_GETREGS_OLD
1177 * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1178 * fallback to getregs_old.
1180 static int use_getregs_old;
1181 if (use_getregs_old < 0) {
1182 get_regs_error = ptrace_getregset_or_getregs(pid);
1184 } else if (use_getregs_old == 0) {
1185 get_regs_error = ptrace_getregset_or_getregs(pid);
1186 if (get_regs_error >= 0) {
1187 use_getregs_old = -1;
1190 if (errno == EPERM || errno == ESRCH)
1192 use_getregs_old = 1;
1194 get_regs_error = getregs_old(pid);
1195 # else /* !HAVE_GETREGS_OLD */
1196 /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1197 get_regs_error = ptrace_getregset_or_getregs(pid);
1198 # endif /* !HAVE_GETREGS_OLD */
1200 #else /* !ptrace_getregset_or_getregs */
1202 # define USE_GET_SYSCALL_RESULT_REGS 1
1203 # warning get_regs is not implemented for this architecture yet
1206 #endif /* !ptrace_getregset_or_getregs */
1209 #ifdef ptrace_setregset_or_setregs
1213 return ptrace_setregset_or_setregs(pid);
1215 #endif /* ptrace_setregset_or_setregs */
1220 char buf[sizeof("syscall_%lu") + sizeof(kernel_ulong_t) * 3];
1224 free_sysent_buf(void *ptr)
1226 struct sysent_buf *s = ptr;
1227 s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1233 * 0: "ignore this ptrace stop", bail out of trace_syscall_entering() silently.
1234 * 1: ok, continue in trace_syscall_entering().
1235 * other: error, trace_syscall_entering() should print error indicator
1236 * ("????" etc) and bail out.
1239 get_scno(struct tcb *tcp)
1246 int rc = arch_get_scno(tcp);
1250 if (scno_is_valid(tcp->scno)) {
1251 tcp->s_ent = &sysent[tcp->scno];
1252 tcp->qual_flg = qual_flags(tcp->scno);
1254 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1257 s->ent.nargs = MAX_ARGS;
1258 s->ent.sen = SEN_printargs;
1259 s->ent.sys_func = printargs;
1260 s->ent.sys_name = s->buf;
1261 sprintf(s->buf, "syscall_%" PRI_klu, shuffle_scno(tcp->scno));
1263 tcp->s_ent = &s->ent;
1264 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1266 set_tcb_priv_data(tcp, s, free_sysent_buf);
1269 error_msg("pid %d invalid syscall %" PRI_kld,
1270 tcp->pid, tcp->scno);
1275 #ifdef USE_GET_SYSCALL_RESULT_REGS
1276 static int get_syscall_result_regs(struct tcb *);
1280 * 1: ok, continue in trace_syscall_exiting().
1281 * -1: error, trace_syscall_exiting() should print error indicator
1282 * ("????" etc) and bail out.
1285 get_syscall_result(struct tcb *tcp)
1287 #ifdef USE_GET_SYSCALL_RESULT_REGS
1288 if (get_syscall_result_regs(tcp))
1292 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1297 #include "get_scno.c"
1298 #include "set_scno.c"
1299 #include "get_syscall_args.c"
1300 #ifdef USE_GET_SYSCALL_RESULT_REGS
1301 # include "get_syscall_result.c"
1303 #include "get_error.c"
1304 #include "set_error.c"
1305 #ifdef HAVE_GETREGS_OLD
1306 # include "getregs_old.c"
1310 syscall_name(kernel_ulong_t scno)
1312 #if defined X32_PERSONALITY_NUMBER && defined __X32_SYSCALL_BIT
1313 if (current_personality == X32_PERSONALITY_NUMBER)
1314 scno &= ~__X32_SYSCALL_BIT;
1316 return scno_is_valid(scno) ? sysent[scno].sys_name: NULL;