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;
656 * 0: "ignore this ptrace stop", bail out silently.
657 * 1: ok, decoded; call
658 * syscall_entering_finish(tcp, syscall_entering_trace(tcp, ...)).
659 * other: error; call syscall_entering_finish(tcp, res), where res is the value
663 syscall_entering_decode(struct tcb *tcp)
665 int res = get_scno(tcp);
669 if (res != 1 || (res = get_syscall_args(tcp)) != 1) {
671 tprintf("%s(", scno_good == 1 ? tcp->s_ent->sys_name : "????");
673 * " <unavailable>" will be added later by the code which
674 * detects ptrace errors.
680 if (SEN_syscall == tcp->s_ent->sen)
681 decode_mips_subcall(tcp);
684 #if defined(SYS_socket_subcall) || defined(SYS_ipc_subcall)
685 switch (tcp->s_ent->sen) {
686 # ifdef SYS_socket_subcall
688 decode_socket_subcall(tcp);
691 # ifdef SYS_ipc_subcall
693 decode_ipc_subcall(tcp);
703 syscall_entering_trace(struct tcb *tcp, unsigned int *sig)
705 /* Restrain from fault injection while the trace executes strace code. */
707 tcp->qual_flg &= ~QUAL_INJECT;
710 switch (tcp->s_ent->sen) {
713 #if defined SPARC || defined SPARC64
716 tcp->flags &= ~TCB_HIDE_LOG;
720 if (!(tcp->qual_flg & QUAL_TRACE)
721 || (tracing_paths && !pathtrace_match(tcp))
723 tcp->flags |= TCB_FILTERED;
727 tcp->flags &= ~TCB_FILTERED;
733 if (tcp->qual_flg & QUAL_INJECT)
734 tamper_with_syscall_entering(tcp, sig);
736 if (cflag == CFLAG_ONLY_STATS) {
741 if (stack_trace_enabled) {
742 if (tcp->s_ent->sys_flags & STACKTRACE_CAPTURE_ON_ENTER)
743 unwind_capture_stacktrace(tcp);
748 tprintf("%s(", tcp->s_ent->sys_name);
749 int res = (tcp->qual_flg & QUAL_RAW)
750 ? printargs(tcp) : tcp->s_ent->sys_func(tcp);
756 syscall_entering_finish(struct tcb *tcp, int res)
758 tcp->flags |= TCB_INSYSCALL;
759 tcp->sys_func_rval = res;
760 /* Measure the entrance time as late as possible to avoid errors. */
761 if ((Tflag || cflag) && !filtered(tcp))
762 gettimeofday(&tcp->etime, NULL);
766 syscall_tampered(struct tcb *tcp)
768 return tcp->flags & TCB_TAMPERED;
774 * -1: error in one of ptrace ops.
776 * If not 0, call syscall_exiting_trace(tcp, res), where res is the return
777 * value. Anyway, call syscall_exiting_finish(tcp) then.
780 syscall_exiting_decode(struct tcb *tcp, struct timeval *ptv)
782 /* Measure the exit time as early as possible to avoid errors. */
783 if ((Tflag || cflag) && !(filtered(tcp) || hide_log(tcp)))
784 gettimeofday(ptv, NULL);
787 if (stack_trace_enabled) {
788 if (tcp->s_ent->sys_flags & STACKTRACE_INVALIDATE_CACHE)
789 unwind_cache_invalidate(tcp);
793 if (filtered(tcp) || hide_log(tcp))
797 #if SUPPORTED_PERSONALITIES > 1
798 update_personality(tcp, tcp->currpers);
800 return get_regs_error ? -1 : get_syscall_result(tcp);
804 syscall_exiting_trace(struct tcb *tcp, struct timeval tv, int res)
806 if (syserror(tcp) && syscall_tampered(tcp))
807 tamper_with_syscall_exiting(tcp);
810 count_syscall(tcp, &tv);
811 if (cflag == CFLAG_ONLY_STATS) {
816 /* If not in -ff mode, and printing_tcp != tcp,
817 * then the log currently does not end with output
818 * of _our syscall entry_, but with something else.
819 * We need to say which syscall's return is this.
821 * Forced reprinting via TCB_REPRINT is used only by
822 * "strace -ff -oLOG test/threaded_execve" corner case.
823 * It's the only case when -ff mode needs reprinting.
825 if ((followfork < 2 && printing_tcp != tcp) || (tcp->flags & TCB_REPRINT)) {
826 tcp->flags &= ~TCB_REPRINT;
828 tprintf("<... %s resumed> ", tcp->s_ent->sys_name);
832 tcp->s_prev_ent = NULL;
834 /* There was error in one of prior ptrace ops */
837 tprints("= ? <unavailable>\n");
841 tcp->s_prev_ent = tcp->s_ent;
844 if (tcp->qual_flg & QUAL_RAW) {
845 /* sys_res = printargs(tcp); - but it's nop on sysexit */
847 /* FIXME: not_failing_only (IOW, option -z) is broken:
848 * failure of syscall is known only after syscall return.
849 * Thus we end up with something like this on, say, ENOENT:
850 * open("does_not_exist", O_RDONLY <unfinished ...>
851 * {next syscall decode}
852 * whereas the intended result is that open(...) line
853 * is not shown at all.
855 if (not_failing_only && tcp->u_error)
856 return 0; /* ignore failed syscalls */
857 if (tcp->sys_func_rval & RVAL_DECODED)
858 sys_res = tcp->sys_func_rval;
860 sys_res = tcp->s_ent->sys_func(tcp);
865 unsigned long u_error = tcp->u_error;
867 if (tcp->qual_flg & QUAL_RAW) {
869 tprintf("= -1 (errno %lu)", u_error);
871 tprintf("= %#" PRI_klx, tcp->u_rval);
873 if (syscall_tampered(tcp))
874 tprints(" (INJECTED)");
876 else if (!(sys_res & RVAL_NONE) && u_error) {
877 const char *u_error_str;
880 /* Blocked signals do not interrupt any syscalls.
881 * In this case syscalls don't return ERESTARTfoo codes.
883 * Deadly signals set to SIG_DFL interrupt syscalls
884 * and kill the process regardless of which of the codes below
885 * is returned by the interrupted syscall.
886 * In some cases, kernel forces a kernel-generated deadly
887 * signal to be unblocked and set to SIG_DFL (and thus cause
888 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
889 * or SIGILL. (The alternative is to leave process spinning
890 * forever on the faulty instruction - not useful).
892 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
893 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
894 * but kernel will always restart them.
897 /* Most common type of signal-interrupted syscall exit code.
898 * The system call will be restarted with the same arguments
899 * if SA_RESTART is set; otherwise, it will fail with EINTR.
901 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
904 /* Rare. For example, fork() returns this if interrupted.
905 * SA_RESTART is ignored (assumed set): the restart is unconditional.
907 tprints("= ? ERESTARTNOINTR (To be restarted)");
910 /* pause(), rt_sigsuspend() etc use this code.
911 * SA_RESTART is ignored (assumed not set):
912 * syscall won't restart (will return EINTR instead)
913 * even after signal with SA_RESTART set. However,
914 * after SIG_IGN or SIG_DFL signal it will restart
915 * (thus the name "restart only if has no handler").
917 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
919 case ERESTART_RESTARTBLOCK:
920 /* Syscalls like nanosleep(), poll() which can't be
921 * restarted with their original arguments use this
922 * code. Kernel will execute restart_syscall() instead,
923 * which changes arguments before restarting syscall.
924 * SA_RESTART is ignored (assumed not set) similarly
925 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
926 * since restart data is saved in "restart block"
927 * in task struct, and if signal handler uses a syscall
928 * which in turn saves another such restart block,
929 * old data is lost and restart becomes impossible)
931 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
934 u_error_str = err_name(u_error);
936 tprintf("= -1 %s (%s)",
937 u_error_str, strerror(u_error));
939 tprintf("= -1 %lu (%s)",
940 u_error, strerror(u_error));
943 if (syscall_tampered(tcp))
944 tprints(" (INJECTED)");
945 if ((sys_res & RVAL_STR) && tcp->auxstr)
946 tprintf(" (%s)", tcp->auxstr);
949 if (sys_res & RVAL_NONE)
952 switch (sys_res & RVAL_MASK) {
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_klx, tcp->u_rval);
966 print_numeric_long_umask(tcp->u_rval);
969 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
970 if (current_wordsize < sizeof(tcp->u_rval)) {
972 (unsigned int) tcp->u_rval);
976 tprintf("= %" PRI_klu, tcp->u_rval);
980 tprintf("= %" PRI_kld, tcp->u_rval);
985 printfd(tcp, tcp->u_rval);
988 tprintf("= %" PRI_kld, tcp->u_rval);
991 error_msg("invalid rval format");
995 if ((sys_res & RVAL_STR) && tcp->auxstr)
996 tprintf(" (%s)", tcp->auxstr);
997 if (syscall_tampered(tcp))
998 tprints(" (INJECTED)");
1001 tv_sub(&tv, &tv, &tcp->etime);
1002 tprintf(" <%ld.%06ld>",
1003 (long) tv.tv_sec, (long) tv.tv_usec);
1009 #ifdef USE_LIBUNWIND
1010 if (stack_trace_enabled)
1011 unwind_print_stacktrace(tcp);
1017 syscall_exiting_finish(struct tcb *tcp)
1019 tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED);
1020 tcp->sys_func_rval = 0;
1021 free_tcb_priv_data(tcp);
1025 is_erestart(struct tcb *tcp)
1027 switch (tcp->u_error) {
1029 case ERESTARTNOINTR:
1030 case ERESTARTNOHAND:
1031 case ERESTART_RESTARTBLOCK:
1038 static unsigned long saved_u_error;
1041 temporarily_clear_syserror(struct tcb *tcp)
1043 saved_u_error = tcp->u_error;
1048 restore_cleared_syserror(struct tcb *tcp)
1050 tcp->u_error = saved_u_error;
1054 * Check the syscall return value register value for whether it is
1055 * a negated errno code indicating an error, or a success return value.
1058 is_negated_errno(kernel_ulong_t val)
1060 /* Linux kernel defines MAX_ERRNO to 4095. */
1061 kernel_ulong_t max = -(kernel_long_t) 4095;
1063 #ifndef current_klongsize
1064 if (current_klongsize < sizeof(val)) {
1065 val = (uint32_t) val;
1066 max = (uint32_t) max;
1068 #endif /* !current_klongsize */
1073 #include "arch_regs.c"
1075 #ifdef HAVE_GETRVAL2
1076 # include "arch_getrval2.c"
1080 print_pc(struct tcb *tcp)
1082 #if defined ARCH_PC_REG
1083 # define ARCH_GET_PC 0
1084 #elif defined ARCH_PC_PEEK_ADDR
1086 # define ARCH_PC_REG pc
1087 # define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
1089 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1092 if (get_regs_error || ARCH_GET_PC)
1093 tprints(current_wordsize == 4 ? "[????????] "
1094 : "[????????????????] ");
1096 tprintf(current_wordsize == 4
1097 ? "[%08" PRI_klx "] " : "[%016" PRI_klx "] ",
1098 (kernel_ulong_t) ARCH_PC_REG);
1101 #include "getregs_old.h"
1103 #undef ptrace_getregset_or_getregs
1104 #undef ptrace_setregset_or_setregs
1105 #ifdef ARCH_REGS_FOR_GETREGSET
1107 # define ptrace_getregset_or_getregs ptrace_getregset
1109 ptrace_getregset(pid_t pid)
1111 # ifdef ARCH_IOVEC_FOR_GETREGSET
1112 /* variable iovec */
1113 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1114 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1115 &ARCH_IOVEC_FOR_GETREGSET);
1117 /* constant iovec */
1118 static struct iovec io = {
1119 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1120 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1122 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1127 # ifndef HAVE_GETREGS_OLD
1128 # define ptrace_setregset_or_setregs ptrace_setregset
1130 ptrace_setregset(pid_t pid)
1132 # ifdef ARCH_IOVEC_FOR_GETREGSET
1133 /* variable iovec */
1134 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1135 &ARCH_IOVEC_FOR_GETREGSET);
1137 /* constant iovec */
1138 static struct iovec io = {
1139 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1140 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1142 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1145 # endif /* !HAVE_GETREGS_OLD */
1147 #elif defined ARCH_REGS_FOR_GETREGS
1149 # define ptrace_getregset_or_getregs ptrace_getregs
1151 ptrace_getregs(pid_t pid)
1153 # if defined SPARC || defined SPARC64
1154 /* SPARC systems have the meaning of data and addr reversed */
1155 return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1157 return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1161 # ifndef HAVE_GETREGS_OLD
1162 # define ptrace_setregset_or_setregs ptrace_setregs
1164 ptrace_setregs(pid_t pid)
1166 # if defined SPARC || defined SPARC64
1167 /* SPARC systems have the meaning of data and addr reversed */
1168 return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1170 return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1173 # endif /* !HAVE_GETREGS_OLD */
1175 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1180 #undef USE_GET_SYSCALL_RESULT_REGS
1181 #ifdef ptrace_getregset_or_getregs
1183 if (get_regs_error != -1)
1186 # ifdef HAVE_GETREGS_OLD
1188 * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1189 * fallback to getregs_old.
1191 static int use_getregs_old;
1192 if (use_getregs_old < 0) {
1193 get_regs_error = ptrace_getregset_or_getregs(pid);
1195 } else if (use_getregs_old == 0) {
1196 get_regs_error = ptrace_getregset_or_getregs(pid);
1197 if (get_regs_error >= 0) {
1198 use_getregs_old = -1;
1201 if (errno == EPERM || errno == ESRCH)
1203 use_getregs_old = 1;
1205 get_regs_error = getregs_old(pid);
1206 # else /* !HAVE_GETREGS_OLD */
1207 /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1208 get_regs_error = ptrace_getregset_or_getregs(pid);
1209 # endif /* !HAVE_GETREGS_OLD */
1211 #else /* !ptrace_getregset_or_getregs */
1213 # define USE_GET_SYSCALL_RESULT_REGS 1
1214 # warning get_regs is not implemented for this architecture yet
1217 #endif /* !ptrace_getregset_or_getregs */
1220 #ifdef ptrace_setregset_or_setregs
1224 return ptrace_setregset_or_setregs(pid);
1226 #endif /* ptrace_setregset_or_setregs */
1231 char buf[sizeof("syscall_%lu") + sizeof(kernel_ulong_t) * 3];
1235 free_sysent_buf(void *ptr)
1237 struct sysent_buf *s = ptr;
1238 s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1244 * 0: "ignore this ptrace stop", syscall_entering_decode() should return a "bail
1245 * out silently" code.
1246 * 1: ok, continue in syscall_entering_decode().
1247 * other: error, syscall_entering_decode() should print error indicator
1248 * ("????" etc) and return an appropriate code.
1251 get_scno(struct tcb *tcp)
1258 int rc = arch_get_scno(tcp);
1262 if (scno_is_valid(tcp->scno)) {
1263 tcp->s_ent = &sysent[tcp->scno];
1264 tcp->qual_flg = qual_flags(tcp->scno);
1266 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1269 s->ent.nargs = MAX_ARGS;
1270 s->ent.sen = SEN_printargs;
1271 s->ent.sys_func = printargs;
1272 s->ent.sys_name = s->buf;
1273 sprintf(s->buf, "syscall_%" PRI_klu, shuffle_scno(tcp->scno));
1275 tcp->s_ent = &s->ent;
1276 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1278 set_tcb_priv_data(tcp, s, free_sysent_buf);
1281 error_msg("pid %d invalid syscall %" PRI_kld,
1282 tcp->pid, tcp->scno);
1287 #ifdef USE_GET_SYSCALL_RESULT_REGS
1288 static int get_syscall_result_regs(struct tcb *);
1292 * 1: ok, continue in syscall_exiting_trace().
1293 * -1: error, syscall_exiting_trace() should print error indicator
1294 * ("????" etc) and bail out.
1297 get_syscall_result(struct tcb *tcp)
1299 #ifdef USE_GET_SYSCALL_RESULT_REGS
1300 if (get_syscall_result_regs(tcp))
1304 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1309 #include "get_scno.c"
1310 #include "set_scno.c"
1311 #include "get_syscall_args.c"
1312 #ifdef USE_GET_SYSCALL_RESULT_REGS
1313 # include "get_syscall_result.c"
1315 #include "get_error.c"
1316 #include "set_error.c"
1317 #ifdef HAVE_GETREGS_OLD
1318 # include "getregs_old.c"
1322 syscall_name(kernel_ulong_t scno)
1324 #if defined X32_PERSONALITY_NUMBER && defined __X32_SYSCALL_BIT
1325 if (current_personality == X32_PERSONALITY_NUMBER)
1326 scno &= ~__X32_SYSCALL_BIT;
1328 return scno_is_valid(scno) ? sysent[scno].sys_name: NULL;