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)");
875 } else if (!(sys_res & RVAL_NONE) && u_error) {
876 const char *u_error_str;
879 /* Blocked signals do not interrupt any syscalls.
880 * In this case syscalls don't return ERESTARTfoo codes.
882 * Deadly signals set to SIG_DFL interrupt syscalls
883 * and kill the process regardless of which of the codes below
884 * is returned by the interrupted syscall.
885 * In some cases, kernel forces a kernel-generated deadly
886 * signal to be unblocked and set to SIG_DFL (and thus cause
887 * death) if it is blocked or SIG_IGNed: for example, SIGSEGV
888 * or SIGILL. (The alternative is to leave process spinning
889 * forever on the faulty instruction - not useful).
891 * SIG_IGNed signals and non-deadly signals set to SIG_DFL
892 * (for example, SIGCHLD, SIGWINCH) interrupt syscalls,
893 * but kernel will always restart them.
896 /* Most common type of signal-interrupted syscall exit code.
897 * The system call will be restarted with the same arguments
898 * if SA_RESTART is set; otherwise, it will fail with EINTR.
900 tprints("= ? ERESTARTSYS (To be restarted if SA_RESTART is set)");
903 /* Rare. For example, fork() returns this if interrupted.
904 * SA_RESTART is ignored (assumed set): the restart is unconditional.
906 tprints("= ? ERESTARTNOINTR (To be restarted)");
909 /* pause(), rt_sigsuspend() etc use this code.
910 * SA_RESTART is ignored (assumed not set):
911 * syscall won't restart (will return EINTR instead)
912 * even after signal with SA_RESTART set. However,
913 * after SIG_IGN or SIG_DFL signal it will restart
914 * (thus the name "restart only if has no handler").
916 tprints("= ? ERESTARTNOHAND (To be restarted if no handler)");
918 case ERESTART_RESTARTBLOCK:
919 /* Syscalls like nanosleep(), poll() which can't be
920 * restarted with their original arguments use this
921 * code. Kernel will execute restart_syscall() instead,
922 * which changes arguments before restarting syscall.
923 * SA_RESTART is ignored (assumed not set) similarly
924 * to ERESTARTNOHAND. (Kernel can't honor SA_RESTART
925 * since restart data is saved in "restart block"
926 * in task struct, and if signal handler uses a syscall
927 * which in turn saves another such restart block,
928 * old data is lost and restart becomes impossible)
930 tprints("= ? ERESTART_RESTARTBLOCK (Interrupted by signal)");
933 u_error_str = err_name(u_error);
935 tprintf("= -1 %s (%s)",
936 u_error_str, strerror(u_error));
938 tprintf("= -1 %lu (%s)",
939 u_error, strerror(u_error));
942 if (syscall_tampered(tcp))
943 tprints(" (INJECTED)");
944 if ((sys_res & RVAL_STR) && tcp->auxstr)
945 tprintf(" (%s)", tcp->auxstr);
947 if (sys_res & RVAL_NONE)
950 switch (sys_res & RVAL_MASK) {
952 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
953 if (current_wordsize < sizeof(tcp->u_rval)) {
955 (unsigned int) tcp->u_rval);
959 tprintf("= %#" PRI_klx, tcp->u_rval);
964 print_numeric_long_umask(tcp->u_rval);
967 #if ANY_WORDSIZE_LESS_THAN_KERNEL_LONG
968 if (current_wordsize < sizeof(tcp->u_rval)) {
970 (unsigned int) tcp->u_rval);
974 tprintf("= %" PRI_klu, tcp->u_rval);
978 tprintf("= %" PRI_kld, tcp->u_rval);
983 printfd(tcp, tcp->u_rval);
985 tprintf("= %" PRI_kld, tcp->u_rval);
988 error_msg("invalid rval format");
992 if ((sys_res & RVAL_STR) && tcp->auxstr)
993 tprintf(" (%s)", tcp->auxstr);
994 if (syscall_tampered(tcp))
995 tprints(" (INJECTED)");
998 tv_sub(&tv, &tv, &tcp->etime);
999 tprintf(" <%ld.%06ld>",
1000 (long) tv.tv_sec, (long) tv.tv_usec);
1006 #ifdef USE_LIBUNWIND
1007 if (stack_trace_enabled)
1008 unwind_print_stacktrace(tcp);
1014 syscall_exiting_finish(struct tcb *tcp)
1016 tcp->flags &= ~(TCB_INSYSCALL | TCB_TAMPERED);
1017 tcp->sys_func_rval = 0;
1018 free_tcb_priv_data(tcp);
1022 is_erestart(struct tcb *tcp)
1024 switch (tcp->u_error) {
1026 case ERESTARTNOINTR:
1027 case ERESTARTNOHAND:
1028 case ERESTART_RESTARTBLOCK:
1035 static unsigned long saved_u_error;
1038 temporarily_clear_syserror(struct tcb *tcp)
1040 saved_u_error = tcp->u_error;
1045 restore_cleared_syserror(struct tcb *tcp)
1047 tcp->u_error = saved_u_error;
1051 * Check the syscall return value register value for whether it is
1052 * a negated errno code indicating an error, or a success return value.
1055 is_negated_errno(kernel_ulong_t val)
1057 /* Linux kernel defines MAX_ERRNO to 4095. */
1058 kernel_ulong_t max = -(kernel_long_t) 4095;
1060 #ifndef current_klongsize
1061 if (current_klongsize < sizeof(val)) {
1062 val = (uint32_t) val;
1063 max = (uint32_t) max;
1065 #endif /* !current_klongsize */
1070 #include "arch_regs.c"
1072 #ifdef HAVE_GETRVAL2
1073 # include "arch_getrval2.c"
1077 print_pc(struct tcb *tcp)
1079 #if defined ARCH_PC_REG
1080 # define ARCH_GET_PC 0
1081 #elif defined ARCH_PC_PEEK_ADDR
1083 # define ARCH_PC_REG pc
1084 # define ARCH_GET_PC upeek(tcp->pid, ARCH_PC_PEEK_ADDR, &pc)
1086 # error Neither ARCH_PC_REG nor ARCH_PC_PEEK_ADDR is defined
1089 if (get_regs_error || ARCH_GET_PC)
1090 tprints(current_wordsize == 4 ? "[????????] "
1091 : "[????????????????] ");
1093 tprintf(current_wordsize == 4
1094 ? "[%08" PRI_klx "] " : "[%016" PRI_klx "] ",
1095 (kernel_ulong_t) ARCH_PC_REG);
1098 #include "getregs_old.h"
1100 #undef ptrace_getregset_or_getregs
1101 #undef ptrace_setregset_or_setregs
1102 #ifdef ARCH_REGS_FOR_GETREGSET
1104 # define ptrace_getregset_or_getregs ptrace_getregset
1106 ptrace_getregset(pid_t pid)
1108 # ifdef ARCH_IOVEC_FOR_GETREGSET
1109 /* variable iovec */
1110 ARCH_IOVEC_FOR_GETREGSET.iov_len = sizeof(ARCH_REGS_FOR_GETREGSET);
1111 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS,
1112 &ARCH_IOVEC_FOR_GETREGSET);
1114 /* constant iovec */
1115 static struct iovec io = {
1116 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1117 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1119 return ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &io);
1124 # ifndef HAVE_GETREGS_OLD
1125 # define ptrace_setregset_or_setregs ptrace_setregset
1127 ptrace_setregset(pid_t pid)
1129 # ifdef ARCH_IOVEC_FOR_GETREGSET
1130 /* variable iovec */
1131 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS,
1132 &ARCH_IOVEC_FOR_GETREGSET);
1134 /* constant iovec */
1135 static struct iovec io = {
1136 .iov_base = &ARCH_REGS_FOR_GETREGSET,
1137 .iov_len = sizeof(ARCH_REGS_FOR_GETREGSET)
1139 return ptrace(PTRACE_SETREGSET, pid, NT_PRSTATUS, &io);
1142 # endif /* !HAVE_GETREGS_OLD */
1144 #elif defined ARCH_REGS_FOR_GETREGS
1146 # define ptrace_getregset_or_getregs ptrace_getregs
1148 ptrace_getregs(pid_t pid)
1150 # if defined SPARC || defined SPARC64
1151 /* SPARC systems have the meaning of data and addr reversed */
1152 return ptrace(PTRACE_GETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1154 return ptrace(PTRACE_GETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1158 # ifndef HAVE_GETREGS_OLD
1159 # define ptrace_setregset_or_setregs ptrace_setregs
1161 ptrace_setregs(pid_t pid)
1163 # if defined SPARC || defined SPARC64
1164 /* SPARC systems have the meaning of data and addr reversed */
1165 return ptrace(PTRACE_SETREGS, pid, (void *) &ARCH_REGS_FOR_GETREGS, 0);
1167 return ptrace(PTRACE_SETREGS, pid, NULL, &ARCH_REGS_FOR_GETREGS);
1170 # endif /* !HAVE_GETREGS_OLD */
1172 #endif /* ARCH_REGS_FOR_GETREGSET || ARCH_REGS_FOR_GETREGS */
1177 #undef USE_GET_SYSCALL_RESULT_REGS
1178 #ifdef ptrace_getregset_or_getregs
1180 if (get_regs_error != -1)
1183 # ifdef HAVE_GETREGS_OLD
1185 * Try PTRACE_GETREGSET/PTRACE_GETREGS first,
1186 * fallback to getregs_old.
1188 static int use_getregs_old;
1189 if (use_getregs_old < 0) {
1190 get_regs_error = ptrace_getregset_or_getregs(pid);
1192 } else if (use_getregs_old == 0) {
1193 get_regs_error = ptrace_getregset_or_getregs(pid);
1194 if (get_regs_error >= 0) {
1195 use_getregs_old = -1;
1198 if (errno == EPERM || errno == ESRCH)
1200 use_getregs_old = 1;
1202 get_regs_error = getregs_old(pid);
1203 # else /* !HAVE_GETREGS_OLD */
1204 /* Assume that PTRACE_GETREGSET/PTRACE_GETREGS works. */
1205 get_regs_error = ptrace_getregset_or_getregs(pid);
1206 # endif /* !HAVE_GETREGS_OLD */
1208 #else /* !ptrace_getregset_or_getregs */
1210 # define USE_GET_SYSCALL_RESULT_REGS 1
1211 # warning get_regs is not implemented for this architecture yet
1214 #endif /* !ptrace_getregset_or_getregs */
1217 #ifdef ptrace_setregset_or_setregs
1221 return ptrace_setregset_or_setregs(pid);
1223 #endif /* ptrace_setregset_or_setregs */
1228 char buf[sizeof("syscall_%lu") + sizeof(kernel_ulong_t) * 3];
1232 free_sysent_buf(void *ptr)
1234 struct sysent_buf *s = ptr;
1235 s->tcp->s_prev_ent = s->tcp->s_ent = NULL;
1241 * 0: "ignore this ptrace stop", syscall_entering_decode() should return a "bail
1242 * out silently" code.
1243 * 1: ok, continue in syscall_entering_decode().
1244 * other: error, syscall_entering_decode() should print error indicator
1245 * ("????" etc) and return an appropriate code.
1248 get_scno(struct tcb *tcp)
1255 int rc = arch_get_scno(tcp);
1259 if (scno_is_valid(tcp->scno)) {
1260 tcp->s_ent = &sysent[tcp->scno];
1261 tcp->qual_flg = qual_flags(tcp->scno);
1263 struct sysent_buf *s = xcalloc(1, sizeof(*s));
1266 s->ent.nargs = MAX_ARGS;
1267 s->ent.sen = SEN_printargs;
1268 s->ent.sys_func = printargs;
1269 s->ent.sys_name = s->buf;
1270 sprintf(s->buf, "syscall_%" PRI_klu, shuffle_scno(tcp->scno));
1272 tcp->s_ent = &s->ent;
1273 tcp->qual_flg = QUAL_RAW | DEFAULT_QUAL_FLAGS;
1275 set_tcb_priv_data(tcp, s, free_sysent_buf);
1278 error_msg("pid %d invalid syscall %" PRI_kld,
1279 tcp->pid, tcp->scno);
1284 #ifdef USE_GET_SYSCALL_RESULT_REGS
1285 static int get_syscall_result_regs(struct tcb *);
1289 * 1: ok, continue in syscall_exiting_trace().
1290 * -1: error, syscall_exiting_trace() should print error indicator
1291 * ("????" etc) and bail out.
1294 get_syscall_result(struct tcb *tcp)
1296 #ifdef USE_GET_SYSCALL_RESULT_REGS
1297 if (get_syscall_result_regs(tcp))
1301 get_error(tcp, !(tcp->s_ent->sys_flags & SYSCALL_NEVER_FAILS));
1306 #include "get_scno.c"
1307 #include "set_scno.c"
1308 #include "get_syscall_args.c"
1309 #ifdef USE_GET_SYSCALL_RESULT_REGS
1310 # include "get_syscall_result.c"
1312 #include "get_error.c"
1313 #include "set_error.c"
1314 #ifdef HAVE_GETREGS_OLD
1315 # include "getregs_old.c"
1319 syscall_name(kernel_ulong_t scno)
1321 #if defined X32_PERSONALITY_NUMBER && defined __X32_SYSCALL_BIT
1322 if (current_personality == X32_PERSONALITY_NUMBER)
1323 scno &= ~__X32_SYSCALL_BIT;
1325 return scno_is_valid(scno) ? sysent[scno].sys_name : NULL;